zkTrie maximum depth limit is not enforced in Scroll
The Scroll verifier's walkTree function did not enforce the zkTrie maximum depth limit of 248 bits, allowing proofs deeper than 248 levels to be accepted. This could let an attacker submit invalid proofs that should be rejected according to the protocol specification.
Submitted by Evo
Impact
The walkTree method in ScrollVerifierHooks.sol lacks implementation of maximum depth limit of 248 bits that is specified in Scroll’s zkTrie documentation. This causes the Scroll verifier to work differently than intended, potentially accepting proofs that should be invalid according to the protocol specification.
Proof of Concept
From Scroll’s official documentation, zkTrie is explicitly specified to have these constraints:
“We limit the maximum depth of zkTrie to 248, meaning that the tree will only traverse the lower 248 bits of the key. Because the secure key space is a finite field used by Poseidon hash that doesn’t occupy the full range of 2^256, the bit representation of the key can be ambiguous in the finite field and thus results in a soundness issue in the zk circuit.”
Looking at scroll contracts implementation of walkTree also the check is there.
However, reviewing the walkTree implementation in ScrollVerifierHooks.sol:
function walkTree(
bytes32 key,
bytes memory encodedProof,
bytes32 rootHash,
uint256 leafSize
) internal view returns (bytes32 keyHash, bytes32 h, bytes memory v, bool exists) {
bytes[] memory proof = abi.decode(encodedProof, (bytes[]));
keyHash = poseidonHash1(key);
h = rootHash;
for (uint256 i; ; i++) {
// Missing depth limit check: if (i >= 248) revert InvalidProof();
if (i == proof.length) revert InvalidProof();
v = proof[i];
if (v.length == 0) revert InvalidProof();
uint256 nodeType = uint8(v[0]);
if (nodeType == NODE_LEAF) {
bytes32 temp;
assembly {
temp := mload(add(v, 33))
}
if (temp == keyHash) {
assembly {
temp := mload(add(v, leafSize))
}
if (temp != key) revert InvalidProof();
exists = true;
} else {
bytes32 p = bytes32((1 << i) - 1); // prefix mask
if ((temp & p) != (keyHash & p)) revert InvalidProof();
keyHash = temp;
}
break;
}
// ... rest of the code
}
}
The implementation clearly not implementing any maximum depth limit check in the main loop allowing proofs deeper than 248 levels.
Recommended Mitigation Steps
Add depth limit check as specified in documentation:
// In main loop
if (i >= 248) revert InvalidProof();
Or, apply the same check in scroll contract implementation.
I agree with this. I was not aware of this limit.
thomasclowes (Unruggable) confirmed
Unruggable mitigated:
Added Scroll Max Trie Depth Guard here.
Status: Mitigation confirmed. Issue is resolved with the added MAX_TRIE_DEPTH (248) check that ensures provided proof does not exceed the max depth of 248.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The walkTree loop in ScrollVerifierHooks.sol did not include a check to revert if the proof depth exceeded 248, unlike Scroll's reference implementation.
Impact
The verifier could accept proofs that should be invalid according to the protocol specification, potentially undermining soundness.
What the attacker needed
Ability to submit a proof to the verifier
The fix
The issue was fixed by adding a MAX_TRIE_DEPTH (248) check to ensure proofs do not exceed the maximum depth.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A zkTrie is a data structure used in zero-knowledge proofs, and it has a rule that keys can only be 248 bits deep. The code that checks proofs in Scroll forgot to enforce this limit, so someone could provide a proof that goes deeper than allowed and it might be accepted even though it should not be.
What is Crypto?
Encryption, signatures, randomness or certificate checks that do not protect what they claim to.