@@ -422,6 +422,7 @@ pub enum RangeProofType {
422
422
#[ cfg( test) ]
423
423
mod tests {
424
424
use crate :: maybestd:: vec:: Vec ;
425
+ use crate :: simple_merkle:: error:: RangeProofError ;
425
426
use crate :: NamespaceMerkleHasher ;
426
427
use crate :: {
427
428
namespaced_hash:: { NamespaceId , NamespacedSha2Hasher } ,
@@ -458,6 +459,20 @@ mod tests {
458
459
tree
459
460
}
460
461
462
+ fn tree_from_one_namespace < const NS_ID_SIZE : usize > (
463
+ leaves : u64 ,
464
+ namespace : u64 ,
465
+ ) -> DefaultNmt < NS_ID_SIZE > {
466
+ let mut tree = DefaultNmt :: new ( ) ;
467
+ let namespace = ns_id_from_u64 ( namespace) ;
468
+ for i in 0 ..leaves {
469
+ let data = format ! ( "leaf_{i}" ) ;
470
+ tree. push_leaf ( data. as_bytes ( ) , namespace)
471
+ . expect ( "Failed to push the leaf" ) ;
472
+ }
473
+ tree
474
+ }
475
+
461
476
/// Builds a tree with N leaves
462
477
fn tree_with_n_leaves < const NS_ID_SIZE : usize > ( n : usize ) -> DefaultNmt < NS_ID_SIZE > {
463
478
tree_from_namespace_ids ( ( 0 ..n as u64 ) . collect :: < Vec < _ > > ( ) )
@@ -587,6 +602,63 @@ mod tests {
587
602
}
588
603
}
589
604
605
+ fn test_range_proof_narrowing_within_namespace < const NS_ID_SIZE : usize > ( n : usize ) {
606
+ let ns_id = 4 ;
607
+ let mut tree = tree_from_one_namespace :: < NS_ID_SIZE > ( n as u64 , ns_id) ; // since there's a single namespace, the actual ID shouldn't matter
608
+ let root = tree. root ( ) ;
609
+ for i in 1 ..=n {
610
+ for j in 0 ..=i {
611
+ let proof_nmt = NamespaceProof :: PresenceProof {
612
+ proof : tree. build_range_proof ( j..i) ,
613
+ ignore_max_ns : tree. ignore_max_ns ,
614
+ } ;
615
+ for k in ( j + 1 ) ..=i {
616
+ for l in j..=k {
617
+ let left_leaf_datas: Vec < _ > =
618
+ tree. leaves ( ) [ j..l] . iter ( ) . map ( |l| l. data ( ) ) . collect ( ) ;
619
+ let right_leaf_datas: Vec < _ > =
620
+ tree. leaves ( ) [ k..i] . iter ( ) . map ( |l| l. data ( ) ) . collect ( ) ;
621
+ let narrowed_proof_nmt = proof_nmt. narrow_range (
622
+ & left_leaf_datas,
623
+ & right_leaf_datas,
624
+ ns_id_from_u64 ( ns_id) ,
625
+ ) ;
626
+ if k == l {
627
+ // Cannot prove the empty range!
628
+ assert ! ( narrowed_proof_nmt. is_err( ) ) ;
629
+ assert_eq ! (
630
+ narrowed_proof_nmt. unwrap_err( ) ,
631
+ RangeProofError :: NoLeavesProvided
632
+ ) ;
633
+ continue ;
634
+ } else {
635
+ assert ! ( narrowed_proof_nmt. is_ok( ) ) ;
636
+ }
637
+ let narrowed_proof = narrowed_proof_nmt. unwrap ( ) ;
638
+ let new_leaves: Vec < _ > = tree. leaves ( ) [ l..k]
639
+ . iter ( )
640
+ . map ( |l| l. hash ( ) . clone ( ) )
641
+ . collect ( ) ;
642
+ tree. check_range_proof ( & root, & new_leaves, narrowed_proof. siblings ( ) , l)
643
+ . unwrap ( ) ;
644
+ }
645
+ }
646
+ }
647
+ }
648
+ test_min_and_max_ns_against ( & mut tree)
649
+ }
650
+
651
+ #[ test]
652
+ fn test_range_proof_narrowing_nmt ( ) {
653
+ for x in 0 ..20 {
654
+ test_range_proof_narrowing_within_namespace :: < 8 > ( x) ;
655
+ test_range_proof_narrowing_within_namespace :: < 17 > ( x) ;
656
+ test_range_proof_narrowing_within_namespace :: < 24 > ( x) ;
657
+ test_range_proof_narrowing_within_namespace :: < CELESTIA_NS_ID_SIZE > ( x) ;
658
+ test_range_proof_narrowing_within_namespace :: < 32 > ( x) ;
659
+ }
660
+ }
661
+
590
662
/// Builds a tree with n leaves, and then creates and checks proofs of all valid
591
663
/// ranges, and attempts to narrow every proof and re-check it for the narrowed range
592
664
fn test_range_proof_narrowing_with_n_leaves < const NS_ID_SIZE : usize > ( n : usize ) {
@@ -595,12 +667,8 @@ mod tests {
595
667
for i in 1 ..=n {
596
668
for j in 0 ..=i {
597
669
let proof = tree. build_range_proof ( j..i) ;
598
- let leaf_hashes: Vec < _ > = tree. leaves ( ) [ j..i]
599
- . iter ( )
600
- . map ( |l| l. hash ( ) . clone ( ) )
601
- . collect ( ) ;
602
- for k in ( j + 1 ) ..i {
603
- for l in j..k {
670
+ for k in ( j + 1 ) ..=i {
671
+ for l in j..=k {
604
672
let left_hashes: Vec < _ > = tree. leaves ( ) [ j..l]
605
673
. iter ( )
606
674
. map ( |l| l. hash ( ) . clone ( ) )
@@ -609,47 +677,38 @@ mod tests {
609
677
. iter ( )
610
678
. map ( |l| l. hash ( ) . clone ( ) )
611
679
. collect ( ) ;
612
- let narrowed_proof = proof
613
- . narrow_range_with_hasher (
614
- & left_hashes,
615
- & right_hashes,
616
- NamespacedSha2Hasher :: with_ignore_max_ns ( tree. ignore_max_ns ) ,
617
- )
618
- . unwrap ( ) ;
680
+ let narrowed_proof_simple = proof. narrow_range_with_hasher (
681
+ & left_hashes,
682
+ & right_hashes,
683
+ NamespacedSha2Hasher :: with_ignore_max_ns ( tree. ignore_max_ns ) ,
684
+ ) ;
685
+ if k == l {
686
+ // Cannot prove the empty range!
687
+ assert ! ( narrowed_proof_simple. is_err( ) ) ;
688
+ assert_eq ! (
689
+ narrowed_proof_simple. unwrap_err( ) ,
690
+ RangeProofError :: NoLeavesProvided
691
+ ) ;
692
+ continue ;
693
+ } else {
694
+ assert ! ( narrowed_proof_simple. is_ok( ) ) ;
695
+ }
696
+ let narrowed_proof = narrowed_proof_simple. unwrap ( ) ;
619
697
let new_leaves: Vec < _ > = tree. leaves ( ) [ l..k]
620
698
. iter ( )
621
699
. map ( |l| l. hash ( ) . clone ( ) )
622
700
. collect ( ) ;
623
- let res = tree. check_range_proof (
624
- & root,
625
- & new_leaves,
626
- narrowed_proof. siblings ( ) ,
627
- l,
628
- ) ;
629
- if l != k {
630
- assert ! ( res. is_ok( ) ) ;
631
- assert_eq ! ( res. unwrap( ) , RangeProofType :: Complete )
632
- } else {
633
- // Cannot prove the empty range!
634
- assert ! ( res. is_err( ) )
635
- }
701
+ tree. check_range_proof ( & root, & new_leaves, narrowed_proof. siblings ( ) , l)
702
+ . unwrap ( ) ;
636
703
}
637
704
}
638
- let res = tree. check_range_proof ( & root, & leaf_hashes, proof. siblings ( ) , j) ;
639
- if i != j {
640
- assert ! ( res. is_ok( ) ) ;
641
- assert_eq ! ( res. unwrap( ) , RangeProofType :: Complete )
642
- } else {
643
- // Cannot prove the empty range!
644
- assert ! ( res. is_err( ) )
645
- }
646
705
}
647
706
}
648
707
test_min_and_max_ns_against ( & mut tree)
649
708
}
650
709
651
710
#[ test]
652
- fn test_range_proof_narrowing ( ) {
711
+ fn test_range_proof_narrowing_simple ( ) {
653
712
for x in 0 ..20 {
654
713
test_range_proof_narrowing_with_n_leaves :: < 8 > ( x) ;
655
714
test_range_proof_narrowing_with_n_leaves :: < 17 > ( x) ;
0 commit comments