BigInteger
class BigInteger implements Serializable (View source)
Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.
Methods
Sets engine type.
Returns the engine type
Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.
Converts a BigInteger to a base-10 number.
__toString() magic method
__debugInfo() magic method
Converts a BigInteger to a byte string (eg. base-256).
Converts a BigInteger to a hex string (eg. base-16).
Converts a BigInteger to a bit string (eg. base-2).
Absolute value.
Set Precision
Get Precision
Serialize
Serialize
Logical Not
Logical Right Shift
Logical Left Shift
Logical Left Rotate
Logical Right Rotate
Returns the smallest and largest n-bit number
Return the size of a BigInteger in bits
Return the size of a BigInteger in bytes
Generates a random number of a certain size
Generates a random prime number of a certain size
Checks a numer to see if it's prime
Calculates the nth root of a biginteger.
Tests BigInteger to see if it is between two integers, inclusive
Clone
Is Odd?
Tests if a bit is set
Is Negative?
Negate
Create Recurring Modulo Function
Bitwise Split
Details
static
setEngine(string $main, array $modexps = ['DefaultEngine'])
Sets engine type.
Throws an exception if the type is invalid
static string[]
getEngine()
Returns the engine type
BigInteger
__construct(string|int|Engine $x = 0, int $base = 10)
Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.
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
toString()
Converts a BigInteger to a base-10 number.
__toString()
__toString() magic method
__debugInfo()
__debugInfo() magic method
Will be called, automatically, when print_r() or var_dump() are called
string
toBytes(bool $twos_compliment = false)
Converts a BigInteger to a byte string (eg. base-256).
string
toHex(bool $twos_compliment = false)
Converts a BigInteger to a hex string (eg. base-16).
string
toBits(bool $twos_compliment = false)
Converts a BigInteger to a bit string (eg. base-2).
Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.
BigInteger
add(BigInteger $y)
Adds two BigIntegers.
BigInteger
subtract(BigInteger $y)
Subtracts two BigIntegers.
BigInteger
multiply(BigInteger $x)
Multiplies two BigIntegers
BigInteger[]
divide(BigInteger $y)
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 (basically, the "common residue" is the first positive modulo).
Here's an example:
divide($b);
echo $quotient->toString(); // outputs 0
echo "\r\n";
echo $remainder->toString(); // outputs 10
?>
BigInteger
modInverse(BigInteger $n)
Calculates modular inverses.
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
BigInteger[]
extendedGCD(BigInteger $n)
Calculates modular inverses.
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
BigInteger
gcd(BigInteger $n)
Calculates the greatest common divisor
Say you have 693 and 609. The GCD is 21.
BigInteger
abs()
Absolute value.
setPrecision(int $bits)
Set Precision
Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.
int|bool
getPrecision()
Get Precision
Returns the precision if it exists, false if it doesn't
string
serialize()
Serialize
Will be called, automatically, when serialize() is called on a BigInteger object.
phpseclib 1.0 serialized strings look like this: O:15:"Math_BigInteger":1:{s:3:"hex";s:18:"00ab54a98ceb1f0ad2";}
phpseclib 3.0 serialized strings look like this: C:25:"phpseclib\Math\BigInteger":42:{a:1:{s:3:"hex";s:18:"00ab54a98ceb1f0ad2";}}
unserialize(string $serialized)
Serialize
Will be called, automatically, when unserialize() is called on a BigInteger object.
BigInteger
powMod(BigInteger $e, BigInteger $n)
Performs modular exponentiation.
BigInteger
modPow(BigInteger $e, BigInteger $n)
Performs modular exponentiation.
int
compare(BigInteger $y)
Compares two numbers.
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.}
bool
equals(BigInteger $x)
Tests the equality of two numbers.
If you need to see if one number is greater than or less than another number, use BigInteger::compare()
BigInteger
bitwise_not()
Logical Not
BigInteger
bitwise_and(BigInteger $x)
Logical And
BigInteger
bitwise_or(BigInteger $x)
Logical Or
BigInteger
bitwise_xor(BigInteger $x)
Logical Exclusive Or
BigInteger
bitwise_rightShift(int $shift)
Logical Right Shift
Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
BigInteger
bitwise_leftShift(int $shift)
Logical Left Shift
Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
BigInteger
bitwise_leftRotate(int $shift)
Logical Left Rotate
Instead of the top x bits being dropped they're appended to the shifted bit string.
BigInteger
bitwise_rightRotate(int $shift)
Logical Right Rotate
Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
static BigInteger[]
minMaxBits(int $bits)
Returns the smallest and largest n-bit number
int
getLength()
Return the size of a BigInteger in bits
int
getLengthInBytes()
Return the size of a BigInteger in bytes
static BigInteger
random(int $size)
Generates a random number of a certain size
Bit length is equal to $size
static BigInteger
randomPrime(int $size)
Generates a random prime number of a certain size
Bit length is equal to $size
static false|BigInteger
randomRangePrime(BigInteger $min, BigInteger $max)
Generate a random prime number between a range
If there's not a prime within the given range, false will be returned.
static BigInteger
randomRange(BigInteger $min, BigInteger $max)
Generate a random number between a range
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)
bool
isPrime(int|bool $t = false)
Checks a numer to see if it's prime
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.
BigInteger
root(int $n = 2)
Calculates the nth root of a biginteger.
Returns the nth root of a positive biginteger, where n defaults to 2
BigInteger
pow(BigInteger $n)
Performs exponentiation.
static BigInteger
min(BigInteger ...$nums)
Return the minimum BigInteger between an arbitrary number of BigIntegers.
static BigInteger
max(BigInteger ...$nums)
Return the maximum BigInteger between an arbitrary number of BigIntegers.
bool
between(BigInteger $min, BigInteger $max)
Tests BigInteger to see if it is between two integers, inclusive
__clone()
Clone
bool
isOdd()
Is Odd?
bool
testBit(int $x)
Tests if a bit is set
bool
isNegative()
Is Negative?
BigInteger
negate()
Negate
Given $k, returns -$k
static int
scan1divide(BigInteger $r)
Scan for 1 and right shift by that amount
ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));
callable
createRecurringModuloFunction()
Create Recurring Modulo Function
Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation
BigInteger[]
bitwise_split(int $split)
Bitwise Split
Splits BigInteger's into chunks of $split bits