From 65033cc40d55f4e1bc301ce5902586aa2277b431 Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Mon, 31 Jul 2023 19:55:50 +0300 Subject: [PATCH 1/4] Reduce stack operations in mulDiv --- contracts/utils/math/Math.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index e3d7f799d41..67e20b97a78 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -124,11 +124,10 @@ library Math { // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256 // variables such that product = prod1 * 2^256 + prod0. - uint256 prod0; // Least significant 256 bits of the product + uint256 prod0 = x * y; // Least significant 256 bits of the product uint256 prod1; // Most significant 256 bits of the product assembly { let mm := mulmod(x, y, not(0)) - prod0 := mul(x, y) prod1 := sub(sub(mm, prod0), lt(mm, prod0)) } From f5e8beb3dbc08b8e931954db5fb91a61f5245b7b Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Mon, 31 Jul 2023 20:00:27 +0300 Subject: [PATCH 2/4] Simplify twos calculation in mulDiv --- contracts/utils/math/Math.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/contracts/utils/math/Math.sol b/contracts/utils/math/Math.sol index 67e20b97a78..e47a5aae546 100644 --- a/contracts/utils/math/Math.sol +++ b/contracts/utils/math/Math.sol @@ -162,8 +162,7 @@ library Math { // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1. // See https://cs.stackexchange.com/q/138556/92363. - // Does not overflow because the denominator cannot be zero at this stage in the function. - uint256 twos = denominator & (~denominator + 1); + uint256 twos = denominator & (0 - denominator); assembly { // Divide denominator by twos. denominator := div(denominator, twos) From e54956d8453f04f59491511df8d353dbaab98202 Mon Sep 17 00:00:00 2001 From: Vladislav Volosnikov Date: Mon, 31 Jul 2023 20:04:26 +0300 Subject: [PATCH 3/4] Add changeset --- .changeset/pink-suns-mix.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pink-suns-mix.md diff --git a/.changeset/pink-suns-mix.md b/.changeset/pink-suns-mix.md new file mode 100644 index 00000000000..07eadc485eb --- /dev/null +++ b/.changeset/pink-suns-mix.md @@ -0,0 +1,5 @@ +--- +'openzeppelin-solidity': patch +--- + +optimization of stack operations in mulDiv (Math.sol) From 9a86e03a134257726a191e5115ebe11d03f57b1e Mon Sep 17 00:00:00 2001 From: Francisco Date: Mon, 31 Jul 2023 17:10:53 -0300 Subject: [PATCH 4/4] Update pink-suns-mix.md --- .changeset/pink-suns-mix.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/pink-suns-mix.md b/.changeset/pink-suns-mix.md index 07eadc485eb..eb7aaac464b 100644 --- a/.changeset/pink-suns-mix.md +++ b/.changeset/pink-suns-mix.md @@ -2,4 +2,4 @@ 'openzeppelin-solidity': patch --- -optimization of stack operations in mulDiv (Math.sol) +`Math`: Optimized stack operations in `mulDiv`.