Skip to content

Commit

Permalink
⌛ Add execution benches
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 24, 2023
1 parent 13aa111 commit 355bb18
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
6 changes: 5 additions & 1 deletion crates/mipsevm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.106"
fnv = "1.0.7"
elf = "0.7.2"
revm = { version = "3.3.0", features = ["no_gas_measuring"] }

# misc
anyhow = "1.0.75"
tracing = { version = "0.1.37", optional = true }

[dev-dependencies]
revm = { version = "3.3.0", features = ["no_gas_measuring"] }
rand = "0.8.5"
criterion = { version = "0.5.1", features = ["html_reports"] }

Expand All @@ -31,3 +31,7 @@ tracing = ["dep:tracing"]
[[bench]]
name = "memory"
harness = false

[[bench]]
name = "execution"
harness = false
54 changes: 54 additions & 0 deletions crates/mipsevm/benches/execution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use cannon_mipsevm::{
load_elf, patch_go, patch_stack,
test_utils::{ClaimTestOracle, StaticOracle},
InstrumentedState, PreimageOracle,
};
use criterion::{criterion_group, criterion_main, Bencher, Criterion};
use std::io::BufWriter;

fn bench_exec(
elf_bytes: &[u8],
oracle: impl PreimageOracle,
compute_witness: bool,
b: &mut Bencher,
) {
let mut state = load_elf(elf_bytes).unwrap();
patch_go(elf_bytes, &state).unwrap();
patch_stack(&mut state).unwrap();

let out = BufWriter::new(Vec::default());
let err = BufWriter::new(Vec::default());
let mut ins = InstrumentedState::new(state, oracle, out, err);

b.iter(|| loop {
if ins.state.exited {
break;
}
ins.step(compute_witness).unwrap();
})
}

fn execution(c: &mut Criterion) {
c.bench_function("[No Witness] Execution (hello.elf)", |b| {
let elf_bytes = include_bytes!("../../../example/bin/hello.elf");
bench_exec(elf_bytes, StaticOracle::default(), false, b);
});

c.bench_function("[Witness] Execution (hello.elf)", |b| {
let elf_bytes = include_bytes!("../../../example/bin/hello.elf");
bench_exec(elf_bytes, StaticOracle::default(), true, b);
});

c.bench_function("[No Witness] Execution (claim.elf)", |b| {
let elf_bytes = include_bytes!("../../../example/bin/claim.elf");
bench_exec(elf_bytes, ClaimTestOracle::default(), false, b);
});

c.bench_function("[Witness] Execution (claim.elf)", |b| {
let elf_bytes = include_bytes!("../../../example/bin/claim.elf");
bench_exec(elf_bytes, ClaimTestOracle::default(), true, b);
});
}

criterion_group!(benches, execution);
criterion_main!(benches);
3 changes: 1 addition & 2 deletions crates/mipsevm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ pub use mips::InstrumentedState;
mod patch;
pub use patch::{load_elf, patch_go, patch_stack, MultiReader};

#[cfg(test)]
mod test_utils;
pub mod test_utils;
6 changes: 6 additions & 0 deletions crates/mipsevm/src/test_utils/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ pub struct MipsEVM<DB: Database> {
pub inner: EVM<DB>,
}

impl Default for MipsEVM<CacheDB<EmptyDB>> {
fn default() -> Self {
Self::new()
}
}

impl MipsEVM<CacheDB<EmptyDB>> {
/// Creates a new MIPS EVM with an in-memory backend.
pub fn new() -> Self {
Expand Down
36 changes: 22 additions & 14 deletions crates/mipsevm/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use crate::{utils::concat_fixed, PreimageOracle};
use alloy_primitives::{hex, keccak256, B256};
use once_cell::sync::Lazy;
use preimage_oracle::{Keccak256Key, Key, LocalIndexKey};
use revm::primitives::HashMap;

Expand Down Expand Up @@ -46,14 +45,21 @@ impl ClaimTestOracle {
pub(crate) const S: u64 = 1000;
pub(crate) const A: u64 = 3;
pub(crate) const B: u64 = 4;
const DIFF: Lazy<[u8; 64]> = Lazy::new(|| {

pub fn diff() -> [u8; 64] {
concat_fixed(
keccak256(Self::A.to_be_bytes()).into(),
keccak256(Self::B.to_be_bytes()).into(),
)
});
const PRE_HASH: Lazy<B256> = Lazy::new(|| keccak256(Self::S.to_be_bytes()));
const DIFF_HASH: Lazy<B256> = Lazy::new(|| keccak256(Self::DIFF.as_slice()));
}

pub fn pre_hash() -> B256 {
keccak256(Self::S.to_be_bytes())
}

pub fn diff_hash() -> B256 {
keccak256(Self::diff().as_slice())
}
}

impl Default for ClaimTestOracle {
Expand All @@ -62,11 +68,13 @@ impl Default for ClaimTestOracle {
images: HashMap::new(),
};

s.images
.insert((0 as LocalIndexKey).preimage_key(), Self::PRE_HASH.to_vec());
s.images.insert(
(0 as LocalIndexKey).preimage_key(),
Self::pre_hash().to_vec(),
);
s.images.insert(
(1 as LocalIndexKey).preimage_key(),
Self::DIFF_HASH.to_vec(),
Self::diff_hash().to_vec(),
);
s.images.insert(
(2 as LocalIndexKey).preimage_key(),
Expand All @@ -80,7 +88,7 @@ impl Default for ClaimTestOracle {
impl PreimageOracle for ClaimTestOracle {
fn hint(&mut self, value: &[u8]) {
let s = String::from_utf8(value.to_vec()).unwrap();
let parts: Vec<&str> = s.split(" ").collect();
let parts: Vec<&str> = s.split(' ').collect();

assert_eq!(parts.len(), 2);

Expand All @@ -92,24 +100,24 @@ impl PreimageOracle for ClaimTestOracle {
"fetch-state" => {
assert_eq!(
hash,
*Self::PRE_HASH,
Self::pre_hash(),
"Expecting request for pre-state preimage"
);

self.images.insert(
(*Self::PRE_HASH as Keccak256Key).preimage_key(),
(Self::pre_hash() as Keccak256Key).preimage_key(),
Self::S.to_be_bytes().to_vec(),
);
}
"fetch-diff" => {
assert_eq!(
hash,
*Self::DIFF_HASH,
Self::diff_hash(),
"Expecting request for diff preimage"
);
self.images.insert(
(*Self::DIFF_HASH as Keccak256Key).preimage_key(),
Self::DIFF.to_vec(),
(Self::diff_hash() as Keccak256Key).preimage_key(),
Self::diff().to_vec(),
);
self.images.insert(
(keccak256(Self::A.to_be_bytes()) as Keccak256Key).preimage_key(),
Expand Down

0 comments on commit 355bb18

Please sign in to comment.