Math_BigInteger

# Class: Math_BigInteger

Source Location: /lib/3rdParty/phpseclib/Math/BigInteger.php

### Class Overview

Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.

#### Author(s):

- Jim Wigginton <terrafrost@php.net>

#### Version:

- 1.0.0RC4

### Methods

- Math_BigInteger
- abs
- add
- bitwise_and
- bitwise_leftRotate
- bitwise_leftShift
- bitwise_not
- bitwise_or
- bitwise_rightRotate
- bitwise_rightShift
- bitwise_xor
- compare
- copy
- divide
- equals
- extendedGCD
- gcd
- isPrime
- modInverse
- modPow
- multiply
- powMod
- random
- randomPrime
- setPrecision
- setRandomGenerator
- subtract
- toBits
- toBytes
- toHex
- toString
- __clone
- __sleep
- __toString
- __wakeup

### Class Details

[line 193]
Pure-PHP arbitrary precision integer arithmetic library. Supports base-2, base-10, base-16, and base-256 numbers.

#### Tags:

author: | Jim Wigginton <terrafrost@php.net>
version: | 1.0.0RC4
access: | public

[ Top ]

### Class Methods

### constructor Math_BigInteger [line 270]

`Math_BigInteger Math_BigInteger( [optional $x = 0], [optional $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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('0x32', 16); // 50 in base-16
6. echo $a-> toString (); // outputs 50
7. ?>

#### Tags:

access: | public

#### Parameters:

optional | $x | base-10 number or base-$base number if $base set.
optional | $base | integer $base

[ Top ]

### method abs [line 2524]

`Math_BigInteger abs( )`

Absolute value.

#### Tags:

access: | public

[ Top ]

### method add [line 786]

`Math_BigInteger add( Math_BigInteger $y)`

Adds two BigIntegers.

Here's an example:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('10');
5. $b = new Math_BigInteger ('20');
7. $c = $a-> add ($b);
9. echo $c-> toString (); // outputs 30
10. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $y |

[ Top ]

### method bitwise_and [line 2659]

`Math_BigInteger bitwise_and( Math_BigInteger $x)`

Logical And

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $x |

[ Top ]

### method bitwise_leftRotate [line 2893]

`Math_BigInteger bitwise_leftRotate( Integer $shift)`

Logical Left Rotate

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

#### Tags:

access: | public

#### Parameters:

Integer | $shift |

[ Top ]

### method bitwise_leftShift [line 2856]

`Math_BigInteger bitwise_leftShift( Integer $shift)`

Logical Left Shift

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

#### Tags:

access: | public

#### Parameters:

Integer | $shift |

[ Top ]

### method bitwise_not [line 2779]

`Math_BigInteger bitwise_not( )`

Logical Not

#### Tags:

access: | public

[ Top ]

### method bitwise_or [line 2700]

`Math_BigInteger bitwise_or( Math_BigInteger $x)`

Logical Or

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $x |

[ Top ]

### method bitwise_rightRotate [line 2937]

`Math_BigInteger bitwise_rightRotate( Integer $shift)`

Logical Right Rotate

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

#### Tags:

access: | public

#### Parameters:

Integer | $shift |

[ Top ]

### method bitwise_rightShift [line 2818]

`Math_BigInteger bitwise_rightShift( Integer $shift)`

Logical Right Shift

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

#### Tags:

access: | public

#### Parameters:

Integer | $shift |

[ Top ]

### method bitwise_xor [line 2740]

`Math_BigInteger bitwise_xor( Math_BigInteger $x)`

Logical Exclusive-Or

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $x |

[ Top ]

### method compare [line 2560]

`Integer compare( $y, Math_BigInteger $x)`

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:

return: | < 0 if $this is less than $x; > 0 if $this is greater than $x, and 0 if they are equal.
see: | Math_BigInteger::equals()
access: | public

#### Parameters:

Math_BigInteger | $x |
| $y |

[ Top ]

### method copy [line 680]

`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:

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

#### Tags:

see: | Math_BigInteger::__clone()
access: | public

[ Top ]

### method divide [line 1339]

`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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('10');
5. $b = new Math_BigInteger ('20');
7. list($quotient, $remainder) = $a-> divide ($b);
9. echo $quotient-> toString (); // outputs 0
10. echo "\r\n";
11. echo $remainder-> toString (); // outputs 10
12. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $y |

[ Top ]

### method equals [line 2618]

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

#### Tags:

see: | Math_BigInteger::compare()
access: | public

#### Parameters:

Math_BigInteger | $x |

[ Top ]

### method extendedGCD [line 2381]

`Math_BigInteger extendedGCD( Math_BigInteger $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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger (693);
5. $b = new Math_BigInteger (609);
7. extract ($a-> extendedGCD ($b));
9. echo $gcd-> toString () . "\r\n"; // outputs 21
10. echo $a-> toString () * $x-> toString () + $b-> toString () * $y-> toString (); // outputs 21
11. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $n |

[ Top ]

### method gcd [line 2512]

`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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger (693);
5. $b = new Math_BigInteger (609);
7. $gcd = a-> extendedGCD ($b);
9. echo $gcd-> toString () . "\r\n"; // outputs 21
10. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $n |

[ Top ]

### 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:

access: | public

#### Parameters:

optional | $t | Integer $t

[ Top ]

### method modInverse [line 2315]

`mixed 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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger (30);
5. $b = new Math_BigInteger (17);
7. $c = $a-> modInverse ($b);
8. echo $c-> toString (); // outputs 4
10. echo "\r\n";
12. $d = $a-> multiply ($c);
13. list(, $d) = $d-> divide ($b);
14. echo $d; // outputs 1 (as per the definition of modular inverse)
15. ?>

#### Tags:

return: | false, if no modular inverse exists, Math_BigInteger, otherwise.
access: | public

#### Parameters:

Math_BigInteger | $n |

[ Top ]

### method modPow [line 1577]

`Math_BigInteger modPow( Math_BigInteger $e, Math_BigInteger $n)`

Performs modular exponentiation.

Here's an example:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('10');
5. $b = new Math_BigInteger ('20');
6. $c = new Math_BigInteger ('30');
8. $c = $a-> modPow ($b, $c);
10. echo $c-> toString (); // outputs 10
11. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $e |
Math_BigInteger | $n |

[ Top ]

### method multiply [line 1052]

`Math_BigInteger multiply( Math_BigInteger $x)`

Multiplies two BigIntegers

Here's an example:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('10');
5. $b = new Math_BigInteger ('20');
7. $c = $a-> multiply ($b);
9. echo $c-> toString (); // outputs 200
10. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $x |

[ Top ]

### method powMod [line 1676]

`Math_BigInteger powMod( Math_BigInteger $e, Math_BigInteger $n)`

Performs modular exponentiation.

Alias for Math_BigInteger::modPow()

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $e |
Math_BigInteger | $n |

[ Top ]

### method random [line 2969]

`Math_BigInteger random( [optional $min = false], [optional $max = false])`

Generate a random number

#### Tags:

access: | public

#### Parameters:

optional | $min | Integer $min
optional | $max | Integer $max

[ Top ]

### 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:

access: | public

#### Parameters:

optional | $min | Integer $min
optional | $max | Integer $max
optional | $timeout | Integer $timeout

[ Top ]

### method setPrecision [line 2638]

`Math_BigInteger setPrecision( $bits, Math_BigInteger $x)`

Set Precision

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

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $x |
| $bits |

[ Top ]

### 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:

see: | Math_BigInteger::random()
see: | Math_BigInteger::randomPrime()
access: | public

#### Parameters:

optional | $generator | String $generator

[ Top ]

### method subtract [line 917]

`Math_BigInteger subtract( Math_BigInteger $y)`

Subtracts two BigIntegers.

Here's an example:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('10');
5. $b = new Math_BigInteger ('20');
7. $c = $a-> subtract ($b);
9. echo $c-> toString (); // outputs -10
10. ?>

#### Tags:

access: | public

#### Parameters:

Math_BigInteger | $y |

[ Top ]

### 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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('65');
6. echo $a-> toBits (); // outputs '1000001'
7. ?>

#### Tags:

access: | public

#### Parameters:

Boolean | $twos_compliment |

[ Top ]

### 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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('65');
6. echo $a-> toBytes (); // outputs chr(65)
7. ?>

#### Tags:

access: | public

#### Parameters:

Boolean | $twos_compliment |

[ Top ]

### 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:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('65');
6. echo $a-> toHex (); // outputs '41'
7. ?>

#### Tags:

access: | public

#### Parameters:

Boolean | $twos_compliment |

[ Top ]

### method toString [line 629]

`String toString( )`

Converts a BigInteger to a base-10 number.

Here's an example:

1. <?php
2. include('Math/BigInteger.php');
4. $a = new Math_BigInteger ('50');
6. echo $a-> toString (); // outputs 50
7. ?>

#### Tags:

access: | public

[ Top ]

### method __clone [line 717]

`Math_BigInteger __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.

#### Tags:

see: | Math_BigInteger::copy()
access: | public

[ Top ]

### method __sleep [line 730]

`void __sleep( )`

__sleep() magic method

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

#### Tags:

see: | Math_BigInteger::__wakeup()
access: | public

[ Top ]

### method __toString [line 700]

`void __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().

#### Tags:

access: | public

[ Top ]

### method __wakeup [line 752]

`void __wakeup( )`

__wakeup() magic method

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

#### Tags:

see: | Math_BigInteger::__sleep()
access: | public

[ Top ]

Documentation generated on Tue, 22 Nov 2011 16:29:33 +0100 by phpDocumentor 1.4.1