Skip to content

Commit

Permalink
⚡️ MerkleProof Verify Optimization (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized authored May 18, 2022
1 parent deb69bd commit f878f20
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
10 changes: 5 additions & 5 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
27 changes: 12 additions & 15 deletions src/utils/MerkleProof.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}

0 comments on commit f878f20

Please sign in to comment.