1717
1818pub mod kzg;
1919
20- use crate :: { Blake2b256Hash , BLAKE2B_256_HASH_SIZE } ;
21- use blake2_rfc:: blake2b:: { blake2b, Blake2b } ;
20+ use crate :: Blake2b256Hash ;
21+ use blake2:: digest:: typenum:: U32 ;
22+ use blake2:: digest:: { FixedOutput , Update } ;
23+ use blake2:: { Blake2b , Blake2bMac , Digest } ;
2224
2325/// BLAKE2b-256 hashing of a single value.
2426pub fn blake2b_256_hash ( data : & [ u8 ] ) -> Blake2b256Hash {
25- blake2b_256_hash_with_key ( data, & [ ] )
27+ let mut state = Blake2b :: < U32 > :: new ( ) ;
28+ Update :: update ( & mut state, data) ;
29+ state. finalize_fixed ( ) . into ( )
2630}
2731
2832/// BLAKE2b-256 hashing of a single value truncated to 254 bits.
2933///
3034/// TODO: We probably wouldn't need this eventually
3135pub fn blake2b_256_254_hash ( data : & [ u8 ] ) -> Blake2b256Hash {
32- let mut hash = blake2b_256_hash_with_key ( data, & [ ] ) ;
36+ let mut hash = blake2b_256_hash ( data) ;
3337 // Erase last 2 bits to effectively truncate the hash (number is interpreted as little-endian)
3438 hash[ 31 ] &= 0b00111111 ;
3539 hash
@@ -39,21 +43,20 @@ pub fn blake2b_256_254_hash(data: &[u8]) -> Blake2b256Hash {
3943///
4044/// PANIC: Panics if key is longer than 64 bytes.
4145pub fn blake2b_256_hash_with_key ( data : & [ u8 ] , key : & [ u8 ] ) -> Blake2b256Hash {
42- blake2b ( BLAKE2B_256_HASH_SIZE , key , data )
43- . as_bytes ( )
44- . try_into ( )
45- . expect ( "Initialized with correct length; qed" )
46+ let mut state = Blake2bMac :: < U32 > :: new_with_salt_and_personal ( key , & [ ] , & [ ] )
47+ . expect ( "Only panics when key is over 64 bytes as specified in function description" ) ;
48+ Update :: update ( & mut state , data ) ;
49+ state . finalize_fixed ( ) . into ( )
4650}
4751
4852/// BLAKE2b-256 hashing of a list of values.
4953pub fn blake2b_256_hash_list ( data : & [ & [ u8 ] ] ) -> Blake2b256Hash {
50- let mut state = Blake2b :: new ( BLAKE2B_256_HASH_SIZE ) ;
54+ let mut state = Blake2b :: < U32 > :: new ( ) ;
5155 for d in data {
52- state . update ( d) ;
56+ Update :: update ( & mut state , d) ;
5357 }
5458 state
5559 . finalize ( )
56- . as_bytes ( )
5760 . try_into ( )
5861 . expect ( "Initialized with correct length; qed" )
5962}
0 commit comments