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-2010 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
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-2010 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_Bcmath implements Zend_Crypt_Math_BigInteger_Interface
43 * Initialise a big integer into an extension specific type. This is not
44 * applicable to BCMath.
45 * @param string $operand
49 public function init($operand, $base = 10)
55 * Adds two arbitrary precision numbers
57 * @param string $left_operand
58 * @param string $right_operand
61 public function add($left_operand, $right_operand)
63 return bcadd($left_operand, $right_operand);
67 * @param string $left_operand
68 * @param string $right_operand
71 public function subtract($left_operand, $right_operand)
73 return bcsub($left_operand, $right_operand);
77 * Compare two big integers and returns result as an integer where 0 means
78 * both are identical, 1 that left_operand is larger, or -1 that
79 * right_operand is larger.
80 * @param string $left_operand
81 * @param string $right_operand
84 public function compare($left_operand, $right_operand)
86 return bccomp($left_operand, $right_operand);
90 * Divide two big integers and return result or NULL if the denominator
92 * @param string $left_operand
93 * @param string $right_operand
96 public function divide($left_operand, $right_operand)
98 return bcdiv($left_operand, $right_operand);
102 * @param string $left_operand
103 * @param string $right_operand
106 public function modulus($left_operand, $modulus)
108 return bcmod($left_operand, $modulus);
112 * @param string $left_operand
113 * @param string $right_operand
116 public function multiply($left_operand, $right_operand)
118 return bcmul($left_operand, $right_operand);
122 * @param string $left_operand
123 * @param string $right_operand
126 public function pow($left_operand, $right_operand)
128 return bcpow($left_operand, $right_operand);
132 * @param string $left_operand
133 * @param string $right_operand
136 public function powmod($left_operand, $right_operand, $modulus)
138 return bcpowmod($left_operand, $right_operand, $modulus);
142 * @param string $left_operand
143 * @param string $right_operand
146 public function sqrt($operand)
148 return bcsqrt($operand);
152 public function binaryToInteger($operand)
155 while (strlen($operand)) {
156 $ord = ord(substr($operand, 0, 1));
157 $result = bcadd(bcmul($result, 256), $ord);
158 $operand = substr($operand, 1);
164 public function integerToBinary($operand)
166 $cmp = bccomp($operand, 0);
171 while (bccomp($operand, 0) > 0) {
172 $return = chr(bcmod($operand, 256)) . $return;
173 $operand = bcdiv($operand, 256);
175 if (ord($return[0]) > 127) {
176 $return = "\0" . $return;
181 /**public function integerToBinary($operand)
184 while(bccomp($operand, '0')) {
185 $return .= chr(bcmod($operand, '256'));
186 $operand = bcdiv($operand, '256');
189 }**/ // Prior version for referenced offset
192 public function hexToDecimal($operand)
195 while(strlen($hex)) {
196 $hex = hexdec(substr($operand, 0, 4));
197 $dec = bcadd(bcmul($return, 65536), $hex);
198 $operand = substr($operand, 4);