From 52ebdf19a2c8cb132fb526a97ed5894a8eb217a9 Mon Sep 17 00:00:00 2001 From: clabby Date: Tue, 12 Sep 2023 23:20:03 -0400 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=20Clean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yaml | 27 +++++++++++++-- crates/mipsevm/src/lib.rs | 3 +- crates/mipsevm/src/{memory.rs => page.rs} | 40 +++++++++++------------ crates/mipsevm/src/utils.rs | 2 +- 4 files changed, 48 insertions(+), 24 deletions(-) rename crates/mipsevm/src/{memory.rs => page.rs} (80%) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 5a90642..2015ba6 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -11,7 +11,7 @@ jobs: - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: nightly profile: minimal override: true - uses: Swatinem/rust-cache@v1 @@ -49,7 +49,7 @@ jobs: - name: Install Rust toolchain uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: nightly profile: minimal override: true - uses: Swatinem/rust-cache@v1 @@ -59,3 +59,26 @@ jobs: id: build continue-on-error: true run: cargo build --all + cargo-doc: + runs-on: ubuntu-latest + timeout-minutes: 20 + continue-on-error: true + steps: + - name: Checkout sources + uses: actions/checkout@v3 + - name: Install Rust toolchain + uses: actions-rs/toolchain@v1 + with: + toolchain: nightly + profile: minimal + override: true + - uses: Swatinem/rust-cache@v1 + with: + cache-on-failure: true + - name: doclint + id: build + continue-on-error: true + run: RUSTDOCFLAGS="-D warnings" cargo doc --all --no-deps --all-features --document-private-items + - name: doctest + run: cargo test --doc --all --all-features + diff --git a/crates/mipsevm/src/lib.rs b/crates/mipsevm/src/lib.rs index 4d628cf..6dc524d 100644 --- a/crates/mipsevm/src/lib.rs +++ b/crates/mipsevm/src/lib.rs @@ -1,7 +1,8 @@ // #![doc = include_str!("../README.md")] #![feature(generic_const_exprs)] +#![allow(incomplete_features, dead_code)] -mod memory; +mod page; mod state; mod traits; mod utils; diff --git a/crates/mipsevm/src/memory.rs b/crates/mipsevm/src/page.rs similarity index 80% rename from crates/mipsevm/src/memory.rs rename to crates/mipsevm/src/page.rs index 507bf14..9c0668c 100644 --- a/crates/mipsevm/src/memory.rs +++ b/crates/mipsevm/src/page.rs @@ -1,31 +1,31 @@ //! This module contains the data structure for a [Page] within the //! MIPS emulator's memory. -use crate::utils::concat_arrays; +use crate::utils::concat_fixed; use alloy_primitives::{keccak256, B256}; use once_cell::sync::Lazy; -const PAGE_ADDRESS_SIZE: usize = 12; -const PAGE_KEY_SIZE: usize = 32 - PAGE_ADDRESS_SIZE; -const PAGE_SIZE: usize = 1 << PAGE_ADDRESS_SIZE; -const PAGE_ADDRESS_MASK: usize = PAGE_SIZE - 1; -const MAX_PAGE_COUNT: usize = 1 << PAGE_KEY_SIZE; -const PAGE_KEY_MASK: usize = MAX_PAGE_COUNT - 1; - -/// A [Page] is a portion of memory of size [PAGE_SIZE]. -type Page = [u8; PAGE_SIZE]; +pub(crate) const PAGE_ADDRESS_SIZE: usize = 12; +pub(crate) const PAGE_KEY_SIZE: usize = 32 - PAGE_ADDRESS_SIZE; +pub(crate) const PAGE_SIZE: usize = 1 << PAGE_ADDRESS_SIZE; +pub(crate) const PAGE_ADDRESS_MASK: usize = PAGE_SIZE - 1; +pub(crate) const MAX_PAGE_COUNT: usize = 1 << PAGE_KEY_SIZE; +pub(crate) const PAGE_KEY_MASK: usize = MAX_PAGE_COUNT - 1; /// Precomputed hashes of each full-zero range sub-tree level. -static ZERO_HASHES: Lazy<[B256; 256]> = Lazy::new(|| { +pub(crate) static ZERO_HASHES: Lazy<[B256; 256]> = Lazy::new(|| { let mut out = [B256::ZERO; 256]; for i in 1..256 { - out[i] = keccak256(concat_arrays(out[i - 1].into(), out[i - 1].into())) + out[i] = keccak256(concat_fixed(out[i - 1].into(), out[i - 1].into())) } out }); +/// A [Page] is a portion of memory of size [PAGE_SIZE]. +pub type Page = [u8; PAGE_SIZE]; + /// A [CachedPage] is a [Page] with an in-memory cache of intermediate nodes. -struct CachedPage { +pub struct CachedPage { data: Page, /// Storage for intermediate nodes cache: [[u8; 32]; PAGE_SIZE >> 5], @@ -71,7 +71,7 @@ impl CachedPage { pub fn merkle_root(&mut self) -> B256 { // First, hash the bottom layer. for i in (0..PAGE_SIZE).step_by(64) { - let j = PAGE_SIZE >> 5 + i >> 5; + let j = (PAGE_SIZE >> 6) + (i >> 6); if self.valid[j] { continue; } @@ -81,12 +81,12 @@ impl CachedPage { } // Then, hash the cache layers. - for i in (1..=PAGE_SIZE >> 5 - 2).rev().step_by(2) { + for i in (1..=(PAGE_SIZE >> 5) - 2).rev().step_by(2) { let j = i >> 1; if self.valid[j] { continue; } - self.cache[j] = *keccak256(concat_arrays(self.cache[i], self.cache[i + 1])); + self.cache[j] = *keccak256(concat_fixed(self.cache[i], self.cache[i + 1])); self.valid[j] = true; } @@ -98,12 +98,12 @@ impl CachedPage { let _ = self.merkle_root(); if g_index >= PAGE_SIZE >> 5 { - if g_index >= PAGE_SIZE >> 5 * 2 { + if g_index >= (PAGE_SIZE >> 5) * 2 { panic!("Gindex too deep"); } let node_index = g_index & (PAGE_ADDRESS_MASK >> 5); - return B256::from_slice(&self.data[(node_index << 5)..(node_index << 5 + 32)]); + return B256::from_slice(&self.data[node_index << 5..(node_index << 5) + 32]); } self.cache[g_index].into() @@ -126,12 +126,12 @@ mod test { assert_eq!(node, expected_leaf, "Leaf nodes should not be hashed"); let node = page.merklize_subtree(g_index >> 1); - let expected_parent = keccak256(concat_arrays(ZERO_HASHES[0].into(), expected_leaf.into())); + let expected_parent = keccak256(concat_fixed(ZERO_HASHES[0].into(), expected_leaf.into())); assert_eq!(node, expected_parent, "Parent should be correct"); let node = page.merklize_subtree(g_index >> 2); let expected_grandparent = - keccak256(concat_arrays(expected_parent.into(), ZERO_HASHES[1].into())); + keccak256(concat_fixed(expected_parent.into(), ZERO_HASHES[1].into())); assert_eq!(node, expected_grandparent, "Grandparent should be correct"); let pre = page.merkle_root(); diff --git a/crates/mipsevm/src/utils.rs b/crates/mipsevm/src/utils.rs index 3d35d13..394221a 100644 --- a/crates/mipsevm/src/utils.rs +++ b/crates/mipsevm/src/utils.rs @@ -2,7 +2,7 @@ /// Concatenate two fixed sized arrays together into a new array with minimal reallocation. #[inline(always)] -pub(crate) fn concat_arrays(a: [T; N], b: [T; M]) -> [T; N + M] +pub(crate) fn concat_fixed(a: [T; N], b: [T; M]) -> [T; N + M] where T: Copy + Default, {