function fact($x) { if ($x <= 1) { return 1; } return gmp_mul($x, fact($x - 1)); }
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; }
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; }
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); }
/** * 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))); }
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 testMul() { $this->assertEquals(gmp_strval(gmp_mul($this->a, $this->a)), $this->math->mul($this->a, $this->a)); $this->assertEquals(gmp_strval(gmp_mul($this->b, $this->b)), $this->math->mul($this->b, $this->b)); $this->assertEquals(gmp_strval(gmp_mul($this->c, $this->c)), $this->math->mul($this->c, $this->c)); $this->assertEquals(1, $this->math->mul(1, 1)); }
/** * 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 fact($x) { $return = 1; for ($i = 2; $i <= $x; $i++) { $return = gmp_mul($return, $i); } return $return; }
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); }
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); }
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); }
/** * @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; }
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; }
function loadbits($bits) { foreach ($bits as $key => $value) { $this->_bitmap[$key] = $value; if ($value > $this->_curbit) { $this->_curbit = $value; } } $this->_curbit = gmp_mul($this->_curbit, 2); }
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'])); }
private function computeBaseNDigits($number, $targetBase) { $digits = array(); $length = $this->computeBaseNLength($number, $targetBase); for ($i = 0; $i < $length; $i++) { $pow = gmp_pow($targetBase, $length - $i - 1); $div = gmp_div($number, $pow, GMP_ROUND_ZERO); $number = gmp_sub($number, gmp_mul($div, $pow)); $digits[] = $div; } return array_map('gmp_strval', $digits); }
/** * Decode an integer. * * @param int $value * @return int */ public function decode($value) { if (!is_numeric($value)) { throw new InvalidArgumentException('Argument should be an integer'); } switch (static::$mode) { case static::MODE_GMP: return gmp_intval(gmp_mul((int) $value ^ $this->xor, $this->inverse)) & static::MAX_INT; default: return ((int) $value ^ $this->xor) * $this->inverse & static::MAX_INT; } }
public function verifies($hash, Signature $signature) { if (extension_loaded('gmp') && USE_EXT == 'GMP') { $G = $this->generator; $n = $this->generator->getOrder(); $point = $this->point; $r = $signature->getR(); $s = $signature->getS(); if (gmp_cmp($r, 1) < 0 || gmp_cmp($r, gmp_sub($n, 1)) > 0) { return false; } if (gmp_cmp($s, 1) < 0 || gmp_cmp($s, gmp_sub($n, 1)) > 0) { return false; } $c = NumberTheory::inverse_mod($s, $n); $u1 = gmp_Utils::gmp_mod2(gmp_mul($hash, $c), $n); $u2 = gmp_Utils::gmp_mod2(gmp_mul($r, $c), $n); $xy = Point::add(Point::mul($u1, $G), Point::mul($u2, $point)); $v = gmp_Utils::gmp_mod2($xy->getX(), $n); if (gmp_cmp($v, $r) == 0) { return true; } else { return false; } } else { if (extension_loaded('bcmath') && USE_EXT == 'BCMATH') { $G = $this->generator; $n = $this->generator->getOrder(); $point = $this->point; $r = $signature->getR(); $s = $signature->getS(); if (bccomp($r, 1) == -1 || bccomp($r, bcsub($n, 1)) == 1) { return false; } if (bccomp($s, 1) == -1 || bccomp($s, bcsub($n, 1)) == 1) { return false; } $c = NumberTheory::inverse_mod($s, $n); $u1 = bcmod(bcmul($hash, $c), $n); $u2 = bcmod(bcmul($r, $c), $n); $xy = Point::add(Point::mul($u1, $G), Point::mul($u2, $point)); $v = bcmod($xy->getX(), $n); if (bccomp($v, $r) == 0) { return true; } else { return false; } } else { throw new ErrorException("Please install BCMATH or GMP"); } } }
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 GOST_verifies($hash, Signature $signature) { if (extension_loaded('gmp') && USE_EXT == 'GMP') { $G = $this->generator; //P $n = $this->generator->getOrder(); //q $point = $this->point; //Q $r = $signature->getR(); $s = $signature->getS(); if (gmp_cmp($r, 1) < 0 || gmp_cmp($r, gmp_sub($n, 1)) > 0) { return false; } if (gmp_cmp($s, 1) < 0 || gmp_cmp($s, gmp_sub($n, 1)) > 0) { return false; } //step 3 GOST $e = gmp_Utils::gmp_mod2($hash, $n); if (gmp_cmp($e, '0') === 0) { $e = gmp_init('1'); } // step 4 GOST $v = gmp_strval(gmp_invert($e, $n)); // step 5 GOST $z1 = gmp_Utils::gmp_mod2(gmp_mul($s, $v), $n); $z2 = gmp_Utils::gmp_mod2(gmp_mul(gmp_neg($r), $v), $n); // step 6 GOST $C = Point::add(Point::mul($z1, $G), Point::mul($z2, $point)); $R = gmp_Utils::gmp_mod2($C->getX(), $n); if (0) { echo "n - " . $n . "\n"; echo "h - " . $hash . "\n"; echo "e - " . gmp_Utils::gmp_dechex($e) . "\n"; echo "v - " . gmp_Utils::gmp_dechex($v) . "\n"; echo "r - " . $r . "\n"; echo "s - " . $s . "\n"; echo "z1 - " . gmp_Utils::gmp_dechex($z1) . "\nz2 - " . gmp_Utils::gmp_dechex($z2) . "\n"; echo "Q - " . $point . "\nG - " . $G . "\n"; echo "C - " . $C . "\nR - " . $R . "\n"; } if (gmp_cmp($R, $r) == 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; }
/** * 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; }
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; }
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"); } }
public static function fromBinary(&$binaryData, &$offsetIndex = 0) { $parsedObject = new static(0); self::parseIdentifier($binaryData[$offsetIndex], $parsedObject->getType(), $offsetIndex++); $contentLength = self::parseContentLength($binaryData, $offsetIndex, 1); $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)); } $parsedObject = new static(gmp_strval($number, 10)); $parsedObject->setContentLength($contentLength); return $parsedObject; }
/** * Base58 Decode * * This function accepts a base58 encoded string, and decodes the * string into a number, which is converted to hexadecimal. It is then * padded with zero's. * @param $base58 * @return string */ public static function base58_decode($base58) { $origbase58 = $base58; $return = "0"; for ($i = 0; $i < strlen($base58); $i++) { // return = return*58 + current position of $base58[i]in self::$base58chars $return = gmp_add(gmp_mul($return, 58), strpos(self::$base58chars, $base58[$i])); } $return = gmp_strval($return, 16); for ($i = 0; $i < strlen($origbase58) && $origbase58[$i] == "1"; $i++) { $return = "00" . $return; } if (strlen($return) % 2 != 0) { $return = "0" . $return; } return $return; }
protected static function make64Int($hi, $lo) { if (PHP_INT_SIZE > 4) { return (int) $hi << 32 | (int) $lo; } $lo = sprintf("%u", $lo); if (function_exists("gmp_mul")) { return gmp_strval(gmp_add(gmp_mul($hi, "4294967296"), $lo)); } if (function_exists("bcmul")) { return bcadd(bcmul($hi, "4294967296"), $lo); } if (class_exists('Math_BigInteger')) { $bi = new Math_BigInteger($hi); return $bi->multiply("4294967296")->add($lo)->toString(); } throw new PListException("either gmp or bc has to be installed, or the Math_BigInteger has to be available!"); }