openvm-stark-backend 2.0.0

Multi-matrix STARK backend for the SWIRL proof system
Documentation
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::{cmp::max, collections::HashMap, sync::Arc};

use itertools::Itertools;
use p3_air::BaseAir;
use p3_field::{Field, PrimeCharacteristicRing};
use p3_util::log2_strict_usize;
use tracing::instrument;

use crate::{
    air_builders::symbolic::{
        get_symbolic_builder,
        symbolic_variable::{Entry, SymbolicVariable},
        SymbolicConstraintsDag, SymbolicExpressionNode,
    },
    hasher::MerkleHasher,
    keygen::types::{
        KeygenError, LinearConstraint, MultiStarkProvingKey, MultiStarkVerifyingKey0,
        StarkProvingKey, StarkVerifyingKey, StarkVerifyingParams, TraceWidth,
        VerifierSinglePreprocessedData,
    },
    proof::CODEC_VERSION,
    prover::{
        stacked_pcs::{stacked_commit, StackedPcsData},
        ColMajorMatrix, MatrixDimensions,
    },
    AirRef, AnyAir, StarkProtocolConfig, SystemParams,
};

pub mod types;

struct AirKeygenBuilder<SC: StarkProtocolConfig> {
    pub is_required: bool,
    air: AirRef<SC>,
    prep_keygen_data: PrepKeygenData<SC>,
}

/// Stateful builder to create multi-stark proving and verifying keys
/// for system of multiple RAPs with multiple multi-matrix commitments
pub struct MultiStarkKeygenBuilder<SC: StarkProtocolConfig> {
    pub config: SC,
    /// Information for partitioned AIRs.
    partitioned_airs: Vec<AirKeygenBuilder<SC>>,
}

impl<SC: StarkProtocolConfig> MultiStarkKeygenBuilder<SC> {
    pub fn new(config: SC) -> Self {
        Self {
            config,
            partitioned_airs: vec![],
        }
    }

    pub fn params(&self) -> &SystemParams {
        self.config.params()
    }

    /// Default way to add a single Interactive AIR.
    /// Returns `air_id`
    pub fn add_air(&mut self, air: AirRef<SC>) -> usize {
        self.add_air_impl(air, false)
    }

    pub fn add_required_air(&mut self, air: AirRef<SC>) -> usize {
        self.add_air_impl(air, true)
    }

    #[instrument(level = "debug", skip_all, fields(name = air.name(), is_required = is_required))]
    fn add_air_impl(&mut self, air: AirRef<SC>, is_required: bool) -> usize {
        self.partitioned_airs
            .push(AirKeygenBuilder::new(&self.config, air, is_required));
        self.partitioned_airs.len() - 1
    }

    /// Consume the builder and generate proving key.
    /// The verifying key can be obtained from the proving key.
    pub fn generate_pk(self) -> Result<MultiStarkProvingKey<SC>, KeygenError> {
        let params = self.params().clone();
        let max_constraint_degree = params.max_constraint_degree;
        let pk_per_air: Vec<_> = self
            .partitioned_airs
            .into_iter()
            .map(|keygen_builder| {
                // Second pass: get final constraints, where RAP phase constraints may have changed
                keygen_builder.generate_pk(max_constraint_degree)
            })
            .collect::<Result<Vec<_>, KeygenError>>()?;

        let mut air_max_constraint_degree = 0;
        #[allow(unused)]
        for (air_id, pk) in pk_per_air.iter().enumerate() {
            let width = &pk.vk.params.width;
            tracing::info!("{:<20} | Constraint Deg = {:<2} | Prep Cols = {:<2} | Main Cols = {:<8} | {:4} Constraints | {:3} Interactions",
                pk.air_name,
                pk.vk.max_constraint_degree,
                width.preprocessed.unwrap_or(0),
                format!("{:?}",width.main_widths()),
                pk.vk.symbolic_constraints.constraints.constraint_idx.len(),
                pk.vk.symbolic_constraints.interactions.len(),
            );
            air_max_constraint_degree = max(air_max_constraint_degree, pk.vk.max_constraint_degree);
            tracing::debug!(
                "On Buses {:?}",
                pk.vk
                    .symbolic_constraints
                    .interactions
                    .iter()
                    .map(|i| i.bus_index)
                    .collect_vec()
            );
            #[cfg(feature = "metrics")]
            {
                let labels = [
                    ("air_name", pk.air_name.clone()),
                    ("air_id", air_id.to_string()),
                ];
                metrics::counter!("constraint_deg", &labels)
                    .absolute(pk.vk.max_constraint_degree as u64);
                // column info will be logged by prover later
                metrics::counter!("constraints", &labels)
                    .absolute(pk.vk.symbolic_constraints.constraints.constraint_idx.len() as u64);
                metrics::counter!("interactions", &labels)
                    .absolute(pk.vk.symbolic_constraints.interactions.len() as u64);
                metrics::counter!("need_rot", &labels).absolute(pk.vk.params.need_rot as u64);
            }
        }
        if max_constraint_degree != air_max_constraint_degree as usize {
            tracing::info!(
            "Actual max constraint degree across all AIRs ({air_max_constraint_degree}) does not match configured max constraint degree ({max_constraint_degree})",
        );
        }

        let num_airs = pk_per_air.len();
        let base_order = SC::F::order().to_u32_digits()[0];
        let mut count_weight_per_air_per_bus_index = HashMap::new();

        let mut num_interactions_per_air: Vec<u32> = Vec::with_capacity(num_airs);
        // We compute the a_i's for the constraints of the form a_0 n_0 + ... + a_{k-1} n_{k-1} <
        // a_k, First the constraints that the total number of interactions on each bus is
        // at most the base field order.
        for (air_idx, pk) in pk_per_air.iter().enumerate() {
            let constraints = &pk.vk.symbolic_constraints;
            num_interactions_per_air.push(constraints.interactions.len().try_into().unwrap());
            for interaction in &constraints.interactions {
                // Also make sure that this of interaction is valid given the security params.
                // +1 because of the bus
                let max_msg_len = params.logup.max_message_length();
                // plus one because of the bus
                let total_message_length = interaction.message.len() + 1;
                assert!(
                    total_message_length <= max_msg_len,
                    "interaction message with bus has length {}, which is more than max {max_msg_len}",
                    total_message_length,
                );

                let b = interaction.bus_index;
                let constraint = count_weight_per_air_per_bus_index
                    .entry(b)
                    .or_insert_with(|| LinearConstraint {
                        coefficients: vec![0; num_airs],
                        threshold: base_order,
                    });
                constraint.coefficients[air_idx] += interaction.count_weight;
            }
        }

        let log_up_security_params = params.logup;

        // Collect all constraints: per-bus sorted by bus index, then global.
        let mut all_constraints: Vec<LinearConstraint> = count_weight_per_air_per_bus_index
            .into_iter()
            .sorted_by_key(|(bus_index, _)| *bus_index)
            .map(|(_, constraint)| constraint)
            .collect_vec();

        all_constraints.push(LinearConstraint {
            coefficients: num_interactions_per_air,
            threshold: log_up_security_params.max_interaction_count,
        });

        // Minimize: keep only constraints not implied by any other.
        let mut trace_height_constraints: Vec<LinearConstraint> = Vec::new();
        for constraint in all_constraints {
            if trace_height_constraints
                .iter()
                .any(|c| constraint.is_implied_by(c))
            {
                continue;
            }
            trace_height_constraints.retain(|c| !c.is_implied_by(&constraint));
            trace_height_constraints.push(constraint);
        }

        let pre_vk: MultiStarkVerifyingKey0<SC> = MultiStarkVerifyingKey0 {
            params: params.clone(),
            per_air: pk_per_air.iter().map(|pk| pk.vk.clone()).collect(),
            trace_height_constraints: trace_height_constraints.clone(),
        };
        // To protect against weak Fiat-Shamir, we hash the "pre"-verifying key and include it in
        // the final verifying key. This just needs to commit to the verifying key and does
        // not need to be verified by the verifier, so we just use postcard to serialize it.
        let vk_bytes = postcard::to_allocvec(&pre_vk).unwrap();
        tracing::debug!("pre-vkey: {} bytes", vk_bytes.len());
        // Since postcard serialization is not self-describing, we include a version for domain
        // separation. For further domain separation, we include the byte length.
        let versioned_vk_bytes = CODEC_VERSION
            .to_le_bytes()
            .into_iter()
            .chain((vk_bytes.len() as u64).to_le_bytes())
            .chain(vk_bytes);
        // Purely to get type compatibility and convenience, we hash using the native hash
        let vk_pre_hash = self.config.hasher().hash_slice(
            &versioned_vk_bytes
                .into_iter()
                .map(SC::F::from_u8)
                .collect_vec(),
        );

        Ok(MultiStarkProvingKey {
            params,
            per_air: pk_per_air,
            trace_height_constraints,
            max_constraint_degree,
            vk_pre_hash,
        })
    }
}

impl<SC: StarkProtocolConfig> AirKeygenBuilder<SC> {
    pub fn new(config: &SC, air: AirRef<SC>, is_required: bool) -> Self {
        let prep_keygen_data = PrepKeygenData::new(config.hasher(), config.params(), air.as_ref());
        Self {
            is_required,
            air,
            prep_keygen_data,
        }
    }

    /// `max_constraint_degree` is the global max constraint degree. If this AIR's constraint degree
    /// exceeds it, an error will be returned.
    pub fn generate_pk(
        self,
        max_constraint_degree: usize,
    ) -> Result<StarkProvingKey<SC>, KeygenError> {
        let air_name = self.air.name();

        let width = self.trace_width();
        if width.main_width() == 0 {
            return Err(KeygenError::AirWidthZero { name: air_name });
        }

        let symbolic_builder = get_symbolic_builder(self.air.as_ref(), &width);
        let num_public_values = symbolic_builder.num_public_values();

        let symbolic_constraints = symbolic_builder.constraints();
        if symbolic_constraints.constraints.is_empty()
            && symbolic_constraints.interactions.is_empty()
        {
            return Err(KeygenError::AirNoConstraintsOrInteractions { name: air_name });
        }
        if let Some(interaction_index) = symbolic_constraints
            .interactions
            .iter()
            .position(|interaction| interaction.message.is_empty())
        {
            return Err(KeygenError::InteractionMessageEmpty {
                name: air_name,
                interaction_index,
            });
        }
        let constraint_degree = symbolic_constraints.max_constraint_degree();
        if constraint_degree > max_constraint_degree {
            return Err(KeygenError::MaxConstraintDegreeExceeded {
                name: air_name.clone(),
                degree: constraint_degree,
                max_degree: max_constraint_degree,
            });
        }

        let Self {
            prep_keygen_data:
                PrepKeygenData {
                    verifier_data: preprocessed_vdata,
                    prover_data: prep_prover_data,
                },
            ..
        } = self;

        let dag = SymbolicConstraintsDag::from(symbolic_constraints);
        let max_rotation = dag.constraints.max_rotation();
        debug_assert!(max_rotation <= 1);
        let need_rot = max_rotation == 1;
        let vparams = StarkVerifyingParams {
            width,
            num_public_values,
            need_rot,
        };

        let unused_variables = find_unused_vars(&dag, &vparams.width, need_rot);
        let vk = StarkVerifyingKey {
            preprocessed_data: preprocessed_vdata,
            params: vparams,
            symbolic_constraints: dag,
            max_constraint_degree: constraint_degree
                .try_into()
                .expect("constraint degree should fit in u8"),
            is_required: self.is_required,
            unused_variables,
        };
        Ok(StarkProvingKey {
            air_name,
            vk,
            preprocessed_data: prep_prover_data,
        })
    }

    fn trace_width(&self) -> TraceWidth {
        TraceWidth {
            preprocessed: self.prep_keygen_data.width(),
            cached_mains: self.air.cached_main_widths(),
            common_main: self.air.common_main_width(),
        }
    }
}

pub(super) struct PrepKeygenData<SC: StarkProtocolConfig> {
    pub verifier_data: Option<VerifierSinglePreprocessedData<SC::Digest>>,
    pub prover_data: Option<Arc<StackedPcsData<SC::F, SC::Digest>>>,
}

impl<SC: StarkProtocolConfig> PrepKeygenData<SC> {
    fn new(hasher: &SC::Hasher, params: &SystemParams, air: &dyn AnyAir<SC>) -> Self {
        let preprocessed_trace = BaseAir::<SC::F>::preprocessed_trace(air);
        let vpdata_opt = preprocessed_trace.map(|trace| {
            let trace = ColMajorMatrix::from_row_major(&trace);
            let (commit, data) = stacked_commit(
                hasher,
                params.l_skip,
                params.n_stack,
                params.log_blowup,
                params.k_whir(),
                &[&trace],
            )
            .unwrap();
            debug_assert_eq!(trace.width(), data.mat_view(0).width());
            let vdata = VerifierSinglePreprocessedData {
                commit,
                hypercube_dim: log2_strict_usize(trace.height()) as isize - params.l_skip as isize,
                stacking_width: data.matrix.width(),
            };
            let pdata = Arc::new(data);
            (vdata, pdata)
        });
        if let Some((vdata, pdata)) = vpdata_opt {
            Self {
                prover_data: Some(pdata),
                verifier_data: Some(vdata),
            }
        } else {
            Self {
                prover_data: None,
                verifier_data: None,
            }
        }
    }

    fn width(&self) -> Option<usize> {
        self.prover_data.as_ref().map(|d| d.mat_view(0).width())
    }
}

pub(crate) fn find_unused_vars<F: Field>(
    constraints: &SymbolicConstraintsDag<F>,
    width: &TraceWidth,
    need_rot: bool,
) -> Vec<SymbolicVariable<F>> {
    let preprocessed_width = width.preprocessed.unwrap_or(0);
    let mut preprocessed_present = vec![vec![false; 2]; preprocessed_width];

    let mut main_present = vec![];
    for width in width.main_widths() {
        main_present.push(vec![vec![false; 2]; width]);
    }

    for node in &constraints.constraints.nodes {
        let SymbolicExpressionNode::Variable(var) = node else {
            continue;
        };

        match var.entry {
            Entry::Preprocessed { offset } => {
                preprocessed_present[var.index][offset] = true;
            }
            Entry::Main { part_index, offset } => {
                main_present[part_index][var.index][offset] = true;
            }
            Entry::Public => {}
            Entry::Challenge => unreachable!(),
        }
    }

    let mut missing = vec![];
    for (index, presents) in preprocessed_present.iter().enumerate() {
        for (offset, present) in presents.iter().enumerate() {
            if !present && (offset == 0 || need_rot) {
                missing.push(SymbolicVariable::new(Entry::Preprocessed { offset }, index));
            }
        }
    }
    for (part_index, present_per_part) in main_present.iter().enumerate() {
        for (index, presents) in present_per_part.iter().enumerate() {
            for (offset, present) in presents.iter().enumerate() {
                if !present && (offset == 0 || need_rot) {
                    missing.push(SymbolicVariable::new(
                        Entry::Main { part_index, offset },
                        index,
                    ));
                }
            }
        }
    }
    missing
}