Replace new Math_BigInteger with new static.

This commit is contained in:
Andreas Fischer 2014-06-02 19:33:55 +02:00
parent 740422c65f
commit a011596578

View File

@ -384,7 +384,7 @@ class BigInteger
if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) { if (MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL) {
$this->is_negative = false; $this->is_negative = false;
} }
$temp = $this->add(new Math_BigInteger('-1')); $temp = $this->add(new static('-1'));
$this->value = $temp->value; $this->value = $temp->value;
} }
break; break;
@ -411,18 +411,18 @@ class BigInteger
break; break;
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
$x = ( strlen($x) & 1 ) ? '0' . $x : $x; $x = ( strlen($x) & 1 ) ? '0' . $x : $x;
$temp = new Math_BigInteger(pack('H*', $x), 256); $temp = new static(pack('H*', $x), 256);
$this->value = $this->is_negative ? '-' . $temp->value : $temp->value; $this->value = $this->is_negative ? '-' . $temp->value : $temp->value;
$this->is_negative = false; $this->is_negative = false;
break; break;
default: default:
$x = ( strlen($x) & 1 ) ? '0' . $x : $x; $x = ( strlen($x) & 1 ) ? '0' . $x : $x;
$temp = new Math_BigInteger(pack('H*', $x), 256); $temp = new static(pack('H*', $x), 256);
$this->value = $temp->value; $this->value = $temp->value;
} }
if ($is_negative) { if ($is_negative) {
$temp = $this->add(new Math_BigInteger('-1')); $temp = $this->add(new static('-1'));
$this->value = $temp->value; $this->value = $temp->value;
} }
break; break;
@ -443,9 +443,9 @@ class BigInteger
$this->value = $x === '-' ? '0' : (string) $x; $this->value = $x === '-' ? '0' : (string) $x;
break; break;
default: default:
$temp = new Math_BigInteger(); $temp = new static();
$multiplier = new Math_BigInteger(); $multiplier = new static();
$multiplier->value = array(MATH_BIGINTEGER_MAX10); $multiplier->value = array(MATH_BIGINTEGER_MAX10);
if ($x[0] == '-') { if ($x[0] == '-') {
@ -456,7 +456,7 @@ class BigInteger
$x = str_pad($x, strlen($x) + ((MATH_BIGINTEGER_MAX10_LEN - 1) * strlen($x)) % MATH_BIGINTEGER_MAX10_LEN, 0, STR_PAD_LEFT); $x = str_pad($x, strlen($x) + ((MATH_BIGINTEGER_MAX10_LEN - 1) * strlen($x)) % MATH_BIGINTEGER_MAX10_LEN, 0, STR_PAD_LEFT);
while (strlen($x)) { while (strlen($x)) {
$temp = $temp->multiply($multiplier); $temp = $temp->multiply($multiplier);
$temp = $temp->add(new Math_BigInteger($this->_int2bytes(substr($x, 0, MATH_BIGINTEGER_MAX10_LEN)), 256)); $temp = $temp->add(new static($this->_int2bytes(substr($x, 0, MATH_BIGINTEGER_MAX10_LEN)), 256));
$x = substr($x, MATH_BIGINTEGER_MAX10_LEN); $x = substr($x, MATH_BIGINTEGER_MAX10_LEN);
} }
@ -484,7 +484,7 @@ class BigInteger
$str = '-' . $str; $str = '-' . $str;
} }
$temp = new Math_BigInteger($str, 8 * $base); // ie. either -16 or +16 $temp = new static($str, 8 * $base); // ie. either -16 or +16
$this->value = $temp->value; $this->value = $temp->value;
$this->is_negative = $temp->is_negative; $this->is_negative = $temp->is_negative;
@ -517,12 +517,12 @@ class BigInteger
function toBytes($twos_compliment = false) function toBytes($twos_compliment = false)
{ {
if ($twos_compliment) { if ($twos_compliment) {
$comparison = $this->compare(new Math_BigInteger()); $comparison = $this->compare(new static());
if ($comparison == 0) { if ($comparison == 0) {
return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : ''; return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
} }
$temp = $comparison < 0 ? $this->add(new Math_BigInteger(1)) : $this->copy(); $temp = $comparison < 0 ? $this->add(new static(1)) : $this->copy();
$bytes = $temp->toBytes(); $bytes = $temp->toBytes();
if (empty($bytes)) { // eg. if the number we're trying to convert is -1 if (empty($bytes)) { // eg. if the number we're trying to convert is -1
@ -646,7 +646,7 @@ class BigInteger
} }
$result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0'); $result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0');
if ($twos_compliment && $this->compare(new Math_BigInteger()) > 0 && $this->precision <= 0) { if ($twos_compliment && $this->compare(new static()) > 0 && $this->precision <= 0) {
return '0' . $result; return '0' . $result;
} }
@ -689,7 +689,7 @@ class BigInteger
$temp = $this->copy(); $temp = $this->copy();
$temp->is_negative = false; $temp->is_negative = false;
$divisor = new Math_BigInteger(); $divisor = new static();
$divisor->value = array(MATH_BIGINTEGER_MAX10); $divisor->value = array(MATH_BIGINTEGER_MAX10);
$result = ''; $result = '';
while (count($temp->value)) { while (count($temp->value)) {
@ -722,7 +722,7 @@ class BigInteger
*/ */
function copy() function copy()
{ {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = $this->value; $temp->value = $this->value;
$temp->is_negative = $this->is_negative; $temp->is_negative = $this->is_negative;
$temp->generator = $this->generator; $temp->generator = $this->generator;
@ -794,7 +794,7 @@ class BigInteger
*/ */
function __wakeup() function __wakeup()
{ {
$temp = new Math_BigInteger($this->hex, -16); $temp = new static($this->hex, -16);
$this->value = $temp->value; $this->value = $temp->value;
$this->is_negative = $temp->is_negative; $this->is_negative = $temp->is_negative;
if ($this->precision > 0) { if ($this->precision > 0) {
@ -827,12 +827,12 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_add($this->value, $y->value); $temp->value = gmp_add($this->value, $y->value);
return $this->_normalize($temp); return $this->_normalize($temp);
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = bcadd($this->value, $y->value, 0); $temp->value = bcadd($this->value, $y->value, 0);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -840,7 +840,7 @@ class BigInteger
$temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative); $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative);
$result = new Math_BigInteger(); $result = new static();
$result->value = $temp[MATH_BIGINTEGER_VALUE]; $result->value = $temp[MATH_BIGINTEGER_VALUE];
$result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; $result->is_negative = $temp[MATH_BIGINTEGER_SIGN];
@ -956,12 +956,12 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_sub($this->value, $y->value); $temp->value = gmp_sub($this->value, $y->value);
return $this->_normalize($temp); return $this->_normalize($temp);
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = bcsub($this->value, $y->value, 0); $temp->value = bcsub($this->value, $y->value, 0);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -969,7 +969,7 @@ class BigInteger
$temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative); $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative);
$result = new Math_BigInteger(); $result = new static();
$result->value = $temp[MATH_BIGINTEGER_VALUE]; $result->value = $temp[MATH_BIGINTEGER_VALUE];
$result->is_negative = $temp[MATH_BIGINTEGER_SIGN]; $result->is_negative = $temp[MATH_BIGINTEGER_SIGN];
@ -1089,12 +1089,12 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_mul($this->value, $x->value); $temp->value = gmp_mul($this->value, $x->value);
return $this->_normalize($temp); return $this->_normalize($temp);
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = bcmul($this->value, $x->value, 0); $temp->value = bcmul($this->value, $x->value, 0);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -1102,7 +1102,7 @@ class BigInteger
$temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative); $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative);
$product = new Math_BigInteger(); $product = new static();
$product->value = $temp[MATH_BIGINTEGER_VALUE]; $product->value = $temp[MATH_BIGINTEGER_VALUE];
$product->is_negative = $temp[MATH_BIGINTEGER_SIGN]; $product->is_negative = $temp[MATH_BIGINTEGER_SIGN];
@ -1374,8 +1374,8 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$quotient = new Math_BigInteger(); $quotient = new static();
$remainder = new Math_BigInteger(); $remainder = new static();
list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value); list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value);
@ -1385,8 +1385,8 @@ class BigInteger
return array($this->_normalize($quotient), $this->_normalize($remainder)); return array($this->_normalize($quotient), $this->_normalize($remainder));
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
$quotient = new Math_BigInteger(); $quotient = new static();
$remainder = new Math_BigInteger(); $remainder = new static();
$quotient->value = bcdiv($this->value, $y->value, 0); $quotient->value = bcdiv($this->value, $y->value, 0);
$remainder->value = bcmod($this->value, $y->value); $remainder->value = bcmod($this->value, $y->value);
@ -1400,8 +1400,8 @@ class BigInteger
if (count($y->value) == 1) { if (count($y->value) == 1) {
list($q, $r) = $this->_divide_digit($this->value, $y->value[0]); list($q, $r) = $this->_divide_digit($this->value, $y->value[0]);
$quotient = new Math_BigInteger(); $quotient = new static();
$remainder = new Math_BigInteger(); $remainder = new static();
$quotient->value = $q; $quotient->value = $q;
$remainder->value = array($r); $remainder->value = array($r);
$quotient->is_negative = $this->is_negative != $y->is_negative; $quotient->is_negative = $this->is_negative != $y->is_negative;
@ -1410,7 +1410,7 @@ class BigInteger
static $zero; static $zero;
if ( !isset($zero) ) { if ( !isset($zero) ) {
$zero = new Math_BigInteger(); $zero = new static();
} }
$x = $this->copy(); $x = $this->copy();
@ -1424,10 +1424,10 @@ class BigInteger
$diff = $x->compare($y); $diff = $x->compare($y);
if ( !$diff ) { if ( !$diff ) {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = array(1); $temp->value = array(1);
$temp->is_negative = $x_sign != $y_sign; $temp->is_negative = $x_sign != $y_sign;
return array($this->_normalize($temp), $this->_normalize(new Math_BigInteger())); return array($this->_normalize($temp), $this->_normalize(new static()));
} }
if ( $diff < 0 ) { if ( $diff < 0 ) {
@ -1435,7 +1435,7 @@ class BigInteger
if ( $x_sign ) { if ( $x_sign ) {
$x = $y->subtract($x); $x = $y->subtract($x);
} }
return array($this->_normalize(new Math_BigInteger()), $this->_normalize($x)); return array($this->_normalize(new static()), $this->_normalize($x));
} }
// normalize $x and $y as described in HAC 14.23 / 14.24 // normalize $x and $y as described in HAC 14.23 / 14.24
@ -1450,15 +1450,15 @@ class BigInteger
$x_max = count($x->value) - 1; $x_max = count($x->value) - 1;
$y_max = count($y->value) - 1; $y_max = count($y->value) - 1;
$quotient = new Math_BigInteger(); $quotient = new static();
$quotient_value = &$quotient->value; $quotient_value = &$quotient->value;
$quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1); $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1);
static $temp, $lhs, $rhs; static $temp, $lhs, $rhs;
if (!isset($temp)) { if (!isset($temp)) {
$temp = new Math_BigInteger(); $temp = new static();
$lhs = new Math_BigInteger(); $lhs = new static();
$rhs = new Math_BigInteger(); $rhs = new static();
} }
$temp_value = &$temp->value; $temp_value = &$temp->value;
$rhs_value = &$rhs->value; $rhs_value = &$rhs->value;
@ -1609,7 +1609,7 @@ class BigInteger
{ {
$n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs(); $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs();
if ($e->compare(new Math_BigInteger()) < 0) { if ($e->compare(new static()) < 0) {
$e = $e->abs(); $e = $e->abs();
$temp = $this->modInverse($n); $temp = $this->modInverse($n);
@ -1621,13 +1621,13 @@ class BigInteger
} }
if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP ) { if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP ) {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_powm($this->value, $e->value, $n->value); $temp->value = gmp_powm($this->value, $e->value, $n->value);
return $this->_normalize($temp); return $this->_normalize($temp);
} }
if ($this->compare(new Math_BigInteger()) < 0 || $this->compare($n) > 0) { if ($this->compare(new static()) < 0 || $this->compare($n) > 0) {
list(, $temp) = $this->divide($n); list(, $temp) = $this->divide($n);
return $temp->modPow($e, $n); return $temp->modPow($e, $n);
} }
@ -1663,19 +1663,19 @@ class BigInteger
$plaintext = str_pad($this->toBytes(), strlen($n->toBytes(true)) - 1, "\0", STR_PAD_LEFT); $plaintext = str_pad($this->toBytes(), strlen($n->toBytes(true)) - 1, "\0", STR_PAD_LEFT);
if (openssl_public_encrypt($plaintext, $result, $RSAPublicKey, OPENSSL_NO_PADDING)) { if (openssl_public_encrypt($plaintext, $result, $RSAPublicKey, OPENSSL_NO_PADDING)) {
return new Math_BigInteger($result, 256); return new static($result, 256);
} }
} }
if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = bcpowmod($this->value, $e->value, $n->value, 0); $temp->value = bcpowmod($this->value, $e->value, $n->value, 0);
return $this->_normalize($temp); return $this->_normalize($temp);
} }
if ( empty($e->value) ) { if ( empty($e->value) ) {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = array(1); $temp->value = array(1);
return $this->_normalize($temp); return $this->_normalize($temp);
} }
@ -1686,7 +1686,7 @@ class BigInteger
} }
if ( $e->value == array(2) ) { if ( $e->value == array(2) ) {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = $this->_square($this->value); $temp->value = $this->_square($this->value);
list(, $temp) = $temp->divide($n); list(, $temp) = $temp->divide($n);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -1718,11 +1718,11 @@ class BigInteger
$mod1 = $n->copy(); $mod1 = $n->copy();
$mod1->_rshift($j); $mod1->_rshift($j);
$mod2 = new Math_BigInteger(); $mod2 = new static();
$mod2->value = array(1); $mod2->value = array(1);
$mod2->_lshift($j); $mod2->_lshift($j);
$part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new Math_BigInteger(); $part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new static();
$part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2); $part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2);
$y1 = $mod2->modInverse($mod1); $y1 = $mod2->modInverse($mod1);
@ -1826,7 +1826,7 @@ class BigInteger
} }
} }
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = $this->_reduce($result, $n_value, $mode); $temp->value = $this->_reduce($result, $n_value, $mode);
return $temp; return $temp;
@ -1852,15 +1852,15 @@ class BigInteger
case MATH_BIGINTEGER_BARRETT: case MATH_BIGINTEGER_BARRETT:
return $this->_barrett($x, $n); return $this->_barrett($x, $n);
case MATH_BIGINTEGER_POWEROF2: case MATH_BIGINTEGER_POWEROF2:
$lhs = new Math_BigInteger(); $lhs = new static();
$lhs->value = $x; $lhs->value = $x;
$rhs = new Math_BigInteger(); $rhs = new static();
$rhs->value = $n; $rhs->value = $n;
return $x->_mod2($n); return $x->_mod2($n);
case MATH_BIGINTEGER_CLASSIC: case MATH_BIGINTEGER_CLASSIC:
$lhs = new Math_BigInteger(); $lhs = new static();
$lhs->value = $x; $lhs->value = $x;
$rhs = new Math_BigInteger(); $rhs = new static();
$rhs->value = $n; $rhs->value = $n;
list(, $temp) = $lhs->divide($rhs); list(, $temp) = $lhs->divide($rhs);
return $temp->value; return $temp->value;
@ -1940,7 +1940,7 @@ class BigInteger
*/ */
function _mod2($n) function _mod2($n)
{ {
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = array(1); $temp->value = array(1);
return $this->bitwise_and($n->subtract($temp)); return $this->bitwise_and($n->subtract($temp));
} }
@ -1980,8 +1980,8 @@ class BigInteger
// if ($this->_compare($n, $this->_square($m)) >= 0) { // if ($this->_compare($n, $this->_square($m)) >= 0) {
if (count($n) > 2 * $m_length) { if (count($n) > 2 * $m_length) {
$lhs = new Math_BigInteger(); $lhs = new static();
$rhs = new Math_BigInteger(); $rhs = new static();
$lhs->value = $n; $lhs->value = $n;
$rhs->value = $m; $rhs->value = $m;
list(, $temp) = $lhs->divide($rhs); list(, $temp) = $lhs->divide($rhs);
@ -1999,11 +1999,11 @@ class BigInteger
$key = count($cache[MATH_BIGINTEGER_VARIABLE]); $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
$cache[MATH_BIGINTEGER_VARIABLE][] = $m; $cache[MATH_BIGINTEGER_VARIABLE][] = $m;
$lhs = new Math_BigInteger(); $lhs = new static();
$lhs_value = &$lhs->value; $lhs_value = &$lhs->value;
$lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1)); $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1));
$lhs_value[] = 1; $lhs_value[] = 1;
$rhs = new Math_BigInteger(); $rhs = new static();
$rhs->value = $m; $rhs->value = $m;
list($u, $m1) = $lhs->divide($rhs); list($u, $m1) = $lhs->divide($rhs);
@ -2076,8 +2076,8 @@ class BigInteger
$n_length = count($n); $n_length = count($n);
if (count($x) > 2 * $n_length) { if (count($x) > 2 * $n_length) {
$lhs = new Math_BigInteger(); $lhs = new static();
$rhs = new Math_BigInteger(); $rhs = new static();
$lhs->value = $x; $lhs->value = $x;
$rhs->value = $n; $rhs->value = $n;
list(, $temp) = $lhs->divide($rhs); list(, $temp) = $lhs->divide($rhs);
@ -2087,11 +2087,11 @@ class BigInteger
if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) { if ( ($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false ) {
$key = count($cache[MATH_BIGINTEGER_VARIABLE]); $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
$cache[MATH_BIGINTEGER_VARIABLE][] = $n; $cache[MATH_BIGINTEGER_VARIABLE][] = $n;
$lhs = new Math_BigInteger(); $lhs = new static();
$lhs_value = &$lhs->value; $lhs_value = &$lhs->value;
$lhs_value = $this->_array_repeat(0, 2 * $n_length); $lhs_value = $this->_array_repeat(0, 2 * $n_length);
$lhs_value[] = 1; $lhs_value[] = 1;
$rhs = new Math_BigInteger(); $rhs = new static();
$rhs->value = $n; $rhs->value = $n;
list($temp, ) = $lhs->divide($rhs); // m.length list($temp, ) = $lhs->divide($rhs); // m.length
$cache[MATH_BIGINTEGER_DATA][] = $temp->value; $cache[MATH_BIGINTEGER_DATA][] = $temp->value;
@ -2320,9 +2320,9 @@ class BigInteger
*/ */
function _prepMontgomery($x, $n) function _prepMontgomery($x, $n)
{ {
$lhs = new Math_BigInteger(); $lhs = new static();
$lhs->value = array_merge($this->_array_repeat(0, count($n)), $x); $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x);
$rhs = new Math_BigInteger(); $rhs = new static();
$rhs->value = $n; $rhs->value = $n;
list(, $temp) = $lhs->divide($rhs); list(, $temp) = $lhs->divide($rhs);
@ -2397,7 +2397,7 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_invert($this->value, $n->value); $temp->value = gmp_invert($this->value, $n->value);
return ( $temp->value === false ) ? false : $this->_normalize($temp); return ( $temp->value === false ) ? false : $this->_normalize($temp);
@ -2405,8 +2405,8 @@ class BigInteger
static $zero, $one; static $zero, $one;
if (!isset($zero)) { if (!isset($zero)) {
$zero = new Math_BigInteger(); $zero = new static();
$one = new Math_BigInteger(1); $one = new static(1);
} }
// $x mod -$n == $x mod $n. // $x mod -$n == $x mod $n.
@ -2464,9 +2464,9 @@ class BigInteger
extract(gmp_gcdext($this->value, $n->value)); extract(gmp_gcdext($this->value, $n->value));
return array( return array(
'gcd' => $this->_normalize(new Math_BigInteger($g)), 'gcd' => $this->_normalize(new static($g)),
'x' => $this->_normalize(new Math_BigInteger($s)), 'x' => $this->_normalize(new static($s)),
'y' => $this->_normalize(new Math_BigInteger($t)) 'y' => $this->_normalize(new static($t))
); );
case MATH_BIGINTEGER_MODE_BCMATH: case MATH_BIGINTEGER_MODE_BCMATH:
// it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works
@ -2498,15 +2498,15 @@ class BigInteger
} }
return array( return array(
'gcd' => $this->_normalize(new Math_BigInteger($u)), 'gcd' => $this->_normalize(new static($u)),
'x' => $this->_normalize(new Math_BigInteger($a)), 'x' => $this->_normalize(new static($a)),
'y' => $this->_normalize(new Math_BigInteger($b)) 'y' => $this->_normalize(new static($b))
); );
} }
$y = $n->copy(); $y = $n->copy();
$x = $this->copy(); $x = $this->copy();
$g = new Math_BigInteger(); $g = new static();
$g->value = array(1); $g->value = array(1);
while ( !(($x->value[0] & 1)|| ($y->value[0] & 1)) ) { while ( !(($x->value[0] & 1)|| ($y->value[0] & 1)) ) {
@ -2518,10 +2518,10 @@ class BigInteger
$u = $x->copy(); $u = $x->copy();
$v = $y->copy(); $v = $y->copy();
$a = new Math_BigInteger(); $a = new static();
$b = new Math_BigInteger(); $b = new static();
$c = new Math_BigInteger(); $c = new static();
$d = new Math_BigInteger(); $d = new static();
$a->value = $d->value = $g->value = array(1); $a->value = $d->value = $g->value = array(1);
$b->value = $c->value = array(); $b->value = $c->value = array();
@ -2600,7 +2600,7 @@ class BigInteger
*/ */
function abs() function abs()
{ {
$temp = new Math_BigInteger(); $temp = new static();
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
@ -2715,9 +2715,9 @@ class BigInteger
{ {
$this->precision = $bits; $this->precision = $bits;
if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ) { if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ) {
$this->bitmask = new Math_BigInteger(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256); $this->bitmask = new static(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256);
} else { } else {
$this->bitmask = new Math_BigInteger(bcpow('2', $bits, 0)); $this->bitmask = new static(bcpow('2', $bits, 0));
} }
$temp = $this->_normalize($this); $temp = $this->_normalize($this);
@ -2736,7 +2736,7 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_and($this->value, $x->value); $temp->value = gmp_and($this->value, $x->value);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -2749,7 +2749,7 @@ class BigInteger
$left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
$right = str_pad($right, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
return $this->_normalize(new Math_BigInteger($left & $right, 256)); return $this->_normalize(new static($left & $right, 256));
} }
$result = $this->copy(); $result = $this->copy();
@ -2777,7 +2777,7 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_or($this->value, $x->value); $temp->value = gmp_or($this->value, $x->value);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -2790,7 +2790,7 @@ class BigInteger
$left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
$right = str_pad($right, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
return $this->_normalize(new Math_BigInteger($left | $right, 256)); return $this->_normalize(new static($left | $right, 256));
} }
$length = max(count($this->value), count($x->value)); $length = max(count($this->value), count($x->value));
@ -2817,7 +2817,7 @@ class BigInteger
{ {
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
$temp = new Math_BigInteger(); $temp = new static();
$temp->value = gmp_xor($this->value, $x->value); $temp->value = gmp_xor($this->value, $x->value);
return $this->_normalize($temp); return $this->_normalize($temp);
@ -2830,7 +2830,7 @@ class BigInteger
$left = str_pad($left, $length, chr(0), STR_PAD_LEFT); $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
$right = str_pad($right, $length, chr(0), STR_PAD_LEFT); $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
return $this->_normalize(new Math_BigInteger($left ^ $right, 256)); return $this->_normalize(new static($left ^ $right, 256));
} }
$length = max(count($this->value), count($x->value)); $length = max(count($this->value), count($x->value));
@ -2869,7 +2869,7 @@ class BigInteger
$current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8; $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8;
$new_bits = $this->precision - $current_bits; $new_bits = $this->precision - $current_bits;
if ($new_bits <= 0) { if ($new_bits <= 0) {
return $this->_normalize(new Math_BigInteger($temp, 256)); return $this->_normalize(new static($temp, 256));
} }
// generate as many leading 1's as we need to. // generate as many leading 1's as we need to.
@ -2878,7 +2878,7 @@ class BigInteger
$temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT); $temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT);
return $this->_normalize(new Math_BigInteger($leading_ones | $temp, 256)); return $this->_normalize(new static($leading_ones | $temp, 256));
} }
/** /**
@ -2893,7 +2893,7 @@ class BigInteger
*/ */
function bitwise_rightShift($shift) function bitwise_rightShift($shift)
{ {
$temp = new Math_BigInteger(); $temp = new static();
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
@ -2931,7 +2931,7 @@ class BigInteger
*/ */
function bitwise_leftShift($shift) function bitwise_leftShift($shift)
{ {
$temp = new Math_BigInteger(); $temp = new static();
switch ( MATH_BIGINTEGER_MODE ) { switch ( MATH_BIGINTEGER_MODE ) {
case MATH_BIGINTEGER_MODE_GMP: case MATH_BIGINTEGER_MODE_GMP:
@ -2973,7 +2973,7 @@ class BigInteger
if ($this->precision > 0) { if ($this->precision > 0) {
$precision = $this->precision; $precision = $this->precision;
if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) { if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH ) {
$mask = $this->bitmask->subtract(new Math_BigInteger(1)); $mask = $this->bitmask->subtract(new static(1));
$mask = $mask->toBytes(); $mask = $mask->toBytes();
} else { } else {
$mask = $this->bitmask->toBytes(); $mask = $this->bitmask->toBytes();
@ -2995,7 +2995,7 @@ class BigInteger
} }
$left = $this->bitwise_leftShift($shift); $left = $this->bitwise_leftShift($shift);
$left = $left->bitwise_and(new Math_BigInteger($mask, 256)); $left = $left->bitwise_and(new static($mask, 256));
$right = $this->bitwise_rightShift($precision - $shift); $right = $this->bitwise_rightShift($precision - $shift);
$result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right); $result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right);
return $this->_normalize($result); return $this->_normalize($result);
@ -3042,7 +3042,7 @@ class BigInteger
} }
} }
return new Math_BigInteger($random, 256); return new static($random, 256);
} }
/** /**
@ -3056,11 +3056,11 @@ class BigInteger
function random($min = false, $max = false) function random($min = false, $max = false)
{ {
if ($min === false) { if ($min === false) {
$min = new Math_BigInteger(0); $min = new static(0);
} }
if ($max === false) { if ($max === false) {
$max = new Math_BigInteger(0x7FFFFFFF); $max = new static(0x7FFFFFFF);
} }
$compare = $max->compare($min); $compare = $max->compare($min);
@ -3076,7 +3076,7 @@ class BigInteger
static $one; static $one;
if (!isset($one)) { if (!isset($one)) {
$one = new Math_BigInteger(1); $one = new static(1);
} }
$max = $max->subtract($min->subtract($one)); $max = $max->subtract($min->subtract($one));
@ -3097,7 +3097,7 @@ class BigInteger
http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string
*/ */
$random_max = new Math_BigInteger(chr(1) . str_repeat("\0", $size), 256); $random_max = new static(chr(1) . str_repeat("\0", $size), 256);
$random = $this->_random_number_helper($size); $random = $this->_random_number_helper($size);
list($max_multiple) = $random_max->divide($max); list($max_multiple) = $random_max->divide($max);
@ -3133,11 +3133,11 @@ class BigInteger
function randomPrime($min = false, $max = false, $timeout = false) function randomPrime($min = false, $max = false, $timeout = false)
{ {
if ($min === false) { if ($min === false) {
$min = new Math_BigInteger(0); $min = new static(0);
} }
if ($max === false) { if ($max === false) {
$max = new Math_BigInteger(0x7FFFFFFF); $max = new static(0x7FFFFFFF);
} }
$compare = $max->compare($min); $compare = $max->compare($min);
@ -3153,8 +3153,8 @@ class BigInteger
static $one, $two; static $one, $two;
if (!isset($one)) { if (!isset($one)) {
$one = new Math_BigInteger(1); $one = new static(1);
$two = new Math_BigInteger(2); $two = new static(2);
} }
$start = time(); $start = time();
@ -3163,7 +3163,7 @@ class BigInteger
// gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>. // gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>.
if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime') ) { if ( MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime') ) {
$p = new Math_BigInteger(); $p = new static();
$p->value = gmp_nextprime($x->value); $p->value = gmp_nextprime($x->value);
if ($p->compare($max) <= 0) { if ($p->compare($max) <= 0) {
@ -3319,13 +3319,13 @@ class BigInteger
if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) { if ( MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL ) {
for ($i = 0; $i < count($primes); ++$i) { for ($i = 0; $i < count($primes); ++$i) {
$primes[$i] = new Math_BigInteger($primes[$i]); $primes[$i] = new static($primes[$i]);
} }
} }
$zero = new Math_BigInteger(); $zero = new static();
$one = new Math_BigInteger(1); $one = new static(1);
$two = new Math_BigInteger(2); $two = new static(2);
} }
if ($this->equals($one)) { if ($this->equals($one)) {