aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJack O'Connor <[email protected]>2019-12-24 16:41:16 -0600
committerJack O'Connor <[email protected]>2019-12-24 16:51:43 -0600
commitba2806496360aa61cc2aa64204a39d923cb13895 (patch)
tree4b9079ac7764ca1ec8f40a006674efe5932ce2e2
parentc9ffbdd36532b89a1163a33071a5569bcef02ceb (diff)
switch back to counting trailing 0 bits
These things are totally equivalent, and I keep going back and forth, but now I think this is slightly clearer.
-rw-r--r--reference_impl/reference_impl.rs19
1 files changed, 9 insertions, 10 deletions
diff --git a/reference_impl/reference_impl.rs b/reference_impl/reference_impl.rs
index 3be4e5a..bb8b511 100644
--- a/reference_impl/reference_impl.rs
+++ b/reference_impl/reference_impl.rs
@@ -289,18 +289,17 @@ impl Hasher {
self.cv_stack[self.cv_stack_len as usize]
}
- fn add_chunk_chaining_value(&mut self, mut new_cv: [u32; 8], mut previous_total: u64) {
+ fn add_chunk_chaining_value(&mut self, mut new_cv: [u32; 8], mut total_chunks: u64) {
// This chunk might complete some subtrees. For each completed subtree,
// its left child will be the current top entry in the CV stack, and
// its right child will be the current value of `new_cv`. Pop each left
// child off the stack, merge it with `new_cv`, and overwrite `new_cv`
- // with the result. After all these merges, push the resulting value of
- // `new_cv` onto the CV stack. The number of merges we need to do is
- // the same as the number of trailing 1 bits in the previous total
- // number of chunks.
- while previous_total & 1 == 1 {
+ // with the result. After all these merges, push the final value of
+ // `new_cv` onto the stack. The number of completed subtrees is given
+ // by the number of trailing 0 bits in the new total number of chunks.
+ while total_chunks & 1 == 0 {
new_cv = parent_cv(self.pop_stack(), new_cv, self.key, self.flags);
- previous_total >>= 1;
+ total_chunks >>= 1;
}
self.push_stack(new_cv);
}
@@ -312,9 +311,9 @@ impl Hasher {
// chunk state. More input is coming, so this chunk is not ROOT.
if self.chunk_state.len() == CHUNK_LEN {
let chunk_cv = self.chunk_state.output().chaining_value();
- let counter = self.chunk_state.chunk_counter;
- self.add_chunk_chaining_value(chunk_cv, counter);
- self.chunk_state = ChunkState::new(self.key, counter + 1, self.flags);
+ let total_chunks = self.chunk_state.chunk_counter + 1;
+ self.add_chunk_chaining_value(chunk_cv, total_chunks);
+ self.chunk_state = ChunkState::new(self.key, total_chunks, self.flags);
}
// Compress input bytes into the current chunk state.