Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor fuzzing
  • Loading branch information
Amxx committed Jan 17, 2024
commit 6ea4d455b492096b7c82762a3fb4510ea58dcd19
50 changes: 22 additions & 28 deletions test/utils/math/Math.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,38 +56,32 @@ contract MathTest is Test {
}

// INV
function testInv1(uint256 seed) public {
uint256 p;
uint256 value;
uint256 inverse;

// 17 is a prime
p = 17;
value = bound(seed, 1, p - 1);
inverse = Math.inv(value, p);
assertEq(mulmod(value, inverse, p), 1);
assertLt(inverse, p);

// 65537 is a prime
p = 65537;
value = bound(seed, 1, p - 1);
inverse = Math.inv(value, p);
assertEq(mulmod(value, inverse, p), 1);
assertLt(inverse, p);

// 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff is a prime
p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff;
value = bound(seed, 1, p - 1);
inverse = Math.inv(value, p);
assertEq(mulmod(value, inverse, p), 1);
assertLt(inverse, p);
}

function testInv2(uint256 value, uint256 p) public {
function testInv(uint256 value, uint256 p) public {
_testInv(value, p, true);
}

function testInv17(uint256 seed) public {
uint256 p = 17; // prime
_testInv(bound(seed, 1, p - 1), p, false);
}

function testInv65537(uint256 seed) public {
uint256 p = 65537; // prime
_testInv(bound(seed, 1, p - 1), p, false);
}

function testInvP256(uint256 seed) public {
uint256 p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff; // prime
Copy link
Member

Choose a reason for hiding this comment

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

Wow now I get why the rumors of a backdoor in secp256r1, this is a weird number

_testInv(bound(seed, 1, p - 1), p, false);
}

function _testInv(uint256 value, uint256 p, bool allowZero) private {
uint256 inverse = Math.inv(value, p);
if (inverse != 0) {
assertEq(mulmod(value, inverse, p), 1);
assertLt(inverse, p);
} else {
assertTrue(allowZero);
}
}

Expand Down