openvm_native_recursion/
vars.rs

1use openvm_native_compiler::{
2    ir::{Array, Config, Ext, Felt, Usize},
3    prelude::*,
4};
5
6use crate::{digest::DigestVariable, fri::types::FriProofVariable, OUTER_DIGEST_SIZE};
7
8pub type OuterDigestVariable<C> = [Var<<C as Config>::N>; OUTER_DIGEST_SIZE];
9
10#[derive(DslVariable, Clone)]
11pub struct HintSlice<C: Config> {
12    pub length: Usize<C::N>,
13    pub id: Usize<C::N>,
14}
15
16#[derive(DslVariable, Clone)]
17pub struct StarkProofVariable<C: Config> {
18    pub commitments: CommitmentsVariable<C>,
19    pub opening: OpeningProofVariable<C>,
20    pub per_air: Array<C, AirProofDataVariable<C>>,
21    /// A permutation of AIR indexes which are sorted by log_degree in descending order.
22    pub air_perm_by_height: Array<C, Usize<C::N>>,
23    pub log_up_pow_witness: Felt<C::F>,
24}
25
26#[derive(DslVariable, Clone)]
27pub struct AirProofDataVariable<C: Config> {
28    pub air_id: Usize<C::N>,
29    /// height of trace matrix.
30    pub log_degree: Usize<C::N>,
31    /// For each challenge phase with trace, the values to expose to the verifier in that phase
32    #[allow(clippy::type_complexity)]
33    pub exposed_values_after_challenge: Array<C, Array<C, Ext<C::F, C::EF>>>,
34    // The public values to expose to the verifier
35    pub public_values: Array<C, Felt<C::F>>,
36}
37
38#[derive(Clone)]
39pub struct MultiStarkVerificationAdviceVariable<C: Config> {
40    pub per_air: Array<C, StarkVerificationAdviceVariable<C>>,
41    /// Shape is as same as the shape of the original VK's `num_challenges_to_sample.
42    /// Each element is 0 or 1. 1 means the challenge should be sampled.
43    pub num_challenges_to_sample_mask: Vec<Vec<Usize<C::N>>>,
44    pub trace_height_constraint_system: TraceHeightConstraintSystem<C>,
45}
46
47#[derive(Clone)]
48pub struct LinearConstraintVariable<C: Config> {
49    pub coefficients: Array<C, Var<C::N>>,
50    pub threshold: Var<C::N>,
51    /// Whether `threshold == p`, to help distinguish from `threshold == 0` in the field.
52    pub is_threshold_at_p: bool,
53}
54
55#[derive(Clone)]
56pub struct TraceHeightConstraintSystem<C: Config> {
57    /// Linear constraints where each constraint includes all trace heights.
58    pub height_constraints: Vec<LinearConstraintVariable<C>>,
59    /// Optional hard constraints on the height of each trace, derived from the above
60    /// `height_constraints` to ensure that c_{ij} * a_j does not overflow the field.
61    /// `height` should be less than `height_max`.
62    pub height_maxes: Array<C, OptionalVar<C>>,
63}
64
65#[derive(DslVariable, Clone)]
66pub struct OptionalVar<C: Config> {
67    pub is_some: Usize<C::N>,
68    pub value: Var<C::N>,
69}
70
71#[derive(DslVariable, Clone)]
72pub struct StarkVerificationAdviceVariable<C: Config> {
73    /// Preprocessed trace data, if any
74    pub preprocessed_data: Array<C, DigestVariable<C>>,
75    /// Trace sub-matrix widths
76    pub width: TraceWidthVariable<C>,
77    /// The factor to multiply the trace degree by to get the degree of the quotient polynomial.
78    /// Determined from the max constraint degree of the AIR constraints. This is equivalently
79    /// the number of chunks the quotient polynomial is split into.
80    pub log_quotient_degree: Usize<C::N>,
81    /// Number of public values for this STARK only
82    pub num_public_values: Usize<C::N>,
83    /// For only this RAP, how many challenges are needed in each trace challenge phase
84    pub num_challenges_to_sample: Array<C, Usize<C::N>>,
85    /// Number of values to expose to verifier in each trace challenge phase
86    pub num_exposed_values_after_challenge: Array<C, Usize<C::N>>,
87}
88
89#[derive(DslVariable, Debug, Clone)]
90pub struct TraceWidthVariable<C: Config> {
91    pub preprocessed: Array<C, Usize<C::N>>,
92    pub cached_mains: Array<C, Usize<C::N>>,
93    pub common_main: Usize<C::N>,
94    /// Width counted by extension field elements, _not_ base field elements
95    pub after_challenge: Array<C, Usize<C::N>>,
96}
97
98#[derive(DslVariable, Clone)]
99pub struct CommitmentsVariable<C: Config> {
100    pub main_trace: Array<C, DigestVariable<C>>,
101    pub after_challenge: Array<C, DigestVariable<C>>,
102    pub quotient: DigestVariable<C>,
103}
104
105#[derive(DslVariable, Clone)]
106pub struct OpeningProofVariable<C: Config> {
107    pub proof: FriProofVariable<C>,
108    pub values: OpenedValuesVariable<C>,
109}
110
111#[allow(clippy::type_complexity)]
112#[derive(DslVariable, Clone)]
113pub struct OpenedValuesVariable<C: Config> {
114    // For each preprocessed commitment, the opened values
115    pub preprocessed: Array<C, AdjacentOpenedValuesVariable<C>>,
116    /// For each main trace commitment, for each matrix in commitment, the
117    /// opened values
118    pub main: Array<C, Array<C, AdjacentOpenedValuesVariable<C>>>,
119    /// For each phase, for each RAP, the opened values,
120    pub after_challenge: Array<C, Array<C, AdjacentOpenedValuesVariable<C>>>,
121    /// For each RAP, for each quotient chunk in quotient poly, the opened values
122    pub quotient: Array<C, Array<C, Array<C, Ext<C::F, C::EF>>>>,
123}
124
125#[derive(DslVariable, Debug, Clone)]
126pub struct AdjacentOpenedValuesVariable<C: Config> {
127    pub local: Array<C, Ext<C::F, C::EF>>,
128    pub next: Array<C, Ext<C::F, C::EF>>,
129}