PHP

Extends\phpseclib3\Math\BigInteger\Engines\Engine

Pure-PHP 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\PHP

Performs addition.

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

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

array

Array Repeat

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

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
static

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

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

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>

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
static
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

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
static

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

Arguments

$base

integer

Converts 32-bit integers to bytes.

int2bytes(integer $x): string
static

Arguments

$x

integer

Response

string

Is Negative?

isNegative(): boolean

Response

boolean

Is Odd?

isOdd(): boolean

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

jsonSerialize

jsonSerialize()
inherited

Performs Karatsuba multiplication on two BigIntegers

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

Arguments

$x_value

array

$y_value

array

Response

array

Performs Karatsuba "squaring" on two BigIntegers

karatsubaSquare(array $value): array
static

Arguments

$value

array

Response

array

Logical Left Shift

lshift(integer $shift)

Shifts BigInteger's by $shift bits.

Arguments

$shift

integer

Make the current number odd

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

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}

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
static

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

array

Negate

negate(): static

Given $k, returns -$k

Response

static

Normalize

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

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

Arguments

$str

string

Response

string

Performs exponentiation.

powHelper(\phpseclib3\Math\BigInteger\Engines\PHP $n): \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

Performs some pre-processing for powMod

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

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

Performs long multiplication on two BigIntegers

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

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)

Shifts BigInteger's by $shift bits.

Arguments

$shift

integer

Single digit division

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

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
static

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>
static

Arguments

$x

\phpseclib3\Math\BigInteger\Engines\list

Response

\phpseclib3\Math\BigInteger\Engines\list

Performs subtraction.

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

Arguments

$x_value

array

$x_negative

boolean

$y_value

array

$y_negative

boolean

Response

array

Tests if a bit is set

testBit( $x): boolean

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

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

Response

string

Trim

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

Removes leading zeros

Arguments

$value

\phpseclib3\Math\BigInteger\Engines\list

Response

\phpseclib3\Math\BigInteger\Engines\list

Constants

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

VALUE

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

SIGN

Karatsuba Cutoff

KARATSUBA_CUTOFF

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

Can Bitwise operations be done fast?

FAST_BITWISE
see

Engine Directory

ENGINE_DIR
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