Remove max_depth in native MerkleTree (#442)

This simplifies the MerkleTree (and container) API.
Defer the max depth check when assigning the witness (merkle proof siblings) to the merkle tree circuit.

In this implementation the native Merkle Tree branches grow as much as they needed.  There are no checks of max depth in the merkle tree.  All keys are 256 bits (I added a debug_assert for this); so in the worst case a path will have depth 256.  It can't have a longer depth because the `insert` method calls `prove_nonexistence` which errors if the key already exists; another one may exist which must be different and thus require a path <= 256 depth. 

Resolve #436
This commit is contained in:
Eduard S. 2025-12-16 13:18:49 +01:00 committed by GitHub
parent 32dc85471d
commit 813a86c670
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 266 additions and 462 deletions

View file

@ -446,24 +446,13 @@ impl Operation {
let root = val(root_v, root_s)?;
let key = val(key_v, key_s)?;
let value = val(val_v, val_s)?;
MerkleTree::verify(
params.max_depth_mt_containers,
root.raw().into(),
pf,
&key.raw(),
&value.raw(),
)?;
MerkleTree::verify(root.raw().into(), pf, &key.raw(), &value.raw())?;
true
}
(Self::NotContainsFromEntries(root_s, key_s, pf), NotContains(root_v, key_v)) => {
let root = val(root_v, root_s)?;
let key = val(key_v, key_s)?;
MerkleTree::verify_nonexistence(
params.max_depth_mt_containers,
root.raw().into(),
pf,
&key.raw(),
)?;
MerkleTree::verify_nonexistence(root.raw().into(), pf, &key.raw())?;
true
}
(
@ -507,7 +496,7 @@ impl Operation {
"The provided Merkle tree state transition proof does not match the claim."
.into(),
))?;
MerkleTree::verify_state_transition(params.max_depth_mt_containers, pf)?;
MerkleTree::verify_state_transition(pf)?;
true
}
(
@ -528,7 +517,7 @@ impl Operation {
"The provided Merkle tree state transition proof does not match the claim."
.into(),
))?;
MerkleTree::verify_state_transition(params.max_depth_mt_containers, pf)?;
MerkleTree::verify_state_transition(pf)?;
true
}
(
@ -547,7 +536,7 @@ impl Operation {
"The provided Merkle tree state transition proof does not match the claim."
.into(),
))?;
MerkleTree::verify_state_transition(params.max_depth_mt_containers, pf)?;
MerkleTree::verify_state_transition(pf)?;
true
}
(Self::Custom(CustomPredicateRef { batch, index }, args), Custom(cpr, s_args))
@ -815,7 +804,7 @@ mod tests {
let kvs = (0..10)
.map(|i| (hash_value(&i.into()).into(), i.into()))
.collect::<HashMap<_, _>>();
let mt = MerkleTree::new(params.max_depth_mt_containers, &kvs)?;
let mt = MerkleTree::new(&kvs);
let root = mt.root();
// Check existence proofs
@ -873,7 +862,7 @@ mod tests {
let kvs = (0..10)
.map(|i| (hash_value(&i.into()).into(), i.into()))
.collect::<HashMap<_, _>>();
let mut mt = MerkleTree::new(params.max_depth_mt_containers, &kvs)?;
let mut mt = MerkleTree::new(&kvs);
// Check insertion proofs
(11..20)