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

static 
setEngine(string $main, array $modexps = ['DefaultEngine'])

Sets engine type.

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.

string
toString()

Converts a BigInteger to a base-10 number.

__toString()

__toString() magic method

__debugInfo()

__debugInfo() magic method

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).

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.

BigInteger
modInverse(BigInteger $n)

Calculates modular inverses.

BigInteger[]
extendedGCD(BigInteger $n)

Calculates modular inverses.

BigInteger
gcd(BigInteger $n)

Calculates the greatest common divisor

BigInteger
abs()

Absolute value.

setPrecision(int $bits)

Set Precision

int|bool
getPrecision()

Get Precision

string
serialize()

Serialize

unserialize(string $serialized)

Serialize

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.

bool
equals(BigInteger $x)

Tests the equality of two numbers.

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

BigInteger
bitwise_leftShift(int $shift)

Logical Left Shift

BigInteger
bitwise_leftRotate(int $shift)

Logical Left Rotate

BigInteger
bitwise_rightRotate(int $shift)

Logical Right Rotate

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

static BigInteger
randomPrime(int $size)

Generates a random prime number of a certain size

static false|BigInteger
randomRangePrime(BigInteger $min, BigInteger $max)

Generate a random prime number between a range

static BigInteger
randomRange(BigInteger $min, BigInteger $max)

Generate a random number between a range

bool
isPrime(int|bool $t = false)

Checks a numer to see if it's prime

BigInteger
root(int $n = 2)

Calculates the nth root of a biginteger.

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

static int
scan1divide(BigInteger $r)

Scan for 1 and right shift by that amount

callable
createRecurringModuloFunction()

Create Recurring Modulo Function

bitwise_split(int $split)

Bitwise Split

Details

static setEngine(string $main, array $modexps = ['DefaultEngine'])

Sets engine type.

Throws an exception if the type is invalid

Parameters

string $main
array $modexps optional

static string[] getEngine()

Returns the engine type

Return Value

string[]

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.

Parameters

string|int|Engine $x Base-10 number or base-$base number if $base set.
int $base

Return Value

BigInteger

string toString()

Converts a BigInteger to a base-10 number.

Return Value

string

__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).

Parameters

bool $twos_compliment

Return Value

string

string toHex(bool $twos_compliment = false)

Converts a BigInteger to a hex string (eg. base-16).

Parameters

bool $twos_compliment

Return Value

string

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.

Parameters

bool $twos_compliment

Return Value

string

BigInteger add(BigInteger $y)

Adds two BigIntegers.

Parameters

BigInteger $y

Return Value

BigInteger

BigInteger subtract(BigInteger $y)

Subtracts two BigIntegers.

Parameters

BigInteger $y

Return Value

BigInteger

BigInteger multiply(BigInteger $x)

Multiplies two BigIntegers

Parameters

BigInteger $x

Return Value

BigInteger

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 ?>

Parameters

BigInteger $y

Return Value

BigInteger[]

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.

Parameters

BigInteger $n

Return Value

BigInteger

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.

Parameters

BigInteger $n

Return Value

BigInteger[]

BigInteger gcd(BigInteger $n)

Calculates the greatest common divisor

Say you have 693 and 609. The GCD is 21.

Parameters

BigInteger $n

Return Value

BigInteger

BigInteger abs()

Absolute value.

Return Value

BigInteger

setPrecision(int $bits)

Set Precision

Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.

Parameters

int $bits

int|bool getPrecision()

Get Precision

Returns the precision if it exists, false if it doesn't

Return Value

int|bool

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";}}

Return Value

string

unserialize(string $serialized)

Serialize

Will be called, automatically, when unserialize() is called on a BigInteger object.

Parameters

string $serialized

BigInteger powMod(BigInteger $e, BigInteger $n)

Performs modular exponentiation.

Parameters

BigInteger $e
BigInteger $n

Return Value

BigInteger

BigInteger modPow(BigInteger $e, BigInteger $n)

Performs modular exponentiation.

Parameters

BigInteger $e
BigInteger $n

Return Value

BigInteger

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.}

Parameters

BigInteger $y

Return Value

int in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.

See also

\self::equals()

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()

Parameters

BigInteger $x

Return Value

bool

BigInteger bitwise_not()

Logical Not

Return Value

BigInteger

BigInteger bitwise_and(BigInteger $x)

Logical And

Parameters

BigInteger $x

Return Value

BigInteger

BigInteger bitwise_or(BigInteger $x)

Logical Or

Parameters

BigInteger $x

Return Value

BigInteger

BigInteger bitwise_xor(BigInteger $x)

Logical Exclusive Or

Parameters

BigInteger $x

Return Value

BigInteger

BigInteger bitwise_rightShift(int $shift)

Logical Right Shift

Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.

Parameters

int $shift

Return Value

BigInteger

BigInteger bitwise_leftShift(int $shift)

Logical Left Shift

Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.

Parameters

int $shift

Return Value

BigInteger

BigInteger bitwise_leftRotate(int $shift)

Logical Left Rotate

Instead of the top x bits being dropped they're appended to the shifted bit string.

Parameters

int $shift

Return Value

BigInteger

BigInteger bitwise_rightRotate(int $shift)

Logical Right Rotate

Instead of the bottom x bits being dropped they're prepended to the shifted bit string.

Parameters

int $shift

Return Value

BigInteger

static BigInteger[] minMaxBits(int $bits)

Returns the smallest and largest n-bit number

Parameters

int $bits

Return Value

BigInteger[]

int getLength()

Return the size of a BigInteger in bits

Return Value

int

int getLengthInBytes()

Return the size of a BigInteger in bytes

Return Value

int

static BigInteger random(int $size)

Generates a random number of a certain size

Bit length is equal to $size

Parameters

int $size

Return Value

BigInteger

static BigInteger randomPrime(int $size)

Generates a random prime number of a certain size

Bit length is equal to $size

Parameters

int $size

Return Value

BigInteger

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.

Parameters

BigInteger $min
BigInteger $max

Return Value

false|BigInteger

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)

Parameters

BigInteger $min
BigInteger $max

Return Value

BigInteger

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.

Parameters

int|bool $t

Return Value

bool

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

Parameters

int $n optional

Return Value

BigInteger

BigInteger pow(BigInteger $n)

Performs exponentiation.

Parameters

BigInteger $n

Return Value

BigInteger

static BigInteger min(BigInteger ...$nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

Parameters

BigInteger ...$nums

Return Value

BigInteger

static BigInteger max(BigInteger ...$nums)

Return the maximum BigInteger between an arbitrary number of BigIntegers.

Parameters

BigInteger ...$nums

Return Value

BigInteger

bool between(BigInteger $min, BigInteger $max)

Tests BigInteger to see if it is between two integers, inclusive

Parameters

BigInteger $min
BigInteger $max

Return Value

bool

__clone()

Clone

bool isOdd()

Is Odd?

Return Value

bool

bool testBit(int $x)

Tests if a bit is set

Parameters

int $x

Return Value

bool

bool isNegative()

Is Negative?

Return Value

bool

BigInteger negate()

Negate

Given $k, returns -$k

Return Value

BigInteger

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));

Parameters

BigInteger $r

Return Value

int

callable createRecurringModuloFunction()

Create Recurring Modulo Function

Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation

Return Value

callable

BigInteger[] bitwise_split(int $split)

Bitwise Split

Splits BigInteger's into chunks of $split bits

Parameters

int $split

Return Value

BigInteger[]