Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to efficiently hash entire non-root subtrees to guts #329

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/guts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
pub const BLOCK_LEN: usize = 64;
pub const CHUNK_LEN: usize = 1024;

pub fn hash_block(start_chunk: u64, data: &[u8], is_root: bool) -> crate::Hash {
let mut hasher = crate::Hasher::new_with_start_chunk(start_chunk);
hasher.update(data);
hasher.finalize_node(is_root)
}

#[derive(Clone, Debug)]
pub struct ChunkState(crate::ChunkState);

Expand Down Expand Up @@ -98,4 +104,12 @@ mod test {
let root = parent_cv(&parent, &chunk2_cv, true);
assert_eq!(hasher.finalize(), root);
}

#[test]
fn test_hash_block() {
assert_eq!(
crate::hash(b"foo"),
hash_block(0, b"foo", true)
);
}
}
38 changes: 32 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,18 @@ impl Output {
Hash(platform::le_bytes_from_words_32(&cv))
}

fn hash(&self, is_root: bool) -> Hash {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just exposes a way to create non root hashes

// debug_assert_eq!(self.counter, 0);
let mut cv = self.input_chaining_value;
let mut flags = self.flags;
if is_root {
flags |= ROOT;
}
self.platform
.compress_in_place(&mut cv, &self.block, self.block_len, 0, flags);
Hash(platform::le_bytes_from_words_32(&cv))
}

fn root_output_block(&self) -> [u8; 2 * OUT_LEN] {
self.platform.compress_xof(
&self.input_chaining_value,
Expand Down Expand Up @@ -977,6 +989,15 @@ impl Hasher {
Self::new_internal(IV, 0)
}

/// Construct a new `Hasher` with a start chunk
fn new_with_start_chunk(start_chunk: u64) -> Self {
Self {
key: *IV,
chunk_state: ChunkState::new(IV, start_chunk, 0, Platform::detect()),
cv_stack: ArrayVec::new(),
}
}

/// Construct a new `Hasher` for the keyed hash function. See
/// [`keyed_hash`].
///
Expand Down Expand Up @@ -1246,7 +1267,7 @@ impl Hasher {
// also. Convert it directly into an Output. Otherwise, we need to
// merge subtrees below.
if self.cv_stack.is_empty() {
debug_assert_eq!(self.chunk_state.chunk_counter, 0);
// debug_assert_eq!(self.chunk_state.chunk_counter, 0);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be disabled because the chunk counter can be set to a non zero value in the constructor now.

return self.chunk_state.output();
}

Expand All @@ -1265,11 +1286,11 @@ impl Hasher {
let mut output: Output;
let mut num_cvs_remaining = self.cv_stack.len();
if self.chunk_state.len() > 0 {
debug_assert_eq!(
self.cv_stack.len(),
self.chunk_state.chunk_counter.count_ones() as usize,
"cv stack does not need a merge"
);
// debug_assert_eq!(
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must be disabled since because the chunk counter does not necessarily start at 0, it's current value is no longer equivalent to the number of chunks.

I don't think there is a way to reenable this except by adding an additional value to the state that tracks the start chunk.

// self.cv_stack.len(),
// self.chunk_state.chunk_counter.count_ones() as usize,
// "cv stack does not need a merge"
// );
output = self.chunk_state.output();
} else {
debug_assert!(self.cv_stack.len() >= 2);
Expand Down Expand Up @@ -1304,6 +1325,11 @@ impl Hasher {
self.final_output().root_hash()
}

fn finalize_node(&self, is_root: bool) -> Hash {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just exposes a way to create non root hashes

let output = self.final_output();
output.hash(is_root)
}

/// Finalize the hash state and return an [`OutputReader`], which can
/// supply any number of output bytes.
///
Expand Down