openvm_stark_backend/prover/
types.rs1use std::{cmp::Reverse, sync::Arc};
2
3use derivative::Derivative;
4
5use crate::{
6 keygen::types::{
7 LinearConstraint, MultiStarkVerifyingKey, MultiStarkVerifyingKey0, StarkVerifyingKey,
8 },
9 proof::TraceVData,
10 prover::{MatrixDimensions, ProverBackend},
11 StarkProtocolConfig, SystemParams,
12};
13
14#[derive(Derivative)]
17#[derivative(Clone(bound = "PB::Matrix: Clone"))]
18pub struct CommittedTraceData<PB: ProverBackend> {
19 pub commitment: PB::Commitment,
21 pub trace: PB::Matrix,
23 pub data: Arc<PB::PcsData>,
25}
26
27#[derive(derive_new::new)]
32pub struct DeviceMultiStarkProvingKey<PB: ProverBackend> {
33 pub per_air: Vec<DeviceStarkProvingKey<PB>>,
34 pub trace_height_constraints: Vec<LinearConstraint>,
35 pub max_constraint_degree: usize,
37 pub params: SystemParams,
38 pub vk_pre_hash: PB::Commitment,
39}
40
41pub struct DeviceStarkProvingKey<PB: ProverBackend> {
44 pub air_name: String,
46 pub vk: StarkVerifyingKey<PB::Val, PB::Commitment>,
47 pub preprocessed_data: Option<CommittedTraceData<PB>>,
49 pub other_data: PB::OtherAirData,
50}
51
52#[derive(derive_new::new)]
53pub struct ProvingContext<PB: ProverBackend> {
54 pub per_trace: Vec<(usize, AirProvingContext<PB>)>,
57}
58
59#[derive(derive_new::new)]
60pub struct AirProvingContext<PB: ProverBackend> {
61 pub cached_mains: Vec<CommittedTraceData<PB>>,
69 pub common_main: PB::Matrix,
71 pub public_values: Vec<PB::Val>,
73}
74
75pub struct HostProof<SC: StarkProtocolConfig, PB: ProverBackend, ConstraintsProof, OpeningProof> {
77 pub common_main_commit: PB::Commitment,
79
80 pub trace_vdata: Vec<Option<TraceVData<SC>>>,
84
85 pub public_values: Vec<Vec<PB::Val>>,
88
89 pub constraints_proof: ConstraintsProof,
90 pub opening_proof: OpeningProof,
92}
93
94impl<PB: ProverBackend> CommittedTraceData<PB> {
95 #[inline(always)]
96 pub fn height(&self) -> usize {
97 self.trace.height()
98 }
99}
100
101impl<PB: ProverBackend> DeviceMultiStarkProvingKey<PB> {
102 pub fn get_vk<SC>(&self) -> MultiStarkVerifyingKey<SC>
103 where
104 SC: StarkProtocolConfig<F = PB::Val, Digest = PB::Commitment>,
105 {
106 let per_air = self.per_air.iter().map(|pk| pk.vk.clone()).collect();
107 let inner = MultiStarkVerifyingKey0 {
108 params: self.params.clone(),
109 per_air,
110 trace_height_constraints: self.trace_height_constraints.clone(),
111 };
112 MultiStarkVerifyingKey {
113 inner,
114 pre_hash: self.vk_pre_hash.clone(),
115 }
116 }
117}
118
119impl<PB: ProverBackend> IntoIterator for ProvingContext<PB> {
120 type Item = (usize, AirProvingContext<PB>);
121 type IntoIter = std::vec::IntoIter<Self::Item>;
122
123 fn into_iter(self) -> Self::IntoIter {
124 self.per_trace.into_iter()
125 }
126}
127
128impl<PB: ProverBackend> ProvingContext<PB> {
129 pub fn common_main_traces(&self) -> impl Iterator<Item = (usize, &PB::Matrix)> {
130 self.per_trace
131 .iter()
132 .map(|(air_idx, trace_ctx)| (*air_idx, &trace_ctx.common_main))
133 }
134
135 pub fn into_sorted(mut self) -> Self {
138 self.sort_for_stacking();
139 self
140 }
141
142 pub fn sort_for_stacking(&mut self) {
145 self.per_trace.sort_by_key(|(air_idx, trace_ctx)| {
146 (Reverse(trace_ctx.common_main.height()), *air_idx)
147 });
148 }
149}
150
151impl<PB: ProverBackend> AirProvingContext<PB> {
152 pub fn simple(common_main_trace: PB::Matrix, public_values: Vec<PB::Val>) -> Self {
153 Self::new(vec![], common_main_trace, public_values)
154 }
155 pub fn simple_no_pis(common_main_trace: PB::Matrix) -> Self {
156 Self::simple(common_main_trace, vec![])
157 }
158
159 pub fn height(&self) -> usize {
161 self.common_main.height()
162 }
163}