本文整理汇总了PHP中gmp_div_qr函数的典型用法代码示例。如果您正苦于以下问题:PHP gmp_div_qr函数的具体用法?PHP gmp_div_qr怎么用?PHP gmp_div_qr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了gmp_div_qr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: convert
/**
* Convert this number into a other number-system.
*
* @param NumberSystem $newSystem
*
* @return Number
*/
public function convert(NumberSystem $newSystem)
{
$newDigits = [];
$decimalValue = gmp_init($this->decimalValue());
do {
$divisionResult = gmp_div_qr($decimalValue, $newSystem->getBase());
$remainder = gmp_strval($divisionResult[1]);
$decimalValue = $divisionResult[0];
$newDigits[] = $newSystem->getSymbolForPosition($remainder);
} while (gmp_strval($decimalValue) > 0);
return $newSystem->buildNumber(array_reverse($newDigits));
}
示例2: kirim_chat
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");
}
示例3: bcdiv
function bcdiv($a, $b, $precision = NULL)
{
$qr = gmp_div_qr($a, $b);
$q = gmp_strval($qr[0]);
$r = gmp_strval($qr[1]);
if (!$r || $precision === 0) {
return $q;
} else {
if (isset($precision)) {
$r = substr($r, 0, $precision);
}
return "{$q}.{$r}";
}
}
示例4: base58_encode
function base58_encode($string)
{
$table = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
$long_value = gmp_init(bin2hex($string), 16);
$result = '';
while (gmp_cmp($long_value, 58) > 0) {
list($long_value, $mod) = gmp_div_qr($long_value, 58);
$result .= $table[gmp_intval($mod)];
}
$result .= $table[gmp_intval($long_value)];
for ($nPad = 0; $string[$nPad] == ""; ++$nPad) {
}
return str_repeat($table[0], $nPad) . strrev($result);
}
示例5: _decodeFromDER
protected static function _decodeFromDER(Identifier $identifier, $data, &$offset)
{
$idx = $offset;
$len = Length::expectFromDER($data, $idx)->length();
$subids = self::_decodeSubIDs(substr($data, $idx, $len));
$idx += $len;
// decode first subidentifier according to spec section 8.19.4
if (isset($subids[0])) {
list($x, $y) = gmp_div_qr($subids[0], "40");
array_splice($subids, 0, 1, array($x, $y));
}
$offset = $idx;
return new self(self::_implodeSubIDs(...$subids));
}
示例6: convertFractions
public function convertFractions(array $number)
{
$target = gmp_init($this->target->getRadix());
$dividend = $this->getDecimal($number);
$divisor = $this->getDecimal([$this->source->getDigit(1)] + array_fill(1, max(count($number), 1), $this->source->getDigit(0)));
$digits = $this->getFractionDigitCount(count($number));
$zero = gmp_init('0');
$result = [];
for ($i = 0; $i < $digits && gmp_cmp($dividend, $zero) > 0; $i++) {
list($digit, $dividend) = gmp_div_qr(gmp_mul($dividend, $target), $divisor);
$result[] = gmp_intval($digit);
}
return $this->target->getDigits(empty($result) ? [0] : $result);
}
示例7: show_mini_orderbook_table_cell
function show_mini_orderbook_table_cell($id, $curr, $price, $have, $want, $fiat_depth, $btc_depth)
{
// $have and $want is what the 'worst priced' existing order has and wants, and is used here to set the price
// $fiat_depth and $btc_depth are combined amounts available which we want to match, and may include orders at better prices
// $curr is the currency type they want
if ($curr == 'BTC') {
// we are selling BTC
$depth = $btc_depth;
$p = clean_sql_numstr(bcdiv($have, $want, 8));
} else {
// we are buying BTC
$depth = $fiat_depth;
$p = clean_sql_numstr(bcdiv($want, $have, 8));
}
list($w, $r) = gmp_div_qr(gmp_mul($depth, $have), $want);
$w = gmp_strval(gmp_cmp($r, 0) ? gmp_sub($w, 1) : $w);
$h = gmp_strval($depth);
active_table_cell_trade($id, 'l', internal_to_numstr($btc_depth, BTC_PRECISION), "?page=trade&in={$curr}&have={$h}&want={$w}&rate={$p}", 'right');
active_table_cell_trade($id, 'r', internal_to_numstr($fiat_depth, FIAT_PRECISION), "?page=trade&in={$curr}&have={$h}&want={$w}&rate={$p}", 'right');
}
示例8: encodeBase58
/**
* Encode a binary string to base58
*
* @param $binary
* @return string
* @throws \Exception
*/
protected function encodeBase58($binary_bitcoin_address)
{
$size = strlen($binary_bitcoin_address);
if ($size == 0) {
return '';
}
$orig = $binary_bitcoin_address;
$decimal = gmp_import($binary_bitcoin_address);
$return = "";
while (gmp_cmp($decimal, 0) > 0) {
list($decimal, $rem) = gmp_div_qr($decimal, 58);
$return = $return . self::$BASE_58_CHARS[gmp_intval($rem)];
}
$return = strrev($return);
//leading zeros
for ($i = 0; $i < $size && $orig[$i] == ""; $i++) {
$return = "1" . $return;
}
return $return;
}
示例9: CantorExpand
function CantorExpand($n, $x)
{
//參數說明
// $n - 排列的位數
// $x - 第幾個 (必須要是字串)
//1. $x 先減1
$x = gmp_sub($x, "1");
$str = "";
for ($i = 1; $i <= $n; $i++) {
//2. 先算($n-$i)階乘
$fac = gmp_strval(gmp_fact($n - $i));
//3. 再算 $x / ($n-$i) 的商跟餘數
$res = gmp_div_qr($x, $fac);
$quotient = gmp_strval($res[0]);
$remainder = gmp_strval($res[1]);
//4. 比這個位數小的數目總共有 $quotient 個,所以開始找出這個位數
$str .= findMax($quotient);
//5. 把餘數設為 $x,下次要使用
$x = $remainder;
}
return $str;
}
示例10: fulfill_order
function fulfill_order($our_orderid)
{
$our = fetch_order_info($our_orderid);
if ($our->status != 'OPEN') {
return;
}
if ($our->processed) {
throw new Error('Unprocessed', "Shouldn't be here for {$our_orderid}");
}
// Dividing two bignum(20) values only gives us 4 decimal places in the result
// this can cause us to process the matching orders out of sequence unless we arrange
// for the quotient to be greater than 1 by putting the bigger value on top.
//
// With BTC at around 10 GBP each, I just saw the previous version of this query
// process 2 orders out of sequence because the values of initial_want_amount / initial_amount
// for the two orders were 0.09348 and 0.09346, which compare equal to 4 decimal places
if ($our->initial_amount > $our->initial_want_amount) {
$order_by = "initial_want_amount / initial_amount ASC";
} else {
$order_by = "initial_amount / initial_want_amount DESC";
}
$query = "\n SELECT orderid, uid\n FROM orderbook\n WHERE\n status='OPEN'\n AND processed=TRUE\n AND type='{$our->want_type}'\n AND want_type='{$our->type}'\n AND initial_amount * '{$our->initial_amount}' >= initial_want_amount * '{$our->initial_want_amount}'\n AND uid!='{$our->uid}'\n ORDER BY {$order_by}, timest ASC;\n ";
wait_for_lock($our->uid);
$result = b_query($query);
while ($row = mysql_fetch_array($result)) {
echo "Found matching ", $row['orderid'], " from user ", $row['uid'], ".\n";
wait_for_lock($row['uid']);
// lock their account
$them = fetch_order_info($row['orderid']);
// re-fetch their order now that they're locked
if ($them->status != 'OPEN') {
echo "order {$them->orderid} was cancelled on us\n";
release_lock($them->uid);
continue;
}
printf("old order: has %s; wants %s\n", internal_to_numstr($them->amount), internal_to_numstr($them->want_amount));
if ($them->type != $our->want_type || $our->type != $them->want_type) {
throw Error('Problem', 'Urgent problem. Contact the site owner IMMEDIATELY.');
}
// echo " them: orderid {$them->orderid}, uid {$them->uid}, have {$them->amount} {$them->type}, want {$them->want_amount}\n";
// echo " us: orderid {$our->orderid}, uid {$our->uid }, have: {$our->amount} {$our->type}, want {$our->want_amount}\n";
// echo " them->initial_amount = {$them->initial_amount}, them->initial_want_amount = {$them->initial_want_amount}\n";
$left = gmp_mul($our->amount, $them->initial_amount);
$right = gmp_mul($them->amount, $them->initial_want_amount);
if (gmp_cmp($left, $right) >= 0) {
// We need to calculate how much of our stuff they can afford at their price
// we ignore the remainder - it's totally insignificant.
list($them->new_want, $remainder) = gmp_div_qr($right, $them->initial_amount);
if (gmp_cmp($remainder, 0) != 0) {
$them->new_want = gmp_add($them->new_want, 1);
}
$them->new_want = gmp_strval($them->new_want);
echo " we swallow them; they can afford {$them->new_want} from us\n";
pacman($them->orderid, $them->uid, $them->amount, $them->type, $them->commission, $our->orderid, $our->uid, $them->new_want, $our->type, $our->commission, $our->amount, $our->initial_amount, $our->initial_want_amount);
release_lock($them->uid);
// re-update as still haven't finished...
// info needed for any further transactions
$our = fetch_order_info($our->orderid);
// order was closed and our job is done.
if ($our->status != 'OPEN') {
break;
}
} else {
// We need to calculate how much of their stuff we can afford at their price
// we ignore the remainder - it's totally insignificant.
list($our->new_want, $remainder) = gmp_div_qr($left, $them->initial_want_amount);
if (gmp_cmp($remainder, 0) != 0) {
$our->new_want = gmp_add($our->new_want, 1);
}
$our->new_want = gmp_strval($our->new_want);
echo " they swallow us; we can afford {$our->new_want} from them\n";
pacman($our->orderid, $our->uid, $our->amount, $our->type, $our->commission, $them->orderid, $them->uid, $our->new_want, $our->want_type, $them->commission, $them->amount, $them->initial_amount, $them->initial_want_amount);
release_lock($them->uid);
break;
}
}
release_lock($our->uid);
}
示例11: gmpSatoshisToFloatBTC
public static function gmpSatoshisToFloatBTC($value)
{
/*{{{*/
$result = gmp_div_qr($value, gmp_init('100000000'));
$whole = gmp_strval($result[0]);
$fraction = sprintf('%08s', gmp_strval($result[1]));
$btc = floatval("{$whole}.{$fraction}");
return $btc;
}
示例12: divide
/**
* Divides two BigIntegers.
*
* Returns an array whose first element contains the quotient and whose second element contains the
* "common residue". If the remainder would be positive, the "common residue" and the remainder are the
* same. If the remainder would be negative, the "common residue" is equal to the sum of the remainder
* and the divisor.
*
* Here's a quick 'n dirty example:
* <code>
* <?php
* include('Math/BigInteger.php');
*
* $a = new Math_BigInteger('10');
* $b = new Math_BigInteger('20');
*
* list($quotient,$remainder) = $a->divide($b);
*
* echo $quotient->toString(); // outputs 0
* echo "\r\n";
* echo $remainder->toString(); // outputs 10
* ?>
* </code>
*
* @param Math_BigInteger $y
* @return Array
* @access public
* @internal This function is based off of {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=9 HAC 14.20}
* with a slight variation due to the fact that this script, initially, did not support negative numbers. Now,
* it does, but I don't want to change that which already works.
*/
function divide($y)
{
switch (MATH_BIGINTEGER_MODE) {
case MATH_BIGINTEGER_MODE_GMP:
$quotient = new Math_BigInteger();
$remainder = new Math_BigInteger();
list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value);
if (gmp_sign($remainder->value) < 0) {
$remainder->value = gmp_add($remainder->value, gmp_abs($y->value));
}
return array($quotient, $remainder);
case MATH_BIGINTEGER_MODE_BCMATH:
$quotient = new Math_BigInteger();
$remainder = new Math_BigInteger();
$quotient->value = bcdiv($this->value, $y->value);
$remainder->value = bcmod($this->value, $y->value);
if ($remainder->value[0] == '-') {
$remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value);
}
return array($quotient, $remainder);
}
$x = $this->_copy();
$y = $y->_copy();
$x_sign = $x->is_negative;
$y_sign = $y->is_negative;
$x->is_negative = $y->is_negative = false;
$diff = $x->compare($y);
if (!$diff) {
$temp = new Math_BigInteger();
$temp->value = array(1);
$temp->is_negative = $x_sign != $y_sign;
return array($temp, new Math_BigInteger());
}
if ($diff < 0) {
// if $x is negative, "add" $y.
if ($x_sign) {
$x = $y->subtract($x);
}
return array(new Math_BigInteger(), $x);
}
// normalize $x and $y as described in HAC 14.23 / 14.24
// (incidently, i haven't been able to find a definitive example showing that this
// results in worth-while speedup, but whatever)
$msb = $y->value[count($y->value) - 1];
for ($shift = 0; !($msb & 0x2000000); $shift++) {
$msb <<= 1;
}
$x->_lshift($shift);
$y->_lshift($shift);
$x_max = count($x->value) - 1;
$y_max = count($y->value) - 1;
$quotient = new Math_BigInteger();
$quotient->value = $this->_array_repeat(0, $x_max - $y_max + 1);
// $temp = $y << ($x_max - $y_max-1) in base 2**26
$temp = new Math_BigInteger();
$temp->value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y->value);
while ($x->compare($temp) >= 0) {
// calculate the "common residue"
$quotient->value[$x_max - $y_max]++;
$x = $x->subtract($temp);
$x_max = count($x->value) - 1;
}
for ($i = $x_max; $i >= $y_max + 1; $i--) {
$x_value = array($x->value[$i], $i > 0 ? $x->value[$i - 1] : 0, $i - 1 > 0 ? $x->value[$i - 2] : 0);
$y_value = array($y->value[$y_max], $y_max > 0 ? $y_max - 1 : 0);
$q_index = $i - $y_max - 1;
if ($x_value[0] == $y_value[0]) {
$quotient->value[$q_index] = 0x3ffffff;
} else {
//.........这里部分代码省略.........
示例13: assetIdToName
protected function assetIdToName($asset_id)
{
// BTC = 'BTC'
// XCP = 'XCP'
if ($asset_id === 0) {
return 'BTC';
}
if ($asset_id === 1) {
return 'XCP';
}
$b26_digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if ($asset_id < pow(26, 3)) {
throw new Exception("asset ID was too low (" . json_encode($asset_id, 192) . ")", 1);
}
# Divide that integer into Base 26 string.
$asset_name = '';
$n = gmp_init($asset_id);
while (gmp_cmp($n, 0) > 0) {
list($n, $r) = gmp_div_qr($n, 26, GMP_ROUND_ZERO);
$asset_name = substr($b26_digits, gmp_intval($r), 1) . $asset_name;
}
return $asset_name;
}
示例14: base58_encode
public function base58_encode($data, $littleEndian = true)
{
$res = '';
$dataIntVal = gmp_init($data, 16);
while (gmp_cmp($dataIntVal, gmp_init(0, 10)) > 0) {
$qr = gmp_div_qr($dataIntVal, gmp_init(58, 10));
$dataIntVal = $qr[0];
$reminder = gmp_strval($qr[1]);
if (!$this->base58_permutation($reminder)) {
throw new \Exception('Something went wrong during base58 encoding');
}
$res .= $this->base58_permutation($reminder);
}
//get number of leading zeros
$leading = '';
$i = 0;
while (substr($data, $i, 1) == '0') {
if ($i != 0 && $i % 2) {
$leading .= '1';
}
$i++;
}
if ($littleEndian) {
return strrev($res . $leading);
} else {
return $res . $leading;
}
}
示例15: divide
public function divide($y)
{
switch (MATH_BIGINTEGER_MODE) {
case MATH_BIGINTEGER_MODE_GMP:
$quotient = new Math_BigInteger();
$remainder = new Math_BigInteger();
list($quotient->value, $remainder->value) = gmp_div_qr($this->value, $y->value);
if (gmp_sign($remainder->value) < 0) {
$remainder->value = gmp_add($remainder->value, gmp_abs($y->value));
}
return array($this->_normalize($quotient), $this->_normalize($remainder));
case MATH_BIGINTEGER_MODE_BCMATH:
$quotient = new Math_BigInteger();
$remainder = new Math_BigInteger();
$quotient->value = bcdiv($this->value, $y->value, 0);
$remainder->value = bcmod($this->value, $y->value);
if ($remainder->value[0] == '-') {
$remainder->value = bcadd($remainder->value, $y->value[0] == '-' ? substr($y->value, 1) : $y->value, 0);
}
return array($this->_normalize($quotient), $this->_normalize($remainder));
}
if (count($y->value) == 1) {
list($q, $r) = $this->_divide_digit($this->value, $y->value[0]);
$quotient = new Math_BigInteger();
$remainder = new Math_BigInteger();
$quotient->value = $q;
$remainder->value = array($r);
$quotient->is_negative = $this->is_negative != $y->is_negative;
return array($this->_normalize($quotient), $this->_normalize($remainder));
}
static $zero;
if (!isset($zero)) {
$zero = new Math_BigInteger();
}
$x = $this->copy();
$y = $y->copy();
$x_sign = $x->is_negative;
$y_sign = $y->is_negative;
$x->is_negative = $y->is_negative = false;
$diff = $x->compare($y);
if (!$diff) {
$temp = new Math_BigInteger();
$temp->value = array(1);
$temp->is_negative = $x_sign != $y_sign;
return array($this->_normalize($temp), $this->_normalize(new Math_BigInteger()));
}
if ($diff < 0) {
if ($x_sign) {
$x = $y->subtract($x);
}
return array($this->_normalize(new Math_BigInteger()), $this->_normalize($x));
}
$msb = $y->value[count($y->value) - 1];
for ($shift = 0; !($msb & MATH_BIGINTEGER_MSB); ++$shift) {
$msb <<= 1;
}
$x->_lshift($shift);
$y->_lshift($shift);
$y_value =& $y->value;
$x_max = count($x->value) - 1;
$y_max = count($y->value) - 1;
$quotient = new Math_BigInteger();
$quotient_value =& $quotient->value;
$quotient_value = $this->_array_repeat(0, $x_max - $y_max + 1);
static $temp;
static $lhs;
static $rhs;
if (!isset($temp)) {
$temp = new Math_BigInteger();
$lhs = new Math_BigInteger();
$rhs = new Math_BigInteger();
}
$temp_value =& $temp->value;
$rhs_value =& $rhs->value;
$temp_value = array_merge($this->_array_repeat(0, $x_max - $y_max), $y_value);
while (0 <= $x->compare($temp)) {
++$quotient_value[$x_max - $y_max];
$x = $x->subtract($temp);
$x_max = count($x->value) - 1;
}
for ($i = $x_max; $y_max + 1 <= $i; --$i) {
$x_value =& $x->value;
$x_window = array(isset($x_value[$i]) ? $x_value[$i] : 0, isset($x_value[$i - 1]) ? $x_value[$i - 1] : 0, isset($x_value[$i - 2]) ? $x_value[$i - 2] : 0);
$y_window = array($y_value[$y_max], 0 < $y_max ? $y_value[$y_max - 1] : 0);
$q_index = $i - $y_max - 1;
if ($x_window[0] == $y_window[0]) {
$quotient_value[$q_index] = MATH_BIGINTEGER_MAX_DIGIT;
} else {
$quotient_value[$q_index] = (int) ($x_window[0] * MATH_BIGINTEGER_BASE_FULL + $x_window[1]) / $y_window[0];
}
$temp_value = array($y_window[1], $y_window[0]);
$lhs->value = array($quotient_value[$q_index]);
$lhs = $lhs->multiply($temp);
$rhs_value = array($x_window[2], $x_window[1], $x_window[0]);
while (0 < $lhs->compare($rhs)) {
--$quotient_value[$q_index];
$lhs->value = array($quotient_value[$q_index]);
$lhs = $lhs->multiply($temp);
}
$adjust = $this->_array_repeat(0, $q_index);
//.........这里部分代码省略.........