forked from Moonsong-Labs/cairo-bootloader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_program.rs
118 lines (107 loc) · 4.46 KB
/
run_program.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::collections::HashMap;
use std::error::Error;
use std::path::Path;
use std::rc::Rc;
use cairo_vm::cairo_run::{cairo_run_program_with_initial_scope, CairoRunConfig};
use cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintFunc;
use cairo_vm::hint_processor::builtin_hint_processor::hint_utils::insert_value_from_var_name;
use cairo_vm::types::exec_scope::ExecutionScopes;
use cairo_vm::types::layout_name::LayoutName;
use cairo_vm::types::program::Program;
use cairo_vm::vm::errors::cairo_run_errors::CairoRunError;
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
use cairo_vm::Felt252;
use cairo_bootloader::bootloaders::load_bootloader;
use cairo_bootloader::tasks::make_bootloader_tasks;
use cairo_bootloader::{
insert_bootloader_input, BootloaderConfig, BootloaderHintProcessor, BootloaderInput,
PackedOutput, SimpleBootloaderInput, TaskSpec,
};
fn cairo_run_bootloader_in_proof_mode(
bootloader_program: &Program,
tasks: Vec<TaskSpec>,
) -> Result<CairoRunner, CairoRunError> {
let mut hint_processor = BootloaderHintProcessor::new();
hint_processor.add_hint(
"ids.fibonacci_claim_index = program_input['fibonacci_claim_index']".to_string(),
Rc::new(HintFunc(Box::new(
|vm, exec_scopes, ids_data, ap_tracking, _constants| {
let program_input: HashMap<String, serde_json::Value> = exec_scopes
.get::<HashMap<String, serde_json::Value>>("program_input")
.unwrap()
.clone();
let fibonacci_claim_index: Felt252 = program_input
.get("fibonacci_claim_index")
.unwrap()
.as_u64()
.unwrap()
.into();
insert_value_from_var_name(
"fibonacci_claim_index",
fibonacci_claim_index,
vm,
ids_data,
ap_tracking,
)?;
Ok(())
},
))),
);
let cairo_run_config = CairoRunConfig {
entrypoint: "main",
trace_enabled: true,
relocate_mem: true,
layout: LayoutName::starknet_with_keccak,
proof_mode: true,
secure_run: None,
disable_trace_padding: false,
allow_missing_builtins: None,
..Default::default()
};
// Build the bootloader input
let n_tasks = tasks.len();
let bootloader_input = BootloaderInput {
simple_bootloader_input: SimpleBootloaderInput {
fact_topologies_path: None,
single_page: false,
tasks,
},
bootloader_config: BootloaderConfig {
simple_bootloader_program_hash: Felt252::from(0),
supported_cairo_verifier_program_hashes: vec![],
},
packed_outputs: vec![PackedOutput::Plain(vec![]); n_tasks],
ignore_fact_topologies: false,
};
// Note: the method used to set the bootloader input depends on
// https://github.com/lambdaclass/cairo-vm/pull/1772 and may change depending on review.
let mut exec_scopes = ExecutionScopes::new();
insert_bootloader_input(&mut exec_scopes, bootloader_input);
// Run the bootloader
cairo_run_program_with_initial_scope(
&bootloader_program,
&cairo_run_config,
&mut hint_processor,
exec_scopes,
)
}
fn main() -> Result<(), Box<dyn Error>> {
let bootloader_program = load_bootloader()?;
// let program_paths = vec![Path::new("./examples/fibonacci_with_hint.json")];
// let pie_paths = vec![Path::new(
// "./dependencies/test-programs/bootloader/pies/fibonacci/cairo_pie.zip",
// )];
let pie_paths = vec![Path::new("./examples/example.zip")];
// let program_inputs_path = Path::new("./examples/fibonacci_input.json");
// let program_inputs_str = std::fs::read_to_string(program_inputs_path)?;
// let program_inputs =
// serde_json::from_str::<HashMap<String, serde_json::Value>>(&program_inputs_str)?;
// let program_inputs = vec![program_inputs];
// let tasks = make_bootloader_tasks(Some(&program_paths), Some(&program_inputs), None)?;
let tasks = make_bootloader_tasks(None, None, Some(&pie_paths))?;
let mut runner = cairo_run_bootloader_in_proof_mode(&bootloader_program, tasks)?;
let mut output_buffer = "Program Output:\n".to_string();
runner.vm.write_output(&mut output_buffer)?;
print!("{output_buffer}");
Ok(())
}