/** * @param int $usageIndicator */ public function addKeyUsage($usageIndicator) { if (!($usageIndicator >= 0 && $usageIndicator < 8)) { throw new \InvalidArgumentException(); } gmp_setbit($this->bits, $usageIndicator); }
public function add($value) { $hashes = $this->_hashes($value); foreach ($hashes as $hash) { gmp_setbit($this->_gmp, $hash); } }
public static function BitClear($Number, $Bit) { if (is_resource($Number) !== true) { $Number = gmp_init($Number); } gmp_setbit($Number, $Bit - 1, false); return $Number; }
function bigint_random($n, $s) { $result = gmp_init(0); for ($i = 0; $i < $n; $i++) { if (mt_rand(0, 1)) { gmp_setbit($result, $i); } } if ($s) { gmp_setbit($result, $n - 1); } return $result; }
/** * Make the current number odd * * If the current number is odd it'll be unchanged. If it's even, one will be added to it. * * @see self::randomPrime() * @access private */ function _make_odd() { switch (MATH_BIGINTEGER_MODE) { case MATH_BIGINTEGER_MODE_GMP: gmp_setbit($this->value, 0); break; case MATH_BIGINTEGER_MODE_BCMATH: if ($this->value[strlen($this->value) - 1] % 2 == 0) { $this->value = bcadd($this->value, '1'); } break; default: $this->value[0] |= 1; } }
/** * Generate a random prime number. * * If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, * give up and return false. * * @param optional Integer $min * @param optional Integer $max * @param optional Integer $timeout * @return Math_BigInteger * @access public * @internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=15 HAC 4.44}. */ function randomPrime($min = false, $max = false, $timeout = false) { // gmp_nextprime() requires PHP 5 >= 5.2.0 per <http://php.net/gmp-nextprime>. if (MATH_BIGINTEGER_MODE == MATH_BIGINTEGER_MODE_GMP && function_exists('gmp_nextprime')) { // we don't rely on Math_BigInteger::random()'s min / max when gmp_nextprime() is being used since this function // does its own checks on $max / $min when gmp_nextprime() is used. When gmp_nextprime() is not used, however, // the same $max / $min checks are not performed. 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; } } $x = $this->random($min, $max); $x->value = gmp_nextprime($x->value); if ($x->compare($max) <= 0) { return $x; } $x->value = gmp_nextprime($min->value); if ($x->compare($max) <= 0) { return $x; } return false; } $repeat1 = $repeat2 = array(); $one = new Math_BigInteger(1); $two = new Math_BigInteger(2); $start = time(); do { if ($timeout !== false && time() - $start > $timeout) { return false; } $x = $this->random($min, $max); if ($x->equals($two)) { return $x; } // make the number odd switch (MATH_BIGINTEGER_MODE) { case MATH_BIGINTEGER_MODE_GMP: gmp_setbit($x->value, 0); break; case MATH_BIGINTEGER_MODE_BCMATH: if ($x->value[strlen($x->value) - 1] % 2 == 0) { $x = $x->add($one); } break; default: $x->value[0] |= 1; } // if we've seen this number twice before, assume there are no prime numbers within the given range if (in_array($x->value, $repeat1)) { if (in_array($x->value, $repeat2)) { return false; } else { $repeat2[] = $x->value; } } else { $repeat1[] = $x->value; } } while (!$x->isPrime()); return $x; }
public static function random($n, $s) { switch (BigInt::support()) { case 'gmp': $result = gmp_init(0); for ($i = 0; $i < $n; $i++) { if (mt_rand(0, 1)) { gmp_setbit($result, $i); } } if ($s) { gmp_setbit($result, $n - 1); } return $result; case 'big_int': $result = bi_rand($n); if ($s) { $result = bi_set_bit($result, $n - 1); } return $result; case 'bcmath': bcscale(0); $t = BigInt::bcpow(2, $n); if ($s == 1) { $m = bcdiv($t, 2); $t = bcsub($m, 1); } else { $m = 0; $t = bcsub($t, 1); } $l = strlen($t); $n = (int) ($l / 9) + 1; $r = ''; while ($n) { $r .= substr('000000000' . mt_rand(0, 999999999), -9); --$n; } $r = substr($r, 0, $l); while (bccomp($r, $t) == 1) { $r = substr($r, 1, $l) . mt_rand(0, 9); } return bcadd($r, $m); case '': default: return BigInt::_random($n, $s); } }
/** * Scan the blobs table for rows not registered in blob_tracking (and thus not * registered in the text table). * * Orphan blobs are indicative of DB corruption. They are inaccessible and * should probably be deleted. */ function findOrphanBlobs() { if (!extension_loaded('gmp')) { echo "Can't find orphan blobs, need bitfield support provided by GMP.\n"; return; } $dbw = wfGetDB(DB_MASTER); foreach ($this->clusters as $cluster) { echo "Searching for orphan blobs in {$cluster}...\n"; $lb = wfGetLBFactory()->getExternalLB($cluster); try { $extDB = $lb->getConnection(DB_SLAVE); } catch (DBConnectionError $e) { if (strpos($e->error, 'Unknown database') !== false) { echo "No database on {$cluster}\n"; } else { echo "Error on {$cluster}: " . $e->getMessage() . "\n"; } continue; } $table = $extDB->getLBInfo('blobs table'); if (is_null($table)) { $table = 'blobs'; } if (!$extDB->tableExists($table)) { echo "No blobs table on cluster {$cluster}\n"; continue; } $startId = 0; $batchesDone = 0; $actualBlobs = gmp_init(0); $endId = $extDB->selectField($table, 'MAX(blob_id)', false, __METHOD__); // Build a bitmap of actual blob rows while (true) { $res = $extDB->select($table, array('blob_id'), array('blob_id > ' . $extDB->addQuotes($startId)), __METHOD__, array('LIMIT' => $this->batchSize, 'ORDER BY' => 'blob_id')); if (!$res->numRows()) { break; } foreach ($res as $row) { gmp_setbit($actualBlobs, $row->blob_id); } $startId = $row->blob_id; ++$batchesDone; if ($batchesDone >= $this->reportingInterval) { $batchesDone = 0; echo "{$startId} / {$endId}\n"; } } // Find actual blobs that weren't tracked by the previous passes // This is a set-theoretic difference A \ B, or in bitwise terms, A & ~B $orphans = gmp_and($actualBlobs, gmp_com($this->trackedBlobs[$cluster])); // Traverse the orphan list $insertBatch = array(); $id = 0; $numOrphans = 0; while (true) { $id = gmp_scan1($orphans, $id); if ($id == -1) { break; } $insertBatch[] = array('bo_cluster' => $cluster, 'bo_blob_id' => $id); if (count($insertBatch) > $this->batchSize) { $dbw->insert('blob_orphans', $insertBatch, __METHOD__); $insertBatch = array(); } ++$id; ++$numOrphans; } if ($insertBatch) { $dbw->insert('blob_orphans', $insertBatch, __METHOD__); } echo "Found {$numOrphans} orphan(s) in {$cluster}\n"; } }
list($sqrt2, $sqrt2rem) = gmp_sqrtrem("7"); list($sqrt3, $sqrt3rem) = gmp_sqrtrem("1048576"); echo gmp_strval($sqrt1) . ", " . gmp_strval($sqrt1rem) . "\n"; echo gmp_strval($sqrt2) . ", " . gmp_strval($sqrt2rem) . "\n"; echo gmp_strval($sqrt3) . ", " . gmp_strval($sqrt3rem) . "\n"; // gmp_strval $a = gmp_init("0x41682179fbf5"); printf("Decimal: %s, 36-based: %s" . PHP_EOL, gmp_strval($a), gmp_strval($a, 36)); // gmp_sub $sub = gmp_sub("281474976710656", "4294967296"); // 2^48 - 2^32 echo gmp_strval($sub) . "\n"; // gmp_testbit $n = gmp_init("1000000"); var_dump(gmp_testbit($n, 1)); gmp_setbit($n, 1); var_dump(gmp_testbit($n, 1)); // gmp_xor $xor1 = gmp_init("1101101110011101", 2); $xor2 = gmp_init("0110011001011001", 2); $xor3 = gmp_xor($xor1, $xor2); echo gmp_strval($xor3, 2) . "\n"; // misc $someBigNumber = '521384146951941511609433057270365759591953092186117381932611' . '7931051185480744623799627495673518857527248912279381830119491298336733624' . '4065664308602139494639522473719070217986094370277293176752384674818467669' . '4051320005681271452635608277857713427577896091736371787214684409012654958' . '5371050792279689258923542019956112129021960864034418159813629774771309960' . '5187072113497804995105973173281609631859502445945534690830264252230825334' . '4685035261931188171010003137835332083814206171776691473035982534904287554' . '687311595628638823537875937519577818577805321712'; $a = gmp_init($someBigNumber, 10); var_dump(gmp_strval($a) == $someBigNumber); var_dump(gmp_strval($a, 32)); $b = gmp_init(0xffffff, 16); var_dump(gmp_strval($b)); $c = gmp_init($b); var_dump(gmp_strval($c, 16));
<?php $n = gmp_init(0); var_dump(gmp_testbit($n, -10)); var_dump(gmp_testbit($n, 0)); var_dump(gmp_testbit($n, 1)); var_dump(gmp_testbit($n, 100)); $n = gmp_init(-1); var_dump(gmp_testbit($n, 1)); var_dump(gmp_testbit($n, -1)); $n = gmp_init("1000000"); var_dump(gmp_testbit($n, 1)); gmp_setbit($n, 1); var_dump(gmp_testbit($n, 1)); var_dump(gmp_strval($n)); gmp_setbit($n, 5); var_dump(gmp_testbit($n, 5)); var_dump(gmp_strval($n)); $n = gmp_init("238462734628347239571823641234"); var_dump(gmp_testbit($n, 5)); gmp_setbit($n, 5); var_dump(gmp_testbit($n, 5)); var_dump(gmp_strval($n)); gmp_clrbit($n, 5); var_dump(gmp_testbit($n, 5)); var_dump(gmp_strval($n)); echo "Done\n";
/** * Arithmetic right shift * @param resource|int|string $g * @param int $shift number of bits to shift right * @return resource $g shifted right $shift bits */ static function shift_right($g, $shift) { if (0 == $shift) { return $g; } if (0 <= gmp_sign($g)) { $m = gmp_div($g, gmp_pow(self::gmp_2(), $shift)); } else { $g = gmp_and($g, self::gmp_0xfs()); $m = gmp_div($g, gmp_pow(self::gmp_2(), $shift)); $m = gmp_and($m, self::gmp_0xfs()); for ($i = 63; $i >= 63 - $shift; $i--) { gmp_setbit($m, $i); } $m = gmp_neg(gmp_add(gmp_and(gmp_com($m), self::gmp_0xfs()), self::gmp_1())); } return $m; }
/** * Make the current number odd * * If the current number is odd it'll be unchanged. If it's even, one will be added to it. * * @see randomPrime() * @access private */ private function _make_odd() { switch (self::$mode) { case Math_BigInteger::MODE_GMP: gmp_setbit($this->value, 0); break; case Math_BigInteger::MODE_BCMATH: if ($this->value[strlen($this->value) - 1] % 2 == 0) { $this->value = bcadd($this->value, '1'); } break; default: $this->value[0] |= 1; } }
private static function inet_ntogmp( $addr ) { // 16*8 == 128 $gmp = gmp_init( 0 ); for( $bits = 0; $bits < 16; $bits++ ) { $byte = ord( $addr[15-$bits] ); for( $b = 0; $b < 8; $b++ ) { gmp_setbit( $gmp, $bits*8+$b, $byte & (1<<$b) ); } } // echo inet_ntop( $addr ).' -> '.gmp_strval( $gmp, 16 )."<br />\n"; return $gmp; }
<?php $n = gmp_init(-1); gmp_setbit($n, 10, -1); var_dump(gmp_strval($n)); $n = gmp_init(5); gmp_setbit($n, -20, 0); var_dump(gmp_strval($n)); $n = gmp_init(5); gmp_setbit($n, 2, 0); var_dump(gmp_strval($n)); $n = gmp_init(5); gmp_setbit($n, 1, 1); var_dump(gmp_strval($n)); $n = gmp_init("100000000000"); gmp_setbit($n, 23, 1); var_dump(gmp_strval($n)); gmp_setbit($n, 23, 0); var_dump(gmp_strval($n)); gmp_setbit($n, 3); var_dump(gmp_strval($n)); $b = ""; gmp_setbit($b, 23); gmp_setbit($b); gmp_setbit($b, 23, 1, 1); gmp_setbit($b, array()); $a = array(); gmp_setbit($a, array()); echo "Done\n";