Class: Math_BigInteger
Source Location: /lib/3rdParty/phpseclib/Math/BigInteger.php
Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.
Author(s):
Version:
|
|
Class Details
Class Methods
constructor Math_BigInteger [line 270]
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');
?>
Tags:
Parameters:
method abs [line 2524]
method add [line 786]
Adds two BigIntegers. Here's an example: <?php
include('Math/BigInteger.php');
?>
Tags:
Parameters:
method bitwise_and [line 2659]
Logical And
Tags:
Parameters:
method bitwise_leftRotate [line 2893]
Logical Left Rotate Instead of the top x bits being dropped they're appended to the shifted bit string.
Tags:
Parameters:
method bitwise_leftShift [line 2856]
Logical Left Shift Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift.
Tags:
Parameters:
method bitwise_not [line 2779]
method bitwise_or [line 2700]
Logical Or
Tags:
Parameters:
method bitwise_rightRotate [line 2937]
Logical Right Rotate Instead of the bottom x bits being dropped they're prepended to the shifted bit string.
Tags:
Parameters:
method bitwise_rightShift [line 2818]
Logical Right Shift Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift.
Tags:
Parameters:
method bitwise_xor [line 2740]
Logical Exclusive-Or
Tags:
Parameters:
method compare [line 2560]
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).
Tags:
Parameters:
method copy [line 680]
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
Tags:
method divide [line 1339]
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');
list ($quotient, $remainder) = $a->divide($b);
echo $quotient->toString(); // outputs 0
echo "\r\n";
echo $remainder->toString(); // outputs 10
?>
Tags:
Parameters:
method equals [line 2618]
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()
Tags:
Parameters:
method extendedGCD [line 2381]
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');
echo $gcd->toString() . "\r\n"; // outputs 21
?>
Tags:
Parameters:
method gcd [line 2512]
Calculates the greatest common divisor Say you have 693 and 609. The GCD is 21. Here's an example: <?php
include('Math/BigInteger.php');
echo $gcd->toString() . "\r\n"; // outputs 21
?>
Tags:
Parameters:
method isPrime [line 3164]
Boolean isPrime(
[optional
$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.
Tags:
Parameters:
method modInverse [line 2315]
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');
echo "\r\n";
echo $d; // outputs 1 (as per the definition of modular inverse)
?>
Tags:
Parameters:
method modPow [line 1577]
Performs modular exponentiation. Here's an example: <?php
include('Math/BigInteger.php');
?>
Tags:
Parameters:
method multiply [line 1052]
Multiplies two BigIntegers Here's an example: <?php
include('Math/BigInteger.php');
?>
Tags:
Parameters:
method powMod [line 1676]
Performs modular exponentiation. Alias for Math_BigInteger::modPow()
Tags:
Parameters:
method random [line 2969]
Generate a random number
Tags:
Parameters:
method randomPrime [line 3033]
Math_BigInteger randomPrime(
[optional
$min = false], [optional
$max = false], [optional
$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.
Tags:
Parameters:
method setPrecision [line 2638]
Set Precision Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates.
Tags:
Parameters:
method setRandomGenerator [line 2956]
void setRandomGenerator(
optional
$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().
Tags:
Parameters:
method subtract [line 917]
Subtracts two BigIntegers. Here's an example: <?php
include('Math/BigInteger.php');
?>
Tags:
Parameters:
method toBits [line 598]
String toBits(
[Boolean
$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');
echo $a->toBits(); // outputs '1000001'
?>
Tags:
Parameters:
method toBytes [line 474]
String toBytes(
[Boolean
$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');
echo $a->toBytes(); // outputs chr(65)
?>
Tags:
Parameters:
method toHex [line 571]
String toHex(
[Boolean
$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');
echo $a->toHex(); // outputs '41'
?>
Tags:
Parameters:
method toString [line 629]
Converts a BigInteger to a base-10 number. Here's an example: <?php
include('Math/BigInteger.php');
?>
Tags:
method __clone [line 717]
__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.
Tags:
method __sleep [line 730]
__sleep() magic method Will be called, automatically, when serialize() is called on a Math_BigInteger object.
Tags:
method __toString [line 700]
__toString() magic method Will be called, automatically, if you're supporting just PHP5. If you're supporting PHP4, you'll need to call toString().
Tags:
method __wakeup [line 752]
__wakeup() magic method Will be called, automatically, when unserialize() is called on a Math_BigInteger object.
Tags:
|
|