SecureLogin

Math_BigInteger Class Reference

Public Member Functions

 Math_BigInteger ($x=0, $base=10)
 toBytes ($twos_compliment=false)
 toHex ($twos_compliment=false)
 toBits ($twos_compliment=false)
 toString ()
 copy ()
 __toString ()
 __clone ()
 __sleep ()
 __wakeup ()
 add ($y)
 _add ($x_value, $x_negative, $y_value, $y_negative)
 subtract ($y)
 _subtract ($x_value, $x_negative, $y_value, $y_negative)
 multiply ($x)
 _multiply ($x_value, $x_negative, $y_value, $y_negative)
 _regularMultiply ($x_value, $y_value)
 _karatsuba ($x_value, $y_value)
 _square ($x=false)
 _baseSquare ($value)
 _karatsubaSquare ($value)
 divide ($y)
 _divide_digit ($dividend, $divisor)
 modPow ($e, $n)
 powMod ($e, $n)
 _slidingWindow ($e, $n, $mode)
 _reduce ($x, $n, $mode)
 _prepareReduce ($x, $n, $mode)
 _multiplyReduce ($x, $y, $n, $mode)
 _squareReduce ($x, $n, $mode)
 _mod2 ($n)
 _barrett ($n, $m)
 _regularBarrett ($x, $n)
 _multiplyLower ($x_value, $x_negative, $y_value, $y_negative, $stop)
 _montgomery ($x, $n)
 _montgomeryMultiply ($x, $y, $m)
 _prepMontgomery ($x, $n)
 _modInverse67108864 ($x)
 modInverse ($n)
 extendedGCD ($n)
 gcd ($n)
 abs ()
 compare ($y)
 _compare ($x_value, $x_negative, $y_value, $y_negative)
 equals ($x)
 setPrecision ($bits)
 bitwise_and ($x)
 bitwise_or ($x)
 bitwise_xor ($x)
 bitwise_not ()
 bitwise_rightShift ($shift)
 bitwise_leftShift ($shift)
 bitwise_leftRotate ($shift)
 bitwise_rightRotate ($shift)
 setRandomGenerator ($generator)
 random ($min=false, $max=false)
 randomPrime ($min=false, $max=false, $timeout=false)
 _make_odd ()
 isPrime ($t=false)
 _lshift ($shift)
 _rshift ($shift)
 _normalize ($result)
 _trim ($value)
 _array_repeat ($input, $multiplier)
 _base256_lshift (&$x, $shift)
 _base256_rshift (&$x, $shift)
 _int2bytes ($x)
 _bytes2int ($x)

Data Fields

 $value
 $is_negative = false
 $generator = 'mt_rand'
 $precision = -1
 $bitmask = false
 $hex

Member Function Documentation

__clone ( )

__clone() magic method

Although you can call Math_BigInteger::__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 Math_BigInteger::copy(), instead.

public

See also:
copy()
Returns:
Math_BigInteger
__sleep ( )

__sleep() magic method

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

See also:
__wakeup() 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().

public

__wakeup ( )

__wakeup() magic method

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

See also:
__sleep() public
_add ( x_value,
x_negative,
y_value,
y_negative 
)

Performs addition.

Parameters:
Array$x_value
Boolean$x_negative
Array$y_value
Boolean$y_negative
Returns:
Array private
_array_repeat ( input,
multiplier 
)

Array Repeat

Parameters:
$inputArray
$multipliermixed
Returns:
Array private
_barrett ( n,
m 
)

Barrett Modular Reduction

See HAC 14.3.3 / 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 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 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.

See also:
_slidingWindow() private
Parameters:
Array$n
Array$m
Returns:
Array
_base256_lshift ( &$  x,
shift 
)

Logical Left Shift

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

Parameters:
$xString
$shiftInteger
Returns:
String private
_base256_rshift ( &$  x,
shift 
)

Logical Right Shift

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

Parameters:
$xString
$shiftInteger
Returns:
String private
_baseSquare ( value)

Performs traditional squaring on two BigIntegers

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:
Array$value
Returns:
Array private
_bytes2int ( x)

Converts bytes to 32-bit integers

Parameters:
String$x
Returns:
Integer private
_compare ( x_value,
x_negative,
y_value,
y_negative 
)

Compares two numbers.

Parameters:
Array$x_value
Boolean$x_negative
Array$y_value
Boolean$y_negative
Returns:
Integer
See also:
compare() private
_divide_digit ( dividend,
divisor 
)

Divides a BigInteger by a regular integer

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

Parameters:
Array$dividend
Array$divisor
Returns:
Array private
_int2bytes ( x)

Converts 32-bit integers to bytes.

Parameters:
Integer$x
Returns:
String private
_karatsuba ( x_value,
y_value 
)

Performs Karatsuba multiplication on two BigIntegers

See Karatsuba algorithm and MPM 5.2.3.

Parameters:
Array$x_value
Array$y_value
Returns:
Array private
_karatsubaSquare ( value)

Performs Karatsuba "squaring" on two BigIntegers

See Karatsuba algorithm and MPM 5.3.4.

Parameters:
Array$value
Returns:
Array private
_lshift ( shift)

Logical Left Shift

Shifts BigInteger's by $shift bits.

Parameters:
Integer$shiftprivate
_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:
randomPrime() private
_mod2 ( 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.

See also:
_slidingWindow() private
Parameters:
Math_BigInteger
Returns:
Math_BigInteger
_modInverse67108864 ( x)

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

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

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

The following URL provides more info:

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

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

Thanks to Pedro Gimeno Fortea for input!

See also:
_montgomery() private
Parameters:
Array$x
Returns:
Integer
_montgomery ( x,
n 
)

Montgomery Modular Reduction

($x->_prepMontgomery($n))->_montgomery($n) yields $x % $n. 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.

See also:
_prepMontgomery()
_slidingWindow() private
Parameters:
Array$x
Array$n
Returns:
Array
_montgomeryMultiply ( x,
y,
m 
)

Montgomery Multiply

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

See also:
_prepMontgomery()
_montgomery() private
Parameters:
Array$x
Array$y
Array$m
Returns:
Array
_multiply ( x_value,
x_negative,
y_value,
y_negative 
)

Performs multiplication.

Parameters:
Array$x_value
Boolean$x_negative
Array$y_value
Boolean$y_negative
Returns:
Array private
_multiplyLower ( x_value,
x_negative,
y_value,
y_negative,
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.

See also:
_regularBarrett()
Parameters:
Array$x_value
Boolean$x_negative
Array$y_value
Boolean$y_negative
Returns:
Array private
_multiplyReduce ( x,
y,
n,
mode 
)

Modular multiply

See also:
_slidingWindow() private
Parameters:
Array$x
Array$y
Array$n
Integer$mode
Returns:
Array
_normalize ( result)

Normalize

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

Parameters:
Math_BigInteger
Returns:
Math_BigInteger
See also:
_trim() private
_prepareReduce ( x,
n,
mode 
)

Modular reduction preperation

See also:
_slidingWindow() private
Parameters:
Array$x
Array$n
Integer$mode
Returns:
Array
_prepMontgomery ( x,
n 
)

Prepare a number for use in Montgomery Modular Reductions

See also:
_montgomery()
_slidingWindow() private
Parameters:
Array$x
Array$n
Returns:
Array
_reduce ( x,
n,
mode 
)

Modular reduction

For most $modes this will return the remainder.

See also:
_slidingWindow() private
Parameters:
Array$x
Array$n
Integer$mode
Returns:
Array
_regularBarrett ( x,
n 
)

(Regular) Barrett Modular Reduction

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

See also:
_slidingWindow() private
Parameters:
Array$x
Array$n
Returns:
Array
_regularMultiply ( x_value,
y_value 
)

Performs long multiplication on two BigIntegers

Modeled after 'multiply' in MutableBigInteger.java.

Parameters:
Array$x_value
Array$y_value
Returns:
Array private
_rshift ( shift)

Logical Right Shift

Shifts BigInteger's by $shift bits.

Parameters:
Integer$shiftprivate
_slidingWindow ( e,
n,
mode 
)

Sliding Window k-ary Modular Exponentiation

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:
Math_BigInteger$e
Math_BigInteger$n
Integer$mode
Returns:
Math_BigInteger private
_square ( x = false)

Performs squaring

Parameters:
Array$x
Returns:
Array private
_squareReduce ( x,
n,
mode 
)

Modular square

See also:
_slidingWindow() private
Parameters:
Array$x
Array$n
Integer$mode
Returns:
Array
_subtract ( x_value,
x_negative,
y_value,
y_negative 
)

Performs subtraction.

Parameters:
Array$x_value
Boolean$x_negative
Array$y_value
Boolean$y_negative
Returns:
Array private
_trim ( value)

Trim

Removes leading zeros

Returns:
Math_BigInteger private
abs ( )

Absolute value.

Returns:
Math_BigInteger public
add ( y)

Adds two BigIntegers.

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

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

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

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

Parameters:
Math_BigInteger$y
Returns:
Math_BigInteger public
bitwise_and ( x)

Logical And

Parameters:
Math_BigInteger$xpublic
bitwise_leftRotate ( shift)

Logical Left Rotate

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

Parameters:
Integer$shift
Returns:
Math_BigInteger public
bitwise_leftShift ( shift)

Logical Left Shift

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

Parameters:
Integer$shift
Returns:
Math_BigInteger public
bitwise_not ( )

Logical Not

public

bitwise_or ( x)

Logical Or

Parameters:
Math_BigInteger$xpublic
bitwise_rightRotate ( shift)

Logical Right Rotate

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

Parameters:
Integer$shift
Returns:
Math_BigInteger public
bitwise_rightShift ( shift)

Logical Right Shift

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

Parameters:
Integer$shift
Returns:
Math_BigInteger public
bitwise_xor ( x)

Logical Exclusive-Or

Parameters:
Math_BigInteger$xpublic
compare ( 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$x
Returns:
Integer < 0 if $this is less than $x; > 0 if $this is greater than $x, and 0 if they are equal. public
See also:
equals()
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:

http://php.net/language.oop5.basic#51624

public

See also:
__clone()
Returns:
Math_BigInteger
divide ( 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 Math_BigInteger('10'); $b = new Math_BigInteger('20');

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

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

Parameters:
Math_BigInteger$y
Returns:
Array public
equals ( 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
Returns:
Boolean public
See also:
compare()
extendedGCD ( n)

Calculates the greatest common divisor and Bézout's identity.

Say you have 693 and 609. The GCD is 21. Bézout's identity states that there exist integers x and y such that 693*x + 609*y == 21. In point of fact, there are actually an infinite number of x and y combinations and which combination is returned is dependant upon which mode is in use. See Bézout's identity - Wikipedia for more information.

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

$a = new Math_BigInteger(693); $b = new Math_BigInteger(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
Returns:
Math_BigInteger public
gcd ( 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 Math_BigInteger(693); $b = new Math_BigInteger(609);

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

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

Parameters:
Math_BigInteger$n
Returns:
Math_BigInteger public
isPrime ( 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 accross multiple pageloads on a website instead of just one.

Parameters:
optionalInteger $t
Returns:
Boolean public
Math_BigInteger ( x = 0,
base = 10 
)

Converts base-2, base-10, base-16, and binary strings (eg. 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:
optional$xbase-10 number or base-$base number if $base set.
optionalinteger $base
Returns:
Math_BigInteger public
modInverse ( 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 Math_BigInteger(30); $b = new Math_BigInteger(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
Returns:
mixed false, if no modular inverse exists, Math_BigInteger, otherwise. public
modPow ( e,
n 
)

Performs modular exponentiation.

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

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

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

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

Parameters:
Math_BigInteger$e
Math_BigInteger$n
Returns:
Math_BigInteger public
multiply ( x)

Multiplies two BigIntegers

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

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

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

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

Parameters:
Math_BigInteger$x
Returns:
Math_BigInteger public
powMod ( e,
n 
)

Performs modular exponentiation.

Alias for Math_BigInteger::modPow()

Parameters:
Math_BigInteger$e
Math_BigInteger$n
Returns:
Math_BigInteger public
random ( min = false,
max = false 
)

Generate a random number

Parameters:
optionalInteger $min
optionalInteger $max
Returns:
Math_BigInteger public
randomPrime ( min = false,
max = false,
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:
optionalInteger $min
optionalInteger $max
optionalInteger $timeout
Returns:
Math_BigInteger public
setPrecision ( bits)

Set Precision

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

Parameters:
Math_BigInteger$xpublic
Returns:
Math_BigInteger
setRandomGenerator ( generator)

Set random number generator function

$generator should be the name of a random generating function whose first parameter is the minimum value and whose second parameter is the maximum value. If this function needs to be seeded, it should be seeded prior to calling Math_BigInteger::random() or Math_BigInteger::randomPrime()

If the random generating function is not explicitly set, it'll be assumed to be mt_rand().

See also:
random()
randomPrime()
Parameters:
optionalString $generator public
subtract ( y)

Subtracts two BigIntegers.

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

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

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

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

Parameters:
Math_BigInteger$y
Returns:
Math_BigInteger public
toBits ( 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:
Boolean$twos_compliment
Returns:
String public
toBytes ( 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:
Boolean$twos_compliment
Returns:
String public
toHex ( 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:
Boolean$twos_compliment
Returns:
String public
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 ?>

Returns:
String public

Field Documentation

$bitmask = false

Precision Bitmask

See also:
setPrecision() private
$generator = 'mt_rand'

Random number generator function

See also:
setRandomGenerator() private
$precision = -1

Precision

See also:
setPrecision() private

The documentation for this class was generated from the following file:
 All Data Structures Namespaces Files Functions Variables