use std::cell::RefCell;
use std::rc::Rc;
use triton_vm::prelude::*;
use crate::library::Library;
use crate::prove_and_verify;
use crate::snippet_bencher::BenchmarkResult;
use crate::traits::basic_snippet::BasicSnippet;
use crate::VmHasher;
pub fn link_for_isolated_run<T: BasicSnippet>(snippet: Rc<RefCell<T>>) -> Vec<LabelledInstruction> {
let mut snippet_state = Library::new();
let entrypoint = snippet.borrow().entrypoint();
let function_body = snippet.borrow().annotated_code(&mut snippet_state);
let library_code = snippet_state.all_imports();
let code = triton_asm!(
call {entrypoint}
halt
{&function_body}
{&library_code}
);
code
}
pub fn execute_bench(
code: &[LabelledInstruction],
stack: &[BFieldElement],
std_in: Vec<BFieldElement>,
nondeterminism: NonDeterminism,
sponge: Option<VmHasher>,
) -> BenchmarkResult {
let program = Program::new(code);
let public_input = PublicInput::new(std_in.clone());
let mut vm_state = VMState::new(
program.clone(),
public_input.clone(),
nondeterminism.clone(),
);
vm_state.op_stack.stack = stack.to_vec();
vm_state.sponge = sponge;
let (simulation_trace, _) = VM::trace_execution_of_state(vm_state).unwrap();
if std::env::var("DYING_TO_PROVE").is_ok() {
prove_and_verify(program, &std_in, &nondeterminism, Some(stack.to_vec()));
}
BenchmarkResult::new(&simulation_trace)
}