[ Index ] |
PHP Cross Reference of DokuWiki |
[Source view] [Print] [Project Stats]
Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers. Uses the GMP or BCMath extensions, if available, and an internal implementation, otherwise.
Author: | Jim Wigginton |
Copyright: | 2017 Jim Wigginton |
License: | http://www.opensource.org/licenses/mit-license.html MIT License |
File Size: | 892 lines (22 kb) |
Included or required: | 0 times |
Referenced: | 0 times |
Includes or requires: | 0 files |
BigInteger:: (56 methods):
setEngine()
getEngine()
initialize_static_variables()
__construct()
toString()
__toString()
__debugInfo()
toBytes()
toHex()
toBits()
add()
subtract()
multiply()
divide()
modInverse()
extendedGCD()
gcd()
abs()
setPrecision()
getPrecision()
__sleep()
__wakeup()
jsonSerialize()
powMod()
modPow()
compare()
equals()
bitwise_not()
bitwise_and()
bitwise_or()
bitwise_xor()
bitwise_rightShift()
bitwise_leftShift()
bitwise_leftRotate()
bitwise_rightRotate()
minMaxBits()
getLength()
getLengthInBytes()
random()
randomPrime()
randomRangePrime()
randomRange()
isPrime()
root()
pow()
min()
max()
between()
__clone()
isOdd()
testBit()
isNegative()
negate()
scan1divide()
createRecurringModuloFunction()
bitwise_split()
Class: BigInteger - X-Ref
Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256setEngine($main, array $modexps = ['DefaultEngine']) X-Ref |
Sets engine type. Throws an exception if the type is invalid param: string $main param: list<string> $modexps optional return: void |
getEngine() X-Ref |
Returns the engine type return: string[] |
initialize_static_variables() X-Ref |
Initialize static variables |
__construct($x = 0, $base = 10) X-Ref |
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. param: string|int|BigInteger\Engines\Engine $x Base-10 number or base-$base number if $base set. param: int $base |
toString() X-Ref |
Converts a BigInteger to a base-10 number. return: string |
__toString() X-Ref |
__toString() magic method |
__debugInfo() X-Ref |
__debugInfo() magic method Will be called, automatically, when print_r() or var_dump() are called |
toBytes($twos_compliment = false) X-Ref |
Converts a BigInteger to a byte string (eg. base-256). param: bool $twos_compliment return: string |
toHex($twos_compliment = false) X-Ref |
Converts a BigInteger to a hex string (eg. base-16). param: bool $twos_compliment return: string |
toBits($twos_compliment = false) X-Ref |
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. param: bool $twos_compliment return: string |
add(BigInteger $y) X-Ref |
Adds two BigIntegers. param: BigInteger $y return: BigInteger |
subtract(BigInteger $y) X-Ref |
Subtracts two BigIntegers. param: BigInteger $y return: BigInteger |
multiply(BigInteger $x) X-Ref |
Multiplies two BigIntegers param: BigInteger $x return: BigInteger |
divide(BigInteger $y) X-Ref |
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: <code> <?php $a = new \phpseclib3\Math\BigInteger('10'); $b = new \phpseclib3\Math\BigInteger('20'); list($quotient, $remainder) = $a->divide($b); echo $quotient->toString(); // outputs 0 echo "\r\n"; echo $remainder->toString(); // outputs 10 ?> </code> param: BigInteger $y return: BigInteger[] |
modInverse(BigInteger $n) X-Ref |
Calculates modular inverses. Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. param: BigInteger $n return: BigInteger |
extendedGCD(BigInteger $n) X-Ref |
Calculates modular inverses. Say you have (30 mod 17 * x mod 17) mod 17 == 1. x can be found using modular inverses. param: BigInteger $n return: BigInteger[] |
gcd(BigInteger $n) X-Ref |
Calculates the greatest common divisor Say you have 693 and 609. The GCD is 21. param: BigInteger $n return: BigInteger |
abs() X-Ref |
Absolute value. return: BigInteger |
setPrecision($bits) X-Ref |
Set Precision Some bitwise operations give different results depending on the precision being used. Examples include left shift, not, and rotates. param: int $bits |
getPrecision() X-Ref |
Get Precision Returns the precision if it exists, false if it doesn't return: int|bool |
__sleep() X-Ref |
Serialize Will be called, automatically, when serialize() is called on a BigInteger object. __sleep() / __wakeup() have been around since PHP 4.0 \Serializable was introduced in PHP 5.1 and deprecated in PHP 8.1: https://wiki.php.net/rfc/phase_out_serializable __serialize() / __unserialize() were introduced in PHP 7.4: https://wiki.php.net/rfc/custom_object_serialization return: array |
__wakeup() X-Ref |
Serialize Will be called, automatically, when unserialize() is called on a BigInteger object. |
jsonSerialize() X-Ref |
JSON Serialize Will be called, automatically, when json_encode() is called on a BigInteger object. return: array{hex: string, precision?: int] |
powMod(BigInteger $e, BigInteger $n) X-Ref |
Performs modular exponentiation. param: BigInteger $e param: BigInteger $n return: BigInteger |
modPow(BigInteger $e, BigInteger $n) X-Ref |
Performs modular exponentiation. param: BigInteger $e param: BigInteger $n return: BigInteger |
compare(BigInteger $y) X-Ref |
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). {@internal Could return $this->subtract($x), but that's not as fast as what we do do.} param: BigInteger $y see: self::equals() return: int in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal. |
equals(BigInteger $x) X-Ref |
Tests the equality of two numbers. If you need to see if one number is greater than or less than another number, use BigInteger::compare() param: BigInteger $x return: bool |
bitwise_not() X-Ref |
Logical Not return: BigInteger |
bitwise_and(BigInteger $x) X-Ref |
Logical And param: BigInteger $x return: BigInteger |
bitwise_or(BigInteger $x) X-Ref |
Logical Or param: BigInteger $x return: BigInteger |
bitwise_xor(BigInteger $x) X-Ref |
Logical Exclusive Or param: BigInteger $x return: BigInteger |
bitwise_rightShift($shift) X-Ref |
Logical Right Shift Shifts BigInteger's by $shift bits, effectively dividing by 2**$shift. param: int $shift return: BigInteger |
bitwise_leftShift($shift) X-Ref |
Logical Left Shift Shifts BigInteger's by $shift bits, effectively multiplying by 2**$shift. param: int $shift return: BigInteger |
bitwise_leftRotate($shift) X-Ref |
Logical Left Rotate Instead of the top x bits being dropped they're appended to the shifted bit string. param: int $shift return: BigInteger |
bitwise_rightRotate($shift) X-Ref |
Logical Right Rotate Instead of the bottom x bits being dropped they're prepended to the shifted bit string. param: int $shift return: BigInteger |
minMaxBits($bits) X-Ref |
Returns the smallest and largest n-bit number param: int $bits return: BigInteger[] |
getLength() X-Ref |
Return the size of a BigInteger in bits return: int |
getLengthInBytes() X-Ref |
Return the size of a BigInteger in bytes return: int |
random($size) X-Ref |
Generates a random number of a certain size Bit length is equal to $size param: int $size return: BigInteger |
randomPrime($size) X-Ref |
Generates a random prime number of a certain size Bit length is equal to $size param: int $size return: BigInteger |
randomRangePrime(BigInteger $min, BigInteger $max) X-Ref |
Generate a random prime number between a range If there's not a prime within the given range, false will be returned. param: BigInteger $min param: BigInteger $max return: false|BigInteger |
randomRange(BigInteger $min, BigInteger $max) X-Ref |
Generate a random number between a range 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) param: BigInteger $min param: BigInteger $max return: BigInteger |
isPrime($t = false) X-Ref |
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. BigInteger::randomPrime() can be distributed across multiple pageloads on a website instead of just one. param: int|bool $t return: bool |
root($n = 2) X-Ref |
Calculates the nth root of a biginteger. Returns the nth root of a positive biginteger, where n defaults to 2 param: int $n optional return: BigInteger |
pow(BigInteger $n) X-Ref |
Performs exponentiation. param: BigInteger $n return: BigInteger |
min(BigInteger ...$nums) X-Ref |
Return the minimum BigInteger between an arbitrary number of BigIntegers. param: BigInteger ...$nums return: BigInteger |
max(BigInteger ...$nums) X-Ref |
Return the maximum BigInteger between an arbitrary number of BigIntegers. param: BigInteger ...$nums return: BigInteger |
between(BigInteger $min, BigInteger $max) X-Ref |
Tests BigInteger to see if it is between two integers, inclusive param: BigInteger $min param: BigInteger $max return: bool |
__clone() X-Ref |
Clone |
isOdd() X-Ref |
Is Odd? return: bool |
testBit($x) X-Ref |
Tests if a bit is set param: int $x return: bool |
isNegative() X-Ref |
Is Negative? return: bool |
negate() X-Ref |
Negate Given $k, returns -$k return: BigInteger |
scan1divide(BigInteger $r) X-Ref |
Scan for 1 and right shift by that amount ie. $s = gmp_scan1($n, 0) and $r = gmp_div_q($n, gmp_pow(gmp_init('2'), $s)); param: BigInteger $r return: int |
createRecurringModuloFunction() X-Ref |
Create Recurring Modulo Function Sometimes it may be desirable to do repeated modulos with the same number outside of modular exponentiation return: callable |
bitwise_split($split) X-Ref |
Bitwise Split Splits BigInteger's into chunks of $split bits param: int $split return: BigInteger[] |