phpseclib API Documentation
Class

Math_BigInteger

class Math_BigInteger

Pure-PHP arbitrary precision integer arithmetic library.

Supports base-2, base-10, base-16, and base-256 numbers.

Properties

array $value Holds the BigInteger's value.
bool $is_negative Holds the BigInteger's magnitude.
$precision Precision
$bitmask Precision Bitmask
string $hex Mode independent value used for serialization.

Methods

Math_BigInteger __construct($x $x, int $base = 10)

Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.

Math_BigInteger($x $x, int $base = 10)

PHP4 compatible Default Constructor.

string toBytes(bool $twos_compliment = false)

Converts a BigInteger to a byte string (eg.

string toHex(bool $twos_compliment = false)

Converts a BigInteger to a hex string (eg.

string toBits(bool $twos_compliment = false)

Converts a BigInteger to a bit string (eg.

string toString()

Converts a BigInteger to a base-10 number.

Math_BigInteger copy()

Copy an object

__toString()

__toString() magic method

Math_BigInteger __clone()

__clone() magic method

__sleep()

__sleep() magic method

__wakeup()

__wakeup() magic method

__debugInfo()

__debugInfo() magic method

Math_BigInteger add(Math_BigInteger $y)

Adds two BigIntegers.

array _add(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Performs addition.

Math_BigInteger subtract(Math_BigInteger $y)

Subtracts two BigIntegers.

array _subtract(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Performs subtraction.

Math_BigInteger multiply(Math_BigInteger $x)

Multiplies two BigIntegers

array _multiply(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Performs multiplication.

array _regularMultiply(array $x_value, array $y_value)

Performs long multiplication on two BigIntegers

array _karatsuba(array $x_value, array $y_value)

Performs Karatsuba multiplication on two BigIntegers

array _square(array $x = false)

Performs squaring

array _baseSquare(array $value)

Performs traditional squaring on two BigIntegers

array _karatsubaSquare(array $value)

Performs Karatsuba "squaring" on two BigIntegers

array divide(Math_BigInteger $y)

Divides two BigIntegers.

array _divide_digit(array $dividend, array $divisor)

Divides a BigInteger by a regular integer

Math_BigInteger modPow(Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Math_BigInteger powMod(Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Math_BigInteger _slidingWindow(Math_BigInteger $e, Math_BigInteger $n, int $mode)

Sliding Window k-ary Modular Exponentiation

array _reduce(array $x, array $n, int $mode)

Modular reduction

array _prepareReduce(array $x, array $n, int $mode)

Modular reduction preperation

array _multiplyReduce(array $x, array $y, array $n, int $mode)

Modular multiply

array _squareReduce(array $x, array $n, int $mode)

Modular square

Math_BigInteger _mod2(Math_BigInteger $n)

Modulos for Powers of Two

array _barrett(array $n, array $m)

Barrett Modular Reduction

array _regularBarrett(array $x, array $n)

(Regular) Barrett Modular Reduction

array _multiplyLower(array $x_value, bool $x_negative, array $y_value, bool $y_negative, int $stop)

Performs long multiplication up to $stop digits

array _montgomery(array $x, array $n)

Montgomery Modular Reduction

array _montgomeryMultiply(array $x, array $y, array $m)

Montgomery Multiply

array _prepMontgomery(array $x, array $n)

Prepare a number for use in Montgomery Modular Reductions

int _modInverse67108864(array $x)

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

Math_BigInteger|false modInverse(Math_BigInteger $n)

Calculates modular inverses.

Math_BigInteger extendedGCD(Math_BigInteger $n)

Calculates the greatest common divisor and Bezout's identity.

Math_BigInteger gcd(Math_BigInteger $n)

Calculates the greatest common divisor

Math_BigInteger abs()

Absolute value.

int compare(Math_BigInteger $y)

Compares two numbers.

int _compare(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Compares two numbers.

bool equals(Math_BigInteger $x)

Tests the equality of two numbers.

setPrecision(int $bits)

Set Precision

Math_BigInteger bitwise_and(Math_BigInteger $x)

Logical And

Math_BigInteger bitwise_or(Math_BigInteger $x)

Logical Or

Math_BigInteger bitwise_xor(Math_BigInteger $x)

Logical Exclusive-Or

Math_BigInteger bitwise_not()

Logical Not

Math_BigInteger bitwise_rightShift(int $shift)

Logical Right Shift

Math_BigInteger bitwise_leftShift(int $shift)

Logical Left Shift

Math_BigInteger bitwise_leftRotate(int $shift)

Logical Left Rotate

Math_BigInteger bitwise_rightRotate(int $shift)

Logical Right Rotate

setRandomGenerator(string $generator)

Set random number generator function

Math_BigInteger _random_number_helper($size)

Generates a random BigInteger

Math_BigInteger random(Math_BigInteger $arg1, Math_BigInteger $arg2 = false)

Generate a random number

Math_BigInteger|false randomPrime(Math_BigInteger $arg1, Math_BigInteger $arg2 = false, int $timeout = false)

Generate a random prime number.

_make_odd()

Make the current number odd

bool isPrime(Math_BigInteger $t = false)

Checks a numer to see if it's prime

_lshift(int $shift)

Logical Left Shift

_rshift(int $shift)

Logical Right Shift

Math_BigInteger _normalize(Math_BigInteger $result)

Normalize

Math_BigInteger _trim(array $value)

Trim

array _array_repeat($input $input, $multiplier $multiplier)

Array Repeat

string _base256_lshift($x $x, $shift $shift)

Logical Left Shift

string _base256_rshift($x $x, $shift $shift)

Logical Right Shift

string _int2bytes(int $x)

Converts 32-bit integers to bytes.

int _bytes2int(string $x)

Converts bytes to 32-bit integers

string _encodeASN1Length(int $length)

DER-encode an integer

int _safe_divide(int $x, int $y)

Single digit division

Details

at line 245
public Math_BigInteger __construct($x $x, int $base = 10)

Converts base-2, base-10, base-16, and binary strings (base-256) to BigIntegers.

If the second parameter - $base - is negative, then it will be assumed that the number's are encoded using two's compliment. The sole exception to this is -10, which is treated the same as 10 is.

Here's an example: <?php include 'Math/BigInteger.php';

$a = new Math_BigInteger('0x32', 16); // 50 in base-16

echo $a->toString(); // outputs 50 ?>

Parameters

$x $x base-10 number or base-$base number if $base set.
int $base

Return Value

Math_BigInteger

at line 517
public Math_BigInteger($x $x, int $base = 10)

PHP4 compatible Default Constructor.

Parameters

$x $x base-10 number or base-$base number if $base set.
int $base

See also

self::__construct()

at line 544
public string toBytes(bool $twos_compliment = false)

Converts a BigInteger to a byte string (eg.

base-256).

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

Here's an example: <?php include 'Math/BigInteger.php';

$a = new Math_BigInteger('65');

echo $a->toBytes(); // outputs chr(65) ?>

Parameters

bool $twos_compliment

Return Value

string

at line 645
public string toHex(bool $twos_compliment = false)

Converts a BigInteger to a hex string (eg.

base-16)).

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

Here's an example: <?php include 'Math/BigInteger.php';

$a = new Math_BigInteger('65');

echo $a->toHex(); // outputs '41' ?>

Parameters

bool $twos_compliment

Return Value

string

at line 672
public string toBits(bool $twos_compliment = false)

Converts a BigInteger to a bit string (eg.

base-2).

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

Here's an example: <?php include 'Math/BigInteger.php';

$a = new Math_BigInteger('65');

echo $a->toBits(); // outputs '1000001' ?>

Parameters

bool $twos_compliment

Return Value

string

at line 709
public string toString()

Converts a BigInteger to a base-10 number.

Here's an example: <?php include 'Math/BigInteger.php';

$a = new Math_BigInteger('50');

echo $a->toString(); // outputs 50 ?>

Return Value

string

at line 761
public Math_BigInteger copy()

Copy an object

PHP5 passes objects by reference while PHP4 passes by value. As such, we need a function to guarantee that all objects are passed by value, when appropriate. More information can be found here:

{@link http://php.net/language.oop5.basic#51624}

Return Value

Math_BigInteger

See also

self::__clone()

at line 780
public __toString()

__toString() magic method

Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call toString().

at line 797
public Math_BigInteger __clone()

__clone() magic method

Although you can call MathBigInteger::toString() directly in PHP5, you cannot call Math_BigInteger::clone() directly in PHP5. You can in PHP4 since it's not a magic method, but in PHP5, you have to call it by using the PHP5 only syntax of $y = clone $x. As such, if you're trying to write an application that works on both PHP4 and PHP5, call MathBigInteger::copy(), instead.

Return Value

Math_BigInteger

See also

self::copy()

at line 810
public __sleep()

__sleep() magic method

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

See also

self::__wakeup()

at line 828
public __wakeup()

__wakeup() magic method

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

See also

self::__sleep()

at line 846
public __debugInfo()

__debugInfo() magic method

Will be called, automatically, when printr() or vardump() are called

at line 894
public Math_BigInteger add(Math_BigInteger $y)

Adds two BigIntegers.

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger('10'); $b = new MathBigInteger('20');

$c = $a->add($b);

echo $c->toString(); // outputs 30 ?>

Parameters

Math_BigInteger $y

Return Value

Math_BigInteger

at line 928
public array _add(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Performs addition.

Parameters

array $x_value
bool $x_negative
array $y_value
bool $y_negative

Return Value

array

at line 1025
public Math_BigInteger subtract(Math_BigInteger $y)

Subtracts two BigIntegers.

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger('10'); $b = new MathBigInteger('20');

$c = $a->subtract($b);

echo $c->toString(); // outputs -10 ?>

Parameters

Math_BigInteger $y

Return Value

Math_BigInteger

at line 1059
public array _subtract(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Performs subtraction.

Parameters

array $x_value
bool $x_negative
array $y_value
bool $y_negative

Return Value

array

at line 1160
public Math_BigInteger multiply(Math_BigInteger $x)

Multiplies two BigIntegers

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger('10'); $b = new MathBigInteger('20');

$c = $a->multiply($b);

echo $c->toString(); // outputs 200 ?>

Parameters

Math_BigInteger $x

Return Value

Math_BigInteger

at line 1194
public array _multiply(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Performs multiplication.

Parameters

array $x_value
bool $x_negative
array $y_value
bool $y_negative

Return Value

array

at line 1231
public array _regularMultiply(array $x_value, array $y_value)

Performs long multiplication on two BigIntegers

Modeled after 'multiply' in MutableBigInteger.java.

Parameters

array $x_value
array $y_value

Return Value

array

at line 1295
public array _karatsuba(array $x_value, array $y_value)

Performs Karatsuba multiplication on two BigIntegers

See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and {@link http://math.libtomcrypt.com/files/tommath.pdf#page=120 MPM 5.2.3}.

Parameters

array $x_value
array $y_value

Return Value

array

at line 1333
public array _square(array $x = false)

Performs squaring

Parameters

array $x

Return Value

array

at line 1351
public array _baseSquare(array $value)

Performs traditional squaring on two BigIntegers

Squaring can be done faster than multiplying a number by itself can be. See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=7 HAC 14.2.4} / {@link http://math.libtomcrypt.com/files/tommath.pdf#page=141 MPM 5.3} for more information.

Parameters

array $value

Return Value

array

at line 1390
public array _karatsubaSquare(array $value)

Performs Karatsuba "squaring" on two BigIntegers

See {@link http://en.wikipedia.org/wiki/Karatsuba_algorithm Karatsuba algorithm} and {@link http://math.libtomcrypt.com/files/tommath.pdf#page=151 MPM 5.3.4}.

Parameters

array $value

Return Value

array

at line 1447
public array divide(Math_BigInteger $y)

Divides two BigIntegers.

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

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger('10'); $b = new MathBigInteger('20');

list($quotient, $remainder) = $a->divide($b);

echo $quotient->toString(); // outputs 0 echo "\r\n"; echo $remainder->toString(); // outputs 10 ?>

Parameters

Math_BigInteger $y

Return Value

array

at line 1630
public array _divide_digit(array $dividend, array $divisor)

Divides a BigInteger by a regular integer

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

Parameters

array $dividend
array $divisor

Return Value

array

at line 1686
public Math_BigInteger modPow(Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger('10'); $b = new MathBigInteger('20'); $c = new Math_BigInteger('30');

$c = $a->modPow($b, $c);

echo $c->toString(); // outputs 10 ?>

Parameters

Math_BigInteger $e
Math_BigInteger $n

Return Value

Math_BigInteger

at line 1837
public Math_BigInteger powMod(Math_BigInteger $e, Math_BigInteger $n)

Performs modular exponentiation.

Alias for Math_BigInteger::modPow()

Parameters

Math_BigInteger $e
Math_BigInteger $n

Return Value

Math_BigInteger

at line 1856
public Math_BigInteger _slidingWindow(Math_BigInteger $e, Math_BigInteger $n, int $mode)

Sliding Window k-ary Modular Exponentiation

Based on {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=27 HAC 14.85} / {@link http://math.libtomcrypt.com/files/tommath.pdf#page=210 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

Math_BigInteger $e
Math_BigInteger $n
int $mode

Return Value

Math_BigInteger

at line 1933
public array _reduce(array $x, array $n, int $mode)

Modular reduction

For most $modes this will return the remainder.

Parameters

array $x
array $n
int $mode

Return Value

array

See also

self::_slidingWindow()

at line 1970
public array _prepareReduce(array $x, array $n, int $mode)

Modular reduction preperation

Parameters

array $x
array $n
int $mode

Return Value

array

See also

self::_slidingWindow()

at line 1989
public array _multiplyReduce(array $x, array $y, array $n, int $mode)

Modular multiply

Parameters

array $x
array $y
array $n
int $mode

Return Value

array

See also

self::_slidingWindow()

at line 2008
public array _squareReduce(array $x, array $n, int $mode)

Modular square

Parameters

array $x
array $n
int $mode

Return Value

array

See also

self::_slidingWindow()

at line 2027
public Math_BigInteger _mod2(Math_BigInteger $n)

Modulos for Powers of Two

Calculates $x%$n, where $n = 2**$e, for some $e. Since this is basically the same as doing $x & ($n-1), we'll just use this function as a wrapper for doing that.

Parameters

Math_BigInteger $n

Return Value

Math_BigInteger

See also

self::_slidingWindow()

at line 2058
public array _barrett(array $n, array $m)

Barrett Modular Reduction

See {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=14 HAC 14.3.3} / {@link http://math.libtomcrypt.com/files/tommath.pdf#page=165 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 {@link http://www.cosic.esat.kuleuven.be/publications/thesis-149.pdf#page=66 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 {@link http://math.libtomcrypt.com/files/tommath.pdf#page=162 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.

Parameters

array $n
array $m

Return Value

array

See also

self::_slidingWindow()

at line 2155
public array _regularBarrett(array $x, array $n)

(Regular) Barrett Modular Reduction

For numbers with more than four digits MathBigInteger::barrett() is faster. The difference between that and this is that this function does not fold the denominator into a smaller form.

Parameters

array $x
array $n

Return Value

array

See also

self::_slidingWindow()

at line 2229
public array _multiplyLower(array $x_value, bool $x_negative, array $y_value, bool $y_negative, int $stop)

Performs long multiplication up to $stop digits

If you're going to be doing array_slice($product->value, 0, $stop), some cycles can be saved.

Parameters

array $x_value
bool $x_negative
array $y_value
bool $y_negative
int $stop

Return Value

array

See also

self::_regularBarrett()

at line 2308
public array _montgomery(array $x, array $n)

Montgomery Modular Reduction

($x->prepMontgomery($n))->montgomery($n) yields $x % $n. {@link http://math.libtomcrypt.com/files/tommath.pdf#page=170 MPM 6.3} provides insights on how this can be improved upon (basically, by using the comba method). gcd($n, 2) must be equal to one for this function to work correctly.

Parameters

array $x
array $n

Return Value

array

See also

self::_prepMontgomery()
self::_slidingWindow()

at line 2356
public array _montgomeryMultiply(array $x, array $y, array $m)

Montgomery Multiply

Interleaves the montgomery reduction and long multiplication algorithms together as described in {@link http://www.cacr.math.uwaterloo.ca/hac/about/chap14.pdf#page=13 HAC 14.36}

Parameters

array $x
array $y
array $m

Return Value

array

See also

self::_prepMontgomery()
self::_montgomery()

at line 2407
public array _prepMontgomery(array $x, array $n)

Prepare a number for use in Montgomery Modular Reductions

Parameters

array $x
array $n

Return Value

array

See also

self::_montgomery()
self::_slidingWindow()

at line 2444
public int _modInverse67108864(array $x)

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

67108864)

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

{@link http://www-cs-students.stanford.edu/~tjw/jsbn/jsbn.js}

The following URL provides more info:

{@link 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!

Parameters

array $x

Return Value

int

See also

self::_montgomery()

at line 2484
public Math_BigInteger|false modInverse(Math_BigInteger $n)

Calculates modular inverses.

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

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger(30); $b = new MathBigInteger(17);

$c = $a->modInverse($b); echo $c->toString(); // outputs 4

echo "\r\n";

$d = $a->multiply($c); list(, $d) = $d->divide($b); echo $d; // outputs 1 (as per the definition of modular inverse) ?>

Parameters

Math_BigInteger $n

Return Value

Math_BigInteger|false

at line 2550
public Math_BigInteger extendedGCD(Math_BigInteger $n)

Calculates the greatest common divisor and Bezout's identity.

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 {@link http://en.wikipedia.org/wiki/B%C3%A9zout%27s_identity Bezout's identity - Wikipedia} for more information.

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger(693); $b = new MathBigInteger(609);

extract($a->extendedGCD($b));

echo $gcd->toString() . "\r\n"; // outputs 21 echo $a->toString() * $x->toString() + $b->toString() * $y->toString(); // outputs 21 ?>

Parameters

Math_BigInteger $n

Return Value

Math_BigInteger

at line 2681
public Math_BigInteger gcd(Math_BigInteger $n)

Calculates the greatest common divisor

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

Here's an example: <?php include 'Math/BigInteger.php';

$a = new MathBigInteger(693); $b = new MathBigInteger(609);

$gcd = a->extendedGCD($b);

echo $gcd->toString() . "\r\n"; // outputs 21 ?>

Parameters

Math_BigInteger $n

Return Value

Math_BigInteger

at line 2693
public Math_BigInteger abs()

Absolute value.

Return Value

Math_BigInteger

at line 2729
public int compare(Math_BigInteger $y)

Compares two numbers.

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

Math_BigInteger $y

Return Value

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

See also

self::equals()

at line 2759
public int _compare(array $x_value, bool $x_negative, array $y_value, bool $y_negative)

Compares two numbers.

Parameters

array $x_value
bool $x_negative
array $y_value
bool $y_negative

Return Value

int

See also

self::compare()

at line 2794
public bool equals(Math_BigInteger $x)

Tests the equality of two numbers.

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

Parameters

Math_BigInteger $x

Return Value

bool

See also

self::compare()

at line 2813
public setPrecision(int $bits)

Set Precision

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

Parameters

int $bits

at line 2834
public Math_BigInteger bitwise_and(Math_BigInteger $x)

Logical And

Parameters

Math_BigInteger $x

Return Value

Math_BigInteger

at line 2875
public Math_BigInteger bitwise_or(Math_BigInteger $x)

Logical Or

Parameters

Math_BigInteger $x

Return Value

Math_BigInteger

at line 2915
public Math_BigInteger bitwise_xor(Math_BigInteger $x)

Logical Exclusive-Or

Parameters

Math_BigInteger $x

Return Value

Math_BigInteger

at line 2955
public Math_BigInteger bitwise_not()

Logical Not

Return Value

Math_BigInteger

at line 2997
public Math_BigInteger bitwise_rightShift(int $shift)

Logical Right Shift

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

Parameters

int $shift

Return Value

Math_BigInteger

at line 3035
public Math_BigInteger bitwise_leftShift(int $shift)

Logical Left Shift

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

Parameters

int $shift

Return Value

Math_BigInteger

at line 3072
public Math_BigInteger bitwise_leftRotate(int $shift)

Logical Left Rotate

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

Parameters

int $shift

Return Value

Math_BigInteger

at line 3117
public Math_BigInteger bitwise_rightRotate(int $shift)

Logical Right Rotate

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

Parameters

int $shift

Return Value

Math_BigInteger

at line 3130
public setRandomGenerator(string $generator)

Set random number generator function

This function is deprecated.

Parameters

string $generator

at line 3143
public Math_BigInteger _random_number_helper($size)

Generates a random BigInteger

Byte length is equal to $length. Uses cryptrandom if it's loaded and mtrand if it's not.

Parameters

$size

Return Value

Math_BigInteger

at line 3180
public Math_BigInteger random(Math_BigInteger $arg1, Math_BigInteger $arg2 = false)

Generate a random number

Returns a random number between $min and $max where $min and $max can be defined using one of the two methods:

$min->random($max) $max->random($min)

Parameters

Math_BigInteger $arg1
Math_BigInteger $arg2

Return Value

Math_BigInteger

at line 3261
public Math_BigInteger|false randomPrime(Math_BigInteger $arg1, Math_BigInteger $arg2 = false, int $timeout = false)

Generate a random prime number.

If there's not a prime within the given range, false will be returned. If more than $timeout seconds have elapsed, give up and return false.

Parameters

Math_BigInteger $arg1
Math_BigInteger $arg2
int $timeout

Return Value

Math_BigInteger|false

at line 3361
public _make_odd()

Make the current number odd

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

See also

self::randomPrime()

at line 3391
public bool isPrime(Math_BigInteger $t = false)

Checks a numer to see if it's prime

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. Math_BigInteger::randomPrime() can be distributed across multiple pageloads on a website instead of just one.

Parameters

Math_BigInteger $t

Return Value

bool

at line 3540
public _lshift(int $shift)

Logical Left Shift

Shifts BigInteger's by $shift bits.

Parameters

int $shift

at line 3575
public _rshift(int $shift)

Logical Right Shift

Shifts BigInteger's by $shift bits.

Parameters

int $shift

at line 3611
public Math_BigInteger _normalize(Math_BigInteger $result)

Normalize

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

Parameters

Math_BigInteger $result

Return Value

Math_BigInteger

See also

self::_trim()

at line 3668
public Math_BigInteger _trim(array $value)

Trim

Removes leading zeros

Parameters

array $value

Return Value

Math_BigInteger

at line 3688
public array _array_repeat($input $input, $multiplier $multiplier)

Array Repeat

Parameters

$input $input Array
$multiplier $multiplier mixed

Return Value

array

at line 3703
public string _base256_lshift($x $x, $shift $shift)

Logical Left Shift

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

Parameters

$x $x String
$shift $shift Integer

Return Value

string

at line 3732
public string _base256_rshift($x $x, $shift $shift)

Logical Right Shift

Shifts binary strings $shift bits, essentially dividing by 2**$shift and returning the remainder.

Parameters

$x $x String
$shift $shift Integer

Return Value

string

at line 3773
public string _int2bytes(int $x)

Converts 32-bit integers to bytes.

Parameters

int $x

Return Value

string

at line 3785
public int _bytes2int(string $x)

Converts bytes to 32-bit integers

Parameters

string $x

Return Value

int

at line 3801
public string _encodeASN1Length(int $length)

DER-encode an integer

The ability to DER-encode integers is needed to create RSA public keys for use with OpenSSL

Parameters

int $length

Return Value

string

See also

self::modPow()

at line 3824
public int _safe_divide(int $x, int $y)

Single digit division

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

int $x
int $y

Return Value

int