123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.1.0) (utils/cryptography/MerkleProof.sol)
- // This file was procedurally generated from scripts/generate/templates/MerkleProof.js.
- pragma solidity ^0.8.20;
- import {Hashes} from "./Hashes.sol";
- /**
- * @dev These functions deal with verification of Merkle Tree proofs.
- *
- * The tree and the proofs can be generated using our
- * https://github.com/OpenZeppelin/merkle-tree[JavaScript library].
- * You will find a quickstart guide in the readme.
- *
- * WARNING: You should avoid using leaf values that are 64 bytes long prior to
- * hashing, or use a hash function other than keccak256 for hashing leaves.
- * This is because the concatenation of a sorted pair of internal nodes in
- * the Merkle tree could be reinterpreted as a leaf value.
- * OpenZeppelin's JavaScript library generates Merkle trees that are safe
- * against this attack out of the box.
- *
- * IMPORTANT: Consider memory side-effects when using custom hashing functions
- * that access memory in an unsafe way.
- *
- * NOTE: This library supports proof verification for merkle trees built using
- * custom _commutative_ hashing functions (i.e. `H(a, b) == H(b, a)`). Proving
- * leaf inclusion in trees built using non-commutative hashing functions requires
- * additional logic that is not supported by this library.
- */
- library MerkleProof {
- /**
- *@dev The multiproof provided is not valid.
- */
- error MerkleProofInvalidMultiproof();
- /**
- * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
- * defined by `root`. For this, a `proof` must be provided, containing
- * sibling hashes on the branch from the leaf to the root of the tree. Each
- * pair of leaves and each pair of pre-images are assumed to be sorted.
- *
- * This version handles proofs in memory with the default hashing function.
- */
- function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
- return processProof(proof, leaf) == root;
- }
- /**
- * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
- * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
- * hash matches the root of the tree. When processing the proof, the pairs
- * of leaves & pre-images are assumed to be sorted.
- *
- * This version handles proofs in memory with the default hashing function.
- */
- function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
- bytes32 computedHash = leaf;
- for (uint256 i = 0; i < proof.length; i++) {
- computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
- }
- return computedHash;
- }
- /**
- * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
- * defined by `root`. For this, a `proof` must be provided, containing
- * sibling hashes on the branch from the leaf to the root of the tree. Each
- * pair of leaves and each pair of pre-images are assumed to be sorted.
- *
- * This version handles proofs in memory with a custom hashing function.
- */
- function verify(
- bytes32[] memory proof,
- bytes32 root,
- bytes32 leaf,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bool) {
- return processProof(proof, leaf, hasher) == root;
- }
- /**
- * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
- * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
- * hash matches the root of the tree. When processing the proof, the pairs
- * of leaves & pre-images are assumed to be sorted.
- *
- * This version handles proofs in memory with a custom hashing function.
- */
- function processProof(
- bytes32[] memory proof,
- bytes32 leaf,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bytes32) {
- bytes32 computedHash = leaf;
- for (uint256 i = 0; i < proof.length; i++) {
- computedHash = hasher(computedHash, proof[i]);
- }
- return computedHash;
- }
- /**
- * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
- * defined by `root`. For this, a `proof` must be provided, containing
- * sibling hashes on the branch from the leaf to the root of the tree. Each
- * pair of leaves and each pair of pre-images are assumed to be sorted.
- *
- * This version handles proofs in calldata with the default hashing function.
- */
- function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
- return processProofCalldata(proof, leaf) == root;
- }
- /**
- * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
- * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
- * hash matches the root of the tree. When processing the proof, the pairs
- * of leaves & pre-images are assumed to be sorted.
- *
- * This version handles proofs in calldata with the default hashing function.
- */
- function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
- bytes32 computedHash = leaf;
- for (uint256 i = 0; i < proof.length; i++) {
- computedHash = Hashes.commutativeKeccak256(computedHash, proof[i]);
- }
- return computedHash;
- }
- /**
- * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
- * defined by `root`. For this, a `proof` must be provided, containing
- * sibling hashes on the branch from the leaf to the root of the tree. Each
- * pair of leaves and each pair of pre-images are assumed to be sorted.
- *
- * This version handles proofs in calldata with a custom hashing function.
- */
- function verifyCalldata(
- bytes32[] calldata proof,
- bytes32 root,
- bytes32 leaf,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bool) {
- return processProofCalldata(proof, leaf, hasher) == root;
- }
- /**
- * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
- * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
- * hash matches the root of the tree. When processing the proof, the pairs
- * of leaves & pre-images are assumed to be sorted.
- *
- * This version handles proofs in calldata with a custom hashing function.
- */
- function processProofCalldata(
- bytes32[] calldata proof,
- bytes32 leaf,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bytes32) {
- bytes32 computedHash = leaf;
- for (uint256 i = 0; i < proof.length; i++) {
- computedHash = hasher(computedHash, proof[i]);
- }
- return computedHash;
- }
- /**
- * @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
- * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
- *
- * This version handles multiproofs in memory with the default hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
- *
- * NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
- * The `leaves` must be validated independently. See {processMultiProof}.
- */
- function multiProofVerify(
- bytes32[] memory proof,
- bool[] memory proofFlags,
- bytes32 root,
- bytes32[] memory leaves
- ) internal pure returns (bool) {
- return processMultiProof(proof, proofFlags, leaves) == root;
- }
- /**
- * @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
- * proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
- * leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
- * respectively.
- *
- * This version handles multiproofs in memory with the default hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
- * is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
- * tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
- *
- * NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
- * and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
- * validating the leaves elsewhere.
- */
- function processMultiProof(
- bytes32[] memory proof,
- bool[] memory proofFlags,
- bytes32[] memory leaves
- ) internal pure returns (bytes32 merkleRoot) {
- // This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
- // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
- // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
- // the Merkle tree.
- uint256 leavesLen = leaves.length;
- uint256 proofFlagsLen = proofFlags.length;
- // Check proof validity.
- if (leavesLen + proof.length != proofFlagsLen + 1) {
- revert MerkleProofInvalidMultiproof();
- }
- // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
- // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
- bytes32[] memory hashes = new bytes32[](proofFlagsLen);
- uint256 leafPos = 0;
- uint256 hashPos = 0;
- uint256 proofPos = 0;
- // 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 from the "main queue" (merging branches) or an element from the
- // `proof` array.
- for (uint256 i = 0; i < proofFlagsLen; i++) {
- bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
- bytes32 b = proofFlags[i]
- ? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
- : proof[proofPos++];
- hashes[i] = Hashes.commutativeKeccak256(a, b);
- }
- if (proofFlagsLen > 0) {
- if (proofPos != proof.length) {
- revert MerkleProofInvalidMultiproof();
- }
- unchecked {
- return hashes[proofFlagsLen - 1];
- }
- } else if (leavesLen > 0) {
- return leaves[0];
- } else {
- return proof[0];
- }
- }
- /**
- * @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
- * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
- *
- * This version handles multiproofs in memory with a custom hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
- *
- * NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
- * The `leaves` must be validated independently. See {processMultiProof}.
- */
- function multiProofVerify(
- bytes32[] memory proof,
- bool[] memory proofFlags,
- bytes32 root,
- bytes32[] memory leaves,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bool) {
- return processMultiProof(proof, proofFlags, leaves, hasher) == root;
- }
- /**
- * @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
- * proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
- * leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
- * respectively.
- *
- * This version handles multiproofs in memory with a custom hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
- * is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
- * tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
- *
- * NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
- * and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
- * validating the leaves elsewhere.
- */
- function processMultiProof(
- bytes32[] memory proof,
- bool[] memory proofFlags,
- bytes32[] memory leaves,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bytes32 merkleRoot) {
- // This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
- // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
- // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
- // the Merkle tree.
- uint256 leavesLen = leaves.length;
- uint256 proofFlagsLen = proofFlags.length;
- // Check proof validity.
- if (leavesLen + proof.length != proofFlagsLen + 1) {
- revert MerkleProofInvalidMultiproof();
- }
- // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
- // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
- bytes32[] memory hashes = new bytes32[](proofFlagsLen);
- uint256 leafPos = 0;
- uint256 hashPos = 0;
- uint256 proofPos = 0;
- // 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 from the "main queue" (merging branches) or an element from the
- // `proof` array.
- for (uint256 i = 0; i < proofFlagsLen; i++) {
- bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
- bytes32 b = proofFlags[i]
- ? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
- : proof[proofPos++];
- hashes[i] = hasher(a, b);
- }
- if (proofFlagsLen > 0) {
- if (proofPos != proof.length) {
- revert MerkleProofInvalidMultiproof();
- }
- unchecked {
- return hashes[proofFlagsLen - 1];
- }
- } else if (leavesLen > 0) {
- return leaves[0];
- } else {
- return proof[0];
- }
- }
- /**
- * @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
- * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
- *
- * This version handles multiproofs in calldata with the default hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
- *
- * NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
- * The `leaves` must be validated independently. See {processMultiProofCalldata}.
- */
- function multiProofVerifyCalldata(
- bytes32[] calldata proof,
- bool[] calldata proofFlags,
- bytes32 root,
- bytes32[] memory leaves
- ) internal pure returns (bool) {
- return processMultiProofCalldata(proof, proofFlags, leaves) == root;
- }
- /**
- * @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
- * proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
- * leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
- * respectively.
- *
- * This version handles multiproofs in calldata with the default hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
- * is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
- * tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
- *
- * NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
- * and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
- * validating the leaves elsewhere.
- */
- function processMultiProofCalldata(
- bytes32[] calldata proof,
- bool[] calldata proofFlags,
- bytes32[] memory leaves
- ) internal pure returns (bytes32 merkleRoot) {
- // This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
- // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
- // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
- // the Merkle tree.
- uint256 leavesLen = leaves.length;
- uint256 proofFlagsLen = proofFlags.length;
- // Check proof validity.
- if (leavesLen + proof.length != proofFlagsLen + 1) {
- revert MerkleProofInvalidMultiproof();
- }
- // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
- // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
- bytes32[] memory hashes = new bytes32[](proofFlagsLen);
- uint256 leafPos = 0;
- uint256 hashPos = 0;
- uint256 proofPos = 0;
- // 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 from the "main queue" (merging branches) or an element from the
- // `proof` array.
- for (uint256 i = 0; i < proofFlagsLen; i++) {
- bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
- bytes32 b = proofFlags[i]
- ? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
- : proof[proofPos++];
- hashes[i] = Hashes.commutativeKeccak256(a, b);
- }
- if (proofFlagsLen > 0) {
- if (proofPos != proof.length) {
- revert MerkleProofInvalidMultiproof();
- }
- unchecked {
- return hashes[proofFlagsLen - 1];
- }
- } else if (leavesLen > 0) {
- return leaves[0];
- } else {
- return proof[0];
- }
- }
- /**
- * @dev Returns true if the `leaves` can be simultaneously proven to be a part of a Merkle tree defined by
- * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
- *
- * This version handles multiproofs in calldata with a custom hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. See {processMultiProof} for details.
- *
- * NOTE: Consider the case where `root == proof[0] && leaves.length == 0` as it will return `true`.
- * The `leaves` must be validated independently. See {processMultiProofCalldata}.
- */
- function multiProofVerifyCalldata(
- bytes32[] calldata proof,
- bool[] calldata proofFlags,
- bytes32 root,
- bytes32[] memory leaves,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bool) {
- return processMultiProofCalldata(proof, proofFlags, leaves, hasher) == root;
- }
- /**
- * @dev Returns the root of a tree reconstructed from `leaves` and sibling nodes in `proof`. The reconstruction
- * proceeds by incrementally reconstructing all inner nodes by combining a leaf/inner node with either another
- * leaf/inner node or a proof sibling node, depending on whether each `proofFlags` item is true or false
- * respectively.
- *
- * This version handles multiproofs in calldata with a custom hashing function.
- *
- * CAUTION: Not all Merkle trees admit multiproofs. To use multiproofs, it is sufficient to ensure that: 1) the tree
- * is complete (but not necessarily perfect), 2) the leaves to be proven are in the opposite order they are in the
- * tree (i.e., as seen from right to left starting at the deepest layer and continuing at the next layer).
- *
- * NOTE: The _empty set_ (i.e. the case where `proof.length == 1 && leaves.length == 0`) is considered a no-op,
- * and therefore a valid multiproof (i.e. it returns `proof[0]`). Consider disallowing this case if you're not
- * validating the leaves elsewhere.
- */
- function processMultiProofCalldata(
- bytes32[] calldata proof,
- bool[] calldata proofFlags,
- bytes32[] memory leaves,
- function(bytes32, bytes32) view returns (bytes32) hasher
- ) internal view returns (bytes32 merkleRoot) {
- // This function rebuilds the root hash by traversing the tree up from the leaves. The root is rebuilt by
- // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
- // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
- // the Merkle tree.
- uint256 leavesLen = leaves.length;
- uint256 proofFlagsLen = proofFlags.length;
- // Check proof validity.
- if (leavesLen + proof.length != proofFlagsLen + 1) {
- revert MerkleProofInvalidMultiproof();
- }
- // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
- // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
- bytes32[] memory hashes = new bytes32[](proofFlagsLen);
- uint256 leafPos = 0;
- uint256 hashPos = 0;
- uint256 proofPos = 0;
- // 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 from the "main queue" (merging branches) or an element from the
- // `proof` array.
- for (uint256 i = 0; i < proofFlagsLen; i++) {
- bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
- bytes32 b = proofFlags[i]
- ? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
- : proof[proofPos++];
- hashes[i] = hasher(a, b);
- }
- if (proofFlagsLen > 0) {
- if (proofPos != proof.length) {
- revert MerkleProofInvalidMultiproof();
- }
- unchecked {
- return hashes[proofFlagsLen - 1];
- }
- } else if (leavesLen > 0) {
- return leaves[0];
- } else {
- return proof[0];
- }
- }
- }
|