@@ -121,7 +121,7 @@ impl MerkleTree {
121121 /// with the same type as the index type used by `Vec`.
122122 ///
123123 /// If you need to read the root, try [`root()`](Self::root) instead.
124- const ROOT_INDEX : usize = 1_usize ;
124+ const ROOT_INDEX : usize = ROOT_INDEX as usize ;
125125
126126 const MAX_NUM_NODES : usize = 1_usize << 25 ;
127127 const MAX_NUM_LEAFS : usize = Self :: MAX_NUM_NODES / 2 ;
@@ -299,7 +299,7 @@ impl MerkleTree {
299299 pub fn height ( & self ) -> MerkleTreeHeight {
300300 let leaf_count = self . num_leafs ( ) ;
301301 debug_assert ! ( leaf_count. is_power_of_two( ) ) ;
302- MerkleTreeHeight :: try_from ( leaf_count. ilog2 ( ) ) . expect ( "log of num leafs should fit in u32" )
302+ leaf_count. ilog2 ( )
303303 }
304304
305305 /// All nodes of the Merkle tree.
@@ -316,10 +316,14 @@ impl MerkleTree {
316316
317317 /// All leafs of the Merkle tree.
318318 pub fn leafs ( & self ) -> impl Iterator < Item = & Digest > {
319- self . nodes . iter ( ) . skip (
320- ( self . num_nodes ( ) / 2 )
321- . try_into ( )
322- . expect ( "MerkleTreeNodeIndex to usize conversion error" ) ,
319+ // This conversion can only fail if the number of leafs is larger than
320+ // usize::MAX. This implies that the number of nodes is larger than
321+ // usize::MAX. Since the nodes are stored in a Vec, the number of nodes
322+ // can never exceed usize::MAX.
323+ // This proof by contradiction shows that unwrapping is fine.
324+ let num_leafs = usize:: try_from ( self . num_leafs ( ) ) . unwrap ( ) ;
325+
326+ self . nodes . iter ( ) . skip ( num_leafs)
323327 )
324328 }
325329
@@ -656,14 +660,7 @@ pub mod merkle_tree_test {
656660 #[ strategy( arb( ) ) ]
657661 pub tree : MerkleTree ,
658662
659- #[
660- strategy(
661- vec(
662- ( 0 as MerkleTreeLeafIndex ) ..#tree. num_leafs( ) ,
663- 0 ..( #tree. num_leafs( ) as usize )
664- )
665- )
666- ]
663+ #[ strategy( vec( 0 ..#tree. num_leafs( ) , 0 ..( #tree. num_leafs( ) as usize ) ) ) ]
667664 pub selected_indices : Vec < MerkleTreeLeafIndex > ,
668665 }
669666
0 commit comments