MerkleProof.sol 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (utils/cryptography/MerkleProof.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev These functions deal with verification of Merkle Trees proofs.
  6. *
  7. * The proofs can be generated using the JavaScript library
  8. * https://github.com/miguelmota/merkletreejs[merkletreejs].
  9. * Note: the hashing algorithm should be keccak256 and pair sorting should be enabled.
  10. *
  11. * See `test/utils/cryptography/MerkleProof.test.js` for some examples.
  12. *
  13. * WARNING: You should avoid using leaf values that are 64 bytes long prior to
  14. * hashing, or use a hash function other than keccak256 for hashing leaves.
  15. * This is because the concatenation of a sorted pair of internal nodes in
  16. * the merkle tree could be reinterpreted as a leaf value.
  17. */
  18. library MerkleProof {
  19. /**
  20. * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
  21. * defined by `root`. For this, a `proof` must be provided, containing
  22. * sibling hashes on the branch from the leaf to the root of the tree. Each
  23. * pair of leaves and each pair of pre-images are assumed to be sorted.
  24. */
  25. function verify(
  26. bytes32[] memory proof,
  27. bytes32 root,
  28. bytes32 leaf
  29. ) internal pure returns (bool) {
  30. return processProof(proof, leaf) == root;
  31. }
  32. /**
  33. * @dev Calldata version of {verify}
  34. *
  35. * _Available since v4.7._
  36. */
  37. function verifyCalldata(
  38. bytes32[] calldata proof,
  39. bytes32 root,
  40. bytes32 leaf
  41. ) internal pure returns (bool) {
  42. return processProofCalldata(proof, leaf) == root;
  43. }
  44. /**
  45. * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
  46. * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
  47. * hash matches the root of the tree. When processing the proof, the pairs
  48. * of leafs & pre-images are assumed to be sorted.
  49. *
  50. * _Available since v4.4._
  51. */
  52. function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
  53. bytes32 computedHash = leaf;
  54. for (uint256 i = 0; i < proof.length; i++) {
  55. computedHash = _hashPair(computedHash, proof[i]);
  56. }
  57. return computedHash;
  58. }
  59. /**
  60. * @dev Calldata version of {processProof}
  61. *
  62. * _Available since v4.7._
  63. */
  64. function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
  65. bytes32 computedHash = leaf;
  66. for (uint256 i = 0; i < proof.length; i++) {
  67. computedHash = _hashPair(computedHash, proof[i]);
  68. }
  69. return computedHash;
  70. }
  71. /**
  72. * @dev Returns true if a `leafs` can be proved to be a part of a Merkle tree
  73. * defined by `root`. For this, `proofs` for each leaf must be provided, containing
  74. * sibling hashes on the branch from the leaf to the root of the tree. Then
  75. * 'proofFlag' designates the nodes needed for the multi proof.
  76. *
  77. * _Available since v4.7._
  78. */
  79. function multiProofVerify(
  80. bytes32 root,
  81. bytes32[] calldata leaves,
  82. bytes32[] calldata proofs,
  83. bool[] calldata proofFlag
  84. ) internal pure returns (bool) {
  85. return processMultiProof(leaves, proofs, proofFlag) == root;
  86. }
  87. /**
  88. * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
  89. * from `leaf` using the multi proof as `proofFlag`. A multi proof is
  90. * valid if the final hash matches the root of the tree.
  91. *
  92. * _Available since v4.7._
  93. */
  94. function processMultiProof(
  95. bytes32[] calldata leaves,
  96. bytes32[] calldata proofs,
  97. bool[] calldata proofFlag
  98. ) internal pure returns (bytes32 merkleRoot) {
  99. // This function rebuild the root hash by traversing the tree up from the leaves. The root is rebuilt by
  100. // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
  101. // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
  102. // the merkle tree.
  103. uint256 leavesLen = leaves.length;
  104. uint256 totalHashes = proofFlag.length;
  105. // Check proof validity.
  106. require(leavesLen + proofs.length - 1 == totalHashes, "MerkleProof: invalid multiproof");
  107. // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
  108. // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
  109. bytes32[] memory hashes = new bytes32[](totalHashes);
  110. uint256 leafPos = 0;
  111. uint256 hashPos = 0;
  112. uint256 proofPos = 0;
  113. // At each step, we compute the next hash using two values:
  114. // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
  115. // get the next hash.
  116. // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
  117. // `proofs` array.
  118. for (uint256 i = 0; i < totalHashes; i++) {
  119. bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
  120. bytes32 b = proofFlag[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proofs[proofPos++];
  121. hashes[i] = _hashPair(a, b);
  122. }
  123. if (totalHashes > 0) {
  124. return hashes[totalHashes - 1];
  125. } else if (leavesLen > 0) {
  126. return leaves[0];
  127. } else {
  128. return proofs[0];
  129. }
  130. }
  131. function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
  132. return a < b ? _efficientHash(a, b) : _efficientHash(b, a);
  133. }
  134. function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
  135. /// @solidity memory-safe-assembly
  136. assembly {
  137. mstore(0x00, a)
  138. mstore(0x20, b)
  139. value := keccak256(0x00, 0x40)
  140. }
  141. }
  142. }