Skip to content

Commit ffd2f80

Browse files
zimphaThegaram
andauthored
new ScrollChainCommitmentVerifier (#95)
Co-authored-by: Péter Garamvölgyi <[email protected]>
1 parent cc6fc1b commit ffd2f80

File tree

1 file changed

+31
-5
lines changed

1 file changed

+31
-5
lines changed

src/L1/rollup/ScrollChainCommitmentVerifier.sol

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
pragma solidity =0.8.24;
44

5-
import {IScrollChain} from "./IScrollChain.sol";
5+
import {ScrollChain} from "./ScrollChain.sol";
66
import {ZkTrieVerifier} from "../../libraries/verifier/ZkTrieVerifier.sol";
7+
import {PatriciaMerkleTrieVerifier} from "../../libraries/verifier/PatriciaMerkleTrieVerifier.sol";
78

89
contract ScrollChainCommitmentVerifier {
910
/// @notice The address of poseidon hash contract
@@ -17,7 +18,7 @@ contract ScrollChainCommitmentVerifier {
1718
rollup = _rollup;
1819
}
1920

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

41+
/// @notice Validates a proof from eth_getProof in l2geth zktrie node.
42+
/// @param account The address of the contract.
43+
/// @param storageKey The storage slot to verify.
44+
/// @param proof The rlp encoding result of eth_getProof.
45+
/// @return stateRoot The computed state root. Must be checked by the caller.
46+
/// @return storageValue The value of `storageKey`.
47+
///
48+
/// The encoding order of `proof` is
49+
/// ```text
50+
/// | 1 byte | ... | 1 byte | ... |
51+
/// | account proof length | account proof | storage proof length | storage proof |
52+
/// ```
53+
function verifyPatriciaMerkleTrieProof(
54+
address account,
55+
bytes32 storageKey,
56+
bytes calldata proof
57+
) public pure returns (bytes32 stateRoot, bytes32 storageValue) {
58+
return PatriciaMerkleTrieVerifier.verifyPatriciaProof(account, storageKey, proof);
59+
}
60+
4061
/// @notice Verifies a batch inclusion proof.
4162
/// @param batchIndex The index of the batch.
4263
/// @param account The address of the contract in L2.
@@ -49,11 +70,16 @@ contract ScrollChainCommitmentVerifier {
4970
bytes32 storageKey,
5071
bytes calldata proof
5172
) external view returns (bytes32 storageValue) {
52-
require(IScrollChain(rollup).isBatchFinalized(batchIndex), "Batch not finalized");
73+
require(ScrollChain(rollup).isBatchFinalized(batchIndex), "Batch not finalized");
5374

5475
bytes32 computedStateRoot;
55-
(computedStateRoot, storageValue) = verifyZkTrieProof(account, storageKey, proof);
56-
bytes32 expectedStateRoot = IScrollChain(rollup).finalizedStateRoots(batchIndex);
76+
uint256 initialEuclidBatchIndex = ScrollChain(rollup).initialEuclidBatchIndex();
77+
if (batchIndex < initialEuclidBatchIndex) {
78+
(computedStateRoot, storageValue) = verifyZkTrieProof(account, storageKey, proof);
79+
} else {
80+
(computedStateRoot, storageValue) = verifyPatriciaMerkleTrieProof(account, storageKey, proof);
81+
}
82+
bytes32 expectedStateRoot = ScrollChain(rollup).finalizedStateRoots(batchIndex);
5783
require(computedStateRoot == expectedStateRoot, "Invalid inclusion proof");
5884
}
5985
}

0 commit comments

Comments
 (0)