PageRenderTime 71ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/phpseclib/Math/BigInteger.php

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