Skip to content

ext/gmp: Add GMP ECC test #18363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

ext/gmp: Add GMP ECC test #18363

wants to merge 4 commits into from

Conversation

famoser
Copy link
Contributor

@famoser famoser commented Apr 20, 2025

Add a test that executes GMP operations in the context of elliptic curve.

This work is motivated by a bug from a couple of months ago (see #16870), which showed this use-case is uncovered by tests in PHP. The bug was fixed quickly, and in #16896 a unit test already landed for finite field cryptography. The test for elliptic curve cryptography is more complicated (as seen in this PR), so this was decided to be done later: #16896 (comment)

This unit test is written based on code developed for https://github.com/famoser/elliptic, which has been tested extensively. However, the code is not ready to use for security-related applications, as it has not been hardened nor tested against possible side channels. Side channels could leak the secret values, and therefore compromise the security of the algorithms.

@Girgias
Copy link
Member

Girgias commented Apr 22, 2025

Can you maybe rebase and force push on the latest master to see if it fixes the FreeBSD pipeline? As that is unrelated to this PR

@famoser famoser changed the base branch from PHP-8.1 to master April 23, 2025 06:32
@famoser famoser requested a review from Girgias as a code owner April 23, 2025 06:32
@famoser famoser changed the title test: Add GMP ECC test ext/gmp: Add GMP ECC test Apr 26, 2025
@famoser
Copy link
Contributor Author

famoser commented Apr 26, 2025

@Girgias The tests pass now (apparently just flaky). I've also added some documentation for the point decompression math, and a new output check to ensure the test does not pass for trivial calculation (e.g. all 0s). Anything should now either be documented or reference the spec now. If this is not the case, please mention it in your review.

While asking around whether I've covered all fundamental operations of ECC, I was made aware of that Parings (as in Weil-Pairing) could be seen as another fundamental operation. For me, this would be another possible PR in the future; but out of scope for now (as I first have to get used to the exact math there).

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSMT, added two small comments

Comment on lines +150 to +156
public function isOnCurve(Point $point): bool
{
$left = gmp_pow($point->y, 2);
$right = gmp_add(
gmp_add(
gmp_pow($point->x, 3),
gmp_mul($this->curve->getA(), $point->x)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be interesting to have a class that reimplements this using the overloaded operators, to ensure the behaviour is consistent. :)

Copy link
Contributor Author

@famoser famoser Apr 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

However, to avoid duplicating this complicated & slow ECC code, to test the method operators I would probably go for a "kitchen-sink" kind of test. So something like, for reasonably big GMP $a and GMP $b:

var_dump(gmp_cmp(gmp_add($a, $b), $a + $b) === 0);
var_dump(gmp_cmp(gmp_sub($a, $b), $a - $b) === 0);
var_dump(gmp_cmp(gmp_mul($a, $b), $a * $b) === 0);

What do you think / does that make sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might already have a kitchen sink test, so maybe it's better to just change this test to use operator overloading?

Copy link
Contributor Author

@famoser famoser Apr 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the kitchen sink test is in ext/gmp/tests/gmp_cryptography.phpt. Thinking about it, I'd rather change the kitchen sink test to use the operators. This way, the gmp_cryptography_ecc.phpt test remains close to how this would likely be written in crypto libraries: My expectation would be that these try to absolutely minimize risk; hence using operator overloading where there is a risk that the wrong operator is applied due to type confusion seems like a no-go.

So I would:

  • Extend gmp_cryptography.phpt to do all gmp operations with reasonably big numbers (around 15360 bits for operations in (integer) finite fields, around 512 bits for operations used in ECC; see NIST security strength 256).
  • Perform each operation once with the real function, and once with its operator overload.

What do you think about the proposal?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MSTM, thanks for the insights :)

Co-authored-by: Gina Peter Banyard <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants