Browse Source

Use unchecked in MerkleProof.processMultiProof (#3869)

Signed-off-by: Pascal Marco Caversaccio <pascal.caversaccio@hotmail.ch>
Co-authored-by: Francisco <fg@frang.io>
Pascal Marco Caversaccio 2 years ago
parent
commit
a81b0d0b21
2 changed files with 9 additions and 4 deletions
  1. 1 0
      CHANGELOG.md
  2. 8 4
      contracts/utils/cryptography/MerkleProof.sol

+ 1 - 0
CHANGELOG.md

@@ -9,6 +9,7 @@
  * `Math`: optimize `log256` rounding check. ([#3745](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3745))
  * `Math`: optimize `log256` rounding check. ([#3745](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3745))
  * `Strings`: add `equal` method. ([#3774](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3774))
  * `Strings`: add `equal` method. ([#3774](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3774))
  * `Strings`: add `toString` method for signed integers. ([#3773](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3773))
  * `Strings`: add `toString` method for signed integers. ([#3773](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3773))
+ * `MerkleProof`: optimize by using unchecked arithmetic. ([#3745](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3745))
 
 
 ### Deprecations
 ### Deprecations
 
 

+ 8 - 4
contracts/utils/cryptography/MerkleProof.sol

@@ -135,7 +135,7 @@ library MerkleProof {
         // At each step, we compute the next hash using two values:
         // At each step, we compute the next hash using two values:
         // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
         // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
         //   get the next hash.
         //   get the next hash.
-        // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
+        // - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
         //   `proof` array.
         //   `proof` array.
         for (uint256 i = 0; i < totalHashes; i++) {
         for (uint256 i = 0; i < totalHashes; i++) {
             bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
             bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
@@ -146,7 +146,9 @@ library MerkleProof {
         }
         }
 
 
         if (totalHashes > 0) {
         if (totalHashes > 0) {
-            return hashes[totalHashes - 1];
+            unchecked {
+                return hashes[totalHashes - 1];
+            }
         } else if (leavesLen > 0) {
         } else if (leavesLen > 0) {
             return leaves[0];
             return leaves[0];
         } else {
         } else {
@@ -185,7 +187,7 @@ library MerkleProof {
         // At each step, we compute the next hash using two values:
         // At each step, we compute the next hash using two values:
         // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
         // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
         //   get the next hash.
         //   get the next hash.
-        // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
+        // - depending on the flag, either another value from the "main queue" (merging branches) or an element from the
         //   `proof` array.
         //   `proof` array.
         for (uint256 i = 0; i < totalHashes; i++) {
         for (uint256 i = 0; i < totalHashes; i++) {
             bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
             bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
@@ -196,7 +198,9 @@ library MerkleProof {
         }
         }
 
 
         if (totalHashes > 0) {
         if (totalHashes > 0) {
-            return hashes[totalHashes - 1];
+            unchecked {
+                return hashes[totalHashes - 1];
+            }
         } else if (leavesLen > 0) {
         } else if (leavesLen > 0) {
             return leaves[0];
             return leaves[0];
         } else {
         } else {