00001 <?php 00026 require_once 'Zend/Crypt/Math/BigInteger/Interface.php'; 00027 00039 class Zend_Crypt_Math_BigInteger_Gmp implements Zend_Crypt_Math_BigInteger_Interface 00040 { 00041 00048 public function init($operand, $base = 10) 00049 { 00050 return $operand; 00051 } 00052 00060 public function add($left_operand, $right_operand) 00061 { 00062 $result = gmp_add($left_operand, $right_operand); 00063 return gmp_strval($result); 00064 } 00065 00071 public function subtract($left_operand, $right_operand) 00072 { 00073 $result = gmp_sub($left_operand, $right_operand); 00074 return gmp_strval($result); 00075 } 00076 00085 public function compare($left_operand, $right_operand) 00086 { 00087 $result = gmp_cmp($left_operand, $right_operand); 00088 return gmp_strval($result); 00089 } 00090 00098 public function divide($left_operand, $right_operand) 00099 { 00100 $result = gmp_div($left_operand, $right_operand); 00101 return gmp_strval($result); 00102 } 00103 00109 public function modulus($left_operand, $modulus) 00110 { 00111 $result = gmp_mod($left_operand, $modulus); 00112 return gmp_strval($result); 00113 } 00114 00120 public function multiply($left_operand, $right_operand) 00121 { 00122 $result = gmp_mul($left_operand, $right_operand); 00123 return gmp_strval($result); 00124 } 00125 00131 public function pow($left_operand, $right_operand) 00132 { 00133 $result = gmp_pow($left_operand, $right_operand); 00134 return gmp_strval($result); 00135 } 00136 00142 public function powmod($left_operand, $right_operand, $modulus) 00143 { 00144 $result = gmp_powm($left_operand, $right_operand, $modulus); 00145 return gmp_strval($result); 00146 } 00147 00153 public function sqrt($operand) 00154 { 00155 $result = gmp_sqrt($operand); 00156 return gmp_strval($result); 00157 } 00158 00159 00160 public function binaryToInteger($operand) 00161 { 00162 $result = '0'; 00163 while (strlen($operand)) { 00164 $ord = ord(substr($operand, 0, 1)); 00165 $result = gmp_add(gmp_mul($result, 256), $ord); 00166 $operand = substr($operand, 1); 00167 } 00168 return gmp_strval($result); 00169 } 00170 00171 00172 public function integerToBinary($operand) 00173 { 00174 $bigInt = gmp_strval($operand, 16); 00175 if (strlen($bigInt) % 2 != 0) { 00176 $bigInt = '0' . $bigInt; 00177 } else if ($bigInt[0] > '7') { 00178 $bigInt = '00' . $bigInt; 00179 } 00180 $return = pack("H*", $bigInt); 00181 return $return; 00182 } 00183 00184 00185 public function hexToDecimal($operand) 00186 { 00187 $return = '0'; 00188 while(strlen($hex)) { 00189 $hex = hexdec(substr($operand, 0, 4)); 00190 $dec = gmp_add(gmp_mul($return, 65536), $hex); 00191 $operand = substr($operand, 4); 00192 } 00193 return $return; 00194 } 00195 00196 }