| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the PHPASN1 library. |
| 4 |
* |
| 5 |
* For the full copyright and license information, please view the LICENSE |
| 6 |
* file that was distributed with this source code. |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace WP_DEFENDER_VENDOR\FG\Utility; |
| 10 |
|
| 11 |
/** |
| 12 |
* Class BigIntegerGmp |
| 13 |
* Integer representation of big numbers using the GMP extension to perform operations. |
| 14 |
* @package WP_DEFENDER_VENDOR\FG\Utility |
| 15 |
* @internal |
| 16 |
*/ |
| 17 |
class BigIntegerGmp extends BigInteger |
| 18 |
{ |
| 19 |
/** |
| 20 |
* Resource handle. |
| 21 |
* @var \GMP |
| 22 |
*/ |
| 23 |
protected $_rh; |
| 24 |
|
| 25 |
public function __clone() |
| 26 |
{ |
| 27 |
$this->_rh = gmp_add($this->_rh, 0); |
| 28 |
} |
| 29 |
|
| 30 |
protected function _fromString($str) |
| 31 |
{ |
| 32 |
$this->_rh = gmp_init($str, 10); |
| 33 |
} |
| 34 |
|
| 35 |
protected function _fromInteger($integer) |
| 36 |
{ |
| 37 |
$this->_rh = gmp_init($integer, 10); |
| 38 |
} |
| 39 |
|
| 40 |
public function __toString() |
| 41 |
{ |
| 42 |
return gmp_strval($this->_rh, 10); |
| 43 |
} |
| 44 |
|
| 45 |
public function toInteger() |
| 46 |
{ |
| 47 |
if ($this->compare(PHP_INT_MAX) > 0 || $this->compare(PHP_INT_MIN) < 0) { |
| 48 |
throw new \OverflowException(sprintf('Can not represent %s as integer.', $this)); |
| 49 |
} |
| 50 |
return gmp_intval($this->_rh); |
| 51 |
} |
| 52 |
|
| 53 |
public function isNegative() |
| 54 |
{ |
| 55 |
return gmp_sign($this->_rh) === -1; |
| 56 |
} |
| 57 |
|
| 58 |
protected function _unwrap($number) |
| 59 |
{ |
| 60 |
if ($number instanceof self) { |
| 61 |
return $number->_rh; |
| 62 |
} |
| 63 |
return $number; |
| 64 |
} |
| 65 |
|
| 66 |
public function compare($number) |
| 67 |
{ |
| 68 |
return gmp_cmp($this->_rh, $this->_unwrap($number)); |
| 69 |
} |
| 70 |
|
| 71 |
public function add($b) |
| 72 |
{ |
| 73 |
$ret = new self(); |
| 74 |
$ret->_rh = gmp_add($this->_rh, $this->_unwrap($b)); |
| 75 |
return $ret; |
| 76 |
} |
| 77 |
|
| 78 |
public function subtract($b) |
| 79 |
{ |
| 80 |
$ret = new self(); |
| 81 |
$ret->_rh = gmp_sub($this->_rh, $this->_unwrap($b)); |
| 82 |
return $ret; |
| 83 |
} |
| 84 |
|
| 85 |
public function multiply($b) |
| 86 |
{ |
| 87 |
$ret = new self(); |
| 88 |
$ret->_rh = gmp_mul($this->_rh, $this->_unwrap($b)); |
| 89 |
return $ret; |
| 90 |
} |
| 91 |
|
| 92 |
public function modulus($b) |
| 93 |
{ |
| 94 |
$ret = new self(); |
| 95 |
$ret->_rh = gmp_mod($this->_rh, $this->_unwrap($b)); |
| 96 |
return $ret; |
| 97 |
} |
| 98 |
|
| 99 |
public function toPower($b) |
| 100 |
{ |
| 101 |
if ($b instanceof self) { |
| 102 |
// gmp_pow accepts just an integer |
| 103 |
if ($b->compare(PHP_INT_MAX) > 0) { |
| 104 |
throw new \UnexpectedValueException('Unable to raise to power greater than PHP_INT_MAX.'); |
| 105 |
} |
| 106 |
$b = gmp_intval($b->_rh); |
| 107 |
} |
| 108 |
$ret = new self(); |
| 109 |
$ret->_rh = gmp_pow($this->_rh, $b); |
| 110 |
return $ret; |
| 111 |
} |
| 112 |
|
| 113 |
public function shiftRight($bits=8) |
| 114 |
{ |
| 115 |
$ret = new self(); |
| 116 |
$ret->_rh = gmp_div($this->_rh, gmp_pow(2, $bits)); |
| 117 |
return $ret; |
| 118 |
} |
| 119 |
|
| 120 |
public function shiftLeft($bits=8) |
| 121 |
{ |
| 122 |
$ret = new self(); |
| 123 |
$ret->_rh = gmp_mul($this->_rh, gmp_pow(2, $bits)); |
| 124 |
return $ret; |
| 125 |
} |
| 126 |
|
| 127 |
public function absoluteValue() |
| 128 |
{ |
| 129 |
$ret = new self(); |
| 130 |
$ret->_rh = gmp_abs($this->_rh); |
| 131 |
return $ret; |
| 132 |
} |
| 133 |
} |
| 134 |
|