public function setValue(Content $content) { $binaryData = $content->binaryData; $offsetIndex = 0; $contentLength = $this->contentLength->length; $isNegative = (ord($binaryData[$offsetIndex]) & 0x80) != 0x0; $number = gmp_init(ord($binaryData[$offsetIndex++]) & 0x7f, 10); for ($i = 0; $i < $contentLength - 1; $i++) { $number = gmp_or(gmp_mul($number, 0x100), ord($binaryData[$offsetIndex++])); } if ($isNegative) { $number = gmp_sub($number, gmp_pow(2, 8 * $contentLength - 1)); } $value = gmp_strval($number, 10); if (is_string($value)) { // remove gaps between hex digits $value = preg_replace('/\\s|0x/', '', $value); } elseif (is_numeric($value)) { $value = dechex($value); } else { throw new Exception('OctetString: unrecognized input type!'); } if (strlen($value) % 2 != 0) { // transform values like 1F2 to 01F2 $value = '0' . $value; } $this->value = $value; }
/** * Constructor * * @param number $flags Flags * @param int $width The number of flags. If width is larger than number of * bits in $flags, zeroes are prepended to flag field. */ public function __construct($flags, $width) { if (!$width) { $this->_flags = ""; } else { // calculate number of unused bits in last octet $last_octet_bits = $width % 8; $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0; $num = gmp_init($flags); // mask bits outside bitfield width $mask = gmp_sub(gmp_init(1) << $width, 1); $num &= $mask; // shift towards MSB if needed $data = gmp_export($num << $unused_bits, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN); $octets = unpack("C*", $data); $bits = count($octets) * 8; // pad with zeroes while ($bits < $width) { array_unshift($octets, 0); $bits += 8; } $this->_flags = pack("C*", ...$octets); } $this->_width = $width; }
/** * @before */ public function createDependencies() { $this->mathInterface = new GmpMath(); $points = ['R' => 1, 'S' => 2]; $this->signature = new Signature(gmp_init($points['R'], 10), gmp_init($points['S'], 10)); $this->signatureData = ['sha256' => pack('H*', str_repeat('0', 63) . $points['R'] . str_repeat('0', 63) . $points['S']), 'sha384' => pack('H*', str_repeat('0', 95) . $points['R'] . str_repeat('0', 95) . $points['S']), 'sha512' => pack('H*', str_repeat('0', 131) . $points['R'] . str_repeat('0', 131) . $points['S'])]; }
public function testGMP() { $math = \Mdanter\Ecc\EccFactory::getAdapter(); $I_l = "e97a4d6be13f8f5804c0a76080428fc6d51260f74801678c4127045d2640af14"; $private_key = "142018c66b43a95de58c1cf603446fc0da322bc15fb4df068b844b57c706dd05"; $n = "115792089237316195423570985008687907852837564279074904382605163141518161494337"; $gmp_I_l = gmp_init($I_l, 16); $gmp_private_key = gmp_init($private_key, 16); $gmp_add = gmp_add($gmp_I_l, $gmp_private_key); $gmp_add_res = gmp_strval($gmp_add, 10); $this->assertEquals("105604983404708440304568772161069255144976878830542744455590282065741265022740", gmp_strval($gmp_I_l)); $this->assertEquals("9102967069016248707169900673545386030247334423973996501079368232055584775429", gmp_strval($gmp_private_key)); $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval($gmp_add)); $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval(gmp_div_r($gmp_add, gmp_init($n)))); $this->assertEquals("-4", gmp_strval(gmp_cmp(0, gmp_div_r($gmp_add, $n)))); $this->assertEquals("230500039711040884435309657843302549028061777533591645339274813439315011292506", gmp_strval(gmp_add(gmp_init($n), gmp_div_r($gmp_add, gmp_init($n))))); $gmp_mod2 = $math->mod($gmp_add_res, $n); $this->assertTrue(is_string($gmp_mod2)); $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", $gmp_mod2); $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", $gmp_mod2); // when no base is provided both a resource and string work $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval(gmp_init($gmp_mod2))); $this->assertEquals("114707950473724689011738672834614641175224213254516740956669650297796849798169", gmp_strval($gmp_mod2)); // when base is provided it fails on HHVM when inputting a string $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", gmp_strval(gmp_init($gmp_mod2), 16)); // $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", gmp_strval($gmp_mod2, 16)); $this->assertEquals("fd9a66324c8338b5ea4cc4568386ff87af448cb8a7b64692ccab4fb4ed478c19", str_pad(gmp_strval(gmp_init($gmp_mod2), 16), 64, '0', STR_PAD_LEFT)); }
/** * _baseConvert * * Convert values from base-A to base-B with GMP-functions * * @param string $value Input value for convert * @param int $from From base-A value * @param int $to To base-B value * @return string Result string */ private static function _baseConvert($value, $from, $to) { if (!function_exists('gmp_init')) { throw new \SystemErrorException(array('title' => 'HashHelper error', 'description' => 'Requires the PHP gmp module')); } return gmp_strval(gmp_init($value, $from), $to); }
/** * Creates a bignum. * * @param mixed $str An integer, a string in base 2 to 36, or a byte stream in base 256 * @param int $base an integer between 2 and 36, or 256 * @return resource a bignum */ public function __construct($str, $base = 10) { switch ($base) { case 10: if (BIGNUM_GMP) { $this->value = gmp_init($str, 10); } else { $this->value = $str; } return; break; case 256: $bytes = array_merge(unpack('C*', $str)); $value = (new BigNum(0))->value; foreach ($bytes as $byte) { $value = $this->_mul($value, 256); $value = $this->_add($value, (new BigNum($byte))->value); } $this->value = $value; return; break; default: if (!is_integer($base) || $base < 2 || $base > 36) { return FALSE; } $value = (new BigNum(0))->value; for ($i = 0; $i < strlen($str); $i++) { $value = $this->_mul($value, $base); $value = $this->_add($value, (new BigNum(base_convert($str[$i], $base, 10)))->value); } $this->value = $value; return; } throw new \RuntimeException(); }
protected function assetNameToIDHex($asset_name) { if ($asset_name == 'BTC') { return '0'; } if ($asset_name == 'XCP') { return '1'; } if (substr($asset_name, 0, 1) == 'A') { // numerical asset // An integer between 26^12 + 1 and 256^8 (inclusive) $asset_id = gmp_init(substr($asset_name, 1)); if ($asset_id < gmp_init(26) ** 12 + 1) { throw new Exception("Asset ID was too low", 1); } if ($asset_id > gmp_init(2) ** 64 - 1) { throw new Exception("Asset ID was too high", 1); } return gmp_strval($asset_id, 16); } $n = gmp_init(0); for ($i = 0; $i < strlen($asset_name); $i++) { $n = gmp_mul($n, 26); $char = ord(substr($asset_name, $i, 1)); if ($char < 65 or $char > 90) { throw new Exception("Asset name invalid", 1); } $digit = $char - 65; $n = gmp_add($n, $digit); } return gmp_strval($n, 16); }
public function import($data, $hex = false) { if (!$hex) { $data = bin2hex($data); } $this->_gmp = gmp_init($data, 16); }
protected function getIpAttribute($value) { if (!is_null($value)) { $value = inet_ntop(gmp_export(gmp_init($value))); } return $value; }
public static function BitTest($Number, $Bit) { if (is_resource($Number) !== true) { $Number = gmp_init($Number); } return gmp_testbit($Number, $Bit - 1); }
/** * This class accepts two parameters on construction. Only the * first ($number) is required. * * @param $number string * @param $base int */ public function __construct($number, $base = 10) { $this->reference = $number; $this->base = intval($base); $this->int_max = gmp_init(PHP_INT_MAX, $this->base); $this->number = gmp_init($number, $this->base); }
/** * @return GmpBigInteger **/ public static function make($number, $base = 10) { Assert::isTrue(is_numeric($number)); $result = new self(); $result->resource = gmp_init($number, $base); return $result; }
/** * @expectedException \RuntimeException * @expectedExceptionMessage Unknown cipher method */ public function testInvalidCipher() { $privateKey = CurveFactory::getGeneratorByName('nistp256')->getPrivateKeyFrom(gmp_init(100)); $iv = random_bytes(16); $method = 'not-a-known-cipher'; new EncryptedPrivateKey($privateKey, $method, $iv); }
function pidigits($N) { $k = 1; $n1 = gmp_init(4); $n2 = gmp_init(3); $d = gmp_init(1); $i = 0; while ($i < $N) { $y = digit($n1, $n2, $d); if ($y !== false) { echo gmp_strval($y); $i++; if ($i % 10 == 0) { echo "\t:", $i, "\n"; } extractd($n1, $n2, $d, $y); } else { produce($n1, $n2, $d, $k); $k++; } } if ($i % 10 != 0) { echo str_repeat(' ', 10 - $N % 10), "\t:", $N, "\n"; } }
public static function generate($ver = 4, $node = null, $clockSeq = null, $ns = null, $name = null) { $uuid = null; /* Create a new UUID based on provided data. */ switch ((int) $ver) { case 1: $uuid = Uuid::uuid1($node, $clockSeq); break; case 2: // Version 2 is not supported throw new \RuntimeException('UUID version 2 is unsupported.'); case 3: $uuid = Uuid::uuid3($ns, $name); break; case 4: $uuid = Uuid::uuid4(); break; case 5: $uuid = Uuid::uuid5($ns, $name); break; default: throw new \RuntimeException('Selected UUID version is invalid or unsupported.'); } if (function_exists('gmp_strval')) { return gmp_strval(gmp_init($uuid->getHex(), 16), 62); } return Base62::encode((string) $uuid->getInteger()); }
function g($val = null) { if (is_string($val)) { $this->g = gmp_init($val, 16); } return gmp_strval($this->g, 16); }
function sumT($n) { if (gmp_intval($n) == 1) { return gmp_init(1); } return gmp_add(gmp_sub(gmp_pow($n, 2), gmp_pow(gmp_init(gmp_intval($n) - 1), 2)), sumT(gmp_init(gmp_intval($n) - 1))); //return gmp_mod(gmp_add(gmp_sub(gmp_mod(gmp_pow($n, 2), '1000000007'), gmp_mod(gmp_pow(gmp_init(gmp_intval($n)-1), 2), '1000000007')), sumT(gmp_init(gmp_intval($n)-1))), '1000000007'); }
/** * @return string */ public final function getSerialNumber() { $String = $this->getIdentifier(); $String = strlen($String) % 2 == 1 ? 'c' . $String : $String; $String = base64_encode($this->decodeString($String)); $String = $this->convertBase64ToHex($String); return gmp_strval(gmp_init($String, 16)); }
/** * binary decode using gmp extension */ function _bindecGmp($bin) { $dec = gmp_init(0); for ($i = 0; $i < strlen($bin); $i++) { $dec = gmp_add(gmp_mul($dec, 256), ord($bin[$i])); } return $dec; }
private function makeId32($timestamp, $machine, $sequence) { $timestamp = gmp_mul((string) $timestamp, gmp_pow(2, 22)); $machine = gmp_mul((string) $machine, gmp_pow(2, 12)); $sequence = gmp_init((string) $sequence, 10); $value = gmp_or(gmp_or($timestamp, $machine), $sequence); return gmp_strval($value, 10); }
public function convertToPHPValue($value, AbstractPlatform $platform) { if ($value === null) { return null; } $value = Util::convertScientificNotationToDecimal($value); return \gmp_init($value); }
public static function gmp_dechex($dec) { if (extension_loaded('gmp') && USE_EXT == 'GMP') { $hex = gmp_strval(gmp_init($dec), 16); return $hex; } else { throw new Exception("PLEASE INSTALL GMP"); } }
public function invalidAddresses() { $values = array(array("\t"), array(array()), array(new stdClass()), array("-1"), array(gmp_init('-1')), array(gmp_init('340282366920938463463374607431768211456')), array("abcz"), array(12.3), array(-12.3), array('127.0.0.1')); // 32 bits if (PHP_INT_SIZE == 4) { } elseif (PHP_INT_SIZE == 8) { } return $values; }
function irand($min, $max = null) { if (is_null($max)) { $max = $min; $min = 0; } $this->next(); return gmp_intval(gmp_mod(gmp_init('0x' . $this->context), gmp_init($max - $min))) + $min; }
/** * Convert an arbitrarily-long string from one numeric base to * another, optionally zero-padding to a minimum column width. * * Supports base 2 through 36; digit values 10-36 are represented * as lowercase letters a-z. Input is case-insensitive. * * @param string $input Input number * @param int $sourceBase Base of the input number * @param int $destBase Desired base of the output * @param int $pad Minimum number of digits in the output (pad with zeroes) * @param bool $lowercase Whether to output in lowercase or uppercase * @param string $engine Either "gmp", "bcmath", or "php" * @return string|bool The output number as a string, or false on error */ function base_convert($input, $sourceBase, $destBase, $pad = 1, $lowercase = true, $engine = 'auto') { $input = (string) $input; if ($sourceBase < 2 || $sourceBase > 36 || $destBase < 2 || $destBase > 36 || $sourceBase != (int) $sourceBase || $destBase != (int) $destBase || $pad != (int) $pad || !preg_match("/^[" . substr('0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase) . "]+\$/i", $input)) { return false; } static $baseChars = array(10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f', 16 => 'g', 17 => 'h', 18 => 'i', 19 => 'j', 20 => 'k', 21 => 'l', 22 => 'm', 23 => 'n', 24 => 'o', 25 => 'p', 26 => 'q', 27 => 'r', 28 => 's', 29 => 't', 30 => 'u', 31 => 'v', 32 => 'w', 33 => 'x', 34 => 'y', 35 => 'z', '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11, 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, 'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29, 'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35); if (extension_loaded('gmp') && ($engine == 'auto' || $engine == 'gmp')) { // Removing leading zeros works around broken base detection code in // some PHP versions (see <https://bugs.php.net/bug.php?id=50175> and // <https://bugs.php.net/bug.php?id=55398>). $result = gmp_strval(gmp_init(ltrim($input, '0') ?: '0', $sourceBase), $destBase); } elseif (extension_loaded('bcmath') && ($engine == 'auto' || $engine == 'bcmath')) { $decimal = '0'; foreach (str_split(strtolower($input)) as $char) { $decimal = bcmul($decimal, $sourceBase); $decimal = bcadd($decimal, $baseChars[$char]); } // @codingStandardsIgnoreStart Generic.CodeAnalysis.ForLoopWithTestFunctionCall.NotAllowed for ($result = ''; bccomp($decimal, 0); $decimal = bcdiv($decimal, $destBase, 0)) { $result .= $baseChars[bcmod($decimal, $destBase)]; } // @codingStandardsIgnoreEnd $result = strrev($result); } else { $inDigits = array(); foreach (str_split(strtolower($input)) as $char) { $inDigits[] = $baseChars[$char]; } // Iterate over the input, modulo-ing out an output digit // at a time until input is gone. $result = ''; while ($inDigits) { $work = 0; $workDigits = array(); // Long division... foreach ($inDigits as $digit) { $work *= $sourceBase; $work += $digit; if ($workDigits || $work >= $destBase) { $workDigits[] = (int) ($work / $destBase); } $work %= $destBase; } // All that division leaves us with a remainder, // which is conveniently our next output digit. $result .= $baseChars[$work]; // And we continue! $inDigits = $workDigits; } $result = strrev($result); } if (!$lowercase) { $result = strtoupper($result); } return str_pad($result, $pad, '0', STR_PAD_LEFT); }
/** * @param mixed $long An int128. * @return self * @throws \RuntimeException */ public function setLong($long) : self { if (!function_exists('gmp_init') || !function_exists('gmp_strval')) { throw new \RuntimeException(sprintf('please install php%d-gmp', (int) phpversion())); } $ipAddr = str_pad(gmp_strval(gmp_init($long, 10), 16), '0', 32, STR_PAD_LEFT); $ipAddr = self::compress(substr(preg_replace('/([A-f0-9]{4})/', '$1:', $ipAddr), 0, -1)); $this->setIp($ipAddr); return $this; }
public static function initialize() { if (count(static::$curves) !== 0) { return; } # (name, p_XXX, a, b, (xG, yG), n, h) static::$curves['nistp256'] = new static('nistp256', gmp_init('0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff'), gmp_init('0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc'), gmp_init('0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b'), new \fpoirotte\Pssht\ECC\Point('0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296', '0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5'), gmp_init('0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551'), 1); static::$curves['nistp384'] = new static('nistp384', gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFEFFFFFFFF0000000000000000FFFFFFFF'), gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFEFFFFFFFF0000000000000000FFFFFFFC'), gmp_init('0xB3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5' . '013875AC656398D8A2ED19D2A85C8EDD3EC2AEF'), new \fpoirotte\Pssht\ECC\Point('0xAA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E08' . '2542A385502F25DBF55296C3A545E3872760AB7', '0x3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B' . '5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F'), gmp_init('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F' . '4372DDF581A0DB248B0A77AECEC196ACCC52973'), 1); static::$curves['nistp521'] = new static('nistp521', gmp_init('0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFFFFFFF'), gmp_init('0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFFFFFFC'), gmp_init('0x0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B48' . '9918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1' . 'EF451FD46B503F00'), new \fpoirotte\Pssht\ECC\Point('0x00C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828A' . 'F606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429B' . 'F97E7E31C2E5BD66', '0x011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFB' . 'D17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C240' . '88BE94769FD16650'), gmp_init('0x01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' . 'FFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AE' . 'BB6FB71E91386409'), 1); }
/** * Generate Oauth credentials * * @return StdClass */ public static function generate() { $fp = fopen('/dev/urandom', 'rb'); $entropy = fread($fp, 32); fclose($fp); $entropy .= uniqid(mt_rand(), true); $hash = hash('sha512', $entropy); $hash = gmp_strval(gmp_init($hash, 16), 62); return (object) ['key' => substr($hash, 0, 32), 'secret' => substr($hash, 32, 48)]; }
/** * @param $value * * @return string */ public static function getBits($value) { $bits = []; for ($i = gmp_init(1); gmp_cmp($value, $i) >= 0; $i = gmp_mul($i, 2)) { if (static::has($value, $i)) { $bits[] = gmp_strval($i); } } return $bits; }
/** * Converts a BigInteger to a binary string. * * @return string */ public function toBytes() { if (gmp_cmp($this->value, gmp_init(0)) === 0) { return ''; } $temp = gmp_strval(gmp_abs($this->value), 16); $temp = mb_strlen($temp, '8bit') & 1 ? '0' . $temp : $temp; $temp = hex2bin($temp); return ltrim($temp, chr(0)); }