Skip to content

Commit

Permalink
refactor: everything in hash_chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Bostoen committed Sep 6, 2024
1 parent 2f5fbd5 commit f8f2e2c
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions ssz-rs/src/merkleization/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@ static INIT: Once = Once::new();

#[inline]
#[cfg(feature = "hashtree")]
pub fn hash_chunks_hashtree(
left: impl AsRef<[u8]>,
right: impl AsRef<[u8]>,
) -> [u8; BYTES_PER_CHUNK] {
fn hash_chunks_hashtree(left: impl AsRef<[u8]>, right: impl AsRef<[u8]>) -> [u8; BYTES_PER_CHUNK] {
// Initialize the hashtree library (once)
INIT.call_once(|| {
hashtree::init();
});

debug_assert!(left.as_ref().len() == BYTES_PER_CHUNK);
debug_assert!(right.as_ref().len() == BYTES_PER_CHUNK);

let mut out = [0u8; BYTES_PER_CHUNK];

let mut chunks = [0u8; 2 * BYTES_PER_CHUNK];
Expand All @@ -39,13 +33,7 @@ pub fn hash_chunks_hashtree(

#[inline]
#[cfg(not(feature = "hashtree"))]
pub fn hash_chunks_sha256(
left: impl AsRef<[u8]>,
right: impl AsRef<[u8]>,
) -> [u8; BYTES_PER_CHUNK] {
debug_assert!(left.as_ref().len() == BYTES_PER_CHUNK);
debug_assert!(right.as_ref().len() == BYTES_PER_CHUNK);

fn hash_chunks_sha256(left: impl AsRef<[u8]>, right: impl AsRef<[u8]>) -> [u8; BYTES_PER_CHUNK] {
let mut hasher = Sha256::new();
hasher.update(left.as_ref());
hasher.update(right.as_ref());
Expand All @@ -59,6 +47,9 @@ pub fn hash_chunks_sha256(
/// - hashtree (with the "hashtree" feature flag)
#[inline]
pub fn hash_chunks(left: impl AsRef<[u8]>, right: impl AsRef<[u8]>) -> [u8; BYTES_PER_CHUNK] {
debug_assert!(left.as_ref().len() == BYTES_PER_CHUNK);
debug_assert!(right.as_ref().len() == BYTES_PER_CHUNK);

#[cfg(feature = "hashtree")]
return hash_chunks_hashtree(left, right);

Expand Down

0 comments on commit f8f2e2c

Please sign in to comment.