openvm_native_recursion/
types.rs

1use openvm_native_compiler::{
2    asm::AsmConfig,
3    ir::{Config, DIGEST_SIZE},
4};
5use openvm_stark_backend::{
6    air_builders::symbolic::SymbolicExpressionDag,
7    config::{Com, StarkGenericConfig, Val},
8    keygen::types::{LinearConstraint, MultiStarkVerifyingKey, StarkVerifyingKey, TraceWidth},
9    p3_util::log2_strict_usize,
10};
11
12use crate::{
13    digest::DigestVal,
14    hints::{InnerChallenge, InnerVal},
15};
16
17pub type InnerConfig = AsmConfig<InnerVal, InnerChallenge>;
18
19/// Constants determined by AIRs.
20pub struct StarkVerificationAdvice<C: Config> {
21    /// Preprocessed trace data, if any
22    pub preprocessed_data: Option<VerifierSinglePreprocessedDataInProgram<C>>,
23    /// Trace sub-matrix widths
24    pub width: TraceWidth,
25    /// The factor to multiply the trace degree by to get the degree of the quotient polynomial.
26    /// Determined from the max constraint degree of the AIR constraints. This is equivalently
27    /// the number of chunks the quotient polynomial is split into.
28    pub quotient_degree: usize,
29    /// Number of public values for this STARK only
30    pub num_public_values: usize,
31    /// For only this RAP, how many challenges are needed in each trace challenge phase
32    pub num_challenges_to_sample: Vec<usize>,
33    /// Number of values to expose to verifier in each trace challenge phase
34    pub num_exposed_values_after_challenge: Vec<usize>,
35    /// Symbolic representation of all AIR constraints, including logup constraints
36    pub symbolic_constraints: SymbolicExpressionDag<C::F>,
37}
38
39/// Create StarkVerificationAdvice for an inner config.
40pub(crate) fn new_from_inner_vk<SC: StarkGenericConfig, C: Config<F = Val<SC>>>(
41    vk: StarkVerifyingKey<Val<SC>, Com<SC>>,
42) -> StarkVerificationAdvice<C>
43where
44    Com<SC>: Into<[C::F; DIGEST_SIZE]>,
45{
46    let StarkVerifyingKey {
47        preprocessed_data,
48        params,
49        quotient_degree,
50        symbolic_constraints,
51        rap_phase_seq_kind: _,
52    } = vk;
53    StarkVerificationAdvice {
54        preprocessed_data: preprocessed_data.map(|data| VerifierSinglePreprocessedDataInProgram {
55            commit: DigestVal::F(data.commit.clone().into().to_vec()),
56        }),
57        width: params.width,
58        quotient_degree: quotient_degree as usize,
59        num_public_values: params.num_public_values,
60        num_challenges_to_sample: params.num_challenges_to_sample,
61        num_exposed_values_after_challenge: params.num_exposed_values_after_challenge,
62        symbolic_constraints: symbolic_constraints.constraints,
63    }
64}
65
66/// Constants determined by multiple AIRs.
67pub struct MultiStarkVerificationAdvice<C: Config> {
68    pub per_air: Vec<StarkVerificationAdvice<C>>,
69    pub num_challenges_to_sample: Vec<usize>,
70    pub trace_height_constraints: Vec<LinearConstraint>,
71    pub log_up_pow_bits: usize,
72    pub pre_hash: DigestVal<C>,
73}
74
75/// Create MultiStarkVerificationAdvice for an inner config.
76pub fn new_from_inner_multi_vk<SC: StarkGenericConfig, C: Config<F = Val<SC>>>(
77    vk: &MultiStarkVerifyingKey<SC>,
78) -> MultiStarkVerificationAdvice<C>
79where
80    Com<SC>: Into<[C::F; DIGEST_SIZE]>,
81{
82    let num_challenges_to_sample = vk.num_challenges_per_phase();
83    MultiStarkVerificationAdvice {
84        per_air: vk
85            .inner
86            .per_air
87            .iter()
88            .map(|vk| new_from_inner_vk::<SC, C>(vk.clone()))
89            .collect(),
90        num_challenges_to_sample,
91        trace_height_constraints: vk.inner.trace_height_constraints.clone(),
92        log_up_pow_bits: vk.inner.log_up_pow_bits,
93        pre_hash: DigestVal::F(vk.pre_hash.clone().into().to_vec()),
94    }
95}
96
97impl<C: Config> StarkVerificationAdvice<C> {
98    pub fn log_quotient_degree(&self) -> usize {
99        log2_strict_usize(self.quotient_degree)
100    }
101}
102
103pub struct VerifierSinglePreprocessedDataInProgram<C: Config> {
104    pub commit: DigestVal<C>,
105}