diff --git a/.gas-snapshot b/.gas-snapshot index 619d23eb..e473fbad 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -101,7 +101,7 @@ ERC20Test:testApprove() (gas: 31080) ERC20Test:testBurn() (gas: 57014) ERC20Test:testFailPermitBadDeadline() (gas: 36924) ERC20Test:testFailPermitBadNonce() (gas: 36874) -ERC20Test:testFailPermitPastDeadline() (gas: 10938) +ERC20Test:testFailPermitPastDeadline() (gas: 8589) ERC20Test:testFailPermitReplay() (gas: 66285) ERC20Test:testFailTransferFromInsufficientAllowance() (gas: 80837) ERC20Test:testFailTransferFromInsufficientBalance() (gas: 81336) @@ -188,10 +188,10 @@ FixedPointMathLibTest:testMulWadUp() (gas: 1003) FixedPointMathLibTest:testMulWadUpEdgeCases() (gas: 981) FixedPointMathLibTest:testRPow() (gas: 2142) FixedPointMathLibTest:testSqrt() (gas: 2156) -MerkleProofTest:testValidProofSupplied() (gas: 991) -MerkleProofTest:testVerifyEmptyMerkleProofSuppliedLeafAndRootDifferent() (gas: 392) -MerkleProofTest:testVerifyEmptyMerkleProofSuppliedLeafAndRootSame() (gas: 389) -MerkleProofTest:testVerifyInvalidProofSupplied() (gas: 977) +MerkleProofTest:testValidProofSupplied() (gas: 945) +MerkleProofTest:testVerifyEmptyMerkleProofSuppliedLeafAndRootDifferent() (gas: 390) +MerkleProofTest:testVerifyEmptyMerkleProofSuppliedLeafAndRootSame() (gas: 387) +MerkleProofTest:testVerifyInvalidProofSupplied() (gas: 931) MultiRolesAuthorityTest:testCanCallPublicCapability() (gas: 34293) MultiRolesAuthorityTest:testCanCallWithAuthorizedRole() (gas: 80575) MultiRolesAuthorityTest:testCanCallWithCustomAuthority() (gas: 422681) diff --git a/src/utils/MerkleProof.sol b/src/utils/MerkleProof.sol index 86eeebe5..a5df76ed 100644 --- a/src/utils/MerkleProof.sol +++ b/src/utils/MerkleProof.sol @@ -18,26 +18,23 @@ library MerkleProof { // Iterate over proof elements to compute root hash. for { - let end := add(data, mul(mload(proof), 0x20)) + // Left shift by 5 is equivalent to multiplying by 0x20. + let end := add(data, shl(5, mload(proof))) } lt(data, end) { data := add(data, 0x20) } { let loadedData := mload(data) - switch gt(computedHash, loadedData) - case 0 { - // Store elements to hash contiguously in scratch space. - // Scratch space is 64 bytes (0x00 - 0x3f) and both elements are 32 bytes. - mstore(0x00, computedHash) - mstore(0x20, loadedData) - computedHash := keccak256(0x00, 0x40) - } - default { - mstore(0x00, loadedData) - mstore(0x20, computedHash) - computedHash := keccak256(0x00, 0x40) - } + // Slot of `computedHash` in scratch space. + // If the condition is true: 0x20, otherwise: 0x00. + let scratch := shl(5, gt(computedHash, loadedData)) + + // Store elements to hash contiguously in scratch space. + // Scratch space is 64 bytes (0x00 - 0x3f) and both elements are 32 bytes. + mstore(scratch, computedHash) + mstore(xor(scratch, 0x20), loadedData) + computedHash := keccak256(0x00, 0x40) } isValid := eq(computedHash, root) } } -} +} \ No newline at end of file