@@ -114,6 +114,10 @@ pub fn node_indices_added_by_append(old_leaf_count: u64) -> Vec<u64> {
114
114
115
115
/// Get the node indices of the authentication path starting from the specified
116
116
/// leaf, to its peak.
117
+ ///
118
+ /// # Panics
119
+ ///
120
+ /// Panics if the leaf index is out-of-bounds.
117
121
pub fn auth_path_node_indices ( num_leafs : u64 , leaf_index : u64 ) -> Vec < u64 > {
118
122
assert ! (
119
123
leaf_index < num_leafs,
@@ -274,11 +278,14 @@ pub fn node_index_to_leaf_index(node_index: u64) -> Option<u64> {
274
278
275
279
#[ cfg( test) ]
276
280
mod mmr_test {
281
+ use proptest:: prelude:: Just ;
277
282
use proptest:: prop_assert_eq;
278
283
use rand:: RngCore ;
279
284
use test_strategy:: proptest;
280
285
281
286
use super :: * ;
287
+ use crate :: prelude:: Digest ;
288
+ use crate :: prelude:: MmrMembershipProof ;
282
289
283
290
#[ test]
284
291
fn leaf_index_to_node_index_test ( ) {
@@ -540,6 +547,24 @@ mod mmr_test {
540
547
}
541
548
}
542
549
550
+ #[ test]
551
+ #[ should_panic( expected = "Leaf index out-of-bounds: 5/5" ) ]
552
+ fn auth_path_indices_out_of_bounds_unit_test ( ) {
553
+ auth_path_node_indices ( 5 , 5 ) ;
554
+ }
555
+
556
+ #[ proptest]
557
+ fn auth_path_indices_prop (
558
+ #[ strategy( 0u64 ..( u64 :: MAX >>1 ) ) ] _num_leafs : u64 ,
559
+ #[ strategy( 0u64 ..( #_num_leafs) ) ] leaf_index : u64 ,
560
+ #[ strategy( Just ( auth_path_node_indices( #_num_leafs, #leaf_index) ) ) ] node_indices : Vec < u64 > ,
561
+ ) {
562
+ let mp = MmrMembershipProof {
563
+ authentication_path : vec ! [ Digest :: default ( ) ; node_indices. len( ) ] ,
564
+ } ;
565
+ prop_assert_eq ! ( mp. get_node_indices( leaf_index) , node_indices) ;
566
+ }
567
+
543
568
#[ test]
544
569
fn auth_path_indices_unit_test ( ) {
545
570
assert_eq ! ( vec![ 2 , 6 , 14 , 30 ] , auth_path_node_indices( 16 , 0 ) ) ;
0 commit comments