What is a Lean IMT?
A Lean Incremental Merkle Tree (Lean IMT) is a Merkle tree variant optimized for append-only operations. Unlike traditional Merkle trees that require pre-computing a complete tree structure, Lean IMTs grow incrementally as new leaves are added.RotorTree extends the original binary Lean IMT design from PSE’s lean-imt paper to support n-ary trees (2-ary, 3-ary, 4-ary, 8-ary, 16-ary, etc.).
Why N-ary?
Increasing the branching factor from 2 (binary) to N offers several advantages:Reduced Depth
For the same number of leaves, an n-ary tree has lower depth:| Leaves | Binary (N=2) | Quaternary (N=4) | Octal (N=8) |
|---|---|---|---|
| 16 | 4 levels | 2 levels | 2 levels |
| 256 | 8 levels | 4 levels | 3 levels |
| 4,096 | 12 levels | 6 levels | 4 levels |
| 65,536 | 16 levels | 8 levels | 6 levels |
Efficient Disk Storage
Grouping N children together aligns well with disk block sizes:- N=4: 4 × 32 bytes = 128 bytes per node group
- N=8: 8 × 32 bytes = 256 bytes per node group
- Combined with
CHUNK_SIZE=128, each chunk is exactly 4 KB
Trade-offs
- Proof Size: N-ary proofs include up to N-1 siblings per level (vs. 1 for binary)
- Hashing: Each parent hash requires N inputs (vs. 2 for binary)
- Depth vs. Width: Lower depth but wider nodes
The Lean Algorithm
The “lean” aspect refers to how the tree handles incomplete levels without padding.Node Lifting
When a level has an incomplete group (fewer than N children), the remaining child is lifted directly to the parent level:Incremental Updates
When inserting a new leaf:- Place leaf at index: Append to level 0
- Compute parent: Hash the group containing the new leaf
- Update parent level: Store parent at level 1
- Repeat: Continue up the tree until reaching the root
Implementation Details
Type Signature
Compile-Time Constraints
Fromsrc/tree.rs:707-708:
Core Data Structures
levels[0]: Leaf levellevels[1..depth]: Internal node levelsroot: Current Merkle root (cached)size: Total number of leaves inserteddepth:⌈log_N(size)⌉- computed usingceil_log_n()insrc/tree.rs:141
Algorithms
Single Insert
Fromsrc/tree.rs:812-855, the _insert function:
- Only recomputes one hash per level (logarithmic complexity)
- Uses
get_group()for efficient sibling retrieval - Updates only when
child_pos != 0(not the leftmost child)
Batch Insert
Fromsrc/tree.rs:906-1037, the _insert_many function:
- Extend leaves: Append all leaves to level 0
- Preallocate space: Reserve capacity up the tree
- Level-by-level processing: Compute parents for each level
- Parallel processing (if
parallelfeature enabled):- Chunks parent computation using Rayon
- Threshold:
ROTORTREE_PARALLEL_THRESHOLD(default 1024) - Work units:
PAR_CHUNK_SIZE(default 64)
Depth Calculation
Theceil_log_n function (from src/tree.rs:141-151) computes the depth efficiently:
| Size | N=2 | N=3 | N=4 | N=8 |
|---|---|---|---|---|
| 1 | 0 | 0 | 0 | 0 |
| 2 | 1 | 1 | 1 | 1 |
| 4 | 2 | 2 | 1 | 1 |
| 8 | 3 | 2 | 2 | 1 |
| 16 | 4 | 3 | 2 | 2 |
| 256 | 8 | 6 | 4 | 3 |
Provability
RotorTree’s lean IMT design is compatible with zero-knowledge proof systems:Jolt
Execution cycles:N ∈ {2, 4, 8, 16}: Branching factorD = MAX_DEPTH: Maximum depthd = ⌈log_N(num_leaves)⌉: Actual depth
Noir (UltraHonk via bb)
Expression width:The design was chosen specifically to have functional equivalents in ZK DSLs and Solidity.
Usage Examples
Binary Tree (N=2)
Quaternary Tree (N=4)
Octal Tree (N=8)
Performance Tips
Choosing N
Choosing N
- N=2: Best for compatibility, smallest proofs
- N=4: Good balance for most use cases
- N=8: Very low depth, but larger proofs
- N=16: Extreme depth reduction, use with caution
Batch Operations
Batch Operations
Always use
insert_many() for bulk insertions. It’s significantly faster than repeated insert() calls:- Preallocates all levels
- Amortizes Arc cloning costs
- Enables parallelism (with
parallelfeature)
Parallel Feature
Parallel Feature
Enable the Tune the threshold:
parallel feature for workloads with large batches:Limitations
Next Steps
Tree Structure
Learn about structural sharing with chunks and segments
Proof Generation
Generate and verify inclusion proofs
