From 3ec1d656d75a9d05ebe43c99dc9264657e1903b4 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 25 Feb 2020 14:35:46 +0800 Subject: [PATCH 1/3] Add benchmark for encoding Signed-off-by: koushiro --- Cargo.toml | 21 ++++++++++----------- benches/multihash.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 benches/multihash.rs diff --git a/Cargo.toml b/Cargo.toml index 4bae39e5..0a33b302 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,21 +1,14 @@ [package] name = "multihash" -description = "Implementation of the multihash format" -repository = "https://github.com/multiformats/rust-multihash" - -keywords = ["multihash", "ipfs"] - version = "0.9.4" - authors = ["dignifiedquire "] - +edition = "2018" license = "MIT" - readme = "README.md" - +description = "Implementation of the multihash format" documentation = "https://docs.rs/multihash/" - -edition = "2018" +repository = "https://github.com/multiformats/rust-multihash" +keywords = ["multihash", "ipfs"] [dependencies] blake2b_simd = { version = "0.5.9", default-features = false } @@ -26,4 +19,10 @@ tiny-keccak = "1.4" unsigned-varint = "0.3" [dev-dependencies] +criterion = "0.3" +rand = "0.7" quickcheck = "0.9.2" + +[[bench]] +name = "multihash" +harness = false diff --git a/benches/multihash.rs b/benches/multihash.rs new file mode 100644 index 00000000..4d886717 --- /dev/null +++ b/benches/multihash.rs @@ -0,0 +1,43 @@ +use criterion::{black_box, criterion_group, criterion_main, Criterion}; +use rand::Rng; + +use multihash::{encode, Hash}; + +macro_rules! group_encode { + ($criterion:ident, $( $id:expr => $hash:expr, $input:expr)* ) => {{ + let mut group = $criterion.benchmark_group("encode"); + $( + group.bench_function($id, |b| { + b.iter(|| { + let _ = black_box(encode($hash, $input).unwrap()); + }) + }); + )* + group.finish(); + }}; +} + +fn bench_encode(c: &mut Criterion) { + let mut rng = rand::thread_rng(); + let data: Vec = (0..1024).map(|_| rng.gen()).collect(); + group_encode!(c, + "identity" => Hash::Identity, &data + "sha1" => Hash::SHA1, &data + "sha2_256" => Hash::SHA2256, &data + "sha2_512" => Hash::SHA2512, &data + "sha3_224" => Hash::SHA3224, &data + "sha3_256" => Hash::SHA3256, &data + "sha3_384" => Hash::SHA3384, &data + "keccak_224" => Hash::Keccak224, &data + "keccak_256" => Hash::Keccak256, &data + "keccak_384" => Hash::Keccak384, &data + "keccak_512" => Hash::Keccak512, &data + "blake2b_256" => Hash::Blake2b256, &data + "blake2b_512" => Hash::Blake2b512, &data + "blake2s_128" => Hash::Blake2s128, &data + "blake2s_256" => Hash::Blake2s256, &data + ); +} + +criterion_group!(benches, bench_encode); +criterion_main!(benches); From 495a09f9d665a14f1bd9b368aec8cabac3d2d9c1 Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 25 Feb 2020 14:44:40 +0800 Subject: [PATCH 2/3] Use sha-1 and sha3 instead of sha1 and tiny-keccak Signed-off-by: koushiro --- Cargo.toml | 7 ++++--- src/lib.rs | 49 +++++++++++++++++++++++++------------------------ 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0a33b302..a6d518d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,10 @@ keywords = ["multihash", "ipfs"] [dependencies] blake2b_simd = { version = "0.5.9", default-features = false } blake2s_simd = { version = "0.5.9", default-features = false } -sha1 = "0.5" -sha2 = { version = "0.7", default-features = false } -tiny-keccak = "1.4" +digest = { version = "0.8", default-features = false } +sha-1 = { version = "0.8", default-features = false } +sha2 = { version = "0.8", default-features = false } +sha3 = { version = "0.8", default-features = false } unsigned-varint = "0.3" [dev-dependencies] diff --git a/src/lib.rs b/src/lib.rs index 2a6e7289..894a5b84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,41 +6,42 @@ //! A `MultihashRef` is the same as a `Multihash`, except that it doesn't own its data. //! +#![deny(missing_docs)] + mod errors; mod hashes; mod storage; +use std::cmp; use std::convert::TryFrom; -use std::fmt::Debug; +use std::fmt::{self, Debug}; use std::hash; use blake2b_simd::{blake2b, Params as Blake2bVariable}; use blake2s_simd::{blake2s, Params as Blake2sVariable}; -use sha2::Digest; -use tiny_keccak::Keccak; +use digest::Digest; use unsigned_varint::{decode, encode}; -pub use errors::{DecodeError, DecodeOwnedError, EncodeError}; -pub use hashes::Hash; -use std::{cmp, fmt}; -use storage::Storage; +pub use self::errors::{DecodeError, DecodeOwnedError, EncodeError}; +pub use self::hashes::Hash; +use self::storage::Storage; -// Helper macro for encoding input into output using sha1, sha2, tiny_keccak, or blake2 +// Helper macro for encoding input into output using sha1, sha2, sha3, or blake2 macro_rules! encode { - (sha1, Sha1, $input:expr, $output:expr) => {{ - let mut hasher = sha1::Sha1::new(); - hasher.update($input); - $output.copy_from_slice(&hasher.digest().bytes()); + (sha1, $algorithm:ident, $input:expr, $output:expr) => {{ + let mut hasher = sha1::$algorithm::default(); + hasher.input($input); + $output.copy_from_slice(&hasher.result().as_ref()); }}; (sha2, $algorithm:ident, $input:expr, $output:expr) => {{ let mut hasher = sha2::$algorithm::default(); hasher.input($input); $output.copy_from_slice(hasher.result().as_ref()); }}; - (tiny, $constructor:ident, $input:expr, $output:expr) => {{ - let mut kec = Keccak::$constructor(); - kec.update($input); - kec.finalize($output); + (sha3, $algorithm:ident, $input:expr, $output:expr) => {{ + let mut hasher = sha3::$algorithm::default(); + hasher.input($input); + $output.copy_from_slice(hasher.result().as_ref()); }}; (blake2, $algorithm:ident, $input:expr, $output:expr) => {{ let hash = $algorithm($input); @@ -117,14 +118,14 @@ pub fn encode(hash: Hash, input: &[u8]) -> Result { SHA1 => sha1::Sha1, SHA2256 => sha2::Sha256, SHA2512 => sha2::Sha512, - SHA3224 => tiny::new_sha3_224, - SHA3256 => tiny::new_sha3_256, - SHA3384 => tiny::new_sha3_384, - SHA3512 => tiny::new_sha3_512, - Keccak224 => tiny::new_keccak224, - Keccak256 => tiny::new_keccak256, - Keccak384 => tiny::new_keccak384, - Keccak512 => tiny::new_keccak512, + SHA3224 => sha3::Sha3_224, + SHA3256 => sha3::Sha3_256, + SHA3384 => sha3::Sha3_384, + SHA3512 => sha3::Sha3_512, + Keccak224 => sha3::Keccak224, + Keccak256 => sha3::Keccak256, + Keccak384 => sha3::Keccak384, + Keccak512 => sha3::Keccak512, Blake2b512 => blake2::blake2b, Blake2b256 => blake2_256::Blake2bVariable, Blake2s256 => blake2::blake2s, From 0907e5b2d5c2ac6c4a47504cbcdab057cc64822f Mon Sep 17 00:00:00 2001 From: koushiro Date: Tue, 25 Feb 2020 21:45:58 +0800 Subject: [PATCH 3/3] Revert cargo package changes Signed-off-by: koushiro --- Cargo.toml | 8 ++++---- src/hashes.rs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a6d518d6..0a65a43f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,14 @@ [package] name = "multihash" +description = "Implementation of the multihash format" +repository = "https://github.com/multiformats/rust-multihash" +keywords = ["multihash", "ipfs"] version = "0.9.4" authors = ["dignifiedquire "] -edition = "2018" license = "MIT" readme = "README.md" -description = "Implementation of the multihash format" documentation = "https://docs.rs/multihash/" -repository = "https://github.com/multiformats/rust-multihash" -keywords = ["multihash", "ipfs"] +edition = "2018" [dependencies] blake2b_simd = { version = "0.5.9", default-features = false } diff --git a/src/hashes.rs b/src/hashes.rs index 5f22fda6..70b95b3b 100644 --- a/src/hashes.rs +++ b/src/hashes.rs @@ -5,7 +5,7 @@ use digest::Digest; use crate::digests::{wrap, Multihash, MultihashDigest}; /// The code of Multihash. -#[derive(Clone, Copy, Debug, PartialEq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Code { /// Identity (Raw binary ) Identity, @@ -162,7 +162,7 @@ impl Sha1 { } } -/// THe Sha2-256 hasher. +/// The Sha2-256 hasher. #[derive(Clone, Debug)] pub struct Sha2_256; impl MultihashDigest for Sha2_256 {