Skip to content

Commit

Permalink
⚡ Use xkcp-rs keccak256 bindings for mipsevm keccak operations
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Oct 22, 2023
1 parent af37c59 commit 04d56c9
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 11 deletions.
142 changes: 142 additions & 0 deletions 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 crates/mipsevm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ revm = { version = "3.3.0", features = ["no_gas_measuring"] }
rustc-hash = "1.1.0"
serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.107"
xkcp-rs = { git = "https://github.com/DaniPopes/xkcp-rs" }

# misc
anyhow = "1.0.75"
Expand Down
3 changes: 1 addition & 2 deletions crates/mipsevm/src/page.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! This module contains the data structure for a [Page] within the MIPS emulator's [Memory].

use crate::{utils::keccak_concat_fixed, Address, Gindex, Page};
use alloy_primitives::keccak256;
use crate::{utils::keccak256, utils::keccak_concat_fixed, Address, Gindex, Page};
use anyhow::Result;
use once_cell::sync::Lazy;

Expand Down
4 changes: 2 additions & 2 deletions crates/mipsevm/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Testing utilities.

use crate::{utils::concat_fixed, PreimageOracle};
use alloy_primitives::{hex, keccak256};
use crate::{utils::concat_fixed, utils::keccak256, PreimageOracle};
use alloy_primitives::hex;
use anyhow::Result;
use preimage_oracle::{Hint, Keccak256Key, Key, LocalIndexKey};
use rustc_hash::FxHashMap;
Expand Down
16 changes: 11 additions & 5 deletions crates/mipsevm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains utility and helper functions for this crate.

use alloy_primitives::{keccak256, B256};
use alloy_primitives::B256;

/// Concatenate two fixed sized arrays together into a new array with minimal reallocation.
#[inline(always)]
Expand All @@ -17,10 +17,16 @@ where

/// Hash the concatenation of two fixed sized arrays.
#[inline(always)]
pub(crate) fn keccak_concat_fixed<T, const N: usize, const M: usize>(a: [T; N], b: [T; M]) -> B256
pub(crate) fn keccak_concat_fixed<const N: usize, const M: usize>(a: [u8; N], b: [u8; M]) -> B256
where
T: Copy + Default,
[T; N + M]: AsRef<[u8]>,
[(); N + M]:,
{
keccak256(concat_fixed(a, b))
keccak256(concat_fixed(a, b).as_slice())
}

#[inline(always)]
pub(crate) fn keccak256<T: AsRef<[u8]>>(input: T) -> B256 {
let mut out = B256::ZERO;
xkcp_rs::keccak256(input.as_ref(), out.as_mut());
out
}
4 changes: 2 additions & 2 deletions crates/mipsevm/src/witness.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! This module contains the various witness types.

use crate::{State, StateWitness, StateWitnessHasher};
use alloy_primitives::{keccak256, Bytes, B256, U256};
use crate::{utils::keccak256, State, StateWitness, StateWitnessHasher};
use alloy_primitives::{Bytes, B256, U256};
use alloy_sol_types::{sol, SolCall};
use preimage_oracle::KeyType;

Expand Down

0 comments on commit 04d56c9

Please sign in to comment.