Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
91e39eb
Added Wrapper Library for ModularExponentiation along with tests.
mw2000 Mar 27, 2022
712a0f3
Fixed linting issues.
mw2000 Mar 27, 2022
77f33ea
Added function to Math.sol and migrated tests.
mw2000 Apr 9, 2022
4683f26
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
mw2000 Dec 23, 2022
7489ce4
Merge remote-tracking branch 'origin/master' into modular-exponentiat…
mw2000 Jun 8, 2023
d576641
Added tests
mw2000 Jun 9, 2023
0d78d29
Fixed lint
mw2000 Jun 9, 2023
5d99ed8
Added changeset
mw2000 Jun 9, 2023
acb16f2
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
mw2000 Jun 9, 2023
d7e81cf
Fixed codespell error
mw2000 Jun 9, 2023
85187dd
Fixed function calling in tests
mw2000 Jun 10, 2023
d458765
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
mw2000 Jun 11, 2023
01badeb
Update contracts/utils/math/Math.sol
Amxx Jun 12, 2023
a1c1439
Added a restriction to e
mw2000 Jun 25, 2023
d81d69d
Merge branch 'modular-exponentiation-precompile-wrapper-#1985' of htt…
mw2000 Jun 25, 2023
6e8cced
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
mw2000 Jun 25, 2023
fd7b8de
All args should be non-zero
mw2000 Jun 25, 2023
26a036e
Seperating the e and m != 0 out
mw2000 Jun 25, 2023
4ba0a29
Fixed test when m = 1
mw2000 Nov 8, 2023
988c950
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
mw2000 Nov 8, 2023
c08ac50
Added last bracket.
mw2000 Nov 8, 2023
98f7994
Fixed lint issues
mw2000 Nov 8, 2023
f634aab
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
mw2000 Dec 24, 2023
66a0c1f
Added custom errors + tests for it
mw2000 Dec 24, 2023
eecd818
Added comments for errors
mw2000 Dec 24, 2023
1583160
Added a not to be reverted for custom error
mw2000 Dec 24, 2023
19ead8e
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
Amxx Jan 25, 2024
8cf355f
better fuzzing for modexp
Amxx Jan 25, 2024
113e85e
cleanup tests
Amxx Jan 25, 2024
4e1cf0d
Update .changeset/shiny-poets-whisper.md
Amxx Jan 25, 2024
6d7c154
return 0 instead of reverting in case of error
Amxx Jan 25, 2024
84b285d
fallback to revert
Amxx Jan 25, 2024
9e73f46
simplification
Amxx Jan 25, 2024
76c9afa
cleanup
Amxx Jan 25, 2024
1ff0776
Refactor fuzz test
ernestognw Jan 29, 2024
f84b1b6
Lint
ernestognw Jan 29, 2024
fe32a38
mulDiv fuzzing with no external
Amxx Jan 29, 2024
4accc2e
modExp in assembly to avoid memory leaks
Amxx Jan 31, 2024
cfd80e9
optimise and mark as memory-safe
Amxx Jan 31, 2024
526d6b9
add note about using Ferma's little theorem to compute inverse in big…
Amxx Jan 31, 2024
3718090
Add utils/Panic.sol library
Amxx Feb 1, 2024
cd2f2e9
add missing files
Amxx Feb 1, 2024
104002e
fix lint
Amxx Feb 1, 2024
d149ea6
mark constants as internal
Amxx Feb 1, 2024
05aa60e
slither disable
Amxx Feb 1, 2024
f352681
add unit testing for Panic
Amxx Feb 1, 2024
32ea4bb
Update test/utils/Panic.test.js
Amxx Feb 1, 2024
2e962c8
fix lint
Amxx Feb 1, 2024
275c959
Update Panic.sol
Amxx Feb 1, 2024
24cd52a
Implement tryMod
ernestognw Feb 2, 2024
50374a1
Implement review suggestions
ernestognw Feb 2, 2024
ee91836
fix tryModExp behavior and test
Amxx Feb 2, 2024
d13e52d
better vm.expectRevert check
Amxx Feb 2, 2024
969e259
use stdErrors for forge-std
Amxx Feb 2, 2024
e64c3f9
remove unecessary import
Amxx Feb 2, 2024
06220be
add a mock to force import libraries
Amxx Feb 2, 2024
9137bae
rename panic codes following libsolutil
Amxx Feb 2, 2024
81e8ba0
fix lint
Amxx Feb 2, 2024
2b72050
Merge branch 'master' into modular-exponentiation-precompile-wrapper-…
Amxx Feb 2, 2024
2fc20d4
Nits
ernestognw Feb 2, 2024
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
better fuzzing for modexp
  • Loading branch information
Amxx committed Jan 25, 2024
commit 8cf355f78c44a1e9a43e86e42d3132659b55b4a2
2 changes: 1 addition & 1 deletion contracts/utils/math/Math.sol
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ library Math {

/**
* @dev Returns the modular exponentiation of the specified base,
* exponent and modulus (b^e % m)
* exponent and modulus (b ** e % m)
*
* Requirements:
*
Expand Down
4 changes: 2 additions & 2 deletions scripts/generate/templates/Checkpoints.t.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ function _bound${capitalize(opts.keyTypeName)}(
${opts.keyTypeName} x,
${opts.keyTypeName} min,
${opts.keyTypeName} max
) internal view returns (${opts.keyTypeName}) {
) internal pure returns (${opts.keyTypeName}) {
return SafeCast.to${capitalize(opts.keyTypeName)}(bound(uint256(x), uint256(min), uint256(max)));
}

function _prepareKeys(
${opts.keyTypeName}[] memory keys,
${opts.keyTypeName} maxSpread
) internal view {
) internal pure {
${opts.keyTypeName} lastKey = 0;
for (uint256 i = 0; i < keys.length; ++i) {
${opts.keyTypeName} key = _bound${capitalize(opts.keyTypeName)}(keys[i], lastKey, lastKey + maxSpread);
Expand Down
42 changes: 20 additions & 22 deletions test/utils/math/Math.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -208,38 +208,36 @@ contract MathTest is Test {

// MOD EXP
function testModExp(uint256 b, uint256 e, uint256 m) public {
// First, let's assume m is not 0, as that will cause a division by zero error
vm.assume(m != 0);
vm.assume(e < 10);

uint256 result = Math.modExp(b, e, m);

// Next, let's check the correctness of the result
// The result of b^e mod m should be less than m
assertTrue(result < m);

if (m == 1) {
// If the modulo m is 1, then result will always be 0
assertEq(result, 0);
} else {
// The result should also satisfy the equation: result = b^e mod m
// Due to the large numbers involved in this calculation, we can't directly calculate b^e
// However, we can repeatedly multiply and reduce modulo m
try this.modexp(b, e, m) returns (uint256 result) {
assertTrue(result < m);
assertEq(result, _nativeModExp(b, e, m));
} catch {
assertEq(m, 0);
}
}

uint256 expected = 1;
for (uint i = 0; i < e; i++) {
expected = _mulmod(expected, b, m);
function _nativeModExp(uint256 b, uint256 e, uint256 m) private pure returns (uint256) {
uint256 r = 1 % m;
uint256 d = b;
while (e > 0) {
if (e % 2 > 0) {
r = mulmod(r, d, m);
}

assertEq(result, expected);
d = mulmod(d, d, m);
e >>= 1;
}
return r;
}

// External call
function muldiv(uint256 x, uint256 y, uint256 d) external pure returns (uint256) {
return Math.mulDiv(x, y, d);
}

function modexp(uint256 a, uint256 k, uint256 n) external view returns (uint256) {
return Math.modExp(a, k, n);
}

// Helpers
function _asRounding(uint8 r) private pure returns (Math.Rounding) {
vm.assume(r < uint8(type(Math.Rounding).max));
Expand Down
12 changes: 6 additions & 6 deletions test/utils/structs/Checkpoints.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ contract CheckpointsTrace224Test is Test {
Checkpoints.Trace224 internal _ckpts;

// helpers
function _boundUint32(uint32 x, uint32 min, uint32 max) internal view returns (uint32) {
function _boundUint32(uint32 x, uint32 min, uint32 max) internal pure returns (uint32) {
return SafeCast.toUint32(bound(uint256(x), uint256(min), uint256(max)));
}

function _prepareKeys(uint32[] memory keys, uint32 maxSpread) internal view {
function _prepareKeys(uint32[] memory keys, uint32 maxSpread) internal pure {
uint32 lastKey = 0;
for (uint256 i = 0; i < keys.length; ++i) {
uint32 key = _boundUint32(keys[i], lastKey, lastKey + maxSpread);
Expand Down Expand Up @@ -125,11 +125,11 @@ contract CheckpointsTrace208Test is Test {
Checkpoints.Trace208 internal _ckpts;

// helpers
function _boundUint48(uint48 x, uint48 min, uint48 max) internal view returns (uint48) {
function _boundUint48(uint48 x, uint48 min, uint48 max) internal pure returns (uint48) {
return SafeCast.toUint48(bound(uint256(x), uint256(min), uint256(max)));
}

function _prepareKeys(uint48[] memory keys, uint48 maxSpread) internal view {
function _prepareKeys(uint48[] memory keys, uint48 maxSpread) internal pure {
uint48 lastKey = 0;
for (uint256 i = 0; i < keys.length; ++i) {
uint48 key = _boundUint48(keys[i], lastKey, lastKey + maxSpread);
Expand Down Expand Up @@ -233,11 +233,11 @@ contract CheckpointsTrace160Test is Test {
Checkpoints.Trace160 internal _ckpts;

// helpers
function _boundUint96(uint96 x, uint96 min, uint96 max) internal view returns (uint96) {
function _boundUint96(uint96 x, uint96 min, uint96 max) internal pure returns (uint96) {
return SafeCast.toUint96(bound(uint256(x), uint256(min), uint256(max)));
}

function _prepareKeys(uint96[] memory keys, uint96 maxSpread) internal view {
function _prepareKeys(uint96[] memory keys, uint96 maxSpread) internal pure {
uint96 lastKey = 0;
for (uint256 i = 0; i < keys.length; ++i) {
uint96 key = _boundUint96(keys[i], lastKey, lastKey + maxSpread);
Expand Down