PageRenderTime 67ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/php5-3/core/tools/secure/math/BigInteger.class.php

https://bitbucket.org/ronaldobrandini/framework
PHP | 3608 lines | 1922 code | 484 blank | 1202 comment | 336 complexity | 49e1676d28bb691d5ba767dcbc937064 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. namespace core\tools\secure\math;
  3. /**
  4. * Pure-PHP arbitrary precision integer arithmetic library.
  5. *
  6. * Supports base-2, base-10, base-16, and base-256 numbers. Uses the GMP or BCMath extensions, if available,
  7. * and an internal implementation, otherwise.
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * {@internal (all DocBlock comments regarding implementation - such as the one that follows - refer to the
  12. * {@link MATH_BIGINTEGER_MODE_INTERNAL MATH_BIGINTEGER_MODE_INTERNAL} mode)
  13. *
  14. * BigInteger uses base-2**26 to perform operations such as multiplication and division and
  15. * base-2**52 (ie. two base 2**26 digits) to perform addition and subtraction. Because the largest possible
  16. * value when multiplying two base-2**26 numbers together is a base-2**52 number, double precision floating
  17. * point numbers - numbers that should be supported on most hardware and whose significand is 53 bits - are
  18. * used. As a consequence, bitwise operators such as >> and << cannot be used, nor can the modulo operator %,
  19. * which only supports integers. Although this fact will slow this library down, the fact that such a high
  20. * base is being used should more than compensate.
  21. *
  22. * Numbers are stored in {@link http://en.wikipedia.org/wiki/Endianness little endian} format. ie.
  23. * (new BigInteger(pow(2, 26)))->value = array(0, 1)
  24. *
  25. * Useful resources are as follows:
  26. *
  27. * - {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf Handbook of Applied Cryptography (HAC)}
  28. * - {@link http://math.libtomcrypt.com/files/tommath.pdf Multi-Precision Math (MPM)}
  29. * - Java's BigInteger classes. See /j2se/src/share/classes/java/math in jdk-1_5_0-src-jrl.zip
  30. *
  31. * Here's an example of how to use this library:
  32. * <code>
  33. * <?php
  34. * include 'Math/BigInteger.php';
  35. *
  36. * $a = new BigInteger(2);
  37. * $b = new BigInteger(3);
  38. *
  39. * $c = $a->add($b);
  40. *
  41. * echo $c->toString(); // outputs 5
  42. * ?>
  43. * </code>
  44. *
  45. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  46. * of this software and associated documentation files (the "Software"), to deal
  47. * in the Software without restriction, including without limitation the rights
  48. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  49. * copies of the Software, and to permit persons to whom the Software is
  50. * furnished to do so, subject to the following conditions:
  51. *
  52. * The above copyright notice and this permission notice shall be included in
  53. * all copies or substantial portions of the Software.
  54. *
  55. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  56. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  57. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  58. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  59. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  60. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  61. * THE SOFTWARE.
  62. *
  63. * @category Math
  64. * @package BigInteger
  65. * @author Jim Wigginton <terrafrost@php.net>
  66. * @copyright MMVI Jim Wigginton
  67. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  68. * @link http://pear.php.net/package/BigInteger
  69. */
  70. /* * #@+
  71. * Reduction constants
  72. *
  73. * @access private
  74. * @see BigInteger::_reduce()
  75. */
  76. /**
  77. * Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256
  78. * numbers.
  79. *
  80. * @package BigInteger
  81. * @author Jim Wigginton <terrafrost@php.net>
  82. * @access public
  83. */
  84. class BigInteger{
  85. /**
  86. * Holds the BigInteger's value.
  87. *
  88. * @var Array
  89. * @access private
  90. */
  91. private $value;
  92. /**
  93. * Holds the BigInteger's magnitude.
  94. *
  95. * @var Boolean
  96. * @access private
  97. */
  98. private $is_negative = false;
  99. /**
  100. * Random number generator function
  101. *
  102. * @see setRandomGenerator()
  103. * @access private
  104. */
  105. private $generator = 'mt_rand';
  106. /**
  107. * Precision
  108. *
  109. * @see setPrecision()
  110. * @access private
  111. */
  112. private $precision = -1;
  113. /**
  114. * Precision Bitmask
  115. *
  116. * @see setPrecision()
  117. * @access private
  118. */
  119. private $bitmask = false;
  120. /**
  121. * Mode independent value used for serialization.
  122. *
  123. * If the bcmath or gmp extensions are installed $this->value will be a non-serializable resource, hence the need for
  124. * a variable that'll be serializable regardless of whether or not extensions are being used. Unlike $this->value,
  125. * however, $this->hex is only calculated when $this->__sleep() is called.
  126. *
  127. * @see __sleep()
  128. * @see __wakeup()
  129. * @var String
  130. * @access private
  131. */
  132. private $hex;
  133. /**
  134. * Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.
  135. *
  136. * If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using
  137. * two's compliment. The sole exception to this is -10, which is treated the same as 10 is.
  138. *
  139. * Here's an example:
  140. * <code>
  141. * <?php
  142. * include 'Math/BigInteger.php';
  143. *
  144. * $a = new BigInteger('0x32', 16); // 50 in base-16
  145. *
  146. * echo $a->toString(); // outputs 50
  147. * ?>
  148. * </code>
  149. *
  150. * @param optional $x base-10 number or base-$base number if $base set.
  151. * @param optional integer $base
  152. * @return BigInteger
  153. * @access public
  154. */
  155. public function __construct($x = 0, $base = 10){
  156. if(!defined('MATH_BIGINTEGER_MODE')){
  157. switch(true){
  158. case extension_loaded('gmp'):
  159. define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_GMP);
  160. break;
  161. case extension_loaded('bcmath'):
  162. define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_BCMATH);
  163. break;
  164. default:
  165. define('MATH_BIGINTEGER_MODE', MATH_BIGINTEGER_MODE_INTERNAL);
  166. }
  167. }
  168. if(function_exists('openssl_public_encrypt') && !defined('MATH_BIGINTEGER_OPENSSL_DISABLE') && !defined('MATH_BIGINTEGER_OPENSSL_ENABLED')){
  169. // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work
  170. ob_start();
  171. @phpinfo();
  172. $content = ob_get_contents();
  173. ob_end_clean();
  174. preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches);
  175. $versions = array();
  176. if(!empty($matches[1])){
  177. for($i = 0; $i < count($matches[1]); $i++){
  178. $versions[$matches[1][$i]] = trim(str_replace('=>', '', strip_tags($matches[2][$i])));
  179. }
  180. }
  181. // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+
  182. switch(true){
  183. case!isset($versions['Header']):
  184. case!isset($versions['Library']):
  185. case $versions['Header'] == $versions['Library']:
  186. define('MATH_BIGINTEGER_OPENSSL_ENABLED', true);
  187. break;
  188. default:
  189. define('MATH_BIGINTEGER_OPENSSL_DISABLE', true);
  190. }
  191. }
  192. if(!defined('PHP_INT_SIZE')){
  193. define('PHP_INT_SIZE', 4);
  194. }
  195. if(!defined('MATH_BIGINTEGER_BASE') && MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_INTERNAL){
  196. switch(PHP_INT_SIZE){
  197. case 8: // use 64-bit integers if int size is 8 bytes
  198. define('MATH_BIGINTEGER_BASE', 31);
  199. define('MATH_BIGINTEGER_BASE_FULL', 0x80000000);
  200. define('MATH_BIGINTEGER_MAX_DIGIT', 0x7FFFFFFF);
  201. define('MATH_BIGINTEGER_MSB', 0x40000000);
  202. // 10**9 is the closest we can get to 2**31 without passing it
  203. define('MATH_BIGINTEGER_MAX10', 1000000000);
  204. define('MATH_BIGINTEGER_MAX10_LEN', 9);
  205. // the largest digit that may be used in addition / subtraction
  206. define('MATH_BIGINTEGER_MAX_DIGIT2', pow(2, 62));
  207. break;
  208. //case 4: // use 64-bit floats if int size is 4 bytes
  209. default:
  210. define('MATH_BIGINTEGER_BASE', 26);
  211. define('MATH_BIGINTEGER_BASE_FULL', 0x4000000);
  212. define('MATH_BIGINTEGER_MAX_DIGIT', 0x3FFFFFF);
  213. define('MATH_BIGINTEGER_MSB', 0x2000000);
  214. // 10**7 is the closest to 2**26 without passing it
  215. define('MATH_BIGINTEGER_MAX10', 10000000);
  216. define('MATH_BIGINTEGER_MAX10_LEN', 7);
  217. // the largest digit that may be used in addition / subtraction
  218. // we do pow(2, 52) instead of using 4503599627370496 directly because some
  219. // PHP installations will truncate 4503599627370496.
  220. define('MATH_BIGINTEGER_MAX_DIGIT2', pow(2, 52));
  221. }
  222. }
  223. switch(MATH_BIGINTEGER_MODE){
  224. case MATH_BIGINTEGER_MODE_GMP:
  225. if(is_resource($x) && get_resource_type($x) == 'GMP integer'){
  226. $this->value = $x;
  227. return;
  228. }
  229. $this->value = gmp_init(0);
  230. break;
  231. case MATH_BIGINTEGER_MODE_BCMATH:
  232. $this->value = '0';
  233. break;
  234. default:
  235. $this->value = array();
  236. }
  237. // '0' counts as empty() but when the base is 256 '0' is equal to ord('0') or 48
  238. // '0' is the only value like this per http://php.net/empty
  239. if(empty($x) && (abs($base) != 256 || $x !== '0')){
  240. return;
  241. }
  242. switch($base){
  243. case -256:
  244. if(ord($x[0]) & 0x80){
  245. $x = ~$x;
  246. $this->is_negative = true;
  247. }
  248. case 256:
  249. switch(MATH_BIGINTEGER_MODE){
  250. case MATH_BIGINTEGER_MODE_GMP:
  251. $sign = $this->is_negative ? '-' : '';
  252. $this->value = gmp_init($sign . '0x' . bin2hex($x));
  253. break;
  254. case MATH_BIGINTEGER_MODE_BCMATH:
  255. // round $len to the nearest 4 (thanks, DavidMJ!)
  256. $len = (strlen($x) + 3) & 0xFFFFFFFC;
  257. $x = str_pad($x, $len, chr(0), STR_PAD_LEFT);
  258. for($i = 0; $i < $len; $i+= 4){
  259. $this->value = bcmul($this->value, '4294967296', 0); // 4294967296 == 2**32
  260. $this->value = bcadd($this->value, 0x1000000 * ord($x[$i]) + ((ord($x[$i + 1]) << 16) | (ord($x[$i + 2]) << 8) | ord($x[$i + 3])), 0);
  261. }
  262. if($this->is_negative){
  263. $this->value = '-' . $this->value;
  264. }
  265. break;
  266. // converts a base-2**8 (big endian / msb) number to base-2**26 (little endian / lsb)
  267. default:
  268. while(strlen($x)){
  269. $this->value[] = $this->_bytes2int($this->_base256_rshift($x, MATH_BIGINTEGER_BASE));
  270. }
  271. }
  272. if($this->is_negative){
  273. if(MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL){
  274. $this->is_negative = false;
  275. }
  276. $temp = $this->add(new BigInteger('-1'));
  277. $this->value = $temp->value;
  278. }
  279. break;
  280. case 16:
  281. case -16:
  282. if($base > 0 && $x[0] == '-'){
  283. $this->is_negative = true;
  284. $x = substr($x, 1);
  285. }
  286. $x = preg_replace('#^(?:0x)?([A-Fa-f0-9]*).*#', '$1', $x);
  287. $is_negative = false;
  288. if($base < 0 && hexdec($x[0]) >= 8){
  289. $this->is_negative = $is_negative = true;
  290. $x = bin2hex(~pack('H*', $x));
  291. }
  292. switch(MATH_BIGINTEGER_MODE){
  293. case MATH_BIGINTEGER_MODE_GMP:
  294. $temp = $this->is_negative ? '-0x' . $x : '0x' . $x;
  295. $this->value = gmp_init($temp);
  296. $this->is_negative = false;
  297. break;
  298. case MATH_BIGINTEGER_MODE_BCMATH:
  299. $x = ( strlen($x) & 1 ) ? '0' . $x : $x;
  300. $temp = new BigInteger(pack('H*', $x), 256);
  301. $this->value = $this->is_negative ? '-' . $temp->value : $temp->value;
  302. $this->is_negative = false;
  303. break;
  304. default:
  305. $x = ( strlen($x) & 1 ) ? '0' . $x : $x;
  306. $temp = new BigInteger(pack('H*', $x), 256);
  307. $this->value = $temp->value;
  308. }
  309. if($is_negative){
  310. $temp = $this->add(new BigInteger('-1'));
  311. $this->value = $temp->value;
  312. }
  313. break;
  314. case 10:
  315. case -10:
  316. // (?<!^)(?:-).*: find any -'s that aren't at the beginning and then any characters that follow that
  317. // (?<=^|-)0*: find any 0's that are preceded by the start of the string or by a - (ie. octals)
  318. // [^-0-9].*: find any non-numeric characters and then any characters that follow that
  319. $x = preg_replace('#(?<!^)(?:-).*|(?<=^|-)0*|[^-0-9].*#', '', $x);
  320. switch(MATH_BIGINTEGER_MODE){
  321. case MATH_BIGINTEGER_MODE_GMP:
  322. $this->value = gmp_init($x);
  323. break;
  324. case MATH_BIGINTEGER_MODE_BCMATH:
  325. // explicitly casting $x to a string is necessary, here, since doing $x[0] on -1 yields different
  326. // results then doing it on '-1' does (modInverse does $x[0])
  327. $this->value = $x === '-' ? '0' : (string) $x;
  328. break;
  329. default:
  330. $temp = new BigInteger();
  331. $multiplier = new BigInteger();
  332. $multiplier->value = array(MATH_BIGINTEGER_MAX10);
  333. if($x[0] == '-'){
  334. $this->is_negative = true;
  335. $x = substr($x, 1);
  336. }
  337. $x = str_pad($x, strlen($x) + ((MATH_BIGINTEGER_MAX10_LEN - 1) * strlen($x)) % MATH_BIGINTEGER_MAX10_LEN, 0, STR_PAD_LEFT);
  338. while(strlen($x)){
  339. $temp = $temp->multiply($multiplier);
  340. $temp = $temp->add(new BigInteger($this->_int2bytes(substr($x, 0, MATH_BIGINTEGER_MAX10_LEN)), 256));
  341. $x = substr($x, MATH_BIGINTEGER_MAX10_LEN);
  342. }
  343. $this->value = $temp->value;
  344. }
  345. break;
  346. case 2: // base-2 support originally implemented by Lluis Pamies - thanks!
  347. case -2:
  348. if($base > 0 && $x[0] == '-'){
  349. $this->is_negative = true;
  350. $x = substr($x, 1);
  351. }
  352. $x = preg_replace('#^([01]*).*#', '$1', $x);
  353. $x = str_pad($x, strlen($x) + (3 * strlen($x)) % 4, 0, STR_PAD_LEFT);
  354. $str = '0x';
  355. while(strlen($x)){
  356. $part = substr($x, 0, 4);
  357. $str.= dechex(bindec($part));
  358. $x = substr($x, 4);
  359. }
  360. if($this->is_negative){
  361. $str = '-' . $str;
  362. }
  363. $temp = new BigInteger($str, 8 * $base); // ie. either -16 or +16
  364. $this->value = $temp->value;
  365. $this->is_negative = $temp->is_negative;
  366. break;
  367. default:
  368. // base not supported, so we'll let $this == 0
  369. }
  370. }
  371. /**
  372. * Converts a BigInteger to a byte string (eg. base-256).
  373. *
  374. * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're
  375. * saved as two's compliment.
  376. *
  377. * Here's an example:
  378. * <code>
  379. * <?php
  380. * include 'Math/BigInteger.php';
  381. *
  382. * $a = new BigInteger('65');
  383. *
  384. * echo $a->toBytes(); // outputs chr(65)
  385. * ?>
  386. * </code>
  387. *
  388. * @param Boolean $twos_compliment
  389. * @return String
  390. * @access public
  391. * @internal Converts a base-2**26 number to base-2**8
  392. */
  393. public function toBytes($twos_compliment = false){
  394. if($twos_compliment){
  395. $comparison = $this->compare(new BigInteger());
  396. if($comparison == 0){
  397. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  398. }
  399. $temp = $comparison < 0 ? $this->add(new BigInteger(1)) : $this->copy();
  400. $bytes = $temp->toBytes();
  401. if(empty($bytes)){ // eg. if the number we're trying to convert is -1
  402. $bytes = chr(0);
  403. }
  404. if(ord($bytes[0]) & 0x80){
  405. $bytes = chr(0) . $bytes;
  406. }
  407. return $comparison < 0 ? ~$bytes : $bytes;
  408. }
  409. switch(MATH_BIGINTEGER_MODE){
  410. case MATH_BIGINTEGER_MODE_GMP:
  411. if(gmp_cmp($this->value, gmp_init(0)) == 0){
  412. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  413. }
  414. $temp = gmp_strval(gmp_abs($this->value), 16);
  415. $temp = ( strlen($temp) & 1 ) ? '0' . $temp : $temp;
  416. $temp = pack('H*', $temp);
  417. return $this->precision > 0 ?
  418. substr(str_pad($temp, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) :
  419. ltrim($temp, chr(0));
  420. case MATH_BIGINTEGER_MODE_BCMATH:
  421. if($this->value === '0'){
  422. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  423. }
  424. $value = '';
  425. $current = $this->value;
  426. if($current[0] == '-'){
  427. $current = substr($current, 1);
  428. }
  429. while(bccomp($current, '0', 0) > 0){
  430. $temp = bcmod($current, '16777216');
  431. $value = chr($temp >> 16) . chr($temp >> 8) . chr($temp) . $value;
  432. $current = bcdiv($current, '16777216', 0);
  433. }
  434. return $this->precision > 0 ?
  435. substr(str_pad($value, $this->precision >> 3, chr(0), STR_PAD_LEFT), -($this->precision >> 3)) :
  436. ltrim($value, chr(0));
  437. }
  438. if(!count($this->value)){
  439. return $this->precision > 0 ? str_repeat(chr(0), ($this->precision + 1) >> 3) : '';
  440. }
  441. $result = $this->_int2bytes($this->value[count($this->value) - 1]);
  442. $temp = $this->copy();
  443. for($i = count($temp->value) - 2; $i >= 0; --$i){
  444. $temp->_base256_lshift($result, MATH_BIGINTEGER_BASE);
  445. $result = $result | str_pad($temp->_int2bytes($temp->value[$i]), strlen($result), chr(0), STR_PAD_LEFT);
  446. }
  447. return $this->precision > 0 ?
  448. str_pad(substr($result, -(($this->precision + 7) >> 3)), ($this->precision + 7) >> 3, chr(0), STR_PAD_LEFT) :
  449. $result;
  450. }
  451. /**
  452. * Converts a BigInteger to a hex string (eg. base-16)).
  453. *
  454. * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're
  455. * saved as two's compliment.
  456. *
  457. * Here's an example:
  458. * <code>
  459. * <?php
  460. * include 'Math/BigInteger.php';
  461. *
  462. * $a = new BigInteger('65');
  463. *
  464. * echo $a->toHex(); // outputs '41'
  465. * ?>
  466. * </code>
  467. *
  468. * @param Boolean $twos_compliment
  469. * @return String
  470. * @access public
  471. * @internal Converts a base-2**26 number to base-2**8
  472. */
  473. public function toHex($twos_compliment = false){
  474. return bin2hex($this->toBytes($twos_compliment));
  475. }
  476. /**
  477. * Converts a BigInteger to a bit string (eg. base-2).
  478. *
  479. * Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're
  480. * saved as two's compliment.
  481. *
  482. * Here's an example:
  483. * <code>
  484. * <?php
  485. * include 'Math/BigInteger.php';
  486. *
  487. * $a = new BigInteger('65');
  488. *
  489. * echo $a->toBits(); // outputs '1000001'
  490. * ?>
  491. * </code>
  492. *
  493. * @param Boolean $twos_compliment
  494. * @return String
  495. * @access public
  496. * @internal Converts a base-2**26 number to base-2**2
  497. */
  498. public function toBits($twos_compliment = false){
  499. $hex = $this->toHex($twos_compliment);
  500. $bits = '';
  501. for($i = strlen($hex) - 8, $start = strlen($hex) & 7; $i >= $start; $i-=8){
  502. $bits = str_pad(decbin(hexdec(substr($hex, $i, 8))), 32, '0', STR_PAD_LEFT) . $bits;
  503. }
  504. if($start){ // hexdec('') == 0
  505. $bits = str_pad(decbin(hexdec(substr($hex, 0, $start))), 8, '0', STR_PAD_LEFT) . $bits;
  506. }
  507. $result = $this->precision > 0 ? substr($bits, -$this->precision) : ltrim($bits, '0');
  508. if($twos_compliment && $this->compare(new BigInteger()) > 0 && $this->precision <= 0){
  509. return '0' . $result;
  510. }
  511. return $result;
  512. }
  513. /**
  514. * Converts a BigInteger to a base-10 number.
  515. *
  516. * Here's an example:
  517. * <code>
  518. * <?php
  519. * include 'Math/BigInteger.php';
  520. *
  521. * $a = new BigInteger('50');
  522. *
  523. * echo $a->toString(); // outputs 50
  524. * ?>
  525. * </code>
  526. *
  527. * @return String
  528. * @access public
  529. * @internal Converts a base-2**26 number to base-10**7 (which is pretty much base-10)
  530. */
  531. public function toString(){
  532. switch(MATH_BIGINTEGER_MODE){
  533. case MATH_BIGINTEGER_MODE_GMP:
  534. return gmp_strval($this->value);
  535. case MATH_BIGINTEGER_MODE_BCMATH:
  536. if($this->value === '0'){
  537. return '0';
  538. }
  539. return ltrim($this->value, '0');
  540. }
  541. if(!count($this->value)){
  542. return '0';
  543. }
  544. $temp = $this->copy();
  545. $temp->is_negative = false;
  546. $divisor = new BigInteger();
  547. $divisor->value = array(MATH_BIGINTEGER_MAX10);
  548. $result = '';
  549. while(count($temp->value)){
  550. list($temp, $mod) = $temp->divide($divisor);
  551. $result = str_pad(isset($mod->value[0]) ? $mod->value[0] : '', MATH_BIGINTEGER_MAX10_LEN, '0', STR_PAD_LEFT) . $result;
  552. }
  553. $result = ltrim($result, '0');
  554. if(empty($result)){
  555. $result = '0';
  556. }
  557. if($this->is_negative){
  558. $result = '-' . $result;
  559. }
  560. return $result;
  561. }
  562. /**
  563. * Copy an object
  564. *
  565. * PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee
  566. * that all objects are passed by value, when appropriate. More information can be found here:
  567. *
  568. * {@link http://php.net/language.oop5.basic#51624}
  569. *
  570. * @access public
  571. * @see __clone()
  572. * @return BigInteger
  573. */
  574. public function copy(){
  575. $temp = new BigInteger();
  576. $temp->value = $this->value;
  577. $temp->is_negative = $this->is_negative;
  578. $temp->generator = $this->generator;
  579. $temp->precision = $this->precision;
  580. $temp->bitmask = $this->bitmask;
  581. return $temp;
  582. }
  583. /**
  584. * __toString() magic method
  585. *
  586. * Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call
  587. * toString().
  588. *
  589. * @access public
  590. * @internal Implemented per a suggestion by Techie-Michael - thanks!
  591. */
  592. public function __toString(){
  593. return $this->toString();
  594. }
  595. /**
  596. * __clone() magic method
  597. *
  598. * Although you can call BigInteger::__toString() directly in PHP5, you cannot call BigInteger::__clone()
  599. * directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5
  600. * only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5,
  601. * call BigInteger::copy(), instead.
  602. *
  603. * @access public
  604. * @see copy()
  605. * @return BigInteger
  606. */
  607. public function __clone(){
  608. return $this->copy();
  609. }
  610. /**
  611. * __sleep() magic method
  612. *
  613. * Will be called, automatically, when serialize() is called on a BigInteger object.
  614. *
  615. * @see __wakeup()
  616. * @access public
  617. */
  618. public function __sleep(){
  619. $this->hex = $this->toHex(true);
  620. $vars = array('hex');
  621. if($this->generator != 'mt_rand'){
  622. $vars[] = 'generator';
  623. }
  624. if($this->precision > 0){
  625. $vars[] = 'precision';
  626. }
  627. return $vars;
  628. }
  629. /**
  630. * __wakeup() magic method
  631. *
  632. * Will be called, automatically, when unserialize() is called on a BigInteger object.
  633. *
  634. * @see __sleep()
  635. * @access public
  636. */
  637. public function __wakeup(){
  638. $temp = new BigInteger($this->hex, -16);
  639. $this->value = $temp->value;
  640. $this->is_negative = $temp->is_negative;
  641. $this->setRandomGenerator($this->generator);
  642. if($this->precision > 0){
  643. // recalculate $this->bitmask
  644. $this->setPrecision($this->precision);
  645. }
  646. }
  647. /**
  648. * Adds two BigIntegers.
  649. *
  650. * Here's an example:
  651. * <code>
  652. * <?php
  653. * include 'Math/BigInteger.php';
  654. *
  655. * $a = new BigInteger('10');
  656. * $b = new BigInteger('20');
  657. *
  658. * $c = $a->add($b);
  659. *
  660. * echo $c->toString(); // outputs 30
  661. * ?>
  662. * </code>
  663. *
  664. * @param BigInteger $y
  665. * @return BigInteger
  666. * @access public
  667. * @internal Performs base-2**52 addition
  668. */
  669. public function add($y){
  670. switch(MATH_BIGINTEGER_MODE){
  671. case MATH_BIGINTEGER_MODE_GMP:
  672. $temp = new BigInteger();
  673. $temp->value = gmp_add($this->value, $y->value);
  674. return $this->_normalize($temp);
  675. case MATH_BIGINTEGER_MODE_BCMATH:
  676. $temp = new BigInteger();
  677. $temp->value = bcadd($this->value, $y->value, 0);
  678. return $this->_normalize($temp);
  679. }
  680. $temp = $this->_add($this->value, $this->is_negative, $y->value, $y->is_negative);
  681. $result = new BigInteger();
  682. $result->value = $temp[MATH_BIGINTEGER_VALUE];
  683. $result->is_negative = $temp[MATH_BIGINTEGER_SIGN];
  684. return $this->_normalize($result);
  685. }
  686. /**
  687. * Performs addition.
  688. *
  689. * @param Array $x_value
  690. * @param Boolean $x_negative
  691. * @param Array $y_value
  692. * @param Boolean $y_negative
  693. * @return Array
  694. * @access private
  695. */
  696. private function _add($x_value, $x_negative, $y_value, $y_negative){
  697. $x_size = count($x_value);
  698. $y_size = count($y_value);
  699. if($x_size == 0){
  700. return array(
  701. MATH_BIGINTEGER_VALUE => $y_value,
  702. MATH_BIGINTEGER_SIGN => $y_negative
  703. );
  704. }else if($y_size == 0){
  705. return array(
  706. MATH_BIGINTEGER_VALUE => $x_value,
  707. MATH_BIGINTEGER_SIGN => $x_negative
  708. );
  709. }
  710. // subtract, if appropriate
  711. if($x_negative != $y_negative){
  712. if($x_value == $y_value){
  713. return array(
  714. MATH_BIGINTEGER_VALUE => array(),
  715. MATH_BIGINTEGER_SIGN => false
  716. );
  717. }
  718. $temp = $this->_subtract($x_value, false, $y_value, false);
  719. $temp[MATH_BIGINTEGER_SIGN] = $this->_compare($x_value, false, $y_value, false) > 0 ?
  720. $x_negative : $y_negative;
  721. return $temp;
  722. }
  723. if($x_size < $y_size){
  724. $size = $x_size;
  725. $value = $y_value;
  726. }else{
  727. $size = $y_size;
  728. $value = $x_value;
  729. }
  730. $value[] = 0; // just in case the carry adds an extra digit
  731. $carry = 0;
  732. for($i = 0, $j = 1; $j < $size; $i+=2, $j+=2){
  733. $sum = $x_value[$j] * MATH_BIGINTEGER_BASE_FULL + $x_value[$i] + $y_value[$j] * MATH_BIGINTEGER_BASE_FULL + $y_value[$i] + $carry;
  734. $carry = $sum >= MATH_BIGINTEGER_MAX_DIGIT2; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1
  735. $sum = $carry ? $sum - MATH_BIGINTEGER_MAX_DIGIT2 : $sum;
  736. $temp = MATH_BIGINTEGER_BASE === 26 ? intval($sum / 0x4000000) : ($sum >> 31);
  737. $value[$i] = (int) ($sum - MATH_BIGINTEGER_BASE_FULL * $temp); // eg. a faster alternative to fmod($sum, 0x4000000)
  738. $value[$j] = $temp;
  739. }
  740. if($j == $size){ // ie. if $y_size is odd
  741. $sum = $x_value[$i] + $y_value[$i] + $carry;
  742. $carry = $sum >= MATH_BIGINTEGER_BASE_FULL;
  743. $value[$i] = $carry ? $sum - MATH_BIGINTEGER_BASE_FULL : $sum;
  744. ++$i; // ie. let $i = $j since we've just done $value[$i]
  745. }
  746. if($carry){
  747. for(; $value[$i] == MATH_BIGINTEGER_MAX_DIGIT; ++$i){
  748. $value[$i] = 0;
  749. }
  750. ++$value[$i];
  751. }
  752. return array(
  753. MATH_BIGINTEGER_VALUE => $this->_trim($value),
  754. MATH_BIGINTEGER_SIGN => $x_negative
  755. );
  756. }
  757. /**
  758. * Subtracts two BigIntegers.
  759. *
  760. * Here's an example:
  761. * <code>
  762. * <?php
  763. * include 'Math/BigInteger.php';
  764. *
  765. * $a = new BigInteger('10');
  766. * $b = new BigInteger('20');
  767. *
  768. * $c = $a->subtract($b);
  769. *
  770. * echo $c->toString(); // outputs -10
  771. * ?>
  772. * </code>
  773. *
  774. * @param BigInteger $y
  775. * @return BigInteger
  776. * @access public
  777. * @internal Performs base-2**52 subtraction
  778. */
  779. public function subtract($y){
  780. switch(MATH_BIGINTEGER_MODE){
  781. case MATH_BIGINTEGER_MODE_GMP:
  782. $temp = new BigInteger();
  783. $temp->value = gmp_sub($this->value, $y->value);
  784. return $this->_normalize($temp);
  785. case MATH_BIGINTEGER_MODE_BCMATH:
  786. $temp = new BigInteger();
  787. $temp->value = bcsub($this->value, $y->value, 0);
  788. return $this->_normalize($temp);
  789. }
  790. $temp = $this->_subtract($this->value, $this->is_negative, $y->value, $y->is_negative);
  791. $result = new BigInteger();
  792. $result->value = $temp[MATH_BIGINTEGER_VALUE];
  793. $result->is_negative = $temp[MATH_BIGINTEGER_SIGN];
  794. return $this->_normalize($result);
  795. }
  796. /**
  797. * Performs subtraction.
  798. *
  799. * @param Array $x_value
  800. * @param Boolean $x_negative
  801. * @param Array $y_value
  802. * @param Boolean $y_negative
  803. * @return Array
  804. * @access private
  805. */
  806. private function _subtract($x_value, $x_negative, $y_value, $y_negative){
  807. $x_size = count($x_value);
  808. $y_size = count($y_value);
  809. if($x_size == 0){
  810. return array(
  811. MATH_BIGINTEGER_VALUE => $y_value,
  812. MATH_BIGINTEGER_SIGN => !$y_negative
  813. );
  814. }else if($y_size == 0){
  815. return array(
  816. MATH_BIGINTEGER_VALUE => $x_value,
  817. MATH_BIGINTEGER_SIGN => $x_negative
  818. );
  819. }
  820. // add, if appropriate (ie. -$x - +$y or +$x - -$y)
  821. if($x_negative != $y_negative){
  822. $temp = $this->_add($x_value, false, $y_value, false);
  823. $temp[MATH_BIGINTEGER_SIGN] = $x_negative;
  824. return $temp;
  825. }
  826. $diff = $this->_compare($x_value, $x_negative, $y_value, $y_negative);
  827. if(!$diff){
  828. return array(
  829. MATH_BIGINTEGER_VALUE => array(),
  830. MATH_BIGINTEGER_SIGN => false
  831. );
  832. }
  833. // switch $x and $y around, if appropriate.
  834. if((!$x_negative && $diff < 0) || ($x_negative && $diff > 0)){
  835. $temp = $x_value;
  836. $x_value = $y_value;
  837. $y_value = $temp;
  838. $x_negative = !$x_negative;
  839. $x_size = count($x_value);
  840. $y_size = count($y_value);
  841. }
  842. // at this point, $x_value should be at least as big as - if not bigger than - $y_value
  843. $carry = 0;
  844. for($i = 0, $j = 1; $j < $y_size; $i+=2, $j+=2){
  845. $sum = $x_value[$j] * MATH_BIGINTEGER_BASE_FULL + $x_value[$i] - $y_value[$j] * MATH_BIGINTEGER_BASE_FULL - $y_value[$i] - $carry;
  846. $carry = $sum < 0; // eg. floor($sum / 2**52); only possible values (in any base) are 0 and 1
  847. $sum = $carry ? $sum + MATH_BIGINTEGER_MAX_DIGIT2 : $sum;
  848. $temp = MATH_BIGINTEGER_BASE === 26 ? intval($sum / 0x4000000) : ($sum >> 31);
  849. $x_value[$i] = (int) ($sum - MATH_BIGINTEGER_BASE_FULL * $temp);
  850. $x_value[$j] = $temp;
  851. }
  852. if($j == $y_size){ // ie. if $y_size is odd
  853. $sum = $x_value[$i] - $y_value[$i] - $carry;
  854. $carry = $sum < 0;
  855. $x_value[$i] = $carry ? $sum + MATH_BIGINTEGER_BASE_FULL : $sum;
  856. ++$i;
  857. }
  858. if($carry){
  859. for(; !$x_value[$i]; ++$i){
  860. $x_value[$i] = MATH_BIGINTEGER_MAX_DIGIT;
  861. }
  862. --$x_value[$i];
  863. }
  864. return array(
  865. MATH_BIGINTEGER_VALUE => $this->_trim($x_value),
  866. MATH_BIGINTEGER_SIGN => $x_negative
  867. );
  868. }
  869. /**
  870. * Multiplies two BigIntegers
  871. *
  872. * Here's an example:
  873. * <code>
  874. * <?php
  875. * include 'Math/BigInteger.php';
  876. *
  877. * $a = new BigInteger('10');
  878. * $b = new BigInteger('20');
  879. *
  880. * $c = $a->multiply($b);
  881. *
  882. * echo $c->toString(); // outputs 200
  883. * ?>
  884. * </code>
  885. *
  886. * @param BigInteger $x
  887. * @return BigInteger
  888. * @access public
  889. */
  890. public function multiply($x){
  891. switch(MATH_BIGINTEGER_MODE){
  892. case MATH_BIGINTEGER_MODE_GMP:
  893. $temp = new BigInteger();
  894. $temp->value = gmp_mul($this->value, $x->value);
  895. return $this->_normalize($temp);
  896. case MATH_BIGINTEGER_MODE_BCMATH:
  897. $temp = new BigInteger();
  898. $temp->value = bcmul($this->value, $x->value, 0);
  899. return $this->_normalize($temp);
  900. }
  901. $temp = $this->_multiply($this->value, $this->is_negative, $x->value, $x->is_negative);
  902. $product = new BigInteger();
  903. $product->value = $temp[MATH_BIGINTEGER_VALUE];
  904. $product->is_negative = $temp[MATH_BIGINTEGER_SIGN];
  905. return $this->_normalize($product);
  906. }
  907. /**
  908. * Performs multiplication.
  909. *
  910. * @param Array $x_value
  911. * @param Boolean $x_negative
  912. * @param Array $y_value
  913. * @param Boolean $y_negative
  914. * @return Array
  915. * @access private
  916. */
  917. private function _multiply($x_value, $x_negative, $y_value, $y_negative){
  918. //if ( $x_value == $y_value ) {
  919. // return array(
  920. // MATH_BIGINTEGER_VALUE => $this->_square($x_value),
  921. // MATH_BIGINTEGER_SIGN => $x_sign != $y_value
  922. // );
  923. //}
  924. $x_length = count($x_value);
  925. $y_length = count($y_value);
  926. if(!$x_length || !$y_length){ // a 0 is being multiplied
  927. return array(
  928. MATH_BIGINTEGER_VALUE => array(),
  929. MATH_BIGINTEGER_SIGN => false
  930. );
  931. }
  932. return array(
  933. MATH_BIGINTEGER_VALUE => min($x_length, $y_length) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ?
  934. $this->_trim($this->_regularMultiply($x_value, $y_value)) :
  935. $this->_trim($this->_karatsuba($x_value, $y_value)),
  936. MATH_BIGINTEGER_SIGN => $x_negative != $y_negative
  937. );
  938. }
  939. /**
  940. * Performs long multiplication on two BigIntegers
  941. *
  942. * Modeled after 'multiply' in MutableBigInteger.java.
  943. *
  944. * @param Array $x_value
  945. * @param Array $y_value
  946. * @return Array
  947. * @access private
  948. */
  949. private function _regularMultiply($x_value, $y_value){
  950. $x_length = count($x_value);
  951. $y_length = count($y_value);
  952. if(!$x_length || !$y_length){ // a 0 is being multiplied
  953. return array();
  954. }
  955. if($x_length < $y_length){
  956. $temp = $x_value;
  957. $x_value = $y_value;
  958. $y_value = $temp;
  959. $x_length = count($x_value);
  960. $y_length = count($y_value);
  961. }
  962. $product_value = $this->_array_repeat(0, $x_length + $y_length);
  963. // the following for loop could be removed if the for loop following it
  964. // (the one with nested for loops) initially set $i to 0, but
  965. // doing so would also make the result in one set of unnecessary adds,
  966. // since on the outermost loops first pass, $product->value[$k] is going
  967. // to always be 0
  968. $carry = 0;
  969. for($j = 0; $j < $x_length; ++$j){ // ie. $i = 0
  970. $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
  971. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  972. $product_value[$j] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  973. }
  974. $product_value[$j] = $carry;
  975. // the above for loop is what the previous comment was talking about. the
  976. // following for loop is the "one with nested for loops"
  977. for($i = 1; $i < $y_length; ++$i){
  978. $carry = 0;
  979. for($j = 0, $k = $i; $j < $x_length; ++$j, ++$k){
  980. $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
  981. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  982. $product_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  983. }
  984. $product_value[$k] = $carry;
  985. }
  986. return $product_value;
  987. }
  988. /**
  989. * Performs Karatsuba multiplication on two BigIntegers
  990. *
  991. * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and
  992. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}.
  993. *
  994. * @param Array $x_value
  995. * @param Array $y_value
  996. * @return Array
  997. * @access private
  998. */
  999. private function _karatsuba($x_value, $y_value){
  1000. $m = min(count($x_value) >> 1, count($y_value) >> 1);
  1001. if($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF){
  1002. return $this->_regularMultiply($x_value, $y_value);
  1003. }
  1004. $x1 = array_slice($x_value, $m);
  1005. $x0 = array_slice($x_value, 0, $m);
  1006. $y1 = array_slice($y_value, $m);
  1007. $y0 = array_slice($y_value, 0, $m);
  1008. $z2 = $this->_karatsuba($x1, $y1);
  1009. $z0 = $this->_karatsuba($x0, $y0);
  1010. $z1 = $this->_add($x1, false, $x0, false);
  1011. $temp = $this->_add($y1, false, $y0, false);
  1012. $z1 = $this->_karatsuba($z1[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_VALUE]);
  1013. $temp = $this->_add($z2, false, $z0, false);
  1014. $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false);
  1015. $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2);
  1016. $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]);
  1017. $xy = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]);
  1018. $xy = $this->_add($xy[MATH_BIGINTEGER_VALUE], $xy[MATH_BIGINTEGER_SIGN], $z0, false);
  1019. return $xy[MATH_BIGINTEGER_VALUE];
  1020. }
  1021. /**
  1022. * Performs squaring
  1023. *
  1024. * @param Array $x
  1025. * @return Array
  1026. * @access private
  1027. */
  1028. private function _square($x = false){
  1029. return count($x) < 2 * MATH_BIGINTEGER_KARATSUBA_CUTOFF ?
  1030. $this->_trim($this->_baseSquare($x)) :
  1031. $this->_trim($this->_karatsubaSquare($x));
  1032. }
  1033. /**
  1034. * Performs traditional squaring on two BigIntegers
  1035. *
  1036. * Squaring can be done faster than multiplying a number by itself can be. See
  1037. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} /
  1038. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information.
  1039. *
  1040. * @param Array $value
  1041. * @return Array
  1042. * @access private
  1043. */
  1044. private function _baseSquare($value){
  1045. if(empty($value)){
  1046. return array();
  1047. }
  1048. $square_value = $this->_array_repeat(0, 2 * count($value));
  1049. for($i = 0, $max_index = count($value) - 1; $i <= $max_index; ++$i){
  1050. $i2 = $i << 1;
  1051. $temp = $square_value[$i2] + $value[$i] * $value[$i];
  1052. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  1053. $square_value[$i2] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1054. // note how we start from $i+1 instead of 0 as we do in multiplication.
  1055. for($j = $i + 1, $k = $i2 + 1; $j <= $max_index; ++$j, ++$k){
  1056. $temp = $square_value[$k] + 2 * $value[$j] * $value[$i] + $carry;
  1057. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  1058. $square_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1059. }
  1060. // the following line can yield values larger 2**15. at this point, PHP should switch
  1061. // over to floats.
  1062. $square_value[$i + $max_index + 1] = $carry;
  1063. }
  1064. return $square_value;
  1065. }
  1066. /**
  1067. * Performs Karatsuba "squaring" on two BigIntegers
  1068. *
  1069. * See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and
  1070. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}.
  1071. *
  1072. * @param Array $value
  1073. * @return Array
  1074. * @access private
  1075. */
  1076. private function _karatsubaSquare($value){
  1077. $m = count($value) >> 1;
  1078. if($m < MATH_BIGINTEGER_KARATSUBA_CUTOFF){
  1079. return $this->_baseSquare($value);
  1080. }
  1081. $x1 = array_slice($value, $m);
  1082. $x0 = array_slice($value, 0, $m);
  1083. $z2 = $this->_karatsubaSquare($x1);
  1084. $z0 = $this->_karatsubaSquare($x0);
  1085. $z1 = $this->_add($x1, false, $x0, false);
  1086. $z1 = $this->_karatsubaSquare($z1[MATH_BIGINTEGER_VALUE]);
  1087. $temp = $this->_add($z2, false, $z0, false);
  1088. $z1 = $this->_subtract($z1, false, $temp[MATH_BIGINTEGER_VALUE], false);
  1089. $z2 = array_merge(array_fill(0, 2 * $m, 0), $z2);
  1090. $z1[MATH_BIGINTEGER_VALUE] = array_merge(array_fill(0, $m, 0), $z1[MATH_BIGINTEGER_VALUE]);
  1091. $xx = $this->_add($z2, false, $z1[MATH_BIGINTEGER_VALUE], $z1[MATH_BIGINTEGER_SIGN]);
  1092. $xx = $this->_add($xx[MATH_BIGINTEGER_VALUE], $xx[MATH_BIGINTEGER_SIGN], $z0, false);
  1093. return $xx[MATH_BIGINTEGER_VALUE];
  1094. }
  1095. /**
  1096. * Divides two BigIntegers.
  1097. *
  1098. * Returns an array whose first element contains the quotient and whose second element contains the
  1099. * "common residue". If the remainder would be positive, the "common residue" and the remainder are the
  1100. * same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder
  1101. * and the divisor (basically, the "common residue" is the first positive modulo).
  1102. *
  1103. * Here's an example:
  1104. * <code>
  1105. * <?php
  1106. * include 'Math/BigInteger.php';
  1107. *
  1108. * $a = new BigInteger('10');
  1109. * $b = new BigInteger('20');
  1110. *
  1111. * list($quotient, $remainder) = $a->divide($b);
  1112. *
  1113. * echo $quotient->toString(); // outputs 0
  1114. * echo "\r\n";
  1115. * echo $remainder->toString(); // outputs 10
  1116. * ?>
  1117. * </code>
  1118. *
  1119. * @param BigInteger $y
  1120. * @return Array
  1121. * @access public
  1122. * @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}.
  1123. */
  1124. public function divide($y){
  1125. switch(MATH_BIGINTEGER_MODE){
  1126. case MATH_BIGINTEGER_MODE_GMP:
  1127. $quotient = new BigInteger();
  1128. $remainder = new BigInteger();
  1129. list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value);
  1130. if(gmp_sign($remainder->value) < 0){
  1131. $remainder->value = gmp_add($remainder->value, gmp_abs($y->value));
  1132. }
  1133. return array($this->_normalize($quotient), $this->_normalize($remainder));
  1134. case MATH_BIGINTEGER_MODE_BCMATH:
  1135. $quotient = new BigInteger();
  1136. $remainder = new BigInteger();
  1137. $quotient->value = bcdiv($this->value, $y->value, 0);
  1138. $remainder->value = bcmod($this->value, $y->value);
  1139. if($remainder->value[0] == '-'){
  1140. $remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0);
  1141. }
  1142. return array($this->_normalize($quotient), $this->_normalize($remainder));
  1143. }
  1144. if(count($y->value) == 1){
  1145. list($q, $r) = $this->_divide_digit($this->value, $y->value[0]);
  1146. $quotient = new BigInteger();
  1147. $remainder = new BigInteger();
  1148. $quotient->value = $q;
  1149. $remainder->value = array($r);
  1150. $quotient->is_negative = $this->is_negative != $y->is_negative;
  1151. return array($this->_normalize($quotient), $this->_normalize($remainder));
  1152. }
  1153. static $zero;
  1154. if(!isset($zero)){
  1155. $zero = new BigInteger();
  1156. }
  1157. $x = $this->copy();
  1158. $y = $y->copy();
  1159. $x_sign = $x->is_negative;
  1160. $y_sign = $y->is_negative;
  1161. $x->is_negative = $y->is_negative = false;
  1162. $diff = $x->compare($y);
  1163. if(!$diff){
  1164. $temp = new BigInteger();
  1165. $temp->value = array(1);
  1166. $temp->is_negative = $x_sign != $y_sign;
  1167. return array($this->_normalize($temp), $this->_normalize(new BigInteger()));
  1168. }
  1169. if($diff < 0){
  1170. // if $x is negative, "add" $y.
  1171. if($x_sign){
  1172. $x = $y->subtract($x);
  1173. }
  1174. return array($this->_normalize(new BigInteger()), $this->_normalize($x));
  1175. }
  1176. // normalize $x and $y as described in HAC 14.23 / 14.24
  1177. $msb = $y->value[count($y->value) - 1];
  1178. for($shift = 0; !($msb & MATH_BIGINTEGER_MSB); ++$shift){
  1179. $msb <<= 1;
  1180. }
  1181. $x->_lshift($shift);
  1182. $y->_lshift($shift);
  1183. $y_value = &$y->value;
  1184. $x_max = count($x->value) - 1;
  1185. $y_max = count($y->value) - 1;
  1186. $quotient = new BigInteger();
  1187. $quotient_value = &$quotient->value;
  1188. $quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1);
  1189. static $temp, $lhs, $rhs;
  1190. if(!isset($temp)){
  1191. $temp = new BigInteger();
  1192. $lhs = new BigInteger();
  1193. $rhs = new BigInteger();
  1194. }
  1195. $temp_value = &$temp->value;
  1196. $rhs_value = &$rhs->value;
  1197. // $temp = $y << ($x_max - $y_max-1) in base 2**26
  1198. $temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value);
  1199. while($x->compare($temp) >= 0){
  1200. // calculate the "common residue"
  1201. ++$quotient_value[$x_max - $y_max];
  1202. $x = $x->subtract($temp);
  1203. $x_max = count($x->value) - 1;
  1204. }
  1205. for($i = $x_max; $i >= $y_max + 1; --$i){
  1206. $x_value = &$x->value;
  1207. $x_window = array(
  1208. isset($x_value[$i]) ? $x_value[$i] : 0,
  1209. isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0,
  1210. isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0
  1211. );
  1212. $y_window = array(
  1213. $y_value[$y_max],
  1214. ( $y_max > 0 ) ? $y_value[$y_max - 1] : 0
  1215. );
  1216. $q_index = $i - $y_max - 1;
  1217. if($x_window[0] == $y_window[0]){
  1218. $quotient_value[$q_index] = MATH_BIGINTEGER_MAX_DIGIT;
  1219. }else{
  1220. $quotient_value[$q_index] = $this->_safe_divide(
  1221. $x_window[0] * MATH_BIGINTEGER_BASE_FULL + $x_window[1], $y_window[0]
  1222. );
  1223. }
  1224. $temp_value = array($y_window[1], $y_window[0]);
  1225. $lhs->value = array($quotient_value[$q_index]);
  1226. $lhs = $lhs->multiply($temp);
  1227. $rhs_value = array($x_window[2], $x_window[1], $x_window[0]);
  1228. while($lhs->compare($rhs) > 0){
  1229. --$quotient_value[$q_index];
  1230. $lhs->value = array($quotient_value[$q_index]);
  1231. $lhs = $lhs->multiply($temp);
  1232. }
  1233. $adjust = $this->_array_repeat(0, $q_index);
  1234. $temp_value = array($quotient_value[$q_index]);
  1235. $temp = $temp->multiply($y);
  1236. $temp_value = &$temp->value;
  1237. $temp_value = array_merge($adjust, $temp_value);
  1238. $x = $x->subtract($temp);
  1239. if($x->compare($zero) < 0){
  1240. $temp_value = array_merge($adjust, $y_value);
  1241. $x = $x->add($temp);
  1242. --$quotient_value[$q_index];
  1243. }
  1244. $x_max = count($x_value) - 1;
  1245. }
  1246. // unnormalize the remainder
  1247. $x->_rshift($shift);
  1248. $quotient->is_negative = $x_sign != $y_sign;
  1249. // calculate the "common residue", if appropriate
  1250. if($x_sign){
  1251. $y->_rshift($shift);
  1252. $x = $y->subtract($x);
  1253. }
  1254. return array($this->_normalize($quotient), $this->_normalize($x));
  1255. }
  1256. /**
  1257. * Divides a BigInteger by a regular integer
  1258. *
  1259. * abc / x = a00 / x + b0 / x + c / x
  1260. *
  1261. * @param Array $dividend
  1262. * @param Array $divisor
  1263. * @return Array
  1264. * @access private
  1265. */
  1266. private function _divide_digit($dividend, $divisor){
  1267. $carry = 0;
  1268. $result = array();
  1269. for($i = count($dividend) - 1; $i >= 0; --$i){
  1270. $temp = MATH_BIGINTEGER_BASE_FULL * $carry + $dividend[$i];
  1271. $result[$i] = $this->_safe_divide($temp, $divisor);
  1272. $carry = (int) ($temp - $divisor * $result[$i]);
  1273. }
  1274. return array($result, $carry);
  1275. }
  1276. /**
  1277. * Performs modular exponentiation.
  1278. *
  1279. * Here's an example:
  1280. * <code>
  1281. * <?php
  1282. * include 'Math/BigInteger.php';
  1283. *
  1284. * $a = new BigInteger('10');
  1285. * $b = new BigInteger('20');
  1286. * $c = new BigInteger('30');
  1287. *
  1288. * $c = $a->modPow($b, $c);
  1289. *
  1290. * echo $c->toString(); // outputs 10
  1291. * ?>
  1292. * </code>
  1293. *
  1294. * @param BigInteger $e
  1295. * @param BigInteger $n
  1296. * @return BigInteger
  1297. * @access public
  1298. * @internal The most naive approach to modular exponentiation has very unreasonable requirements, and
  1299. * and although the approach involving repeated squaring does vastly better, it, too, is impractical
  1300. * for our purposes. The reason being that division - by far the most complicated and time-consuming
  1301. * of the basic operations (eg. +,-,*,/) - occurs multiple times within it.
  1302. *
  1303. * Modular reductions resolve this issue. Although an individual modular reduction takes more time
  1304. * then an individual division, when performed in succession (with the same modulo), they're a lot faster.
  1305. *
  1306. * The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction,
  1307. * although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the
  1308. * base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because
  1309. * the product of two odd numbers is odd), but what about when RSA isn't used?
  1310. *
  1311. * In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a
  1312. * Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the
  1313. * modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however,
  1314. * uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and
  1315. * the other, a power of two - and recombine them, later. This is the method that this modPow function uses.
  1316. * {@link http://islab.oregonstate.edu/papers/j34monex.pdf Montgomery Reduction with Even Modulus} elaborates.
  1317. */
  1318. public function modPow($e, $n){
  1319. $n = $this->bitmask !== false && $this->bitmask->compare($n) < 0 ? $this->bitmask : $n->abs();
  1320. if($e->compare(new BigInteger()) < 0){
  1321. $e = $e->abs();
  1322. $temp = $this->modInverse($n);
  1323. if($temp === false){
  1324. return false;
  1325. }
  1326. return $this->_normalize($temp->modPow($e, $n));
  1327. }
  1328. if(MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP){
  1329. $temp = new BigInteger();
  1330. $temp->value = gmp_powm($this->value, $e->value, $n->value);
  1331. return $this->_normalize($temp);
  1332. }
  1333. if($this->compare(new BigInteger()) < 0 || $this->compare($n) > 0){
  1334. list(, $temp) = $this->divide($n);
  1335. return $temp->modPow($e, $n);
  1336. }
  1337. if(defined('MATH_BIGINTEGER_OPENSSL_ENABLED')){
  1338. $components = array(
  1339. 'modulus' => $n->toBytes(true),
  1340. 'publicExponent' => $e->toBytes(true)
  1341. );
  1342. $components = array(
  1343. 'modulus' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['modulus'])), $components['modulus']),
  1344. 'publicExponent' => pack('Ca*a*', 2, $this->_encodeASN1Length(strlen($components['publicExponent'])), $components['publicExponent'])
  1345. );
  1346. $RSAPublicKey = pack('Ca*a*a*', 48, $this->_encodeASN1Length(strlen($components['modulus']) + strlen($components['publicExponent'])), $components['modulus'], $components['publicExponent']
  1347. );
  1348. $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
  1349. $RSAPublicKey = chr(0) . $RSAPublicKey;
  1350. $RSAPublicKey = chr(3) . $this->_encodeASN1Length(strlen($RSAPublicKey)) . $RSAPublicKey;
  1351. $encapsulated = pack('Ca*a*', 48, $this->_encodeASN1Length(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey
  1352. );
  1353. $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" .
  1354. chunk_split(base64_encode($encapsulated)) .
  1355. '-----END PUBLIC KEY-----';
  1356. $plaintext = str_pad($this->toBytes(), strlen($n->toBytes(true)) - 1, "\0", STR_PAD_LEFT);
  1357. if(openssl_public_encrypt($plaintext, $result, $RSAPublicKey, OPENSSL_NO_PADDING)){
  1358. return new BigInteger($result, 256);
  1359. }
  1360. }
  1361. if(MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH){
  1362. $temp = new BigInteger();
  1363. $temp->value = bcpowmod($this->value, $e->value, $n->value, 0);
  1364. return $this->_normalize($temp);
  1365. }
  1366. if(empty($e->value)){
  1367. $temp = new BigInteger();
  1368. $temp->value = array(1);
  1369. return $this->_normalize($temp);
  1370. }
  1371. if($e->value == array(1)){
  1372. list(, $temp) = $this->divide($n);
  1373. return $this->_normalize($temp);
  1374. }
  1375. if($e->value == array(2)){
  1376. $temp = new BigInteger();
  1377. $temp->value = $this->_square($this->value);
  1378. list(, $temp) = $temp->divide($n);
  1379. return $this->_normalize($temp);
  1380. }
  1381. return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_BARRETT));
  1382. // the following code, although not callable, can be run independently of the above code
  1383. // although the above code performed better in my benchmarks the following could might
  1384. // perform better under different circumstances. in lieu of deleting it it's just been
  1385. // made uncallable
  1386. // is the modulo odd?
  1387. if($n->value[0] & 1){
  1388. return $this->_normalize($this->_slidingWindow($e, $n, MATH_BIGINTEGER_MONTGOMERY));
  1389. }
  1390. // if it's not, it's even
  1391. // find the lowest set bit (eg. the max pow of 2 that divides $n)
  1392. for($i = 0; $i < count($n->value); ++$i){
  1393. if($n->value[$i]){
  1394. $temp = decbin($n->value[$i]);
  1395. $j = strlen($temp) - strrpos($temp, '1') - 1;
  1396. $j+= 26 * $i;
  1397. break;
  1398. }
  1399. }
  1400. // at this point, 2^$j * $n/(2^$j) == $n
  1401. $mod1 = $n->copy();
  1402. $mod1->_rshift($j);
  1403. $mod2 = new BigInteger();
  1404. $mod2->value = array(1);
  1405. $mod2->_lshift($j);
  1406. $part1 = ( $mod1->value != array(1) ) ? $this->_slidingWindow($e, $mod1, MATH_BIGINTEGER_MONTGOMERY) : new BigInteger();
  1407. $part2 = $this->_slidingWindow($e, $mod2, MATH_BIGINTEGER_POWEROF2);
  1408. $y1 = $mod2->modInverse($mod1);
  1409. $y2 = $mod1->modInverse($mod2);
  1410. $result = $part1->multiply($mod2);
  1411. $result = $result->multiply($y1);
  1412. $temp = $part2->multiply($mod1);
  1413. $temp = $temp->multiply($y2);
  1414. $result = $result->add($temp);
  1415. list(, $result) = $result->divide($n);
  1416. return $this->_normalize($result);
  1417. }
  1418. /**
  1419. * Performs modular exponentiation.
  1420. *
  1421. * Alias for BigInteger::modPow()
  1422. *
  1423. * @param BigInteger $e
  1424. * @param BigInteger $n
  1425. * @return BigInteger
  1426. * @access public
  1427. */
  1428. public function powMod($e, $n){
  1429. return $this->modPow($e, $n);
  1430. }
  1431. /**
  1432. * Sliding Window k-ary Modular Exponentiation
  1433. *
  1434. * Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} /
  1435. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims,
  1436. * however, this function performs a modular reduction after every multiplication and squaring operation.
  1437. * As such, this function has the same preconditions that the reductions being used do.
  1438. *
  1439. * @param BigInteger $e
  1440. * @param BigInteger $n
  1441. * @param Integer $mode
  1442. * @return BigInteger
  1443. * @access private
  1444. */
  1445. private function _slidingWindow($e, $n, $mode){
  1446. static $window_ranges = array(7, 25, 81, 241, 673, 1793); // from BigInteger.java's oddModPow function
  1447. //static $window_ranges = array(0, 7, 36, 140, 450, 1303, 3529); // from MPM 7.3.1
  1448. $e_value = $e->value;
  1449. $e_length = count($e_value) - 1;
  1450. $e_bits = decbin($e_value[$e_length]);
  1451. for($i = $e_length - 1; $i >= 0; --$i){
  1452. $e_bits.= str_pad(decbin($e_value[$i]), MATH_BIGINTEGER_BASE, '0', STR_PAD_LEFT);
  1453. }
  1454. $e_length = strlen($e_bits);
  1455. // calculate the appropriate window size.
  1456. // $window_size == 3 if $window_ranges is between 25 and 81, for example.
  1457. for($i = 0, $window_size = 1; $e_length > $window_ranges[$i] && $i < count($window_ranges); ++$window_size, ++$i)
  1458. ;
  1459. $n_value = $n->value;
  1460. // precompute $this^0 through $this^$window_size
  1461. $powers = array();
  1462. $powers[1] = $this->_prepareReduce($this->value, $n_value, $mode);
  1463. $powers[2] = $this->_squareReduce($powers[1], $n_value, $mode);
  1464. // we do every other number since substr($e_bits, $i, $j+1) (see below) is supposed to end
  1465. // in a 1. ie. it's supposed to be odd.
  1466. $temp = 1 << ($window_size - 1);
  1467. for($i = 1; $i < $temp; ++$i){
  1468. $i2 = $i << 1;
  1469. $powers[$i2 + 1] = $this->_multiplyReduce($powers[$i2 - 1], $powers[2], $n_value, $mode);
  1470. }
  1471. $result = array(1);
  1472. $result = $this->_prepareReduce($result, $n_value, $mode);
  1473. for($i = 0; $i < $e_length;){
  1474. if(!$e_bits[$i]){
  1475. $result = $this->_squareReduce($result, $n_value, $mode);
  1476. ++$i;
  1477. }else{
  1478. for($j = $window_size - 1; $j > 0; --$j){
  1479. if(!empty($e_bits[$i + $j])){
  1480. break;
  1481. }
  1482. }
  1483. for($k = 0; $k <= $j; ++$k){// eg. the length of substr($e_bits, $i, $j+1)
  1484. $result = $this->_squareReduce($result, $n_value, $mode);
  1485. }
  1486. $result = $this->_multiplyReduce($result, $powers[bindec(substr($e_bits, $i, $j + 1))], $n_value, $mode);
  1487. $i+=$j + 1;
  1488. }
  1489. }
  1490. $temp = new BigInteger();
  1491. $temp->value = $this->_reduce($result, $n_value, $mode);
  1492. return $temp;
  1493. }
  1494. /**
  1495. * Modular reduction
  1496. *
  1497. * For most $modes this will return the remainder.
  1498. *
  1499. * @see _slidingWindow()
  1500. * @access private
  1501. * @param Array $x
  1502. * @param Array $n
  1503. * @param Integer $mode
  1504. * @return Array
  1505. */
  1506. private function _reduce($x, $n, $mode){
  1507. switch($mode){
  1508. case MATH_BIGINTEGER_MONTGOMERY:
  1509. return $this->_montgomery($x, $n);
  1510. case MATH_BIGINTEGER_BARRETT:
  1511. return $this->_barrett($x, $n);
  1512. case MATH_BIGINTEGER_POWEROF2:
  1513. $lhs = new BigInteger();
  1514. $lhs->value = $x;
  1515. $rhs = new BigInteger();
  1516. $rhs->value = $n;
  1517. return $x->_mod2($n);
  1518. case MATH_BIGINTEGER_CLASSIC:
  1519. $lhs = new BigInteger();
  1520. $lhs->value = $x;
  1521. $rhs = new BigInteger();
  1522. $rhs->value = $n;
  1523. list(, $temp) = $lhs->divide($rhs);
  1524. return $temp->value;
  1525. case MATH_BIGINTEGER_NONE:
  1526. return $x;
  1527. default:
  1528. // an invalid $mode was provided
  1529. }
  1530. }
  1531. /**
  1532. * Modular reduction preperation
  1533. *
  1534. * @see _slidingWindow()
  1535. * @access private
  1536. * @param Array $x
  1537. * @param Array $n
  1538. * @param Integer $mode
  1539. * @return Array
  1540. */
  1541. private function _prepareReduce($x, $n, $mode){
  1542. if($mode == MATH_BIGINTEGER_MONTGOMERY){
  1543. return $this->_prepMontgomery($x, $n);
  1544. }
  1545. return $this->_reduce($x, $n, $mode);
  1546. }
  1547. /**
  1548. * Modular multiply
  1549. *
  1550. * @see _slidingWindow()
  1551. * @access private
  1552. * @param Array $x
  1553. * @param Array $y
  1554. * @param Array $n
  1555. * @param Integer $mode
  1556. * @return Array
  1557. */
  1558. private function _multiplyReduce($x, $y, $n, $mode){
  1559. if($mode == MATH_BIGINTEGER_MONTGOMERY){
  1560. return $this->_montgomeryMultiply($x, $y, $n);
  1561. }
  1562. $temp = $this->_multiply($x, false, $y, false);
  1563. return $this->_reduce($temp[MATH_BIGINTEGER_VALUE], $n, $mode);
  1564. }
  1565. /**
  1566. * Modular square
  1567. *
  1568. * @see _slidingWindow()
  1569. * @access private
  1570. * @param Array $x
  1571. * @param Array $n
  1572. * @param Integer $mode
  1573. * @return Array
  1574. */
  1575. private function _squareReduce($x, $n, $mode){
  1576. if($mode == MATH_BIGINTEGER_MONTGOMERY){
  1577. return $this->_montgomeryMultiply($x, $x, $n);
  1578. }
  1579. return $this->_reduce($this->_square($x), $n, $mode);
  1580. }
  1581. /**
  1582. * Modulos for Powers of Two
  1583. *
  1584. * Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1),
  1585. * we'll just use this function as a wrapper for doing that.
  1586. *
  1587. * @see _slidingWindow()
  1588. * @access private
  1589. * @param BigInteger
  1590. * @return BigInteger
  1591. */
  1592. private function _mod2($n){
  1593. $temp = new BigInteger();
  1594. $temp->value = array(1);
  1595. return $this->bitwise_and($n->subtract($temp));
  1596. }
  1597. /**
  1598. * Barrett Modular Reduction
  1599. *
  1600. * See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} /
  1601. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 MPM 6.2.5} for more information. Modified slightly,
  1602. * so as not to require negative numbers (initially, this script didn't support negative numbers).
  1603. *
  1604. * Employs "folding", as described at
  1605. * {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 thesis-149.pdf#page=66}. To quote from
  1606. * it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x."
  1607. *
  1608. * Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that
  1609. * usable on account of (1) its not using reasonable radix points as discussed in
  1610. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 MPM 6.2.2} and (2) the fact that, even with reasonable
  1611. * radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that
  1612. * (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line
  1613. * comments for details.
  1614. *
  1615. * @see _slidingWindow()
  1616. * @access private
  1617. * @param Array $n
  1618. * @param Array $m
  1619. * @return Array
  1620. */
  1621. private function _barrett($n, $m){
  1622. static $cache = array(
  1623. MATH_BIGINTEGER_VARIABLE => array(),
  1624. MATH_BIGINTEGER_DATA => array()
  1625. );
  1626. $m_length = count($m);
  1627. // if ($this->_compare($n, $this->_square($m)) >= 0) {
  1628. if(count($n) > 2 * $m_length){
  1629. $lhs = new BigInteger();
  1630. $rhs = new BigInteger();
  1631. $lhs->value = $n;
  1632. $rhs->value = $m;
  1633. list(, $temp) = $lhs->divide($rhs);
  1634. return $temp->value;
  1635. }
  1636. // if (m.length >> 1) + 2 <= m.length then m is too small and n can't be reduced
  1637. if($m_length < 5){
  1638. return $this->_regularBarrett($n, $m);
  1639. }
  1640. // n = 2 * m.length
  1641. if(($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false){
  1642. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1643. $cache[MATH_BIGINTEGER_VARIABLE][] = $m;
  1644. $lhs = new BigInteger();
  1645. $lhs_value = &$lhs->value;
  1646. $lhs_value = $this->_array_repeat(0, $m_length + ($m_length >> 1));
  1647. $lhs_value[] = 1;
  1648. $rhs = new BigInteger();
  1649. $rhs->value = $m;
  1650. list($u, $m1) = $lhs->divide($rhs);
  1651. $u = $u->value;
  1652. $m1 = $m1->value;
  1653. $cache[MATH_BIGINTEGER_DATA][] = array(
  1654. 'u' => $u, // m.length >> 1 (technically (m.length >> 1) + 1)
  1655. 'm1' => $m1 // m.length
  1656. );
  1657. }else{
  1658. extract($cache[MATH_BIGINTEGER_DATA][$key]);
  1659. }
  1660. $cutoff = $m_length + ($m_length >> 1);
  1661. $lsd = array_slice($n, 0, $cutoff); // m.length + (m.length >> 1)
  1662. $msd = array_slice($n, $cutoff); // m.length >> 1
  1663. $lsd = $this->_trim($lsd);
  1664. $temp = $this->_multiply($msd, false, $m1, false);
  1665. $n = $this->_add($lsd, false, $temp[MATH_BIGINTEGER_VALUE], false); // m.length + (m.length >> 1) + 1
  1666. if($m_length & 1){
  1667. return $this->_regularBarrett($n[MATH_BIGINTEGER_VALUE], $m);
  1668. }
  1669. // (m.length + (m.length >> 1) + 1) - (m.length - 1) == (m.length >> 1) + 2
  1670. $temp = array_slice($n[MATH_BIGINTEGER_VALUE], $m_length - 1);
  1671. // if even: ((m.length >> 1) + 2) + (m.length >> 1) == m.length + 2
  1672. // if odd: ((m.length >> 1) + 2) + (m.length >> 1) == (m.length - 1) + 2 == m.length + 1
  1673. $temp = $this->_multiply($temp, false, $u, false);
  1674. // if even: (m.length + 2) - ((m.length >> 1) + 1) = m.length - (m.length >> 1) + 1
  1675. // if odd: (m.length + 1) - ((m.length >> 1) + 1) = m.length - (m.length >> 1)
  1676. $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], ($m_length >> 1) + 1);
  1677. // if even: (m.length - (m.length >> 1) + 1) + m.length = 2 * m.length - (m.length >> 1) + 1
  1678. // if odd: (m.length - (m.length >> 1)) + m.length = 2 * m.length - (m.length >> 1)
  1679. $temp = $this->_multiply($temp, false, $m, false);
  1680. // at this point, if m had an odd number of digits, we'd be subtracting a 2 * m.length - (m.length >> 1) digit
  1681. // number from a m.length + (m.length >> 1) + 1 digit number. ie. there'd be an extra digit and the while loop
  1682. // following this comment would loop a lot (hence our calling _regularBarrett() in that situation).
  1683. $result = $this->_subtract($n[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false);
  1684. while($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false) >= 0){
  1685. $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $m, false);
  1686. }
  1687. return $result[MATH_BIGINTEGER_VALUE];
  1688. }
  1689. /**
  1690. * (Regular) Barrett Modular Reduction
  1691. *
  1692. * For numbers with more than four digits BigInteger::_barrett() is faster. The difference between that and this
  1693. * is that this function does not fold the denominator into a smaller form.
  1694. *
  1695. * @see _slidingWindow()
  1696. * @access private
  1697. * @param Array $x
  1698. * @param Array $n
  1699. * @return Array
  1700. */
  1701. private function _regularBarrett($x, $n){
  1702. static $cache = array(
  1703. MATH_BIGINTEGER_VARIABLE => array(),
  1704. MATH_BIGINTEGER_DATA => array()
  1705. );
  1706. $n_length = count($n);
  1707. if(count($x) > 2 * $n_length){
  1708. $lhs = new BigInteger();
  1709. $rhs = new BigInteger();
  1710. $lhs->value = $x;
  1711. $rhs->value = $n;
  1712. list(, $temp) = $lhs->divide($rhs);
  1713. return $temp->value;
  1714. }
  1715. if(($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false){
  1716. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1717. $cache[MATH_BIGINTEGER_VARIABLE][] = $n;
  1718. $lhs = new BigInteger();
  1719. $lhs_value = &$lhs->value;
  1720. $lhs_value = $this->_array_repeat(0, 2 * $n_length);
  1721. $lhs_value[] = 1;
  1722. $rhs = new BigInteger();
  1723. $rhs->value = $n;
  1724. list($temp, ) = $lhs->divide($rhs); // m.length
  1725. $cache[MATH_BIGINTEGER_DATA][] = $temp->value;
  1726. }
  1727. // 2 * m.length - (m.length - 1) = m.length + 1
  1728. $temp = array_slice($x, $n_length - 1);
  1729. // (m.length + 1) + m.length = 2 * m.length + 1
  1730. $temp = $this->_multiply($temp, false, $cache[MATH_BIGINTEGER_DATA][$key], false);
  1731. // (2 * m.length + 1) - (m.length - 1) = m.length + 2
  1732. $temp = array_slice($temp[MATH_BIGINTEGER_VALUE], $n_length + 1);
  1733. // m.length + 1
  1734. $result = array_slice($x, 0, $n_length + 1);
  1735. // m.length + 1
  1736. $temp = $this->_multiplyLower($temp, false, $n, false, $n_length + 1);
  1737. // $temp == array_slice($temp->_multiply($temp, false, $n, false)->value, 0, $n_length + 1)
  1738. if($this->_compare($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]) < 0){
  1739. $corrector_value = $this->_array_repeat(0, $n_length + 1);
  1740. $corrector_value[] = 1;
  1741. $result = $this->_add($result, false, $corrector_value, false);
  1742. $result = $result[MATH_BIGINTEGER_VALUE];
  1743. }
  1744. // at this point, we're subtracting a number with m.length + 1 digits from another number with m.length + 1 digits
  1745. $result = $this->_subtract($result, false, $temp[MATH_BIGINTEGER_VALUE], $temp[MATH_BIGINTEGER_SIGN]);
  1746. while($this->_compare($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false) > 0){
  1747. $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], $result[MATH_BIGINTEGER_SIGN], $n, false);
  1748. }
  1749. return $result[MATH_BIGINTEGER_VALUE];
  1750. }
  1751. /**
  1752. * Performs long multiplication up to $stop digits
  1753. *
  1754. * If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved.
  1755. *
  1756. * @see _regularBarrett()
  1757. * @param Array $x_value
  1758. * @param Boolean $x_negative
  1759. * @param Array $y_value
  1760. * @param Boolean $y_negative
  1761. * @param Integer $stop
  1762. * @return Array
  1763. * @access private
  1764. */
  1765. private function _multiplyLower($x_value, $x_negative, $y_value, $y_negative, $stop){
  1766. $x_length = count($x_value);
  1767. $y_length = count($y_value);
  1768. if(!$x_length || !$y_length){ // a 0 is being multiplied
  1769. return array(
  1770. MATH_BIGINTEGER_VALUE => array(),
  1771. MATH_BIGINTEGER_SIGN => false
  1772. );
  1773. }
  1774. if($x_length < $y_length){
  1775. $temp = $x_value;
  1776. $x_value = $y_value;
  1777. $y_value = $temp;
  1778. $x_length = count($x_value);
  1779. $y_length = count($y_value);
  1780. }
  1781. $product_value = $this->_array_repeat(0, $x_length + $y_length);
  1782. // the following for loop could be removed if the for loop following it
  1783. // (the one with nested for loops) initially set $i to 0, but
  1784. // doing so would also make the result in one set of unnecessary adds,
  1785. // since on the outermost loops first pass, $product->value[$k] is going
  1786. // to always be 0
  1787. $carry = 0;
  1788. for($j = 0; $j < $x_length; ++$j){ // ie. $i = 0, $k = $i
  1789. $temp = $x_value[$j] * $y_value[0] + $carry; // $product_value[$k] == 0
  1790. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  1791. $product_value[$j] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1792. }
  1793. if($j < $stop){
  1794. $product_value[$j] = $carry;
  1795. }
  1796. // the above for loop is what the previous comment was talking about. the
  1797. // following for loop is the "one with nested for loops"
  1798. for($i = 1; $i < $y_length; ++$i){
  1799. $carry = 0;
  1800. for($j = 0, $k = $i; $j < $x_length && $k < $stop; ++$j, ++$k){
  1801. $temp = $product_value[$k] + $x_value[$j] * $y_value[$i] + $carry;
  1802. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  1803. $product_value[$k] = (int) ($temp - MATH_BIGINTEGER_BASE_FULL * $carry);
  1804. }
  1805. if($k < $stop){
  1806. $product_value[$k] = $carry;
  1807. }
  1808. }
  1809. return array(
  1810. MATH_BIGINTEGER_VALUE => $this->_trim($product_value),
  1811. MATH_BIGINTEGER_SIGN => $x_negative != $y_negative
  1812. );
  1813. }
  1814. /**
  1815. * Montgomery Modular Reduction
  1816. *
  1817. * ($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n.
  1818. * {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be
  1819. * improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function
  1820. * to work correctly.
  1821. *
  1822. * @see _prepMontgomery()
  1823. * @see _slidingWindow()
  1824. * @access private
  1825. * @param Array $x
  1826. * @param Array $n
  1827. * @return Array
  1828. */
  1829. private function _montgomery($x, $n){
  1830. static $cache = array(
  1831. MATH_BIGINTEGER_VARIABLE => array(),
  1832. MATH_BIGINTEGER_DATA => array()
  1833. );
  1834. if(($key = array_search($n, $cache[MATH_BIGINTEGER_VARIABLE])) === false){
  1835. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1836. $cache[MATH_BIGINTEGER_VARIABLE][] = $x;
  1837. $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($n);
  1838. }
  1839. $k = count($n);
  1840. $result = array(MATH_BIGINTEGER_VALUE => $x);
  1841. for($i = 0; $i < $k; ++$i){
  1842. $temp = $result[MATH_BIGINTEGER_VALUE][$i] * $cache[MATH_BIGINTEGER_DATA][$key];
  1843. $temp = $temp - MATH_BIGINTEGER_BASE_FULL * (MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31));
  1844. $temp = $this->_regularMultiply(array($temp), $n);
  1845. $temp = array_merge($this->_array_repeat(0, $i), $temp);
  1846. $result = $this->_add($result[MATH_BIGINTEGER_VALUE], false, $temp, false);
  1847. }
  1848. $result[MATH_BIGINTEGER_VALUE] = array_slice($result[MATH_BIGINTEGER_VALUE], $k);
  1849. if($this->_compare($result, false, $n, false) >= 0){
  1850. $result = $this->_subtract($result[MATH_BIGINTEGER_VALUE], false, $n, false);
  1851. }
  1852. return $result[MATH_BIGINTEGER_VALUE];
  1853. }
  1854. /**
  1855. * Montgomery Multiply
  1856. *
  1857. * Interleaves the montgomery reduction and long multiplication algorithms together as described in
  1858. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36}
  1859. *
  1860. * @see _prepMontgomery()
  1861. * @see _montgomery()
  1862. * @access private
  1863. * @param Array $x
  1864. * @param Array $y
  1865. * @param Array $m
  1866. * @return Array
  1867. */
  1868. private function _montgomeryMultiply($x, $y, $m){
  1869. $temp = $this->_multiply($x, false, $y, false);
  1870. return $this->_montgomery($temp[MATH_BIGINTEGER_VALUE], $m);
  1871. // the following code, although not callable, can be run independently of the above code
  1872. // although the above code performed better in my benchmarks the following could might
  1873. // perform better under different circumstances. in lieu of deleting it it's just been
  1874. // made uncallable
  1875. static $cache = array(
  1876. MATH_BIGINTEGER_VARIABLE => array(),
  1877. MATH_BIGINTEGER_DATA => array()
  1878. );
  1879. if(($key = array_search($m, $cache[MATH_BIGINTEGER_VARIABLE])) === false){
  1880. $key = count($cache[MATH_BIGINTEGER_VARIABLE]);
  1881. $cache[MATH_BIGINTEGER_VARIABLE][] = $m;
  1882. $cache[MATH_BIGINTEGER_DATA][] = $this->_modInverse67108864($m);
  1883. }
  1884. $n = max(count($x), count($y), count($m));
  1885. $x = array_pad($x, $n, 0);
  1886. $y = array_pad($y, $n, 0);
  1887. $m = array_pad($m, $n, 0);
  1888. $a = array(MATH_BIGINTEGER_VALUE => $this->_array_repeat(0, $n + 1));
  1889. for($i = 0; $i < $n; ++$i){
  1890. $temp = $a[MATH_BIGINTEGER_VALUE][0] + $x[$i] * $y[0];
  1891. $temp = $temp - MATH_BIGINTEGER_BASE_FULL * (MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31));
  1892. $temp = $temp * $cache[MATH_BIGINTEGER_DATA][$key];
  1893. $temp = $temp - MATH_BIGINTEGER_BASE_FULL * (MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31));
  1894. $temp = $this->_add($this->_regularMultiply(array($x[$i]), $y), false, $this->_regularMultiply(array($temp), $m), false);
  1895. $a = $this->_add($a[MATH_BIGINTEGER_VALUE], false, $temp[MATH_BIGINTEGER_VALUE], false);
  1896. $a[MATH_BIGINTEGER_VALUE] = array_slice($a[MATH_BIGINTEGER_VALUE], 1);
  1897. }
  1898. if($this->_compare($a[MATH_BIGINTEGER_VALUE], false, $m, false) >= 0){
  1899. $a = $this->_subtract($a[MATH_BIGINTEGER_VALUE], false, $m, false);
  1900. }
  1901. return $a[MATH_BIGINTEGER_VALUE];
  1902. }
  1903. /**
  1904. * Prepare a number for use in Montgomery Modular Reductions
  1905. *
  1906. * @see _montgomery()
  1907. * @see _slidingWindow()
  1908. * @access private
  1909. * @param Array $x
  1910. * @param Array $n
  1911. * @return Array
  1912. */
  1913. private function _prepMontgomery($x, $n){
  1914. $lhs = new BigInteger();
  1915. $lhs->value = array_merge($this->_array_repeat(0, count($n)), $x);
  1916. $rhs = new BigInteger();
  1917. $rhs->value = $n;
  1918. list(, $temp) = $lhs->divide($rhs);
  1919. return $temp->value;
  1920. }
  1921. /**
  1922. * Modular Inverse of a number mod 2**26 (eg. 67108864)
  1923. *
  1924. * Based off of the bnpInvDigit function implemented and justified in the following URL:
  1925. *
  1926. * {@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js}
  1927. *
  1928. * The following URL provides more info:
  1929. *
  1930. * {@link http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85}
  1931. *
  1932. * As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For
  1933. * instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields
  1934. * int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't
  1935. * auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that
  1936. * the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the
  1937. * maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to
  1938. * 40 bits, which only 64-bit floating points will support.
  1939. *
  1940. * Thanks to Pedro Gimeno Fortea for input!
  1941. *
  1942. * @see _montgomery()
  1943. * @access private
  1944. * @param Array $x
  1945. * @return Integer
  1946. */
  1947. private function _modInverse67108864($x){ // 2**26 == 67,108,864
  1948. $x = -$x[0];
  1949. $result = $x & 0x3; // x**-1 mod 2**2
  1950. $result = ($result * (2 - $x * $result)) & 0xF; // x**-1 mod 2**4
  1951. $result = ($result * (2 - ($x & 0xFF) * $result)) & 0xFF; // x**-1 mod 2**8
  1952. $result = ($result * ((2 - ($x & 0xFFFF) * $result) & 0xFFFF)) & 0xFFFF; // x**-1 mod 2**16
  1953. $result = fmod($result * (2 - fmod($x * $result, MATH_BIGINTEGER_BASE_FULL)), MATH_BIGINTEGER_BASE_FULL); // x**-1 mod 2**26
  1954. return $result & MATH_BIGINTEGER_MAX_DIGIT;
  1955. }
  1956. /**
  1957. * Calculates modular inverses.
  1958. *
  1959. * Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
  1960. *
  1961. * Here's an example:
  1962. * <code>
  1963. * <?php
  1964. * include 'Math/BigInteger.php';
  1965. *
  1966. * $a = new BigInteger(30);
  1967. * $b = new BigInteger(17);
  1968. *
  1969. * $c = $a->modInverse($b);
  1970. * echo $c->toString(); // outputs 4
  1971. *
  1972. * echo "\r\n";
  1973. *
  1974. * $d = $a->multiply($c);
  1975. * list(, $d) = $d->divide($b);
  1976. * echo $d; // outputs 1 (as per the definition of modular inverse)
  1977. * ?>
  1978. * </code>
  1979. *
  1980. * @param BigInteger $n
  1981. * @return mixed false, if no modular inverse exists, BigInteger, otherwise.
  1982. * @access public
  1983. * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information.
  1984. */
  1985. public function modInverse($n){
  1986. switch(MATH_BIGINTEGER_MODE){
  1987. case MATH_BIGINTEGER_MODE_GMP:
  1988. $temp = new BigInteger();
  1989. $temp->value = gmp_invert($this->value, $n->value);
  1990. return ( $temp->value === false ) ? false : $this->_normalize($temp);
  1991. }
  1992. static $zero, $one;
  1993. if(!isset($zero)){
  1994. $zero = new BigInteger();
  1995. $one = new BigInteger(1);
  1996. }
  1997. // $x mod -$n == $x mod $n.
  1998. $n = $n->abs();
  1999. if($this->compare($zero) < 0){
  2000. $temp = $this->abs();
  2001. $temp = $temp->modInverse($n);
  2002. return $this->_normalize($n->subtract($temp));
  2003. }
  2004. extract($this->extendedGCD($n));
  2005. if(!$gcd->equals($one)){
  2006. return false;
  2007. }
  2008. $x = $x->compare($zero) < 0 ? $x->add($n) : $x;
  2009. return $this->compare($zero) < 0 ? $this->_normalize($n->subtract($x)) : $this->_normalize($x);
  2010. }
  2011. /**
  2012. * Calculates the greatest common divisor and Bezout's identity.
  2013. *
  2014. * Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that
  2015. * 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which
  2016. * combination is returned is dependant upon which mode is in use. See
  2017. * {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information.
  2018. *
  2019. * Here's an example:
  2020. * <code>
  2021. * <?php
  2022. * include 'Math/BigInteger.php';
  2023. *
  2024. * $a = new BigInteger(693);
  2025. * $b = new BigInteger(609);
  2026. *
  2027. * extract($a->extendedGCD($b));
  2028. *
  2029. * echo $gcd->toString() . "\r\n"; // outputs 21
  2030. * echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21
  2031. * ?>
  2032. * </code>
  2033. *
  2034. * @param BigInteger $n
  2035. * @return BigInteger
  2036. * @access public
  2037. * @internal Calculates the GCD using the binary xGCD algorithim described in
  2038. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=19 HAC 14.61}. As the text above 14.61 notes,
  2039. * the more traditional algorithim requires "relatively costly multiple-precision divisions".
  2040. */
  2041. public function extendedGCD($n){
  2042. switch(MATH_BIGINTEGER_MODE){
  2043. case MATH_BIGINTEGER_MODE_GMP:
  2044. extract(gmp_gcdext($this->value, $n->value));
  2045. return array(
  2046. 'gcd' => $this->_normalize(new BigInteger($g)),
  2047. 'x' => $this->_normalize(new BigInteger($s)),
  2048. 'y' => $this->_normalize(new BigInteger($t))
  2049. );
  2050. case MATH_BIGINTEGER_MODE_BCMATH:
  2051. // it might be faster to use the binary xGCD algorithim here, as well, but (1) that algorithim works
  2052. // best when the base is a power of 2 and (2) i don't think it'd make much difference, anyway. as is,
  2053. // the basic extended euclidean algorithim is what we're using.
  2054. $u = $this->value;
  2055. $v = $n->value;
  2056. $a = '1';
  2057. $b = '0';
  2058. $c = '0';
  2059. $d = '1';
  2060. while(bccomp($v, '0', 0) != 0){
  2061. $q = bcdiv($u, $v, 0);
  2062. $temp = $u;
  2063. $u = $v;
  2064. $v = bcsub($temp, bcmul($v, $q, 0), 0);
  2065. $temp = $a;
  2066. $a = $c;
  2067. $c = bcsub($temp, bcmul($a, $q, 0), 0);
  2068. $temp = $b;
  2069. $b = $d;
  2070. $d = bcsub($temp, bcmul($b, $q, 0), 0);
  2071. }
  2072. return array(
  2073. 'gcd' => $this->_normalize(new BigInteger($u)),
  2074. 'x' => $this->_normalize(new BigInteger($a)),
  2075. 'y' => $this->_normalize(new BigInteger($b))
  2076. );
  2077. }
  2078. $y = $n->copy();
  2079. $x = $this->copy();
  2080. $g = new BigInteger();
  2081. $g->value = array(1);
  2082. while(!(($x->value[0] & 1) || ($y->value[0] & 1))){
  2083. $x->_rshift(1);
  2084. $y->_rshift(1);
  2085. $g->_lshift(1);
  2086. }
  2087. $u = $x->copy();
  2088. $v = $y->copy();
  2089. $a = new BigInteger();
  2090. $b = new BigInteger();
  2091. $c = new BigInteger();
  2092. $d = new BigInteger();
  2093. $a->value = $d->value = $g->value = array(1);
  2094. $b->value = $c->value = array();
  2095. while(!empty($u->value)){
  2096. while(!($u->value[0] & 1)){
  2097. $u->_rshift(1);
  2098. if((!empty($a->value) && ($a->value[0] & 1)) || (!empty($b->value) && ($b->value[0] & 1))){
  2099. $a = $a->add($y);
  2100. $b = $b->subtract($x);
  2101. }
  2102. $a->_rshift(1);
  2103. $b->_rshift(1);
  2104. }
  2105. while(!($v->value[0] & 1)){
  2106. $v->_rshift(1);
  2107. if((!empty($d->value) && ($d->value[0] & 1)) || (!empty($c->value) && ($c->value[0] & 1))){
  2108. $c = $c->add($y);
  2109. $d = $d->subtract($x);
  2110. }
  2111. $c->_rshift(1);
  2112. $d->_rshift(1);
  2113. }
  2114. if($u->compare($v) >= 0){
  2115. $u = $u->subtract($v);
  2116. $a = $a->subtract($c);
  2117. $b = $b->subtract($d);
  2118. }else{
  2119. $v = $v->subtract($u);
  2120. $c = $c->subtract($a);
  2121. $d = $d->subtract($b);
  2122. }
  2123. }
  2124. return array(
  2125. 'gcd' => $this->_normalize($g->multiply($v)),
  2126. 'x' => $this->_normalize($c),
  2127. 'y' => $this->_normalize($d)
  2128. );
  2129. }
  2130. /**
  2131. * Calculates the greatest common divisor
  2132. *
  2133. * Say you have 693 and 609. The GCD is 21.
  2134. *
  2135. * Here's an example:
  2136. * <code>
  2137. * <?php
  2138. * include 'Math/BigInteger.php';
  2139. *
  2140. * $a = new BigInteger(693);
  2141. * $b = new BigInteger(609);
  2142. *
  2143. * $gcd = a->extendedGCD($b);
  2144. *
  2145. * echo $gcd->toString() . "\r\n"; // outputs 21
  2146. * ?>
  2147. * </code>
  2148. *
  2149. * @param BigInteger $n
  2150. * @return BigInteger
  2151. * @access public
  2152. */
  2153. public function gcd($n){
  2154. extract($this->extendedGCD($n));
  2155. return $gcd;
  2156. }
  2157. /**
  2158. * Absolute value.
  2159. *
  2160. * @return BigInteger
  2161. * @access public
  2162. */
  2163. public function abs(){
  2164. $temp = new BigInteger();
  2165. switch(MATH_BIGINTEGER_MODE){
  2166. case MATH_BIGINTEGER_MODE_GMP:
  2167. $temp->value = gmp_abs($this->value);
  2168. break;
  2169. case MATH_BIGINTEGER_MODE_BCMATH:
  2170. $temp->value = (bccomp($this->value, '0', 0) < 0) ? substr($this->value, 1) : $this->value;
  2171. break;
  2172. default:
  2173. $temp->value = $this->value;
  2174. }
  2175. return $temp;
  2176. }
  2177. /**
  2178. * Compares two numbers.
  2179. *
  2180. * Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is
  2181. * demonstrated thusly:
  2182. *
  2183. * $x > $y: $x->compare($y) > 0
  2184. * $x < $y: $x->compare($y) < 0
  2185. * $x == $y: $x->compare($y) == 0
  2186. *
  2187. * Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y).
  2188. *
  2189. * @param BigInteger $y
  2190. * @return Integer < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
  2191. * @access public
  2192. * @see equals()
  2193. * @internal Could return $this->subtract($x), but that's not as fast as what we do do.
  2194. */
  2195. public function compare($y){
  2196. switch(MATH_BIGINTEGER_MODE){
  2197. case MATH_BIGINTEGER_MODE_GMP:
  2198. return gmp_cmp($this->value, $y->value);
  2199. case MATH_BIGINTEGER_MODE_BCMATH:
  2200. return bccomp($this->value, $y->value, 0);
  2201. }
  2202. return $this->_compare($this->value, $this->is_negative, $y->value, $y->is_negative);
  2203. }
  2204. /**
  2205. * Compares two numbers.
  2206. *
  2207. * @param Array $x_value
  2208. * @param Boolean $x_negative
  2209. * @param Array $y_value
  2210. * @param Boolean $y_negative
  2211. * @return Integer
  2212. * @see compare()
  2213. * @access private
  2214. */
  2215. private function _compare($x_value, $x_negative, $y_value, $y_negative){
  2216. if($x_negative != $y_negative){
  2217. return (!$x_negative && $y_negative ) ? 1 : -1;
  2218. }
  2219. $result = $x_negative ? -1 : 1;
  2220. if(count($x_value) != count($y_value)){
  2221. return ( count($x_value) > count($y_value) ) ? $result : -$result;
  2222. }
  2223. $size = max(count($x_value), count($y_value));
  2224. $x_value = array_pad($x_value, $size, 0);
  2225. $y_value = array_pad($y_value, $size, 0);
  2226. for($i = count($x_value) - 1; $i >= 0; --$i){
  2227. if($x_value[$i] != $y_value[$i]){
  2228. return ( $x_value[$i] > $y_value[$i] ) ? $result : -$result;
  2229. }
  2230. }
  2231. return 0;
  2232. }
  2233. /**
  2234. * Tests the equality of two numbers.
  2235. *
  2236. * If you need to see if one number is greater than or less than another number, use BigInteger::compare()
  2237. *
  2238. * @param BigInteger $x
  2239. * @return Boolean
  2240. * @access public
  2241. * @see compare()
  2242. */
  2243. public function equals($x){
  2244. switch(MATH_BIGINTEGER_MODE){
  2245. case MATH_BIGINTEGER_MODE_GMP:
  2246. return gmp_cmp($this->value, $x->value) == 0;
  2247. default:
  2248. return $this->value === $x->value && $this->is_negative == $x->is_negative;
  2249. }
  2250. }
  2251. /**
  2252. * Set Precision
  2253. *
  2254. * Some bitwise operations give different results depending on the precision being used. Examples include left
  2255. * shift, not, and rotates.
  2256. *
  2257. * @param Integer $bits
  2258. * @access public
  2259. */
  2260. public function setPrecision($bits){
  2261. $this->precision = $bits;
  2262. if(MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH){
  2263. $this->bitmask = new BigInteger(chr((1 << ($bits & 0x7)) - 1) . str_repeat(chr(0xFF), $bits >> 3), 256);
  2264. }else{
  2265. $this->bitmask = new BigInteger(bcpow('2', $bits, 0));
  2266. }
  2267. $temp = $this->_normalize($this);
  2268. $this->value = $temp->value;
  2269. }
  2270. /**
  2271. * Logical And
  2272. *
  2273. * @param BigInteger $x
  2274. * @access public
  2275. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2276. * @return BigInteger
  2277. */
  2278. public function bitwise_and($x){
  2279. switch(MATH_BIGINTEGER_MODE){
  2280. case MATH_BIGINTEGER_MODE_GMP:
  2281. $temp = new BigInteger();
  2282. $temp->value = gmp_and($this->value, $x->value);
  2283. return $this->_normalize($temp);
  2284. case MATH_BIGINTEGER_MODE_BCMATH:
  2285. $left = $this->toBytes();
  2286. $right = $x->toBytes();
  2287. $length = max(strlen($left), strlen($right));
  2288. $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
  2289. $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
  2290. return $this->_normalize(new BigInteger($left & $right, 256));
  2291. }
  2292. $result = $this->copy();
  2293. $length = min(count($x->value), count($this->value));
  2294. $result->value = array_slice($result->value, 0, $length);
  2295. for($i = 0; $i < $length; ++$i){
  2296. $result->value[$i]&= $x->value[$i];
  2297. }
  2298. return $this->_normalize($result);
  2299. }
  2300. /**
  2301. * Logical Or
  2302. *
  2303. * @param BigInteger $x
  2304. * @access public
  2305. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2306. * @return BigInteger
  2307. */
  2308. public function bitwise_or($x){
  2309. switch(MATH_BIGINTEGER_MODE){
  2310. case MATH_BIGINTEGER_MODE_GMP:
  2311. $temp = new BigInteger();
  2312. $temp->value = gmp_or($this->value, $x->value);
  2313. return $this->_normalize($temp);
  2314. case MATH_BIGINTEGER_MODE_BCMATH:
  2315. $left = $this->toBytes();
  2316. $right = $x->toBytes();
  2317. $length = max(strlen($left), strlen($right));
  2318. $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
  2319. $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
  2320. return $this->_normalize(new BigInteger($left | $right, 256));
  2321. }
  2322. $length = max(count($this->value), count($x->value));
  2323. $result = $this->copy();
  2324. $result->value = array_pad($result->value, $length, 0);
  2325. $x->value = array_pad($x->value, $length, 0);
  2326. for($i = 0; $i < $length; ++$i){
  2327. $result->value[$i]|= $x->value[$i];
  2328. }
  2329. return $this->_normalize($result);
  2330. }
  2331. /**
  2332. * Logical Exclusive-Or
  2333. *
  2334. * @param BigInteger $x
  2335. * @access public
  2336. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2337. * @return BigInteger
  2338. */
  2339. public function bitwise_xor($x){
  2340. switch(MATH_BIGINTEGER_MODE){
  2341. case MATH_BIGINTEGER_MODE_GMP:
  2342. $temp = new BigInteger();
  2343. $temp->value = gmp_xor($this->value, $x->value);
  2344. return $this->_normalize($temp);
  2345. case MATH_BIGINTEGER_MODE_BCMATH:
  2346. $left = $this->toBytes();
  2347. $right = $x->toBytes();
  2348. $length = max(strlen($left), strlen($right));
  2349. $left = str_pad($left, $length, chr(0), STR_PAD_LEFT);
  2350. $right = str_pad($right, $length, chr(0), STR_PAD_LEFT);
  2351. return $this->_normalize(new BigInteger($left ^ $right, 256));
  2352. }
  2353. $length = max(count($this->value), count($x->value));
  2354. $result = $this->copy();
  2355. $result->value = array_pad($result->value, $length, 0);
  2356. $x->value = array_pad($x->value, $length, 0);
  2357. for($i = 0; $i < $length; ++$i){
  2358. $result->value[$i]^= $x->value[$i];
  2359. }
  2360. return $this->_normalize($result);
  2361. }
  2362. /**
  2363. * Logical Not
  2364. *
  2365. * @access public
  2366. * @internal Implemented per a request by Lluis Pamies i Juarez <lluis _a_ pamies.cat>
  2367. * @return BigInteger
  2368. */
  2369. public function bitwise_not(){
  2370. // calculuate "not" without regard to $this->precision
  2371. // (will always result in a smaller number. ie. ~1 isn't 1111 1110 - it's 0)
  2372. $temp = $this->toBytes();
  2373. $pre_msb = decbin(ord($temp[0]));
  2374. $temp = ~$temp;
  2375. $msb = decbin(ord($temp[0]));
  2376. if(strlen($msb) == 8){
  2377. $msb = substr($msb, strpos($msb, '0'));
  2378. }
  2379. $temp[0] = chr(bindec($msb));
  2380. // see if we need to add extra leading 1's
  2381. $current_bits = strlen($pre_msb) + 8 * strlen($temp) - 8;
  2382. $new_bits = $this->precision - $current_bits;
  2383. if($new_bits <= 0){
  2384. return $this->_normalize(new BigInteger($temp, 256));
  2385. }
  2386. // generate as many leading 1's as we need to.
  2387. $leading_ones = chr((1 << ($new_bits & 0x7)) - 1) . str_repeat(chr(0xFF), $new_bits >> 3);
  2388. $this->_base256_lshift($leading_ones, $current_bits);
  2389. $temp = str_pad($temp, ceil($this->bits / 8), chr(0), STR_PAD_LEFT);
  2390. return $this->_normalize(new BigInteger($leading_ones | $temp, 256));
  2391. }
  2392. /**
  2393. * Logical Right Shift
  2394. *
  2395. * Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
  2396. *
  2397. * @param Integer $shift
  2398. * @return BigInteger
  2399. * @access public
  2400. * @internal The only version that yields any speed increases is the internal version.
  2401. */
  2402. public function bitwise_rightShift($shift){
  2403. $temp = new BigInteger();
  2404. switch(MATH_BIGINTEGER_MODE){
  2405. case MATH_BIGINTEGER_MODE_GMP:
  2406. static $two;
  2407. if(!isset($two)){
  2408. $two = gmp_init('2');
  2409. }
  2410. $temp->value = gmp_div_q($this->value, gmp_pow($two, $shift));
  2411. break;
  2412. case MATH_BIGINTEGER_MODE_BCMATH:
  2413. $temp->value = bcdiv($this->value, bcpow('2', $shift, 0), 0);
  2414. break;
  2415. default: // could just replace _lshift with this, but then all _lshift() calls would need to be rewritten
  2416. // and I don't want to do that...
  2417. $temp->value = $this->value;
  2418. $temp->_rshift($shift);
  2419. }
  2420. return $this->_normalize($temp);
  2421. }
  2422. /**
  2423. * Logical Left Shift
  2424. *
  2425. * Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
  2426. *
  2427. * @param Integer $shift
  2428. * @return BigInteger
  2429. * @access public
  2430. * @internal The only version that yields any speed increases is the internal version.
  2431. */
  2432. public function bitwise_leftShift($shift){
  2433. $temp = new BigInteger();
  2434. switch(MATH_BIGINTEGER_MODE){
  2435. case MATH_BIGINTEGER_MODE_GMP:
  2436. static $two;
  2437. if(!isset($two)){
  2438. $two = gmp_init('2');
  2439. }
  2440. $temp->value = gmp_mul($this->value, gmp_pow($two, $shift));
  2441. break;
  2442. case MATH_BIGINTEGER_MODE_BCMATH:
  2443. $temp->value = bcmul($this->value, bcpow('2', $shift, 0), 0);
  2444. break;
  2445. default: // could just replace _rshift with this, but then all _lshift() calls would need to be rewritten
  2446. // and I don't want to do that...
  2447. $temp->value = $this->value;
  2448. $temp->_lshift($shift);
  2449. }
  2450. return $this->_normalize($temp);
  2451. }
  2452. /**
  2453. * Logical Left Rotate
  2454. *
  2455. * Instead of the top x bits being dropped they're appended to the shifted bit string.
  2456. *
  2457. * @param Integer $shift
  2458. * @return BigInteger
  2459. * @access public
  2460. */
  2461. public function bitwise_leftRotate($shift){
  2462. $bits = $this->toBytes();
  2463. if($this->precision > 0){
  2464. $precision = $this->precision;
  2465. if(MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH){
  2466. $mask = $this->bitmask->subtract(new BigInteger(1));
  2467. $mask = $mask->toBytes();
  2468. }else{
  2469. $mask = $this->bitmask->toBytes();
  2470. }
  2471. }else{
  2472. $temp = ord($bits[0]);
  2473. for($i = 0; $temp >> $i; ++$i)
  2474. ;
  2475. $precision = 8 * strlen($bits) - 8 + $i;
  2476. $mask = chr((1 << ($precision & 0x7)) - 1) . str_repeat(chr(0xFF), $precision >> 3);
  2477. }
  2478. if($shift < 0){
  2479. $shift+= $precision;
  2480. }
  2481. $shift%= $precision;
  2482. if(!$shift){
  2483. return $this->copy();
  2484. }
  2485. $left = $this->bitwise_leftShift($shift);
  2486. $left = $left->bitwise_and(new BigInteger($mask, 256));
  2487. $right = $this->bitwise_rightShift($precision - $shift);
  2488. $result = MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_BCMATH ? $left->bitwise_or($right) : $left->add($right);
  2489. return $this->_normalize($result);
  2490. }
  2491. /**
  2492. * Logical Right Rotate
  2493. *
  2494. * Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
  2495. *
  2496. * @param Integer $shift
  2497. * @return BigInteger
  2498. * @access public
  2499. */
  2500. public function bitwise_rightRotate($shift){
  2501. return $this->bitwise_leftRotate(-$shift);
  2502. }
  2503. /**
  2504. * Set random number generator function
  2505. *
  2506. * This function is deprecated.
  2507. *
  2508. * @param String $generator
  2509. * @access public
  2510. */
  2511. public function setRandomGenerator($generator){
  2512. }
  2513. /**
  2514. * Generates a random BigInteger
  2515. *
  2516. * Byte length is equal to $length. Uses crypt_random if it's loaded and mt_rand if it's not.
  2517. *
  2518. * @param Integer $length
  2519. * @return BigInteger
  2520. * @access private
  2521. */
  2522. private function _random_number_helper($size){
  2523. if(function_exists('crypt_random_string')){
  2524. $random = crypt_random_string($size);
  2525. }else{
  2526. $random = '';
  2527. if($size & 1){
  2528. $random.= chr(mt_rand(0, 255));
  2529. }
  2530. $blocks = $size >> 1;
  2531. for($i = 0; $i < $blocks; ++$i){
  2532. // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems
  2533. $random.= pack('n', mt_rand(0, 0xFFFF));
  2534. }
  2535. }
  2536. return new BigInteger($random, 256);
  2537. }
  2538. /**
  2539. * Generate a random number
  2540. *
  2541. * Returns a random number between $min and $max where $min and $max
  2542. * can be defined using one of the two methods:
  2543. *
  2544. * $min->random($max)
  2545. * $max->random($min)
  2546. *
  2547. * @param BigInteger $arg1
  2548. * @param optional BigInteger $arg2
  2549. * @return BigInteger
  2550. * @access public
  2551. * @internal The API for creating random numbers used to be $a->random($min, $max), where $a was a BigInteger object.
  2552. * That method is still supported for BC purposes.
  2553. */
  2554. public function random($arg1, $arg2 = false){
  2555. if($arg1 === false){
  2556. return false;
  2557. }
  2558. if($arg2 === false){
  2559. $max = $arg1;
  2560. $min = $this;
  2561. }else{
  2562. $min = $arg1;
  2563. $max = $arg2;
  2564. }
  2565. $compare = $max->compare($min);
  2566. if(!$compare){
  2567. return $this->_normalize($min);
  2568. }else if($compare < 0){
  2569. // if $min is bigger then $max, swap $min and $max
  2570. $temp = $max;
  2571. $max = $min;
  2572. $min = $temp;
  2573. }
  2574. static $one;
  2575. if(!isset($one)){
  2576. $one = new BigInteger(1);
  2577. }
  2578. $max = $max->subtract($min->subtract($one));
  2579. $size = strlen(ltrim($max->toBytes(), chr(0)));
  2580. /*
  2581. doing $random % $max doesn't work because some numbers will be more likely to occur than others.
  2582. eg. if $max is 140 and $random's max is 255 then that'd mean both $random = 5 and $random = 145
  2583. would produce 5 whereas the only value of random that could produce 139 would be 139. ie.
  2584. not all numbers would be equally likely. some would be more likely than others.
  2585. creating a whole new random number until you find one that is within the range doesn't work
  2586. because, for sufficiently small ranges, the likelihood that you'd get a number within that range
  2587. would be pretty small. eg. with $random's max being 255 and if your $max being 1 the probability
  2588. would be pretty high that $random would be greater than $max.
  2589. phpseclib works around this using the technique described here:
  2590. http://crypto.stackexchange.com/questions/5708/creating-a-small-number-from-a-cryptographically-secure-random-string
  2591. */
  2592. $random_max = new BigInteger(chr(1) . str_repeat("\0", $size), 256);
  2593. $random = $this->_random_number_helper($size);
  2594. list($max_multiple) = $random_max->divide($max);
  2595. $max_multiple = $max_multiple->multiply($max);
  2596. while($random->compare($max_multiple) >= 0){
  2597. $random = $random->subtract($max_multiple);
  2598. $random_max = $random_max->subtract($max_multiple);
  2599. $random = $random->bitwise_leftShift(8);
  2600. $random = $random->add($this->_random_number_helper(1));
  2601. $random_max = $random_max->bitwise_leftShift(8);
  2602. list($max_multiple) = $random_max->divide($max);
  2603. $max_multiple = $max_multiple->multiply($max);
  2604. }
  2605. list(, $random) = $random->divide($max);
  2606. return $this->_normalize($random->add($min));
  2607. }
  2608. /**
  2609. * Generate a random prime number.
  2610. *
  2611. * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed,
  2612. * give up and return false.
  2613. *
  2614. * @param BigInteger $arg1
  2615. * @param optional BigInteger $arg2
  2616. * @param optional Integer $timeout
  2617. * @return Mixed
  2618. * @access public
  2619. * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}.
  2620. */
  2621. public function randomPrime($arg1, $arg2 = false, $timeout = false){
  2622. if($arg1 === false){
  2623. return false;
  2624. }
  2625. if($arg2 === false){
  2626. $max = $arg1;
  2627. $min = $this;
  2628. }else{
  2629. $min = $arg1;
  2630. $max = $arg2;
  2631. }
  2632. $compare = $max->compare($min);
  2633. if(!$compare){
  2634. return $min->isPrime() ? $min : false;
  2635. }else if($compare < 0){
  2636. // if $min is bigger then $max, swap $min and $max
  2637. $temp = $max;
  2638. $max = $min;
  2639. $min = $temp;
  2640. }
  2641. static $one, $two;
  2642. if(!isset($one)){
  2643. $one = new BigInteger(1);
  2644. $two = new BigInteger(2);
  2645. }
  2646. $start = time();
  2647. $x = $this->random($min, $max);
  2648. // gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>.
  2649. if(MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime')){
  2650. $p = new BigInteger();
  2651. $p->value = gmp_nextprime($x->value);
  2652. if($p->compare($max) <= 0){
  2653. return $p;
  2654. }
  2655. if(!$min->equals($x)){
  2656. $x = $x->subtract($one);
  2657. }
  2658. return $x->randomPrime($min, $x);
  2659. }
  2660. if($x->equals($two)){
  2661. return $x;
  2662. }
  2663. $x->_make_odd();
  2664. if($x->compare($max) > 0){
  2665. // if $x > $max then $max is even and if $min == $max then no prime number exists between the specified range
  2666. if($min->equals($max)){
  2667. return false;
  2668. }
  2669. $x = $min->copy();
  2670. $x->_make_odd();
  2671. }
  2672. $initial_x = $x->copy();
  2673. while(true){
  2674. if($timeout !== false && time() - $start > $timeout){
  2675. return false;
  2676. }
  2677. if($x->isPrime()){
  2678. return $x;
  2679. }
  2680. $x = $x->add($two);
  2681. if($x->compare($max) > 0){
  2682. $x = $min->copy();
  2683. if($x->equals($two)){
  2684. return $x;
  2685. }
  2686. $x->_make_odd();
  2687. }
  2688. if($x->equals($initial_x)){
  2689. return false;
  2690. }
  2691. }
  2692. }
  2693. /**
  2694. * Make the current number odd
  2695. *
  2696. * If the current number is odd it'll be unchanged. If it's even, one will be added to it.
  2697. *
  2698. * @see randomPrime()
  2699. * @access private
  2700. */
  2701. private function _make_odd(){
  2702. switch(MATH_BIGINTEGER_MODE){
  2703. case MATH_BIGINTEGER_MODE_GMP:
  2704. gmp_setbit($this->value, 0);
  2705. break;
  2706. case MATH_BIGINTEGER_MODE_BCMATH:
  2707. if($this->value[strlen($this->value) - 1] % 2 == 0){
  2708. $this->value = bcadd($this->value, '1');
  2709. }
  2710. break;
  2711. default:
  2712. $this->value[0] |= 1;
  2713. }
  2714. }
  2715. /**
  2716. * Checks a numer to see if it's prime
  2717. *
  2718. * Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the
  2719. * $t parameter is distributability. BigInteger::randomPrime() can be distributed across multiple pageloads
  2720. * on a website instead of just one.
  2721. *
  2722. * @param optional BigInteger $t
  2723. * @return Boolean
  2724. * @access public
  2725. * @internal Uses the
  2726. * {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See
  2727. * {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24}.
  2728. */
  2729. public function isPrime($t = false){
  2730. $length = strlen($this->toBytes());
  2731. if(!$t){
  2732. // see HAC 4.49 "Note (controlling the error probability)"
  2733. // @codingStandardsIgnoreStart
  2734. if($length >= 163){
  2735. $t = 2;
  2736. } // floor(1300 / 8)
  2737. else if($length >= 106){
  2738. $t = 3;
  2739. } // floor( 850 / 8)
  2740. else if($length >= 81){
  2741. $t = 4;
  2742. } // floor( 650 / 8)
  2743. else if($length >= 68){
  2744. $t = 5;
  2745. } // floor( 550 / 8)
  2746. else if($length >= 56){
  2747. $t = 6;
  2748. } // floor( 450 / 8)
  2749. else if($length >= 50){
  2750. $t = 7;
  2751. } // floor( 400 / 8)
  2752. else if($length >= 43){
  2753. $t = 8;
  2754. } // floor( 350 / 8)
  2755. else if($length >= 37){
  2756. $t = 9;
  2757. } // floor( 300 / 8)
  2758. else if($length >= 31){
  2759. $t = 12;
  2760. } // floor( 250 / 8)
  2761. else if($length >= 25){
  2762. $t = 15;
  2763. } // floor( 200 / 8)
  2764. else if($length >= 18){
  2765. $t = 18;
  2766. } // floor( 150 / 8)
  2767. else{
  2768. $t = 27;
  2769. }
  2770. // @codingStandardsIgnoreEnd
  2771. }
  2772. // ie. gmp_testbit($this, 0)
  2773. // ie. isEven() or !isOdd()
  2774. switch(MATH_BIGINTEGER_MODE){
  2775. case MATH_BIGINTEGER_MODE_GMP:
  2776. return gmp_prob_prime($this->value, $t) != 0;
  2777. case MATH_BIGINTEGER_MODE_BCMATH:
  2778. if($this->value === '2'){
  2779. return true;
  2780. }
  2781. if($this->value[strlen($this->value) - 1] % 2 == 0){
  2782. return false;
  2783. }
  2784. break;
  2785. default:
  2786. if($this->value == array(2)){
  2787. return true;
  2788. }
  2789. if(~$this->value[0] & 1){
  2790. return false;
  2791. }
  2792. }
  2793. static $primes, $zero, $one, $two;
  2794. if(!isset($primes)){
  2795. $primes = array(
  2796. 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59,
  2797. 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137,
  2798. 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227,
  2799. 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,
  2800. 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419,
  2801. 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509,
  2802. 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617,
  2803. 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727,
  2804. 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829,
  2805. 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947,
  2806. 953, 967, 971, 977, 983, 991, 997
  2807. );
  2808. if(MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL){
  2809. for($i = 0; $i < count($primes); ++$i){
  2810. $primes[$i] = new BigInteger($primes[$i]);
  2811. }
  2812. }
  2813. $zero = new BigInteger();
  2814. $one = new BigInteger(1);
  2815. $two = new BigInteger(2);
  2816. }
  2817. if($this->equals($one)){
  2818. return false;
  2819. }
  2820. // see HAC 4.4.1 "Random search for probable primes"
  2821. if(MATH_BIGINTEGER_MODE != MATH_BIGINTEGER_MODE_INTERNAL){
  2822. foreach($primes as $prime){
  2823. list(, $r) = $this->divide($prime);
  2824. if($r->equals($zero)){
  2825. return $this->equals($prime);
  2826. }
  2827. }
  2828. }else{
  2829. $value = $this->value;
  2830. foreach($primes as $prime){
  2831. list(, $r) = $this->_divide_digit($value, $prime);
  2832. if(!$r){
  2833. return count($value) == 1 && $value[0] == $prime;
  2834. }
  2835. }
  2836. }
  2837. $n = $this->copy();
  2838. $n_1 = $n->subtract($one);
  2839. $n_2 = $n->subtract($two);
  2840. $r = $n_1->copy();
  2841. $r_value = $r->value;
  2842. // ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));
  2843. if(MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_BCMATH){
  2844. $s = 0;
  2845. // if $n was 1, $r would be 0 and this would be an infinite loop, hence our $this->equals($one) check earlier
  2846. while($r->value[strlen($r->value) - 1] % 2 == 0){
  2847. $r->value = bcdiv($r->value, '2', 0);
  2848. ++$s;
  2849. }
  2850. }else{
  2851. for($i = 0, $r_length = count($r_value); $i < $r_length; ++$i){
  2852. $temp = ~$r_value[$i] & 0xFFFFFF;
  2853. for($j = 1; ($temp >> $j) & 1; ++$j)
  2854. ;
  2855. if($j != 25){
  2856. break;
  2857. }
  2858. }
  2859. $s = 26 * $i + $j - 1;
  2860. $r->_rshift($s);
  2861. }
  2862. for($i = 0; $i < $t; ++$i){
  2863. $a = $this->random($two, $n_2);
  2864. $y = $a->modPow($r, $n);
  2865. if(!$y->equals($one) && !$y->equals($n_1)){
  2866. for($j = 1; $j < $s && !$y->equals($n_1); ++$j){
  2867. $y = $y->modPow($two, $n);
  2868. if($y->equals($one)){
  2869. return false;
  2870. }
  2871. }
  2872. if(!$y->equals($n_1)){
  2873. return false;
  2874. }
  2875. }
  2876. }
  2877. return true;
  2878. }
  2879. /**
  2880. * Logical Left Shift
  2881. *
  2882. * Shifts BigInteger's by $shift bits.
  2883. *
  2884. * @param Integer $shift
  2885. * @access private
  2886. */
  2887. private function _lshift($shift){
  2888. if($shift == 0){
  2889. return;
  2890. }
  2891. $num_digits = (int) ($shift / MATH_BIGINTEGER_BASE);
  2892. $shift %= MATH_BIGINTEGER_BASE;
  2893. $shift = 1 << $shift;
  2894. $carry = 0;
  2895. for($i = 0; $i < count($this->value); ++$i){
  2896. $temp = $this->value[$i] * $shift + $carry;
  2897. $carry = MATH_BIGINTEGER_BASE === 26 ? intval($temp / 0x4000000) : ($temp >> 31);
  2898. $this->value[$i] = (int) ($temp - $carry * MATH_BIGINTEGER_BASE_FULL);
  2899. }
  2900. if($carry){
  2901. $this->value[] = $carry;
  2902. }
  2903. while($num_digits--){
  2904. array_unshift($this->value, 0);
  2905. }
  2906. }
  2907. /**
  2908. * Logical Right Shift
  2909. *
  2910. * Shifts BigInteger's by $shift bits.
  2911. *
  2912. * @param Integer $shift
  2913. * @access private
  2914. */
  2915. private function _rshift($shift){
  2916. if($shift == 0){
  2917. return;
  2918. }
  2919. $num_digits = (int) ($shift / MATH_BIGINTEGER_BASE);
  2920. $shift %= MATH_BIGINTEGER_BASE;
  2921. $carry_shift = MATH_BIGINTEGER_BASE - $shift;
  2922. $carry_mask = (1 << $shift) - 1;
  2923. if($num_digits){
  2924. $this->value = array_slice($this->value, $num_digits);
  2925. }
  2926. $carry = 0;
  2927. for($i = count($this->value) - 1; $i >= 0; --$i){
  2928. $temp = $this->value[$i] >> $shift | $carry;
  2929. $carry = ($this->value[$i] & $carry_mask) << $carry_shift;
  2930. $this->value[$i] = $temp;
  2931. }
  2932. $this->value = $this->_trim($this->value);
  2933. }
  2934. /**
  2935. * Normalize
  2936. *
  2937. * Removes leading zeros and truncates (if necessary) to maintain the appropriate precision
  2938. *
  2939. * @param BigInteger
  2940. * @return BigInteger
  2941. * @see _trim()
  2942. * @access private
  2943. */
  2944. private function _normalize($result){
  2945. $result->precision = $this->precision;
  2946. $result->bitmask = $this->bitmask;
  2947. switch(MATH_BIGINTEGER_MODE){
  2948. case MATH_BIGINTEGER_MODE_GMP:
  2949. if(!empty($result->bitmask->value)){
  2950. $result->value = gmp_and($result->value, $result->bitmask->value);
  2951. }
  2952. return $result;
  2953. case MATH_BIGINTEGER_MODE_BCMATH:
  2954. if(!empty($result->bitmask->value)){
  2955. $result->value = bcmod($result->value, $result->bitmask->value);
  2956. }
  2957. return $result;
  2958. }
  2959. $value = &$result->value;
  2960. if(!count($value)){
  2961. return $result;
  2962. }
  2963. $value = $this->_trim($value);
  2964. if(!empty($result->bitmask->value)){
  2965. $length = min(count($value), count($this->bitmask->value));
  2966. $value = array_slice($value, 0, $length);
  2967. for($i = 0; $i < $length; ++$i){
  2968. $value[$i] = $value[$i] & $this->bitmask->value[$i];
  2969. }
  2970. }
  2971. return $result;
  2972. }
  2973. /**
  2974. * Trim
  2975. *
  2976. * Removes leading zeros
  2977. *
  2978. * @param Array $value
  2979. * @return BigInteger
  2980. * @access private
  2981. */
  2982. private function _trim($value){
  2983. for($i = count($value) - 1; $i >= 0; --$i){
  2984. if($value[$i]){
  2985. break;
  2986. }
  2987. unset($value[$i]);
  2988. }
  2989. return $value;
  2990. }
  2991. /**
  2992. * Array Repeat
  2993. *
  2994. * @param $input Array
  2995. * @param $multiplier mixed
  2996. * @return Array
  2997. * @access private
  2998. */
  2999. private function _array_repeat($input, $multiplier){
  3000. return ($multiplier) ? array_fill(0, $multiplier, $input) : array();
  3001. }
  3002. /**
  3003. * Logical Left Shift
  3004. *
  3005. * Shifts binary strings $shift bits, essentially multiplying by 2**$shift.
  3006. *
  3007. * @param $x String
  3008. * @param $shift Integer
  3009. * @return String
  3010. * @access private
  3011. */
  3012. private function _base256_lshift(&$x, $shift){
  3013. if($shift == 0){
  3014. return;
  3015. }
  3016. $num_bytes = $shift >> 3; // eg. floor($shift/8)
  3017. $shift &= 7; // eg. $shift % 8
  3018. $carry = 0;
  3019. for($i = strlen($x) - 1; $i >= 0; --$i){
  3020. $temp = ord($x[$i]) << $shift | $carry;
  3021. $x[$i] = chr($temp);
  3022. $carry = $temp >> 8;
  3023. }
  3024. $carry = ($carry != 0) ? chr($carry) : '';
  3025. $x = $carry . $x . str_repeat(chr(0), $num_bytes);
  3026. }
  3027. /**
  3028. * Logical Right Shift
  3029. *
  3030. * Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder.
  3031. *
  3032. * @param $x String
  3033. * @param $shift Integer
  3034. * @return String
  3035. * @access private
  3036. */
  3037. private function _base256_rshift(&$x, $shift){
  3038. if($shift == 0){
  3039. $x = ltrim($x, chr(0));
  3040. return '';
  3041. }
  3042. $num_bytes = $shift >> 3; // eg. floor($shift/8)
  3043. $shift &= 7; // eg. $shift % 8
  3044. $remainder = '';
  3045. if($num_bytes){
  3046. $start = $num_bytes > strlen($x) ? -strlen($x) : -$num_bytes;
  3047. $remainder = substr($x, $start);
  3048. $x = substr($x, 0, -$num_bytes);
  3049. }
  3050. $carry = 0;
  3051. $carry_shift = 8 - $shift;
  3052. for($i = 0; $i < strlen($x); ++$i){
  3053. $temp = (ord($x[$i]) >> $shift) | $carry;
  3054. $carry = (ord($x[$i]) << $carry_shift) & 0xFF;
  3055. $x[$i] = chr($temp);
  3056. }
  3057. $x = ltrim($x, chr(0));
  3058. $remainder = chr($carry >> $carry_shift) . $remainder;
  3059. return ltrim($remainder, chr(0));
  3060. }
  3061. // one quirk about how the following functions are implemented is that PHP defines N to be an unsigned long
  3062. // at 32-bits, while java's longs are 64-bits.
  3063. /**
  3064. * Converts 32-bit integers to bytes.
  3065. *
  3066. * @param Integer $x
  3067. * @return String
  3068. * @access private
  3069. */
  3070. private function _int2bytes($x){
  3071. return ltrim(pack('N', $x), chr(0));
  3072. }
  3073. /**
  3074. * Converts bytes to 32-bit integers
  3075. *
  3076. * @param String $x
  3077. * @return Integer
  3078. * @access private
  3079. */
  3080. private function _bytes2int($x){
  3081. $temp = unpack('Nint', str_pad($x, 4, chr(0), STR_PAD_LEFT));
  3082. return $temp['int'];
  3083. }
  3084. /**
  3085. * DER-encode an integer
  3086. *
  3087. * The ability to DER-encode integers is needed to create RSA public keys for use with OpenSSL
  3088. *
  3089. * @see modPow()
  3090. * @access private
  3091. * @param Integer $length
  3092. * @return String
  3093. */
  3094. private function _encodeASN1Length($length){
  3095. if($length <= 0x7F){
  3096. return chr($length);
  3097. }
  3098. $temp = ltrim(pack('N', $length), chr(0));
  3099. return pack('Ca*', 0x80 | strlen($temp), $temp);
  3100. }
  3101. /**
  3102. * Single digit division
  3103. *
  3104. * Even if int64 is being used the division operator will return a float64 value
  3105. * if the dividend is not evenly divisible by the divisor. Since a float64 doesn't
  3106. * have the precision of int64 this is a problem so, when int64 is being used,
  3107. * we'll guarantee that the dividend is divisible by first subtracting the remainder.
  3108. *
  3109. * @access private
  3110. * @param Integer $x
  3111. * @param Integer $y
  3112. * @return Integer
  3113. */
  3114. private function _safe_divide($x, $y){
  3115. if(MATH_BIGINTEGER_BASE === 26){
  3116. return (int) ($x / $y);
  3117. }
  3118. // MATH_BIGINTEGER_BASE === 31
  3119. return ($x - ($x % $y)) / $y;
  3120. }
  3121. }