PHP32
class PHP32 extends PHP (View source)
Pure-PHP 32-bit Engine.
Uses 64-bit floats if int size is 4 bits
Constants
VALUE |
$result[self::VALUE] contains the value. |
SIGN |
$result[self::SIGN] contains the sign. |
KARATSUBA_CUTOFF |
Karatsuba Cutoff At what point do we switch between Karatsuba multiplication and schoolbook long multiplication? |
FAST_BITWISE |
Can Bitwise operations be done fast? |
ENGINE_DIR |
Engine Directory |
BASE |
|
BASE_FULL |
|
MAX_DIGIT |
|
MSB |
|
MAX10 |
MAX10 in greatest MAX10LEN satisfying MAX10 = 10MAX10LEN <= 2BASE. |
MAX10LEN |
MAX10LEN in greatest MAX10LEN satisfying MAX10 = 10MAX10LEN <= 2BASE. |
MAX_DIGIT2 |
|
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 array | $primes | Primes > 2 and < 1000 | |
static protected PHP32 | $zero | BigInteger(0) | |
static protected PHP32 | $one | BigInteger(1) | |
static protected PHP32 | $two | BigInteger(2) |
Methods
Converts a BigInteger to a hex string (eg. base-16).
Converts a BigInteger to a bit string (eg. base-2).
Sliding Window k-ary Modular Exponentiation
Performs some post-processing for randomRangePrime
Return the minimum BigInteger between an arbitrary number of BigIntegers.
Return the minimum BigInteger between an arbitrary number of BigIntegers.
Calculates the greatest common divisor and Bezout's identity.
Initialize a PHP32 BigInteger Engine instance
Converts a BigInteger to a byte string (eg. base-256).
Performs addition.
Performs subtraction.
Performs multiplication.
Performs long multiplication on two BigIntegers
No description
Performs Karatsuba "squaring" on two BigIntegers
Test for engine validity
Details
in
PHP at line 82
__construct(mixed $x = 0, int $base = 10)
Default constructor
static
setModExpEngine(string $engine)
Sets engine type.
Throws an exception if the type is invalid
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.
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).
Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.
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.}
string
serialize()
Serialize
Will be called, automatically, when serialize() is called on a BigInteger object.
unserialize(string $serialized)
Serialize
Will be called, automatically, when unserialize() is called on a BigInteger object.
string
__toString()
Converts a BigInteger to a base-10 number.
__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.
int
getPrecision()
Get Precision
Returns the precision if it exists, -1 if it doesn't
static protected Engine
setBitmask(int $bits)
Set Bitmask
Engine|string
bitwise_not()
Logical Not
static protected string
base256_lshift(string $x, int $shift)
Logical Left Shift
Shifts binary strings $shift bits, essentially multiplying by 2**$shift.
Engine
bitwise_leftRotate(int $shift)
Logical Left Rotate
Instead of the top x bits being dropped they're appended to the shifted bit string.
Engine
bitwise_rightRotate(int $shift)
Logical Right Rotate
Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
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
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.
static Engine
random(int $size)
Generates a random number of a certain size
Bit length is equal to $size
static Engine
randomPrime(int $size)
Generates a random prime number of a certain size
Bit length is equal to $size
static protected bool|Engine
randomRangePrimeOuter(Engine $min, Engine $max)
Performs some pre-processing for randomRangePrime
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)
static protected bool|Engine
randomRangePrimeInner(Engine $x, Engine $min, Engine $max)
Performs some post-processing for randomRangePrime
protected int
setupIsPrime()
Sets the $t parameter for primality testing
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.
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.
protected Engine
rootHelper(int $n)
Performs a few preliminary checks on root
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}.}
Engine
root(int $n = 2)
Calculates the nth root of a biginteger.
static protected Engine
minHelper(array $nums)
Return the minimum BigInteger between an arbitrary number of BigIntegers.
static protected Engine
maxHelper(array $nums)
Return the minimum BigInteger between an arbitrary number of BigIntegers.
callable
createRecurringModuloFunction()
Create Recurring Modulo Function
Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation
protected Engine
extendedGCDHelper(Engine $n, Engine $stop = null)
Calculates the greatest common divisor and Bezout's identity.
in
PHP at line 1209
Engine[]
bitwise_split(int $split)
Bitwise Split
Splits BigInteger's into chunks of $split bits
protected Engine
bitwiseAndHelper(Engine $x)
Logical And
protected Engine
bitwiseOrHelper(Engine $x)
Logical Or
protected Engine
bitwiseXorHelper(Engine $x)
Logical Exclusive Or
protected
initialize(int $base)
Initialize a PHP32 BigInteger Engine instance
in
PHP at line 139
protected string
pad(string $str)
Pads strings so that unpack may be used on them
in
PHP at line 153
string
toString()
Converts a BigInteger to a base-10 number.
in
PHP at line 188
string
toBytes(bool $twos_compliment = false)
Converts a BigInteger to a byte string (eg. base-256).
in
PHP at line 215
static protected array
addHelper(array $x_value, bool $x_negative, array $y_value, bool $y_negative)
Performs addition.
in
PHP at line 300
static array
subtractHelper(array $x_value, bool $x_negative, array $y_value, bool $y_negative)
Performs subtraction.
in
PHP at line 390
static protected array
multiplyHelper(array $x_value, bool $x_negative, array $y_value, bool $y_negative)
Performs multiplication.
in
PHP at line 467
static protected array
regularMultiply(array $x_value, array $y_value)
Performs long multiplication on two BigIntegers
Modeled after 'multiply' in MutableBigInteger.java.
in
PHP at line 522
protected array
divideHelper(PHP $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).
in
PHP at line 715
protected
convertToObj(array $arr)
in
PHP at line 732
protected PHP
normalize(PHP $result)
Normalize
Removes leading zeros and truncates (if necessary) to maintain the appropriate precision
in
PHP at line 770
static protected
compareHelper(array $x_value, $x_negative, array $y_value, $y_negative)
in
PHP at line 800
PHP
abs()
Absolute value.
in
PHP at line 816
static protected PHP
trim(array $value)
Trim
Removes leading zeros
in
PHP at line 836
PHP
bitwise_rightShift(int $shift)
Logical Right Shift
Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
in
PHP at line 856
PHP
bitwise_leftShift(int $shift)
Logical Left Shift
Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
in
PHP at line 885
static protected array
array_repeat(int $input, int $multiplier)
Array Repeat
in
PHP at line 897
protected
lshift(int $shift)
Logical Left Shift
Shifts BigInteger's by $shift bits.
in
PHP at line 931
protected
rshift(int $shift)
Logical Right Shift
Shifts BigInteger's by $shift bits.
in
PHP at line 964
protected PHP
powModInner(PHP $e, PHP $n)
Performs modular exponentiation.
in
PHP at line 980
static protected array
square(array $x)
Performs squaring
in
PHP at line 997
static protected array
baseSquare(array $value)
Performs traditional squaring on two BigIntegers
Squaring can be done faster than multiplying a number by itself can be. See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} / {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information.
in
PHP at line 1035
static protected array
karatsubaSquare(array $value)
Performs Karatsuba "squaring" on two BigIntegers
See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}.
in
PHP at line 1070
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.
in
PHP at line 1080
protected
testSmallPrimes()
Test the number against small primes.
in
PHP at line 1112
static int
scan1divide(PHP $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));
in
PHP at line 1134
protected PHP
powHelper(PHP $n)
Performs exponentiation.
in
PHP at line 1154
bool
isOdd()
Is Odd?
in
PHP at line 1164
bool
testBit($x)
Tests if a bit is set
in
PHP at line 1181
bool
isNegative()
Is Negative?
in
PHP at line 1193
BigInteger
negate()
Negate
Given $k, returns -$k
static bool
isValidEngine()
Test for engine validity
PHP32
add(PHP32 $y)
Adds two BigIntegers.
PHP32
subtract(PHP32 $y)
Subtracts two BigIntegers.
PHP32
multiply(PHP32 $y)
Multiplies two BigIntegers.
PHP32
divide(PHP32 $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).
false|PHP32
modInverse(PHP32 $n)
Calculates modular inverses.
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
PHP32[]
extendedGCD(PHP32 $n)
Calculates modular inverses.
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
PHP32
gcd(PHP32 $n)
Calculates the greatest common divisor
Say you have 693 and 609. The GCD is 21.
PHP32
bitwise_and(PHP32 $x)
Logical And
PHP32
bitwise_or(PHP32 $x)
Logical Or
PHP32
bitwise_xor(PHP32 $x)
Logical Exclusive Or
int
compare(PHP32 $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.}
bool
equals(PHP32 $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()
static false|PHP32
randomRangePrime(PHP32 $min, PHP32 $max)
Generate a random prime number between a range
If there's not a prime within the given range, false will be returned.
static PHP32
randomRange(PHP32 $min, PHP32 $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)
PHP32
pow(PHP32 $n)
Performs exponentiation.
static PHP32
min(PHP32 ...$nums)
Return the minimum BigInteger between an arbitrary number of BigIntegers.
static PHP32
max(PHP32 ...$nums)
Return the maximum BigInteger between an arbitrary number of BigIntegers.