Skip to content

Commit

Permalink
refactor: change keccak hash function (#1157)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason Gao authored Apr 18, 2023
1 parent 1684b06 commit a64937c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 12 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ members = [
"common/channel",
"common/config-parser",
"common/crypto",
"common/hasher",
"common/logger",
"common/memory-tracker",
"common/merkle",
Expand Down
8 changes: 8 additions & 0 deletions common/hasher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "common-hasher"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
tiny-keccak = { version = "2.0", features = ["keccak"] }
9 changes: 9 additions & 0 deletions common/hasher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use tiny_keccak::{Hasher, Keccak};

pub fn keccak256<B: AsRef<[u8]>>(data: B) -> [u8; 32] {
let mut result = [0u8; 32];
let mut hasher = Keccak::v256();
hasher.update(data.as_ref());
hasher.finalize(&mut result);
result
}
5 changes: 3 additions & 2 deletions protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ ckb-jsonrpc-types = "0.108"
ckb-sdk = "2.5"
ckb-traits = "0.108"
ckb-types = "0.108"
common-crypto = { path = "../common/crypto" }
creep = "0.2"
derive_more = "0.99"
ethereum = { version = "0.14", features = ["with-codec", "with-serde"] }
ethereum-types = { version = "0.14", features = ["arbitrary", "codec", "rlp", "serialize", "std"] }
evm = { version = "0.37", features = ["with-serde"] }
faster-hex = "0.6"
hasher = { version = "0.1", features = ["hash-keccak"] }
lazy_static = "1.4"
ophelia = "0.3"
ophelia-secp256k1 = "0.3"
Expand All @@ -35,6 +33,9 @@ serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.27", features = ["full"] }
trie = { package = "cita_trie", version = "4.0" }

common-crypto = { path = "../common/crypto" }
common-hasher = { path = "../common/hasher" }

[dev-dependencies]
hex = "0.4"
serde_json = "1.0"
Expand Down
12 changes: 3 additions & 9 deletions protocol/src/types/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@ pub use ethereum_types::{
U512, U64,
};

use hasher::{Hasher as KeccakHasher, HasherKeccak};
use ophelia::{PublicKey, UncompressedPublicKey};
use ophelia_secp256k1::Secp256k1PublicKey;
use overlord::DurationConfig;
use rlp_derive::{RlpDecodable, RlpEncodable};
use serde::{de, Deserialize, Serialize};

use common_hasher::keccak256;

use crate::codec::{hex_decode, hex_encode};
use crate::types::{BlockNumber, Bytes, TypesError};
use crate::{ProtocolError, ProtocolResult};

lazy_static::lazy_static! {
static ref HASHER_INST: HasherKeccak = HasherKeccak::new();
}

pub type Hash = H256;
pub type MerkleRoot = Hash;

Expand Down Expand Up @@ -55,10 +52,7 @@ impl Hasher {
return NIL_DATA;
}

let hash = HASHER_INST.digest(bytes.as_ref());
let mut ret = Hash::default();
ret.0.copy_from_slice(&hash[0..32]);
ret
H256(keccak256(bytes))
}
}

Expand Down

0 comments on commit a64937c

Please sign in to comment.