[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/vendor/phpseclib/phpseclib/phpseclib/Math/ -> BigInteger.php (summary)

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

Defines 1 class

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-256
numbers.

setEngine($main, array $modexps = ['DefaultEngine'])   X-Ref
Sets engine type.

Throws an exception if the type is invalid

return: void
param: string $main
param: list<string> $modexps optional

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

return: string
param: bool $twos_compliment

toHex($twos_compliment = false)   X-Ref
Converts a BigInteger to a hex string (eg. base-16).

return: string
param: bool $twos_compliment

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.

return: string
param: bool $twos_compliment

add(BigInteger $y)   X-Ref
Adds two BigIntegers.

return: BigInteger
param: BigInteger $y

subtract(BigInteger $y)   X-Ref
Subtracts two BigIntegers.

return: BigInteger
param: BigInteger $y

multiply(BigInteger $x)   X-Ref
Multiplies two BigIntegers

return: BigInteger
param: BigInteger $x

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>

return: BigInteger[]
param: BigInteger $y

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.

return: BigInteger
param: BigInteger $n

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.

return: BigInteger[]
param: BigInteger $n

gcd(BigInteger $n)   X-Ref
Calculates the greatest common divisor

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

return: BigInteger
param: BigInteger $n

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.

return: BigInteger
param: BigInteger $e
param: BigInteger $n

modPow(BigInteger $e, BigInteger $n)   X-Ref
Performs modular exponentiation.

return: BigInteger
param: BigInteger $e
param: BigInteger $n

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

return: int in case < 0 if $this is less than $y; > 0 if $this is greater than $y, and 0 if they are equal.
param: BigInteger $y
see: self::equals()

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

return: bool
param: BigInteger $x

bitwise_not()   X-Ref
Logical Not

return: BigInteger

bitwise_and(BigInteger $x)   X-Ref
Logical And

return: BigInteger
param: BigInteger $x

bitwise_or(BigInteger $x)   X-Ref
Logical Or

return: BigInteger
param: BigInteger $x

bitwise_xor(BigInteger $x)   X-Ref
Logical Exclusive Or

return: BigInteger
param: BigInteger $x

bitwise_rightShift($shift)   X-Ref
Logical Right Shift

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

return: BigInteger
param: int $shift

bitwise_leftShift($shift)   X-Ref
Logical Left Shift

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

return: BigInteger
param: int $shift

bitwise_leftRotate($shift)   X-Ref
Logical Left Rotate

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

return: BigInteger
param: int $shift

bitwise_rightRotate($shift)   X-Ref
Logical Right Rotate

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

return: BigInteger
param: int $shift

minMaxBits($bits)   X-Ref
Returns the smallest and largest n-bit number

return: BigInteger[]
param: int $bits

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

return: BigInteger
param: int $size

randomPrime($size)   X-Ref
Generates a random prime number of a certain size

Bit length is equal to $size

return: BigInteger
param: int $size

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.

return: false|BigInteger
param: BigInteger $min
param: BigInteger $max

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)

return: BigInteger
param: BigInteger $min
param: BigInteger $max

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.

return: bool
param: int|bool $t

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

return: BigInteger
param: int $n optional

pow(BigInteger $n)   X-Ref
Performs exponentiation.

return: BigInteger
param: BigInteger $n

min(BigInteger ...$nums)   X-Ref
Return the minimum BigInteger between an arbitrary number of BigIntegers.

return: BigInteger
param: BigInteger ...$nums

max(BigInteger ...$nums)   X-Ref
Return the maximum BigInteger between an arbitrary number of BigIntegers.

return: BigInteger
param: BigInteger ...$nums

between(BigInteger $min, BigInteger $max)   X-Ref
Tests BigInteger to see if it is between two integers, inclusive

return: bool
param: BigInteger $min
param: BigInteger $max

__clone()   X-Ref
Clone


isOdd()   X-Ref
Is Odd?

return: bool

testBit($x)   X-Ref
Tests if a bit is set

return: bool
param: int $x

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

return: int
param: BigInteger $r

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

return: BigInteger[]
param: int $split