From b1d82e888da3665f59405a12613a2646aebc59b7 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Sat, 31 Aug 2024 01:43:30 +0000 Subject: [PATCH] T --- .gas-snapshot | 78 ++++++++++++++++----------------- src/utils/FixedPointMathLib.sol | 20 +++++---- test/FixedPointMathLib.t.sol | 4 +- 3 files changed, 52 insertions(+), 50 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index eb97611f5..b1c389540 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -448,18 +448,18 @@ FixedPointMathLibTest:testDist() (gas: 678) FixedPointMathLibTest:testDist(int256,int256) (runs: 315, μ: 534, ~: 539) FixedPointMathLibTest:testDist(uint256,uint256) (runs: 315, μ: 532, ~: 528) FixedPointMathLibTest:testDistEdgeCases() (gas: 570) -FixedPointMathLibTest:testDivWad() (gas: 747) -FixedPointMathLibTest:testDivWad(uint256,uint256) (runs: 315, μ: 6870, ~: 891) -FixedPointMathLibTest:testDivWadEdgeCases() (gas: 414) -FixedPointMathLibTest:testDivWadOverflowReverts(uint256,uint256) (runs: 315, μ: 22588, ~: 26501) +FixedPointMathLibTest:testDivWad() (gas: 726) +FixedPointMathLibTest:testDivWad(uint256,uint256) (runs: 315, μ: 6798, ~: 854) +FixedPointMathLibTest:testDivWadEdgeCases() (gas: 407) +FixedPointMathLibTest:testDivWadOverflowReverts(uint256,uint256) (runs: 315, μ: 22595, ~: 26494) FixedPointMathLibTest:testDivWadUp() (gas: 3085) FixedPointMathLibTest:testDivWadUp(uint256,uint256) (runs: 315, μ: 7149, ~: 1009) FixedPointMathLibTest:testDivWadUpEdgeCases() (gas: 439) -FixedPointMathLibTest:testDivWadUpOverflowReverts(uint256,uint256) (runs: 315, μ: 22504, ~: 26499) +FixedPointMathLibTest:testDivWadUpOverflowReverts(uint256,uint256) (runs: 315, μ: 22506, ~: 26499) FixedPointMathLibTest:testDivWadUpZeroDenominatorReverts() (gas: 3261) FixedPointMathLibTest:testDivWadUpZeroDenominatorReverts(uint256) (runs: 315, μ: 3295, ~: 3295) -FixedPointMathLibTest:testDivWadZeroDenominatorReverts() (gas: 3218) -FixedPointMathLibTest:testDivWadZeroDenominatorReverts(uint256) (runs: 315, μ: 3337, ~: 3337) +FixedPointMathLibTest:testDivWadZeroDenominatorReverts() (gas: 3211) +FixedPointMathLibTest:testDivWadZeroDenominatorReverts(uint256) (runs: 315, μ: 3330, ~: 3330) FixedPointMathLibTest:testExpWad() (gas: 8047) FixedPointMathLibTest:testFactorial() (gas: 98491) FixedPointMathLibTest:testFactorialOriginal() (gas: 94187) @@ -472,7 +472,7 @@ FixedPointMathLibTest:testFullMulDivUp(uint256,uint256,uint256) (runs: 315, μ: FixedPointMathLibTest:testFullMulDivUpRevertsIfRoundedUpResultOverflowsCase1() (gas: 3694) FixedPointMathLibTest:testFullMulDivUpRevertsIfRoundedUpResultOverflowsCase2() (gas: 3727) FixedPointMathLibTest:testGcd() (gas: 4206) -FixedPointMathLibTest:testGcd(uint256,uint256) (runs: 315, μ: 6655, ~: 3007) +FixedPointMathLibTest:testGcd(uint256,uint256) (runs: 315, μ: 6647, ~: 3007) FixedPointMathLibTest:testIsEven(uint256) (runs: 315, μ: 475, ~: 475) FixedPointMathLibTest:testLambertW0WadAccuracy() (gas: 7160) FixedPointMathLibTest:testLambertW0WadAccuracy(uint184) (runs: 315, μ: 2338, ~: 431) @@ -496,7 +496,7 @@ FixedPointMathLibTest:testLnWadNegativeReverts() (gas: 3318) FixedPointMathLibTest:testLnWadOverflowReverts() (gas: 3316) FixedPointMathLibTest:testLnWadSmall() (gas: 2666) FixedPointMathLibTest:testLog10() (gas: 76168) -FixedPointMathLibTest:testLog10(uint256,uint256) (runs: 315, μ: 2161, ~: 2195) +FixedPointMathLibTest:testLog10(uint256,uint256) (runs: 315, μ: 2162, ~: 2195) FixedPointMathLibTest:testLog10Up() (gas: 4369) FixedPointMathLibTest:testLog2() (gas: 243209) FixedPointMathLibTest:testLog256() (gas: 22831) @@ -513,7 +513,7 @@ FixedPointMathLibTest:testMinSigned(int256,int256) (runs: 315, μ: 498, ~: 503) FixedPointMathLibTest:testMulDiv() (gas: 1890) FixedPointMathLibTest:testMulDiv(uint256,uint256,uint256) (runs: 315, μ: 1603, ~: 794) FixedPointMathLibTest:testMulDivEdgeCases() (gas: 741) -FixedPointMathLibTest:testMulDivOverflowReverts(uint256,uint256,uint256) (runs: 315, μ: 20173, ~: 27078) +FixedPointMathLibTest:testMulDivOverflowReverts(uint256,uint256,uint256) (runs: 315, μ: 20170, ~: 27074) FixedPointMathLibTest:testMulDivUp() (gas: 2148) FixedPointMathLibTest:testMulDivUp(uint256,uint256,uint256) (runs: 315, μ: 1834, ~: 1121) FixedPointMathLibTest:testMulDivUpEdgeCases() (gas: 817) @@ -522,16 +522,16 @@ FixedPointMathLibTest:testMulDivUpZeroDenominator() (gas: 3234) FixedPointMathLibTest:testMulDivUpZeroDenominatorReverts(uint256,uint256) (runs: 315, μ: 3357, ~: 3357) FixedPointMathLibTest:testMulDivZeroDenominatorReverts() (gas: 3256) FixedPointMathLibTest:testMulDivZeroDenominatorReverts(uint256,uint256) (runs: 315, μ: 3380, ~: 3380) -FixedPointMathLibTest:testMulWad() (gas: 736) -FixedPointMathLibTest:testMulWad(uint256,uint256) (runs: 315, μ: 721, ~: 855) -FixedPointMathLibTest:testMulWadEdgeCases() (gas: 714) -FixedPointMathLibTest:testMulWadOverflowReverts(uint256,uint256) (runs: 315, μ: 18920, ~: 26336) -FixedPointMathLibTest:testMulWadUp() (gas: 837) -FixedPointMathLibTest:testMulWadUp(uint256,uint256) (runs: 315, μ: 21434, ~: 1065) -FixedPointMathLibTest:testMulWadUpEdgeCases() (gas: 815) -FixedPointMathLibTest:testMulWadUpOverflowReverts(uint256,uint256) (runs: 315, μ: 18957, ~: 26379) +FixedPointMathLibTest:testMulWad() (gas: 712) +FixedPointMathLibTest:testMulWad(uint256,uint256) (runs: 315, μ: 716, ~: 847) +FixedPointMathLibTest:testMulWadEdgeCases() (gas: 709) +FixedPointMathLibTest:testMulWadOverflowReverts(uint256,uint256) (runs: 315, μ: 18928, ~: 26347) +FixedPointMathLibTest:testMulWadUp() (gas: 813) +FixedPointMathLibTest:testMulWadUp(uint256,uint256) (runs: 315, μ: 21428, ~: 1057) +FixedPointMathLibTest:testMulWadUpEdgeCases() (gas: 810) +FixedPointMathLibTest:testMulWadUpOverflowReverts(uint256,uint256) (runs: 315, μ: 18970, ~: 26390) FixedPointMathLibTest:testPackUnpackSci() (gas: 129327) -FixedPointMathLibTest:testPackUnpackSci(uint256) (runs: 315, μ: 30420, ~: 30425) +FixedPointMathLibTest:testPackUnpackSci(uint256) (runs: 315, μ: 30427, ~: 30425) FixedPointMathLibTest:testRPow() (gas: 3320) FixedPointMathLibTest:testRPowOverflowReverts() (gas: 4974) FixedPointMathLibTest:testRawAdd(int256,int256) (runs: 315, μ: 462, ~: 462) @@ -549,21 +549,21 @@ FixedPointMathLibTest:testRawSub(uint256,uint256) (runs: 315, μ: 440, ~: 440) FixedPointMathLibTest:testSDivWad() (gas: 865) FixedPointMathLibTest:testSDivWad(int256,int256) (runs: 315, μ: 6731, ~: 954) FixedPointMathLibTest:testSDivWadEdgeCases() (gas: 405) -FixedPointMathLibTest:testSDivWadOverflowReverts(int256,int256) (runs: 315, μ: 22219, ~: 26508) +FixedPointMathLibTest:testSDivWadOverflowReverts(int256,int256) (runs: 315, μ: 22218, ~: 26508) FixedPointMathLibTest:testSDivWadZeroDenominatorReverts(int256) (runs: 315, μ: 3327, ~: 3327) FixedPointMathLibTest:testSMulWad() (gas: 1032) FixedPointMathLibTest:testSMulWad(int256,int256) (runs: 315, μ: 876, ~: 1047) FixedPointMathLibTest:testSMulWadEdgeCases() (gas: 1309) -FixedPointMathLibTest:testSMulWadOverflowRevertsOnCondition1(int256,int256) (runs: 315, μ: 19624, ~: 26556) -FixedPointMathLibTest:testSMulWadOverflowRevertsOnCondition2(int256) (runs: 315, μ: 19327, ~: 25788) +FixedPointMathLibTest:testSMulWadOverflowRevertsOnCondition1(int256,int256) (runs: 315, μ: 19625, ~: 26566) +FixedPointMathLibTest:testSMulWadOverflowRevertsOnCondition2(int256) (runs: 315, μ: 19316, ~: 25788) FixedPointMathLibTest:testSMulWadOverflowTrickDifferential(int256,int256) (runs: 315, μ: 643, ~: 654) FixedPointMathLibTest:testSci() (gas: 1838636) -FixedPointMathLibTest:testSci(uint256) (runs: 315, μ: 34107, ~: 37866) -FixedPointMathLibTest:testSci2(uint256) (runs: 315, μ: 956, ~: 955) +FixedPointMathLibTest:testSci(uint256) (runs: 315, μ: 34284, ~: 38563) +FixedPointMathLibTest:testSci2(uint256) (runs: 315, μ: 955, ~: 955) FixedPointMathLibTest:testSqrt() (gas: 43798) FixedPointMathLibTest:testSqrt(uint256) (runs: 315, μ: 1047, ~: 1054) FixedPointMathLibTest:testSqrtBack(uint256) (runs: 315, μ: 9948, ~: 363) -FixedPointMathLibTest:testSqrtHashed(uint256) (runs: 315, μ: 54683, ~: 54659) +FixedPointMathLibTest:testSqrtHashed(uint256) (runs: 315, μ: 54679, ~: 54659) FixedPointMathLibTest:testSqrtHashedSingle() (gas: 54587) FixedPointMathLibTest:testSqrtWad() (gas: 7937) FixedPointMathLibTest:testSqrtWad(uint256) (runs: 315, μ: 1654, ~: 1580) @@ -572,7 +572,7 @@ FixedPointMathLibTest:testSqrtWadMonotonicallyIncreasing(uint256,uint256) (runs: FixedPointMathLibTest:testTernary(bool,uint256,uint256) (runs: 315, μ: 518, ~: 514) FixedPointMathLibTest:testZeroFloorSub(uint256,uint256) (runs: 315, μ: 570, ~: 540) FixedPointMathLibTest:testZeroFloorSubCasted(uint32,uint32,uint256) (runs: 315, μ: 882, ~: 846) -FixedPointMathLibTest:test__codesize() (gas: 45220) +FixedPointMathLibTest:test__codesize() (gas: 45209) GasBurnerLibTest:testBurn() (gas: 105035) GasBurnerLibTest:testBurn(uint256) (runs: 315, μ: 43479, ~: 33806) GasBurnerLibTest:testBurnPure() (gas: 1700850) @@ -798,26 +798,26 @@ LibPRNGTest:testPRNGShuffleGas() (gas: 1610927) LibPRNGTest:testPRNGUniform() (gas: 559430) LibPRNGTest:testStandardNormalWad() (gas: 4425913) LibPRNGTest:test__codesize() (gas: 14449) -LibRLPTest:testComputeAddressDifferential(address,uint256) (runs: 315, μ: 4480, ~: 4353) +LibRLPTest:testComputeAddressDifferential(address,uint256) (runs: 315, μ: 4440, ~: 4287) LibRLPTest:testComputeAddressForLargeNonces() (gas: 1760) LibRLPTest:testComputeAddressForSmallNonces() (gas: 1007) LibRLPTest:testComputeAddressOriginalForLargeNonces() (gas: 3766) LibRLPTest:testComputeAddressOriginalForSmallNonces() (gas: 2185) -LibRLPTest:testRLPEncodeAddressDifferential(address) (runs: 315, μ: 237244, ~: 26224) -LibRLPTest:testRLPEncodeAddressViaList(address,address) (runs: 315, μ: 194371, ~: 28079) +LibRLPTest:testRLPEncodeAddressDifferential(address) (runs: 315, μ: 259061, ~: 26287) +LibRLPTest:testRLPEncodeAddressViaList(address,address) (runs: 315, μ: 151593, ~: 28012) LibRLPTest:testRLPEncodeBool(bool) (runs: 315, μ: 27682, ~: 27818) LibRLPTest:testRLPEncodeBytes() (gas: 382181) LibRLPTest:testRLPEncodeBytes2() (gas: 156293) -LibRLPTest:testRLPEncodeBytesDifferential(bytes32) (runs: 315, μ: 273414, ~: 29721) +LibRLPTest:testRLPEncodeBytesDifferential(bytes32) (runs: 315, μ: 266574, ~: 29578) LibRLPTest:testRLPEncodeList() (gas: 2738950) LibRLPTest:testRLPEncodeList2() (gas: 803256) -LibRLPTest:testRLPEncodeListDifferential(bytes,uint256) (runs: 315, μ: 433825, ~: 39250) +LibRLPTest:testRLPEncodeListDifferential(bytes,uint256) (runs: 315, μ: 442868, ~: 645895) LibRLPTest:testRLPEncodeUint() (gas: 1291481) -LibRLPTest:testRLPEncodeUint(uint256) (runs: 315, μ: 99850, ~: 25002) -LibRLPTest:testRLPEncodeUintDifferential(uint256) (runs: 315, μ: 262020, ~: 27106) -LibRLPTest:testRLPMemory(bytes32) (runs: 315, μ: 299915, ~: 33205) +LibRLPTest:testRLPEncodeUint(uint256) (runs: 315, μ: 105646, ~: 24988) +LibRLPTest:testRLPEncodeUintDifferential(uint256) (runs: 315, μ: 243856, ~: 27082) +LibRLPTest:testRLPMemory(bytes32) (runs: 315, μ: 324197, ~: 34888) LibRLPTest:testRLPPUint256() (gas: 364901) -LibRLPTest:testSmallLog256Equivalence(uint256) (runs: 315, μ: 23678, ~: 23700) +LibRLPTest:testSmallLog256Equivalence(uint256) (runs: 315, μ: 23669, ~: 23700) LibRLPTest:test__codesize() (gas: 15520) LibSortTest:testCopy(uint256) (runs: 315, μ: 56415, ~: 25732) LibSortTest:testInsertionSortAddressesDifferential(uint256) (runs: 315, μ: 45216, ~: 24962) @@ -1157,15 +1157,15 @@ ReentrancyGuardTest:testRevertRecursiveDirectGuardedCall() (gas: 55123) ReentrancyGuardTest:testRevertRecursiveIndirectGuardedCall() (gas: 56529) ReentrancyGuardTest:testRevertRemoteCallback() (gas: 56230) ReentrancyGuardTest:test__codesize() (gas: 4403) -SSTORE2Test:testReadRevertsOnZeroCodeAddress(address) (runs: 315, μ: 108162, ~: 30000) +SSTORE2Test:testReadRevertsOnZeroCodeAddress(address) (runs: 315, μ: 93791, ~: 30000) SSTORE2Test:testReadSlicing() (gas: 48874) SSTORE2Test:testWriteRead() (gas: 35880) -SSTORE2Test:testWriteRead(uint256,uint256) (runs: 315, μ: 1105332, ~: 67277) -SSTORE2Test:testWriteReadCounterfactual(bytes,bytes32,address) (runs: 315, μ: 4107283, ~: 108268) +SSTORE2Test:testWriteRead(uint256,uint256) (runs: 315, μ: 1113188, ~: 67176) +SSTORE2Test:testWriteReadCounterfactual(bytes,bytes32,address) (runs: 315, μ: 4414484, ~: 108268) SSTORE2Test:testWriteReadCustomBounds() (gas: 33936) SSTORE2Test:testWriteReadCustomStartBound() (gas: 34020) SSTORE2Test:testWriteReadDeterministic() (gas: 70156) -SSTORE2Test:testWriteReadDeterministic(bytes,bytes32) (runs: 315, μ: 3478987, ~: 96934) +SSTORE2Test:testWriteReadDeterministic(bytes,bytes32) (runs: 315, μ: 4093496, ~: 96934) SSTORE2Test:testWriteReadEmptyBound() (gas: 33734) SSTORE2Test:testWriteReadFullBoundedRead() (gas: 35937) SSTORE2Test:testWriteReadFullStartBound() (gas: 33983) diff --git a/src/utils/FixedPointMathLib.sol b/src/utils/FixedPointMathLib.sol index 235c6a7c7..587564cf9 100644 --- a/src/utils/FixedPointMathLib.sol +++ b/src/utils/FixedPointMathLib.sol @@ -65,9 +65,11 @@ library FixedPointMathLib { /// @solidity memory-safe-assembly assembly { // Equivalent to `require(y == 0 || x <= type(uint256).max / y)`. - if mul(y, gt(x, div(not(0), y))) { - mstore(0x00, 0xbac65e5b) // `MulWadFailed()`. - revert(0x1c, 0x04) + if gt(x, div(not(0), y)) { + if y { + mstore(0x00, 0xbac65e5b) // `MulWadFailed()`. + revert(0x1c, 0x04) + } } z := div(mul(x, y), WAD) } @@ -108,9 +110,11 @@ library FixedPointMathLib { /// @solidity memory-safe-assembly assembly { // Equivalent to `require(y == 0 || x <= type(uint256).max / y)`. - if mul(y, gt(x, div(not(0), y))) { - mstore(0x00, 0xbac65e5b) // `MulWadFailed()`. - revert(0x1c, 0x04) + if gt(x, div(not(0), y)) { + if y { + mstore(0x00, 0xbac65e5b) // `MulWadFailed()`. + revert(0x1c, 0x04) + } } z := add(iszero(iszero(mod(mul(x, y), WAD))), div(mul(x, y), WAD)) } @@ -128,8 +132,8 @@ library FixedPointMathLib { function divWad(uint256 x, uint256 y) internal pure returns (uint256 z) { /// @solidity memory-safe-assembly assembly { - // Equivalent to `require(y != 0 && (WAD == 0 || x <= type(uint256).max / WAD))`. - if iszero(mul(y, iszero(mul(WAD, gt(x, div(not(0), WAD)))))) { + // Equivalent to `require(y != 0 && x <= type(uint256).max / WAD)`. + if or(iszero(y), gt(x, div(not(0), WAD))) { mstore(0x00, 0x7c5f487d) // `DivWadFailed()`. revert(0x1c, 0x04) } diff --git a/test/FixedPointMathLib.t.sol b/test/FixedPointMathLib.t.sol index de6a3c775..5b84a7d98 100644 --- a/test/FixedPointMathLib.t.sol +++ b/test/FixedPointMathLib.t.sol @@ -1168,7 +1168,6 @@ contract FixedPointMathLibTest is SoladyTest { unchecked { if (x != 0 && (x * y) / x != y) return; } - uint256 result = FixedPointMathLib.mulWad(x, y); assertEq(result, (x * y) / 1e18); assertEq(FixedPointMathLib.rawMulWad(x, y), result); @@ -1179,7 +1178,6 @@ contract FixedPointMathLibTest is SoladyTest { unchecked { if ((x != 0 && (x * y) / x != y) || (x == -1 && y == type(int256).min)) return; } - int256 result = FixedPointMathLib.sMulWad(x, y); assertEq(result, int256((x * y) / 1e18)); assertEq(FixedPointMathLib.rawSMulWad(x, y), result); @@ -1239,7 +1237,7 @@ contract FixedPointMathLibTest is SoladyTest { function testDivWad(uint256 x, uint256 y) public { // Ignore cases where x * WAD overflows or y is 0. unchecked { - while (y == 0 || (x != 0 && (x * 1e18) / 1e18 != x)) { + while (y == 0 || (x * 1e18) / 1e18 != x) { x = _random(); y = _random(); }