Skip to content

Commit

Permalink
Implement simple zkasm runner in Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
aborg-dev committed Feb 8, 2024
1 parent 283aeb1 commit 6244bf2
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion cranelift/filetests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ anyhow = { workspace = true }
similar = { workspace = true }
wat.workspace = true
toml = { workspace = true }
serde = { workspace = true }
serde.workspace = true
serde_derive = { workspace = true }
cranelift-wasm.workspace = true
wasmparser.workspace = true
Expand All @@ -42,3 +42,5 @@ smallvec = { workspace = true }
regex = { workspace = true }
wasmtime = { workspace = true, features = ["cranelift"] }
walkdir = { workspace = true }
tempfile.workspace = true
serde_json.workspace = true
1 change: 1 addition & 0 deletions cranelift/filetests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod match_directive;
mod runner;
mod runone;
mod subtest;
mod zkasm_runner;

mod test_alias_analysis;
mod test_cat;
Expand Down
3 changes: 3 additions & 0 deletions cranelift/filetests/src/test_zkasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ mod tests {
use walkdir::WalkDir;
use wasmtime::*;

use crate::zkasm_runner::run_zkasm;

fn setup() {
let _ = env_logger::builder().is_test(true).try_init();
}
Expand Down Expand Up @@ -380,6 +382,7 @@ mod tests {
.join(format!("generated/{name}.zkasm"))];
let result = std::panic::catch_unwind(|| {
let program = generate_zkasm(&module_binary);
run_zkasm(&program).unwrap();
expected.assert_eq(&program);
});
if let Err(err) = result {
Expand Down
61 changes: 61 additions & 0 deletions cranelift/filetests/src/zkasm_runner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use serde_derive::Deserialize;
use std::io::Read;
use std::path::Path;
use std::task::Wake;
use tempfile::{NamedTempFile, TempDir};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Counters {
cnt_arith: String,
cnt_binary: String,
cnt_keccak_f: String,
cnt_mem_align: String,
cnt_steps: u64,
}

#[derive(Deserialize)]
pub struct ExecutionResult {
path: String,
status: String,
counters: Option<Counters>,
}

pub fn run_zkasm(contents: &str) -> anyhow::Result<Vec<ExecutionResult>> {
let tmp_dir = TempDir::new()?;
let zkasm_file = tmp_dir.path().join("code.zkasm");
std::fs::write(&zkasm_file, contents)?;
run_zkasm_path(&zkasm_file)
}

pub fn run_zkasm_path(input_path: &Path) -> anyhow::Result<Vec<ExecutionResult>> {
let dir_path = if input_path.is_dir() {
input_path
} else {
input_path.parent().unwrap()
};
std::fs::create_dir(dir_path.join("helpers"))?;
let helpers_file = dir_path.join("helpers/2-exp.zkasm");
std::fs::write(
helpers_file,
include_str!("../../zkasm_data/generated/helpers/2-exp.zkasm"),
)?;

let mut output_file = NamedTempFile::new()?;
let output = std::process::Command::new("npm")
.args([
"--prefix",
"../../tests/zkasm",
"test",
input_path.to_str().unwrap(),
output_file.path().to_str().unwrap(),
])
.output()?;
let mut buf = String::new();
output_file.read_to_string(&mut buf)?;
if buf.is_empty() {
return Ok(Vec::new());
}
let execution_results = serde_json::from_str(&buf)?;
Ok(execution_results)
}

0 comments on commit 6244bf2

Please sign in to comment.