protected static function _powmod($x, $y, $m) { $n = count($y); $p = array(1); for ($i = 0; $i < $n - 1; $i++) { $tmp = $y[$i]; for ($j = 0; $j < 0xf; $j++) { if ($tmp & 1) { $p = BigInt::div(BigInt::mul($p, $x), $m, 1); } $tmp >>= 1; $x = BigInt::div(BigInt::mul($x, $x), $m, 1); } } $tmp = $y[$i]; while ($tmp) { if ($tmp & 1) { $p = BigInt::div(BigInt::mul($p, $x), $m, 1); } $tmp >>= 1; $x = BigInt::div(BigInt::mul($x, $x), $m, 1); } return $p; }
/** Return a new BigInt object containing the result of a logical AND operation on this BigInt and $bigint. */ public function LogicalAND(BigInt $bigint) { if ($bigint == null) { throw new Exception("Argument error :: bigint cannot be null!"); } $count = $this->width; if ($count != $bigint->Width()) { throw new Exception("Format error :: BigInts must be of equal size!"); } $bigint_arr = $bigint->ToBitArray(); $ret = $this->InitializeArray($count); for ($i = 0; $i < $count; $i++) { if ($this->bits[$i] == 1 && $bigint_arr[$i] == 1) { $ret[$i] = 1; } } return $this->ToBigInt($ret); }
public function divisionLarge() { $a = new BigInt('1846332104484924953979619544386780054125593365543499568033685888050'); $b = new BigInt('36486872398567981638843143228254546051651870'); $r = new BigInt('50602640980469152653015'); $this->assertEquals($r, $a->divide($b)); }
function _key_exchange() { if (!is_null($this->_key) || $this->_encryptMode == 0) { return true; } $request = "phprpc_encrypt=true&phprpc_keylen={$this->_keylen}"; $result = $this->_post($request); if (is_a($result, 'PHPRPC_Error')) { return $result; } if (array_key_exists('phprpc_keylen', $result)) { $this->_keylen = (int) $result['phprpc_keylen']; } else { $this->_keylen = 128; } if (array_key_exists('phprpc_encrypt', $result)) { $encrypt = unserialize(base64_decode($result['phprpc_encrypt'])); PHPRPC::xxtea(); // require_once('xxtea.php'); $x = BigInt::random($this->_keylen - 1, true); $key = BigInt::powmod(BigInt::dec2num($encrypt['y']), $x, BigInt::dec2num($encrypt['p'])); if ($this->_keylen == 128) { $key = BigInt::num2str($key); } else { $key = pack('H*', md5(BigInt::num2dec($key))); } $this->_key = str_pad($key, 16, "", STR_PAD_LEFT); $encrypt = BigInt::num2dec(BigInt::powmod(BigInt::dec2num($encrypt['g']), $x, BigInt::dec2num($encrypt['p']))); $request = "phprpc_encrypt={$encrypt}"; $result = $this->_post($request); if (is_a($result, 'PHPRPC_Error')) { return $result; } } else { $this->_key = NULL; $this->_encryptMode = 0; } return true; }
/** * CRC32 * * @param math.BigInt key * @param int char * @return math.BigInt updated checksum */ protected function crc32($key, $char) { $l = new BigInt(self::$crc32[$key->bitwiseXor($char)->byteValue()]); return new BigInt($l->bitwiseXor($key->shiftRight(8))); }