|
@@ -4,7 +4,7 @@
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
/**
|
|
|
- * @dev These functions deal with verification of Merkle Trees proofs.
|
|
|
+ * @dev These functions deal with verification of Merkle Tree proofs.
|
|
|
*
|
|
|
* The proofs can be generated using the JavaScript library
|
|
|
* https://github.com/miguelmota/merkletreejs[merkletreejs].
|
|
@@ -75,43 +75,41 @@ library MerkleProof {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @dev Returns true if a `leafs` can be proved to be a part of a Merkle tree
|
|
|
- * defined by `root`. For this, `proofs` for each leaf must be provided, containing
|
|
|
- * sibling hashes on the branch from the leaf to the root of the tree. Then
|
|
|
- * 'proofFlag' designates the nodes needed for the multi proof.
|
|
|
+ * @dev Returns true if the `leaves` can be proved to be a part of a Merkle tree defined by
|
|
|
+ * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
|
|
|
*
|
|
|
* _Available since v4.7._
|
|
|
*/
|
|
|
function multiProofVerify(
|
|
|
+ bytes32[] calldata proof,
|
|
|
+ bool[] calldata proofFlags,
|
|
|
bytes32 root,
|
|
|
- bytes32[] calldata leaves,
|
|
|
- bytes32[] calldata proofs,
|
|
|
- bool[] calldata proofFlag
|
|
|
+ bytes32[] calldata leaves
|
|
|
) internal pure returns (bool) {
|
|
|
- return processMultiProof(leaves, proofs, proofFlag) == root;
|
|
|
+ return processMultiProof(proof, proofFlags, leaves) == root;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
|
|
|
- * from `leaf` using the multi proof as `proofFlag`. A multi proof is
|
|
|
- * valid if the final hash matches the root of the tree.
|
|
|
+ * @dev Returns the root of a tree reconstructed from `leaves` and the sibling nodes in `proof`,
|
|
|
+ * consuming from one or the other at each step according to the instructions given by
|
|
|
+ * `proofFlags`.
|
|
|
*
|
|
|
* _Available since v4.7._
|
|
|
*/
|
|
|
function processMultiProof(
|
|
|
- bytes32[] calldata leaves,
|
|
|
- bytes32[] calldata proofs,
|
|
|
- bool[] calldata proofFlag
|
|
|
+ bytes32[] calldata proof,
|
|
|
+ bool[] calldata proofFlags,
|
|
|
+ bytes32[] calldata leaves
|
|
|
) internal pure returns (bytes32 merkleRoot) {
|
|
|
// This function rebuild 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 totalHashes = proofFlag.length;
|
|
|
+ uint256 totalHashes = proofFlags.length;
|
|
|
|
|
|
// Check proof validity.
|
|
|
- require(leavesLen + proofs.length - 1 == totalHashes, "MerkleProof: invalid multiproof");
|
|
|
+ require(leavesLen + proof.length - 1 == totalHashes, "MerkleProof: invalid multiproof");
|
|
|
|
|
|
// 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".
|
|
@@ -123,10 +121,10 @@ library MerkleProof {
|
|
|
// - 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
|
|
|
- // `proofs` array.
|
|
|
+ // `proof` array.
|
|
|
for (uint256 i = 0; i < totalHashes; i++) {
|
|
|
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
|
|
|
- bytes32 b = proofFlag[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proofs[proofPos++];
|
|
|
+ bytes32 b = proofFlags[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proof[proofPos++];
|
|
|
hashes[i] = _hashPair(a, b);
|
|
|
}
|
|
|
|
|
@@ -135,7 +133,7 @@ library MerkleProof {
|
|
|
} else if (leavesLen > 0) {
|
|
|
return leaves[0];
|
|
|
} else {
|
|
|
- return proofs[0];
|
|
|
+ return proof[0];
|
|
|
}
|
|
|
}
|
|
|
|