Skip to content

Commit ca2c2f6

Browse files
committed
modPow and modInverse tests
1 parent ba6f03b commit ca2c2f6

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

test/Secp256k1.t.sol

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ contract Secp256k1Test is Test {
135135

136136
// ! Expecting revert with internal function calls -> https://book.getfoundry.sh/cheatcodes/expect-revert?highlight=expectRevert#error
137137
/// forge-config: default.allow_internal_expect_revert = true
138-
function testFuzz_negatePointUnchecked_OutOfBounds(uint256 x, uint256 y) public {
138+
function testFuzz_negatePointUnchecked_RevertIf_OutOfBounds(uint256 x, uint256 y) public {
139139
x = bound(x, Secp256k1.p + 1, UINT256_MAX);
140140
y = bound(y, Secp256k1.p + 1, UINT256_MAX);
141141
console.log(x, y);
@@ -259,6 +259,30 @@ contract Secp256k1Test is Test {
259259
vm.expectRevert(abi.encodeWithSelector(Secp256k1.Secp256k1__InvalidPoint.selector, x, y));
260260
Secp256k1.addPoints(x, y, Gx, Gy);
261261
}
262-
263262
// TODO -> add gas tests for unchecked
263+
// TODO -> add tests for doubling point
264+
265+
// ! -----------------------------------------------------------------------------------------------------------------------
266+
// ! modInverse() TESTS
267+
// ! -----------------------------------------------------------------------------------------------------------------------
268+
function testFuzz_modInverse(uint256 number) public pure {
269+
number = bound(number, 1, Secp256k1.p - 1);
270+
uint256 inverse = Secp256k1.modInverse(number);
271+
assertEq(mulmod(number, inverse, Secp256k1.p), 1);
272+
}
273+
274+
// ! -----------------------------------------------------------------------------------------------------------------------
275+
// ! modPow() TESTS
276+
// ! -----------------------------------------------------------------------------------------------------------------------
277+
function testFuzz_ModPow(uint256 base, uint256 exponent) public pure {
278+
// To avoid high gas cost in the fuzz test, restricted the exponent to a reasonable range.
279+
vm.assume(exponent < 1_000_000);
280+
281+
uint256 result = Secp256k1.modPow(base, exponent);
282+
uint256 expected = 1;
283+
for (uint256 i = 0; i < exponent; i++) {
284+
expected = mulmod(expected, base, Secp256k1.p);
285+
}
286+
assertEq(result, expected);
287+
}
264288
}

0 commit comments

Comments
 (0)