From e18d21d587565f8732866d098f63b27c79a0c3b5 Mon Sep 17 00:00:00 2001 From: engn33r Date: Thu, 14 Dec 2023 16:57:11 +0000 Subject: [PATCH 1/2] Optimize short path for wadExp to return zero --- src/utils/SignedWadMath.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/SignedWadMath.sol b/src/utils/SignedWadMath.sol index e7d30a43..09a2a5ce 100644 --- a/src/utils/SignedWadMath.sol +++ b/src/utils/SignedWadMath.sol @@ -107,7 +107,7 @@ function wadExp(int256 x) pure returns (int256 r) { unchecked { // When the result is < 0.5 we return zero. This happens when // x <= floor(log(0.5e18) * 1e18) ~ -42e18 - if (x <= -42139678854452767551) return 0; + if (x <= -41446531673892822313) return 0; // When the result is > (2**255 - 1) / 1e18 we can not represent it as an // int. This happens when x >= floor(log((2**255 - 1) / 1e18) * 1e18) ~ 135. From d398d206f6d73e316cfc4b444b3e33339ce688f3 Mon Sep 17 00:00:00 2001 From: engn33r Date: Thu, 14 Dec 2023 00:00:00 +0000 Subject: [PATCH 2/2] add test, update snapshot, run lint --- .gas-snapshot | 7 ++++--- src/test/CREATE3.t.sol | 6 ++---- src/test/SignedWadMath.t.sol | 7 ++++++- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 13e2410a..45c88307 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -551,12 +551,13 @@ SafeTransferLibTest:testTransferWithReturnsTooMuch() (gas: 37118) SafeTransferLibTest:testTransferWithReturnsTooMuch(address,uint256,bytes) (runs: 256, μ: 36942, ~: 37955) SafeTransferLibTest:testTransferWithStandardERC20() (gas: 36702) SafeTransferLibTest:testTransferWithStandardERC20(address,uint256,bytes) (runs: 256, μ: 36592, ~: 37605) -SignedWadMathTest:testFailWadDivOverflow(int256,int256) (runs: 256, μ: 347, ~: 329) -SignedWadMathTest:testFailWadDivZeroDenominator(int256) (runs: 256, μ: 296, ~: 296) +SignedWadMathTest:testFailWadDivOverflow(int256,int256) (runs: 256, μ: 349, ~: 329) +SignedWadMathTest:testFailWadDivZeroDenominator(int256) (runs: 256, μ: 319, ~: 319) SignedWadMathTest:testFailWadMulEdgeCase() (gas: 286) SignedWadMathTest:testFailWadMulEdgeCase2() (gas: 309) -SignedWadMathTest:testFailWadMulOverflow(int256,int256) (runs: 256, μ: 354, ~: 319) +SignedWadMathTest:testFailWadMulOverflow(int256,int256) (runs: 256, μ: 356, ~: 319) SignedWadMathTest:testWadDiv(uint256,uint256,bool,bool) (runs: 256, μ: 5712, ~: 5714) +SignedWadMathTest:testWadExpZeroPoint() (gas: 771) SignedWadMathTest:testWadMul(uint256,uint256,bool,bool) (runs: 256, μ: 5760, ~: 5762) WETHInvariants:invariantTotalSupplyEqualsBalance() (runs: 256, calls: 3840, reverts: 1776) WETHTest:testDeposit() (gas: 63535) diff --git a/src/test/CREATE3.t.sol b/src/test/CREATE3.t.sol index 7ee8fc0d..d49fccd9 100644 --- a/src/test/CREATE3.t.sol +++ b/src/test/CREATE3.t.sol @@ -40,10 +40,8 @@ contract CREATE3Test is DSTestPlus { bytes32 salt = keccak256(bytes("A salt!")); Factory factory = new Factory(); - MockERC20 deployed = MockERC20( - factory.deploy(salt) - ); - + MockERC20 deployed = MockERC20(factory.deploy(salt)); + assertEq(address(deployed), CREATE3.getDeployed(salt, address(factory))); assertTrue(address(deployed) != CREATE3.getDeployed(salt)); diff --git a/src/test/SignedWadMath.t.sol b/src/test/SignedWadMath.t.sol index 48494480..a79bcb6a 100644 --- a/src/test/SignedWadMath.t.sol +++ b/src/test/SignedWadMath.t.sol @@ -3,7 +3,7 @@ pragma solidity >=0.8.0; import {DSTestPlus} from "./utils/DSTestPlus.sol"; -import {wadMul, wadDiv} from "../utils/SignedWadMath.sol"; +import {wadMul, wadDiv, wadExp} from "../utils/SignedWadMath.sol"; contract SignedWadMathTest is DSTestPlus { function testWadMul( @@ -21,6 +21,11 @@ contract SignedWadMathTest is DSTestPlus { assertEq(wadMul(xPrime, yPrime), (xPrime * yPrime) / 1e18); } + function testWadExpZeroPoint() public { + assertEq(wadExp(-41446531673892822312), 1); + assertEq(wadExp(-41446531673892822313), 0); + } + function testFailWadMulEdgeCase() public pure { int256 x = -1; int256 y = type(int256).min;