From c070e77524680e0937ab4b2131452d8d5dbe80e4 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 21 Dec 2022 21:22:18 +0100 Subject: [PATCH] chore(primitives): Update keccak256 implementation (#563) * chore(primitives): Update keccak256 implementation * chore(primitives): Update keccak256 implementation * revert BytesMut Co-authored-by: Matthias Seitz --- crates/primitives/src/header.rs | 5 +++-- crates/primitives/src/lib.rs | 12 +++++++----- crates/primitives/src/transaction/mod.rs | 11 +++++------ crates/primitives/src/transaction/util.rs | 3 +-- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/crates/primitives/src/header.rs b/crates/primitives/src/header.rs index 7a8d5eaf58..f417bd863a 100644 --- a/crates/primitives/src/header.rs +++ b/crates/primitives/src/header.rs @@ -1,9 +1,10 @@ use crate::{ + keccak256, proofs::{EMPTY_LIST_HASH, EMPTY_ROOT}, BlockHash, BlockNumber, Bloom, H160, H256, U256, }; use bytes::{BufMut, BytesMut}; -use ethers_core::{types::H64, utils::keccak256}; +use ethers_core::types::H64; use reth_codecs::{main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, Encodable}; use serde::{Deserialize, Serialize}; @@ -97,7 +98,7 @@ impl Header { pub fn hash_slow(&self) -> H256 { let mut out = BytesMut::new(); self.encode(&mut out); - H256::from_slice(keccak256(&out).as_slice()) + keccak256(&out) } /// Checks if the header is empty - has no transactions and no ommers diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index 213e7b1a17..70d0f8b8de 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -94,11 +94,13 @@ pub mod utils { pub use __reexport::*; /// Returns the keccak256 hash for the given data. +#[inline] pub fn keccak256(data: impl AsRef<[u8]>) -> H256 { use tiny_keccak::{Hasher, Keccak}; - let mut keccak = Keccak::v256(); - let mut output = [0; 32]; - keccak.update(data.as_ref()); - keccak.finalize(&mut output); - output.into() + + let mut buf = [0u8; 32]; + let mut hasher = Keccak::v256(); + hasher.update(data.as_ref()); + hasher.finalize(&mut buf); + buf.into() } diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 65ab049c26..9c334745fa 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -1,8 +1,7 @@ -use crate::{Address, Bytes, ChainId, TxHash, H256}; +use crate::{keccak256, Address, Bytes, ChainId, TxHash, H256}; pub use access_list::{AccessList, AccessListItem}; use bytes::{Buf, BytesMut}; use derive_more::{AsRef, Deref}; -use ethers_core::utils::keccak256; use reth_codecs::{main_codec, Compact}; use reth_rlp::{length_of_length, Decodable, DecodeError, Encodable, Header, EMPTY_STRING_CODE}; pub use signature::Signature; @@ -185,7 +184,7 @@ impl Transaction { pub fn signature_hash(&self) -> H256 { let mut buf = BytesMut::new(); self.encode(&mut buf); - keccak256(&buf).into() + keccak256(&buf) } /// Sets the transaction's chain id to the provided value. @@ -614,7 +613,7 @@ impl Decodable for TransactionSigned { }; let mut signed = TransactionSigned { transaction, hash: Default::default(), signature }; - signed.hash = keccak256(&original_encoding[..first_header.payload_length]).into(); + signed.hash = keccak256(&original_encoding[..first_header.payload_length]); Ok(signed) } else { let mut transaction = Transaction::Legacy(TxLegacy { @@ -633,7 +632,7 @@ impl Decodable for TransactionSigned { let mut signed = TransactionSigned { transaction, hash: Default::default(), signature }; let tx_length = first_header.payload_length + first_header.length(); - signed.hash = keccak256(&original_encoding[..tx_length]).into(); + signed.hash = keccak256(&original_encoding[..tx_length]); Ok(signed) } } @@ -717,7 +716,7 @@ impl TransactionSigned { pub fn recalculate_hash(&self) -> H256 { let mut buf = Vec::new(); self.encode_inner(&mut buf, false); - keccak256(&buf).into() + keccak256(&buf) } /// Create a new signed transaction from a transaction and its signature. diff --git a/crates/primitives/src/transaction/util.rs b/crates/primitives/src/transaction/util.rs index e91e478f86..58bfc74275 100644 --- a/crates/primitives/src/transaction/util.rs +++ b/crates/primitives/src/transaction/util.rs @@ -1,13 +1,12 @@ use crate::{keccak256, Address}; pub(crate) mod secp256k1 { - + use super::*; use ::secp256k1::{ ecdsa::{RecoverableSignature, RecoveryId}, Error, Message, Secp256k1, }; - use super::*; /// secp256k1 signer recovery pub(crate) fn recover(sig: &[u8; 65], msg: &[u8; 32]) -> Result { let sig =