Engine
in package
implements
Serializable
Base Engine.
Tags
Table of Contents
Interfaces
- Serializable
Properties
- $bitmask : mixed
- Precision Bitmask
- $is_negative : bool
- Holds the BigInteger's sign
- $precision : mixed
- Precision
- $reduce : callable
- Recurring Modulo Function
- $value : mixed
- Holds the BigInteger's value
Methods
- __construct() : mixed
- Default constructor
- __debugInfo() : mixed
- __debugInfo() magic method
- __toString() : string
- Converts a BigInteger to a base-10 number.
- bitwise_leftRotate() : Engine
- Logical Left Rotate
- bitwise_not() : Engine|string
- Logical Not
- bitwise_rightRotate() : Engine
- Logical Right Rotate
- bitwise_split() : array<string|int, Engine>
- Bitwise Split
- createRecurringModuloFunction() : callable
- Create Recurring Modulo Function
- getLength() : int
- Return the size of a BigInteger in bits
- getLengthInBytes() : int
- Return the size of a BigInteger in bytes
- getPrecision() : int
- Get Precision
- isPrime() : bool
- Checks a numer to see if it's prime
- minMaxBits() : array<string|int, Engine>
- Returns the smallest and largest n-bit number
- random() : Engine
- Generates a random number of a certain size
- randomPrime() : Engine
- Generates a random prime number of a certain size
- root() : Engine
- Calculates the nth root of a biginteger.
- serialize() : string
- Serialize
- setModExpEngine() : mixed
- Sets engine type.
- setPrecision() : mixed
- Set Precision
- toBits() : string
- Converts a BigInteger to a bit string (eg. base-2).
- toHex() : string
- Converts a BigInteger to a hex string (eg. base-16).
- unserialize() : mixed
- Serialize
- base256_lshift() : string
- Logical Left Shift
- bitwiseAndHelper() : Engine
- Logical And
- bitwiseOrHelper() : Engine
- Logical Or
- bitwiseXorHelper() : Engine
- Logical Exclusive Or
- extendedGCDHelper() : Engine
- Calculates the greatest common divisor and Bezout's identity.
- maxHelper() : Engine
- Return the minimum BigInteger between an arbitrary number of BigIntegers.
- minHelper() : Engine
- Return the minimum BigInteger between an arbitrary number of BigIntegers.
- modInverseHelper() : Engine|false
- Calculates modular inverses.
- powModOuter() : bool|Engine
- Performs some pre-processing for powMod
- randomRangeHelper() : Engine
- Generate a random number between a range
- randomRangePrimeInner() : bool|Engine
- Performs some post-processing for randomRangePrime
- randomRangePrimeOuter() : bool|Engine
- Performs some pre-processing for randomRangePrime
- rootHelper() : Engine
- Performs a few preliminary checks on root
- rootInner() : Engine
- Calculates the nth root of a biginteger.
- setBitmask() : Engine
- Set Bitmask
- setupIsPrime() : int
- Sets the $t parameter for primality testing
- slidingWindow() : Engine
- Sliding Window k-ary Modular Exponentiation
- testPrimality() : bool
- Tests Primality
- toBytesHelper() : string
- Converts a BigInteger to a byte string (eg. base-256).
Properties
$bitmask
Precision Bitmask
protected
mixed
$bitmask
= false
Tags
$is_negative
Holds the BigInteger's sign
protected
bool
$is_negative
$precision
Precision
protected
mixed
$precision
= -1
Tags
$reduce
Recurring Modulo Function
protected
callable
$reduce
$value
Holds the BigInteger's value
protected
mixed
$value
Methods
__construct()
Default constructor
public
__construct(mixed $x, int $base) : mixed
Parameters
- $x : mixed
-
integer Base-10 number or base-$base number if $base set.
- $base : int
__debugInfo()
__debugInfo() magic method
public
__debugInfo() : mixed
Will be called, automatically, when print_r() or var_dump() are called
__toString()
Converts a BigInteger to a base-10 number.
public
__toString() : string
Return values
stringbitwise_leftRotate()
Logical Left Rotate
public
bitwise_leftRotate(int $shift) : Engine
Instead of the top x bits being dropped they're appended to the shifted bit string.
Parameters
- $shift : int
Return values
Enginebitwise_not()
Logical Not
public
bitwise_not() : Engine|string
Return values
Engine|stringbitwise_rightRotate()
Logical Right Rotate
public
bitwise_rightRotate(int $shift) : Engine
Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
Parameters
- $shift : int
Return values
Enginebitwise_split()
Bitwise Split
public
bitwise_split(int $split) : array<string|int, Engine>
Splits BigInteger's into chunks of $split bits
Parameters
- $split : int
Return values
array<string|int, Engine>createRecurringModuloFunction()
Create Recurring Modulo Function
public
createRecurringModuloFunction() : callable
Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation
Return values
callablegetLength()
Return the size of a BigInteger in bits
public
getLength() : int
Return values
intgetLengthInBytes()
Return the size of a BigInteger in bytes
public
getLengthInBytes() : int
Return values
intgetPrecision()
Get Precision
public
getPrecision() : int
Returns the precision if it exists, -1 if it doesn't
Return values
intisPrime()
Checks a numer to see if it's prime
public
isPrime([int|bool $t = false ]) : bool
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
- $t : int|bool = false
Return values
boolminMaxBits()
Returns the smallest and largest n-bit number
public
static minMaxBits(int $bits) : array<string|int, Engine>
Parameters
- $bits : int
Return values
array<string|int, Engine>random()
Generates a random number of a certain size
public
static random(int $size) : Engine
Bit length is equal to $size
Parameters
- $size : int
Return values
EnginerandomPrime()
Generates a random prime number of a certain size
public
static randomPrime(int $size) : Engine
Bit length is equal to $size
Parameters
- $size : int
Return values
Engineroot()
Calculates the nth root of a biginteger.
public
root([int $n = 2 ]) : Engine
Parameters
- $n : int = 2
Return values
Engineserialize()
Serialize
public
serialize() : string
Will be called, automatically, when serialize() is called on a BigInteger object.
Return values
stringsetModExpEngine()
Sets engine type.
public
static setModExpEngine(string $engine) : mixed
Throws an exception if the type is invalid
Parameters
- $engine : string
setPrecision()
Set Precision
public
setPrecision(int $bits) : mixed
Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.
Parameters
- $bits : int
toBits()
Converts a BigInteger to a bit string (eg. base-2).
public
toBits([bool $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.
Parameters
- $twos_compliment : bool = false
Return values
stringtoHex()
Converts a BigInteger to a hex string (eg. base-16).
public
toHex([bool $twos_compliment = false ]) : string
Parameters
- $twos_compliment : bool = false
Return values
stringunserialize()
Serialize
public
unserialize(string $serialized) : mixed
Will be called, automatically, when unserialize() is called on a BigInteger object.
Parameters
- $serialized : string
base256_lshift()
Logical Left Shift
protected
static base256_lshift(string &$x, int $shift) : string
Shifts binary strings $shift bits, essentially multiplying by 2**$shift.
Parameters
- $x : string
- $shift : int
Return values
stringbitwiseAndHelper()
Logical And
protected
bitwiseAndHelper(Engine $x) : Engine
Parameters
- $x : Engine
Return values
EnginebitwiseOrHelper()
Logical Or
protected
bitwiseOrHelper(Engine $x) : Engine
Parameters
- $x : Engine
Return values
EnginebitwiseXorHelper()
Logical Exclusive Or
protected
bitwiseXorHelper(Engine $x) : Engine
Parameters
- $x : Engine
Return values
EngineextendedGCDHelper()
Calculates the greatest common divisor and Bezout's identity.
protected
extendedGCDHelper(Engine $n[, Engine $stop = null ]) : Engine
Parameters
Return values
EnginemaxHelper()
Return the minimum BigInteger between an arbitrary number of BigIntegers.
protected
static maxHelper(array<string|int, mixed> $nums) : Engine
Parameters
- $nums : array<string|int, mixed>
Return values
EngineminHelper()
Return the minimum BigInteger between an arbitrary number of BigIntegers.
protected
static minHelper(array<string|int, mixed> $nums) : Engine
Parameters
- $nums : array<string|int, mixed>
Return values
EnginemodInverseHelper()
Calculates modular inverses.
protected
modInverseHelper(Engine $n) : Engine|false
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
Parameters
- $n : Engine
Return values
Engine|falsepowModOuter()
Performs some pre-processing for powMod
protected
powModOuter(Engine $e, Engine $n) : bool|Engine
Parameters
Return values
bool|EnginerandomRangeHelper()
Generate a random number between a range
protected
static randomRangeHelper(Engine $min, Engine $max) : Engine
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
Return values
EnginerandomRangePrimeInner()
Performs some post-processing for randomRangePrime
protected
static randomRangePrimeInner(Engine $x, Engine $min, Engine $max) : bool|Engine
Parameters
Return values
bool|EnginerandomRangePrimeOuter()
Performs some pre-processing for randomRangePrime
protected
static randomRangePrimeOuter(Engine $min, Engine $max) : bool|Engine
Parameters
Return values
bool|EnginerootHelper()
Performs a few preliminary checks on root
protected
rootHelper(int $n) : Engine
Parameters
- $n : int
Return values
EnginerootInner()
Calculates the nth root of a biginteger.
protected
rootInner(int $n) : Engine
Returns the nth root of a positive biginteger, where n defaults to 2
Parameters
- $n : int
Return values
EnginesetBitmask()
Set Bitmask
protected
static setBitmask(int $bits) : Engine
Parameters
- $bits : int
Tags
Return values
EnginesetupIsPrime()
Sets the $t parameter for primality testing
protected
setupIsPrime() : int
Return values
intslidingWindow()
Sliding Window k-ary Modular Exponentiation
protected
static slidingWindow(Engine $x, Engine $e, Engine $n, string $class) : Engine
Based on HAC 14.85 / 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
Return values
EnginetestPrimality()
Tests Primality
protected
testPrimality(int $t) : bool
Uses the Miller-Rabin primality test. See HAC 4.24 for more info.
Parameters
- $t : int
Return values
booltoBytesHelper()
Converts a BigInteger to a byte string (eg. base-256).
protected
toBytesHelper() : 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.