PHP Default Modular Exponentiation Engine
author | Jim Wigginton terrafrost@php.net |
---|---|
package | Default |
__construct(integer|\phpseclib3\Math\BigInteger\Engines\numeric-string $x,integer $base = 10)
integer|\phpseclib3\Math\BigInteger\Engines\numeric-string
integer Base-10 number or base-$base number if $base set.
integer
__debugInfo(): array
Will be called, automatically, when print_r() or var_dump() are called
array
__sleep(): array
Will be called, automatically, when serialize() is called on a BigInteger object.
array
__toString(): string
string
__wakeup(): void
Will be called, automatically, when unserialize() is called on a BigInteger object.
abs(): \phpseclib3\Math\BigInteger\Engines\BCMath
add(\phpseclib3\Math\BigInteger\Engines\BCMath $y): \phpseclib3\Math\BigInteger\Engines\BCMath
base256_lshift(string &$x,integer $shift): void
Shifts binary strings $shift bits, essentially multiplying by 2**$shift.
string
integer
between(\phpseclib3\Math\BigInteger\Engines\BCMath $min,\phpseclib3\Math\BigInteger\Engines\BCMath $max): boolean
boolean
bitwise_and(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
bitwise_leftRotate(integer $shift): \phpseclib3\Math\BigInteger\Engines\Engine
Instead of the top x bits being dropped they're appended to the shifted bit string.
integer
\phpseclib3\Math\BigInteger\Engines\Engine
bitwise_leftShift(integer $shift): \phpseclib3\Math\BigInteger\Engines\BCMath
Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
integer
\phpseclib3\Math\BigInteger\Engines\BCMath
bitwise_not(): \phpseclib3\Math\BigInteger\Engines\Engine|string
bitwise_or(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
bitwise_rightRotate(integer $shift): \phpseclib3\Math\BigInteger\Engines\Engine
Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
integer
\phpseclib3\Math\BigInteger\Engines\Engine
bitwise_rightShift(integer $shift): \phpseclib3\Math\BigInteger\Engines\BCMath
Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
integer
\phpseclib3\Math\BigInteger\Engines\BCMath
bitwise_split(integer $split): array<mixed,\phpseclib3\Math\BigInteger\Engines\Engine>
Splits BigInteger's into chunks of $split bits
integer
array<mixed,\phpseclib3\Math\BigInteger\Engines\Engine>
bitwise_xor(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
bitwiseAndHelper(\phpseclib3\Math\BigInteger\Engines\Engine $x): \phpseclib3\Math\BigInteger\Engines\Engine
bitwiseOrHelper(\phpseclib3\Math\BigInteger\Engines\Engine $x): \phpseclib3\Math\BigInteger\Engines\Engine
bitwiseXorHelper(\phpseclib3\Math\BigInteger\Engines\Engine $x): \phpseclib3\Math\BigInteger\Engines\Engine
compare(\phpseclib3\Math\BigInteger\Engines\BCMath $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\Engines\BCMath::equals() |
---|
integer
in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
createRecurringModuloFunction(): callable
Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation
callable
divide(\phpseclib3\Math\BigInteger\Engines\BCMath $y): \phpseclib3\Math\BigInteger\Engines\array{static,
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).
\phpseclib3\Math\BigInteger\Engines\array{static,
static}
equals(\phpseclib3\Math\BigInteger\Engines\BCMath $x): boolean
If you need to see if one number is greater than or less than another number, use BigInteger::compare()
boolean
extendedGCD(\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\array{gcd:
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 Bezout's identity - Wikipedia for more information.
\phpseclib3\Math\BigInteger\Engines\array{gcd:
static, x: static, y: static}
extendedGCDHelper(\phpseclib3\Math\BigInteger\Engines\Engine $n): \phpseclib3\Math\BigInteger\Engines\array{gcd:
\phpseclib3\Math\BigInteger\Engines\array{gcd:
Engine, x: Engine, y: Engine}
gcd(\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
Say you have 693 and 609. The GCD is 21.
\phpseclib3\Math\BigInteger\Engines\BCMath
getLength(): integer
integer
getLengthInBytes(): integer
integer
getPrecision(): integer
Returns the precision if it exists, -1 if it doesn't
integer
initialize(integer $base)
see | \phpseclib3\Math\BigInteger\Engines\parent::__construct() |
---|---|
integer
isNegative(): boolean
boolean
isOdd(): boolean
boolean
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.
integer|boolean
boolean
isValidEngine(): boolean
see | \phpseclib3\Math\BigInteger\Engines\parent::__construct() |
---|---|
boolean
jsonSerialize()
make_odd()
If the current number is odd it'll be unchanged. If it's even, one will be added to it.
see | self::randomPrime() |
---|---|
max(\phpseclib3\Math\BigInteger\Engines\BCMath $nums): \phpseclib3\Math\BigInteger\Engines\BCMath
maxHelper(array $nums): \phpseclib3\Math\BigInteger\Engines\Engine
min(\phpseclib3\Math\BigInteger\Engines\BCMath $nums): \phpseclib3\Math\BigInteger\Engines\BCMath
minHelper(array $nums): \phpseclib3\Math\BigInteger\Engines\Engine
minMaxBits(integer $bits): \phpseclib3\Math\BigInteger\Engines\array{min:
integer
\phpseclib3\Math\BigInteger\Engines\array{min:
static, max: static}
modInverse(\phpseclib3\Math\BigInteger\Engines\BCMath $n): false|\phpseclib3\Math\BigInteger\Engines\BCMath
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
false|\phpseclib3\Math\BigInteger\Engines\BCMath
modInverseHelper(\phpseclib3\Math\BigInteger\Engines\Engine $n): static|false
Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.
{@internal See HAC 14.64 for more information.}
static|false
modPow(\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
multiply(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
multiplyReduce(string $x,string $y,string $n,string $class): string
see | self::slidingWindow() |
---|
string
string
string
string
string
negate(): \phpseclib3\Math\BigInteger\Engines\BCMath
normalize(\phpseclib3\Math\BigInteger\Engines\BCMath $result): \phpseclib3\Math\BigInteger\Engines\BCMath
Removes leading zeros and truncates (if necessary) to maintain the appropriate precision
\phpseclib3\Math\BigInteger\Engines\BCMath
pow(\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
powMod(\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
powModHelper(\phpseclib3\Math\BigInteger\Engines\BCMath $x,\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n,string $class): \phpseclib3\Math\BigInteger\Engines\BCMath
powModInner(\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
powModOuter(\phpseclib3\Math\BigInteger\Engines\Engine $e,\phpseclib3\Math\BigInteger\Engines\Engine $n): static|false
static|false
prepareReduce(string $x,string $n,string $class): string
see | self::slidingWindow() |
---|
string
string
string
string
random(integer $size): \phpseclib3\Math\BigInteger\Engines\Engine
Bit length is equal to $size
integer
\phpseclib3\Math\BigInteger\Engines\Engine
randomPrime(integer $size): \phpseclib3\Math\BigInteger\Engines\Engine
Bit length is equal to $size
integer
\phpseclib3\Math\BigInteger\Engines\Engine
randomRange(\phpseclib3\Math\BigInteger\Engines\BCMath $min,\phpseclib3\Math\BigInteger\Engines\BCMath $max): \phpseclib3\Math\BigInteger\Engines\BCMath
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)
\phpseclib3\Math\BigInteger\Engines\BCMath
randomRangeHelper(\phpseclib3\Math\BigInteger\Engines\Engine $min,\phpseclib3\Math\BigInteger\Engines\Engine $max): \phpseclib3\Math\BigInteger\Engines\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)
\phpseclib3\Math\BigInteger\Engines\Engine
randomRangePrime(\phpseclib3\Math\BigInteger\Engines\BCMath $min,\phpseclib3\Math\BigInteger\Engines\BCMath $max): false|\phpseclib3\Math\BigInteger\Engines\BCMath
If there's not a prime within the given range, false will be returned.
false|\phpseclib3\Math\BigInteger\Engines\BCMath
randomRangePrimeInner(\phpseclib3\Math\BigInteger\Engines\Engine $x,\phpseclib3\Math\BigInteger\Engines\Engine $min,\phpseclib3\Math\BigInteger\Engines\Engine $max): static|false
static|false
randomRangePrimeOuter(\phpseclib3\Math\BigInteger\Engines\Engine $min,\phpseclib3\Math\BigInteger\Engines\Engine $max): static|false
static|false
reduce(string $n,string $m): string
See HAC 14.3.3 / MPM 6.2.5 for more information. Modified slightly, so as not to require negative numbers (initially, this script didn't support negative numbers).
Employs "folding", as described at thesis-149.pdf#page=66. To quote from it, "the idea [behind folding] is to find a value x' such that x (mod m) = x' (mod m), with x' being smaller than x."
Unfortunately, the "Barrett Reduction with Folding" algorithm described in thesis-149.pdf is not, as written, all that usable on account of (1) its not using reasonable radix points as discussed in MPM 6.2.2 and (2) the fact that, even with reasonable radix points, it only works when there are an even number of digits in the denominator. The reason for (2) is that (x >> 1) + (x >> 1) != x / 2 + x / 2. If x is even, they're the same, but if x is odd, they're not. See the in-line comments for details.
string
string
string
regularBarrett(string $x,string $n): string
For numbers with more than four digits BigInteger::_barrett() is faster. The difference between that and this is that this function does not fold the denominator into a smaller form.
string
string
string
root(integer $n = 2): \phpseclib3\Math\BigInteger\Engines\Engine
rootHelper(integer $n): \phpseclib3\Math\BigInteger\Engines\Engine
rootInner(integer $n): \phpseclib3\Math\BigInteger\Engines\Engine
Returns the nth root of a positive biginteger, where n defaults to 2
{@internal This function is based off of this page and this stackoverflow question.}
integer
\phpseclib3\Math\BigInteger\Engines\Engine
scan1divide(\phpseclib3\Math\BigInteger\Engines\BCMath $r): integer
ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));
see | self::isPrime() |
---|
integer
setBitmask(integer $bits): static
see | \phpseclib3\Math\BigInteger\Engines\Engine::setPrecision() |
---|
integer
static
setModExpEngine(\phpseclib3\Math\BigInteger\Engines\class-string<Engine> $engine)
Throws an exception if the type is invalid
\phpseclib3\Math\BigInteger\Engines\class-string
setPrecision(integer $bits)
Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.
integer
setupIsPrime(): integer
integer
slidingWindow(\phpseclib3\Math\BigInteger\Engines\Engine $x,\phpseclib3\Math\BigInteger\Engines\Engine $e,\phpseclib3\Math\BigInteger\Engines\Engine $n,\phpseclib3\Math\BigInteger\Engines\class-string<T> $class): \phpseclib3\Math\BigInteger\Engines\T
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.
template | T of Engine |
---|
\phpseclib3\Math\BigInteger\Engines\class-string
\phpseclib3\Math\BigInteger\Engines\T
squareReduce(string $x,string $n,string $class): string
see | self::slidingWindow() |
---|
string
string
string
string
subtract(\phpseclib3\Math\BigInteger\Engines\BCMath $y): \phpseclib3\Math\BigInteger\Engines\BCMath
testBit( $x): boolean
boolean
testPrimality(integer $t): boolean
Uses the Miller-Rabin primality test. See HAC 4.24 for more info.
integer
boolean
testSmallPrimes()
see | self::isPrime() |
---|---|
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.
boolean
string
toBytes(boolean $twos_compliment = false): string
boolean
string
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.
string
toHex(boolean $twos_compliment = false): string
boolean
string
toString(): string
string
VARIABLE
$cache[self::VARIABLE] tells us whether or not the cached data is still valid.
DATA
FAST_BITWISE
see | |
---|---|
ENGINE_DIR
see | |
---|---|
PRIMES
zero :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
var | static> |
---|
\phpseclib3\Math\BigInteger\Engines\array,
one :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
var | static> |
---|
\phpseclib3\Math\BigInteger\Engines\array,
two :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
var | static> |
---|
\phpseclib3\Math\BigInteger\Engines\array,
modexpEngine :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
var | class-string |
---|
\phpseclib3\Math\BigInteger\Engines\array,
isValidEngine :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
var | bool> |
---|
\phpseclib3\Math\BigInteger\Engines\array,
value :\GMP|string|array|integer
var |
---|
\GMP|string|array|integer
is_negative :boolean
var |
---|
boolean
precision :integer
see | |
---|---|
var |
integer
bitmask :static|false
see | |
---|---|
var |
static|false
reduce :callable
var |
---|
callable
hex :string
see | |
---|---|
var |
string