/** * Return the modulus, also known as absolute value or magnitude of this number * = sqrt(r^2 + i^2); * * @return \Chippyash\Type\Number\Rational\GMPRationalType */ public function modulus() { if ($this->isReal()) { //sqrt(r^2 + 0^2) = sqrt(r^2) = abs(r) /** @noinspection PhpUndefinedMethodInspection */ return $this->value['real']->abs(); } //get r^2 and i^2 $sqrR = array('n' => gmp_pow($this->value['real']->numerator()->gmp(), 2), 'd' => gmp_pow($this->value['real']->denominator()->gmp(), 2)); $sqrI = array('n' => gmp_pow($this->value['imaginary']->numerator()->gmp(), 2), 'd' => gmp_pow($this->value['imaginary']->denominator()->gmp(), 2)); //r^2 + i^2 $den = $this->lcm($sqrR['d'], $sqrI['d']); $numRaw = gmp_strval(gmp_add(gmp_div_q(gmp_mul($sqrR['n'], $den), $sqrR['d']), gmp_div_q(gmp_mul($sqrI['n'], $den), $sqrI['d']))); $num = TypeFactory::createInt($numRaw); //sqrt(num/den) = sqrt(num)/sqrt(den) //now this a fudge - we ought to be able to get a proper square root using //factors etc but what we do instead is to do an approximation by converting //to intermediate rationals using as much precision as we can i.e. // rNum = GMPRationaType(sqrt(num)) // rDen = GMPRationalType(sqrt(den)) // mod = rN/1 * 1/rD $rNum = RationalTypeFactory::fromFloat(sqrt($num())); $rDen = RationalTypeFactory::fromFloat(sqrt(gmp_strval($den))); $modN = gmp_mul($rNum->numerator()->gmp(), $rDen->denominator()->gmp()); $modD = gmp_mul($rNum->denominator()->gmp(), $rDen->numerator()->gmp()); return RationalTypeFactory::create((int) gmp_strval($modN), (int) gmp_strval($modD)); }
function get_funds_graph_data() { $btc = array(); $fiat = array(); $query = "\n SELECT\n req_type, amount, curr_type, " . sql_format_date('timest') . " AS timest2\n FROM\n requests\n WHERE\n status != 'CANCEL'\n ORDER BY\n timest;\n "; $result = do_query($query); $btc_sum = 0; $fiat_sum = 0; while ($row = mysql_fetch_array($result)) { $req_type = $row['req_type']; $amount = $row['amount']; $curr_type = $row['curr_type']; $timest = $row['timest2']; if ($req_type == 'WITHDR') { $amount = gmp_mul(-1, $amount); } if ($curr_type == 'BTC') { $btc_sum = gmp_add($btc_sum, $amount); $btc[$timest] = internal_to_numstr($btc_sum); } else { $fiat_sum = gmp_add($fiat_sum, $amount); $fiat[$timest] = internal_to_numstr($fiat_sum); } } return array($btc, $fiat); }
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)); }
/** * Convert 32-bit SteamID to 64-bit SteamID * * @param string|int $userId * * @return string * @throws Exception */ public static function to64Bit($userId) { if (!function_exists('gmp_add')) { throw new Exception("GMP Library not installed. Cannot convert SteamIDs."); } return gmp_strval(gmp_add(gmp_mul(sprintf("%u", bindec(self::STEAM_ID_UPPER_BITS)), "4294967296"), sprintf("%u", $userId))); }
public function testAdd() { $this->assertEquals(gmp_strval(gmp_add($this->a, $this->a)), $this->math->add($this->a, $this->a)); $this->assertEquals(gmp_strval(gmp_add($this->b, $this->b)), $this->math->add($this->b, $this->b)); $this->assertEquals(gmp_strval(gmp_add($this->c, $this->c)), $this->math->add($this->c, $this->c)); $this->assertEquals(2, $this->math->add(1, 1)); }
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); }
function GetFriendID($pszAuthID) { $iServer = "0"; $iAuthID = "0"; $szAuthID = $pszAuthID; $szTmp = strtok($szAuthID, ":"); while (($szTmp = strtok(":")) !== false) { $szTmp2 = strtok(":"); if ($szTmp2 !== false) { $iServer = $szTmp; $iAuthID = $szTmp2; } } if ($iAuthID == "0") { return false; } if (extension_loaded('bcmath') == 1) { // calc communityid with bcmath $i64friendID = bcmul($iAuthID, "2"); $i64friendID = bcadd($i64friendID, bcadd("76561197960265728", $iServer, 0), 0); return $i64friendID; } else { if (extension_loaded('gmp') == 1) { // calc communityid with gmp $i64friendID = gmp_mul($iAuthID, "2"); $i64friendID = gmp_add($i64friendID, gmp_add("76561197960265728", $iServer)); return gmp_strval($i64friendID); } else { $i64friendID = Mul(strval($iAuthID), "2"); $i64friendID = Add(strval($i64friendID), Add("76561197960265728", strval($iServer))); return $i64friendID; } } return false; }
/** * Pack a long. * * If it is a 32bit PHP we suppose that this log is treated by bcmath * TODO 32bit * * @param int|string $value * * @return string the packed long */ public static function packLong($value) { if (PHP_INT_SIZE > 4) { $value = (int) $value; $binaryString = chr($value >> 56 & 0xff) . chr($value >> 48 & 0xff) . chr($value >> 40 & 0xff) . chr($value >> 32 & 0xff) . chr($value >> 24 & 0xff) . chr($value >> 16 & 0xff) . chr($value >> 8 & 0xff) . chr($value & 0xff); } else { /* * To get the two's complement of a binary number, * the bits are inverted, or "flipped", * by using the bitwise NOT operation; * the value of 1 is then added to the resulting value */ $bitString = ''; $isNegative = $value[0] == '-'; if (function_exists("bcmod")) { //add 1 for the two's complement if ($isNegative) { $value = bcadd($value, '1'); } while ($value !== '0') { $bitString = (string) abs((int) bcmod($value, '2')) . $bitString; $value = bcdiv($value, '2'); } } elseif (function_exists("gmp_mod")) { //add 1 for the two's complement if ($isNegative) { $value = gmp_strval(gmp_add($value, '1')); } while ($value !== '0') { $bitString = gmp_strval(gmp_abs(gmp_mod($value, '2'))) . $bitString; $value = gmp_strval(gmp_div_q($value, '2')); } } else { while ($value != 0) { list($value, $remainder) = self::str2bin((string) $value); $bitString = $remainder . $bitString; } } //Now do the logical not for the two's complement last phase if ($isNegative) { $len = strlen($bitString); for ($x = 0; $x < $len; $x++) { $bitString[$x] = $bitString[$x] == '1' ? '0' : '1'; } } //pad to have 64 bit if ($bitString != '' && $isNegative) { $bitString = str_pad($bitString, 64, '1', STR_PAD_LEFT); } else { $bitString = str_pad($bitString, 64, '0', STR_PAD_LEFT); } $hi = substr($bitString, 0, 32); $lo = substr($bitString, 32, 32); $hiBin = pack('H*', str_pad(base_convert($hi, 2, 16), 8, 0, STR_PAD_LEFT)); $loBin = pack('H*', str_pad(base_convert($lo, 2, 16), 8, 0, STR_PAD_LEFT)); $binaryString = $hiBin . $loBin; } return $binaryString; }
/** * @dataProvider encryptedWords */ public function testEncrypt($in, $out) { $primeNumber = Math\Calculator::generateNthPrime(10000); $fibonacciSum = Math\Calculator::sumOfFibonacciElements(49); $tmp = gmp_strval(gmp_add($primeNumber, $fibonacciSum)); $key = intval(strrev(substr($tmp, 5, -4))); $this->assertEquals($out, self::$cipher->encrypt($in, $key)); }
/** * binary decode using gmp extension */ private static 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; }
public function bin2dec($bin) { $dec = '0'; for ($i = 0, $len = strlen($bin); $i < $len; $i++) { $dec = gmp_add(gmp_mul($dec, 2), $bin[$i]); } return $dec; }
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'); }
public static function inc($X, $n) { $s = gmp_strval($X, 2); $s1 = (string) substr($s, 0, -$n); $s = gmp_add(gmp_init(substr($s, -$n), 2), 1); $s = gmp_mod($s, gmp_pow(2, $n)); $s2 = str_pad(gmp_strval($s, 2), $n, '0', STR_PAD_LEFT); return gmp_init($s1 . $s2, 2); }
public function generate($serial) { $mul = gmp_mul(strval($this->mulFactor), strval($serial)); $add = gmp_add($mul, $this->diffFactor); $mod = gmp_mod($add, strval($this->amount)); $num = gmp_strval($mod); // $num = ($this->mulFactor * $serial + $this->diffFactor) % $this->amount; return str_pad($num, $this->length, $this->padding, STR_PAD_LEFT); }
function extractd(&$n1, &$n2, $d, $y) { global $u; $u = gmp_mul($d, gmp_mul(-10, $y)); $n1 = gmp_mul($n1, 10); $n1 = gmp_add($n1, $u); $n2 = gmp_mul($n2, 10); $n2 = gmp_add($n2, $u); }
function readNumber($fd, $len) { $val = gmp_init(0); for ($inx = 0; $inx < $len; $inx++) { $data = fread($fd, 1); $val = gmp_mul($val, gmp_init(256)); $val = gmp_add($val, gmp_init(ord($data[0]))); } return $val; }
public function edwards($P, $Q) { $x1 = $P[0]; $y1 = $P[1]; $x2 = $Q[0]; $y2 = $Q[1]; $t = gmp_mul($this->params['d'], gmp_mul(gmp_mul($x1, $x2), gmp_mul($y1, $y2))); $x3 = gmp_mul(gmp_add(gmp_mul($x1, $y2), gmp_mul($x2, $y1)), $this->inv(gmp_add(1, $t))); $y3 = gmp_mul(gmp_add(gmp_mul($y1, $y2), gmp_mul($x1, $x2)), $this->inv(gmp_sub(1, $t))); return array(gmp_mod($x3, $this->params['q']), gmp_mod($y3, $this->params['q'])); }
public function testadd() { $a = 1234; $b = '1234123412341234123412341234123412412341234213412421341342342'; $c = '0x1234123412341234123412341234123412412341234213412421341342342'; $math = new GmpEngine(); $this->assertEquals(gmp_strval(gmp_add($a, $a)), $math->add($a, $a)); $this->assertEquals(gmp_strval(gmp_add($b, $b)), $math->add($b, $b)); $this->assertEquals(gmp_strval(gmp_add($c, $c)), $math->add($c, $c)); $this->assertEquals(2, $math->add(1, 1)); }
private function convertToBase10($source, $sourceBase) { $int = gmp_init(0, 10); $length = mb_strlen($source) - 1; for ($i = 0; $i <= $length; $i++) { $pow = gmp_pow($sourceBase, $length - $i); $mul = gmp_mul($this->symbols->getValue($source[$i]), $pow); $int = gmp_add($int, $mul); } return $int; }
public static function gmp_mod2($n, $d) { if (extension_loaded('gmp') && USE_EXT == 'GMP') { $res = gmp_div_r($n, $d); if (gmp_cmp(0, $res) > 0) { $res = gmp_add($d, $res); } return gmp_strval($res); } else { throw new Exception("PLEASE INSTALL GMP"); } }
public static function fibonacci(int $index) : \GMP { self::_validateNumber($index); $result = gmp_init(0); $n = gmp_init(1); for ($counter = 0; $counter < $index; $counter++) { $tmp = $result; $result = $n; $n = gmp_add($n, $tmp); } return $result; }
private static function nextSeed() { // GMP available if (sjMathRandom::$m_hasGMP) { sjMathRandom::$m_seed = (int) gmp_mod(gmp_add(gmp_mul(sjMathRandom::$m_seed, sjMathRandom::PARAM_A), sjMathRandom::PARAM_B), sjMathRandom::PARAM_C); } else { if (sjMathRandom::$m_hasBCMath) { sjMathRandom::$m_seed = (int) bcmod(bcadd(bcmul(sjMathRandom::$m_seed, sjMathRandom::PARAM_A), sjMathRandom::PARAM_B), sjMathRandom::PARAM_C); } else { die('sjMathRandom: no suported math library available'); } } }
public function kirim_chat() { $this->load->view("fungsiRSA"); /* -- keterangan Masing Masing Fungsi yang dipake dari Library gmp -- gmp_div_qr = Bagi; gmp_add = Tambah; gmp_mul = Kali; gmp_sub = Kurang; gmp_gcd = Menghitung Nilai phi; gmp_strval = Convert Nomer ke String; */ // Inisialisasi P = 113 & Q = 157 (Masing Masing adalah Bilangan Prima) <--- Lebih Besar Lebih Bagus // Menghitung N = P*Q $n = gmp_mul(113, 157); $valn = gmp_strval($n); // Menghitung Nilai M =(p-1)*(q-1) $m = gmp_mul(gmp_sub(113, 1), gmp_sub(157, 1)); // Mencari E (Kunci Public --> (e,n)) // Inisialisasi E = 5 // Membuktikan E = FPB (Faktor Persekutuan Terbesar) dari E dan M = 1 for ($e = 5; $e < 1000; $e++) { // Mencoba dengan Perulangan 1000 Kali $fpb = gmp_gcd($e, $m); if (gmp_strval($fpb) == '1') { // Jika Benar E adalah FPB dari E dan M = 1 <-- Hentikan Proses break; } } // Menghitung D (Kunci Private --> (d,n)) // D = (($m * $i) + 1) / e = $key[1] <-- Perulangan Do While $i = 1; do { $key = gmp_div_qr(gmp_add(gmp_mul($m, $i), 1), $e); $i++; if ($i == 1000) { // Dengan Perulangan 1000 Kali break; } } while (gmp_strval($key[1]) != '0'); // Hasil D = $key[0] $d = $key[0]; $vald = gmp_strval($d); $user = $this->input->post("user"); $pesan = $this->input->post("pesan"); $userid = $this->input->post("iduser"); $hasilenkripsi = enkripsi($pesan, $n, $e); $insert = "insert into chat (user,pesan,id_user) VALUES ('{$user}','{$hasilenkripsi}','{$userid}')"; $this->db->query($insert); redirect("home/ambil_pesan"); }
public function contains($x, $y) { $eq_zero = null; if (extension_loaded('gmp') && USE_EXT == 'GMP') { $eq_zero = gmp_cmp(gmp_Utils::gmp_mod2(gmp_sub(gmp_pow($y, 2), gmp_add(gmp_add(gmp_pow($x, 3), gmp_mul($this->a, $x)), $this->b)), $this->prime), 0); if ($eq_zero == 0) { return true; } else { return false; } } else { throw new ErrorException("Please install GMP"); } }
function is_pentagonal($num) { // we do a sqr with remainder list($sr, $rem) = gmp_sqrtrem(gmp_add(gmp_mul(24, $num), 1)); // if it's not an exact root don't bother with the rest if (gmp_strval($rem) == '0') { $d = gmp_add($sr, 1); $m = gmp_strval(gmp_mod($d, 6)); if ($m == '0') { return true; } } return false; }
function number62_decode($string) { if (preg_match('#^[a-zA-Z0-9]+$#iu', $string) == 0) { return 0; } $out = 0; $length = mb_strlen($string); $array = array_flip(str_split("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")); for ($i = 0; $i < $length; $i++) { $out = gmp_add($out, gmp_mul($array[$string[$length - $i - 1]], gmp_pow(62, $i))); } $out = gmp_strval($out, 10); return $out; }
/** * Converts the number from source base to a decimal GMP resource. * @param array $number Digits for the number to convert * @return resource resulting number as a GMP resource */ private function getDecimal(array $number) { if ($this->isStandardBase($this->source->getDigitList())) { return gmp_init(implode('', $this->source->canonizeDigits($number)), $this->source->getRadix()); } $number = $this->source->getValues($number); $decimal = gmp_init('0'); $count = count($number); $radix = gmp_init($this->source->getRadix()); for ($i = 0; $i < $count; $i++) { $decimal = gmp_add($decimal, gmp_mul(gmp_init($number[$i]), gmp_pow($radix, $count - $i - 1))); } return $decimal; }
public static function add($x, $y) { switch (self::getMode()) { case self::modeGmp: return gmp_strval(gmp_add($x, $y)); break; case self::modeBcmath: return bcadd($x, $y); break; case self::modeNative: return $x + $y; break; } }
protected function getEncodedValue() { $numericValue = gmp_init($this->value, 10); $contentLength = $this->getContentLength(); if (gmp_sign($numericValue) < 0) { $numericValue = gmp_add($numericValue, gmp_sub(gmp_pow(2, 8 * $contentLength), 1)); $numericValue = gmp_add($numericValue, 1); } $result = ''; for ($shiftLength = ($contentLength - 1) * 8; $shiftLength >= 0; $shiftLength -= 8) { $octet = gmp_strval(gmp_mod($this->rightShift($numericValue, $shiftLength), 256)); $result .= chr($octet); } return $result; }
public function encrypt($plain, $iv, $counter = 0) { if (strlen($iv) !== 8) { throw new \InvalidArgumentException(); } $len = strlen($plain); $m = ($len >> 6) + ($len % 64 > 0); $keyStream = ''; for ($i = 0; $i < $m; $i++) { $c = gmp_strval(gmp_add($counter, $i), 16); $c = pack('H*', str_pad($c, 16, '0', STR_PAD_LEFT)); $keyStream .= $this->block($iv, strrev($c)); } return $plain ^ $keyStream; }