PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Crypt/Math/BigInteger/Gmp.php

https://bitbucket.org/gkawka/zend-framework
PHP | 196 lines | 85 code | 19 blank | 92 comment | 5 complexity | 3b86b97f296577b9ad7565458c3c6eeb 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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Gmp.php 24593 2012-01-05 20:35:02Z matthew $
  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-2012 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 string $left_operand
  53. * @param string $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. * @param string $left_operand
  63. * @param string $right_operand
  64. * @return string
  65. */
  66. public function subtract($left_operand, $right_operand)
  67. {
  68. $result = gmp_sub($left_operand, $right_operand);
  69. return gmp_strval($result);
  70. }
  71. /**
  72. * Compare two big integers and returns result as an integer where 0 means
  73. * both are identical, 1 that left_operand is larger, or -1 that
  74. * right_operand is larger.
  75. * @param string $left_operand
  76. * @param string $right_operand
  77. * @return int
  78. */
  79. public function compare($left_operand, $right_operand)
  80. {
  81. $result = gmp_cmp($left_operand, $right_operand);
  82. return gmp_strval($result);
  83. }
  84. /**
  85. * Divide two big integers and return result or NULL if the denominator
  86. * is zero.
  87. * @param string $left_operand
  88. * @param string $right_operand
  89. * @return string|null
  90. */
  91. public function divide($left_operand, $right_operand)
  92. {
  93. $result = gmp_div($left_operand, $right_operand);
  94. return gmp_strval($result);
  95. }
  96. /**
  97. * @param string $left_operand
  98. * @param string $right_operand
  99. * @return string
  100. */
  101. public function modulus($left_operand, $modulus)
  102. {
  103. $result = gmp_mod($left_operand, $modulus);
  104. return gmp_strval($result);
  105. }
  106. /**
  107. * @param string $left_operand
  108. * @param string $right_operand
  109. * @return string
  110. */
  111. public function multiply($left_operand, $right_operand)
  112. {
  113. $result = gmp_mul($left_operand, $right_operand);
  114. return gmp_strval($result);
  115. }
  116. /**
  117. * @param string $left_operand
  118. * @param string $right_operand
  119. * @return string
  120. */
  121. public function pow($left_operand, $right_operand)
  122. {
  123. $result = gmp_pow($left_operand, $right_operand);
  124. return gmp_strval($result);
  125. }
  126. /**
  127. * @param string $left_operand
  128. * @param string $right_operand
  129. * @return string
  130. */
  131. public function powmod($left_operand, $right_operand, $modulus)
  132. {
  133. $result = gmp_powm($left_operand, $right_operand, $modulus);
  134. return gmp_strval($result);
  135. }
  136. /**
  137. * @param string $left_operand
  138. * @param string $right_operand
  139. * @return string
  140. */
  141. public function sqrt($operand)
  142. {
  143. $result = gmp_sqrt($operand);
  144. return gmp_strval($result);
  145. }
  146. public function binaryToInteger($operand)
  147. {
  148. $result = '0';
  149. while (strlen($operand)) {
  150. $ord = ord(substr($operand, 0, 1));
  151. $result = gmp_add(gmp_mul($result, 256), $ord);
  152. $operand = substr($operand, 1);
  153. }
  154. return gmp_strval($result);
  155. }
  156. public function integerToBinary($operand)
  157. {
  158. $bigInt = gmp_strval($operand, 16);
  159. if (strlen($bigInt) % 2 != 0) {
  160. $bigInt = '0' . $bigInt;
  161. } else if ($bigInt[0] > '7') {
  162. $bigInt = '00' . $bigInt;
  163. }
  164. $return = pack("H*", $bigInt);
  165. return $return;
  166. }
  167. public function hexToDecimal($operand)
  168. {
  169. $return = '0';
  170. while(strlen($hex)) {
  171. $hex = hexdec(substr($operand, 0, 4));
  172. $dec = gmp_add(gmp_mul($return, 65536), $hex);
  173. $operand = substr($operand, 4);
  174. }
  175. return $return;
  176. }
  177. }