PHP RFC: Add BigNum support to OpenSSL extension


The OpenSSL library has long supported arbitrarily large integer math, however we've simply never exposed it. Add a new class `OpenSSL\BigNum` to expose this functionality.


This class will expose similar functionality to the GMP class, however it will do so via instance methods rather than global functions. Like GMP, OpenSSL\BigNum will behave as an immutable, producing new objects from all binary and unary ops.

The following is a psuedo definition of the class:

  namespace OpenSSL;
  class BigNum {
      public function __construct(int|string $initval = 0);
      static public function createFromBinary(string $bin): BigNum;
      public function add(BigNum $val): BigNum;
      public function sub(BigNum $val): BigNum;
      public function mul(BigNum $val): BigNum;
      public function div(BigNum $val): array; /* tuple(BigNum $quotient, BigNum $remainder) */
      public function intdiv(BigNum $val): BigNum;
      public function mod(BigNum $val): BigNum;
      public function pow(BigNum $exp): BigNum;
      public function powmod(BigNum $exp, BigNum $mod): BigNum;
      public function cmp(BigNum $val): int; /* trinary compare */
      public function gcd(BigNum $val): BigNum; /* Greatest Common Divisor */
      public function shr(int $bits): BigNum;
      public function shl(int $bits): BigNum;
      public function toDec(): string;
      public function toHex(): string;
      public function toBin(): string;
      public function __toString() { return $this->toDec(); }
      public function __debugInfo() { return [ 'dec' => $this->toDec(), 'hex' => $this->toHex() ]; }

Additionally, `OpenSSL\BigNum` will support most operator overloads (excluding bitwise operators), producing a new value from the inputs.

Proposed PHP Version(s)

PHP 7.2, with an independent BC module `openssl-bignum` for 7.1 and 7.0

Proposed Voting Choices

Simple 50%+1 Yes/No for including the proposed patch

Patches and Tests

rfc/openssl.bignum.txt · Last modified: 2017/09/22 13:28 (external edit)