MerkleProof.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 Returns the rebuilt hash obtained by traversing a Merkle tree up
  34. * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
  35. * hash matches the root of the tree. When processing the proof, the pairs
  36. * of leafs & pre-images are assumed to be sorted.
  37. *
  38. * _Available since v4.4._
  39. */
  40. function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
  41. bytes32 computedHash = leaf;
  42. for (uint256 i = 0; i < proof.length; i++) {
  43. computedHash = _hashPair(computedHash, proof[i]);
  44. }
  45. return computedHash;
  46. }
  47. /**
  48. * @dev Returns true if a `leafs` can be proved to be a part of a Merkle tree
  49. * defined by `root`. For this, `proofs` for each leaf must be provided, containing
  50. * sibling hashes on the branch from the leaf to the root of the tree. Then
  51. * 'proofFlag' designates the nodes needed for the multi proof.
  52. *
  53. * _Available since v4.7._
  54. */
  55. function multiProofVerify(
  56. bytes32 root,
  57. bytes32[] memory leafs,
  58. bytes32[] memory proofs,
  59. bool[] memory proofFlag
  60. ) internal pure returns (bool) {
  61. return processMultiProof(leafs, proofs, proofFlag) == root;
  62. }
  63. /**
  64. * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
  65. * from `leaf` using the multi proof as `proofFlag`. A multi proof is
  66. * valid if the final hash matches the root of the tree.
  67. *
  68. * _Available since v4.7._
  69. */
  70. function processMultiProof(
  71. bytes32[] memory leafs,
  72. bytes32[] memory proofs,
  73. bool[] memory proofFlag
  74. ) internal pure returns (bytes32 merkleRoot) {
  75. // This function rebuild the root hash by traversing the tree up from the leaves. The root is rebuilt by
  76. // consuming and producing values on a queue. The queue starts with the `leafs` array, then goes onto the
  77. // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
  78. // the merkle tree.
  79. uint256 leafsLen = leafs.length;
  80. uint256 proofsLen = proofs.length;
  81. uint256 totalHashes = proofFlag.length;
  82. // Check proof validity.
  83. require(leafsLen + proofsLen - 1 == totalHashes, "MerkleProof: invalid multiproof");
  84. // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
  85. // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
  86. bytes32[] memory hashes = new bytes32[](totalHashes);
  87. uint256 leafPos = 0;
  88. uint256 hashPos = 0;
  89. uint256 proofPos = 0;
  90. // At each step, we compute the next hash using two values:
  91. // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
  92. // get the next hash.
  93. // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
  94. // `proofs` array.
  95. for (uint256 i = 0; i < totalHashes; i++) {
  96. bytes32 a = leafPos < leafsLen ? leafs[leafPos++] : hashes[hashPos++];
  97. bytes32 b = proofFlag[i] ? leafPos < leafsLen ? leafs[leafPos++] : hashes[hashPos++] : proofs[proofPos++];
  98. hashes[i] = _hashPair(a, b);
  99. }
  100. return hashes[totalHashes - 1];
  101. }
  102. function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
  103. return a < b ? _efficientHash(a, b) : _efficientHash(b, a);
  104. }
  105. function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
  106. /// @solidity memory-safe-assembly
  107. assembly {
  108. mstore(0x00, a)
  109. mstore(0x20, b)
  110. value := keccak256(0x00, 0x40)
  111. }
  112. }
  113. }