Skip to content

Commit 26c022a

Browse files
committed
fix #73
1 parent b7879da commit 26c022a

File tree

7 files changed

+112
-86
lines changed

7 files changed

+112
-86
lines changed

crates/lean_compiler/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn compile_and_run(
4747
program,
4848
&function_locations,
4949
no_vec_runtime_memory,
50-
profiler,
50+
(profiler, true),
5151
);
5252
}
5353

crates/lean_prover/src/prove_execution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pub fn prove_execution(
5353
source_code,
5454
function_locations,
5555
no_vec_runtime_memory,
56-
vm_profiler,
56+
(vm_profiler, false),
5757
);
5858
get_execution_trace(bytecode, execution_result)
5959
});

crates/lean_prover/tests/test_zkvm.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ use lean_prover::{
55
use lean_vm::*;
66
use p3_field::PrimeCharacteristicRing;
77

8+
const NO_VEC_RUNTIME_MEMORY: usize = 1 << 20;
9+
810
#[test]
911
fn test_zk_vm() {
1012
let program_str = r#"
@@ -87,7 +89,7 @@ fn test_zk_vm() {
8789
&function_locations,
8890
(&public_input, &private_input),
8991
whir_config_builder(),
90-
1 << 20,
92+
NO_VEC_RUNTIME_MEMORY,
9193
false,
9294
)
9395
.0;

crates/lean_vm/src/execution/runner.rs

Lines changed: 77 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,20 @@ pub fn execute_bytecode(
6868
source_code: &str,
6969
function_locations: &BTreeMap<usize, String>,
7070
no_vec_runtime_memory: usize, // size of the "non-vectorized" runtime memory
71-
profiler: bool,
71+
(profiler, display_std_out_and_stats): (bool, bool),
7272
) -> ExecutionResult {
7373
let mut std_out = String::new();
7474
let mut instruction_history = ExecutionHistory::new();
7575
let result = execute_bytecode_helper(
7676
bytecode,
7777
public_input,
7878
private_input,
79-
no_vec_runtime_memory,
8079
&mut std_out,
8180
&mut instruction_history,
82-
profiler,
8381
function_locations,
82+
no_vec_runtime_memory,
83+
profiler,
84+
display_std_out_and_stats,
8485
)
8586
.unwrap_or_else(|err| {
8687
let lines_history = &instruction_history.lines;
@@ -152,11 +153,12 @@ fn execute_bytecode_helper(
152153
bytecode: &Bytecode,
153154
public_input: &[F],
154155
private_input: &[F],
155-
no_vec_runtime_memory: usize,
156156
std_out: &mut String,
157157
instruction_history: &mut ExecutionHistory,
158-
profiler: bool,
159158
function_locations: &BTreeMap<usize, String>,
159+
no_vec_runtime_memory: usize,
160+
profiler: bool,
161+
display_std_out_and_stats: bool,
160162
) -> Result<ExecutionResult, RunnerError> {
161163
// set public memory
162164
let mut memory = Memory::new(build_public_memory(public_input));
@@ -254,87 +256,90 @@ fn execute_bytecode_helper(
254256

255257
let no_vec_runtime_memory = ap - initial_ap;
256258

257-
// Ensure event counts match call counts in final execution
258259
if profiler {
259260
let report = crate::diagnostics::profiling_report(instruction_history, function_locations);
260261
println!("\n{report}");
261262
}
262-
if !std_out.is_empty() {
263-
println!("╔═════════════════════════════════════════════════════════════════════════╗");
264-
println!("║ STD-OUT ║");
265-
println!("╚═════════════════════════════════════════════════════════════════════════╝");
266-
print!("\n{std_out}");
267-
println!("──────────────────────────────────────────────────────────────────────────\n");
268-
}
269-
270-
println!("╔═════════════════════════════════════════════════════════════════════════╗");
271-
println!("║ STATS ║");
272-
println!("╚═════════════════════════════════════════════════════════════════════════╝\n");
263+
if display_std_out_and_stats {
264+
if !std_out.is_empty() {
265+
println!("╔═════════════════════════════════════════════════════════════════════════╗");
266+
println!("║ STD-OUT ║");
267+
println!("╚═════════════════════════════════════════════════════════════════════════╝");
268+
print!("\n{std_out}");
269+
println!(
270+
"──────────────────────────────────────────────────────────────────────────\n"
271+
);
272+
}
273273

274-
println!("CYCLES: {}", pretty_integer(cpu_cycles));
275-
println!("MEMORY: {}", pretty_integer(memory.0.len()));
276-
println!();
274+
println!("╔═════════════════════════════════════════════════════════════════════════╗");
275+
println!("║ STATS ║");
276+
println!("╚═════════════════════════════════════════════════════════════════════════╝\n");
277277

278-
let runtime_memory_size = memory.0.len() - (PUBLIC_INPUT_START + public_input.len());
279-
println!(
280-
"Bytecode size: {}",
281-
pretty_integer(bytecode.instructions.len())
282-
);
283-
println!("Public input size: {}", pretty_integer(public_input.len()));
284-
println!(
285-
"Private input size: {}",
286-
pretty_integer(private_input.len())
287-
);
288-
println!(
289-
"Runtime memory: {} ({:.2}% vec) (no vec mem: {})",
290-
pretty_integer(runtime_memory_size),
291-
(VECTOR_LEN * (ap_vec - initial_ap_vec)) as f64 / runtime_memory_size as f64 * 100.0,
292-
no_vec_runtime_memory
293-
);
294-
let used_memory_cells = memory
295-
.0
296-
.iter()
297-
.skip(PUBLIC_INPUT_START + public_input.len())
298-
.filter(|&&x| x.is_some())
299-
.count();
300-
println!(
301-
"Memory usage: {:.1}%",
302-
used_memory_cells as f64 / runtime_memory_size as f64 * 100.0
303-
);
278+
println!("CYCLES: {}", pretty_integer(cpu_cycles));
279+
println!("MEMORY: {}", pretty_integer(memory.0.len()));
280+
println!();
304281

305-
println!();
306-
307-
if poseidons_16.len() + poseidons_24.len() > 0 {
282+
let runtime_memory_size = memory.0.len() - (PUBLIC_INPUT_START + public_input.len());
308283
println!(
309-
"Poseidon2_16 calls: {}, Poseidon2_24 calls: {} (1 poseidon per {} instructions)",
310-
pretty_integer(poseidons_16.len()),
311-
pretty_integer(poseidons_24.len()),
312-
cpu_cycles / (poseidons_16.len() + poseidons_24.len())
284+
"Bytecode size: {}",
285+
pretty_integer(bytecode.instructions.len())
313286
);
314-
}
315-
if !dot_products.is_empty() {
316-
println!("DotProduct calls: {}", pretty_integer(dot_products.len()));
317-
}
318-
if !multilinear_evals.is_empty() {
287+
println!("Public input size: {}", pretty_integer(public_input.len()));
319288
println!(
320-
"MultilinearEval calls: {}",
321-
pretty_integer(multilinear_evals.len())
289+
"Private input size: {}",
290+
pretty_integer(private_input.len())
322291
);
323-
}
324-
325-
if false {
326-
println!("Low level instruction counts:");
327292
println!(
328-
"COMPUTE: {} ({} ADD, {} MUL)",
329-
add_counts + mul_counts,
330-
add_counts,
331-
mul_counts
293+
"Runtime memory: {} ({:.2}% vec) (no vec mem: {})",
294+
pretty_integer(runtime_memory_size),
295+
(VECTOR_LEN * (ap_vec - initial_ap_vec)) as f64 / runtime_memory_size as f64 * 100.0,
296+
no_vec_runtime_memory
332297
);
333-
println!("DEREF: {deref_counts}");
334-
println!("JUMP: {jump_counts}");
335-
}
298+
let used_memory_cells = memory
299+
.0
300+
.iter()
301+
.skip(PUBLIC_INPUT_START + public_input.len())
302+
.filter(|&&x| x.is_some())
303+
.count();
304+
println!(
305+
"Memory usage: {:.1}%",
306+
used_memory_cells as f64 / runtime_memory_size as f64 * 100.0
307+
);
308+
309+
println!();
336310

337-
println!("──────────────────────────────────────────────────────────────────────────\n");
311+
if poseidons_16.len() + poseidons_24.len() > 0 {
312+
println!(
313+
"Poseidon2_16 calls: {}, Poseidon2_24 calls: {} (1 poseidon per {} instructions)",
314+
pretty_integer(poseidons_16.len()),
315+
pretty_integer(poseidons_24.len()),
316+
cpu_cycles / (poseidons_16.len() + poseidons_24.len())
317+
);
318+
}
319+
if !dot_products.is_empty() {
320+
println!("DotProduct calls: {}", pretty_integer(dot_products.len()));
321+
}
322+
if !multilinear_evals.is_empty() {
323+
println!(
324+
"MultilinearEval calls: {}",
325+
pretty_integer(multilinear_evals.len())
326+
);
327+
}
328+
329+
if false {
330+
println!("Low level instruction counts:");
331+
println!(
332+
"COMPUTE: {} ({} ADD, {} MUL)",
333+
add_counts + mul_counts,
334+
add_counts,
335+
mul_counts
336+
);
337+
println!("DEREF: {deref_counts}");
338+
println!("JUMP: {jump_counts}");
339+
}
340+
341+
println!("──────────────────────────────────────────────────────────────────────────\n");
342+
}
338343

339344
Ok(ExecutionResult {
340345
no_vec_runtime_memory,

crates/lean_vm/tests/test_lean_vm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn run_program() -> (Bytecode, ExecutionResult) {
209209
"",
210210
&BTreeMap::new(),
211211
1 << 20,
212-
false,
212+
(false, true),
213213
);
214214
(bytecode, result)
215215
}

crates/rec_aggregation/src/recursion.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,29 @@ fn run_recursion_benchmark() -> RecursionBenchStats {
181181

182182
utils::init_tracing();
183183
let (bytecode, function_locations) = compile_program(&program_str);
184+
185+
// in practice we will precompute all the possible values
186+
// (depending on the number of recursions + the number of xmss signatures)
187+
// (or even better: find a linear relation)
188+
let no_vec_runtime_memory = execute_bytecode(
189+
&bytecode,
190+
&public_input,
191+
&[],
192+
&program_str,
193+
&function_locations,
194+
1 << 20,
195+
(false, true),
196+
)
197+
.no_vec_runtime_memory;
198+
184199
let time = Instant::now();
185200
let (proof_data, proof_size) = prove_execution(
186201
&bytecode,
187202
&program_str,
188203
&function_locations,
189204
(&public_input, &[]),
190205
whir_config_builder(),
191-
// in practice we will precompute all the possible values
192-
// (depending on the number of recursions + the number of xmss signatures)
193-
// (or even better: find a linear relation)
194-
256327,
206+
no_vec_runtime_memory,
195207
false,
196208
);
197209
let proving_time = time.elapsed();

crates/rec_aggregation/src/xmss_aggregate.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,23 @@ pub fn run_xmss_benchmark(n_public_keys: usize) -> XmssBenchStats {
257257
private_input.extend(F::zero_vec(LOG_LIFETIME.next_multiple_of(8) - LOG_LIFETIME));
258258
}
259259
let (bytecode, function_locations) = compile_program(&program_str);
260-
let time = Instant::now();
261260

262261
// in practice we will precompute all the possible values
263262
// (depending on the number of recursions + the number of xmss signatures)
264263
// (or even better: find a linear relation)
265-
let no_vec_runtime_memory = match (n_public_keys, INV_BITFIELD_DENSITY, LOG_LIFETIME) {
266-
(100, 1, 32) => 148329,
267-
(500, 1, 32) => 741529,
268-
_ => unimplemented!(),
269-
};
264+
let no_vec_runtime_memory = execute_bytecode(
265+
&bytecode,
266+
&public_input,
267+
&private_input,
268+
&program_str,
269+
&function_locations,
270+
1 << 20,
271+
(false, true),
272+
)
273+
.no_vec_runtime_memory;
274+
275+
let time = Instant::now();
276+
270277
let (proof_data, proof_size) = prove_execution(
271278
&bytecode,
272279
&program_str,

0 commit comments

Comments
 (0)