class GMP extends Engine (View source)

GMP Engine.

Constants

FAST_BITWISE

Can Bitwise operations be done fast?

ENGINE_DIR

Engine Directory

Properties

protected mixed $value Holds the BigInteger's value from  Engine
protected bool $is_negative Holds the BigInteger's sign from  Engine
protected $precision Precision from  Engine
protected $bitmask Precision Bitmask from  Engine
protected callable $reduce Recurring Modulo Function from  Engine
static protected string $modexpEngine Modular Exponentiation Engine
static protected bool $isValidEngine Engine Validity Flag
static protected GMP $zero BigInteger(0)
static protected GMP $one BigInteger(1)
static protected GMP $two BigInteger(2)
static protected mixed $primes Primes > 2 and < 1000

Methods

__construct(mixed $x = 0, int $base = 10)

Default constructor

static 
setModExpEngine(string $engine)

Sets engine type.

from  Engine
string
toBytesHelper()

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

from  Engine
string
toHex(bool $twos_compliment = false)

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

from  Engine
string
toBits(bool $twos_compliment = false)

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

Engine|false
modInverseHelper(Engine $n)

Calculates modular inverses.

from  Engine
string
serialize()

Serialize

from  Engine
unserialize(string $serialized)

Serialize

from  Engine
string
__toString()

Converts a BigInteger to a base-10 number.

from  Engine
__debugInfo()

__debugInfo() magic method

from  Engine
setPrecision(int $bits)

Set Precision

from  Engine
int
getPrecision()

Get Precision

from  Engine
static Engine
setBitmask(int $bits)

Set Bitmask

from  Engine
Engine|string
bitwise_not()

Logical Not

from  Engine
static string
base256_lshift(string $x, int $shift)

Logical Left Shift

from  Engine
bitwise_leftRotate(int $shift)

Logical Left Rotate

from  Engine
bitwise_rightRotate(int $shift)

Logical Right Rotate

from  Engine
static Engine[]
minMaxBits(int $bits)

Returns the smallest and largest n-bit number

from  Engine
int
getLength()

Return the size of a BigInteger in bits

from  Engine
int
getLengthInBytes()

Return the size of a BigInteger in bytes

from  Engine
bool|Engine
powModOuter(Engine $e, Engine $n)

Performs some pre-processing for powMod

from  Engine
static Engine
slidingWindow(Engine $x, Engine $e, Engine $n, string $class)

Sliding Window k-ary Modular Exponentiation

from  Engine
static Engine
random(int $size)

Generates a random number of a certain size

from  Engine
static Engine
randomPrime(int $size)

Generates a random prime number of a certain size

from  Engine
static bool|Engine
randomRangePrimeOuter(Engine $min, Engine $max)

Performs some pre-processing for randomRangePrime

from  Engine
static Engine
randomRangeHelper(Engine $min, Engine $max)

Generate a random number between a range

from  Engine
static bool|Engine
randomRangePrimeInner(Engine $x, Engine $min, Engine $max)

Performs some post-processing for randomRangePrime

int
setupIsPrime()

Sets the $t parameter for primality testing

from  Engine
bool
testPrimality(int $t)

Tests Primality

bool
isPrime(int|bool $t = false)

Checks a numer to see if it's prime

from  Engine
rootHelper(int $n)

Performs a few preliminary checks on root

from  Engine
rootInner(int $n)

Calculates the nth root of a biginteger.

Engine
root(int $n = 2)

Calculates the nth root of a biginteger.

from  Engine
static Engine
minHelper(array $nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

from  Engine
static Engine
maxHelper(array $nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

from  Engine
callable
createRecurringModuloFunction()

Create Recurring Modulo Function

Engine
extendedGCDHelper(Engine $n, Engine $stop = null)

Calculates the greatest common divisor and Bezout's identity.

from  Engine
Engine[]
bitwise_split(int $split)

Bitwise Split

from  Engine
Engine
bitwiseAndHelper(Engine $x)

Logical And

from  Engine
Engine
bitwiseOrHelper(Engine $x)

Logical Or

from  Engine
Engine
bitwiseXorHelper(Engine $x)

Logical Exclusive Or

from  Engine
static bool
isValidEngine()

Test for engine validity

initialize(int $base)

Initialize a GMP BigInteger Engine instance

string
toString()

Converts a BigInteger to a base-10 number.

string
toBytes(bool $twos_compliment = false)

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

GMP
add(GMP $y)

Adds two BigIntegers.

GMP
subtract(GMP $y)

Subtracts two BigIntegers.

GMP
multiply(GMP $x)

Multiplies two BigIntegers.

GMP
divide(GMP $y)

Divides two BigIntegers.

int
compare(GMP $y)

Compares two numbers.

bool
equals(GMP $x)

Tests the equality of two numbers.

false|GMP
modInverse(GMP $n)

Calculates modular inverses.

GMP[]
extendedGCD(GMP $n)

Calculates the greatest common divisor and Bezout's identity.

GMP
gcd(GMP $n)

Calculates the greatest common divisor

GMP
abs()

Absolute value.

GMP
bitwise_and(GMP $x)

Logical And

GMP
bitwise_or(GMP $x)

Logical Or

GMP
bitwise_xor(GMP $x)

Logical Exclusive Or

GMP
bitwise_rightShift(int $shift)

Logical Right Shift

GMP
bitwise_leftShift(int $shift)

Logical Left Shift

GMP
modPow(GMP $e, GMP $n)

Performs modular exponentiation.

GMP
powMod(GMP $e, GMP $n)

Performs modular exponentiation.

GMP
powModInner(GMP $e, GMP $n)

Performs modular exponentiation.

GMP
normalize(GMP $result)

Normalize

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

Generate a random prime number between a range

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

Generate a random number between a range

make_odd()

Make the current number odd

GMP
pow(GMP $n)

Performs exponentiation.

static GMP
min(GMP ...$nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

static GMP
max(GMP ...$nums)

Return the maximum BigInteger between an arbitrary number of BigIntegers.

bool
between(GMP $min, GMP $max)

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

static int
scan1divide(GMP $r)

Scan for 1 and right shift by that amount

bool
isOdd()

Is Odd?

bool
testBit($x)

Tests if a bit is set

bool
isNegative()

Is Negative?

GMP
negate()

Negate

Details

__construct(mixed $x = 0, int $base = 10)

Default constructor

Parameters

mixed $x integer Base-10 number or base-$base number if $base set.
int $base

See also

\parent::__construct()

static setModExpEngine(string $engine)

Sets engine type.

Throws an exception if the type is invalid

Parameters

string $engine

protected string toBytesHelper()

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

Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's 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

protected Engine|false modInverseHelper(Engine $n)

Calculates modular inverses.

Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.

{@internal See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=21 HAC 14.64} for more information.}

Parameters

Engine $n

Return Value

Engine|false

string serialize()

Serialize

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

Return Value

string

unserialize(string $serialized)

Serialize

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

Parameters

string $serialized

string __toString()

Converts a BigInteger to a base-10 number.

Return Value

string

__debugInfo()

__debugInfo() magic method

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

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

Get Precision

Returns the precision if it exists, -1 if it doesn't

Return Value

int

static protected Engine setBitmask(int $bits)

Set Bitmask

Parameters

int $bits

Return Value

Engine

See also

\self::setPrecision()

Engine|string bitwise_not()

Logical Not

Return Value

Engine|string

static protected string base256_lshift(string $x, int $shift)

Logical Left Shift

Shifts binary strings $shift bits, essentially multiplying by 2**$shift.

Parameters

string $x
int $shift

Return Value

string

Engine 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

Engine

Engine 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

Engine

static Engine[] minMaxBits(int $bits)

Returns the smallest and largest n-bit number

Parameters

int $bits

Return Value

Engine[]

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

protected bool|Engine powModOuter(Engine $e, Engine $n)

Performs some pre-processing for powMod

Parameters

Engine $e
Engine $n

Return Value

bool|Engine

static protected Engine slidingWindow(Engine $x, Engine $e, Engine $n, string $class)

Sliding Window k-ary Modular Exponentiation

Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} / {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 MPM 7.7}. In a departure from those algorithims, however, this function performs a modular reduction after every multiplication and squaring operation. As such, this function has the same preconditions that the reductions being used do.

Parameters

Engine $x
Engine $e
Engine $n
string $class

Return Value

Engine

static Engine random(int $size)

Generates a random number of a certain size

Bit length is equal to $size

Parameters

int $size

Return Value

Engine

static Engine randomPrime(int $size)

Generates a random prime number of a certain size

Bit length is equal to $size

Parameters

int $size

Return Value

Engine

static protected bool|Engine randomRangePrimeOuter(Engine $min, Engine $max)

Performs some pre-processing for randomRangePrime

Parameters

Engine $min
Engine $max

Return Value

bool|Engine

static protected Engine randomRangeHelper(Engine $min, Engine $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

Engine $min
Engine $max

Return Value

Engine

static protected bool|Engine randomRangePrimeInner(Engine $x, Engine $min, Engine $max)

Performs some post-processing for randomRangePrime

Parameters

Engine $x
Engine $min
Engine $max

Return Value

bool|Engine

protected int setupIsPrime()

Sets the $t parameter for primality testing

Return Value

int

protected bool testPrimality(int $t)

Tests Primality

Parameters

int $t

Return Value

bool

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

protected Engine rootHelper(int $n)

Performs a few preliminary checks on root

Parameters

int $n

Return Value

Engine

protected Engine rootInner(int $n)

Calculates the nth root of a biginteger.

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

Parameters

int $n

Return Value

Engine

Engine root(int $n = 2)

Calculates the nth root of a biginteger.

Parameters

int $n

Return Value

Engine

static protected Engine minHelper(array $nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

Parameters

array $nums

Return Value

Engine

static protected Engine maxHelper(array $nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

Parameters

array $nums

Return Value

Engine

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

protected Engine extendedGCDHelper(Engine $n, Engine $stop = null)

Calculates the greatest common divisor and Bezout's identity.

Parameters

Engine $n
Engine $stop (optional)

Return Value

Engine

Engine[] bitwise_split(int $split)

Bitwise Split

Splits BigInteger's into chunks of $split bits

Parameters

int $split

Return Value

Engine[]

protected Engine bitwiseAndHelper(Engine $x)

Logical And

Parameters

Engine $x

Return Value

Engine

protected Engine bitwiseOrHelper(Engine $x)

Logical Or

Parameters

Engine $x

Return Value

Engine

protected Engine bitwiseXorHelper(Engine $x)

Logical Exclusive Or

Parameters

Engine $x

Return Value

Engine

static bool isValidEngine()

Test for engine validity

Return Value

bool

See also

\parent::__construct()

protected initialize(int $base)

Initialize a GMP BigInteger Engine instance

Parameters

int $base

See also

\parent::__construct()

string toString()

Converts a BigInteger to a base-10 number.

Return Value

string

string toBytes(bool $twos_compliment = false)

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

Parameters

bool $twos_compliment

Return Value

string

GMP add(GMP $y)

Adds two BigIntegers.

Parameters

GMP $y

Return Value

GMP

GMP subtract(GMP $y)

Subtracts two BigIntegers.

Parameters

GMP $y

Return Value

GMP

GMP multiply(GMP $x)

Multiplies two BigIntegers.

Parameters

GMP $x

Return Value

GMP

GMP divide(GMP $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).

Parameters

GMP $y

Return Value

GMP

int compare(GMP $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

GMP $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(GMP $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

GMP $x

Return Value

bool

false|GMP modInverse(GMP $n)

Calculates modular inverses.

Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.

Parameters

GMP $n

Return Value

false|GMP

GMP[] extendedGCD(GMP $n)

Calculates the greatest common divisor and Bezout's identity.

Say you have 693 and 609. The GCD is 21. Bezout's identity states that there exist integers x and y such that 693x + 609y == 21. In point of fact, there are actually an infinite number of x and y combinations and which combination is returned is dependent upon which mode is in use. See {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information.

Parameters

GMP $n

Return Value

GMP[]

GMP gcd(GMP $n)

Calculates the greatest common divisor

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

Parameters

GMP $n

Return Value

GMP

GMP abs()

Absolute value.

Return Value

GMP

GMP bitwise_and(GMP $x)

Logical And

Parameters

GMP $x

Return Value

GMP

GMP bitwise_or(GMP $x)

Logical Or

Parameters

GMP $x

Return Value

GMP

GMP bitwise_xor(GMP $x)

Logical Exclusive Or

Parameters

GMP $x

Return Value

GMP

GMP bitwise_rightShift(int $shift)

Logical Right Shift

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

Parameters

int $shift

Return Value

GMP

GMP bitwise_leftShift(int $shift)

Logical Left Shift

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

Parameters

int $shift

Return Value

GMP

GMP modPow(GMP $e, GMP $n)

Performs modular exponentiation.

Parameters

GMP $e
GMP $n

Return Value

GMP

GMP powMod(GMP $e, GMP $n)

Performs modular exponentiation.

Alias for modPow().

Parameters

GMP $e
GMP $n

Return Value

GMP

protected GMP powModInner(GMP $e, GMP $n)

Performs modular exponentiation.

Parameters

GMP $e
GMP $n

Return Value

GMP

protected GMP normalize(GMP $result)

Normalize

Removes leading zeros and truncates (if necessary) to maintain the appropriate precision

Parameters

GMP $result

Return Value

GMP

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

Generate a random prime number between a range

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

Parameters

GMP $min
GMP $max

Return Value

false|GMP

static GMP randomRange(GMP $min, GMP $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

GMP $min
GMP $max

Return Value

GMP

protected make_odd()

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 also

\self::randomPrime()

GMP pow(GMP $n)

Performs exponentiation.

Parameters

GMP $n

Return Value

GMP

static GMP min(GMP ...$nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

Parameters

GMP ...$nums

Return Value

GMP

static GMP max(GMP ...$nums)

Return the maximum BigInteger between an arbitrary number of BigIntegers.

Parameters

GMP ...$nums

Return Value

GMP

bool between(GMP $min, GMP $max)

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

Parameters

GMP $min
GMP $max

Return Value

bool

static int scan1divide(GMP $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

GMP $r

Return Value

int

bool isOdd()

Is Odd?

Return Value

bool

bool testBit($x)

Tests if a bit is set

Parameters

$x

Return Value

bool

bool isNegative()

Is Negative?

Return Value

bool

GMP negate()

Negate

Given $k, returns -$k

Return Value

GMP