diff --git a/Cargo.toml b/Cargo.toml index c4cc3ca3..e586e414 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } @@ -30,6 +30,7 @@ rand = "0.7.3" [features] test = ["quickcheck", "rand"] +use_blake3 = ["blake3"] [[bench]] name = "multihash" diff --git a/benches/multihash.rs b/benches/multihash.rs index acec7fb7..2ac575c7 100644 --- a/benches/multihash.rs +++ b/benches/multihash.rs @@ -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)* ) => {{ @@ -45,6 +51,7 @@ macro_rules! group_stream { fn bench_digest(c: &mut Criterion) { let mut rng = rand::thread_rng(); let data: Vec = (0..1024).map(|_| rng.gen()).collect(); + #[cfg(feature = "use_blake3")] group_digest!(c, "identity" => Identity, &data "sha1" => Sha1, &data @@ -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 = (0..1024).map(|_| rng.gen()).collect(); + #[cfg(feature = "use_blake3")] group_stream!(c, "identity" => Identity, &data "sha1" => Sha1, &data @@ -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); diff --git a/src/arb.rs b/src/arb.rs index 8b92ebda..b19c7873 100644 --- a/src/arb.rs +++ b/src/arb.rs @@ -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. diff --git a/src/hashes.rs b/src/hashes.rs index b0e74396..8945d986 100644 --- a/src/hashes.rs +++ b/src/hashes.rs @@ -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, @@ -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; diff --git a/tests/lib.rs b/tests/lib.rs index b490bc76..6c43bdee 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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"; } } @@ -81,6 +85,9 @@ fn assert_decode() { Blake2s256, "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b"; Blake2b256, "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610"; Blake2s128, "d0e4021037deae0226c30da2ab424a7b8ee14e83"; + } + #[cfg(feature = "use_blake3")] + assert_decode! { Blake3, "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24"; } } @@ -217,6 +224,7 @@ fn multihash_methods() { "d0e40210", "37deae0226c30da2ab424a7b8ee14e83", ); + #[cfg(feature = "use_blake3")] test_methods( Blake3::default(), "1e20",