From e179a1e1569519af9137c48c2c98c5b90076fadd Mon Sep 17 00:00:00 2001 From: Marcel Hernandez Date: Fri, 29 Nov 2024 17:37:44 +0100 Subject: [PATCH] * freshen up GH actions * avoid using gmp_pow() due to https://github.com/php/php-src/issues/16870#issuecomment-2506560656 --- .github/workflows/test.yml | 45 +++---------- .scrutinizer.yml | 94 ---------------------------- Makefile | 12 ---- composer.json | 4 +- src/Math/GmpMath.php | 14 ++--- src/Math/NumberTheory.php | 12 ++-- src/Primitives/JacobianPoint.php | 6 +- src/Random/RandomNumberGenerator.php | 5 +- tests/unit/Math/MathTest.php | 16 ++--- 9 files changed, 37 insertions(+), 171 deletions(-) delete mode 100644 .scrutinizer.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04196633..fafea950 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 @@ -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 @@ -46,14 +35,14 @@ 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 @@ -61,22 +50,8 @@ jobs: 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 diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index bb481fb8..00000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,94 +0,0 @@ -build: - nodes: - php-scrutinizer-run: - project_setup: - override: true - tests: - override: [php-scrutinizer-run] - - environment: - mysql: false - redis: false - postgresql: false - php: - version: 7.2 - ini: - 'zend.assertions': '1' - 'assert.exception': '1' - dependencies: - override: - - - command: 'composer install' - tests: - override: true - project_setup: - override: true - -filter: - paths: - - src/* - excluded_paths: - - examples/* - - tests/* - - vendor/* - -tools: - php_cpd: true - php_pdepend: true - php_analyzer: true - php_sim: true - php_changetracking: true - php_mess_detector: true - php_code_sniffer: true - sensiolabs_security_checker: true - php_code_coverage: true - php_pdepend: - excluded_dirs: [vendor, doc, examples, build, tests] - external_code_coverage: - timeout: 1300 - runs: 1 - -changetracking: - bug_patterns: ["\bfix(?:es|ed)?\b"] - feature_patterns: ["\badd(?:s|ed)?\b", "\bimplement(?:s|ed)?\b"] - -build_failure_conditions: - # No new critical issue is introduced (existing ones are tolerated) - - 'issues.severity(>= CRITICAL).new.exists' - - # Code Coverage drops below 60% - - 'project.metric("scrutinizer.test_coverage", < 0.80)' - - # Code Coverage decreased from previous inspection by more than 1% - - 'project.metric_change("scrutinizer.test_coverage", < -0.01)' - -checks: - php: - simplify_boolean_return: true - return_doc_comments: true - return_doc_comment_if_not_inferrable: true - remove_extra_empty_lines: true - properties_in_camelcaps: true - phpunit_assertions: true - parameters_in_camelcaps: true - parameter_doc_comments: true - param_doc_comment_if_not_inferrable: true - overriding_parameter: true - no_short_variable_names: - minimum: '3' - no_short_method_names: - minimum: '3' - no_long_variable_names: - maximum: '20' - no_goto: true - fix_use_statements: - remove_unused: true - preserve_multiple: false - preserve_blanklines: false - order_alphabetically: false - fix_line_ending: true - check_method_contracts: - verify_interface_like_constraints: true - verify_documented_constraints: true - verify_parent_constraints: true - diff --git a/Makefile b/Makefile index f3f76601..922c3a94 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/composer.json b/composer.json index 4ffccfdb..2cc401b9 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/Math/GmpMath.php b/src/Math/GmpMath.php index 250f7f04..fe409397 100644 --- a/src/Math/GmpMath.php +++ b/src/Math/GmpMath.php @@ -25,7 +25,7 @@ public function equals(\GMP $first, \GMP $other): bool { return gmp_cmp($first, $other) === 0; } - + /** * {@inheritDoc} * @see GmpMathInterface::mod() @@ -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; } /** @@ -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); } /** @@ -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); } /** @@ -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; diff --git a/src/Math/NumberTheory.php b/src/Math/NumberTheory.php index 9b43b477..5a354d0b 100644 --- a/src/Math/NumberTheory.php +++ b/src/Math/NumberTheory.php @@ -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; diff --git a/src/Primitives/JacobianPoint.php b/src/Primitives/JacobianPoint.php index cc457c64..617a3866 100644 --- a/src/Primitives/JacobianPoint.php +++ b/src/Primitives/JacobianPoint.php @@ -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); } /** @@ -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)); diff --git a/src/Random/RandomNumberGenerator.php b/src/Random/RandomNumberGenerator.php index 8b59d6cc..31159b27 100644 --- a/src/Random/RandomNumberGenerator.php +++ b/src/Random/RandomNumberGenerator.php @@ -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); } } diff --git a/tests/unit/Math/MathTest.php b/tests/unit/Math/MathTest.php index 3d4d8533..56ec10b8 100644 --- a/tests/unit/Math/MathTest.php +++ b/tests/unit/Math/MathTest.php @@ -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([ @@ -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], ]); }