Gas optimized verification of proof of inclusion for a leaf in a Merkle tree.
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf)
internal
pure
returns (bool isValid)Returns whether leaf exists in the Merkle tree with root, given proof.
function verifyCalldata(
bytes32[] calldata proof,
bytes32 root,
bytes32 leaf
) internal pure returns (bool isValid)Returns whether leaf exists in the Merkle tree with root, given proof.
function verifyMultiProof(
bytes32[] memory proof,
bytes32 root,
bytes32[] memory leaves,
bool[] memory flags
) internal pure returns (bool isValid)Returns whether all leaves exist in the Merkle tree with root,
given proof and flags.
Note:
- Breaking the invariant
flags.length == (leaves.length - 1) + proof.length
will always return false. - The sum of the lengths of
proofandleavesmust never overflow. - Any non-zero word in the
flagsarray is treated as true. - The memory offset of
proofmust be non-zero
(i.e.proofis not pointing to the scratch space).
function verifyMultiProofCalldata(
bytes32[] calldata proof,
bytes32 root,
bytes32[] calldata leaves,
bool[] calldata flags
) internal pure returns (bool isValid)Returns whether all leaves exist in the Merkle tree with root,
given proof and flags.
Note:
- Breaking the invariant
flags.length == (leaves.length - 1) + proof.length
will always return false. - Any non-zero word in the
flagsarray is treated as true. - The calldata offset of
proofmust be non-zero
(i.e.proofis from a regular Solidity function with a 4-byte selector).
function emptyProof() internal pure returns (bytes32[] calldata proof)Returns an empty calldata bytes32 array.
function emptyLeaves() internal pure returns (bytes32[] calldata leaves)Returns an empty calldata bytes32 array.
function emptyFlags() internal pure returns (bool[] calldata flags)Returns an empty calldata bool array.