Skip to content

Commit 42cba30

Browse files
author
Mark Riise
authored
fix: optional blake3 (#80)
Hide blake3 behind optional build feature.
1 parent eebdb2c commit 42cba30

File tree

5 files changed

+99
-2
lines changed

5 files changed

+99
-2
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ edition = "2018"
1313
[dependencies]
1414
blake2b_simd = { version = "0.5.9", default-features = false }
1515
blake2s_simd = { version = "0.5.9", default-features = false }
16-
blake3 = { version = "0.3.5", default-features = false }
16+
blake3 = { version = "0.3.5", default-features = false, optional = true }
1717
digest = { version = "0.9", default-features = false }
1818
sha-1 = { version = "0.9", default-features = false }
1919
sha2 = { version = "0.9", default-features = false }
@@ -30,6 +30,7 @@ rand = "0.7.3"
3030

3131
[features]
3232
test = ["quickcheck", "rand"]
33+
use_blake3 = ["blake3"]
3334

3435
[[bench]]
3536
name = "multihash"

benches/multihash.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use rand::Rng;
33

4+
#[cfg(feature = "use_blake3")]
45
use multihash::{
56
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3, Identity, Keccak224, Keccak256,
67
Keccak384, Keccak512, MultihashDigest, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384,
78
Sha3_512,
89
};
10+
#[cfg(not(feature = "use_blake3"))]
11+
use multihash::{
12+
Blake2b256, Blake2b512, Blake2s128, Blake2s256, Identity, Keccak224, Keccak256, Keccak384,
13+
Keccak512, MultihashDigest, Sha1, Sha2_256, Sha2_512, Sha3_224, Sha3_256, Sha3_384, Sha3_512,
14+
};
915

1016
macro_rules! group_digest {
1117
($criterion:ident, $( $id:expr => $hash:ident, $input:expr)* ) => {{
@@ -45,6 +51,7 @@ macro_rules! group_stream {
4551
fn bench_digest(c: &mut Criterion) {
4652
let mut rng = rand::thread_rng();
4753
let data: Vec<u8> = (0..1024).map(|_| rng.gen()).collect();
54+
#[cfg(feature = "use_blake3")]
4855
group_digest!(c,
4956
"identity" => Identity, &data
5057
"sha1" => Sha1, &data
@@ -64,12 +71,32 @@ fn bench_digest(c: &mut Criterion) {
6471
"blake2s_256" => Blake2s256, &data
6572
"blake3" => Blake3, &data
6673
);
74+
#[cfg(not(feature = "use_blake3"))]
75+
group_digest!(c,
76+
"identity" => Identity, &data
77+
"sha1" => Sha1, &data
78+
"sha2_256" => Sha2_256, &data
79+
"sha2_512" => Sha2_512, &data
80+
"sha3_224" => Sha3_224, &data
81+
"sha3_256" => Sha3_256, &data
82+
"sha3_384" => Sha3_384, &data
83+
"sha3_512" => Sha3_512, &data
84+
"keccak_224" => Keccak224, &data
85+
"keccak_256" => Keccak256, &data
86+
"keccak_384" => Keccak384, &data
87+
"keccak_512" => Keccak512, &data
88+
"blake2b_256" => Blake2b256, &data
89+
"blake2b_512" => Blake2b512, &data
90+
"blake2s_128" => Blake2s128, &data
91+
"blake2s_256" => Blake2s256, &data
92+
);
6793
}
6894

6995
/// Chunks the data into 256-byte slices.
7096
fn bench_stream(c: &mut Criterion) {
7197
let mut rng = rand::thread_rng();
7298
let data: Vec<u8> = (0..1024).map(|_| rng.gen()).collect();
99+
#[cfg(feature = "use_blake3")]
73100
group_stream!(c,
74101
"identity" => Identity, &data
75102
"sha1" => Sha1, &data
@@ -89,6 +116,25 @@ fn bench_stream(c: &mut Criterion) {
89116
"blake2s_256" => Blake2s256, &data
90117
"blake3" => Blake3, &data
91118
);
119+
#[cfg(not(feature = "use_blake3"))]
120+
group_stream!(c,
121+
"identity" => Identity, &data
122+
"sha1" => Sha1, &data
123+
"sha2_256" => Sha2_256, &data
124+
"sha2_512" => Sha2_512, &data
125+
"sha3_224" => Sha3_224, &data
126+
"sha3_256" => Sha3_256, &data
127+
"sha3_384" => Sha3_384, &data
128+
"sha3_512" => Sha3_512, &data
129+
"keccak_224" => Keccak224, &data
130+
"keccak_256" => Keccak256, &data
131+
"keccak_384" => Keccak384, &data
132+
"keccak_512" => Keccak512, &data
133+
"blake2b_256" => Blake2b256, &data
134+
"blake2b_512" => Blake2b512, &data
135+
"blake2s_128" => Blake2s128, &data
136+
"blake2s_256" => Blake2s256, &data
137+
);
92138
}
93139

94140
criterion_group!(benches, bench_digest, bench_stream);

src/arb.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ use rand::seq::SliceRandom;
33

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

6+
#[cfg(not(feature = "use_blake3"))]
7+
const HASHES: [Code; 16] = [
8+
Identity, Sha1, Sha2_256, Sha2_512, Sha3_512, Sha3_384, Sha3_256, Sha3_224, Keccak224,
9+
Keccak256, Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256,
10+
];
11+
#[cfg(feature = "use_blake3")]
612
const HASHES: [Code; 17] = [
713
Identity, Sha1, Sha2_256, Sha2_512, Sha3_512, Sha3_384, Sha3_256, Sha3_224, Keccak224,
814
Keccak256, Keccak384, Keccak512, Blake2b256, Blake2b512, Blake2s128, Blake2s256, Blake3,
915
];
10-
1116
/// Generates a random hash algorithm.
1217
///
1318
/// The more exotic ones will be generated just as frequently as the common ones.

src/hashes.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,42 @@ macro_rules! derive_digest {
275275
}
276276
}
277277

278+
#[cfg(not(feature = "use_blake3"))]
279+
impl_code! {
280+
/// Identity (Raw binary)
281+
Identity => 0x00,
282+
/// SHA-1 (20-byte hash size)
283+
Sha1 => 0x11,
284+
/// SHA-256 (32-byte hash size)
285+
Sha2_256 => 0x12,
286+
/// SHA-512 (64-byte hash size)
287+
Sha2_512 => 0x13,
288+
/// SHA3-224 (28-byte hash size)
289+
Sha3_224 => 0x17,
290+
/// SHA3-256 (32-byte hash size)
291+
Sha3_256 => 0x16,
292+
/// SHA3-384 (48-byte hash size)
293+
Sha3_384 => 0x15,
294+
/// SHA3-512 (64-byte hash size)
295+
Sha3_512 => 0x14,
296+
/// Keccak-224 (28-byte hash size)
297+
Keccak224 => 0x1a,
298+
/// Keccak-256 (32-byte hash size)
299+
Keccak256 => 0x1b,
300+
/// Keccak-384 (48-byte hash size)
301+
Keccak384 => 0x1c,
302+
/// Keccak-512 (64-byte hash size)
303+
Keccak512 => 0x1d,
304+
/// BLAKE2b-256 (32-byte hash size)
305+
Blake2b256 => 0xb220,
306+
/// BLAKE2b-512 (64-byte hash size)
307+
Blake2b512 => 0xb240,
308+
/// BLAKE2s-128 (16-byte hash size)
309+
Blake2s128 => 0xb250,
310+
/// BLAKE2s-256 (32-byte hash size)
311+
Blake2s256 => 0xb260,
312+
}
313+
#[cfg(feature = "use_blake3")]
278314
impl_code! {
279315
/// Identity (Raw binary)
280316
Identity => 0x00,
@@ -425,6 +461,7 @@ derive_digest! {
425461
@blake Blake2s | Blake2sParams as Blake2s256 32;
426462
@code_doc "The code of the Blake2-256 hasher, 0xb260.",
427463
}
464+
#[cfg(feature = "use_blake3")]
428465
derive_digest! {
429466
/// The Blake3 hasher.
430467
@blake3 blake3::Hasher as Blake3;

tests/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ fn multihash_encode() {
4444
Blake2s256, b"hello world", "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b";
4545
Blake2b256, b"hello world", "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610";
4646
Blake2s128, b"hello world", "d0e4021037deae0226c30da2ab424a7b8ee14e83";
47+
}
48+
49+
#[cfg(feature = "use_blake3")]
50+
assert_encode! {
4751
Blake3, b"hello world", "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24";
4852
}
4953
}
@@ -81,6 +85,9 @@ fn assert_decode() {
8185
Blake2s256, "e0e402209aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b";
8286
Blake2b256, "a0e40220256c83b297114d201b30179f3f0ef0cace9783622da5974326b436178aeef610";
8387
Blake2s128, "d0e4021037deae0226c30da2ab424a7b8ee14e83";
88+
}
89+
#[cfg(feature = "use_blake3")]
90+
assert_decode! {
8491
Blake3, "1e20d74981efa70a0c880b8d8c1985d075dbcbf679b99a5f9914e5aaf96b831a9e24";
8592
}
8693
}
@@ -217,6 +224,7 @@ fn multihash_methods() {
217224
"d0e40210",
218225
"37deae0226c30da2ab424a7b8ee14e83",
219226
);
227+
#[cfg(feature = "use_blake3")]
220228
test_methods(
221229
Blake3::default(),
222230
"1e20",

0 commit comments

Comments
 (0)