diff --git a/CHANGELOG.md b/CHANGELOG.md index 5549260a5..58f21d999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ * `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 `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 diff --git a/contracts/utils/cryptography/MerkleProof.sol b/contracts/utils/cryptography/MerkleProof.sol index 0ce87faf2..3862fdbfe 100644 --- a/contracts/utils/cryptography/MerkleProof.sol +++ b/contracts/utils/cryptography/MerkleProof.sol @@ -135,7 +135,7 @@ library MerkleProof { // 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 // 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. for (uint256 i = 0; i < totalHashes; i++) { bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++]; @@ -146,7 +146,9 @@ library MerkleProof { } if (totalHashes > 0) { - return hashes[totalHashes - 1]; + unchecked { + return hashes[totalHashes - 1]; + } } else if (leavesLen > 0) { return leaves[0]; } else { @@ -185,7 +187,7 @@ library MerkleProof { // 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 // 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. for (uint256 i = 0; i < totalHashes; i++) { bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++]; @@ -196,7 +198,9 @@ library MerkleProof { } if (totalHashes > 0) { - return hashes[totalHashes - 1]; + unchecked { + return hashes[totalHashes - 1]; + } } else if (leavesLen > 0) { return leaves[0]; } else {