dev-master
9999999-devCommon interface for php_gmp and php_bcmath modules
MIT
1.0.0
1.0.0.0Common interface for php_gmp and php_bcmath modules
MIT
Common interface for php_gmp and php_bcmath modules
This library is a common interface for php_gmp and php_bcmath modules. It automatically detects supported modules and uses the best of them (gmp>bcmath). Gmp is a lot faster, but is also missing on many hosting services -- that is why this wrapper has been created. It is used for example in encryption functions of the PrivMX WebMail software., (*1)
You can install this library via Composer:, (*2)
composer require simplito/bigint-wrapper-php
If you want to force using a specific implementation, then define constant S_MATH_BIGINTEGER_MODE - set it to "gmp" or "bcmath". If you do not do this, mode of operation and the constant will be set automatically., (*3)
If there are no gmp and bcmath modules, an exception will be thrown. If you want to prevent this, then simply define S_MATH_BIGINTEGER_QUIET constant., (*4)
All functions of this library are implemented as members of class BigInteger, which is located under BI namespace. Instances of BigInteger are immutable - member functions usually return new instances of the BigInteger class., (*5)
To make the below documentation more readable we use the "ConvertibleToBi" type symbol, which in reality can be one of the following types:
- an instance of the BigInteger class
- an integer
- a decimal string
- a gmp resource or class (only when you are in gmp mode), (*6)
If you have a non-decimal string and want to use it -- first you have to convert it to BigInteger class using:, (*7)
new BigInteger($myNonDecimalString, $baseOfMyNonDecimalString)
Creates a new instance of BigInteger. If you pass an invalid value, an exception will be thrown. If $base === true then passed $value will be used without any check and conversion. Supported bases: 2, 10, 16, 256. - GMP implementation: gmp_init + bin2hex for 256 base - Bcmath implementation: custom(bcadd + bcmul), (*8)
Creates a new BigInteger instance in the same way as constructor, but if there is an error, false will be returned instead of throwing an exception., (*9)
Adds numbers - GMP implementation: gmp_add - Bcmath implementation: bcadd, (*10)
Subtracts numbers - GMP implementation: gmp_sub - Bcmath implementation: bcsub, (*11)
Multiplies numbers - GMP implementation: gmp_mul - Bcmath implementation: bcmul, (*12)
Divides numbers - GMP implementation: gmp_div_q - Bcmath implementation: bcdiv, (*13)
Returns a remainder of the division of numbers. The remainder has the sign of the divided number. - GMP implementation: gmp_div_r - Bcmath implementation: bcmod, (*14)
Divides numbers and returns quotient and remainder. Returns an array(), with the first element being quotient, and the second being remainder. - GMP implementation: gmp_div_qr - Bcmath implementation: div + divR, (*15)
The "division modulo" operation. The result is always non-negative, the sign of divider is ignored. - GMP implementation: gmp_mod - Bcmath implementation: custom (bcmod + bcadd), (*16)
Calculates greatest common divisor - GMP implementation: gmp_gcd - Bcmath implementation: custom (bccomp + bcdiv + bcsub + bcmul), (*17)
Inverses by modulo, returns false if inversion does not exist. - GMP implementation: gmp_invert - Bcmath implementation: custom (gcd), (*18)
The power function. - GMP implementation: gmp_pow - Bcmath implementation: bcpow, (*19)
The modular power function. - GMP implementation: gmp_powm - Bcmath implementation: bcpowmod, (*20)
Returns absolute value. - GMP implementation: gmp_abs - Bcmath implementation: check first character, (*21)
Negates the number - GMP implementation: gmp_neg - Bcmath implementation: check first character, (*22)
Bitwise AND. - GMP implementation: gmp_and - Bcmath implementation: custom (toBytes + php string and), (*23)
Bitwise OR - GMP implementation: gmp_or - Bcmath implementation: custom (toBytes + php string or), (*24)
Bitwise XOR - GMP implementation: gmp_xor - Bcmath implementation: custom (toBytes + php string xor), (*25)
Sets bit at given index - GMP implementation: gmp_setbit - Bcmath implementation: custom (toBits), (*26)
Tests if a bit at given index is set - GMP implementation: gmp_testbit - Bcmath implementation: custom (toBits), (*27)
Scans for 0, and returns index of first found bit - GMP implementation: gmp_scan0 - Bcmath implementation: custom (toBits), (*28)
Scans for 1, and returns index of first found bit - GMP implementation: gmp_scan1 - Bcmath implementation: custom (toBits), (*29)
Compares numbers, returns <0, 0, >0 - GMP implementation: gmp_cmp - Bcmath implementation: bccomp, (*30)
Checks if numbers are equal - GMP implementation: gmp_cmp - Bcmath implementation: bccomp, (*31)
Sign of number, returns -1, 0, 1 - GMP implementation: gmp_sign - Bcmath implementation: check first character, (*32)
Converts to number (use only with small 32/64bit numbers) - GMP implementation: gmp_intval - Bcmath implementation: intval, (*33)
Converts to decimal string - GMP implementation: gmp_strval - Bcmath implementation: just the value, (*34)
Converts to hex string - GMP implementation: gmp_strval - Bcmath implementation: toBytes + bin2hex, (*35)
Converts to binary string - GMP implementation: gmp_strval + hex2bin - Bcmath implementation: custom (bcmod + bcdiv + bccomp), (*36)
Converts to bits string (0 and 1 characters) - GMP implementation: gmp_strval - Bcmath implementation: toBytes + decbin, (*37)
Converts to string using given base (supported bases 2-62, 256) - GMP implementation: all above toX functions, and for non standard gmp_strval - Bcmath implementation: all above toX functions, and for non standard bcmod + bcdiv + bccomp, (*38)
Common interface for php_gmp and php_bcmath modules
MIT
Common interface for php_gmp and php_bcmath modules
MIT