Skip to content

new ScrollChainCommitmentVerifier #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/L1/rollup/ScrollChainCommitmentVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

pragma solidity =0.8.24;

import {IScrollChain} from "./IScrollChain.sol";
import {ScrollChain} from "./ScrollChain.sol";
import {ZkTrieVerifier} from "../../libraries/verifier/ZkTrieVerifier.sol";
import {PatriciaMerkleTrieVerifier} from "../../libraries/verifier/PatriciaMerkleTrieVerifier.sol";

contract ScrollChainCommitmentVerifier {
/// @notice The address of poseidon hash contract
Expand All @@ -17,7 +18,7 @@ contract ScrollChainCommitmentVerifier {
rollup = _rollup;
}

/// @notice Validates a proof from eth_getProof in l2geth.
/// @notice Validates a proof from eth_getProof in l2geth zktrie node.
/// @param account The address of the contract.
/// @param storageKey The storage slot to verify.
/// @param proof The rlp encoding result of eth_getProof.
Expand All @@ -37,6 +38,26 @@ contract ScrollChainCommitmentVerifier {
return ZkTrieVerifier.verifyZkTrieProof(poseidon, account, storageKey, proof);
}

/// @notice Validates a proof from eth_getProof in l2geth zktrie node.
/// @param account The address of the contract.
/// @param storageKey The storage slot to verify.
/// @param proof The rlp encoding result of eth_getProof.
/// @return stateRoot The computed state root. Must be checked by the caller.
/// @return storageValue The value of `storageKey`.
///
/// The encoding order of `proof` is
/// ```text
/// | 1 byte | ... | 1 byte | ... |
/// | account proof length | account proof | storage proof length | storage proof |
/// ```
function verifyPatriciaMerkleTrieProof(
address account,
bytes32 storageKey,
bytes calldata proof
) public pure returns (bytes32 stateRoot, bytes32 storageValue) {
return PatriciaMerkleTrieVerifier.verifyPatriciaProof(account, storageKey, proof);
}

/// @notice Verifies a batch inclusion proof.
/// @param batchIndex The index of the batch.
/// @param account The address of the contract in L2.
Expand All @@ -49,11 +70,16 @@ contract ScrollChainCommitmentVerifier {
bytes32 storageKey,
bytes calldata proof
) external view returns (bytes32 storageValue) {
require(IScrollChain(rollup).isBatchFinalized(batchIndex), "Batch not finalized");
require(ScrollChain(rollup).isBatchFinalized(batchIndex), "Batch not finalized");

bytes32 computedStateRoot;
(computedStateRoot, storageValue) = verifyZkTrieProof(account, storageKey, proof);
bytes32 expectedStateRoot = IScrollChain(rollup).finalizedStateRoots(batchIndex);
uint256 initialEuclidBatchIndex = ScrollChain(rollup).initialEuclidBatchIndex();
if (batchIndex < initialEuclidBatchIndex) {
(computedStateRoot, storageValue) = verifyZkTrieProof(account, storageKey, proof);
} else {
(computedStateRoot, storageValue) = verifyPatriciaMerkleTrieProof(account, storageKey, proof);
}
bytes32 expectedStateRoot = ScrollChain(rollup).finalizedStateRoots(batchIndex);
require(computedStateRoot == expectedStateRoot, "Invalid inclusion proof");
}
}
Loading