Skip to content
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

Optimize overflow check in mulDivRoundingUp #667

Merged
merged 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1974
1961
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_oneForZero_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1713
1700
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2982
2956
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2091
2073
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactInPartial.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3044
3021
2 changes: 1 addition & 1 deletion .forge-snapshots/SwapMath_zeroForOne_exactOutCapped.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1830
1812
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2572
2554
Original file line number Diff line number Diff line change
@@ -1 +1 @@
153006
152993
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity CA fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
330666
330653
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with empty hook.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
285662
285649
2 changes: 1 addition & 1 deletion .forge-snapshots/addLiquidity with native token.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
143216
143203
Original file line number Diff line number Diff line change
@@ -1 +1 @@
301184
301171
Original file line number Diff line number Diff line change
@@ -1 +1 @@
665
652
Original file line number Diff line number Diff line change
@@ -1 +1 @@
776
763
Original file line number Diff line number Diff line change
@@ -1 +1 @@
856
843
2 changes: 1 addition & 1 deletion .forge-snapshots/poolManager bytecode size.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
21173
21161
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap with native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
115813
115780
2 changes: 1 addition & 1 deletion .forge-snapshots/simple swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
130974
130941
2 changes: 1 addition & 1 deletion .forge-snapshots/swap CA fee on unspecified.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
182094
182061
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111942
111903
2 changes: 1 addition & 1 deletion .forge-snapshots/swap against liquidity.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
123285
123246
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135150
135124
2 changes: 1 addition & 1 deletion .forge-snapshots/swap burn native 6909 for input.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
124397
124371
2 changes: 1 addition & 1 deletion .forge-snapshots/swap mint output as 6909.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
162828
162795
Original file line number Diff line number Diff line change
@@ -1 +1 @@
220585
220513
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
147029
146996
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with hooks.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
142010
141971
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with lp fee and protocol fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
179060
179040
2 changes: 1 addition & 1 deletion .forge-snapshots/swap with return dynamic fee.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154882
154849
2 changes: 1 addition & 1 deletion .forge-snapshots/update dynamic fee in before swap.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157485
157452
5 changes: 2 additions & 3 deletions src/libraries/FullMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ library FullMath {
function mulDivRoundingUp(uint256 a, uint256 b, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) > 0) {
require(result < type(uint256).max);
result++;
if (mulmod(a, b, denominator) != 0) {
require(++result > 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

looks good. I see that the test test_fuzz_mulDivRoundingUp isn't very good because it checks

assertTrue(result == numerator / d || result == numerator / d + 1);

🥲
please could you update it to correctly identify whether it should have +1 or not with a mod?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

}
}
}
Expand Down
10 changes: 7 additions & 3 deletions test/libraries/FullMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract FullMathTest is Test {
function test_fuzz_mulDiv(uint256 x, uint256 y, uint256 d) public pure {
vm.assume(d != 0);
vm.assume(y != 0);
vm.assume(x <= type(uint256).max / y);
x = bound(x, 0, type(uint256).max / y);
assertEq(FullMath.mulDiv(x, y, d), x * y / d);
}

Expand Down Expand Up @@ -97,10 +97,14 @@ contract FullMathTest is Test {
function test_fuzz_mulDivRoundingUp(uint256 x, uint256 y, uint256 d) public pure {
vm.assume(d != 0);
vm.assume(y != 0);
vm.assume(x <= type(uint256).max / y);
x = bound(x, 0, type(uint256).max / y);
uint256 numerator = x * y;
uint256 result = FullMath.mulDivRoundingUp(x, y, d);
assertTrue(result == numerator / d || result == numerator / d + 1);
if (mulmod(x, y, d) > 0) {
assertEq(result, numerator / d + 1);
} else {
assertEq(result, numerator / d);
}
}

function test_invariant_mulDivRounding(uint256 x, uint256 y, uint256 d) public pure {
Expand Down
Loading