blockchainethdev / bigint-wrapper-php
php_gmp 和 php_bcmath 模块的通用接口
README
信息
这个库是 php_gmp 和 php_bcmath 模块的通用接口。它自动检测支持的模块并使用其中最好的(gmp>bcmath)。Gmp 要快得多,但许多托管服务中都没有 -- 因此创建了此包装器。例如,它用于 PrivMX WebMail 软件的加密功能PrivMX WebMail。
安装
您可以通过 Composer 安装此库
composer require blockchainethdev/bigint-wrapper-php
文档
如果您想强制使用特定的实现,请定义常量 S_MATH_BIGINTEGER_MODE - 将其设置为 "gmp" 或 "bcmath"。如果您不这样做,操作模式将自动设置。
如果没有 gmp 和 bcmath 模块,将抛出异常。如果您想防止这种情况,则只需定义 S_MATH_BIGINTEGER_QUIET 常量。
此库的所有功能都作为位于 BI 命名空间下的 BigInteger 类的成员实现。BigInteger 实例是不可变的 - 成员函数通常返回 BigInteger 类的新实例。
ConvertibleToBi - 一个占位符类型
为了使以下文档更易于阅读,我们使用 "ConvertibleToBi" 类型符号,实际上它可以是指以下类型之一
- BigInteger 类的实例
- 一个整数
- 一个十进制字符串
- 一个 gmp 资源或类(仅在您处于 gmp 模式时)
如果您有一个非十进制字符串并想使用它 -- 首先,您必须使用以下方法将其转换为 BigInteger 类
new BigInteger($myNonDecimalString, $baseOfMyNonDecimalString)
BI\BigInteger 类成员
构造函数(ConvertibleToBi $value = 0, int $base = 10)
创建一个新的 BigInteger 实例。如果您传递一个无效的值,将抛出一个异常。如果 $base === true,则传递的 $value 将不带任何检查和转换使用。支持的基础:2,10,16,256。
- GMP 实现: gmp_init + bin2hex(对于 256 基础)
- Bcmath 实现: custom(bcadd + bcmul)
static BigInteger|false createSafe(ConvertibleToBi $value = 0, int $base = 10)
以与构造函数相同的方式创建一个新的 BigInteger 实例,但如果发生错误,则返回 false 而不是抛出异常。
BigInteger add(ConvertibleToBi $x)
添加数字
- GMP 实现: gmp_add
- Bcmath 实现: bcadd
BigInteger sub(ConvertibleToBi $x)
减去数字
- GMP 实现: gmp_sub
- Bcmath 实现: bcsub
BigInteger mul(ConvertibleToBi $x)
乘以数字
- GMP 实现: gmp_mul
- Bcmath 实现: bcmul
BigInteger div(ConvertibleToBi $x)
除以数字
- GMP 实现: gmp_div_q
- Bcmath 实现: bcdiv
BigInteger divR(ConvertibleToBi $x)
返回数字除法的结果。余数具有被除数的符号。
- GMP 实现: gmp_div_r
- Bcmath 实现: bcmod
array(BigInteger, BigInteger) divQR(ConvertibleToBi $x)
除以数字并返回商和余数。返回一个数组,其中第一个元素是商,第二个元素是余数。
- GMP 实现: gmp_div_qr
- Bcmath 实现: div + divR
BigInteger mod(ConvertibleToBi $x)
“除模”操作。结果始终为非负,除数的符号被忽略。
- GMP实现: gmp_mod
- Bcmath实现: 自定义(bcmod + bcadd)
BigInteger gcd(ConvertibleToBi $x)
计算最大公约数
- GMP实现: gmp_gcd
- Bcmath实现: 自定义(bccomp + bcdiv + bcsub + bcmul)
BigInteger|false modInverse(ConvertibleToBi $x)
通过模逆运算,如果不存在逆元则返回false。
- GMP实现: gmp_invert
- Bcmath实现: 自定义(gcd)
BigInteger pow(ConvertibleToBi $x)
幂函数。
- GMP实现: gmp_pow
- Bcmath实现: bcpow
BigInteger powMod(ConvertibleToBi $x, ConvertibleToBi $n)
模幂函数。
- GMP实现: gmp_powm
- Bcmath实现: bcpowmod
BigInteger abs()
返回绝对值。
- GMP实现: gmp_abs
- Bcmath实现: 检查第一个字符
BigInteger neg()
取反数
- GMP实现: gmp_neg
- Bcmath实现: 检查第一个字符
BigInteger binaryAnd(ConvertibleToBi $x)
按位与。
- GMP实现: gmp_and
- Bcmath实现: 自定义(toBytes + php字符串与)
BigInteger binaryOr(ConvertibleToBi $x)
按位或
- GMP实现: gmp_or
- Bcmath实现: 自定义(toBytes + php字符串或)
BigInteger binaryXor(ConvertibleToBi $x)
按位异或
- GMP实现: gmp_xor
- Bcmath实现: 自定义(toBytes + php字符串异或)
BigInteger setbit($index, $bitOn = true)
设置指定索引的位
- GMP实现: gmp_setbit
- Bcmath实现: 自定义(toBits)
bool testbit($index)
检查指定索引的位是否被设置
- GMP实现: gmp_testbit
- Bcmath实现: 自定义(toBits)
int scan0($start)
查找0,并返回第一个找到的位的索引
- GMP实现: gmp_scan0
- Bcmath实现: 自定义(toBits)
int scan1($start)
查找1,并返回第一个找到的位的索引
- GMP实现: gmp_scan1
- Bcmath实现: 自定义(toBits)
int cmp(ConvertibleToBi $x)
比较数字,返回 <0, 0, >0
- GMP实现: gmp_cmp
- Bcmath实现: bccomp
bool equals(ConvertibleToBi $x)
检查数字是否相等
- GMP实现: gmp_cmp
- Bcmath实现: bccomp
int sign()
数字的符号,返回 -1, 0, 1
- GMP实现: gmp_sign
- Bcmath实现: 检查第一个字符
int toNumber()
转换为数字(仅适用于小的32/64位数字)
- GMP实现: gmp_intval
- Bcmath实现: intval
string toDec()
转换为十进制字符串
- GMP实现: gmp_strval
- Bcmath实现: 仅值
string toHex()
转换为十六进制字符串
- GMP实现: gmp_strval
- Bcmath实现: toBytes + bin2hex
string toBytes
转换为二进制字符串
- GMP实现: gmp_strval + hex2bin
- Bcmath实现: 自定义(bcmod + bcdiv + bccomp)
string toBits()
转换为位字符串(0和1字符)
- GMP实现: gmp_strval
- Bcmath实现: toBytes + decbin
string toString(int $base = 10)
使用给定的基数转换为字符串(支持的基数2-62, 256)
- GMP实现: 所有上述toX函数,以及非标准gmp_strval
- Bcmath实现: 所有上述toX函数,以及非标准bcmod + bcdiv + bccomp