forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement simple zkasm runner in Rust
- Loading branch information
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |