Skip to content

Commit

Permalink
Remove the chunking and rename compute to finalize
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Apr 29, 2024
1 parent 0f5f921 commit 4115d12
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion benches/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@ fn context(size: usize, bencher: &mut test::Bencher) {
bencher.iter(|| {
let mut context = Context::new();
context.consume(&data);
test::black_box(context.compute());
test::black_box(context.finalize());
});
}
56 changes: 30 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,15 @@ impl Context {
}

/// Consume data.
#[cfg(target_pointer_width = "32")]
#[inline]
pub fn consume<T: AsRef<[u8]>>(&mut self, data: T) {
consume(self, data.as_ref());
}

/// Consume data.
#[cfg(target_pointer_width = "64")]
pub fn consume<T: AsRef<[u8]>>(&mut self, data: T) {
for chunk in data.as_ref().chunks(core::u32::MAX as usize) {
consume(self, chunk);
}
}

/// Finalize and return the digest.
#[rustfmt::skip]
#[allow(clippy::double_parens, clippy::needless_range_loop)]
pub fn compute(mut self) -> Digest {
pub fn finalize(mut self) -> Digest {
let mut input = [0u32; 16];
let k = ((self.count[0] >> 3) & 0x3f) as usize;
input[14] = self.count[0];
Expand Down Expand Up @@ -163,6 +154,13 @@ impl Context {
}
Digest(digest)
}

/// Finalize and return the digest.
#[deprecated(since = "0.8.0", note = "Use `finalize`.")]
#[inline]
pub fn compute(self) -> Digest {
self.finalize()
}
}

impl Default for Context {
Expand All @@ -175,7 +173,7 @@ impl Default for Context {
impl convert::From<Context> for Digest {
#[inline]
fn from(context: Context) -> Digest {
context.compute()
context.finalize()
}
}

Expand All @@ -198,7 +196,7 @@ impl io::Write for Context {
pub fn compute<T: AsRef<[u8]>>(data: T) -> Digest {
let mut context = Context::new();
context.consume(data);
context.compute()
context.finalize()
}

#[rustfmt::skip]
Expand Down Expand Up @@ -382,6 +380,10 @@ fn transform(state: &mut [u32; 4], input: &[u32; 16]) {

#[cfg(test)]
mod tests {
use std::io::prelude::Write;

use super::Context;

#[test]
fn compute() {
let inputs = [
Expand All @@ -405,7 +407,13 @@ mod tests {
"57edf4a22be3c955ac49da2e2107b67a",
];
for (input, &output) in inputs.iter().zip(outputs.iter()) {
assert_eq!(format!("{:x}", super::compute(input)), output);
let digest = super::compute(input);
assert_eq!(format!("{digest:x}"), output);

let mut context = Context::new();
context.consume(input);
let digest = context.finalize();
assert_eq!(format!("{digest:x}"), output);
}
}

Expand All @@ -418,30 +426,26 @@ mod tests {
}

#[test]
fn overflow_count() {
use std::io::prelude::Write;
fn write_29() {
let data = vec![0; 8 * 1024 * 1024];
let mut context = super::Context::new();
let mut context = Context::new();
for _ in 0..64 {
context.write(&data).unwrap();
}
assert_eq!(
format!("{:x}", context.compute()),
"aa559b4e3523a6c931f08f4df52d58f2"
format!("{:x}", context.finalize()),
"aa559b4e3523a6c931f08f4df52d58f2",
);
}

#[test]
#[cfg(target_pointer_width = "64")]
fn overflow_length() {
use std::io::prelude::Write;
use std::u32::MAX;
let data = vec![0; MAX as usize + 1];
let mut context = super::Context::new();
fn write_32() {
let data = vec![0; std::u32::MAX as usize + 1];
let mut context = Context::new();
context.write(&data).unwrap();
assert_eq!(
format!("{:x}", context.compute()),
"c9a5a6878d97b48cc965c1e41859f034"
format!("{:x}", context.finalize()),
"c9a5a6878d97b48cc965c1e41859f034",
);
}
}

0 comments on commit 4115d12

Please sign in to comment.