/library/Zend/Crypt/Math/BigInteger/Gmp.php

https://github.com/sidealice/zf2 · PHP · 205 lines · 85 code · 14 blank · 106 comment · 5 complexity · cde905930c44f7d8a5b087160cf69824 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage Math
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Crypt\Math\BigInteger;
  25. /**
  26. * Support for arbitrary precision mathematics in PHP.
  27. *
  28. * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
  29. * extension.
  30. *
  31. * @uses Zend\Crypt\Math\BigInteger\BigIntegerCapable
  32. * @category Zend
  33. * @package Zend_Crypt
  34. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Gmp implements BigIntegerCapable
  38. {
  39. /**
  40. * Initialise a big integer into an extension specific type.
  41. *
  42. * @param string $operand
  43. * @param int $base
  44. * @return string
  45. */
  46. public function init($operand, $base = 10)
  47. {
  48. return $operand;
  49. }
  50. /**
  51. * Adds two arbitrary precision numbers
  52. *
  53. * @param string $left_operand
  54. * @param string $right_operand
  55. * @return string
  56. */
  57. public function add($left_operand, $right_operand)
  58. {
  59. $result = gmp_add($left_operand, $right_operand);
  60. return gmp_strval($result);
  61. }
  62. /**
  63. * @param string $left_operand
  64. * @param string $right_operand
  65. * @return string
  66. */
  67. public function subtract($left_operand, $right_operand)
  68. {
  69. $result = gmp_sub($left_operand, $right_operand);
  70. return gmp_strval($result);
  71. }
  72. /**
  73. * Compare two big integers and returns result as an integer where 0 means
  74. * both are identical, 1 that left_operand is larger, or -1 that
  75. * right_operand is larger.
  76. *
  77. * @param string $left_operand
  78. * @param string $right_operand
  79. * @return int
  80. */
  81. public function compare($left_operand, $right_operand)
  82. {
  83. $result = gmp_cmp($left_operand, $right_operand);
  84. return gmp_strval($result);
  85. }
  86. /**
  87. * Divide two big integers and return result or NULL if the denominator
  88. * is zero.
  89. * @param string $left_operand
  90. * @param string $right_operand
  91. * @return string|null
  92. */
  93. public function divide($left_operand, $right_operand)
  94. {
  95. $result = gmp_div($left_operand, $right_operand);
  96. return gmp_strval($result);
  97. }
  98. /**
  99. * @param string $left_operand
  100. * @param string $right_operand
  101. * @return string
  102. */
  103. public function modulus($left_operand, $modulus)
  104. {
  105. $result = gmp_mod($left_operand, $modulus);
  106. return gmp_strval($result);
  107. }
  108. /**
  109. * @param string $left_operand
  110. * @param string $right_operand
  111. * @return string
  112. */
  113. public function multiply($left_operand, $right_operand)
  114. {
  115. $result = gmp_mul($left_operand, $right_operand);
  116. return gmp_strval($result);
  117. }
  118. /**
  119. * @param string $left_operand
  120. * @param string $right_operand
  121. * @return string
  122. */
  123. public function pow($left_operand, $right_operand)
  124. {
  125. $result = gmp_pow($left_operand, $right_operand);
  126. return gmp_strval($result);
  127. }
  128. /**
  129. * @param string $left_operand
  130. * @param string $right_operand
  131. * @return string
  132. */
  133. public function powmod($left_operand, $right_operand, $modulus)
  134. {
  135. $result = gmp_powm($left_operand, $right_operand, $modulus);
  136. return gmp_strval($result);
  137. }
  138. /**
  139. * @param string $left_operand
  140. * @param string $right_operand
  141. * @return string
  142. */
  143. public function sqrt($operand)
  144. {
  145. $result = gmp_sqrt($operand);
  146. return gmp_strval($result);
  147. }
  148. /**
  149. * @param string $operand
  150. * @return integer
  151. */
  152. public function binaryToInteger($operand)
  153. {
  154. $result = '0';
  155. while (strlen($operand)) {
  156. $ord = ord(substr($operand, 0, 1));
  157. $result = gmp_add(gmp_mul($result, 256), $ord);
  158. $operand = substr($operand, 1);
  159. }
  160. return gmp_strval($result);
  161. }
  162. /**
  163. * @param string|integer $operand
  164. * @return string
  165. */
  166. public function integerToBinary($operand)
  167. {
  168. $bigInt = gmp_strval($operand, 16);
  169. if (strlen($bigInt) % 2 != 0) {
  170. $bigInt = '0' . $bigInt;
  171. } else if ($bigInt[0] > '7') {
  172. $bigInt = '00' . $bigInt;
  173. }
  174. $return = pack("H*", $bigInt);
  175. return $return;
  176. }
  177. /**
  178. * @param string $operand
  179. * @return string
  180. */
  181. public function hexToDecimal($operand)
  182. {
  183. $return = '0';
  184. while(strlen($hex)) {
  185. $hex = hexdec(substr($operand, 0, 4));
  186. $dec = gmp_add(gmp_mul($return, 65536), $hex);
  187. $operand = substr($operand, 4);
  188. }
  189. return $return;
  190. }
  191. }