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. Determined from the max constraint degree of the AIR constraints.
78    /// This is equivalently the number of chunks the quotient polynomial is split into.
79    pub log_quotient_degree: Usize<C::N>,
80    /// Number of public values for this STARK only
81    pub num_public_values: Usize<C::N>,
82    /// For only this RAP, how many challenges are needed in each trace challenge phase
83    pub num_challenges_to_sample: Array<C, Usize<C::N>>,
84    /// Number of values to expose to verifier in each trace challenge phase
85    pub num_exposed_values_after_challenge: Array<C, Usize<C::N>>,
86}
87
88#[derive(DslVariable, Debug, Clone)]
89pub struct TraceWidthVariable<C: Config> {
90    pub preprocessed: Array<C, Usize<C::N>>,
91    pub cached_mains: Array<C, Usize<C::N>>,
92    pub common_main: Usize<C::N>,
93    /// Width counted by extension field elements, _not_ base field elements
94    pub after_challenge: Array<C, Usize<C::N>>,
95}
96
97#[derive(DslVariable, Clone)]
98pub struct CommitmentsVariable<C: Config> {
99    pub main_trace: Array<C, DigestVariable<C>>,
100    pub after_challenge: Array<C, DigestVariable<C>>,
101    pub quotient: DigestVariable<C>,
102}
103
104#[derive(DslVariable, Clone)]
105pub struct OpeningProofVariable<C: Config> {
106    pub proof: FriProofVariable<C>,
107    pub values: OpenedValuesVariable<C>,
108}
109
110#[allow(clippy::type_complexity)]
111#[derive(DslVariable, Clone)]
112pub struct OpenedValuesVariable<C: Config> {
113    // For each preprocessed commitment, the opened values
114    pub preprocessed: Array<C, AdjacentOpenedValuesVariable<C>>,
115    /// For each main trace commitment, for each matrix in commitment, the
116    /// opened values
117    pub main: Array<C, Array<C, AdjacentOpenedValuesVariable<C>>>,
118    /// For each phase, for each RAP, the opened values,
119    pub after_challenge: Array<C, Array<C, AdjacentOpenedValuesVariable<C>>>,
120    /// For each RAP, for each quotient chunk in quotient poly, the opened values
121    pub quotient: Array<C, Array<C, Array<C, Ext<C::F, C::EF>>>>,
122}
123
124#[derive(DslVariable, Debug, Clone)]
125pub struct AdjacentOpenedValuesVariable<C: Config> {
126    pub local: Array<C, Ext<C::F, C::EF>>,
127    pub next: Array<C, Ext<C::F, C::EF>>,
128}