-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathtraits.rs
231 lines (193 loc) · 7.34 KB
/
traits.rs
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
use std::collections::HashMap;
use lambdaworks_crypto::fiat_shamir::is_transcript::IsTranscript;
use lambdaworks_math::{
field::{
element::FieldElement,
traits::{IsFFTField, IsField, IsSubFieldOf},
},
polynomial::Polynomial,
};
use crate::{constraints::transition::TransitionConstraint, domain::Domain};
use super::{
constraints::boundary::BoundaryConstraints, context::AirContext, frame::Frame,
proof::options::ProofOptions, trace::TraceTable,
};
type ZerofierGroupKey = (usize, usize, Option<usize>, Option<usize>, usize);
/// This enum is necessary because, while both the prover and verifier perform the same operations
/// to compute transition constraints, their frames differ.
/// The prover uses a frame containing elements from both the base field and its extension
/// (common when working with small fields and challengers in the extension).
/// In contrast, the verifier, lacking access to the trace and relying solely on evaluations at the challengers,
/// works with a frame that contains only elements from the extension.
pub enum TransitionEvaluationContext<'a, F, E>
where
F: IsSubFieldOf<E>,
E: IsField,
{
Prover {
frame: &'a Frame<'a, F, E>,
periodic_values: &'a [FieldElement<F>],
rap_challenges: &'a [FieldElement<E>],
},
Verifier {
frame: &'a Frame<'a, E, E>,
periodic_values: &'a [FieldElement<E>],
rap_challenges: &'a [FieldElement<E>],
},
}
impl<'a, F, E> TransitionEvaluationContext<'a, F, E>
where
F: IsSubFieldOf<E>,
E: IsField,
{
pub fn new_prover(
frame: &'a Frame<'a, F, E>,
periodic_values: &'a [FieldElement<F>],
rap_challenges: &'a [FieldElement<E>],
) -> Self {
Self::Prover {
frame,
periodic_values,
rap_challenges,
}
}
pub fn new_verifier(
frame: &'a Frame<'a, E, E>,
periodic_values: &'a [FieldElement<E>],
rap_challenges: &'a [FieldElement<E>],
) -> Self {
Self::Verifier {
frame,
periodic_values,
rap_challenges,
}
}
}
/// AIR is a representation of the Constraints
pub trait AIR {
type Field: IsFFTField + IsSubFieldOf<Self::FieldExtension> + Send + Sync;
type FieldExtension: IsField + Send + Sync;
type PublicInputs;
const STEP_SIZE: usize;
fn new(
trace_length: usize,
pub_inputs: &Self::PublicInputs,
proof_options: &ProofOptions,
) -> Self;
fn build_auxiliary_trace(
&self,
_main_trace: &mut TraceTable<Self::Field, Self::FieldExtension>,
_rap_challenges: &[FieldElement<Self::FieldExtension>],
) where
Self::FieldExtension: IsFFTField,
{
}
fn build_rap_challenges(
&self,
_transcript: &mut impl IsTranscript<Self::FieldExtension>,
) -> Vec<FieldElement<Self::FieldExtension>> {
Vec::new()
}
/// Returns the amount main trace columns and auxiliary trace columns
fn trace_layout(&self) -> (usize, usize);
fn has_trace_interaction(&self) -> bool {
let (_main_trace_columns, aux_trace_columns) = self.trace_layout();
aux_trace_columns != 0
}
fn num_auxiliary_rap_columns(&self) -> usize {
self.trace_layout().1
}
fn composition_poly_degree_bound(&self) -> usize;
/// The method called by the prover to evaluate the transitions corresponding to an evaluation frame.
/// In the case of the prover, the main evaluation table of the frame takes values in
/// `Self::Field`, since they are the evaluations of the main trace at the LDE domain.
/// In the case of the verifier, the frame take elements of Self::FieldExtension.
fn compute_transition(
&self,
evaluation_context: &TransitionEvaluationContext<Self::Field, Self::FieldExtension>,
) -> Vec<FieldElement<Self::FieldExtension>> {
let mut evaluations =
vec![FieldElement::<Self::FieldExtension>::zero(); self.num_transition_constraints()];
self.transition_constraints()
.iter()
.for_each(|c| c.evaluate(evaluation_context, &mut evaluations));
evaluations
}
fn boundary_constraints(
&self,
rap_challenges: &[FieldElement<Self::FieldExtension>],
) -> BoundaryConstraints<Self::FieldExtension>;
fn context(&self) -> &AirContext;
fn trace_length(&self) -> usize;
fn options(&self) -> &ProofOptions {
&self.context().proof_options
}
fn blowup_factor(&self) -> u8 {
self.options().blowup_factor
}
fn coset_offset(&self) -> FieldElement<Self::Field> {
FieldElement::from(self.options().coset_offset)
}
fn trace_primitive_root(&self) -> FieldElement<Self::Field> {
let trace_length = self.trace_length();
let root_of_unity_order = u64::from(trace_length.trailing_zeros());
Self::Field::get_primitive_root_of_unity(root_of_unity_order).unwrap()
}
fn num_transition_constraints(&self) -> usize {
self.context().num_transition_constraints
}
fn pub_inputs(&self) -> &Self::PublicInputs;
fn get_periodic_column_values(&self) -> Vec<Vec<FieldElement<Self::Field>>> {
vec![]
}
fn get_periodic_column_polynomials(&self) -> Vec<Polynomial<FieldElement<Self::Field>>> {
let mut result = Vec::new();
for periodic_column in self.get_periodic_column_values() {
let values: Vec<_> = periodic_column
.iter()
.cycle()
.take(self.trace_length())
.cloned()
.collect();
let poly =
Polynomial::<FieldElement<Self::Field>>::interpolate_fft::<Self::Field>(&values)
.unwrap();
result.push(poly);
}
result
}
fn transition_constraints(
&self,
) -> &Vec<Box<dyn TransitionConstraint<Self::Field, Self::FieldExtension>>>;
fn transition_zerofier_evaluations(
&self,
domain: &Domain<Self::Field>,
) -> Vec<Vec<FieldElement<Self::Field>>> {
let mut evals = vec![Vec::new(); self.num_transition_constraints()];
let mut zerofier_groups: HashMap<ZerofierGroupKey, Vec<FieldElement<Self::Field>>> =
HashMap::new();
self.transition_constraints().iter().for_each(|c| {
let period = c.period();
let offset = c.offset();
let exemptions_period = c.exemptions_period();
let periodic_exemptions_offset = c.periodic_exemptions_offset();
let end_exemptions = c.end_exemptions();
// This hashmap is used to avoid recomputing with an fft the same zerofier evaluation
// If there are multiple domain and subdomains it can be further optimized
// as to share computation between them
let zerofier_group_key = (
period,
offset,
exemptions_period,
periodic_exemptions_offset,
end_exemptions,
);
zerofier_groups
.entry(zerofier_group_key)
.or_insert_with(|| c.zerofier_evaluations_on_extended_domain(domain));
let zerofier_evaluations = zerofier_groups.get(&zerofier_group_key).unwrap();
evals[c.constraint_idx()] = zerofier_evaluations.clone();
});
evals
}
}