11pub use blake2_rfc:: blake2b:: Blake2bResult ;
22
3+ use crate :: storage:: Node ;
34use blake2_rfc:: blake2b:: Blake2b ;
45use byteorder:: { BigEndian , WriteBytesExt } ;
56// use ed25519_dalek::PublicKey;
6- use crate :: storage:: Node ;
77use merkle_tree_stream:: Node as NodeTrait ;
88use std:: convert:: AsRef ;
9+ use std:: fmt;
910use std:: mem;
1011use std:: ops:: { Deref , DerefMut } ;
1112
@@ -14,53 +15,57 @@ const LEAF_TYPE: [u8; 1] = [0x00];
1415const PARENT_TYPE : [ u8 ; 1 ] = [ 0x01 ] ;
1516const ROOT_TYPE : [ u8 ; 1 ] = [ 0x02 ] ;
1617//const HYPERCORE: [u8; 9] = *b"hypercore";
18+ const BLAKE_2_HASH_SIZE : usize = 32 ;
1719
20+ type StoredHash = [ u8 ; BLAKE_2_HASH_SIZE ] ;
1821/// `BLAKE2b` hash.
19- #[ derive( Debug , Clone , PartialEq ) ]
22+ /// uses [blake2_rfc::blake2b::Blake2bResult] to hash its inputs on initalisation, the calculated
23+ /// hash is then constant and can not be changed.
24+ #[ derive( Debug , Clone , PartialEq , Eq ) ]
2025pub struct Hash {
21- hash : Blake2bResult ,
26+ hash : StoredHash ,
2227}
2328
2429impl Hash {
2530 /// Hash a `Leaf` node.
2631 pub fn from_leaf ( data : & [ u8 ] ) -> Self {
2732 let size = u64_as_be ( data. len ( ) as u64 ) ;
2833
29- let mut hasher = Blake2b :: new ( 32 ) ;
34+ let mut hasher = Blake2b :: new ( BLAKE_2_HASH_SIZE ) ;
3035 hasher. update ( & LEAF_TYPE ) ;
3136 hasher. update ( & size) ;
3237 hasher. update ( data) ;
3338
3439 Self {
35- hash : hasher . finalize ( ) ,
40+ hash : hasher_to_stored_hash ( hasher ) ,
3641 }
3742 }
3843
3944 /// Hash two `Leaf` nodes hashes together to form a `Parent` hash.
4045 pub fn from_hashes ( left : & Node , right : & Node ) -> Self {
41- let ( node1, node2) = if left. index <= right. index {
46+ let ( node1, node2) = if left <= right {
4247 ( left, right)
4348 } else {
4449 ( right, left)
4550 } ;
4651
4752 let size = u64_as_be ( ( node1. length + node2. length ) as u64 ) ;
4853
49- let mut hasher = Blake2b :: new ( 32 ) ;
54+ let mut hasher = Blake2b :: new ( BLAKE_2_HASH_SIZE ) ;
5055 hasher. update ( & PARENT_TYPE ) ;
5156 hasher. update ( & size) ;
5257 hasher. update ( node1. hash ( ) ) ;
5358 hasher. update ( node2. hash ( ) ) ;
5459
5560 Self {
56- hash : hasher . finalize ( ) ,
61+ hash : hasher_to_stored_hash ( hasher ) ,
5762 }
5863 }
5964
6065 // /// Hash a public key. Useful to find the key you're looking for on a public
6166 // /// network without leaking the key itself.
6267 // pub fn from_key(public_key: PublicKey) -> Self {
63- // let mut hasher = Blake2b::new(32 );
68+ // let mut hasher = Blake2b::new(BLAKE_2_HASH_SIZE );
6469 // hasher.update(*HYPERCORE);
6570 // hasher.update(public_key.as_bytes());
6671 // Self {
@@ -71,7 +76,7 @@ impl Hash {
7176 /// Hash a vector of `Root` nodes.
7277 // Called `crypto.tree()` in the JS implementation.
7378 pub fn from_roots ( roots : & [ impl AsRef < Node > ] ) -> Self {
74- let mut hasher = Blake2b :: new ( 32 ) ;
79+ let mut hasher = Blake2b :: new ( BLAKE_2_HASH_SIZE ) ;
7580 hasher. update ( & ROOT_TYPE ) ;
7681
7782 for node in roots {
@@ -82,14 +87,37 @@ impl Hash {
8287 }
8388
8489 Self {
85- hash : hasher. finalize ( ) ,
90+ hash : hasher_to_stored_hash ( hasher) ,
91+ }
92+ }
93+
94+ pub fn from_bytes ( bytes : & [ u8 ] ) -> Self {
95+ Self {
96+ hash : slice_to_stored_hash ( bytes) ,
8697 }
8798 }
8899
89100 /// Returns a byte slice of this `Hash`'s contents.
90101 pub fn as_bytes ( & self ) -> & [ u8 ] {
91- self . hash . as_bytes ( )
102+ & self . hash [ ..]
103+ }
104+ }
105+
106+ fn slice_to_stored_hash ( slice : & [ u8 ] ) -> StoredHash {
107+ assert ! ( slice. len( ) == BLAKE_2_HASH_SIZE ) ;
108+
109+ let mut stored_hash: StoredHash = [ 0 ; BLAKE_2_HASH_SIZE ] ;
110+ let mut i = 0 ;
111+ for byte in slice. iter ( ) {
112+ stored_hash[ i] = * byte;
113+ i = i + 1 ;
92114 }
115+
116+ stored_hash
117+ }
118+
119+ fn hasher_to_stored_hash ( hasher : Blake2b ) -> StoredHash {
120+ slice_to_stored_hash ( hasher. finalize ( ) . as_bytes ( ) )
93121}
94122
95123fn u64_as_be ( n : u64 ) -> [ u8 ; 8 ] {
@@ -99,7 +127,7 @@ fn u64_as_be(n: u64) -> [u8; 8] {
99127}
100128
101129impl Deref for Hash {
102- type Target = Blake2bResult ;
130+ type Target = StoredHash ;
103131
104132 fn deref ( & self ) -> & Self :: Target {
105133 & self . hash
@@ -112,6 +140,12 @@ impl DerefMut for Hash {
112140 }
113141}
114142
143+ impl fmt:: Display for Hash {
144+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
145+ write ! ( f, "{:x?}" , self . hash)
146+ }
147+ }
148+
115149#[ cfg( test) ]
116150mod tests {
117151 use super :: * ;
@@ -143,8 +177,8 @@ mod tests {
143177 fn parent_hash ( ) {
144178 let d1: & [ u8 ] = & [ 0 , 1 , 2 , 3 , 4 ] ;
145179 let d2: & [ u8 ] = & [ 42 , 43 , 44 , 45 , 46 , 47 , 48 ] ;
146- let node1 = Node :: new ( 0 , Hash :: from_leaf ( d1) . as_bytes ( ) . to_vec ( ) , d1. len ( ) ) ;
147- let node2 = Node :: new ( 1 , Hash :: from_leaf ( d2) . as_bytes ( ) . to_vec ( ) , d2. len ( ) ) ;
180+ let node1 = Node :: new ( 0 , Hash :: from_leaf ( d1) , d1. len ( ) ) ;
181+ let node2 = Node :: new ( 1 , Hash :: from_leaf ( d2) , d2. len ( ) ) ;
148182 check_hash (
149183 Hash :: from_hashes ( & node1, & node2) ,
150184 "6fac58578fa385f25a54c0637adaca71fdfddcea885d561f33d80c4487149a14" ,
@@ -159,8 +193,8 @@ mod tests {
159193 fn root_hash ( ) {
160194 let d1: & [ u8 ] = & [ 0 , 1 , 2 , 3 , 4 ] ;
161195 let d2: & [ u8 ] = & [ 42 , 43 , 44 , 45 , 46 , 47 , 48 ] ;
162- let node1 = Node :: new ( 0 , Hash :: from_leaf ( d1) . as_bytes ( ) . to_vec ( ) , d1. len ( ) ) ;
163- let node2 = Node :: new ( 1 , Hash :: from_leaf ( d2) . as_bytes ( ) . to_vec ( ) , d2. len ( ) ) ;
196+ let node1 = Node :: new ( 0 , Hash :: from_leaf ( d1) , d1. len ( ) ) ;
197+ let node2 = Node :: new ( 1 , Hash :: from_leaf ( d2) , d2. len ( ) ) ;
164198 check_hash (
165199 Hash :: from_roots ( & [ & node1, & node2] ) ,
166200 "2d117e0bb15c6e5236b6ce764649baed1c41890da901a015341503146cc20bcd" ,
0 commit comments