Skip to content

Commit

Permalink
Process and verify merkle proofs (and multiproof) with custom hash fu…
Browse files Browse the repository at this point in the history
…nction (#4887)

Co-authored-by: ernestognw <ernestognw@gmail.com>
  • Loading branch information
Amxx and ernestognw authored Jul 15, 2024
1 parent 4b33d32 commit b73bcb2
Show file tree
Hide file tree
Showing 10 changed files with 772 additions and 262 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-queens-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`MerkleProof`: Add variations of `verify`, `processProof`, `multiProofVerify` and `processMultiProof` (and equivalent calldata version) with support for custom hashing functions.
62 changes: 62 additions & 0 deletions contracts/mocks/MerkleProofCustomHashMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.20;

import {MerkleProof} from "../utils/cryptography/MerkleProof.sol";

// This could be a library, but then we would have to add it to the Stateless.sol mock for upgradeable tests
abstract contract MerkleProofCustomHashMock {
function customHash(bytes32 a, bytes32 b) internal pure returns (bytes32) {
return a < b ? sha256(abi.encode(a, b)) : sha256(abi.encode(b, a));
}

function verify(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal view returns (bool) {
return MerkleProof.verify(proof, root, leaf, customHash);
}

function processProof(bytes32[] calldata proof, bytes32 leaf) internal view returns (bytes32) {
return MerkleProof.processProof(proof, leaf, customHash);
}

function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal view returns (bool) {
return MerkleProof.verifyCalldata(proof, root, leaf, customHash);
}

function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal view returns (bytes32) {
return MerkleProof.processProofCalldata(proof, leaf, customHash);
}

function multiProofVerify(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] calldata leaves
) internal view returns (bool) {
return MerkleProof.multiProofVerify(proof, proofFlags, root, leaves, customHash);
}

function processMultiProof(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] calldata leaves
) internal view returns (bytes32) {
return MerkleProof.processMultiProof(proof, proofFlags, leaves, customHash);
}

function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] calldata leaves
) internal view returns (bool) {
return MerkleProof.multiProofVerifyCalldata(proof, proofFlags, root, leaves, customHash);
}

function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] calldata leaves
) internal view returns (bytes32) {
return MerkleProof.processMultiProofCalldata(proof, proofFlags, leaves, customHash);
}
}
Loading

0 comments on commit b73bcb2

Please sign in to comment.