Documentation

PHP64 extends PHP
in package

Pure-PHP 64-bit Engine.

Uses 64-bit integers if int size is 8 bits

Tags
author

Jim Wigginton terrafrost@php.net

access

public

Table of Contents

Constants

BASE  = 31
BASE_FULL  = 0x80000000
ENGINE_DIR  = 'PHP'
Engine Directory
FAST_BITWISE  = true
Can Bitwise operations be done fast?
KARATSUBA_CUTOFF  = 25
Karatsuba Cutoff
MAX10  = 1000000000
MAX10 in greatest MAX10LEN satisfying MAX10 = 10**MAX10LEN <= 2**BASE.
MAX10LEN  = 9
MAX10LEN in greatest MAX10LEN satisfying MAX10 = 10**MAX10LEN <= 2**BASE.
MAX_DIGIT  = 0x7fffffff
MAX_DIGIT2  = 4611686018427387904
MSB  = 0x40000000
SIGN  = 1
$result[self::SIGN] contains the sign.
VALUE  = 0
$result[self::VALUE] contains the value.

Properties

$bitmask  : mixed
Precision Bitmask
$is_negative  : bool
Holds the BigInteger's sign
$isValidEngine  : bool
Engine Validity Flag
$modexpEngine  : string
Modular Exponentiation Engine
$one  : PHP64
BigInteger(1)
$precision  : mixed
Precision
$primes  : array<string|int, mixed>
Primes > 2 and < 1000
$reduce  : callable
Recurring Modulo Function
$two  : PHP64
BigInteger(2)
$value  : mixed
Holds the BigInteger's value
$zero  : PHP64
BigInteger(0)

Methods

__construct()  : PHP
Default constructor
__debugInfo()  : mixed
__debugInfo() magic method
__toString()  : string
Converts a BigInteger to a base-10 number.
abs()  : PHP
Absolute value.
add()  : PHP64
Adds two BigIntegers.
between()  : bool
Tests BigInteger to see if it is between two integers, inclusive
bitwise_and()  : PHP64
Logical And
bitwise_leftRotate()  : Engine
Logical Left Rotate
bitwise_leftShift()  : PHP
Logical Left Shift
bitwise_not()  : Engine|string
Logical Not
bitwise_or()  : PHP64
Logical Or
bitwise_rightRotate()  : Engine
Logical Right Rotate
bitwise_rightShift()  : PHP
Logical Right Shift
bitwise_split()  : array<string|int, PHP>
Bitwise Split
bitwise_xor()  : PHP64
Logical Exclusive Or
compare()  : int
Compares two numbers.
createRecurringModuloFunction()  : callable
Create Recurring Modulo Function
divide()  : PHP64
Divides two BigIntegers.
equals()  : bool
Tests the equality of two numbers.
extendedGCD()  : array<string|int, PHP64>
Calculates modular inverses.
gcd()  : PHP64
Calculates the greatest common divisor
getLength()  : int
Return the size of a BigInteger in bits
getLengthInBytes()  : int
Return the size of a BigInteger in bytes
getPrecision()  : int
Get Precision
isNegative()  : bool
Is Negative?
isOdd()  : bool
Is Odd?
isPrime()  : bool
Checks a numer to see if it's prime
isValidEngine()  : bool
Test for engine validity
max()  : PHP64
Return the maximum BigInteger between an arbitrary number of BigIntegers.
min()  : PHP64
Return the minimum BigInteger between an arbitrary number of BigIntegers.
minMaxBits()  : array<string|int, Engine>
Returns the smallest and largest n-bit number
modInverse()  : false|PHP64
Calculates modular inverses.
modPow()  : PHP64
Performs modular exponentiation.
multiply()  : PHP64
Multiplies two BigIntegers.
negate()  : BigInteger
Negate
pow()  : PHP64
Performs exponentiation.
powMod()  : PHP64
Performs modular exponentiation.
random()  : Engine
Generates a random number of a certain size
randomPrime()  : Engine
Generates a random prime number of a certain size
randomRange()  : PHP64
Generate a random number between a range
randomRangePrime()  : false|PHP64
Generate a random prime number between a range
root()  : Engine
Calculates the nth root of a biginteger.
scan1divide()  : int
Scan for 1 and right shift by that amount
serialize()  : string
Serialize
setModExpEngine()  : mixed
Sets engine type.
setPrecision()  : mixed
Set Precision
subtract()  : PHP64
Subtracts two BigIntegers.
subtractHelper()  : array<string|int, mixed>
Performs subtraction.
testBit()  : bool
Tests if a bit is set
toBits()  : string
Converts a BigInteger to a bit string (eg. base-2).
toBytes()  : string
Converts a BigInteger to a byte string (eg. base-256).
toHex()  : string
Converts a BigInteger to a hex string (eg. base-16).
toString()  : string
Converts a BigInteger to a base-10 number.
unserialize()  : mixed
Serialize
addHelper()  : array<string|int, mixed>
Performs addition.
array_repeat()  : array<string|int, mixed>
Array Repeat
base256_lshift()  : string
Logical Left Shift
baseSquare()  : array<string|int, mixed>
Performs traditional squaring on two BigIntegers
bitwiseAndHelper()  : Engine
Logical And
bitwiseOrHelper()  : Engine
Logical Or
bitwiseXorHelper()  : Engine
Logical Exclusive Or
compareHelper()  : mixed
convertToObj()  : mixed
extendedGCDHelper()  : Engine
Calculates the greatest common divisor and Bezout's identity.
initialize()  : mixed
Initialize a PHP64 BigInteger Engine instance
karatsubaSquare()  : array<string|int, mixed>
Performs Karatsuba "squaring" on two BigIntegers
lshift()  : mixed
Logical Left Shift
make_odd()  : mixed
Make the current number odd
maxHelper()  : Engine
Return the minimum BigInteger between an arbitrary number of BigIntegers.
minHelper()  : Engine
Return the minimum BigInteger between an arbitrary number of BigIntegers.
modInverseHelper()  : Engine|false
Calculates modular inverses.
multiplyHelper()  : array<string|int, mixed>
Performs multiplication.
normalize()  : PHP
Normalize
pad()  : string
Pads strings so that unpack may be used on them
powHelper()  : PHP
Performs exponentiation.
powModInner()  : PHP
Performs modular exponentiation.
powModOuter()  : bool|Engine
Performs some pre-processing for powMod
randomRangeHelper()  : Engine
Generate a random number between a range
randomRangePrimeInner()  : bool|Engine
Performs some post-processing for randomRangePrime
randomRangePrimeOuter()  : bool|Engine
Performs some pre-processing for randomRangePrime
regularMultiply()  : array<string|int, mixed>
Performs long multiplication on two BigIntegers
rootHelper()  : Engine
Performs a few preliminary checks on root
rootInner()  : Engine
Calculates the nth root of a biginteger.
rshift()  : mixed
Logical Right Shift
setBitmask()  : Engine
Set Bitmask
setupIsPrime()  : int
Sets the $t parameter for primality testing
slidingWindow()  : Engine
Sliding Window k-ary Modular Exponentiation
square()  : array<string|int, mixed>
Performs squaring
testPrimality()  : bool
Tests Primality
testSmallPrimes()  : mixed
Test the number against small primes.
toBytesHelper()  : string
Converts a BigInteger to a byte string (eg. base-256).
trim()  : PHP
Trim
bitwise_small_split()  : array<string|int, PHP>
Bitwise Split where $split < static::BASE
divide_digit()  : array<string|int, mixed>
Divides a BigInteger by a regular integer
int2bytes()  : string
Converts 32-bit integers to bytes.
karatsuba()  : array<string|int, mixed>
Performs Karatsuba multiplication on two BigIntegers
safe_divide()  : int
Single digit division

Constants

BASE_FULL

public mixed BASE_FULL = 0x80000000

ENGINE_DIR

Engine Directory

public mixed ENGINE_DIR = 'PHP'
Tags
see
parent::setModExpEngine
access

protected

FAST_BITWISE

Can Bitwise operations be done fast?

public mixed FAST_BITWISE = true
Tags
see
parent::bitwise_leftRotate()
see
parent::bitwise_rightRotate()
access

protected

KARATSUBA_CUTOFF

Karatsuba Cutoff

public mixed KARATSUBA_CUTOFF = 25

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

Tags
access

private

MAX10

MAX10 in greatest MAX10LEN satisfying MAX10 = 10**MAX10LEN <= 2**BASE.

public mixed MAX10 = 1000000000

MAX10LEN

MAX10LEN in greatest MAX10LEN satisfying MAX10 = 10**MAX10LEN <= 2**BASE.

public mixed MAX10LEN = 9

MAX_DIGIT

public mixed MAX_DIGIT = 0x7fffffff

MAX_DIGIT2

public mixed MAX_DIGIT2 = 4611686018427387904

MSB

public mixed MSB = 0x40000000

SIGN

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

public mixed SIGN = 1

VALUE

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

public mixed VALUE = 0

Properties

$bitmask

Precision Bitmask

protected mixed $bitmask = false
Tags
see
static::setPrecision()

$is_negative

Holds the BigInteger's sign

protected bool $is_negative

$isValidEngine

Engine Validity Flag

protected static bool $isValidEngine

$modexpEngine

Modular Exponentiation Engine

protected static string $modexpEngine

$precision

Precision

protected mixed $precision = -1
Tags
see
static::setPrecision()

$primes

Primes > 2 and < 1000

protected static array<string|int, mixed> $primes

$reduce

Recurring Modulo Function

protected callable $reduce

$value

Holds the BigInteger's value

protected mixed $value

Methods

__construct()

Default constructor

public __construct([mixed $x = 0 ][, int $base = 10 ]) : PHP
Parameters
$x : mixed = 0

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

$base : int = 10
Tags
see
parent::__construct()
Return values
PHP

__debugInfo()

__debugInfo() magic method

public __debugInfo() : mixed

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

__toString()

Converts a BigInteger to a base-10 number.

public __toString() : string
Return values
string

abs()

Absolute value.

public abs() : PHP
Return values
PHP

between()

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

public between(PHP64 $min, PHP64 $max) : bool
Parameters
$min : PHP64
$max : PHP64
Return values
bool

bitwise_leftRotate()

Logical Left Rotate

public bitwise_leftRotate(int $shift) : Engine

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

Parameters
$shift : int
Return values
Engine

bitwise_leftShift()

Logical Left Shift

public bitwise_leftShift(int $shift) : PHP

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

Parameters
$shift : int
Return values
PHP

bitwise_rightRotate()

Logical Right Rotate

public bitwise_rightRotate(int $shift) : Engine

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

Parameters
$shift : int
Return values
Engine

bitwise_rightShift()

Logical Right Shift

public bitwise_rightShift(int $shift) : PHP

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

Parameters
$shift : int
Return values
PHP

bitwise_split()

Bitwise Split

public bitwise_split(int $split) : array<string|int, PHP>

Splits BigInteger's into chunks of $split bits

Parameters
$split : int
Return values
array<string|int, PHP>

compare()

Compares two numbers.

public compare(PHP64 $y) : int

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

Parameters
$y : PHP64
Tags
access

public

see
self::equals()
Return values
int

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

createRecurringModuloFunction()

Create Recurring Modulo Function

public createRecurringModuloFunction() : callable

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

Return values
callable

divide()

Divides two BigIntegers.

public divide(PHP64 $y) : PHP64

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

Parameters
$y : PHP64
Return values
PHP64

equals()

Tests the equality of two numbers.

public equals(PHP64 $x) : bool

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

Parameters
$x : PHP64
Return values
bool

extendedGCD()

Calculates modular inverses.

public extendedGCD(PHP64 $n) : array<string|int, PHP64>

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

Parameters
$n : PHP64
Return values
array<string|int, PHP64>

gcd()

Calculates the greatest common divisor

public gcd(PHP64 $n) : PHP64

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

Parameters
$n : PHP64
Return values
PHP64

getLength()

Return the size of a BigInteger in bits

public getLength() : int
Return values
int

getLengthInBytes()

Return the size of a BigInteger in bytes

public getLengthInBytes() : int
Return values
int

getPrecision()

Get Precision

public getPrecision() : int

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

Return values
int

isNegative()

Is Negative?

public isNegative() : bool
Return values
bool

isOdd()

Is Odd?

public isOdd() : bool
Return values
bool

isPrime()

Checks a numer to see if it's prime

public isPrime([int|bool $t = false ]) : bool

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.

Parameters
$t : int|bool = false
Return values
bool

isValidEngine()

Test for engine validity

public static isValidEngine() : bool
Tags
see
parent::__construct()
Return values
bool

max()

Return the maximum BigInteger between an arbitrary number of BigIntegers.

public static max(PHP64 ...$nums) : PHP64
Parameters
$nums : PHP64
Return values
PHP64

min()

Return the minimum BigInteger between an arbitrary number of BigIntegers.

public static min(PHP64 ...$nums) : PHP64
Parameters
$nums : PHP64
Return values
PHP64

minMaxBits()

Returns the smallest and largest n-bit number

public static minMaxBits(int $bits) : array<string|int, Engine>
Parameters
$bits : int
Return values
array<string|int, Engine>

modInverse()

Calculates modular inverses.

public modInverse(PHP64 $n) : false|PHP64

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

Parameters
$n : PHP64
Return values
false|PHP64

negate()

Negate

public negate() : BigInteger

Given $k, returns -$k

Return values
BigInteger

random()

Generates a random number of a certain size

public static random(int $size) : Engine

Bit length is equal to $size

Parameters
$size : int
Return values
Engine

randomPrime()

Generates a random prime number of a certain size

public static randomPrime(int $size) : Engine

Bit length is equal to $size

Parameters
$size : int
Return values
Engine

randomRange()

Generate a random number between a range

public static randomRange(PHP64 $min, PHP64 $max) : PHP64

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)

Parameters
$min : PHP64
$max : PHP64
Return values
PHP64

randomRangePrime()

Generate a random prime number between a range

public static randomRangePrime(PHP64 $min, PHP64 $max) : false|PHP64

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

Parameters
$min : PHP64
$max : PHP64
Return values
false|PHP64

root()

Calculates the nth root of a biginteger.

public root([int $n = 2 ]) : Engine
Parameters
$n : int = 2
Return values
Engine

scan1divide()

Scan for 1 and right shift by that amount

public static scan1divide(PHP $r) : int

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

Parameters
$r : PHP
Tags
see
self::isPrime()
Return values
int

serialize()

Serialize

public serialize() : string

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

Return values
string

setModExpEngine()

Sets engine type.

public static setModExpEngine(string $engine) : mixed

Throws an exception if the type is invalid

Parameters
$engine : string

setPrecision()

Set Precision

public setPrecision(int $bits) : mixed

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

Parameters
$bits : int

subtractHelper()

Performs subtraction.

public static subtractHelper(array<string|int, mixed> $x_value, bool $x_negative, array<string|int, mixed> $y_value, bool $y_negative) : array<string|int, mixed>
Parameters
$x_value : array<string|int, mixed>
$x_negative : bool
$y_value : array<string|int, mixed>
$y_negative : bool
Return values
array<string|int, mixed>

testBit()

Tests if a bit is set

public testBit(mixed $x) : bool
Parameters
$x : mixed
Return values
bool

toBits()

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

public toBits([bool $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.

Parameters
$twos_compliment : bool = false
Return values
string

toBytes()

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

public toBytes([bool $twos_compliment = false ]) : string
Parameters
$twos_compliment : bool = false
Return values
string

toHex()

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

public toHex([bool $twos_compliment = false ]) : string
Parameters
$twos_compliment : bool = false
Return values
string

toString()

Converts a BigInteger to a base-10 number.

public toString() : string
Return values
string

unserialize()

Serialize

public unserialize(string $serialized) : mixed

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

Parameters
$serialized : string

addHelper()

Performs addition.

protected static addHelper(array<string|int, mixed> $x_value, bool $x_negative, array<string|int, mixed> $y_value, bool $y_negative) : array<string|int, mixed>
Parameters
$x_value : array<string|int, mixed>
$x_negative : bool
$y_value : array<string|int, mixed>
$y_negative : bool
Return values
array<string|int, mixed>

array_repeat()

Array Repeat

protected static array_repeat(int $input, int $multiplier) : array<string|int, mixed>
Parameters
$input : int
$multiplier : int
Return values
array<string|int, mixed>

base256_lshift()

Logical Left Shift

protected static base256_lshift(string &$x, int $shift) : string

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

Parameters
$x : string
$shift : int
Return values
string

baseSquare()

Performs traditional squaring on two BigIntegers

protected static baseSquare(array<string|int, mixed> $value) : array<string|int, mixed>

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

Parameters
$value : array<string|int, mixed>
Return values
array<string|int, mixed>

compareHelper()

protected static compareHelper(array<string|int, mixed> $x_value, mixed $x_negative, array<string|int, mixed> $y_value, mixed $y_negative) : mixed
Parameters
$x_value : array<string|int, mixed>
$x_negative : mixed
$y_value : array<string|int, mixed>
$y_negative : mixed

convertToObj()

protected convertToObj(array<string|int, mixed> $arr) : mixed
Parameters
$arr : array<string|int, mixed>

extendedGCDHelper()

Calculates the greatest common divisor and Bezout's identity.

protected extendedGCDHelper(Engine $n[, Engine $stop = null ]) : Engine
Parameters
$n : Engine
$stop : Engine = null

(optional)

Return values
Engine

initialize()

Initialize a PHP64 BigInteger Engine instance

protected initialize(int $base) : mixed
Parameters
$base : int
Tags
see
parent::initialize()

karatsubaSquare()

Performs Karatsuba "squaring" on two BigIntegers

protected static karatsubaSquare(array<string|int, mixed> $value) : array<string|int, mixed>

See Karatsuba algorithm and MPM 5.3.4.

Parameters
$value : array<string|int, mixed>
Return values
array<string|int, mixed>

lshift()

Logical Left Shift

protected lshift(int $shift) : mixed

Shifts BigInteger's by $shift bits.

Parameters
$shift : int

make_odd()

Make the current number odd

protected make_odd() : mixed

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

Tags
see
self::randomPrime()

maxHelper()

Return the minimum BigInteger between an arbitrary number of BigIntegers.

protected static maxHelper(array<string|int, mixed> $nums) : Engine
Parameters
$nums : array<string|int, mixed>
Return values
Engine

minHelper()

Return the minimum BigInteger between an arbitrary number of BigIntegers.

protected static minHelper(array<string|int, mixed> $nums) : Engine
Parameters
$nums : array<string|int, mixed>
Return values
Engine

modInverseHelper()

Calculates modular inverses.

protected modInverseHelper(Engine $n) : Engine|false

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

Parameters
$n : Engine
Return values
Engine|false

multiplyHelper()

Performs multiplication.

protected static multiplyHelper(array<string|int, mixed> $x_value, bool $x_negative, array<string|int, mixed> $y_value, bool $y_negative) : array<string|int, mixed>
Parameters
$x_value : array<string|int, mixed>
$x_negative : bool
$y_value : array<string|int, mixed>
$y_negative : bool
Return values
array<string|int, mixed>

normalize()

Normalize

protected normalize(PHP $result) : PHP

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

Parameters
$result : PHP
Return values
PHP

pad()

Pads strings so that unpack may be used on them

protected pad(string $str) : string
Parameters
$str : string
Return values
string

powHelper()

Performs exponentiation.

protected powHelper(PHP $n) : PHP
Parameters
$n : PHP
Return values
PHP

powModInner()

Performs modular exponentiation.

protected powModInner(PHP $e, PHP $n) : PHP
Parameters
$e : PHP
$n : PHP
Return values
PHP

randomRangeHelper()

Generate a random number between a range

protected static randomRangeHelper(Engine $min, Engine $max) : 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)

Parameters
$min : Engine
$max : Engine
Return values
Engine

randomRangePrimeOuter()

Performs some pre-processing for randomRangePrime

protected static randomRangePrimeOuter(Engine $min, Engine $max) : bool|Engine
Parameters
$min : Engine
$max : Engine
Return values
bool|Engine

regularMultiply()

Performs long multiplication on two BigIntegers

protected static regularMultiply(array<string|int, mixed> $x_value, array<string|int, mixed> $y_value) : array<string|int, mixed>

Modeled after 'multiply' in MutableBigInteger.java.

Parameters
$x_value : array<string|int, mixed>
$y_value : array<string|int, mixed>
Return values
array<string|int, mixed>

rootHelper()

Performs a few preliminary checks on root

protected rootHelper(int $n) : Engine
Parameters
$n : int
Return values
Engine

rootInner()

Calculates the nth root of a biginteger.

protected rootInner(int $n) : Engine

Returns the nth root of a positive biginteger, where n defaults to 2

Parameters
$n : int
Return values
Engine

rshift()

Logical Right Shift

protected rshift(int $shift) : mixed

Shifts BigInteger's by $shift bits.

Parameters
$shift : int

setBitmask()

Set Bitmask

protected static setBitmask(int $bits) : Engine
Parameters
$bits : int
Tags
see
self::setPrecision()
Return values
Engine

setupIsPrime()

Sets the $t parameter for primality testing

protected setupIsPrime() : int
Return values
int

slidingWindow()

Sliding Window k-ary Modular Exponentiation

protected static slidingWindow(Engine $x, Engine $e, Engine $n, string $class) : Engine

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.

Parameters
$x : Engine
$e : Engine
$n : Engine
$class : string
Return values
Engine

square()

Performs squaring

protected static square(array<string|int, mixed> $x) : array<string|int, mixed>
Parameters
$x : array<string|int, mixed>
Return values
array<string|int, mixed>

testSmallPrimes()

Test the number against small primes.

protected testSmallPrimes() : mixed
Tags
see
self::isPrime()

toBytesHelper()

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

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

Return values
string

trim()

Trim

protected static trim(array<string|int, mixed> $value) : PHP

Removes leading zeros

Parameters
$value : array<string|int, mixed>
Return values
PHP

bitwise_small_split()

Bitwise Split where $split < static::BASE

private bitwise_small_split(int $split) : array<string|int, PHP>
Parameters
$split : int
Return values
array<string|int, PHP>

divide_digit()

Divides a BigInteger by a regular integer

private static divide_digit(array<string|int, mixed> $dividend, int $divisor) : array<string|int, mixed>

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

Parameters
$dividend : array<string|int, mixed>
$divisor : int
Return values
array<string|int, mixed>

int2bytes()

Converts 32-bit integers to bytes.

private static int2bytes(int $x) : string
Parameters
$x : int
Return values
string

karatsuba()

Performs Karatsuba multiplication on two BigIntegers

private static karatsuba(array<string|int, mixed> $x_value, array<string|int, mixed> $y_value) : array<string|int, mixed>

See Karatsuba algorithm and MPM 5.2.3.

Parameters
$x_value : array<string|int, mixed>
$y_value : array<string|int, mixed>
Return values
array<string|int, mixed>

safe_divide()

Single digit division

private static safe_divide(int $x, int $y) : int

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.

Parameters
$x : int
$y : int
Return values
int

        
On this page

Search results