From 597d123ecbb22358b3f9a633f80b61e880889f32 Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Tue, 23 Apr 2024 14:13:37 -0400 Subject: [PATCH] Drop PHP 7.0 support We need nullable types, but we cannot support them in both 7.0 and 8.4, so we're cutting 7.0 out. --- .github/workflows/test.yml | 41 ++++++++++++++++++++-- README.md | 7 +--- composer.json | 2 +- phpunit.xml | 2 ++ src/Math/ConstantTimeMath.php | 2 ++ src/Math/MathAdapterFactory.php | 6 ++-- src/Math/NumberTheory.php | 60 +++++++++++++++++++++------------ 7 files changed, 86 insertions(+), 34 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 940b8503..539201ad 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -5,12 +5,49 @@ on: pull_request: jobs: - test: + test-php7: runs-on: ${{ matrix.operating-system }} strategy: matrix: operating-system: ['ubuntu-20.04', 'ubuntu-22.04'] - php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.1', '8.2', '8.3', '8.4'] + php-versions: ['7.1', '7.2', '7.3', '7.4'] + phpunit-versions: ['latest'] + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: gmp + coverage: none + + - name: Get composer cache directory + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v2 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install dependencies + run: composer install --prefer-dist + + - name: Run tests + run: make phpunit-full-ci + + test-php8: + runs-on: ${{ matrix.operating-system }} + strategy: + matrix: + operating-system: ['ubuntu-20.04', 'ubuntu-22.04'] + php-versions: ['8.0', '8.1', '8.2', '8.3', '8.4'] phpunit-versions: ['latest'] steps: - name: Checkout diff --git a/README.md b/README.md index ad1e788e..e6789651 100644 --- a/README.md +++ b/README.md @@ -53,15 +53,10 @@ This package is released under the MIT license. ### Requirements -* PHP 7.0+ or PHP 8.0+ +* PHP 7.1+ or PHP 8.0+ * composer * ext-gmp -Support for older PHP versions: - * v0.4.x: php ^5.6|<7.2 - * v0.5.x: php ^7.0 - * v1.0.x: php ^7.0|^8.0 - ### Installation You can install this library via Composer : diff --git a/composer.json b/composer.json index fb0586ae..ee4795ab 100644 --- a/composer.json +++ b/composer.json @@ -30,7 +30,7 @@ } ], "require": { - "php": "^7.0||^8.0", + "php": "^7.1||^8.0", "ext-gmp": "*", "genkgo/php-asn1": "^2.0" }, diff --git a/phpunit.xml b/phpunit.xml index e1c48e68..9c5da2f7 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -14,6 +14,7 @@ ./tests/unit + diff --git a/src/Math/ConstantTimeMath.php b/src/Math/ConstantTimeMath.php index 73816298..d0d71884 100644 --- a/src/Math/ConstantTimeMath.php +++ b/src/Math/ConstantTimeMath.php @@ -122,6 +122,7 @@ public function binaryGcd(GMP $X, GMP $Y): array $d = \gmp_init(1, 10); do { + // Iterate over U for ($bits = $this->trailingZeroes($u); $bits > 0; --$bits) { $u = $this->rightShift($u, 1); $swap = (~$this->lsb($a) & ~$this->lsb($b)) & 1; @@ -133,6 +134,7 @@ public function binaryGcd(GMP $X, GMP $Y): array $b = $this->rightShift($b, 1); } + // Iterate over V for ($bits = $this->trailingZeroes($v); $bits > 0; --$bits) { $v = $this->rightShift($v, 1); $swap = (~$this->lsb($c) & ~$this->lsb($d)) & 1; diff --git a/src/Math/MathAdapterFactory.php b/src/Math/MathAdapterFactory.php index 3251b6f8..a05a8947 100644 --- a/src/Math/MathAdapterFactory.php +++ b/src/Math/MathAdapterFactory.php @@ -5,14 +5,14 @@ class MathAdapterFactory { /** - * @var GmpMathInterface + * @var >GmpMathInterface */ private static $forcedAdapter = null; /** - * @param GmpMathInterface $adapter + * @param ?GmpMathInterface $adapter */ - public static function forceAdapter(GmpMathInterface $adapter = null) + public static function forceAdapter(?GmpMathInterface $adapter = null) { self::$forcedAdapter = $adapter; } diff --git a/src/Math/NumberTheory.php b/src/Math/NumberTheory.php index 9b43b477..74bf04ad 100644 --- a/src/Math/NumberTheory.php +++ b/src/Math/NumberTheory.php @@ -30,6 +30,7 @@ * @author Matyas Danter */ +use GMP; use Mdanter\Ecc\Exception\NumberTheoryException; use Mdanter\Ecc\Exception\SquareRootException; @@ -44,6 +45,21 @@ class NumberTheory */ private $adapter; + /** + * @var GMP|resource + */ + private $zero; + + /** + * @var GMP|resource + */ + private $one; + + /** + * @var GMP|resource + */ + private $two; + /** * @param GmpMathInterface $adapter */ @@ -56,17 +72,17 @@ public function __construct(GmpMathInterface $adapter) } /** - * @param \GMP[] $poly - * @param \GMP[] $polymod - * @param \GMP $p - * @return \GMP[] + * @param GMP[] $poly + * @param GMP[] $polymod + * @param GMP $p + * @return GMP[] */ - public function polynomialReduceMod(array $poly, array $polymod, \GMP $p): array + public function polynomialReduceMod(array $poly, array $polymod, GMP $p): array { $adapter = $this->adapter; // Only enter if last value is set, implying count > 0 - if ((($last = end($polymod)) instanceof \GMP) && $adapter->equals($last, $this->one)) { + if ((($last = end($polymod)) instanceof GMP) && $adapter->equals($last, $this->one)) { $count_polymod = count($polymod); while (count($poly) >= $count_polymod) { if (!$adapter->equals(end($poly), $this->zero)) { @@ -95,13 +111,13 @@ public function polynomialReduceMod(array $poly, array $polymod, \GMP $p): array } /** - * @param \GMP[] $m1 - * @param \GMP[] $m2 - * @param \GMP[] $polymod - * @param \GMP $p - * @return \GMP[] + * @param GMP[] $m1 + * @param GMP[] $m2 + * @param GMP[] $polymod + * @param GMP $p + * @return GMP[] */ - public function polynomialMultiplyMod(array $m1, array $m2, array $polymod, \GMP $p): array + public function polynomialMultiplyMod(array $m1, array $m2, array $polymod, GMP $p): array { $prod = array(); $cm1 = count($m1); @@ -131,13 +147,13 @@ public function polynomialMultiplyMod(array $m1, array $m2, array $polymod, \GMP } /** - * @param \GMP[] $base - * @param \GMP $exponent - * @param \GMP[] $polymod - * @param \GMP $p - * @return \GMP[] + * @param GMP[] $base + * @param GMP $exponent + * @param GMP[] $polymod + * @param GMP $p + * @return GMP[] */ - public function polynomialPowMod(array $base, \GMP $exponent, array $polymod, \GMP $p): array + public function polynomialPowMod(array $base, GMP $exponent, array $polymod, GMP $p): array { $adapter = $this->adapter; @@ -171,11 +187,11 @@ public function polynomialPowMod(array $base, \GMP $exponent, array $polymod, \G } /** - * @param \GMP $a - * @param \GMP $p - * @return \GMP + * @param GMP $a + * @param GMP $p + * @return GMP */ - public function squareRootModP(\GMP $a, \GMP $p): \GMP + public function squareRootModP(GMP $a, GMP $p): GMP { $math = $this->adapter; $four = gmp_init(4, 10);