chore(primitives): Update keccak256 implementation (#563)

* chore(primitives): Update keccak256 implementation

* chore(primitives): Update keccak256 implementation

* revert BytesMut

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
DaniPopes
2022-12-21 21:22:18 +01:00
committed by GitHub
parent c2ed3b2b52
commit c070e77524
4 changed files with 16 additions and 15 deletions

View File

@@ -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

View File

@@ -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()
}

View File

@@ -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.

View File

@@ -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<Address, Error> {
let sig =