Skip to content

Commit

Permalink
test(finite-field): add tests for non-canonical zero handling in sqrt…
Browse files Browse the repository at this point in the history
… and isSquare methods

The commit adds two new test cases to the finite field unit tests:

1. It asserts that calling `sqrt` with a non-canonical zero (i.e., `p`) should return the same result as calling it with a canonical zero (`0n`). This ensures that the `sqrt` method handles non-canonical zero inputs correctly.

2. It asserts that calling `isSquare` with a non-canonical zero (`p`) should return the same result as calling it with a canonical zero (`0n`). This ensures that the `isSquare` method treats non-canonical zero inputs the same way as canonical zero.

These tests were added to improve the robustness of the finite field implementation by verifying that it correctly handles non-canonical zero values, which are equivalent to the canonical zero value in the field.
  • Loading branch information
MartinMinkov committed Jul 23, 2024
1 parent 6f46ec8 commit 9aad038
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crypto/finite-field.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ for (let F of fields) {
let squareX = F.square(x);
assert(F.isSquare(squareX), 'square + isSquare');
assert([x, F.negate(x)].includes(F.sqrt(squareX)!), 'square + sqrt');
assert.equal(F.sqrt(0n), F.sqrt(p), 'sqrt handles non-canonical 0');

if (F.M >= 2n) {
assert(F.isSquare(p - 1n), 'isSquare -1');
assert.equal(
F.isSquare(0n),
F.isSquare(p),
'isSquare handles non-canonical 0'
);
let i = F.power(F.twoadicRoot, 1n << (F.M - 2n));
assert([i, F.negate(i)].includes(F.sqrt(p - 1n)!), 'sqrt -1');
}
Expand Down

0 comments on commit 9aad038

Please sign in to comment.