From 41f66ce21c7190f60466340cbf4b146ce5683a05 Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Tue, 12 Nov 2019 16:02:17 +0100 Subject: [PATCH 1/3] upgrade tiny-keccak to 2.0 --- ethbloom/Cargo.toml | 2 +- ethbloom/benches/bloom.rs | 16 ++++++++-------- ethbloom/src/lib.rs | 10 ++++++++-- keccak-hash/Cargo.toml | 2 +- keccak-hash/src/lib.rs | 30 +++++++++++++++++++----------- parity-crypto/Cargo.toml | 2 +- parity-crypto/src/lib.rs | 4 ++-- triehash/Cargo.toml | 2 +- triehash/benches/triehash.rs | 10 +++++++++- 9 files changed, 50 insertions(+), 28 deletions(-) diff --git a/ethbloom/Cargo.toml b/ethbloom/Cargo.toml index b65c88813..0f9c06c32 100644 --- a/ethbloom/Cargo.toml +++ b/ethbloom/Cargo.toml @@ -10,7 +10,7 @@ repository = "https://github.com/paritytech/parity-common" edition = "2018" [dependencies] -tiny-keccak = "1.5.0" +tiny-keccak = { version = "2.0.1", features = ["keccak"] } crunchy = { version = "0.2.2", default-features = false, features = ["limit_256"] } fixed-hash = { path = "../fixed-hash", version = "0.5", default-features = false } impl-serde = { path = "../primitive-types/impls/serde", version = "0.2", default-features = false, optional = true } diff --git a/ethbloom/benches/bloom.rs b/ethbloom/benches/bloom.rs index 07f11a92f..c36efceb0 100644 --- a/ethbloom/benches/bloom.rs +++ b/ethbloom/benches/bloom.rs @@ -1,7 +1,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; use ethbloom::{Bloom, Input}; use hex_literal::hex; -use tiny_keccak::keccak256; +use tiny_keccak::Keccak; fn test_bloom() -> Bloom { use std::str::FromStr; @@ -54,8 +54,8 @@ fn bench_accrue(c: &mut Criterion) { }); c.bench_function("accrue_hash", |b| { let mut bloom = Bloom::default(); - let topic = keccak256(&test_topic()); - let address = keccak256(&test_address()); + let topic = Keccak::v256(&test_topic()); + let address = Keccak::v256(&test_address()); b.iter(|| { bloom.accrue(Input::Hash(&topic)); bloom.accrue(Input::Hash(&address)); @@ -75,8 +75,8 @@ fn bench_contains(c: &mut Criterion) { }); c.bench_function("contains_input_hash", |b| { let bloom = test_bloom(); - let topic = keccak256(&test_topic()); - let address = keccak256(&test_address()); + let topic = Keccak::v256(&test_topic()); + let address = Keccak::v256(&test_address()); b.iter(|| { assert!(bloom.contains_input(Input::Hash(&topic))); assert!(bloom.contains_input(Input::Hash(&address))); @@ -96,8 +96,8 @@ fn bench_not_contains(c: &mut Criterion) { }); c.bench_function("does_not_contain_hash", |b| { let bloom = test_bloom(); - let dummy = keccak256(&test_dummy()); - let dummy2 = keccak256(&test_dummy2()); + let dummy = Keccak::v256(&test_dummy()); + let dummy2 = Keccak::v256(&test_dummy2()); b.iter(|| { assert!(!bloom.contains_input(Input::Hash(&dummy))); assert!(!bloom.contains_input(Input::Hash(&dummy2))); @@ -105,7 +105,7 @@ fn bench_not_contains(c: &mut Criterion) { }); c.bench_function("does_not_contain_random_hash", |b| { let bloom = test_bloom(); - let dummy: Vec<_> = (0..255u8).map(|i| keccak256(&[i])).collect(); + let dummy: Vec<_> = (0..255u8).map(|i| Keccak::v256(&[i])).collect(); b.iter(|| { for d in &dummy { assert!(!bloom.contains_input(Input::Hash(d))); diff --git a/ethbloom/src/lib.rs b/ethbloom/src/lib.rs index 61afa629e..d21f62022 100644 --- a/ethbloom/src/lib.rs +++ b/ethbloom/src/lib.rs @@ -52,7 +52,7 @@ use fixed_hash::*; use impl_rlp::impl_fixed_hash_rlp; #[cfg(feature = "serialize")] use impl_serde::impl_fixed_hash_serde; -use tiny_keccak::keccak256; +use tiny_keccak::{Hasher, Keccak}; // 3 according to yellowpaper const BLOOM_BITS: u32 = 3; @@ -87,7 +87,13 @@ enum Hash<'a> { impl<'a> From> for Hash<'a> { fn from(input: Input<'a>) -> Self { match input { - Input::Raw(raw) => Hash::Owned(keccak256(raw)), + Input::Raw(raw) => { + let mut out = [0u8; 32]; + let mut keccak256 = Keccak::v256(); + keccak256.update(raw); + keccak256.finalize(&mut out); + Hash::Owned(out) + }, Input::Hash(hash) => Hash::Ref(hash), } } diff --git a/keccak-hash/Cargo.toml b/keccak-hash/Cargo.toml index d90da7964..6df6f58e2 100644 --- a/keccak-hash/Cargo.toml +++ b/keccak-hash/Cargo.toml @@ -9,7 +9,7 @@ license = "GPL-3.0" edition = "2018" [dependencies] -tiny-keccak = "1.5.0" +tiny-keccak = { version = "2.0.1", features = ["keccak"] } primitive-types = { path = "../primitive-types", version = "0.6", default-features = false } [dev-dependencies] diff --git a/keccak-hash/src/lib.rs b/keccak-hash/src/lib.rs index 4f19937ae..02cd19903 100644 --- a/keccak-hash/src/lib.rs +++ b/keccak-hash/src/lib.rs @@ -21,7 +21,7 @@ use core::slice; use std::io; pub use primitive_types::H256; -use tiny_keccak::Keccak; +use tiny_keccak::{Hasher, Keccak}; /// Get the KECCAK (i.e. Keccak) hash of the empty bytes string. pub const KECCAK_EMPTY: H256 = H256([ @@ -50,32 +50,40 @@ pub fn keccak>(s: T) -> H256 { pub unsafe fn keccak_256_unchecked(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) { // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This // means that we can reuse the input buffer for both input and output. - Keccak::keccak256(slice::from_raw_parts(input, inputlen), slice::from_raw_parts_mut(out, outlen)); + let input = slice::from_raw_parts(input, inputlen); + let output = slice::from_raw_parts_mut(out, outlen); + keccak_256(input, output); } pub unsafe fn keccak_512_unchecked(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) { // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This // means that we can reuse the input buffer for both input and output. - Keccak::keccak512(slice::from_raw_parts(input, inputlen), slice::from_raw_parts_mut(out, outlen)); + let input = slice::from_raw_parts(input, inputlen); + let output = slice::from_raw_parts_mut(out, outlen); + keccak_512(input, output); } -pub fn keccak_256(input: &[u8], mut output: &mut [u8]) { - Keccak::keccak256(input, &mut output); +pub fn keccak_256(input: &[u8], output: &mut [u8]) { + write_keccak(input, output); } -pub fn keccak_512(input: &[u8], mut output: &mut [u8]) { - Keccak::keccak512(input, &mut output); +pub fn keccak_512(input: &[u8], output: &mut [u8]) { + let mut keccak512 = Keccak::v512(); + keccak512.update(input); + keccak512.finalize(output); } pub fn write_keccak>(s: T, dest: &mut [u8]) { - Keccak::keccak256(s.as_ref(), dest); + let mut keccak256 = Keccak::v256(); + keccak256.update(s.as_ref()); + keccak256.finalize(dest); } #[cfg(feature = "std")] pub fn keccak_pipe(r: &mut dyn io::BufRead, w: &mut dyn io::Write) -> Result { let mut output = [0u8; 32]; let mut input = [0u8; 1024]; - let mut keccak = Keccak::new_keccak256(); + let mut keccak256 = Keccak::v256(); // read file loop { @@ -83,11 +91,11 @@ pub fn keccak_pipe(r: &mut dyn io::BufRead, w: &mut dyn io::Write) -> Result, { fn keccak256(&self) -> [u8; 32] { - let mut keccak = Keccak::new_keccak256(); + let mut keccak = Keccak::v256(); let mut result = [0u8; 32]; keccak.update(self.as_ref()); keccak.finalize(&mut result); diff --git a/triehash/Cargo.toml b/triehash/Cargo.toml index 6342f7d03..4b49ae9c2 100644 --- a/triehash/Cargo.toml +++ b/triehash/Cargo.toml @@ -15,7 +15,7 @@ rlp = { version = "0.4", path = "../rlp" } criterion = "0.3.0" keccak-hasher = "0.15.2" ethereum-types = { version = "0.8.0", path = "../ethereum-types" } -tiny-keccak = "1.5.0" +tiny-keccak = { version = "2.0.1", features = ["keccak"] } trie-standardmap = "0.15.2" hex-literal = "0.2.1" diff --git a/triehash/benches/triehash.rs b/triehash/benches/triehash.rs index fea116a46..684484265 100644 --- a/triehash/benches/triehash.rs +++ b/triehash/benches/triehash.rs @@ -17,10 +17,18 @@ use criterion::{criterion_group, criterion_main, Criterion}; use ethereum_types::H256; use keccak_hasher::KeccakHasher; -use tiny_keccak::keccak256; +use tiny_keccak::{Hasher, Keccak}; use trie_standardmap::{Alphabet, StandardMap, ValueMode}; use triehash::trie_root; +fn keccak256(input: &[u8]) -> [u8; 32] { + let mut keccak256 = Keccak::v256(); + let mut out = [0u8; 32]; + keccak256.update(input); + keccak256.finalize(&mut out); + out +} + fn random_word(alphabet: &[u8], min_count: usize, diff_count: usize, seed: &mut H256) -> Vec { assert!(min_count + diff_count <= 32); *seed = H256(keccak256(seed.as_bytes())); From 7279cd4d461f4d05009000f06ab97fc1e9cc4bce Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Tue, 12 Nov 2019 17:17:38 +0100 Subject: [PATCH 2/3] address Marek's feedback --- ethbloom/Cargo.toml | 2 +- keccak-hash/Cargo.toml | 2 +- keccak-hash/src/lib.rs | 23 ++++++++++------------- parity-crypto/Cargo.toml | 2 +- triehash/Cargo.toml | 2 +- 5 files changed, 14 insertions(+), 17 deletions(-) diff --git a/ethbloom/Cargo.toml b/ethbloom/Cargo.toml index 0f9c06c32..56a092776 100644 --- a/ethbloom/Cargo.toml +++ b/ethbloom/Cargo.toml @@ -10,7 +10,7 @@ repository = "https://github.com/paritytech/parity-common" edition = "2018" [dependencies] -tiny-keccak = { version = "2.0.1", features = ["keccak"] } +tiny-keccak = { version = "2.0", features = ["keccak"] } crunchy = { version = "0.2.2", default-features = false, features = ["limit_256"] } fixed-hash = { path = "../fixed-hash", version = "0.5", default-features = false } impl-serde = { path = "../primitive-types/impls/serde", version = "0.2", default-features = false, optional = true } diff --git a/keccak-hash/Cargo.toml b/keccak-hash/Cargo.toml index 6df6f58e2..14b6b19ba 100644 --- a/keccak-hash/Cargo.toml +++ b/keccak-hash/Cargo.toml @@ -9,7 +9,7 @@ license = "GPL-3.0" edition = "2018" [dependencies] -tiny-keccak = { version = "2.0.1", features = ["keccak"] } +tiny-keccak = { version = "2.0", features = ["keccak"] } primitive-types = { path = "../primitive-types", version = "0.6", default-features = false } [dev-dependencies] diff --git a/keccak-hash/src/lib.rs b/keccak-hash/src/lib.rs index 02cd19903..e66650b39 100644 --- a/keccak-hash/src/lib.rs +++ b/keccak-hash/src/lib.rs @@ -16,7 +16,6 @@ #![cfg_attr(not(feature = "std"), no_std)] -use core::slice; #[cfg(feature = "std")] use std::io; @@ -47,20 +46,18 @@ pub fn keccak>(s: T) -> H256 { H256(result) } -pub unsafe fn keccak_256_unchecked(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) { - // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This - // means that we can reuse the input buffer for both input and output. - let input = slice::from_raw_parts(input, inputlen); - let output = slice::from_raw_parts_mut(out, outlen); - keccak_256(input, output); +/// Computes in-place keccak256 hash of `data`. +pub fn keccak256(data: &mut [u8]) { + let mut keccak256 = Keccak::v256(); + keccak256.update(data.as_ref()); + keccak256.finalize(data); } -pub unsafe fn keccak_512_unchecked(out: *mut u8, outlen: usize, input: *const u8, inputlen: usize) { - // This is safe since `keccak_*` uses an internal buffer and copies the result to the output. This - // means that we can reuse the input buffer for both input and output. - let input = slice::from_raw_parts(input, inputlen); - let output = slice::from_raw_parts_mut(out, outlen); - keccak_512(input, output); +/// Computes in-place keccak512 hash of `data`. +pub fn keccak512(data: &mut [u8]) { + let mut keccak512 = Keccak::v512(); + keccak512.update(data.as_ref()); + keccak512.finalize(data); } pub fn keccak_256(input: &[u8], output: &mut [u8]) { diff --git a/parity-crypto/Cargo.toml b/parity-crypto/Cargo.toml index a6f5d26df..d3e7b1994 100644 --- a/parity-crypto/Cargo.toml +++ b/parity-crypto/Cargo.toml @@ -14,7 +14,7 @@ harness = false required-features = ["publickey"] [dependencies] -tiny-keccak = { version = "2.0.1", features = ["keccak"] } +tiny-keccak = { version = "2.0", features = ["keccak"] } scrypt = { version = "0.2.0", default-features = false } parity-secp256k1 = { version = "0.7.0", optional = true } ethereum-types = { version = "0.8.0", optional = true } diff --git a/triehash/Cargo.toml b/triehash/Cargo.toml index 4b49ae9c2..701aae36b 100644 --- a/triehash/Cargo.toml +++ b/triehash/Cargo.toml @@ -15,7 +15,7 @@ rlp = { version = "0.4", path = "../rlp" } criterion = "0.3.0" keccak-hasher = "0.15.2" ethereum-types = { version = "0.8.0", path = "../ethereum-types" } -tiny-keccak = { version = "2.0.1", features = ["keccak"] } +tiny-keccak = { version = "2.0", features = ["keccak"] } trie-standardmap = "0.15.2" hex-literal = "0.2.1" From 60b28f2996e9140e7507c80b45244bb092e8173d Mon Sep 17 00:00:00 2001 From: Andronik Ordian Date: Tue, 12 Nov 2019 17:31:58 +0100 Subject: [PATCH 3/3] [ethbloom] fix benches --- ethbloom/benches/bloom.rs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/ethbloom/benches/bloom.rs b/ethbloom/benches/bloom.rs index c36efceb0..005cfd88f 100644 --- a/ethbloom/benches/bloom.rs +++ b/ethbloom/benches/bloom.rs @@ -1,7 +1,7 @@ use criterion::{criterion_group, criterion_main, Criterion}; use ethbloom::{Bloom, Input}; use hex_literal::hex; -use tiny_keccak::Keccak; +use tiny_keccak::{Hasher, Keccak}; fn test_bloom() -> Bloom { use std::str::FromStr; @@ -26,6 +26,14 @@ fn test_bloom() -> Bloom { .unwrap() } +fn keccak256(input: &[u8]) -> [u8; 32] { + let mut out = [0u8; 32]; + let mut keccak256 = Keccak::v256(); + keccak256.update(input); + keccak256.finalize(&mut out); + out +} + fn test_topic() -> Vec { hex!("02c69be41d0b7e40352fc85be1cd65eb03d40ef8427a0ca4596b1ead9a00e9fc").to_vec() } @@ -54,8 +62,8 @@ fn bench_accrue(c: &mut Criterion) { }); c.bench_function("accrue_hash", |b| { let mut bloom = Bloom::default(); - let topic = Keccak::v256(&test_topic()); - let address = Keccak::v256(&test_address()); + let topic = keccak256(&test_topic()); + let address = keccak256(&test_address()); b.iter(|| { bloom.accrue(Input::Hash(&topic)); bloom.accrue(Input::Hash(&address)); @@ -75,8 +83,8 @@ fn bench_contains(c: &mut Criterion) { }); c.bench_function("contains_input_hash", |b| { let bloom = test_bloom(); - let topic = Keccak::v256(&test_topic()); - let address = Keccak::v256(&test_address()); + let topic = keccak256(&test_topic()); + let address = keccak256(&test_address()); b.iter(|| { assert!(bloom.contains_input(Input::Hash(&topic))); assert!(bloom.contains_input(Input::Hash(&address))); @@ -96,8 +104,8 @@ fn bench_not_contains(c: &mut Criterion) { }); c.bench_function("does_not_contain_hash", |b| { let bloom = test_bloom(); - let dummy = Keccak::v256(&test_dummy()); - let dummy2 = Keccak::v256(&test_dummy2()); + let dummy = keccak256(&test_dummy()); + let dummy2 = keccak256(&test_dummy2()); b.iter(|| { assert!(!bloom.contains_input(Input::Hash(&dummy))); assert!(!bloom.contains_input(Input::Hash(&dummy2))); @@ -105,7 +113,7 @@ fn bench_not_contains(c: &mut Criterion) { }); c.bench_function("does_not_contain_random_hash", |b| { let bloom = test_bloom(); - let dummy: Vec<_> = (0..255u8).map(|i| Keccak::v256(&[i])).collect(); + let dummy: Vec<_> = (0..255u8).map(|i| keccak256(&[i])).collect(); b.iter(|| { for d in &dummy { assert!(!bloom.contains_input(Input::Hash(d)));