MontgomeryMult

Extends\phpseclib3\Math\BigInteger\Engines\PHP\Reductions\Montgomery

PHP Montgomery Modular Exponentiation Engine with interleaved multiplication

author

Jim Wigginton terrafrost@php.net

package

Default

Methods

Default constructor

__construct(integer|\phpseclib3\Math\BigInteger\Engines\numeric-string $x,integer $base = 10)
inherited

Arguments

$x

integer|\phpseclib3\Math\BigInteger\Engines\numeric-string

integer Base-10 number or base-$base number if $base set.

$base

integer

__debugInfo() magic method

__debugInfo(): array
inherited

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

Response

array

Serialize

__sleep(): array
inherited

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

Response

array

Converts a BigInteger to a base-10 number.

__toString(): string
inherited

Response

string

Serialize

__wakeup(): void
inherited

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

Absolute value.

abs(): \phpseclib3\Math\BigInteger\Engines\PHP
inherited

Performs addition.

addHelper(array $x_value,boolean $x_negative,array $y_value,boolean $y_negative): array
inheritedstatic

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

array

Array Repeat

array_repeat(integer $input,integer $multiplier): array
inheritedstatic

Arguments

$input

integer

$multiplier

integer

Response

array

Logical Left Shift

base256_lshift(string &$x,integer $shift): void
inheritedstatic

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

Arguments

$x

string

$shift

integer

Performs traditional squaring on two BigIntegers

baseSquare(array $value): array
inheritedstatic

Squaring can be done faster than multiplying a number by itself can be. See HAC 14.2.4 / MPM 5.3 for more information.

Arguments

$value

array

Response

array

Logical Left Rotate

bitwise_leftRotate(integer $shift): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Instead of the top x bits being dropped they're appended to the shifted bit string.

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Logical Left Shift

bitwise_leftShift(integer $shift): \phpseclib3\Math\BigInteger\Engines\PHP
inherited

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger\Engines\PHP

Logical Not

bitwise_not(): \phpseclib3\Math\BigInteger\Engines\Engine|string
inherited

Logical Right Rotate

bitwise_rightRotate(integer $shift): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Instead of the bottom x bits being dropped they're prepended to the shifted bit string.

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Logical Right Shift

bitwise_rightShift(integer $shift): \phpseclib3\Math\BigInteger\Engines\PHP
inherited

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger\Engines\PHP

Bitwise Split where $split < static::BASE

bitwise_small_split(integer $split): \phpseclib3\Math\BigInteger\Engines\list<int>
inherited

Arguments

$split

integer

Response

\phpseclib3\Math\BigInteger\Engines\list

Bitwise Split

bitwise_split(integer $split): array<mixed,\phpseclib3\Math\BigInteger\Engines\Engine>
inherited

Splits BigInteger's into chunks of $split bits

Arguments

$split

integer

Response

array<mixed,\phpseclib3\Math\BigInteger\Engines\Engine>

Logical And

bitwiseAndHelper(\phpseclib3\Math\BigInteger\Engines\Engine $x): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Logical Or

bitwiseOrHelper(\phpseclib3\Math\BigInteger\Engines\Engine $x): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Logical Exclusive Or

bitwiseXorHelper(\phpseclib3\Math\BigInteger\Engines\Engine $x): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Compares two numbers.

compareHelper(array $x_value,boolean $x_negative,array $y_value,boolean $y_negative): integer
inheritedstatic
see static::compare()

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

integer

Convert an array / boolean to a PHP BigInteger object

convertToObj(array $arr): static
inherited

Arguments

$arr

array

Response

static

Create Recurring Modulo Function

createRecurringModuloFunction(): callable
inherited

Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation

Response

callable

Divides a BigInteger by a regular integer

divide_digit(array $dividend,integer $divisor): array
inheritedstatic

abc / x = a00 / x + b0 / x + c / x

Arguments

$dividend

array

$divisor

integer

Response

array

Calculates the greatest common divisor and Bezout's identity.

extendedGCDHelper(\phpseclib3\Math\BigInteger\Engines\Engine $n): \phpseclib3\Math\BigInteger\Engines\array{gcd:
inherited

Arguments

Response

\phpseclib3\Math\BigInteger\Engines\array{gcd:

Engine, x: Engine, y: Engine}

Return the size of a BigInteger in bits

getLength(): integer
inherited

Response

integer

Return the size of a BigInteger in bytes

getLengthInBytes(): integer
inherited

Response

integer

Get Precision

getPrecision(): integer
inherited

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

Response

integer

Initialize a PHP BigInteger Engine instance

initialize(integer $base)
inherited
see \phpseclib3\Math\BigInteger\Engines\parent::__construct()

Arguments

$base

integer

Converts 32-bit integers to bytes.

int2bytes(integer $x): string
inheritedstatic

Arguments

$x

integer

Response

string

Is Negative?

isNegative(): boolean
inherited

Response

boolean

Is Odd?

isOdd(): boolean
inherited

Response

boolean

Checks a numer to see if it's prime

isPrime(integer|boolean $t = false): boolean
inherited

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.

Arguments

$t

integer|boolean

Response

boolean

Test for engine validity

isValidEngine(): boolean
inheritedstatic

Response

boolean

jsonSerialize

jsonSerialize()
inherited

Performs Karatsuba multiplication on two BigIntegers

karatsuba(array $x_value,array $y_value): array
inheritedstatic

Arguments

$x_value

array

$y_value

array

Response

array

Performs Karatsuba "squaring" on two BigIntegers

karatsubaSquare(array $value): array
inheritedstatic

Arguments

$value

array

Response

array

Logical Left Shift

lshift(integer $shift)
inherited

Shifts BigInteger's by $shift bits.

Arguments

$shift

integer

Make the current number odd

make_odd()
inherited

If the current number is odd it'll be unchanged. If it's even, one will be added to it.

see self::randomPrime()

Return the minimum BigInteger between an arbitrary number of BigIntegers.

maxHelper(array $nums): \phpseclib3\Math\BigInteger\Engines\Engine
inheritedstatic

Arguments

$nums

array

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Return the minimum BigInteger between an arbitrary number of BigIntegers.

minHelper(array $nums): \phpseclib3\Math\BigInteger\Engines\Engine
inheritedstatic

Arguments

$nums

array

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Returns the smallest and largest n-bit number

minMaxBits(integer $bits): \phpseclib3\Math\BigInteger\Engines\array{min:
inheritedstatic

Arguments

$bits

integer

Response

\phpseclib3\Math\BigInteger\Engines\array{min:

static, max: static}

Modular Inverse of a number mod 2**26 (eg. 67108864)

modInverse67108864(array $x,string $class): integer
inheritedstatic

Based off of the bnpInvDigit function implemented and justified in the following URL:

http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js

The following URL provides more info:

http://groups.google.com/group/sci.crypt/msg/7a137205c1be7d85

As for why we do all the bitmasking... strange things can happen when converting from floats to ints. For instance, on some computers, var_dump((int) -4294967297) yields int(-1) and on others, it yields int(-2147483648). To avoid problems stemming from this, we use bitmasks to guarantee that ints aren't auto-converted to floats. The outermost bitmask is present because without it, there's no guarantee that the "residue" returned would be the so-called "common residue". We use fmod, in the last step, because the maximum possible $x is 26 bits and the maximum $result is 16 bits. Thus, we have to be able to handle up to 40 bits, which only 64-bit floating points will support.

Thanks to Pedro Gimeno Fortea for input!

Arguments

$x

array

$class

string

Response

integer

Calculates modular inverses.

modInverseHelper(\phpseclib3\Math\BigInteger\Engines\Engine $n): static|false
inherited

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

Arguments

Response

static|false

Performs multiplication.

multiplyHelper(array $x_value,boolean $x_negative,array $y_value,boolean $y_negative): array
inheritedstatic

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

array

Modular multiply

multiplyReduce(array $x,array $y,array $n,string $class): array
inheritedstatic
see self::slidingWindow()

Arguments

$x

array

$y

array

$n

array

$class

string

Response

array

Negate

negate(): static
inherited

Given $k, returns -$k

Response

static

Normalize

normalize(\phpseclib3\Math\BigInteger\Engines\PHP $result): static
inherited

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

Arguments

Response

static

Pads strings so that unpack may be used on them

pad(string $str): string
inherited

Arguments

$str

string

Response

string

Performs exponentiation.

powHelper(\phpseclib3\Math\BigInteger\Engines\PHP $n): \phpseclib3\Math\BigInteger\Engines\PHP
inherited

Performs modular exponentiation.

powModHelper(\phpseclib3\Math\BigInteger\Engines\PHP $x,\phpseclib3\Math\BigInteger\Engines\PHP $e,\phpseclib3\Math\BigInteger\Engines\PHP $n,string $class): \phpseclib3\Math\BigInteger\Engines\PHP
inheritedstatic

The most naive approach to modular exponentiation has very unreasonable requirements, and and although the approach involving repeated squaring does vastly better, it, too, is impractical for our purposes. The reason being that division - by far the most complicated and time-consuming of the basic operations (eg. +,-,*,/) - occurs multiple times within it.

Modular reductions resolve this issue. Although an individual modular reduction takes more time then an individual division, when performed in succession (with the same modulo), they're a lot faster.

The two most commonly used modular reductions are Barrett and Montgomery reduction. Montgomery reduction, although faster, only works when the gcd of the modulo and of the base being used is 1. In RSA, when the base is a power of two, the modulo - a product of two primes - is always going to have a gcd of 1 (because the product of two odd numbers is odd), but what about when RSA isn't used?

In contrast, Barrett reduction has no such constraint. As such, some bigint implementations perform a Barrett reduction after every operation in the modpow function. Others perform Barrett reductions when the modulo is even and Montgomery reductions when the modulo is odd. BigInteger.java's modPow method, however, uses a trick involving the Chinese Remainder Theorem to factor the even modulo into two numbers - one odd and the other, a power of two - and recombine them, later. This is the method that this modPow function uses. Montgomery Reduction with Even Modulus elaborates.

Arguments

$class

string

Response

\phpseclib3\Math\BigInteger\Engines\PHP

Performs modular exponentiation.

powModInner(\phpseclib3\Math\BigInteger\Engines\PHP $e,\phpseclib3\Math\BigInteger\Engines\PHP $n): \phpseclib3\Math\BigInteger\Engines\PHP
inherited

Performs some pre-processing for powMod

powModOuter(\phpseclib3\Math\BigInteger\Engines\Engine $e,\phpseclib3\Math\BigInteger\Engines\Engine $n): static|false
inherited

Modular reduction preparation

prepareReduce(array $x,array $n,string $class): array
inheritedstatic
see self::slidingWindow()

Arguments

$x

array

$n

array

$class

string

Response

array

Generates a random number of a certain size

random(integer $size): \phpseclib3\Math\BigInteger\Engines\Engine
inheritedstatic

Bit length is equal to $size

Arguments

$size

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Generates a random prime number of a certain size

randomPrime(integer $size): \phpseclib3\Math\BigInteger\Engines\Engine
inheritedstatic

Bit length is equal to $size

Arguments

$size

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Generate a random number between a range

randomRangeHelper(\phpseclib3\Math\BigInteger\Engines\Engine $min,\phpseclib3\Math\BigInteger\Engines\Engine $max): \phpseclib3\Math\BigInteger\Engines\Engine
inheritedstatic

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)

Arguments

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Performs some post-processing for randomRangePrime

randomRangePrimeInner(\phpseclib3\Math\BigInteger\Engines\Engine $x,\phpseclib3\Math\BigInteger\Engines\Engine $min,\phpseclib3\Math\BigInteger\Engines\Engine $max): static|false
inheritedstatic

Performs some pre-processing for randomRangePrime

randomRangePrimeOuter(\phpseclib3\Math\BigInteger\Engines\Engine $min,\phpseclib3\Math\BigInteger\Engines\Engine $max): static|false
inheritedstatic

Montgomery Multiply

reduce(array $x,array $n,string $class): array
inheritedstatic

Interleaves the montgomery reduction and long multiplication algorithms together as described in HAC 14.36

Arguments

$x

array

$n

array

$class

string

Response

array

Performs long multiplication on two BigIntegers

regularMultiply(array $x_value,array $y_value): array
inheritedstatic

Modeled after 'multiply' in MutableBigInteger.java.

Arguments

$x_value

array

$y_value

array

Response

array

Calculates the nth root of a biginteger.

root(integer $n = 2): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Arguments

$n

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Performs a few preliminary checks on root

rootHelper(integer $n): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

Arguments

$n

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Calculates the nth root of a biginteger.

rootInner(integer $n): \phpseclib3\Math\BigInteger\Engines\Engine
inherited

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

Arguments

$n

integer

Response

\phpseclib3\Math\BigInteger\Engines\Engine

Logical Right Shift

rshift(integer $shift)
inherited

Shifts BigInteger's by $shift bits.

Arguments

$shift

integer

Single digit division

safe_divide(integer $x,integer $y): integer
inheritedstatic

Even if int64 is being used the division operator will return a float64 value if the dividend is not evenly divisible by the divisor. Since a float64 doesn't have the precision of int64 this is a problem so, when int64 is being used, we'll guarantee that the dividend is divisible by first subtracting the remainder.

Arguments

$x

integer

$y

integer

Response

integer

Scan for 1 and right shift by that amount

scan1divide(\phpseclib3\Math\BigInteger\Engines\PHP $r): integer
inheritedstatic

ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s));

see self::isPrime()

Arguments

Response

integer

Set Bitmask

setBitmask(integer $bits): static
inheritedstatic
see \phpseclib3\Math\BigInteger\Engines\Engine::setPrecision()

Arguments

$bits

integer

Response

static

Sets engine type.

setModExpEngine(\phpseclib3\Math\BigInteger\Engines\class-string<Engine> $engine)
inheritedstatic

Throws an exception if the type is invalid

Arguments

$engine

\phpseclib3\Math\BigInteger\Engines\class-string

Set Precision

setPrecision(integer $bits)
inherited

Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.

Arguments

$bits

integer

Sets the $t parameter for primality testing

setupIsPrime(): integer
inherited

Response

integer

Sliding Window k-ary Modular Exponentiation

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
inheritedstatic

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

Arguments

$class

\phpseclib3\Math\BigInteger\Engines\class-string

Response

\phpseclib3\Math\BigInteger\Engines\T

Performs squaring

square(\phpseclib3\Math\BigInteger\Engines\list<static> $x): \phpseclib3\Math\BigInteger\Engines\list<static>
inheritedstatic

Arguments

$x

\phpseclib3\Math\BigInteger\Engines\list

Response

\phpseclib3\Math\BigInteger\Engines\list

Modular square

squareReduce(array $x,array $n,string $class): array
inheritedstatic
see self::slidingWindow()

Arguments

$x

array

$n

array

$class

string

Response

array

Performs subtraction.

subtractHelper(array $x_value,boolean $x_negative,array $y_value,boolean $y_negative): array
inheritedstatic

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

array

Tests if a bit is set

testBit( $x): boolean
inherited

Arguments

$x

Response

boolean

Tests Primality

testPrimality(integer $t): boolean
inherited

Uses the Miller-Rabin primality test. See HAC 4.24 for more info.

Arguments

$t

integer

Response

boolean

Test the number against small primes.

testSmallPrimes()
inherited
see self::isPrime()

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

toBits(boolean $twos_compliment = false): string
inherited

Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.

Arguments

$twos_compliment

boolean

Response

string

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

toBytes(boolean $twos_compliment = false): string
inherited

Arguments

$twos_compliment

boolean

Response

string

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

toBytesHelper(): string
inherited

Negative numbers are saved as positive numbers, unless $twos_compliment is set to true, at which point, they're saved as two's compliment.

Response

string

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

toHex(boolean $twos_compliment = false): string
inherited

Arguments

$twos_compliment

boolean

Response

string

Converts a BigInteger to a base-10 number.

toString(): string
inherited

Response

string

Trim

trim(\phpseclib3\Math\BigInteger\Engines\list<static> $value): \phpseclib3\Math\BigInteger\Engines\list<static>
inheritedstatic

Removes leading zeros

Arguments

$value

\phpseclib3\Math\BigInteger\Engines\list

Response

\phpseclib3\Math\BigInteger\Engines\list

Constants

Cache constants

VARIABLE
inherited

$cache[self::VARIABLE] tells us whether or not the cached data is still valid.

$cache[self::DATA] contains the cached data.

DATA
inherited

$result[self::VALUE] contains the value.

VALUE
inherited

$result[self::SIGN] contains the sign.

SIGN
inherited

Karatsuba Cutoff

KARATSUBA_CUTOFF
inherited

At what point do we switch between Karatsuba multiplication and schoolbook long multiplication?

Can Bitwise operations be done fast?

FAST_BITWISE
inherited
see

Engine Directory

ENGINE_DIR
inherited
see

PRIMES

PRIMES
inherited

Properties

BigInteger(0)

zero :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
inheritedstatic
var

static>

Type(s)

\phpseclib3\Math\BigInteger\Engines\array,

BigInteger(1)

one :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
inheritedstatic
var

static>

Type(s)

\phpseclib3\Math\BigInteger\Engines\array,

BigInteger(2)

two :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
inheritedstatic
var

static>

Type(s)

\phpseclib3\Math\BigInteger\Engines\array,

Modular Exponentiation Engine

modexpEngine :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
inheritedstatic
var

class-string>

Type(s)

\phpseclib3\Math\BigInteger\Engines\array,

Engine Validity Flag

isValidEngine :\phpseclib3\Math\BigInteger\Engines\array<class-string<static>,
inheritedstatic
var

bool>

Type(s)

\phpseclib3\Math\BigInteger\Engines\array,

Holds the BigInteger's value

value :\GMP|string|array|integer
inherited
var

Type(s)

\GMP|string|array|integer

Holds the BigInteger's sign

is_negative :boolean
inherited
var

Type(s)

boolean

Precision

precision :integer
inherited
see
var

Type(s)

integer

Precision Bitmask

bitmask :static|false
inherited
see
var

Type(s)

static|false

Recurring Modulo Function

reduce :callable
inherited
var

Type(s)

callable

Mode independent value used for serialization.

hex :string
inherited
see
var

Type(s)

string