From 35a25db8edb3da809f62a28c9d1de14ce2d9bdf5 Mon Sep 17 00:00:00 2001 From: Michael Zhu Date: Wed, 16 Aug 2023 12:38:40 -0400 Subject: [PATCH 1/5] simplify mulDivDown --- src/libraries/MathLib.sol | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/libraries/MathLib.sol b/src/libraries/MathLib.sol index 95fb2d27a..6293a1a0b 100644 --- a/src/libraries/MathLib.sol +++ b/src/libraries/MathLib.sol @@ -33,15 +33,7 @@ library MathLib { /// @dev (x * y) / denominator rounded down. function mulDivDown(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 z) { - // Division by zero if denominator == 0. - // Overflow if - // x * y > type(uint256).max - // <=> y > 0 and x > type(uint256).max / y - assembly { - if or(mul(y, gt(x, div(MAX_UINT256, y))), iszero(denominator)) { revert(0, 0) } - - z := div(mul(x, y), denominator) - } + return (x * y) / denominator; } /// @dev (x * y) / denominator rounded up. From b33972d5f1aafe81ef8871dd92d7471def6103be Mon Sep 17 00:00:00 2001 From: Michael Zhu Date: Wed, 16 Aug 2023 12:46:48 -0400 Subject: [PATCH 2/5] Optimize wTaylorCompounded using constants --- src/libraries/MathLib.sol | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libraries/MathLib.sol b/src/libraries/MathLib.sol index 6293a1a0b..cdb4291eb 100644 --- a/src/libraries/MathLib.sol +++ b/src/libraries/MathLib.sol @@ -10,6 +10,8 @@ uint256 constant WAD = 1e18; /// @dev Inspired by https://github.com/morpho-org/morpho-utils. library MathLib { uint256 internal constant MAX_UINT256 = 2 ** 256 - 1; + uint256 private constant TWO_WAD = 2 * WAD; + uint256 private constant THREE_WAD = 3 * WAD; /// @dev (x * y) / WAD rounded down. function wMulDown(uint256 x, uint256 y) internal pure returns (uint256) { @@ -55,8 +57,8 @@ library MathLib { /// to approximate a continuous compound interest rate: e^(nx) - 1. function wTaylorCompounded(uint256 x, uint256 n) internal pure returns (uint256) { uint256 firstTerm = x * n; - uint256 secondTerm = wMulDown(firstTerm, firstTerm) / 2; - uint256 thirdTerm = wMulDown(secondTerm, firstTerm) / 3; + uint256 secondTerm = mulDivDown(firstTerm, firstTerm, TWO_WAD); + uint256 thirdTerm = mulDivDown(secondTerm, firstTerm, THREE_WAD); return firstTerm + secondTerm + thirdTerm; } From ef712fbb621f1c42453901091c5f33e4ca8326f8 Mon Sep 17 00:00:00 2001 From: MathisGD Date: Fri, 18 Aug 2023 23:15:52 +0200 Subject: [PATCH 3/5] perf: mulDivUp full solidity --- src/libraries/MathLib.sol | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/libraries/MathLib.sol b/src/libraries/MathLib.sol index cdb4291eb..e0636b94b 100644 --- a/src/libraries/MathLib.sol +++ b/src/libraries/MathLib.sol @@ -34,23 +34,13 @@ library MathLib { } /// @dev (x * y) / denominator rounded down. - function mulDivDown(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 z) { + function mulDivDown(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256) { return (x * y) / denominator; } /// @dev (x * y) / denominator rounded up. - function mulDivUp(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 z) { - // Underflow if denominator == 0. - // Division by 0 if denominator == 0 (this case cannot occure since the above underflow happens before). - // Overflow if - // x * y + denominator - 1 > type(uint256).max - // <=> x * y > type(uint256).max - denominator - 1 - // <=> y > 0 and x > (type(uint256).max - denominator - 1) / y - assembly { - if or(mul(y, gt(x, div(sub(MAX_UINT256, sub(denominator, 1)), y))), iszero(denominator)) { revert(0, 0) } - - z := div(add(mul(x, y), sub(denominator, 1)), denominator) - } + function mulDivUp(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256) { + return (x * y + (denominator - 1)) / denominator; } /// @dev The sum of the last three terms in a four term taylor series expansion From 38393138f72aa55b93c1f40b9312415caec5c525 Mon Sep 17 00:00:00 2001 From: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> Date: Fri, 18 Aug 2023 23:19:37 +0200 Subject: [PATCH 4/5] refactor: remove useless var Signed-off-by: Merlin Egalite <44097430+MerlinEgalite@users.noreply.github.com> --- src/libraries/MathLib.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libraries/MathLib.sol b/src/libraries/MathLib.sol index e0636b94b..b6d645a4d 100644 --- a/src/libraries/MathLib.sol +++ b/src/libraries/MathLib.sol @@ -9,7 +9,6 @@ uint256 constant WAD = 1e18; /// @notice Library to manage fixed-point arithmetic. /// @dev Inspired by https://github.com/morpho-org/morpho-utils. library MathLib { - uint256 internal constant MAX_UINT256 = 2 ** 256 - 1; uint256 private constant TWO_WAD = 2 * WAD; uint256 private constant THREE_WAD = 3 * WAD; From 7b21dd03f46965eccc0d205daa23d4cbd81f5b9b Mon Sep 17 00:00:00 2001 From: MerlinEgalite Date: Fri, 18 Aug 2023 23:33:44 +0200 Subject: [PATCH 5/5] refactor: remove constants + outdated comment --- src/libraries/MathLib.sol | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/libraries/MathLib.sol b/src/libraries/MathLib.sol index b6d645a4d..a22efa3a4 100644 --- a/src/libraries/MathLib.sol +++ b/src/libraries/MathLib.sol @@ -7,11 +7,7 @@ uint256 constant WAD = 1e18; /// @author Morpho Labs /// @custom:contact security@morpho.xyz /// @notice Library to manage fixed-point arithmetic. -/// @dev Inspired by https://github.com/morpho-org/morpho-utils. library MathLib { - uint256 private constant TWO_WAD = 2 * WAD; - uint256 private constant THREE_WAD = 3 * WAD; - /// @dev (x * y) / WAD rounded down. function wMulDown(uint256 x, uint256 y) internal pure returns (uint256) { return mulDivDown(x, y, WAD); @@ -46,8 +42,8 @@ library MathLib { /// to approximate a continuous compound interest rate: e^(nx) - 1. function wTaylorCompounded(uint256 x, uint256 n) internal pure returns (uint256) { uint256 firstTerm = x * n; - uint256 secondTerm = mulDivDown(firstTerm, firstTerm, TWO_WAD); - uint256 thirdTerm = mulDivDown(secondTerm, firstTerm, THREE_WAD); + uint256 secondTerm = mulDivDown(firstTerm, firstTerm, 2 * WAD); + uint256 thirdTerm = mulDivDown(secondTerm, firstTerm, 3 * WAD); return firstTerm + secondTerm + thirdTerm; }