Gmp.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * This class forms part of a proposal for the Zend Framework. The attached
  16. * copyright will be transferred to Zend Technologies USA Inc. upon future
  17. * acceptance of that proposal:
  18. * http://framework.zend.com/wiki/pages/viewpage.action?pageId=20369
  19. *
  20. * @category Zend
  21. * @package Zend_Crypt
  22. * @subpackage Math
  23. * @copyright Copyright (c) 2007 Pádraic Brady (http://blog.astrumfutura.com)
  24. * @license http://framework.zend.com/license/new-bsd New BSD License
  25. */
  26. /** Zend_Crypt_Math_BigInteger_Interface */
  27. require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
  28. /**
  29. * Support for arbitrary precision mathematics in PHP.
  30. *
  31. * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
  32. * extension.
  33. *
  34. * @category Zend
  35. * @package Zend_Crypt
  36. * @subpackage Math
  37. * @author Pádraic Brady (http://blog.astrumfutura.com)
  38. * @license http://framework.zend.com/license/new-bsd New BSD License
  39. */
  40. class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface
  41. {
  42. /**
  43. * Initialise a big integer into an extension specific type.
  44. * @param string $operand
  45. * @param int $base
  46. * @return string
  47. */
  48. public function init($operand, $base = 10)
  49. {
  50. return $operand;
  51. }
  52. /**
  53. * Adds two arbitrary precision numbers
  54. *
  55. * @param string $left_operand
  56. * @param string $right_operand
  57. * @return string
  58. */
  59. public function add($left_operand, $right_operand)
  60. {
  61. $result = gmp_add($left_operand, $right_operand);
  62. return gmp_strval($result);
  63. }
  64. /**
  65. * @param string $left_operand
  66. * @param string $right_operand
  67. * @return string
  68. */
  69. public function subtract($left_operand, $right_operand)
  70. {
  71. $result = gmp_sub($left_operand, $right_operand);
  72. return gmp_strval($result);
  73. }
  74. /**
  75. * Compare two big integers and returns result as an integer where 0 means
  76. * both are identical, 1 that left_operand is larger, or -1 that
  77. * right_operand is larger.
  78. * @param string $left_operand
  79. * @param string $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. * @param string $left_operand
  91. * @param string $right_operand
  92. * @return string|null
  93. */
  94. public function divide($left_operand, $right_operand)
  95. {
  96. $result = gmp_div($left_operand, $right_operand);
  97. return gmp_strval($result);
  98. }
  99. /**
  100. * @param string $left_operand
  101. * @param string $right_operand
  102. * @return string
  103. */
  104. public function modulus($left_operand, $modulus)
  105. {
  106. $result = gmp_mod($left_operand, $modulus);
  107. return gmp_strval($result);
  108. }
  109. /**
  110. * @param string $left_operand
  111. * @param string $right_operand
  112. * @return string
  113. */
  114. public function multiply($left_operand, $right_operand)
  115. {
  116. $result = gmp_mul($left_operand, $right_operand);
  117. return gmp_strval($result);
  118. }
  119. /**
  120. * @param string $left_operand
  121. * @param string $right_operand
  122. * @return string
  123. */
  124. public function pow($left_operand, $right_operand)
  125. {
  126. $result = gmp_pow($left_operand, $right_operand);
  127. return gmp_strval($result);
  128. }
  129. /**
  130. * @param string $left_operand
  131. * @param string $right_operand
  132. * @return string
  133. */
  134. public function powmod($left_operand, $right_operand, $modulus)
  135. {
  136. $result = gmp_powm($left_operand, $right_operand, $modulus);
  137. return gmp_strval($result);
  138. }
  139. /**
  140. * @param string $left_operand
  141. * @param string $right_operand
  142. * @return string
  143. */
  144. public function sqrt($operand)
  145. {
  146. $result = gmp_sqrt($operand);
  147. return gmp_strval($result);
  148. }
  149. public function binaryToInteger($operand)
  150. {
  151. $result = '0';
  152. while (strlen($operand)) {
  153. $ord = ord(substr($operand, 0, 1));
  154. $result = gmp_add(gmp_mul($result, 256), $ord);
  155. $operand = substr($operand, 1);
  156. }
  157. return gmp_strval($result);
  158. }
  159. public function integerToBinary($operand)
  160. {
  161. $bigInt = gmp_strval($operand, 16);
  162. if (strlen($bigInt) % 2 != 0) {
  163. $bigInt = '0' . $bigInt;
  164. } else if ($bigInt[0] > '7') {
  165. $bigInt = '00' . $bigInt;
  166. }
  167. $return = pack("H*", $bigInt);
  168. return $return;
  169. }
  170. public function hexToDecimal($operand)
  171. {
  172. $return = '0';
  173. while(strlen($hex)) {
  174. $hex = hexdec(substr($operand, 0, 4));
  175. $dec = gmp_add(gmp_mul($return, 65536), $hex);
  176. $operand = substr($operand, 4);
  177. }
  178. return $return;
  179. }
  180. }