Skip to content

Commit

Permalink
feat: add program analysis tool
Browse files Browse the repository at this point in the history
cleanup

print number of program blocks in the script

change path to string

refactor

refactor

more refactor based on review

remove debug

rebase next
  • Loading branch information
tohrnii committed May 23, 2022
1 parent 98bb53c commit 945e478
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ members = [
"miden",
"processor",
"stdlib",
"tools",
"verifier"
]

Expand Down
1 change: 1 addition & 0 deletions miden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub use assembly::{Assembler, AssemblyError};
pub use prover::StarkProof;
pub use verifier::{verify, VerificationError};
pub use vm_core::{program::Script, ProgramInputs};
pub mod tools;

// EXECUTOR
// ================================================================================================
Expand Down
41 changes: 41 additions & 0 deletions miden/src/tools/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use assembly::Assembler;
use vm_core::ProgramInputs;

#[derive(Debug, PartialEq)]
pub struct ProgramInfo {
total_vm_cycles: usize,
}

impl ProgramInfo {
///Creates a new ProgramInfo object
pub fn new(total_vm_cycles: usize) -> ProgramInfo {
Self { total_vm_cycles }
}

///Get total vm cycles to execute a program
pub fn total_vm_cycles(&self) -> usize {
self.total_vm_cycles
}
}

/// Returns program analysis for a given script string. Returns ProgramInfo with following fields:
/// - total_vm_cycles (vm cycles it takes to execute the entire script)
pub fn analyze(program: String) -> ProgramInfo {
let assembler = Assembler::new();
let script = assembler.compile_script(&program).unwrap();
let inputs = ProgramInputs::none();
let count = processor::execute_iter(&script, &inputs).count() - 1;
ProgramInfo::new(count)
}

#[cfg(test)]
mod tests {

#[test]
fn analyze_test() {
let source = "proc.foo.1 pop.local.0 end begin popw.mem.1 push.17 exec.foo end";
let program_info: super::ProgramInfo = super::analyze(source.to_string());
let expected_program_info = super::ProgramInfo::new(24);
assert_eq!(program_info, expected_program_info);
}
}
20 changes: 20 additions & 0 deletions tools/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "miden-vm-tools"
version = "0.1.0"
description = "Tools for working with Miden VM"
authors = ["miden contributors"]
readme = "README.md"
license = "MIT"
repository = "https://github.com/maticnetwork/miden"
edition = "2021"
rust-version = "1.60"

[dependencies]
miden = { path = "../miden", version = "0.2", default-features = false }
structopt = { version = "0.3", default-features = false }

[[bin]]
name = "miden-vm-tools"
path = "src/main.rs"
bench = false
doctest = false
17 changes: 17 additions & 0 deletions tools/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use miden::tools::{analyze, ProgramInfo};
use structopt::StructOpt;

#[derive(StructOpt)]
struct Args {
/// Script Source File Path
path: String,
}

fn main() {
let args = Args::from_args();
//reads input masm file
let program = std::fs::read_to_string(&args.path).expect("Could not read source file");
let program_info: ProgramInfo = analyze(program);
let total_vm_cycles = program_info.total_vm_cycles();
println!("Total Number of VM Cycles: {total_vm_cycles}");
}

0 comments on commit 945e478

Please sign in to comment.