Skip to content

Commit

Permalink
feat: support Keccak with sha3
Browse files Browse the repository at this point in the history
  • Loading branch information
xJonathanLEI committed Sep 13, 2024
1 parent 022c63d commit 9b4e1ac
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ thiserror = "1.0"
k256 = { version = "0.13", default-features = false }
keccak-asm = { version = "0.1.0", default-features = false }
tiny-keccak = "2.0"
sha3 = "0.10.8"

# misc
allocative = { version = "0.3.2", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ sol-types = ["dep:alloy-sol-types"]
tiny-keccak = ["alloy-primitives/tiny-keccak"]
native-keccak = ["alloy-primitives/native-keccak"]
asm-keccak = ["alloy-primitives/asm-keccak"]
sha3-keccak = ["alloy-primitives/sha3-keccak"]

postgres = ["std", "alloy-primitives/postgres"]
getrandom = ["alloy-primitives/getrandom"]
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ itoa.workspace = true
ruint.workspace = true
tiny-keccak = { workspace = true, features = ["keccak"] }
keccak-asm = { workspace = true, optional = true }
sha3 = { workspace = true, optional = true }

# macros
derive_more = { workspace = true, features = [
Expand Down Expand Up @@ -97,6 +98,7 @@ std = [
tiny-keccak = []
native-keccak = []
asm-keccak = ["dep:keccak-asm"]
sha3-keccak = ["dep:sha3"]

postgres = ["std", "dep:postgres-types", "ruint/postgres"]
getrandom = ["dep:getrandom"]
Expand Down
3 changes: 3 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#[macro_use]
extern crate alloc;

#[cfg(feature = "sha3-keccak")]
use sha3 as _;

use tiny_keccak as _;

#[cfg(feature = "postgres")]
Expand Down
21 changes: 17 additions & 4 deletions crates/primitives/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub use units::{
cfg_if! {
if #[cfg(all(feature = "asm-keccak", not(miri)))] {
use keccak_asm::Digest as _;
} else if #[cfg(all(feature = "sha3-keccak", not(miri)))] {
use sha3::Digest as _;
} else {
use tiny_keccak::Hasher as _;
}
Expand Down Expand Up @@ -153,13 +155,13 @@ pub fn keccak256<T: AsRef<[u8]>>(bytes: T) -> B256 {
let mut output = MaybeUninit::<B256>::uninit();

cfg_if! {
if #[cfg(all(feature = "native-keccak", not(feature = "tiny-keccak"), not(miri)))] {
if #[cfg(all(feature = "native-keccak", not(feature = "sha3-keccak"), not(feature = "tiny-keccak"), not(miri)))] {
#[link(wasm_import_module = "vm_hooks")]
extern "C" {
/// When targeting VMs with native keccak hooks, the `native-keccak` feature
/// can be enabled to import and use the host environment's implementation
/// of [`keccak256`] in place of [`tiny_keccak`]. This is overridden when
/// the `tiny-keccak` feature is enabled.
/// of [`keccak256`] in place of [`sha3`] or [`tiny_keccak`]. This is overridden
/// when the `sha3-keccak` or `tiny-keccak` feature is enabled.
///
/// # Safety
///
Expand All @@ -169,6 +171,7 @@ pub fn keccak256<T: AsRef<[u8]>>(bytes: T) -> B256 {
/// - `output` must point to a buffer that is at least 32-bytes long.
///
/// [`keccak256`]: https://en.wikipedia.org/wiki/SHA-3
/// [`sha3`]: https://docs.rs/sha3/latest/sha3/
/// [`tiny_keccak`]: https://docs.rs/tiny-keccak/latest/tiny_keccak/
fn native_keccak256(bytes: *const u8, len: usize, output: *mut u8);
}
Expand Down Expand Up @@ -200,7 +203,12 @@ pub fn keccak256<T: AsRef<[u8]>>(bytes: T) -> B256 {
pub struct Keccak256 {
#[cfg(all(feature = "asm-keccak", not(miri)))]
hasher: keccak_asm::Keccak256,
#[cfg(not(all(feature = "asm-keccak", not(miri))))]
#[cfg(all(feature = "sha3-keccak", not(miri), not(all(feature = "asm-keccak", not(miri)))))]
hasher: sha3::Keccak256,
#[cfg(not(any(
all(feature = "asm-keccak", not(miri)),
all(feature = "sha3-keccak", not(miri)),
)))]
hasher: tiny_keccak::Keccak,
}

Expand All @@ -225,6 +233,8 @@ impl Keccak256 {
cfg_if! {
if #[cfg(all(feature = "asm-keccak", not(miri)))] {
let hasher = keccak_asm::Keccak256::new();
} else if #[cfg(all(feature = "sha3-keccak", not(miri)))] {
let hasher = sha3::Keccak256::new();
} else {
let hasher = tiny_keccak::Keccak::v256();
}
Expand Down Expand Up @@ -265,6 +275,9 @@ impl Keccak256 {
cfg_if! {
if #[cfg(all(feature = "asm-keccak", not(miri)))] {
self.hasher.finalize_into(output.into());
} else if #[cfg(all(feature = "sha3-keccak", not(miri)))] {
<sha3::Keccak256 as sha3::digest::DynDigest>::finalize_into(self.hasher, output)
.unwrap();
} else {
self.hasher.finalize(output);
}
Expand Down

0 comments on commit 9b4e1ac

Please sign in to comment.