Skip to content

Commit

Permalink
Replace GMP function calls with overloaded operators (#90)
Browse files Browse the repository at this point in the history
Fixes #89 (though it really shouldn't).
  • Loading branch information
Firehed authored Nov 26, 2024
1 parent d324c80 commit 1031cc6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
1 change: 0 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ jobs:
run: vendor/bin/phpunit
--coverage-clover coverage.xml
--coverage-text
--printer mheap\\GithubActionsReporter\\Printer

- name: Submit code coverage
if: ${{ always() }}
Expand Down
18 changes: 8 additions & 10 deletions src/PublicKey/EllipticCurve.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
use Sop\ASN1\Type as ASN;
use UnexpectedValueException;

use function gmp_pow;
use function gmp_add;
use function gmp_cmp;
use function gmp_import;
use function gmp_mod;
use function gmp_mul;

/**
* @internal
Expand Down Expand Up @@ -156,13 +152,15 @@ private function isOnCurve(): bool

// This is only tested with P256 (secp256r1) but SHOULD be the same for
// the other curves (none of which are supported yet)/
$x3 = gmp_pow($x, 3);
$ax = gmp_mul($a, $x);
$rhs = gmp_mod(gmp_add($x3, gmp_add($ax, $b)), $p);
$x3 = $x ** 3; // @phpstan-ignore binaryOp.invalid (phpstan/phpstan#12123)
$ax = $a * $x; // @phpstan-ignore binaryOp.invalid
$rhs = ($x3 + $ax + $b) % $p; // @phpstan-ignore binaryOp.invalid

$y2 = gmp_pow($y, 2);
$lhs = gmp_mod($y2, $p);
$y2 = $y ** 2; // @phpstan-ignore binaryOp.invalid
$lhs = $y2 % $p;

return 0 === gmp_cmp($lhs, $rhs); // Values match
// Functionaly, `$lhs === $rhs` but avoids reference equality issues
// w/out having to introduce loose comparision ($lhs == $rhs works)
return 0 === gmp_cmp($lhs, $rhs);
}
}

0 comments on commit 1031cc6

Please sign in to comment.