tasm_lib/
linker.rs

1use triton_vm::prelude::*;
2
3use crate::prelude::*;
4use crate::prove_and_verify;
5use crate::snippet_bencher::BenchmarkResult;
6
7/// Execute a Triton-VM program and return its output and execution trace length
8pub fn execute_bench(
9    code: &[LabelledInstruction],
10    stack: &[BFieldElement],
11    std_in: Vec<BFieldElement>,
12    nondeterminism: NonDeterminism,
13    sponge: Option<Tip5>,
14) -> BenchmarkResult {
15    let program = Program::new(code);
16    let public_input = PublicInput::new(std_in.clone());
17
18    let mut vm_state = VMState::new(
19        program.clone(),
20        public_input.clone(),
21        nondeterminism.clone(),
22    );
23    vm_state.op_stack.stack = stack.to_vec();
24    vm_state.sponge = sponge;
25    let (simulation_trace, _) = VM::trace_execution_of_state(vm_state).unwrap();
26
27    // If this environment variable is set, all programs, including the code to prepare the state,
28    // will be proven and then verified.
29    // Notice that this is only done after the successful execution of the program above, so all
30    // produced proofs here should be valid.
31    // If you run this, make sure `opt-level` is set to 3.
32    if std::env::var("DYING_TO_PROVE").is_ok() {
33        prove_and_verify(program, &std_in, &nondeterminism, Some(stack.to_vec()));
34    }
35
36    BenchmarkResult::new(&simulation_trace)
37}