Base

Extends\phpseclib3\Math\BigInteger\Engines\BCMath

Sliding Window Exponentiation Engine

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\BCMath
inherited

Adds two BigIntegers.

add(\phpseclib3\Math\BigInteger\Engines\BCMath $y): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

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

Tests BigInteger to see if it is between two integers, inclusive

between(\phpseclib3\Math\BigInteger\Engines\BCMath $min,\phpseclib3\Math\BigInteger\Engines\BCMath $max): boolean
inherited

Logical And

bitwise_and(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

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\BCMath
inherited

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger\Engines\BCMath

Logical Not

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

Logical Or

bitwise_or(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
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\BCMath
inherited

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

Arguments

$shift

integer

Response

\phpseclib3\Math\BigInteger\Engines\BCMath

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 Exclusive Or

bitwise_xor(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

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.

compare(\phpseclib3\Math\BigInteger\Engines\BCMath $y): integer
inherited

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

Arguments

Response

integer

in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.

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 two BigIntegers.

divide(\phpseclib3\Math\BigInteger\Engines\BCMath $y): \phpseclib3\Math\BigInteger\Engines\array{static,
inherited

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

Arguments

Response

\phpseclib3\Math\BigInteger\Engines\array{static,

static}

Tests the equality of two numbers.

equals(\phpseclib3\Math\BigInteger\Engines\BCMath $x): boolean
inherited

If you need to see if one number is greater than or less than another number, use BigInteger::compare()

Arguments

Response

boolean

Calculates the greatest common divisor and Bezout's identity.

extendedGCD(\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\array{gcd:
inherited

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.

Arguments

Response

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

static, x: static, y: static}

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}

Calculates the greatest common divisor

gcd(\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Say you have 693 and 609. The GCD is 21.

Arguments

Response

\phpseclib3\Math\BigInteger\Engines\BCMath

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 BCMath BigInteger Engine instance

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

Arguments

$base

integer

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
see \phpseclib3\Math\BigInteger\Engines\parent::__construct()

Response

boolean

jsonSerialize

jsonSerialize()
inherited

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 maximum BigInteger between an arbitrary number of BigIntegers.

max(\phpseclib3\Math\BigInteger\Engines\BCMath $nums): \phpseclib3\Math\BigInteger\Engines\BCMath
inheritedstatic

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.

min(\phpseclib3\Math\BigInteger\Engines\BCMath $nums): \phpseclib3\Math\BigInteger\Engines\BCMath
inheritedstatic

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}

Calculates modular inverses.

modInverse(\phpseclib3\Math\BigInteger\Engines\BCMath $n): false|\phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses.

Arguments

Response

false|\phpseclib3\Math\BigInteger\Engines\BCMath

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 modular exponentiation.

modPow(\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Multiplies two BigIntegers.

multiply(\phpseclib3\Math\BigInteger\Engines\BCMath $x): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Modular multiply

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

Arguments

$x

string

$y

string

$n

string

$class

string

Response

string

Negate

negate(): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Given $k, returns -$k

Response

\phpseclib3\Math\BigInteger\Engines\BCMath

Normalize

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

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

Arguments

Response

\phpseclib3\Math\BigInteger\Engines\BCMath

Performs exponentiation.

pow(\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Performs modular exponentiation.

powMod(\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

Performs modular exponentiation.

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
static

Performs modular exponentiation.

powModInner(\phpseclib3\Math\BigInteger\Engines\BCMath $e,\phpseclib3\Math\BigInteger\Engines\BCMath $n): \phpseclib3\Math\BigInteger\Engines\BCMath
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(string $x,string $n,string $class): string
static
see self::slidingWindow()

Arguments

$x

string

$n

string

$class

string

Response

string

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

randomRange(\phpseclib3\Math\BigInteger\Engines\BCMath $min,\phpseclib3\Math\BigInteger\Engines\BCMath $max): \phpseclib3\Math\BigInteger\Engines\BCMath
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\BCMath

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

Generate a random prime number between a range

randomRangePrime(\phpseclib3\Math\BigInteger\Engines\BCMath $min,\phpseclib3\Math\BigInteger\Engines\BCMath $max): false|\phpseclib3\Math\BigInteger\Engines\BCMath
inheritedstatic

If there's not a prime within the given range, false will be returned.

Arguments

Response

false|\phpseclib3\Math\BigInteger\Engines\BCMath

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

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

Scan for 1 and right shift by that amount

scan1divide(\phpseclib3\Math\BigInteger\Engines\BCMath $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

Modular square

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

Arguments

$x

string

$n

string

$class

string

Response

string

Subtracts two BigIntegers.

subtract(\phpseclib3\Math\BigInteger\Engines\BCMath $y): \phpseclib3\Math\BigInteger\Engines\BCMath
inherited

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

Constants

Cache constants

VARIABLE

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

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

DATA

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