Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.
author | Jim Wigginton terrafrost@php.net |
---|---|
package | Default |
__clone()
__construct(string|integer|\phpseclib3\Math\BigInteger\Engines\Engine $x,integer $base = 10)
If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using two's compliment. The sole exception to this is -10, which is treated the same as 10 is.
string|integer|\phpseclib3\Math\BigInteger\Engines\Engine
Base-10 number or base-$base number if $base set.
integer
__debugInfo()
Will be called, automatically, when print_r() or var_dump() are called
__sleep(): array
Will be called, automatically, when serialize() is called on a BigInteger object.
sleep() / wakeup() have been around since PHP 4.0
\Serializable was introduced in PHP 5.1 and deprecated in PHP 8.1: https://wiki.php.net/rfc/phase_out_serializable
serialize() / unserialize() were introduced in PHP 7.4: https://wiki.php.net/rfc/custom_object_serialization
array
__toString()
__wakeup()
Will be called, automatically, when unserialize() is called on a BigInteger object.
add(\phpseclib3\Math\BigInteger $y): \phpseclib3\Math\BigInteger
between(\phpseclib3\Math\BigInteger $min,\phpseclib3\Math\BigInteger $max): boolean
bitwise_and(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger
bitwise_leftRotate(integer $shift): \phpseclib3\Math\BigInteger
Instead of the top x bits being dropped they're appended to the shifted bit string.
integer
\phpseclib3\Math\BigInteger
bitwise_leftShift(integer $shift): \phpseclib3\Math\BigInteger
Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
integer
\phpseclib3\Math\BigInteger
bitwise_or(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger
bitwise_rightRotate(integer $shift): \phpseclib3\Math\BigInteger
Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
integer
\phpseclib3\Math\BigInteger
bitwise_rightShift(integer $shift): \phpseclib3\Math\BigInteger
Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
integer
\phpseclib3\Math\BigInteger
bitwise_split(integer $split): array<mixed,\phpseclib3\Math\BigInteger>
Splits BigInteger's into chunks of $split bits
integer
array<mixed,\phpseclib3\Math\BigInteger>
bitwise_xor(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger
compare(\phpseclib3\Math\BigInteger $y): integer
Although one might think !$x->compare($y) means $x != $y, it, in fact, means the opposite. The reason for this is demonstrated thusly:
$x > $y: $x->compare($y) > 0 $x < $y: $x->compare($y) < 0 $x == $y: $x->compare($y) == 0
Note how the same comparison operator is used. If you want to test for equality, use $x->equals($y).
{@internal Could return $this->subtract($x), but that's not as fast as what we do do.}
see | \phpseclib3\Math\BigInteger::equals() |
---|
integer
in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
createRecurringModuloFunction(): callable
Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation
callable
divide(\phpseclib3\Math\BigInteger $y): array<mixed,\phpseclib3\Math\BigInteger>
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 (basically, the "common residue" is the first positive modulo).
Here's an example:
<?php
$a = new \phpseclib3\Math\BigInteger('10');
$b = new \phpseclib3\Math\BigInteger('20');
list($quotient, $remainder) = $a->divide($b);
echo $quotient->toString(); // outputs 0 echo "\r\n"; echo $remainder->toString(); // outputs 10 ?>
array<mixed,\phpseclib3\Math\BigInteger>
equals(\phpseclib3\Math\BigInteger $x): boolean
If you need to see if one number is greater than or less than another number, use BigInteger::compare()
boolean
extendedGCD(\phpseclib3\Math\BigInteger $n): array<mixed,\phpseclib3\Math\BigInteger>
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
array<mixed,\phpseclib3\Math\BigInteger>
gcd(\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger
getEngine(): array<mixed,string>
array<mixed,string>
getLength(): integer
integer
getLengthInBytes(): integer
integer
getPrecision(): integer|boolean
Returns the precision if it exists, false if it doesn't
integer|boolean
initialize_static_variables()
isNegative(): boolean
boolean
isOdd(): boolean
boolean
isPrime(integer|boolean $t = false): boolean
Assuming the $t parameter is not set, this function has an error rate of 2**-80. The main motivation for the $t parameter is distributability. BigInteger::randomPrime() can be distributed across multiple pageloads on a website instead of just one.
integer|boolean
boolean
jsonSerialize()
max(\phpseclib3\Math\BigInteger $nums): \phpseclib3\Math\BigInteger
min(\phpseclib3\Math\BigInteger $nums): \phpseclib3\Math\BigInteger
minMaxBits(integer $bits): array<mixed,\phpseclib3\Math\BigInteger>
modInverse(\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
\phpseclib3\Math\BigInteger
modPow(\phpseclib3\Math\BigInteger $e,\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger
multiply(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger
negate(): \phpseclib3\Math\BigInteger
pow(\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger
powMod(\phpseclib3\Math\BigInteger $e,\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger
random(integer $size): \phpseclib3\Math\BigInteger
randomPrime(integer $size): \phpseclib3\Math\BigInteger
randomRange(\phpseclib3\Math\BigInteger $min,\phpseclib3\Math\BigInteger $max): \phpseclib3\Math\BigInteger
Returns a random number between $min and $max where $min and $max can be defined using one of the two methods:
BigInteger::randomRange($min, $max) BigInteger::randomRange($max, $min)
\phpseclib3\Math\BigInteger
randomRangePrime(\phpseclib3\Math\BigInteger $min,\phpseclib3\Math\BigInteger $max): false|\phpseclib3\Math\BigInteger
If there's not a prime within the given range, false will be returned.
false|\phpseclib3\Math\BigInteger
root(integer $n = 2): \phpseclib3\Math\BigInteger
Returns the nth root of a positive biginteger, where n defaults to 2
integer
optional
\phpseclib3\Math\BigInteger
scan1divide(\phpseclib3\Math\BigInteger $r): integer
ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));
integer
setEngine(string $main,\phpseclib3\Math\list<string> $modexps = array('DefaultEngine')): void
Throws an exception if the type is invalid
string
\phpseclib3\Math\list
optional
setPrecision(integer $bits)
Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.
integer
subtract(\phpseclib3\Math\BigInteger $y): \phpseclib3\Math\BigInteger
testBit(integer $x): boolean
integer
boolean
toBits(boolean $twos_compliment = false): string
Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.
boolean
string
toBytes(boolean $twos_compliment = false): string
boolean
string
toHex(boolean $twos_compliment = false): string
boolean
string
toString(): string
string
mainEngine :\phpseclib3\Math\class-string<Engine>
var |
---|
\phpseclib3\Math\class-string
engines :\phpseclib3\Math\list<string>
var |
---|
\phpseclib3\Math\list
value :object
var |
---|
object
hex :string
see | |
---|---|
var |
string
precision :integer
see | |
---|---|
var |
integer