Skip to content

Commit

Permalink
* freshen up GH actions
Browse files Browse the repository at this point in the history
* avoid using gmp_pow() due to php/php-src#16870 (comment)
  • Loading branch information
1ma committed Nov 29, 2024
1 parent a83dfa5 commit e179a1e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 171 deletions.
45 changes: 10 additions & 35 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ jobs:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: ['ubuntu-20.04', 'ubuntu-18.04']
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4', '8.0']
operating-system: ['ubuntu-24.04', 'ubuntu-22.04', 'ubuntu-20.04']
php-versions: ['8.1', '8.2', '8.3', '8.4']
phpunit-versions: ['latest']
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout submodules
uses: textbook/git-checkout-submodule-action@master
uses: actions/checkout@v4
with:
submodules: true

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -25,17 +25,6 @@ jobs:
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

Expand All @@ -46,37 +35,23 @@ jobs:
runs-on: ${{ matrix.operating-system }}
strategy:
matrix:
operating-system: ['ubuntu-20.04']
php-versions: ['7.4']
operating-system: ['ubuntu-24.04']
php-versions: ['8.4']

steps:
- name: Checkout
uses: actions/checkout@v2
- name: Checkout submodules
uses: textbook/git-checkout-submodule-action@master
uses: actions/checkout@v4
with:
submodules: true

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: gmp, xdebug

- 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-ci

- name: Upload coverage to Scrutinizer
run: make scrutinizer
94 changes: 0 additions & 94 deletions .scrutinizer.yml

This file was deleted.

12 changes: 0 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,10 @@ endif
phpcbf: pretest
vendor/bin/phpcbf --standard=PSR2 -n src tests/unit/

ocular:
wget https://scrutinizer-ci.com/ocular.phar

ifdef OCULAR_TOKEN
scrutinizer: ocular
@php ocular.phar code-coverage:upload --format=php-clover tests/output/coverage.clover --access-token=$(OCULAR_TOKEN);
else
scrutinizer: ocular
php ocular.phar code-coverage:upload --format=php-clover tests/output/coverage.clover;
endif

clean: clean-env clean-deps

clean-env:
rm -rf coverage.clover
rm -rf ocular.phar
rm -rf tests/output/

clean-deps:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"fgrosse/phpasn1": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0||^8.0||^9.0",
"phpunit/phpunit": "^9.0",
"squizlabs/php_codesniffer": "^2.0",
"symfony/yaml": "^2.6|^3.0"
"symfony/yaml": "^7.0"
},
"replace": {
"mdanter/ecc": "1.0.0"
Expand Down
14 changes: 7 additions & 7 deletions src/Math/GmpMath.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function equals(\GMP $first, \GMP $other): bool
{
return gmp_cmp($first, $other) === 0;
}

/**
* {@inheritDoc}
* @see GmpMathInterface::mod()
Expand Down Expand Up @@ -77,7 +77,7 @@ public function div(\GMP $dividend, \GMP $divisor): \GMP
*/
public function pow(\GMP $base, int $exponent): \GMP
{
return gmp_pow($base, $exponent);
return $base ** $exponent;
}

/**
Expand All @@ -96,7 +96,7 @@ public function bitwiseAnd(\GMP $first, \GMP $other): \GMP
public function rightShift(\GMP $number, int $positions): \GMP
{
// Shift 1 right = div / 2
return gmp_div($number, gmp_pow(gmp_init(2, 10), $positions));
return gmp_div($number, gmp_init(2, 10) ** $positions);
}

/**
Expand All @@ -115,7 +115,7 @@ public function bitwiseXor(\GMP $first, \GMP $other): \GMP
public function leftShift(\GMP $number, int $positions): \GMP
{
// Shift 1 left = mul by 2
return gmp_mul($number, gmp_pow(2, $positions));
return gmp_mul($number, gmp_init(2, 10) ** $positions);
}

/**
Expand Down Expand Up @@ -228,18 +228,18 @@ public function intToFixedSizeString(\GMP $x, int $byteSize): string
}

$two = gmp_init(2);
$range = gmp_pow($two, $byteSize * 8);
$range = $two ** ($byteSize * 8);
if (NumberSize::bnNumBits($this, $x) >= NumberSize::bnNumBits($this, $range)) {
throw new \RuntimeException("Number overflows byte size");
}

$maskShift = gmp_pow($two, 8);
$maskShift = $two ** 8;
$mask = gmp_mul(gmp_init(255), $range);

$binary = '';
for ($i = $byteSize - 1; $i >= 0; $i--) {
$mask = gmp_div($mask, $maskShift);
$binary .= pack('C', gmp_strval(gmp_div(gmp_and($x, $mask), gmp_pow($two, $i * 8)), 10));
$binary .= pack('C', gmp_strval(gmp_div(gmp_and($x, $mask), $two ** ($i * 8))));
}

return $binary;
Expand Down
12 changes: 5 additions & 7 deletions src/Math/NumberTheory.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@
*/
class NumberTheory
{
/**
* @var GmpMathInterface
*/
private $adapter;
private GmpMathInterface $adapter;

private \GMP $zero;
private \GMP $one;
private \GMP $two;

/**
* @param GmpMathInterface $adapter
*/
public function __construct(GmpMathInterface $adapter)
{
$this->adapter = $adapter;
Expand Down
6 changes: 3 additions & 3 deletions src/Primitives/JacobianPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ public function negate(): self
*/
public function gmp_shiftr(\GMP | int $x, \GMP | int $n): \GMP
{
return gmp_div($x, gmp_pow(2, $n));
return gmp_div($x, gmp_init(2, 10) ** $n);
}

/**
Expand All @@ -293,8 +293,8 @@ public function cmp(self $other): int
$Y2 = $other->getY();
$Z2 = $other->getZ();

$Z1Z1 = $this->mod(gmp_pow($Z1, 2));
$Z2Z2 = $this->mod(gmp_pow($Z2, 2));
$Z1Z1 = $this->mod($Z1 ** 2);
$Z2Z2 = $this->mod($Z2 ** 2);

$U1 = $this->mod(gmp_mul($X1, $Z2Z2));
$U2 = $this->mod(gmp_mul($X2, $Z1Z1));
Expand Down
5 changes: 2 additions & 3 deletions src/Random/RandomNumberGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ public function generate(\GMP $max): \GMP
$bytes = random_bytes($numBytes);
$value = $this->adapter->stringToInt($bytes);

$mask = gmp_sub(gmp_pow(2, $numBits), 1);
$integer = gmp_and($value, $mask);
$mask = gmp_sub(gmp_init(2, 10) ** $numBits, 1);

return $integer;
return gmp_and($value, $mask);
}
}
16 changes: 8 additions & 8 deletions tests/unit/Math/MathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ public function testDigestInteger(GmpMathInterface $math, $integer, $result)
public function fixedSizeIntProvider()
{
$two = gmp_init(2, 10);
$maxRange8 = gmp_sub(gmp_pow($two, 8), gmp_init(1));
$maxRange16 = gmp_sub(gmp_pow($two, 16), gmp_init(1));
$maxRange24 = gmp_sub(gmp_pow($two, 24), gmp_init(1));
$maxRange32 = gmp_sub(gmp_pow($two, 32), gmp_init(1));
$maxRange8 = gmp_sub($two ** 8, gmp_init(1));
$maxRange16 = gmp_sub($two ** 16, gmp_init(1));
$maxRange24 = gmp_sub($two ** 24, gmp_init(1));
$maxRange32 = gmp_sub($two ** 32, gmp_init(1));
$bits519Unpadded = gmp_init("7d48a8cdd8ea2de81d9ad3d5b6597472264bd2c1da2e6ae175dd50397d7812383dce09988bfce60b370e5a0f3eb0090d18545935b77ba521f5db598bf3ed03616a", 16);
$bits519Padded = gmp_init("007d48a8cdd8ea2de81d9ad3d5b6597472264bd2c1da2e6ae175dd50397d7812383dce09988bfce60b370e5a0f3eb0090d18545935b77ba521f5db598bf3ed03616a", 16);
return $this->_getAdapters([
Expand Down Expand Up @@ -293,10 +293,10 @@ public function tooLargeProvider()
[gmp_init(1), 0],
[gmp_init(256), 1],
[gmp_init(65536), 2],
[gmp_pow(2, 32), 1],
[gmp_pow(2, 32), 2],
[gmp_pow(2, 32), 3],
[gmp_pow(2, 32), 4],
[gmp_init(2, 10) ** 32, 1],
[gmp_init(2, 10) ** 32, 2],
[gmp_init(2, 10) ** 32, 3],
[gmp_init(2, 10) ** 32, 4],
]);
}

Expand Down

0 comments on commit e179a1e

Please sign in to comment.