p3_uni_stark/
folder.rs
1use alloc::vec::Vec;
2
3use p3_air::{AirBuilder, AirBuilderWithPublicValues};
4use p3_field::FieldAlgebra;
5use p3_matrix::dense::RowMajorMatrixView;
6use p3_matrix::stack::VerticalPair;
7
8use crate::{PackedChallenge, PackedVal, StarkGenericConfig, Val};
9
10#[derive(Debug)]
11pub struct ProverConstraintFolder<'a, SC: StarkGenericConfig> {
12 pub main: RowMajorMatrixView<'a, PackedVal<SC>>,
13 pub public_values: &'a Vec<Val<SC>>,
14 pub is_first_row: PackedVal<SC>,
15 pub is_last_row: PackedVal<SC>,
16 pub is_transition: PackedVal<SC>,
17 pub alpha_powers: &'a [SC::Challenge],
18 pub accumulator: PackedChallenge<SC>,
19 pub constraint_index: usize,
20}
21
22type ViewPair<'a, T> = VerticalPair<RowMajorMatrixView<'a, T>, RowMajorMatrixView<'a, T>>;
23
24#[derive(Debug)]
25pub struct VerifierConstraintFolder<'a, SC: StarkGenericConfig> {
26 pub main: ViewPair<'a, SC::Challenge>,
27 pub public_values: &'a Vec<Val<SC>>,
28 pub is_first_row: SC::Challenge,
29 pub is_last_row: SC::Challenge,
30 pub is_transition: SC::Challenge,
31 pub alpha: SC::Challenge,
32 pub accumulator: SC::Challenge,
33}
34
35impl<'a, SC: StarkGenericConfig> AirBuilder for ProverConstraintFolder<'a, SC> {
36 type F = Val<SC>;
37 type Expr = PackedVal<SC>;
38 type Var = PackedVal<SC>;
39 type M = RowMajorMatrixView<'a, PackedVal<SC>>;
40
41 #[inline]
42 fn main(&self) -> Self::M {
43 self.main
44 }
45
46 #[inline]
47 fn is_first_row(&self) -> Self::Expr {
48 self.is_first_row
49 }
50
51 #[inline]
52 fn is_last_row(&self) -> Self::Expr {
53 self.is_last_row
54 }
55
56 #[inline]
57 fn is_transition_window(&self, size: usize) -> Self::Expr {
58 if size == 2 {
59 self.is_transition
60 } else {
61 panic!("uni-stark only supports a window size of 2")
62 }
63 }
64
65 #[inline]
66 fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I) {
67 let x: PackedVal<SC> = x.into();
68 let alpha_power = self.alpha_powers[self.constraint_index];
69 self.accumulator += PackedChallenge::<SC>::from_f(alpha_power) * x;
70 self.constraint_index += 1;
71 }
72}
73
74impl<SC: StarkGenericConfig> AirBuilderWithPublicValues for ProverConstraintFolder<'_, SC> {
75 type PublicVar = Self::F;
76
77 #[inline]
78 fn public_values(&self) -> &[Self::F] {
79 self.public_values
80 }
81}
82
83impl<'a, SC: StarkGenericConfig> AirBuilder for VerifierConstraintFolder<'a, SC> {
84 type F = Val<SC>;
85 type Expr = SC::Challenge;
86 type Var = SC::Challenge;
87 type M = ViewPair<'a, SC::Challenge>;
88
89 fn main(&self) -> Self::M {
90 self.main
91 }
92
93 fn is_first_row(&self) -> Self::Expr {
94 self.is_first_row
95 }
96
97 fn is_last_row(&self) -> Self::Expr {
98 self.is_last_row
99 }
100
101 fn is_transition_window(&self, size: usize) -> Self::Expr {
102 if size == 2 {
103 self.is_transition
104 } else {
105 panic!("uni-stark only supports a window size of 2")
106 }
107 }
108
109 fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I) {
110 let x: SC::Challenge = x.into();
111 self.accumulator *= self.alpha;
112 self.accumulator += x;
113 }
114}
115
116impl<SC: StarkGenericConfig> AirBuilderWithPublicValues for VerifierConstraintFolder<'_, SC> {
117 type PublicVar = Self::F;
118
119 fn public_values(&self) -> &[Self::F] {
120 self.public_values
121 }
122}