Skip to content

Commit

Permalink
fix: optional blake3 (#80)
Browse files Browse the repository at this point in the history
Hide blake3 behind optional build feature.
  • Loading branch information
mriise authored Aug 27, 2020
1 parent eebdb2c commit 42cba30
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ edition = "2018"
[dependencies]
blake2b_simd = { version = "0.5.9", default-features = false }
blake2s_simd = { version = "0.5.9", default-features = false }
blake3 = { version = "0.3.5", default-features = false }
blake3 = { version = "0.3.5", default-features = false, optional = true }
digest = { version = "0.9", default-features = false }
sha-1 = { version = "0.9", default-features = false }
sha2 = { version = "0.9", default-features = false }
Expand All @@ -30,6 +30,7 @@ rand = "0.7.3"

[features]
test = ["quickcheck", "rand"]
use_blake3 = ["blake3"]

[[bench]]
name = "multihash"
Expand Down
46 changes: 46 additions & 0 deletions benches/multihash.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use rand::Rng;

#[cfg(feature = "use_blake3")]
use multihash::{
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3, Identity, Keccak224, Keccak256,
Keccak384, Keccak512, MultihashDigest, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384,
Sha3_512,
};
#[cfg(not(feature = "use_blake3"))]
use multihash::{
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Identity, Keccak224, Keccak256, Keccak384,
Keccak512, MultihashDigest, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512,
};

macro_rules! group_digest {
($criterion:ident, $( $id:expr => $hash:ident, $input:expr)* ) => {{
Expand Down Expand Up @@ -45,6 +51,7 @@ macro_rules! group_stream {
fn bench_digest(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..1024).map(|_| rng.gen()).collect();
#[cfg(feature = "use_blake3")]
group_digest!(c,
"identity" => Identity, &data
"sha1" => Sha1, &data
Expand All @@ -64,12 +71,32 @@ fn bench_digest(c: &mut Criterion) {
"blake2s_256" => Blake2s256, &data
"blake3" => Blake3, &data
);
#[cfg(not(feature = "use_blake3"))]
group_digest!(c,
"identity" => Identity, &data
"sha1" => Sha1, &data
"sha2_256" => Sha2_256, &data
"sha2_512" => Sha2_512, &data
"sha3_224" => Sha3_224, &data
"sha3_256" => Sha3_256, &data
"sha3_384" => Sha3_384, &data
"sha3_512" => Sha3_512, &data
"keccak_224" => Keccak224, &data
"keccak_256" => Keccak256, &data
"keccak_384" => Keccak384, &data
"keccak_512" => Keccak512, &data
"blake2b_256" => Blake2b256, &data
"blake2b_512" => Blake2b512, &data
"blake2s_128" => Blake2s128, &data
"blake2s_256" => Blake2s256, &data
);
}

/// Chunks the data into 256-byte slices.
fn bench_stream(c: &mut Criterion) {
let mut rng = rand::thread_rng();
let data: Vec<u8> = (0..1024).map(|_| rng.gen()).collect();
#[cfg(feature = "use_blake3")]
group_stream!(c,
"identity" => Identity, &data
"sha1" => Sha1, &data
Expand All @@ -89,6 +116,25 @@ fn bench_stream(c: &mut Criterion) {
"blake2s_256" => Blake2s256, &data
"blake3" => Blake3, &data
);
#[cfg(not(feature = "use_blake3"))]
group_stream!(c,
"identity" => Identity, &data
"sha1" => Sha1, &data
"sha2_256" => Sha2_256, &data
"sha2_512" => Sha2_512, &data
"sha3_224" => Sha3_224, &data
"sha3_256" => Sha3_256, &data
"sha3_384" => Sha3_384, &data
"sha3_512" => Sha3_512, &data
"keccak_224" => Keccak224, &data
"keccak_256" => Keccak256, &data
"keccak_384" => Keccak384, &data
"keccak_512" => Keccak512, &data
"blake2b_256" => Blake2b256, &data
"blake2b_512" => Blake2b512, &data
"blake2s_128" => Blake2s128, &data
"blake2s_256" => Blake2s256, &data
);
}

criterion_group!(benches, bench_digest, bench_stream);
Expand Down
7 changes: 6 additions & 1 deletion src/arb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@ use rand::seq::SliceRandom;

use crate::{Code, Code::*, Multihash, MultihashDigest};

#[cfg(not(feature = "use_blake3"))]
const HASHES: [Code; 16] = [
Identity, Sha1, Sha2_256, Sha2_512, Sha3_512, Sha3_384, Sha3_256, Sha3_224, Keccak224,
Keccak256, Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256,
];
#[cfg(feature = "use_blake3")]
const HASHES: [Code; 17] = [
Identity, Sha1, Sha2_256, Sha2_512, Sha3_512, Sha3_384, Sha3_256, Sha3_224, Keccak224,
Keccak256, Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3,
];

/// Generates a random hash algorithm.
///
/// The more exotic ones will be generated just as frequently as the common ones.
Expand Down
37 changes: 37 additions & 0 deletions src/hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,42 @@ macro_rules! derive_digest {
}
}

#[cfg(not(feature = "use_blake3"))]
impl_code! {
/// Identity (Raw binary)
Identity => 0x00,
/// SHA-1 (20-byte hash size)
Sha1 => 0x11,
/// SHA-256 (32-byte hash size)
Sha2_256 => 0x12,
/// SHA-512 (64-byte hash size)
Sha2_512 => 0x13,
/// SHA3-224 (28-byte hash size)
Sha3_224 => 0x17,
/// SHA3-256 (32-byte hash size)
Sha3_256 => 0x16,
/// SHA3-384 (48-byte hash size)
Sha3_384 => 0x15,
/// SHA3-512 (64-byte hash size)
Sha3_512 => 0x14,
/// Keccak-224 (28-byte hash size)
Keccak224 => 0x1a,
/// Keccak-256 (32-byte hash size)
Keccak256 => 0x1b,
/// Keccak-384 (48-byte hash size)
Keccak384 => 0x1c,
/// Keccak-512 (64-byte hash size)
Keccak512 => 0x1d,
/// BLAKE2b-256 (32-byte hash size)
Blake2b256 => 0xb220,
/// BLAKE2b-512 (64-byte hash size)
Blake2b512 => 0xb240,
/// BLAKE2s-128 (16-byte hash size)
Blake2s128 => 0xb250,
/// BLAKE2s-256 (32-byte hash size)
Blake2s256 => 0xb260,
}
#[cfg(feature = "use_blake3")]
impl_code! {
/// Identity (Raw binary)
Identity => 0x00,
Expand Down Expand Up @@ -425,6 +461,7 @@ derive_digest! {
@blake Blake2s | Blake2sParams as Blake2s256 32;
@code_doc "The code of the Blake2-256 hasher, 0xb260.",
}
#[cfg(feature = "use_blake3")]
derive_digest! {
/// The Blake3 hasher.
@blake3 blake3::Hasher as Blake3;
Expand Down
8 changes: 8 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ fn multihash_encode() {
Blake2s256, b"hello world", "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b";
Blake2b256, b"hello world", "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610";
Blake2s128, b"hello world", "d0e4021037deae0226c30da2ab424a7b8ee14e83";
}

#[cfg(feature = "use_blake3")]
assert_encode! {
Blake3, b"hello world", "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24";
}
}
Expand Down Expand Up @@ -81,6 +85,9 @@ fn assert_decode() {
Blake2s256, "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b";
Blake2b256, "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610";
Blake2s128, "d0e4021037deae0226c30da2ab424a7b8ee14e83";
}
#[cfg(feature = "use_blake3")]
assert_decode! {
Blake3, "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24";
}
}
Expand Down Expand Up @@ -217,6 +224,7 @@ fn multihash_methods() {
"d0e40210",
"37deae0226c30da2ab424a7b8ee14e83",
);
#[cfg(feature = "use_blake3")]
test_methods(
Blake3::default(),
"1e20",
Expand Down

0 comments on commit 42cba30

Please sign in to comment.