Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ethbloom/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/paritytech/parity-common"
edition = "2018"

[dependencies]
tiny-keccak = "1.5.0"
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 }
Expand Down
16 changes: 8 additions & 8 deletions ethbloom/benches/bloom.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
Expand All @@ -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)));
Expand All @@ -96,16 +96,16 @@ 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)));
})
});
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)));
Expand Down
10 changes: 8 additions & 2 deletions ethbloom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -87,7 +87,13 @@ enum Hash<'a> {
impl<'a> From<Input<'a>> 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),
}
}
Expand Down
2 changes: 1 addition & 1 deletion keccak-hash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "GPL-3.0"
edition = "2018"

[dependencies]
tiny-keccak = "1.5.0"
tiny-keccak = { version = "2.0", features = ["keccak"] }
primitive-types = { path = "../primitive-types", version = "0.6", default-features = false }

[dev-dependencies]
Expand Down
41 changes: 23 additions & 18 deletions keccak-hash/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@

#![cfg_attr(not(feature = "std"), no_std)]

use core::slice;
#[cfg(feature = "std")]
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([
Expand All @@ -47,47 +46,53 @@ pub fn keccak<T: AsRef<[u8]>>(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.
Keccak::keccak256(slice::from_raw_parts(input, inputlen), slice::from_raw_parts_mut(out, outlen));
/// 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.
Keccak::keccak512(slice::from_raw_parts(input, inputlen), slice::from_raw_parts_mut(out, outlen));
/// 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], 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<T: AsRef<[u8]>>(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<H256, io::Error> {
let mut output = [0u8; 32];
let mut input = [0u8; 1024];
let mut keccak = Keccak::new_keccak256();
let mut keccak256 = Keccak::v256();

// read file
loop {
let some = r.read(&mut input)?;
if some == 0 {
break;
}
keccak.update(&input[0..some]);
keccak256.update(&input[0..some]);
w.write_all(&input[0..some])?;
}

keccak.finalize(&mut output);
keccak256.finalize(&mut output);
Ok(output.into())
}

Expand Down
2 changes: 1 addition & 1 deletion parity-crypto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ harness = false
required-features = ["publickey"]

[dependencies]
tiny-keccak = "1.5.0"
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 }
Expand Down
4 changes: 2 additions & 2 deletions parity-crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub mod scrypt;
pub use crate::error::Error;

use subtle::ConstantTimeEq;
use tiny_keccak::Keccak;
use tiny_keccak::{Hasher, Keccak};

pub const KEY_LENGTH: usize = 32;
pub const KEY_ITERATIONS: usize = 10240;
Expand All @@ -48,7 +48,7 @@ where
T: AsRef<[u8]>,
{
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);
Expand Down
2 changes: 1 addition & 1 deletion triehash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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", features = ["keccak"] }
trie-standardmap = "0.15.2"
hex-literal = "0.2.1"

Expand Down
10 changes: 9 additions & 1 deletion triehash/benches/triehash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
assert!(min_count + diff_count <= 32);
*seed = H256(keccak256(seed.as_bytes()));
Expand Down