Skip to content

Commit

Permalink
remove per-hash digest type
Browse files Browse the repository at this point in the history
Remove the per-hasher digest type. Instead, store hash digests inside
the hashers and "borrow" it. In all cases, we're going to copy it into a
`Multihash<S>` anyways.

This:

1. Removes bunch of code.
2. Means that hashers don't need to be generic over the size (unless
they actually support multiple sizes). This fixes the UX issue
introduced in the const generics PR.
3. Avoids some copying.

BREAKING CHANGE

1. `Hasher.digest` no longer exists. Users should use
`Code::SomeCode.digest` where possible.
2. The hasher digests no longer exist.
  • Loading branch information
Stebalien authored and vmx committed Jan 27, 2022
1 parent 22e44c8 commit 423bee0
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 685 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ use multihash::MultihashCode;
#[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)]
#[mh(alloc_size = 64)]
pub enum Code {
#[mh(code = 0x01, hasher = multihash::Sha2_256, digest = multihash::Sha2Digest<32>)]
#[mh(code = 0x01, hasher = multihash::Sha2_256)]
Foo,
#[mh(code = 0x02, hasher = multihash::Sha2_512, digest = multihash::Sha2Digest<64>)]
#[mh(code = 0x02, hasher = multihash::Sha2_512)]
Bar,
}

Expand Down
21 changes: 11 additions & 10 deletions benches/multihash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rand::Rng;
use multihash::{
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3_256, Hasher, Keccak224, Keccak256,
Keccak384, Keccak512, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512,
StatefulHasher, Strobe256, Strobe512,
Strobe256, Strobe512,
};

macro_rules! group_digest {
Expand All @@ -13,7 +13,9 @@ macro_rules! group_digest {
$(
group.bench_function($id, |b| {
b.iter(|| {
let _ = black_box($hash::digest($input));
let mut hasher = $hash::default();
hasher.update(black_box($input));
let _ = black_box(hasher.finalize());
})
});
)*
Expand All @@ -27,14 +29,13 @@ macro_rules! group_stream {
$(
group.bench_function($id, |b| {
b.iter(|| {
let _ = black_box({
let mut hasher = <$hash>::default();
for i in 0..3 {
let start = i * 256;
hasher.update(&$input[start..(start + 256)]);
}
hasher.finalize()
});
let input = black_box($input);
let mut hasher = <$hash>::default();
for i in 0..3 {
let start = i * 256;
hasher.update(&input[start..(start + 256)]);
}
let _ = black_box(hasher.finalize());
})
});
)*
Expand Down
4 changes: 2 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
//! #[derive(Clone, Copy, Debug, Eq, Multihash, PartialEq)]
//! #[mh(alloc_size = 64)]
//! pub enum Code {
//! #[mh(code = 0x01, hasher = multihash::Sha2_256, digest = multihash::Sha2Digest<32>)]
//! #[mh(code = 0x01, hasher = multihash::Sha2_256)]
//! Foo,
//! #[mh(code = 0x02, hasher = multihash::Sha2_512, digest = multihash::Sha2Digest<64>)]
//! #[mh(code = 0x02, hasher = multihash::Sha2_512)]
//! Bar,
//! }
//!
Expand Down
Loading

0 comments on commit 423bee0

Please sign in to comment.