/** * Add two arbitrary precision numbers * * @param string $a The left operand, as a string. * @param string $b The right operand, as a string. * @access public * @return string The sum of the two operands, as a string */ public function add($a, $b) { $a = new Math_BigInteger($a); $b = new Math_BigInteger($b); $c = $a->add($b); return $c->toString(); }
public function add1($pos, $value, $sid) { if ($pos < $this->size()) { $tmp1 = new Math_BigInteger($this->get($pos)->getInt()); $tmp = $tmp1->add($value); unset($this->mPosition[$pos]); $this->mPosition[$pos] = new LogootId($tmp->toString(), $sid); } else { $this->mPosition[] = new LogootId($tmp->toString(), $sid); } }
/** * Generate a random number * * @param optional Integer $min * @param optional Integer $max * @return Math_BigInteger * @access public */ function random($min = false, $max = false) { if ($min === false) { $min = new Math_BigInteger(0); } if ($max === false) { $max = new Math_BigInteger(0x7fffffff); } $compare = $max->compare($min); if (!$compare) { return $this->_normalize($min); } else { if ($compare < 0) { // if $min is bigger then $max, swap $min and $max $temp = $max; $max = $min; $min = $temp; } } $generator = $this->generator; $max = $max->subtract($min); $max = ltrim($max->toBytes(), chr(0)); $size = strlen($max) - 1; $random = ''; $bytes = $size & 1; for ($i = 0; $i < $bytes; ++$i) { $random .= chr($generator(0, 255)); } $blocks = $size >> 1; for ($i = 0; $i < $blocks; ++$i) { // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems $random .= pack('n', $generator(0, 0xffff)); } $temp = new Math_BigInteger($random, 256); if ($temp->compare(new Math_BigInteger(substr($max, 1), 256)) > 0) { $random = chr($generator(0, ord($max[0]) - 1)) . $random; } else { $random = chr($generator(0, ord($max[0]))) . $random; } $random = new Math_BigInteger($random, 256); return $this->_normalize($random->add($min)); }
/** * generation of a position, logoot algorithm * @param <LogootPosition> $p is the previous logootPosition * @param <LogootPosition> $q is the next logootPosition * @param $N number of positions generated (should be 1 in our case) * @param <Integer> $rep_sid session id * @param <Integer> $rep_clock session clock * @param $boundary Cf. method * @return <LogootPosition List> $N logootPosition(s) between $start and $end */ public static function getLogootPosition(LogootPosition $p, LogootPosition $q, $nb, $rep_sid, $rep_clock = 0, $boundary = NULL) { wfDebugLog('p2p', $rep_clock . " - function LogootPosition::getLogootPosition " . $p . " / " . $q . " pour " . $nb . " position(s)"); $one = new Math_BigInteger("1"); // Recherche de l'interval optimal $index = 0; $interval = INT_MIN; $size = max($p->size(), $q->size()) + 1; $prefix_p = array(0 => array('cum_val' => "", 'id_str_val' => "")); $prefix_q = array(0 => array('cum_val' => "", 'id_str_val' => "")); while ($interval < $nb) { $index += 1; // recherche de prefix($p, index); if ($index <= $p->size()) { $str_val_p = str_pad($p->get($index - 1)->getInt(), DIGIT, "0", STR_PAD_LEFT); } else { $str_val_p = LPINTMINDIGIT; } $prefix_p[$index] = array('id_str_val' => $str_val_p, 'cum_val' => $prefix_p[$index - 1]['cum_val'] . $str_val_p); // recherche de prefix($p, index); if ($index <= $q->size()) { $str_val_q = str_pad($q->get($index - 1)->getInt(), DIGIT, "0", STR_PAD_LEFT); } else { $str_val_q = LPINTMINDIGIT; } $prefix_q[$index] = array('id_str_val' => $str_val_q, 'cum_val' => $prefix_q[$index - 1]['cum_val'] . $str_val_q); // Calcul de l'interval sur les nouveaux prefixes $BI_p = new Math_BigInteger($prefix_p[$index]['cum_val']); $BI_q = new Math_BigInteger($prefix_q[$index]['cum_val']); $BIinterval = $BI_q->subtract($BI_p)->subtract($one); $interval = (int) $BIinterval->__toString(); /*wfDebugLog('p2p', $index . " : Prefix_p " . (string) $prefix_p[$index]['cum_val'] . '/' . $prefix_p[$index]['id_str_val'] . " Prefix_q " . (string) $prefix_q[$index]['cum_val'] . '/' . $prefix_q[$index]['id_str_val'] . " Interval " . $interval);*/ } // Construction des identifiants //wfDebugLog('p2p', "N " . $nb . " Interval " . $interval . " index " . $index); $step = (int) $interval / $nb; if (isset($boundary)) { $step = $boundary < $step ? $boundary : $step; } $BI_step = new Math_BigInteger($step); $BI_r = new Math_BigInteger($prefix_p[$index]['cum_val']); $list = array(); //wfDebugLog('p2p', "Step :" . $step . "/" . $boundary); for ($j = 1; $j <= $nb; $j++) { $BI_nr = $BI_r->add(new Math_BigInteger(rand(1, $step))); //wfDebugLog('p2p', "nr " . (string) $BI_nr . " r " . (string) $BI_r); // pour découper une chaine en paquets de N car : str_split($cdc, $N) ! $str_nr0 = (string) $BI_nr; // on fait en sorte que le découpage soit un multiple de DIGIT pour ne pas créer de décallage if (strlen($str_nr0) % ($index * DIGIT) != 0) { $str_nr = str_pad($str_nr0, strlen($str_nr0) + ($index * DIGIT - strlen($str_nr0) % ($index * DIGIT)), "0", STR_PAD_LEFT); } else { $str_nr = $str_nr0; } //wfDebugLog('p2p', "str_nr0 " . $str_nr0 . " str_nr " . $str_nr); $tab_nr = str_split($str_nr, DIGIT); $pos = new LogootPosition(); for ($i = 1; $i <= count($tab_nr); $i++) { $d = $tab_nr[$i - 1]; //wfDebugLog('p2p', "$i#" . $prefix_p[$i]['id_str_val'] . "#" . $prefix_q[$i]['id_str_val'] . "#" . $d); if ($i <= $p->size() && $prefix_p[$i]['id_str_val'] == $d) { $id = new LogootId($d, $p->get($i - 1)->getSessionId(), $p->get($i - 1)->getClock()); } elseif ($i <= $q->size() && $prefix_q[$i]['id_str_val'] == $d) { $id = new LogootId($d, $q->get($i - 1)->getSessionId(), $q->get($i - 1)->getClock()); } else { $id = new LogootId($d, $rep_sid, $rep_clock); } $pos->addId($id); } wfDebugLog('p2p', "===========>" . $pos->__toString()); $list[] = $pos; $BI_r = $BI_r->add($BI_step); } return $list; }
/** * Generate a random number * * @param optional Integer $min * @param optional Integer $max * @return Math_BigInteger * @access public */ function random($min = false, $max = false) { if ($min === false) { $min = new Math_BigInteger(0); } if ($max === false) { $max = new Math_BigInteger(0x7fffffff); } $compare = $max->compare($min); if (!$compare) { return $this->_normalize($min); } else { if ($compare < 0) { // if $min is bigger then $max, swap $min and $max $temp = $max; $max = $min; $min = $temp; } } $generator = $this->generator; $max = $max->subtract($min); $max = ltrim($max->toBytes(), chr(0)); $size = strlen($max) - 1; $crypt_random = function_exists('crypt_random_string') || !class_exists('Crypt_Random') && function_exists('crypt_random_string'); if ($crypt_random) { $random = crypt_random_string($size); } else { $random = ''; if ($size & 1) { $random .= chr(mt_rand(0, 255)); } $blocks = $size >> 1; for ($i = 0; $i < $blocks; ++$i) { // mt_rand(-2147483648, 0x7FFFFFFF) always produces -2147483648 on some systems $random .= pack('n', mt_rand(0, 0xffff)); } } $fragment = new Math_BigInteger($random, 256); $leading = $fragment->compare(new Math_BigInteger(substr($max, 1), 256)) > 0 ? ord($max[0]) - 1 : ord($max[0]); if (!$crypt_random) { $msb = chr(mt_rand(0, $leading)); } else { $cutoff = floor(0xff / $leading) * $leading; while (true) { $msb = ord(crypt_random_string(1)); if ($msb <= $cutoff) { $msb %= $leading; break; } } $msb = chr($msb); } $random = new Math_BigInteger($msb . $random, 256); return $this->_normalize($random->add($min)); }
$newTimeRange = $min_timestamp - 60 * 8; $existQuery = "SELECT address,minerdiff,blockdiff,time FROM shares_history WHERE time > {$newTimeRange}"; $existResultMinersss = mysqli_query($mysqli, $existQuery) or die("Database Error"); $count_response = mysqli_num_rows($existResultMinersss); echo "\nShares_OLD_Taken:" . $count_response . ''; $current .= "\nShares_OLD_Taken:" . $count_response . ''; while ($row = mysqli_fetch_row($existResultMinersss)) { $miner_adr = $row[0]; $miner_adr_balance = new Math_BigInteger($row[1]); $totalMinersDiff = $totalMinersDiff->add($miner_adr_balance); if (!isset($miner_payouts["'{$miner_adr}'"])) { $miner_payouts["'{$miner_adr}'"] = $miner_adr_balance; $old_new_added++; } else { $miner_adr_balance_fromArray = new Math_BigInteger($miner_payouts["'{$miner_adr}'"]); $setNewValue = $miner_adr_balance_fromArray->add($miner_adr_balance); $miner_payouts["'{$miner_adr}'"] = $setNewValue->toString(); $old_old_old++; } } echo "\nShares_OLD_Taken__NEWADDED:" . $old_new_added . ''; $current .= "\nShares_OLD_Taken__NEWADDED:" . $old_new_added . ''; echo "\nShares_OLD_Taken__OLD_SUMMARY:" . $old_old_old . ''; $current .= "\nShares_OLD_Taken__OLD_SUMMARY:" . $old_old_old . ''; } echo "\n============================================================================="; echo "\nTotal Miners Diff:" . $totalMinersDiff->toString() . ' = ' . $block_coins_size->toString() . ' wei'; $current .= "\n============================================================================="; $current .= "\nTotal Miners Diff:" . $totalMinersDiff->toString() . ' = ' . $block_coins_size->toString() . ' wei'; $totalsplit = new Math_BigInteger(0); $totalEther = new Math_BigInteger(0);
// $Id$ // Example of how to use of BigInteger. The output can be compared to the output that the BCMath functions would yield. // bcpowmod is included with Math_BigInteger.php via PHP_Compat. require __DIR__ . '/../vendor/autoload.php'; $x = mt_rand(1, 10000000); $y = mt_rand(1, 10000000); $z = mt_rand(1, 10000000); $_x = new Math_BigInteger($x); $_y = new Math_BigInteger($y); $_z = new Math_BigInteger($z); echo "\$x = {$x};\r\n"; echo "\$y = {$y};\r\n"; echo "\$z = {$z};\r\n"; echo "\r\n"; $result = bcadd($x, $y); $_result = $_x->add($_y); echo "\$result = \$x+\$y;\r\n"; echo "{$result}\r\n"; echo $_result->toString(); echo "\r\n\r\n"; $result = bcsub($result, $y); $_result = $_result->subtract($_y); echo "\$result = \$result-\$y;\r\n"; echo "{$result}\r\n"; echo $_result->toString(); echo "\r\n\r\n"; $result = bcdiv($x, $y); list($_result, ) = $_x->divide($_y); echo "\$result = \$x/\$y;\r\n"; echo "{$result}\r\n"; echo $_result->toString();
/** * Generate a random number * * $generator should be the name of a random number generating function whose first parameter is the minimum * value and whose second parameter is the maximum value. If this function needs to be seeded, it should be * done before this function is called. * * @param optional Integer $min * @param optional Integer $max * @param optional String $generator * @return Math_BigInteger * @access public */ function random(Math_BigInteger $min = NULL, Math_BigInteger $max = NULL, $generator = 'mt_rand') { if ($min === NULL) { $min = new Math_BigInteger(0); } /* * @author muller jean-philippe * This condition is used to exclude the min value from the possible *values returned by this random */ /*else { $min = $min->add(new Math_BigInteger(1)); }*/ // end of modification if ($max === NULL) { $max = new Math_BigInteger(0x7fffffff); } $compare = $max->compare($min); if (!$compare) { return $min; } else { if ($compare < 0) { // if $min is bigger then $max, swap $min and $max $temp = $max; $max = $min; $min = $temp; } } $max = $max->subtract($min); $max = ltrim($max->toBytes(), chr(0)); $size = strlen($max) - 1; $random = ''; $bytes = $size & 3; for ($i = 0; $i < $bytes; $i++) { $random .= chr($generator(0, 255)); } $blocks = $size >> 2; for ($i = 0; $i < $blocks; $i++) { $random .= pack('N', $generator(-2147483648.0, 0x7fffffff)); } $temp = new Math_BigInteger($random, 256); if ($temp->compare(new Math_BigInteger(substr($max, 1), 256)) > 0) { $random = chr($generator(0, ord($max[0]) - 1)) . $random; } else { $random = chr($generator(0, ord($max[0]))) . $random; } $random = new Math_BigInteger($random, 256); return $random->add($min); }
/** * Generate a random number * * $generator should be the name of a random number generating function whose first parameter is the minimum * value and whose second parameter is the maximum value. If this function needs to be seeded, it should be * done before this function is called. * * @param optional Integer $min * @param optional Integer $max * @param optional String $generator * @return Math_BigInteger * @access public */ function random($min = false, $max = false, $generator = 'mt_rand') { if ($min === false) { $min = new Math_BigInteger(0); } if ($max === false) { $max = new Math_BigInteger(0x7fffffff); } $compare = $max->compare($min); if (!$compare) { return $min; } else { if ($compare < 0) { // if $min is bigger then $max, swap $min and $max $temp = $max; $max = $min; $min = $temp; } } $max = $max->subtract($min); $max = ltrim($max->toBytes(), chr(0)); $size = strlen($max) - 1; $random = ''; $bytes = $size & 3; for ($i = 0; $i < $bytes; $i++) { $random .= chr($generator(0, 255)); } $blocks = $size >> 2; for ($i = 0; $i < $blocks; $i++) { $random .= pack('N', $generator(-2147483648.0, 0x7fffffff)); } $temp = new Math_BigInteger($random, 256); if ($temp->compare(new Math_BigInteger(substr($max, 1), 256)) > 0) { $random = chr($generator(0, ord($max[0]) - 1)) . $random; } else { $random = chr($generator(0, ord($max[0]))) . $random; } $random = new Math_BigInteger($random, 256); return $random->add($min); }
public function random($min = false, $max = false) { if ($min === false) { $min = new Math_BigInteger(0); } if ($max === false) { $max = new Math_BigInteger(2147483647); } $compare = $max->compare($min); if (!$compare) { return $this->_normalize($min); } else { if ($compare < 0) { $temp = $max; $max = $min; $min = $temp; } } $max = $max->subtract($min); $max = ltrim($max->toBytes(), chr(0)); $size = strlen($max) - 1; $random = ''; if ($size & 1) { $random .= chr(mt_rand(0, 255)); } $blocks = $size >> 1; for ($i = 0; $i < $blocks; ++$i) { $random .= pack('n', mt_rand(0, 65535)); } $fragment = new Math_BigInteger($random, 256); $leading = 0 < $fragment->compare(new Math_BigInteger(substr($max, 1), 256)) ? ord($max[0]) - 1 : ord($max[0]); $msb = chr(mt_rand(0, $leading)); $random = new Math_BigInteger($msb . $random, 256); return $this->_normalize($random->add($min)); }
/** * generation of a position, logoot algorithm * @param <Object> $start is the previous logootPosition * @param <Object> $end is the next logootPosition * @param <Integer> $N number of positions generated (should be 1 in our case) * @param <Object> $sid session id * @return <Object> a logootPosition between $start and $end */ private function getLogootPosition($start, $end, $N, $sid) { $result = array(); $Id_Max = LogootId::IdMax(); $Id_Min = LogootId::IdMin(); $i = 0; $pos = array(); $currentPosition = new LogootPosition($pos); // voir constructeur $inf = new Math_BigInteger("0"); $sup = new Math_BigInteger("0"); $isInf = false; while (true) { $inf = new Math_BigInteger($start->get($i)->getInt()); if ($isInf == true) { $sup = new Math_BigInteger(INT_MAX); } else { $sup = new Math_BigInteger($end->get($i)->getInt()); } $tmpVal = $sup->subtract($inf); $tmpVal1 = $tmpVal->subtract(new Math_BigInteger("1")); if ($tmpVal1->compare($N) > 0) { // inf = start.get(i).getInteger(); // sup = end.get(i).getInteger(); break; } $currentPosition->add($start->get($i)); $i++; if ($i == $start->size()) { $start->add($Id_Min); } if ($i == $end->size()) { $end->add($Id_Max); } if ($inf->compare($sup) < 0) { $isInf = true; } } $binf = $inf->add(new Math_BigInteger("1")); $bsup = $sup->subtract(new Math_BigInteger("1")); $slot = $bsup->subtract($binf); $stepTmp = $slot->divide($N); $step = $stepTmp[0]; // quotient, [1] is the remainder $old = clone $currentPosition; if ($step->compare(new Math_BigInteger(INT_MAX)) > 0) { $lstep = new Math_BigInteger(INT_MAX); $r = clone $currentPosition; $tmpVal2 = $inf->random($inf, $sup); $r->set($i, $tmpVal2->toString(), $sid); $result[] = $r; // result est une arraylist<Position> return $result; } else { $lstep = $step; } if ($lstep->compare(new Math_BigInteger("0")) == 0) { $lstep = new Math_BigInteger("1"); } $p = clone $currentPosition; $p->set($i, $inf->toString(), $sid); $tmpVal3 = (int) $N->toString(); for ($j = 0; $j < $tmpVal3; $j++) { $r = clone $p; if (!($lstep->compare(new Math_BigInteger("1")) == 0)) { $tmpVal4 = new Math_BigInteger($p->get($i)->getInt()); $tmpVal5 = $tmpVal4->add($lstep); // max $tmpVal6 = new Math_BigInteger($p->get($i)->getInt()); // min $add = $tmpVal6->random($tmpVal6, $tmpVal5); $r->set($i, $add->toString(), $sid); } else { $r->add1($i, new Math_BigInteger("1"), $sid); } $result[] = clone $r; // voir $old = clone $r; $tmpVal7 = new Math_BigInteger($p->get($i)->getInt()); $tmpVal7 = $tmpVal7->add($lstep); $p->set($i, $tmpVal7->toString(), $sid); } return $result; }