@@ -13,6 +13,7 @@ compile_error!("must choose either the `std` or `alloc` feature");
1313
1414#[ path = "entry.rs" ]
1515mod borrowed;
16+ mod proof;
1617
1718/// Cryptographic hash utilities and traits used in Merkle trees.
1819pub mod hasher;
@@ -27,11 +28,12 @@ pub mod tree;
2728/// Includes errors for tree construction, hashing, and I/O operations.
2829pub mod error;
2930
30- pub ( crate ) use crate :: error:: { EntryError , MrkleError , NodeError , ProofError , TreeError } ;
31+ pub ( crate ) use crate :: error:: { EntryError , NodeError , ProofError , TreeError } ;
3132pub ( crate ) use crate :: tree:: DefaultIx ;
3233
3334pub use crate :: hasher:: { GenericArray , Hasher , MrkleHasher } ;
34- pub use crate :: tree:: { IndexType , Iter , IterIdx , Node , NodeIndex , Tree , TreeView } ;
35+ pub use crate :: proof:: { MrkleProof , MrkleProofNode } ;
36+ pub use crate :: tree:: { IndexType , Iter , IterIdx , MutNode , Node , NodeIndex , Tree , TreeView } ;
3537pub use borrowed:: * ;
3638
3739#[ allow( unused_imports, reason = "future proofing for tree features." ) ]
@@ -54,6 +56,7 @@ pub(crate) mod prelude {
5456 pub use std:: vec:: Vec ;
5557 }
5658
59+ pub use core:: fmt:: { Debug , Display } ;
5760 pub use core:: marker:: { Copy , PhantomData } ;
5861 pub use core:: slice:: SliceIndex ;
5962 pub ( crate ) use crypto:: digest:: Digest ;
@@ -63,7 +66,6 @@ pub(crate) mod prelude {
6366 pub use stds:: * ;
6467}
6568
66- use crypto:: digest:: OutputSizeUser ;
6769use prelude:: * ;
6870
6971/// A generic immutable node in a Merkle Tree.
@@ -491,7 +493,7 @@ impl<T, D: Digest, Ix: IndexType> core::fmt::Debug for MrkleNode<T, D, Ix> {
491493}
492494
493495// Display implementation - user-friendly representation
494- impl < T , D : Digest , Ix : IndexType > core :: fmt :: Display for MrkleNode < T , D , Ix > {
496+ impl < T , D : Digest , Ix : IndexType > Display for MrkleNode < T , D , Ix > {
495497 fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
496498 let hash_bytes = self . hash . as_slice ( ) ;
497499 let hash_preview = if hash_bytes. len ( ) >= 4 {
@@ -506,16 +508,7 @@ impl<T, D: Digest, Ix: IndexType> core::fmt::Display for MrkleNode<T, D, Ix> {
506508 format ! ( "{:02x?}" , hash_bytes)
507509 } ;
508510
509- if self . is_leaf ( ) {
510- write ! ( f, "Leaf[hash: {}]" , hash_preview)
511- } else {
512- write ! (
513- f,
514- "Internal[children: {}, hash: {}]" ,
515- self . children. len( ) ,
516- hash_preview
517- )
518- }
511+ write ! ( f, "{}" , hash_preview)
519512 }
520513}
521514
@@ -537,6 +530,22 @@ impl<T, D: Digest, Ix: IndexType> core::fmt::Display for MrkleNode<T, D, Ix> {
537530/// * `Ix` - The index type for node references, defaults to [`DefaultIx`]
538531///
539532///
533+ /// # Example
534+ ///
535+ /// ```
536+ /// use mrkle::MrkleTree;
537+ /// use sha1::Sha1;
538+ ///
539+ /// // build basic binary merkle tree.
540+ /// let tree = MrkleTree::<&str, Sha1>::from(vec![
541+ /// "A",
542+ /// "B",
543+ /// "C",
544+ /// "D",
545+ /// "E",
546+ /// ]);
547+ /// ```
548+ ///
540549/// # Security Considerations
541550///
542551/// The security of a `MrkleTree` depends entirely on the cryptographic strength
@@ -551,7 +560,7 @@ pub struct MrkleTree<T, D: Digest, Ix: IndexType = DefaultIx> {
551560 /// This field is private to maintain invariants about the tree structure
552561 /// and ensure all modifications go through the proper cryptographic
553562 /// verification process.
554- core : Tree < T , MrkleNode < T , D , Ix > , Ix > ,
563+ core : Tree < MrkleNode < T , D , Ix > , Ix > ,
555564}
556565
557566impl < T , D : Digest > Default for MrkleTree < T , D > {
@@ -567,8 +576,8 @@ where
567576{
568577 /// Constructs a Merkle binary tree from leaf nodes using bottom-up approach.
569578 ///
570- /// # Parameters
571- /// - `leaves`: Vector of `T` nodes to build the tree from
579+ /// # Arguments
580+ /// * `leaves` - Vector of `T` nodes to build the tree from
572581 ///
573582 /// # Returns
574583 /// A generic binary [`MrkleTree`]
@@ -664,7 +673,7 @@ impl<T, D: Digest, Ix: IndexType> MrkleTree<T, D, Ix> {
664673
665674 /// Return root [`TreeView`] of the [`MrkleTree`]
666675 #[ inline]
667- pub fn view ( & self ) -> TreeView < ' _ , T , MrkleNode < T , D , Ix > , Ix > {
676+ pub fn view ( & self ) -> TreeView < ' _ , MrkleNode < T , D , Ix > , Ix > {
668677 self . core . view ( )
669678 }
670679
@@ -674,17 +683,17 @@ impl<T, D: Digest, Ix: IndexType> MrkleTree<T, D, Ix> {
674683 pub fn subtree_view (
675684 & self ,
676685 root : NodeIndex < Ix > ,
677- ) -> Option < TreeView < ' _ , T , MrkleNode < T , D , Ix > , Ix > > {
686+ ) -> Option < TreeView < ' _ , MrkleNode < T , D , Ix > , Ix > > {
678687 self . core . subtree_view ( root)
679688 }
680689
681690 /// Returns Iterator pattern [`Iter`] which returns a unmutable Node reference.
682- pub fn iter ( & self ) -> Iter < ' _ , T , MrkleNode < T , D , Ix > , Ix > {
691+ pub fn iter ( & self ) -> Iter < ' _ , MrkleNode < T , D , Ix > , Ix > {
683692 self . core . iter ( )
684693 }
685694
686695 ///Returns Iterator pattern [`IterIdx`] which returns a [`NodeIndex<Ix>`] of the node.
687- pub fn iter_idx ( & self ) -> IterIdx < ' _ , T , MrkleNode < T , D , Ix > , Ix > {
696+ pub fn iter_idx ( & self ) -> IterIdx < ' _ , MrkleNode < T , D , Ix > , Ix > {
688697 self . core . iter_idx ( )
689698 }
690699}
@@ -697,19 +706,40 @@ where
697706 pub fn subtree_from_node (
698707 & self ,
699708 target : & MrkleNode < T , D , Ix > ,
700- ) -> Option < TreeView < ' _ , T , MrkleNode < T , D , Ix > , Ix > > {
709+ ) -> Option < TreeView < ' _ , MrkleNode < T , D , Ix > , Ix > > {
701710 self . core . subtree_from_node ( target)
702711 }
703712}
704713
705714impl < ' a , T , D : Digest , Ix : IndexType > IntoIterator for & ' a MrkleTree < T , D , Ix > {
706- type IntoIter = Iter < ' a , T , MrkleNode < T , D , Ix > , Ix > ;
715+ type IntoIter = Iter < ' a , MrkleNode < T , D , Ix > , Ix > ;
707716 type Item = & ' a MrkleNode < T , D , Ix > ;
708717 fn into_iter ( self ) -> Self :: IntoIter {
709718 self . iter ( )
710719 }
711720}
712721
722+ impl < T , D : Digest , Ix : IndexType > From < Vec < T > > for MrkleTree < T , D , Ix >
723+ where
724+ T : AsRef < [ u8 ] > ,
725+ {
726+ fn from ( value : Vec < T > ) -> Self {
727+ MrkleTree :: from_leaves ( value)
728+ }
729+ }
730+
731+ impl < T , D : Digest , Ix : IndexType > Display for MrkleTree < T , D , Ix > {
732+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
733+ write ! ( f, "{}" , self . core)
734+ }
735+ }
736+
737+ impl < T , D : Digest , Ix : IndexType > Debug for MrkleTree < T , D , Ix > {
738+ fn fmt ( & self , f : & mut core:: fmt:: Formatter < ' _ > ) -> core:: fmt:: Result {
739+ write ! ( f, "{}" , self . core)
740+ }
741+ }
742+
713743#[ cfg( test) ]
714744mod test {
715745
@@ -743,15 +773,15 @@ mod test {
743773 }
744774
745775 #[ test]
746- fn test_build_with_mrkel ( ) {
776+ fn test_build_with_mrkle ( ) {
747777 let hasher = MrkleHasher :: < sha1:: Sha1 > :: new ( ) ;
748778 let node = MrkleNode :: < _ , sha1:: Sha1 , usize > :: leaf_with_hasher ( DATA_PAYLOAD , & hasher) ;
749779
750780 assert_eq ! ( node. hash, sha1:: Sha1 :: digest( DATA_PAYLOAD ) )
751781 }
752782
753783 #[ test]
754- fn test_build_internal_mrkel_node ( ) {
784+ fn test_build_internal_mrkle_node ( ) {
755785 let hasher = MrkleHasher :: < sha1:: Sha1 > :: new ( ) ;
756786 let node1 = MrkleNode :: < _ , sha1:: Sha1 , usize > :: leaf_with_hasher ( DATA_PAYLOAD , & hasher) ;
757787 let node2 = MrkleNode :: < _ , sha1:: Sha1 , usize > :: leaf_with_hasher ( DATA_PAYLOAD , & hasher) ;
@@ -794,6 +824,7 @@ mod test {
794824 fn test_building_binary_tree_base_case ( ) {
795825 let leaves: Vec < & str > = vec ! [ "A" ] ;
796826 let tree = MrkleTree :: < & str , sha1:: Sha1 > :: from_leaves ( leaves) ;
827+ assert ! ( tree. len( ) == 2 )
797828 }
798829
799830 #[ test]
@@ -811,4 +842,12 @@ mod test {
811842 }
812843 }
813844 }
845+
846+ #[ test]
847+ #[ cfg( feature = "std" ) ]
848+ fn test_building_binary_tree_display ( ) {
849+ let leaves: Vec < & str > = vec ! [ "A" , "B" , "C" , "D" , "E" ] ;
850+ let tree = MrkleTree :: < & str , sha1:: Sha1 > :: from_leaves ( leaves. clone ( ) ) ;
851+ println ! ( "{tree}" ) ;
852+ }
814853}
0 commit comments