Skip to content

Commit 7ca79de

Browse files
Stavbegabrielbosio
authored andcommitted
stav: extract information for the prover from the runner (#2001)
1 parent 1ebb0b8 commit 7ca79de

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
* feat: replace `thiserror-no-std` with `thiserror 2` [#1919](https://github.com/lambdaclass/cairo-vm/pull/1919)
88

9+
* feat: Add `ProverInfo` and extract the relevant information for it from the runner [#2001](https://github.com/lambdaclass/cairo-vm/pull/2001)
10+
911
#### [2.0.1] - 2025-03-17
1012

1113
* feat: Limited padding of builtin segments to >=16 [#1981](https://github.com/lambdaclass/cairo-vm/pull/1981)

vm/src/vm/runners/cairo_runner.rs

Lines changed: 149 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
types::{builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName},
1212
vm::{
1313
runners::builtin_runner::SegmentArenaBuiltinRunner,
14-
trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry},
14+
trace::trace_entry::{relocate_trace_register, RelocatedTraceEntry, TraceEntry},
1515
},
1616
Felt252,
1717
};
@@ -1483,6 +1483,67 @@ impl CairoRunner {
14831483
})
14841484
.collect()
14851485
}
1486+
1487+
/// Collects relevant information for the prover from the runner, including the
1488+
/// relocatable form of the trace, memory, public memory, and built-ins.
1489+
pub fn get_prover_input_info(&self) -> Result<ProverInputInfo, RunnerError> {
1490+
let relocatable_trace = self
1491+
.vm
1492+
.trace
1493+
.as_ref()
1494+
.ok_or(RunnerError::Trace(TraceError::TraceNotEnabled))?
1495+
.clone();
1496+
1497+
let relocatable_memory = self
1498+
.vm
1499+
.segments
1500+
.memory
1501+
.data
1502+
.iter()
1503+
.map(|segment| segment.iter().filter_map(|cell| cell.get_value()).collect())
1504+
.collect();
1505+
1506+
let public_memory_offsets = self
1507+
.vm
1508+
.segments
1509+
.public_memory_offsets
1510+
.iter()
1511+
.map(|(segment, offset_page)| {
1512+
let offsets: Vec<usize> = offset_page.iter().map(|(offset, _)| *offset).collect();
1513+
(*segment, offsets)
1514+
})
1515+
.collect();
1516+
1517+
let builtins_segments = self
1518+
.get_builtin_segment_info_for_pie()?
1519+
.into_iter()
1520+
.map(|(name, info)| (info.index as usize, name))
1521+
.collect();
1522+
1523+
Ok(ProverInputInfo {
1524+
relocatable_trace,
1525+
relocatable_memory,
1526+
public_memory_offsets,
1527+
builtins_segments,
1528+
})
1529+
}
1530+
}
1531+
1532+
//* ----------------------
1533+
//* ProverInputInfo
1534+
//* ----------------------
1535+
/// This struct contains all relevant data for the prover.
1536+
/// All addresses are relocatable.
1537+
#[derive(Deserialize, Serialize)]
1538+
pub struct ProverInputInfo {
1539+
/// A vector of trace entries, i.e. pc, ap, fp, where pc is relocatable.
1540+
pub relocatable_trace: Vec<TraceEntry>,
1541+
/// A vector of segments, where each segment is a vector of maybe relocatable values.
1542+
pub relocatable_memory: Vec<Vec<MaybeRelocatable>>,
1543+
/// A map from segment index to a vector of offsets within the segment, representing the public memory addresses.
1544+
pub public_memory_offsets: HashMap<usize, Vec<usize>>,
1545+
/// A map from the builtin segment index into its name.
1546+
pub builtins_segments: HashMap<usize, BuiltinName>,
14861547
}
14871548

14881549
#[derive(Clone, Debug, Eq, PartialEq)]
@@ -5439,4 +5500,91 @@ mod tests {
54395500
_ => panic!("Expected DisableTracePaddingWithoutProofMode error"),
54405501
}
54415502
}
5503+
5504+
#[test]
5505+
fn get_prover_input_info() {
5506+
let program_content =
5507+
include_bytes!("../../../../cairo_programs/proof_programs/common_signature.json");
5508+
let runner = crate::cairo_run::cairo_run(
5509+
program_content,
5510+
&CairoRunConfig {
5511+
trace_enabled: true,
5512+
layout: LayoutName::all_cairo,
5513+
..Default::default()
5514+
},
5515+
&mut BuiltinHintProcessor::new_empty(),
5516+
)
5517+
.unwrap();
5518+
let prover_info = runner.get_prover_input_info().unwrap();
5519+
let expected_trace = vec![
5520+
TraceEntry {
5521+
pc: (0, 15).into(),
5522+
ap: 3,
5523+
fp: 3,
5524+
},
5525+
TraceEntry {
5526+
pc: (0, 16).into(),
5527+
ap: 4,
5528+
fp: 3,
5529+
},
5530+
TraceEntry {
5531+
pc: (0, 18).into(),
5532+
ap: 5,
5533+
fp: 3,
5534+
},
5535+
TraceEntry {
5536+
pc: (0, 20).into(),
5537+
ap: 6,
5538+
fp: 3,
5539+
},
5540+
TraceEntry {
5541+
pc: (0, 22).into(),
5542+
ap: 7,
5543+
fp: 3,
5544+
},
5545+
TraceEntry {
5546+
pc: (0, 24).into(),
5547+
ap: 8,
5548+
fp: 3,
5549+
},
5550+
TraceEntry {
5551+
pc: (0, 10).into(),
5552+
ap: 10,
5553+
fp: 10,
5554+
},
5555+
TraceEntry {
5556+
pc: (0, 11).into(),
5557+
ap: 10,
5558+
fp: 10,
5559+
},
5560+
TraceEntry {
5561+
pc: (0, 12).into(),
5562+
ap: 10,
5563+
fp: 10,
5564+
},
5565+
TraceEntry {
5566+
pc: (0, 14).into(),
5567+
ap: 11,
5568+
fp: 10,
5569+
},
5570+
TraceEntry {
5571+
pc: (0, 26).into(),
5572+
ap: 11,
5573+
fp: 3,
5574+
},
5575+
];
5576+
let expected_in_memory_0_3 = MaybeRelocatable::Int(13.into());
5577+
let expected_in_memory_1_0 = MaybeRelocatable::RelocatableValue(Relocatable {
5578+
segment_index: 2,
5579+
offset: 0,
5580+
});
5581+
assert_eq!(prover_info.relocatable_trace, expected_trace);
5582+
assert_eq!(prover_info.relocatable_memory[0][3], expected_in_memory_0_3);
5583+
assert_eq!(prover_info.relocatable_memory[1][0], expected_in_memory_1_0);
5584+
assert!(prover_info.public_memory_offsets.is_empty());
5585+
assert_eq!(
5586+
prover_info.builtins_segments,
5587+
HashMap::from([(2, BuiltinName::ecdsa)])
5588+
);
5589+
}
54425590
}

0 commit comments

Comments
 (0)