-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(contracts): verify xsubmission merke proofs
Verify XSubmission merkle proofs on xsubmit.
- Loading branch information
1 parent
fbb0fcd
commit 6a17a46
Showing
10 changed files
with
179 additions
and
201 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
pragma solidity 0.8.23; | ||
|
||
import { MerkleProof } from "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol"; | ||
import { XTypes } from "./XTypes.sol"; | ||
|
||
/** | ||
* @title XBlockMerkleProof | ||
* @dev Library for verifying XBlock merkle proofs | ||
*/ | ||
library XBlockMerkleProof { | ||
/** | ||
* @dev Verifies a multi merkle proof for the provided block header and messages, against the provided root. | ||
* Msgs order must match the order used to contstruct the merkle proof. | ||
* @param root The root of the xblock merkle tree, generally XSubmission.attestationRoot. | ||
* @param blockHeader The xblock header. | ||
* @param msgs The xmsgs to verify. | ||
* @param proof The merkle proof. | ||
* @param proofFlags The merkle proof flags. | ||
* @return True if the proof is valid. | ||
*/ | ||
function verify( | ||
bytes32 root, | ||
XTypes.BlockHeader calldata blockHeader, | ||
XTypes.Msg[] calldata msgs, | ||
bytes32[] calldata proof, | ||
bool[] calldata proofFlags | ||
) internal pure returns (bool) { | ||
return MerkleProof.multiProofVerify(proof, proofFlags, root, _leaves(blockHeader, msgs)); | ||
} | ||
|
||
/// @dev Convert block header and msgs to leaf hashes | ||
function _leaves(XTypes.BlockHeader calldata blockHeader, XTypes.Msg[] calldata msgs) | ||
private | ||
pure | ||
returns (bytes32[] memory) | ||
{ | ||
bytes32[] memory leaves = new bytes32[](msgs.length + 1); | ||
|
||
leaves[0] = _leafHash(abi.encode((blockHeader))); | ||
for (uint256 i = 0; i < msgs.length; i++) { | ||
leaves[i + 1] = _leafHash(abi.encode((msgs[i]))); | ||
} | ||
|
||
return leaves; | ||
} | ||
|
||
/// @dev Double hash leaves, as recommended by OpenZeppelin, to prevent second preimage attacks | ||
/// Leaves must be double hashed in tree / proof construction | ||
function _leafHash(bytes memory leaf) private pure returns (bytes32) { | ||
return keccak256(bytes.concat(keccak256(leaf))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.