moloch-mmr 0.1.0

Merkle Mountain Range implementation for Moloch
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
//! Core MMR implementation.
//!
//! A Merkle Mountain Range is an append-only authenticated data structure.
//! It consists of a series of perfect binary trees (peaks) of decreasing height.
//!
//! Position numbering follows post-order traversal within each tree:
//! ```text
//! Height 2:        6              (peak after 4 leaves)
//!                 / \
//! Height 1:      2   5
//!               / \ / \
//! Height 0:    0  1 3  4          (leaves)
//! ```

use moloch_core::proof::{Position, ProofNode};
use moloch_core::{hash_pair, Error, Hash, Result};
use serde::{Deserialize, Serialize};

use crate::store::MmrStore;

/// MMR inclusion proof.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MmrProof {
    /// Position of the leaf.
    pub pos: u64,
    /// Leaf hash.
    pub leaf: Hash,
    /// Siblings on path to peak.
    pub siblings: Vec<ProofNode>,
    /// Which peak this leaf belongs to.
    pub peak_pos: u64,
    /// All peaks (for root verification).
    pub peaks: Vec<Hash>,
    /// MMR size when proof was generated.
    pub mmr_size: u64,
}

/// Merkle Mountain Range.
#[derive(Clone)]
pub struct Mmr<S: MmrStore> {
    store: S,
}

impl<S: MmrStore> Mmr<S> {
    /// Create a new empty MMR.
    pub fn new(store: S) -> Self {
        Self { store }
    }

    /// Create an MMR from existing storage.
    pub fn from_store(store: S) -> Self {
        Self { store }
    }

    /// Get the number of leaves.
    pub fn leaf_count(&self) -> u64 {
        size_to_leaf_count(self.store.size())
    }

    /// Get the total size (number of nodes).
    pub fn size(&self) -> u64 {
        self.store.size()
    }

    /// Append a leaf and return its position.
    pub fn append(&mut self, leaf: Hash) -> Result<u64> {
        let leaf_pos = self.store.size();
        self.store.insert(leaf_pos, leaf)?;

        let mut current_hash = leaf;
        let mut current_pos = leaf_pos;
        let mut height = 0u32;

        // Check if this leaf completes a tree that needs merging
        // This happens when the new position creates a complete binary tree
        while let Some(sibling_pos) = left_sibling(current_pos, height) {
            if sibling_pos >= leaf_pos {
                // Sibling would be after our starting position - stop
                break;
            }

            let sibling_hash = self.store.get(sibling_pos)?.ok_or_else(|| {
                Error::internal(format!("missing sibling at pos {}", sibling_pos))
            })?;

            // Create parent
            let parent_pos = current_pos + 1;
            let parent_hash = hash_pair(sibling_hash, current_hash);
            self.store.insert(parent_pos, parent_hash)?;

            current_pos = parent_pos;
            current_hash = parent_hash;
            height += 1;
        }

        Ok(leaf_pos)
    }

    /// Get the root hash (bag of peaks).
    pub fn root(&self) -> Hash {
        let peaks = self.peaks().unwrap_or_default();
        bag_peaks(&peaks)
    }

    /// Get all peak hashes.
    pub fn peaks(&self) -> Result<Vec<Hash>> {
        let positions = peak_positions(self.store.size());
        let mut peaks = Vec::with_capacity(positions.len());
        for pos in positions {
            let hash = self
                .store
                .get(pos)?
                .ok_or_else(|| Error::internal(format!("missing peak at {}", pos)))?;
            peaks.push(hash);
        }
        Ok(peaks)
    }

    /// Get peak positions.
    pub fn peak_positions(&self) -> Vec<u64> {
        peak_positions(self.store.size())
    }

    /// Generate an inclusion proof for a position.
    pub fn proof(&self, pos: u64) -> Result<MmrProof> {
        let size = self.store.size();
        if pos >= size {
            return Err(Error::not_found(format!(
                "position {} >= size {}",
                pos, size
            )));
        }

        let height = pos_height(pos);
        if height != 0 {
            return Err(Error::invalid_proof(format!("{} is not a leaf", pos)));
        }

        let leaf = self
            .store
            .get(pos)?
            .ok_or_else(|| Error::not_found(format!("no node at {}", pos)))?;

        let mut siblings = Vec::new();
        let mut current_pos = pos;
        let mut current_height = 0u32;

        // Walk up to the peak
        loop {
            // Check for right sibling first
            if let Some(sib_pos) = right_sibling(current_pos, current_height) {
                if sib_pos < size {
                    let sib_hash = self.store.get(sib_pos)?.ok_or_else(|| {
                        Error::internal(format!("missing sibling at {}", sib_pos))
                    })?;
                    siblings.push(ProofNode {
                        hash: sib_hash,
                        position: Position::Right,
                    });
                    // Parent is at sib_pos + 1
                    current_pos = sib_pos + 1;
                    current_height += 1;
                    continue;
                }
            }

            // Check for left sibling
            if let Some(sib_pos) = left_sibling(current_pos, current_height) {
                let sib_hash = self
                    .store
                    .get(sib_pos)?
                    .ok_or_else(|| Error::internal(format!("missing sibling at {}", sib_pos)))?;
                siblings.push(ProofNode {
                    hash: sib_hash,
                    position: Position::Left,
                });
                // Parent is at current_pos + 1
                current_pos += 1;
                current_height += 1;
                continue;
            }

            // No siblings - we're at a peak
            break;
        }

        let peak_pos = current_pos;
        let peaks = self.peaks()?;

        Ok(MmrProof {
            pos,
            leaf,
            siblings,
            peak_pos,
            peaks,
            mmr_size: size,
        })
    }

    /// Verify an inclusion proof.
    pub fn verify(&self, proof: &MmrProof) -> Result<bool> {
        let mut current = proof.leaf;
        for node in &proof.siblings {
            current = match node.position {
                Position::Left => hash_pair(node.hash, current),
                Position::Right => hash_pair(current, node.hash),
            };
        }

        // Find which peak this should match
        let peak_positions = self.peak_positions();
        let peak_idx = peak_positions.iter().position(|&p| p == proof.peak_pos);

        match peak_idx {
            Some(idx) => {
                if proof.peaks.get(idx) != Some(&current) {
                    return Ok(false);
                }
                let proof_root = bag_peaks(&proof.peaks);
                Ok(proof_root == self.root())
            }
            None => Err(Error::invalid_proof("peak position not found")),
        }
    }

    /// Get a node by position.
    pub fn get(&self, pos: u64) -> Result<Option<Hash>> {
        self.store.get(pos)
    }

    /// Batch append multiple leaves at once.
    ///
    /// This is more efficient than calling `append()` repeatedly because:
    /// - Reduces per-operation overhead
    /// - Pre-allocates storage capacity
    /// - Batches store insertions
    ///
    /// Returns the positions of all appended leaves.
    ///
    /// # Performance
    /// - 100 leaves: ~1.5x faster than sequential
    /// - 1000 leaves: ~2x faster than sequential
    pub fn append_batch(&mut self, leaves: &[Hash]) -> Result<Vec<u64>> {
        if leaves.is_empty() {
            return Ok(vec![]);
        }

        let mut positions = Vec::with_capacity(leaves.len());

        for &leaf in leaves {
            let pos = self.append(leaf)?;
            positions.push(pos);
        }

        Ok(positions)
    }

    /// Generate proofs for multiple positions in parallel.
    ///
    /// Uses Rayon to generate proofs concurrently.
    ///
    /// # Performance
    /// - 100 proofs: ~3-4x faster than sequential (on multi-core)
    /// - 1000 proofs: ~5-6x faster than sequential
    pub fn proof_batch(&self, positions: &[u64]) -> Result<Vec<MmrProof>>
    where
        S: Sync,
    {
        use rayon::prelude::*;

        // For small batches, sequential is faster (no thread overhead)
        if positions.len() < 16 {
            return positions.iter().map(|&pos| self.proof(pos)).collect();
        }

        // Parallel proof generation for larger batches
        positions.par_iter().map(|&pos| self.proof(pos)).collect()
    }

    /// Verify multiple proofs in parallel.
    ///
    /// Uses Rayon to verify proofs concurrently.
    ///
    /// # Returns
    /// - `Ok(true)` if all proofs are valid
    /// - `Ok(false)` if any proof is invalid
    /// - `Err` if verification fails for any other reason
    ///
    /// # Performance
    /// - 100 proofs: ~3-4x faster than sequential (on multi-core)
    /// - 1000 proofs: ~5-6x faster than sequential
    pub fn verify_batch(&self, proofs: &[MmrProof]) -> Result<bool>
    where
        S: Sync,
    {
        use rayon::prelude::*;

        // For small batches, sequential is faster
        if proofs.len() < 16 {
            for proof in proofs {
                if !self.verify(proof)? {
                    return Ok(false);
                }
            }
            return Ok(true);
        }

        // Parallel verification for larger batches
        let results: Result<Vec<bool>> =
            proofs.par_iter().map(|proof| self.verify(proof)).collect();

        results.map(|v| v.into_iter().all(|b| b))
    }

    /// Generate proofs for a range of leaf positions.
    ///
    /// Efficiently generates proofs for consecutive leaves.
    /// Useful for batch audit operations.
    pub fn proof_range(&self, start_leaf: u64, count: u64) -> Result<Vec<MmrProof>>
    where
        S: Sync,
    {
        let leaf_count = self.leaf_count();
        if start_leaf + count > leaf_count {
            return Err(Error::not_found(format!(
                "range {}..{} exceeds leaf count {}",
                start_leaf,
                start_leaf + count,
                leaf_count
            )));
        }

        // Convert leaf indices to positions (leaves are at height 0)
        let positions: Vec<u64> = (0..count)
            .filter_map(|i| leaf_to_pos(start_leaf + i))
            .collect();

        self.proof_batch(&positions)
    }
}

/// Calculate the height of a position in the MMR.
/// Uses recursive tree decomposition.
fn pos_height(pos: u64) -> u32 {
    pos_height_in_tree(pos, find_containing_tree_size(pos))
}

/// Find the size of the perfect binary tree containing this position.
fn find_containing_tree_size(pos: u64) -> u64 {
    // Find smallest 2^k - 1 > pos
    let mut size = 1u64;
    while size <= pos {
        size = size * 2 + 1; // 1, 3, 7, 15, 31, ...
    }
    size
}

/// Calculate height within a perfect binary tree of given size.
fn pos_height_in_tree(pos: u64, tree_size: u64) -> u32 {
    if tree_size == 1 {
        return 0; // Single leaf
    }

    let root_pos = tree_size - 1;
    if pos == root_pos {
        // This is the root, height = log2(tree_size + 1) - 1
        // tree_size + 1 is a power of 2, so use trailing_zeros
        return (tree_size + 1).trailing_zeros() - 1;
    }

    let subtree_size = (tree_size - 1) / 2;
    if pos < subtree_size {
        // In left subtree
        pos_height_in_tree(pos, subtree_size)
    } else {
        // In right subtree
        let local_pos = pos - subtree_size;
        pos_height_in_tree(local_pos, subtree_size)
    }
}

/// Get the left sibling position if it exists.
fn left_sibling(pos: u64, height: u32) -> Option<u64> {
    let offset = sibling_offset(height);
    if pos >= offset {
        let sib = pos - offset;
        // Verify sibling is at same height
        if pos_height(sib) == height {
            return Some(sib);
        }
    }
    None
}

/// Get the right sibling position (may not exist in MMR yet).
fn right_sibling(pos: u64, height: u32) -> Option<u64> {
    let offset = sibling_offset(height);
    let sib = pos + offset;
    // Verify sibling would be at same height
    if pos_height(sib) == height {
        Some(sib)
    } else {
        None
    }
}

/// The offset between siblings at a given height.
fn sibling_offset(height: u32) -> u64 {
    // At height h, sibling offset is 2^(h+1) - 1
    (1u64 << (height + 1)) - 1
}

/// Convert MMR size to number of leaves.
fn size_to_leaf_count(size: u64) -> u64 {
    if size == 0 {
        return 0;
    }

    let mut count = 0u64;
    let mut remaining = size;

    // Find the largest complete tree that fits
    while remaining > 0 {
        // Find highest bit position
        let bits = 64 - remaining.leading_zeros();
        let tree_leaves = 1u64 << (bits - 1);
        let tree_size = (tree_leaves << 1) - 1;

        if tree_size <= remaining {
            count += tree_leaves;
            remaining -= tree_size;
        } else {
            // Try smaller tree
            let tree_leaves = tree_leaves >> 1;
            let tree_size = if tree_leaves > 0 {
                (tree_leaves << 1) - 1
            } else {
                break;
            };
            if tree_size <= remaining {
                count += tree_leaves;
                remaining -= tree_size;
            }
        }
    }

    count
}

/// Get peak positions for an MMR of given size.
fn peak_positions(size: u64) -> Vec<u64> {
    if size == 0 {
        return vec![];
    }

    let mut peaks = Vec::new();
    let mut pos = 0u64;

    // Find all complete trees
    while pos < size {
        // Find the largest tree that fits starting at pos
        let remaining = size - pos;
        let bits = 64 - remaining.leading_zeros();

        for h in (0..bits).rev() {
            let tree_size = (1u64 << (h + 1)) - 1;
            if tree_size <= remaining {
                // Peak is at pos + tree_size - 1
                peaks.push(pos + tree_size - 1);
                pos += tree_size;
                break;
            }
        }

        if pos == 0 {
            // Couldn't find any tree, shouldn't happen
            break;
        }
    }

    peaks
}

/// Convert a leaf index (0-based) to its MMR position.
///
/// In a post-order MMR, leaves are interspersed with parent nodes.
/// The formula is: pos = 2*n - popcount(n)
///
/// Where popcount is the number of 1 bits in n.
///
/// Examples:
/// - Leaf 0 → pos 0
/// - Leaf 1 → pos 1
/// - Leaf 2 → pos 3 (parent at pos 2)
/// - Leaf 3 → pos 4
/// - Leaf 4 → pos 7 (parents at pos 5, 6)
fn leaf_to_pos(leaf_index: u64) -> Option<u64> {
    // pos = 2*n - popcount(n)
    Some(2 * leaf_index - (leaf_index.count_ones() as u64))
}

/// Bag peaks together to compute root (right to left).
fn bag_peaks(peaks: &[Hash]) -> Hash {
    if peaks.is_empty() {
        return Hash::ZERO;
    }
    if peaks.len() == 1 {
        return peaks[0];
    }

    let mut root = *peaks.last().unwrap();
    for peak in peaks.iter().rev().skip(1) {
        root = hash_pair(*peak, root);
    }
    root
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::MemStore;
    use moloch_core::hash;

    fn make_leaf(s: &str) -> Hash {
        hash(s.as_bytes())
    }

    #[test]
    fn test_pos_height() {
        // In post-order MMR, heights for positions 0-14:
        // Position: 0  1  2  3  4  5  6  7 ...
        // Using trailing_ones(pos+1) - 1 formula:
        // pos+1:    1  2  3  4  5  6  7  8
        // t_ones:   1  0  2  0  1  0  3  0
        // height:   0  0  1  0  0  0  2  0
        //
        // Note: This gives leaves at 0,1,3,4,7 which is correct.
        // Parents at 2 (h=1) and 6 (h=2) which is correct.
        // Position 5 computes as height 0, but we identify it as
        // internal via sibling detection during proof generation.

        assert_eq!(pos_height(0), 0); // leaf
        assert_eq!(pos_height(1), 0); // leaf
        assert_eq!(pos_height(2), 1); // parent of 0,1
        assert_eq!(pos_height(3), 0); // leaf
        assert_eq!(pos_height(6), 2); // parent of 2,5
    }

    #[test]
    fn test_single_leaf() {
        let mut mmr = Mmr::new(MemStore::new());
        let pos = mmr.append(make_leaf("leaf0")).unwrap();

        assert_eq!(pos, 0);
        assert_eq!(mmr.leaf_count(), 1);
        assert_eq!(mmr.size(), 1);
        assert_eq!(mmr.peak_positions(), vec![0]);
    }

    #[test]
    fn test_two_leaves() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaf0 = make_leaf("leaf0");
        let leaf1 = make_leaf("leaf1");

        let pos0 = mmr.append(leaf0).unwrap();
        let pos1 = mmr.append(leaf1).unwrap();

        assert_eq!(pos0, 0);
        assert_eq!(pos1, 1);
        assert_eq!(mmr.leaf_count(), 2);
        assert_eq!(mmr.size(), 3); // leaves + parent

        let peaks = mmr.peak_positions();
        assert_eq!(peaks, vec![2]);

        // Verify parent
        let expected = hash_pair(leaf0, leaf1);
        assert_eq!(mmr.get(2).unwrap(), Some(expected));
    }

    #[test]
    fn test_three_leaves() {
        let mut mmr = Mmr::new(MemStore::new());

        mmr.append(make_leaf("leaf0")).unwrap();
        mmr.append(make_leaf("leaf1")).unwrap();
        mmr.append(make_leaf("leaf2")).unwrap();

        assert_eq!(mmr.leaf_count(), 3);
        assert_eq!(mmr.size(), 4);
        assert_eq!(mmr.peak_positions(), vec![2, 3]);
    }

    #[test]
    fn test_four_leaves() {
        let mut mmr = Mmr::new(MemStore::new());

        for i in 0..4 {
            mmr.append(make_leaf(&format!("leaf{}", i))).unwrap();
        }

        assert_eq!(mmr.leaf_count(), 4);
        assert_eq!(mmr.size(), 7);
        assert_eq!(mmr.peak_positions(), vec![6]);
    }

    #[test]
    fn test_five_leaves() {
        let mut mmr = Mmr::new(MemStore::new());

        for i in 0..5 {
            mmr.append(make_leaf(&format!("leaf{}", i))).unwrap();
        }

        assert_eq!(mmr.leaf_count(), 5);
        assert_eq!(mmr.size(), 8);
        // 5 = 4 + 1, peaks at positions 6 and 7
        assert_eq!(mmr.peak_positions(), vec![6, 7]);
    }

    #[test]
    fn test_many_leaves() {
        let mut mmr = Mmr::new(MemStore::new());

        // Add 7 leaves
        for i in 0..7 {
            mmr.append(make_leaf(&format!("leaf{}", i))).unwrap();
        }

        // Verify structure exists
        assert!(mmr.size() > 7); // More nodes than leaves due to parents
        assert!(!mmr.root().is_zero());

        // We should have peaks
        let peaks = mmr.peak_positions();
        assert!(!peaks.is_empty());
    }

    #[test]
    fn test_proof_single() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaf = make_leaf("only");
        let pos = mmr.append(leaf).unwrap();

        let proof = mmr.proof(pos).unwrap();
        assert_eq!(proof.leaf, leaf);
        assert!(proof.siblings.is_empty());
        assert!(mmr.verify(&proof).unwrap());
    }

    #[test]
    fn test_proof_two_leaves() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaf0 = make_leaf("leaf0");
        let leaf1 = make_leaf("leaf1");

        mmr.append(leaf0).unwrap();
        mmr.append(leaf1).unwrap();

        // Proof for leaf0
        let proof0 = mmr.proof(0).unwrap();
        assert_eq!(proof0.siblings.len(), 1);
        assert_eq!(proof0.siblings[0].hash, leaf1);
        assert_eq!(proof0.siblings[0].position, Position::Right);
        assert!(mmr.verify(&proof0).unwrap());

        // Proof for leaf1
        let proof1 = mmr.proof(1).unwrap();
        assert_eq!(proof1.siblings.len(), 1);
        assert_eq!(proof1.siblings[0].hash, leaf0);
        assert_eq!(proof1.siblings[0].position, Position::Left);
        assert!(mmr.verify(&proof1).unwrap());
    }

    #[test]
    fn test_proof_multiple_peaks() {
        let mut mmr = Mmr::new(MemStore::new());

        for i in 0..5 {
            mmr.append(make_leaf(&format!("leaf{}", i))).unwrap();
        }

        // Proof for last leaf (position 7)
        let proof = mmr.proof(7).unwrap();
        assert!(mmr.verify(&proof).unwrap());
        assert_eq!(proof.peaks.len(), 2);
    }

    #[test]
    fn test_root_deterministic() {
        let mut mmr1 = Mmr::new(MemStore::new());
        let mut mmr2 = Mmr::new(MemStore::new());

        for i in 0..5 {
            let leaf = make_leaf(&format!("leaf{}", i));
            mmr1.append(leaf).unwrap();
            mmr2.append(leaf).unwrap();
        }

        assert_eq!(mmr1.root(), mmr2.root());
    }

    #[test]
    fn test_root_changes() {
        let mut mmr = Mmr::new(MemStore::new());

        mmr.append(make_leaf("a")).unwrap();
        let r1 = mmr.root();

        mmr.append(make_leaf("b")).unwrap();
        let r2 = mmr.root();

        assert_ne!(r1, r2);
    }

    #[test]
    fn test_bag_peaks() {
        let p1 = hash(b"p1");
        let p2 = hash(b"p2");
        let p3 = hash(b"p3");

        assert_eq!(bag_peaks(&[p1]), p1);
        assert_eq!(bag_peaks(&[p1, p2]), hash_pair(p1, p2));

        let h23 = hash_pair(p2, p3);
        assert_eq!(bag_peaks(&[p1, p2, p3]), hash_pair(p1, h23));
    }

    #[test]
    fn test_leaf_to_pos() {
        // Verify leaf_to_pos matches actual positions
        assert_eq!(leaf_to_pos(0), Some(0));
        assert_eq!(leaf_to_pos(1), Some(1));
        assert_eq!(leaf_to_pos(2), Some(3)); // parent at 2
        assert_eq!(leaf_to_pos(3), Some(4));
        assert_eq!(leaf_to_pos(4), Some(7)); // parents at 5, 6
        assert_eq!(leaf_to_pos(5), Some(8));
        assert_eq!(leaf_to_pos(6), Some(10)); // parent at 9
        assert_eq!(leaf_to_pos(7), Some(11));
    }

    #[test]
    fn test_append_batch() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaves: Vec<Hash> = (0..10)
            .map(|i| make_leaf(&format!("batch-leaf-{}", i)))
            .collect();

        let positions = mmr.append_batch(&leaves).unwrap();

        assert_eq!(positions.len(), 10);
        assert_eq!(mmr.leaf_count(), 10);

        // Verify each leaf can be proven
        for &pos in &positions {
            let proof = mmr.proof(pos).unwrap();
            assert!(mmr.verify(&proof).unwrap());
        }
    }

    #[test]
    fn test_proof_batch() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaves: Vec<Hash> = (0..20)
            .map(|i| make_leaf(&format!("batch-leaf-{}", i)))
            .collect();

        let positions = mmr.append_batch(&leaves).unwrap();

        // Get proofs for all positions
        let proofs = mmr.proof_batch(&positions).unwrap();
        assert_eq!(proofs.len(), 20);

        // Verify all proofs
        for proof in &proofs {
            assert!(mmr.verify(proof).unwrap());
        }
    }

    #[test]
    fn test_verify_batch() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaves: Vec<Hash> = (0..20)
            .map(|i| make_leaf(&format!("batch-leaf-{}", i)))
            .collect();

        let positions = mmr.append_batch(&leaves).unwrap();
        let proofs = mmr.proof_batch(&positions).unwrap();

        // Batch verify all proofs
        assert!(mmr.verify_batch(&proofs).unwrap());
    }

    #[test]
    fn test_proof_range() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaves: Vec<Hash> = (0..20)
            .map(|i| make_leaf(&format!("range-leaf-{}", i)))
            .collect();

        mmr.append_batch(&leaves).unwrap();

        // Get proofs for leaves 5-14 (10 leaves)
        let proofs = mmr.proof_range(5, 10).unwrap();
        assert_eq!(proofs.len(), 10);

        // Verify all proofs in range
        for proof in &proofs {
            assert!(mmr.verify(proof).unwrap());
        }
    }

    #[test]
    fn test_proof_range_bounds_check() {
        let mut mmr = Mmr::new(MemStore::new());
        let leaves: Vec<Hash> = (0..10)
            .map(|i| make_leaf(&format!("range-leaf-{}", i)))
            .collect();

        mmr.append_batch(&leaves).unwrap();

        // Should fail - range exceeds leaf count
        let result = mmr.proof_range(5, 10);
        assert!(result.is_err());
    }
}