Gmp.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Crypt_Math_BigInteger_Interface
  24. */
  25. require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
  26. /**
  27. * Support for arbitrary precision mathematics in PHP.
  28. *
  29. * Zend_Crypt_Math_BigInteger_Gmp is a wrapper across the PHP BCMath
  30. * extension.
  31. *
  32. * @category Zend
  33. * @package Zend_Crypt
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface
  38. {
  39. /**
  40. * Initialise a big integer into an extension specific type.
  41. * @param string $operand
  42. * @param int $base
  43. * @return string
  44. */
  45. public function init($operand, $base = 10)
  46. {
  47. return $operand;
  48. }
  49. /**
  50. * Adds two arbitrary precision numbers
  51. *
  52. * @param resource $left_operand
  53. * @param resource $right_operand
  54. * @return string
  55. */
  56. public function add($left_operand, $right_operand)
  57. {
  58. $result = gmp_add($left_operand, $right_operand);
  59. return gmp_strval($result);
  60. }
  61. /**
  62. * Subtract numbers
  63. *
  64. * @param resource $left_operand
  65. * @param resource $right_operand
  66. * @return string
  67. */
  68. public function subtract($left_operand, $right_operand)
  69. {
  70. $result = gmp_sub($left_operand, $right_operand);
  71. return gmp_strval($result);
  72. }
  73. /**
  74. * Compare two big integers and returns result as an integer where 0 means
  75. * both are identical, 1 that left_operand is larger, or -1 that
  76. * right_operand is larger.
  77. *
  78. * @param resource $left_operand
  79. * @param resource $right_operand
  80. * @return int
  81. */
  82. public function compare($left_operand, $right_operand)
  83. {
  84. $result = gmp_cmp($left_operand, $right_operand);
  85. return gmp_strval($result);
  86. }
  87. /**
  88. * Divide two big integers and return result or NULL if the denominator
  89. * is zero.
  90. *
  91. * @param resource $left_operand
  92. * @param resource $right_operand
  93. * @return string|null
  94. */
  95. public function divide($left_operand, $right_operand)
  96. {
  97. $result = gmp_div($left_operand, $right_operand);
  98. return gmp_strval($result);
  99. }
  100. /**
  101. * Modulo operation
  102. *
  103. * @param resource $left_operand
  104. * @param resource $modulus
  105. * @internal param string $right_operand
  106. * @return string
  107. */
  108. public function modulus($left_operand, $modulus)
  109. {
  110. $result = gmp_mod($left_operand, $modulus);
  111. return gmp_strval($result);
  112. }
  113. /**
  114. * Multiply numbers
  115. *
  116. * @param resource $left_operand
  117. * @param resource $right_operand
  118. * @return string
  119. */
  120. public function multiply($left_operand, $right_operand)
  121. {
  122. $result = gmp_mul($left_operand, $right_operand);
  123. return gmp_strval($result);
  124. }
  125. /**
  126. * Raise number into power
  127. *
  128. * @param resource $left_operand
  129. * @param int $right_operand
  130. * @return string
  131. */
  132. public function pow($left_operand, $right_operand)
  133. {
  134. $result = gmp_pow($left_operand, $right_operand);
  135. return gmp_strval($result);
  136. }
  137. /**
  138. * Raise number into power with modulo
  139. *
  140. * @param resource $left_operand
  141. * @param resource $right_operand
  142. * @param resource $modulus
  143. * @return string
  144. */
  145. public function powmod($left_operand, $right_operand, $modulus)
  146. {
  147. $result = gmp_powm($left_operand, $right_operand, $modulus);
  148. return gmp_strval($result);
  149. }
  150. /**
  151. * Calculate square root
  152. *
  153. * @param $operand
  154. * @return string
  155. */
  156. public function sqrt($operand)
  157. {
  158. $result = gmp_sqrt($operand);
  159. return gmp_strval($result);
  160. }
  161. /**
  162. * @param string $operand
  163. * @return string
  164. */
  165. public function binaryToInteger($operand)
  166. {
  167. $result = '0';
  168. while (strlen($operand)) {
  169. $ord = ord(substr($operand, 0, 1));
  170. $result = gmp_add(gmp_mul($result, 256), $ord);
  171. $operand = substr($operand, 1);
  172. }
  173. return gmp_strval($result);
  174. }
  175. /**
  176. * @param resource $operand GMP number resource
  177. * @return string
  178. */
  179. public function integerToBinary($operand)
  180. {
  181. $bigInt = gmp_strval($operand, 16);
  182. if (strlen($bigInt) % 2 != 0) {
  183. $bigInt = '0' . $bigInt;
  184. } else if ($bigInt[0] > '7') {
  185. $bigInt = '00' . $bigInt;
  186. }
  187. $return = pack("H*", $bigInt);
  188. return $return;
  189. }
  190. /**
  191. * @param string $operand
  192. * @return string
  193. */
  194. public function hexToDecimal($operand)
  195. {
  196. $return = '0';
  197. while(strlen($hex)) {
  198. $hex = hexdec(substr($operand, 0, 4));
  199. $dec = gmp_add(gmp_mul($return, 65536), $hex);
  200. $operand = substr($operand, 4);
  201. }
  202. return $return;
  203. }
  204. }