richardds/big-integer

大整数操作库

0.1.0 2021-01-13 15:43 UTC

This package is auto-updated.

Last update: 2024-09-15 21:41:04 UTC


README

PHP面向对象的大整数操作库,作为GMP库的包装器。该库不将数字转换为字符串,而是使用GMP资源,与其它库相比,速度有显著提升。该库没有外部依赖。

一般要求

  • php-7.2 及以上
  • php-gmp

如何在Apline Linux上安装GMP

RUN apk add --update --no-cache gmp
RUN apk add --update --no-cache --virtual .phpize-deps $PHPIZE_DEPS autoconf g++ make gmp-dev && \
    docker-php-ext-install gmp && \
    apk del .phpize-deps $PHPIZE_DEPS autoconf g++ make gmp-dev

示例

大多数方法接受 BigInteger 对象、stringintBigInteger 对象使用GMP资源。因此,在每个操作中都不会有慢速的字符串转换。

use Richardds\BigInteger\BigInteger;
use Richardds\BigInteger\Utils as BigIntegerUtils;

$g = BigInteger::fromInt(2);
$e = BigInteger::fromString("<big_integer>");
$m = BigInteger::fromString("<big_integer>");
$c = $g->powMod($e, $m);

$p = BigInteger::fromBuffer("<buffer>");
$z = BigInteger::fromString("<big_integer>");

$x = $g->powMod($p, $m)
       ->mul($z->sub(BigInteger::one()))
       ->mod($m);

$a = BigInteger::fromString("<big_integer>");
$b = BigInteger::fromString("<big_integer>");
$gcd = $a->gcd($b);
$gcd = BigIntegerUtils::gcd($a, $b);