BigInteger

Implements\JsonSerializable

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

Methods

Clone

__clone()

Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.

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

Arguments

$x

string|integer|\phpseclib3\Math\BigInteger\Engines\Engine

Base-10 number or base-$base number if $base set.

$base

integer

__debugInfo() magic method

__debugInfo()

Will be called, automatically, when print_r() or var_dump() are called

Serialize

__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

Response

array

__toString() magic method

__toString()

Serialize

__wakeup()

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

Absolute value.

abs(): \phpseclib3\Math\BigInteger

Adds two BigIntegers.

add(\phpseclib3\Math\BigInteger $y): \phpseclib3\Math\BigInteger

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

between(\phpseclib3\Math\BigInteger $min,\phpseclib3\Math\BigInteger $max): boolean

Arguments

Response

boolean

Logical And

bitwise_and(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger

Logical Left Rotate

bitwise_leftRotate(integer $shift): \phpseclib3\Math\BigInteger

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger

Logical Left Shift

bitwise_leftShift(integer $shift): \phpseclib3\Math\BigInteger

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger

Logical Not

bitwise_not(): \phpseclib3\Math\BigInteger

Logical Or

bitwise_or(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger

Logical Right Rotate

bitwise_rightRotate(integer $shift): \phpseclib3\Math\BigInteger

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger

Logical Right Shift

bitwise_rightShift(integer $shift): \phpseclib3\Math\BigInteger

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger

Bitwise Split

bitwise_split(integer $split): array<mixed,\phpseclib3\Math\BigInteger>

Splits BigInteger's into chunks of $split bits

Arguments

$split

integer

Response

array<mixed,\phpseclib3\Math\BigInteger>

Logical Exclusive Or

bitwise_xor(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger

Compares two numbers.

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

Arguments

Response

integer

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

Create Recurring Modulo Function

createRecurringModuloFunction(): callable

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

Response

callable

Divides two BigIntegers.

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

Arguments

Response

array<mixed,\phpseclib3\Math\BigInteger>

Tests the equality of two numbers.

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

Arguments

Response

boolean

Calculates modular inverses.

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.

Arguments

Response

array<mixed,\phpseclib3\Math\BigInteger>

Calculates the greatest common divisor

gcd(\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger

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

Arguments

Response

\phpseclib3\Math\BigInteger

Returns the engine type

getEngine(): array<mixed,string>
static

Response

array<mixed,string>

Return the size of a BigInteger in bits

getLength(): integer

Response

integer

Return the size of a BigInteger in bytes

getLengthInBytes(): integer

Response

integer

Get Precision

getPrecision(): integer|boolean

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

Response

integer|boolean

Initialize static variables

initialize_static_variables()
static

Is Negative?

isNegative(): boolean

Response

boolean

Is Odd?

isOdd(): boolean

Response

boolean

Checks a numer to see if it's prime

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.

Arguments

$t

integer|boolean

Response

boolean

jsonSerialize

jsonSerialize()

Return the maximum BigInteger between an arbitrary number of BigIntegers.

max(\phpseclib3\Math\BigInteger $nums): \phpseclib3\Math\BigInteger
static

Return the minimum BigInteger between an arbitrary number of BigIntegers.

min(\phpseclib3\Math\BigInteger $nums): \phpseclib3\Math\BigInteger
static

Returns the smallest and largest n-bit number

minMaxBits(integer $bits): array<mixed,\phpseclib3\Math\BigInteger>
static

Arguments

$bits

integer

Response

array<mixed,\phpseclib3\Math\BigInteger>

Calculates modular inverses.

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.

Arguments

Response

\phpseclib3\Math\BigInteger

Performs modular exponentiation.

modPow(\phpseclib3\Math\BigInteger $e,\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger

Multiplies two BigIntegers

multiply(\phpseclib3\Math\BigInteger $x): \phpseclib3\Math\BigInteger

Negate

negate(): \phpseclib3\Math\BigInteger

Given $k, returns -$k

Response

\phpseclib3\Math\BigInteger

Performs exponentiation.

pow(\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger

Performs modular exponentiation.

powMod(\phpseclib3\Math\BigInteger $e,\phpseclib3\Math\BigInteger $n): \phpseclib3\Math\BigInteger

Generates a random number of a certain size

random(integer $size): \phpseclib3\Math\BigInteger
static

Bit length is equal to $size

Arguments

$size

integer

Response

\phpseclib3\Math\BigInteger

Generates a random prime number of a certain size

randomPrime(integer $size): \phpseclib3\Math\BigInteger
static

Bit length is equal to $size

Arguments

$size

integer

Response

\phpseclib3\Math\BigInteger

Generate a random number between a range

randomRange(\phpseclib3\Math\BigInteger $min,\phpseclib3\Math\BigInteger $max): \phpseclib3\Math\BigInteger
static

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)

Arguments

Response

\phpseclib3\Math\BigInteger

Generate a random prime number between a range

randomRangePrime(\phpseclib3\Math\BigInteger $min,\phpseclib3\Math\BigInteger $max): false|\phpseclib3\Math\BigInteger
static

If there's not a prime within the given range, false will be returned.

Arguments

Response

false|\phpseclib3\Math\BigInteger

Calculates the nth root of a biginteger.

root(integer $n = 2): \phpseclib3\Math\BigInteger

Returns the nth root of a positive biginteger, where n defaults to 2

Arguments

$n

integer

optional

Response

\phpseclib3\Math\BigInteger

Scan for 1 and right shift by that amount

scan1divide(\phpseclib3\Math\BigInteger $r): integer
static

ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));

Arguments

Response

integer

Sets engine type.

setEngine(string $main,\phpseclib3\Math\list<string> $modexps = array('DefaultEngine')): void
static

Throws an exception if the type is invalid

Arguments

$main

string

$modexps

\phpseclib3\Math\list

optional

Set Precision

setPrecision(integer $bits)

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

Arguments

$bits

integer

Subtracts two BigIntegers.

subtract(\phpseclib3\Math\BigInteger $y): \phpseclib3\Math\BigInteger

Tests if a bit is set

testBit(integer $x): boolean

Arguments

$x

integer

Response

boolean

Converts a BigInteger to a bit string (eg. base-2).

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.

Arguments

$twos_compliment

boolean

Response

string

Converts a BigInteger to a byte string (eg. base-256).

toBytes(boolean $twos_compliment = false): string

Arguments

$twos_compliment

boolean

Response

string

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

toHex(boolean $twos_compliment = false): string

Arguments

$twos_compliment

boolean

Response

string

Converts a BigInteger to a base-10 number.

toString(): string

Response

string

Properties

Main Engine

mainEngine :\phpseclib3\Math\class-string<Engine>
static
var

Type(s)

\phpseclib3\Math\class-string

Selected Engines

engines :\phpseclib3\Math\list<string>
static
var

Type(s)

\phpseclib3\Math\list

The actual BigInteger object

value :object
var

Type(s)

object

Mode independent value used for serialization.

hex :string
see
var

Type(s)

string

Precision (used only for serialization)

precision :integer
see
var

Type(s)

integer