Skip to content

Commit

Permalink
♻️ Rename keccak_concat_fixed -> keccak_concat_hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Nov 10, 2023
1 parent 3fcb5aa commit 9a36de9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: sudo apt-get install xsltproc
- uses: taiki-e/install-action@nextest
- name: cargo test
run: cargo nextest run --workspace --all --locked
run: cargo nextest run --release --workspace --all --locked
cargo-lint:
runs-on: ubuntu-latest
timeout-minutes: 20
Expand All @@ -37,9 +37,9 @@ jobs:
- name: Install xsltproc
run: sudo apt-get install xsltproc
- name: cargo fmt
run: cargo +nightly fmt --all -- --check
run: cargo fmt --all -- --check
- name: cargo clippy
run: cargo +nightly clippy --workspace --all --locked -- -D warnings
run: cargo clippy --workspace --all --locked -- -D warnings
cargo-build:
runs-on: ubuntu-latest
timeout-minutes: 20
Expand Down
22 changes: 11 additions & 11 deletions crates/mipsevm/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::{
page::{self},
types::SharedCachedPage,
utils::keccak_concat_fixed,
utils::keccak_concat_hashes,
Address, Gindex, Page, PageIndex,
};
use anyhow::Result;
Expand Down Expand Up @@ -148,7 +148,7 @@ impl Memory {

let left = self.merkleize_subtree(g_index << 1)?;
let right = self.merkleize_subtree((g_index << 1) | 1)?;
let result = *keccak_concat_fixed(left, right);
let result = *keccak_concat_hashes(left, right);

self.nodes.insert(g_index, Some(result));

Expand Down Expand Up @@ -507,9 +507,9 @@ mod test {
(32..proof.len()).step_by(32).for_each(|i| {
let sib: [u8; 32] = proof[i..i + 32].try_into().unwrap();
if path & 1 != 0 {
node = *keccak_concat_fixed(sib, node);
node = *keccak_concat_hashes(sib, node);
} else {
node = *keccak_concat_fixed(node, sib);
node = *keccak_concat_hashes(node, sib);
}
path >>= 1;
});
Expand Down Expand Up @@ -590,15 +590,15 @@ mod test {
.merkleize_subtree((1 << page::PAGE_KEY_SIZE) | 6)
.unwrap();
let z = page::ZERO_HASHES[page::PAGE_ADDRESS_SIZE - 5];
let r1 = keccak_concat_fixed(
keccak_concat_fixed(
keccak_concat_fixed(z.into(), z.into()).into(),
keccak_concat_fixed(z.into(), p3.into()).into(),
let r1 = keccak_concat_hashes(
keccak_concat_hashes(
keccak_concat_hashes(z.into(), z.into()).into(),
keccak_concat_hashes(z.into(), p3.into()).into(),
)
.into(),
keccak_concat_fixed(
keccak_concat_fixed(z.into(), p5.into()).into(),
keccak_concat_fixed(p6.into(), z.into()).into(),
keccak_concat_hashes(
keccak_concat_hashes(z.into(), p5.into()).into(),
keccak_concat_hashes(p6.into(), z.into()).into(),
)
.into(),
);
Expand Down
10 changes: 5 additions & 5 deletions crates/mipsevm/src/page.rs
Original file line number Diff line number Diff line change
@@ -1,6 +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 crate::{utils::keccak_concat_hashes, Address, Gindex, Page};
use anyhow::Result;
use once_cell::sync::Lazy;

Expand All @@ -19,7 +19,7 @@ pub(crate) const PAGE_KEY_MASK: usize = MAX_PAGE_COUNT - 1;
pub(crate) static ZERO_HASHES: Lazy<[[u8; 32]; 256]> = Lazy::new(|| {
let mut out = [[0u8; 32]; 256];
for i in 1..256 {
out[i] = *keccak_concat_fixed(out[i - 1], out[i - 1])
out[i] = *keccak_concat_hashes(out[i - 1], out[i - 1])
}
out
});
Expand Down Expand Up @@ -138,7 +138,7 @@ impl CachedPage {
let right_child = left_child + 1;

// Ensure children are hashed.
*keccak_concat_fixed(
*keccak_concat_hashes(
self.merkleize_subtree(left_child as Gindex)?,
self.merkleize_subtree(right_child as Gindex)?,
)
Expand Down Expand Up @@ -166,12 +166,12 @@ mod test {
assert_eq!(node, expected_leaf, "Leaf nodes should not be hashed");

let node = page.merkleize_subtree(g_index >> 1).unwrap();
let expected_parent = keccak_concat_fixed(ZERO_HASHES[0].into(), expected_leaf.into());
let expected_parent = keccak_concat_hashes(ZERO_HASHES[0].into(), expected_leaf.into());
assert_eq!(node, expected_parent, "Parent should be correct");

let node = page.merkleize_subtree(g_index >> 2).unwrap();
let expected_grandparent =
keccak_concat_fixed(expected_parent.into(), ZERO_HASHES[1].into());
keccak_concat_hashes(expected_parent.into(), ZERO_HASHES[1].into());
assert_eq!(node, expected_grandparent, "Grandparent should be correct");

let pre = page.merkle_root().unwrap();
Expand Down
7 changes: 2 additions & 5 deletions crates/mipsevm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@ where
concatenated
}

/// Hash the concatenation of two fixed sized arrays.
/// Hash the concatenation of two 32 byte digests.
#[inline(always)]
pub(crate) fn keccak_concat_fixed<const N: usize, const M: usize>(a: [u8; N], b: [u8; M]) -> B256
where
[(); N + M]:,
{
pub(crate) fn keccak_concat_hashes(a: [u8; 32], b: [u8; 32]) -> B256 {
#[cfg(feature = "simd-keccak")]
{
let mut out = B256::ZERO;
Expand Down

0 comments on commit 9a36de9

Please sign in to comment.