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.
18 * @copyright Copyright (c) 2005-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
24 * @see Zend_Crypt_Math_BigInteger_Interface
26 require_once 'Zend/Crypt/Math/BigInteger/Interface.php';
29 * Support for arbitrary precision mathematics in PHP.
31 * Zend_Crypt_Math_BigInteger_Bcmath is a wrapper across the PHP BCMath
36 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
37 * @license http://framework.zend.com/license/new-bsd New BSD License
39 class Zend_Crypt_Math_BigInteger_Gmp
implements Zend_Crypt_Math_BigInteger_Interface
43 * Initialise a big integer into an extension specific type.
44 * @param string $operand
48 public function init($operand, $base = 10)
54 * Adds two arbitrary precision numbers
56 * @param string $left_operand
57 * @param string $right_operand
60 public function add($left_operand, $right_operand)
62 $result = gmp_add($left_operand, $right_operand);
63 return gmp_strval($result);
67 * @param string $left_operand
68 * @param string $right_operand
71 public function subtract($left_operand, $right_operand)
73 $result = gmp_sub($left_operand, $right_operand);
74 return gmp_strval($result);
78 * Compare two big integers and returns result as an integer where 0 means
79 * both are identical, 1 that left_operand is larger, or -1 that
80 * right_operand is larger.
81 * @param string $left_operand
82 * @param string $right_operand
85 public function compare($left_operand, $right_operand)
87 $result = gmp_cmp($left_operand, $right_operand);
88 return gmp_strval($result);
92 * Divide two big integers and return result or NULL if the denominator
94 * @param string $left_operand
95 * @param string $right_operand
98 public function divide($left_operand, $right_operand)
100 $result = gmp_div($left_operand, $right_operand);
101 return gmp_strval($result);
105 * @param string $left_operand
106 * @param string $right_operand
109 public function modulus($left_operand, $modulus)
111 $result = gmp_mod($left_operand, $modulus);
112 return gmp_strval($result);
116 * @param string $left_operand
117 * @param string $right_operand
120 public function multiply($left_operand, $right_operand)
122 $result = gmp_mul($left_operand, $right_operand);
123 return gmp_strval($result);
127 * @param string $left_operand
128 * @param string $right_operand
131 public function pow($left_operand, $right_operand)
133 $result = gmp_pow($left_operand, $right_operand);
134 return gmp_strval($result);
138 * @param string $left_operand
139 * @param string $right_operand
142 public function powmod($left_operand, $right_operand, $modulus)
144 $result = gmp_powm($left_operand, $right_operand, $modulus);
145 return gmp_strval($result);
149 * @param string $left_operand
150 * @param string $right_operand
153 public function sqrt($operand)
155 $result = gmp_sqrt($operand);
156 return gmp_strval($result);
160 public function binaryToInteger($operand)
163 while (strlen($operand)) {
164 $ord = ord(substr($operand, 0, 1));
165 $result = gmp_add(gmp_mul($result, 256), $ord);
166 $operand = substr($operand, 1);
168 return gmp_strval($result);
172 public function integerToBinary($operand)
174 $bigInt = gmp_strval($operand, 16);
175 if (strlen($bigInt) %
2 != 0) {
176 $bigInt = '0' . $bigInt;
177 } else if ($bigInt[0] > '7') {
178 $bigInt = '00' . $bigInt;
180 $return = pack("H*", $bigInt);
185 public function hexToDecimal($operand)
188 while(strlen($hex)) {
189 $hex = hexdec(substr($operand, 0, 4));
190 $dec = gmp_add(gmp_mul($return, 65536), $hex);
191 $operand = substr($operand, 4);