abstract class Engine implements Serializable (View source)

Base Engine.

Properties

protected mixed $value Holds the BigInteger's value
protected bool $is_negative Holds the BigInteger's sign
protected $precision Precision
protected $bitmask Precision Bitmask
protected callable $reduce Recurring Modulo Function

Methods

__construct(mixed $x, int $base)

Default constructor

static 
setModExpEngine(string $engine)

Sets engine type.

string
toBytesHelper()

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

Engine|false
modInverseHelper(Engine $n)

Calculates modular inverses.

string
serialize()

Serialize

unserialize(string $serialized)

Serialize

string
__toString()

Converts a BigInteger to a base-10 number.

__debugInfo()

__debugInfo() magic method

setPrecision(int $bits)

Set Precision

int
getPrecision()

Get Precision

static Engine
setBitmask(int $bits)

Set Bitmask

Engine|string
bitwise_not()

Logical Not

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

Logical Left Shift

bitwise_leftRotate(int $shift)

Logical Left Rotate

bitwise_rightRotate(int $shift)

Logical Right Rotate

static Engine[]
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

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

Performs some pre-processing for powMod

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

Sliding Window k-ary Modular Exponentiation

static Engine
random(int $size)

Generates a random number of a certain size

static Engine
randomPrime(int $size)

Generates a random prime number of a certain size

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

Performs some pre-processing for randomRangePrime

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

Generate a random number between a range

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

bool
testPrimality(int $t)

Tests Primality

bool
isPrime(int|bool $t = false)

Checks a numer to see if it's prime

rootHelper(int $n)

Performs a few preliminary checks on root

rootInner(int $n)

Calculates the nth root of a biginteger.

Engine
root(int $n = 2)

Calculates the nth root of a biginteger.

static Engine
minHelper(array $nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

static Engine
maxHelper(array $nums)

Return the minimum BigInteger between an arbitrary number of BigIntegers.

callable
createRecurringModuloFunction()

Create Recurring Modulo Function

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

Calculates the greatest common divisor and Bezout's identity.

Engine[]
bitwise_split(int $split)

Bitwise Split

Engine
bitwiseAndHelper(Engine $x)

Logical And

Engine
bitwiseOrHelper(Engine $x)

Logical Or

Engine
bitwiseXorHelper(Engine $x)

Logical Exclusive Or

Details

__construct(mixed $x, int $base)

Default constructor

Parameters

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

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

Uses the {@link http://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test Miller-Rabin primality test}. See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap4.pdf#page=8 HAC 4.24} for more info.

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

{@internal This function is based off of {@link http://mathforum.org/library/drmath/view/52605.html this page} and {@link http://stackoverflow.com/questions/11242920/calculating-nth-root-with-bcmath-in-php this stackoverflow question}.}

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