-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathcomponent.rs
More file actions
422 lines (370 loc) · 14.7 KB
/
component.rs
File metadata and controls
422 lines (370 loc) · 14.7 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::iter::zip;
use std::ops::Deref;
use itertools::Itertools;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use tracing::{span, Level};
use super::cpu_domain::CpuDomainEvaluator;
use super::logup::LogupSums;
use super::preprocessed_columns::PreprocessedColumn;
use super::{
EvalAtRow, InfoEvaluator, PointEvaluator, SimdDomainEvaluator, PREPROCESSED_TRACE_IDX,
};
use crate::core::air::accumulation::{DomainEvaluationAccumulator, PointEvaluationAccumulator};
use crate::core::air::{Component, ComponentProver, Trace};
use crate::core::backend::cpu::bit_reverse;
use crate::core::backend::simd::column::VeryPackedSecureColumnByCoords;
use crate::core::backend::simd::m31::LOG_N_LANES;
use crate::core::backend::simd::very_packed_m31::{VeryPackedBaseField, LOG_N_VERY_PACKED_ELEMS};
use crate::core::backend::simd::SimdBackend;
use crate::core::circle::CirclePoint;
use crate::core::constraints::coset_vanishing;
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::secure_column::SecureColumnByCoords;
use crate::core::fields::FieldExpOps;
use crate::core::pcs::{TreeSubspan, TreeVec};
use crate::core::poly::circle::{CanonicCoset, CircleEvaluation, PolyOps};
use crate::core::poly::BitReversedOrder;
use crate::core::ColumnVec;
const CHUNK_SIZE: usize = 1;
#[derive(Debug, Default)]
enum PreprocessedColumnsAllocationMode {
#[default]
Dynamic,
Static,
}
// TODO(andrew): Docs.
// TODO(andrew): Consider better location for this.
#[derive(Debug, Default)]
pub struct TraceLocationAllocator {
/// Mapping of tree index to next available column offset.
next_tree_offsets: TreeVec<usize>,
/// Mapping of preprocessed columns to their index.
preprocessed_columns: HashMap<PreprocessedColumn, usize>,
/// Controls whether the preprocessed columns are dynamic or static (default=Dynamic).
preprocessed_columns_allocation_mode: PreprocessedColumnsAllocationMode,
}
impl TraceLocationAllocator {
pub fn next_for_structure<T>(
&mut self,
structure: &TreeVec<ColumnVec<T>>,
) -> TreeVec<TreeSubspan> {
if structure.len() > self.next_tree_offsets.len() {
self.next_tree_offsets.resize(structure.len(), 0);
}
TreeVec::new(
zip(&mut *self.next_tree_offsets, &**structure)
.enumerate()
.map(|(tree_index, (offset, cols))| {
let col_start = *offset;
let col_end = col_start + cols.len();
*offset = col_end;
TreeSubspan {
tree_index,
col_start,
col_end,
}
})
.collect(),
)
}
/// Create a new `TraceLocationAllocator` with fixed preprocessed columns setup.
pub fn new_with_preproccessed_columns(preprocessed_columns: &[PreprocessedColumn]) -> Self {
Self {
next_tree_offsets: Default::default(),
preprocessed_columns: preprocessed_columns
.iter()
.enumerate()
.map(|(i, &col)| (col, i))
.collect(),
preprocessed_columns_allocation_mode: PreprocessedColumnsAllocationMode::Static,
}
}
pub const fn preprocessed_columns(&self) -> &HashMap<PreprocessedColumn, usize> {
&self.preprocessed_columns
}
// validates that `self.preprocessed_columns` is consistent with
// `preprocessed_columns`.
// I.e. preprocessed_columns[i] == self.preprocessed_columns[i].
pub fn validate_preprocessed_columns(&self, preprocessed_columns: &[PreprocessedColumn]) {
assert_eq!(preprocessed_columns.len(), self.preprocessed_columns.len());
for (column, idx) in self.preprocessed_columns.iter() {
assert_eq!(Some(column), preprocessed_columns.get(*idx));
}
}
}
/// A component defined solely in means of the constraints framework.
///
/// Implementing this trait introduces implementations for [`Component`] and [`ComponentProver`] for
/// the SIMD backend. Note that the constraint framework only supports components with columns of
/// the same size.
pub trait FrameworkEval {
fn log_size(&self) -> u32;
fn max_constraint_log_degree_bound(&self) -> u32;
fn evaluate<E: EvalAtRow>(&self, eval: E) -> E;
}
pub struct FrameworkComponent<C: FrameworkEval> {
eval: C,
trace_locations: TreeVec<TreeSubspan>,
info: InfoEvaluator,
preprocessed_column_indices: Vec<usize>,
logup_sums: LogupSums,
}
impl<E: FrameworkEval> FrameworkComponent<E> {
pub fn new(
location_allocator: &mut TraceLocationAllocator,
eval: E,
logup_sums: LogupSums,
) -> Self {
let info = eval.evaluate(InfoEvaluator::new(eval.log_size(), vec![], logup_sums));
let trace_locations = location_allocator.next_for_structure(&info.mask_offsets);
let preprocessed_column_indices = info
.preprocessed_columns
.iter()
.map(|col| {
let next_column = location_allocator.preprocessed_columns.len();
*location_allocator
.preprocessed_columns
.entry(*col)
.or_insert_with(|| {
if matches!(
location_allocator.preprocessed_columns_allocation_mode,
PreprocessedColumnsAllocationMode::Static
) {
panic!(
"Preprocessed column {:?} is missing from static allocation",
col
);
}
next_column
})
})
.collect();
Self {
eval,
trace_locations,
info,
preprocessed_column_indices,
logup_sums,
}
}
pub fn trace_locations(&self) -> &[TreeSubspan] {
&self.trace_locations
}
}
impl<E: FrameworkEval> Component for FrameworkComponent<E> {
fn n_constraints(&self) -> usize {
self.info.n_constraints
}
fn max_constraint_log_degree_bound(&self) -> u32 {
self.eval.max_constraint_log_degree_bound()
}
fn trace_log_degree_bounds(&self) -> TreeVec<ColumnVec<u32>> {
let mut log_degree_bounds = self
.info
.mask_offsets
.as_ref()
.map(|tree_offsets| vec![self.eval.log_size(); tree_offsets.len()]);
log_degree_bounds[0] = self
.preprocessed_column_indices
.iter()
.map(|_| self.eval.log_size())
.collect();
log_degree_bounds
}
fn mask_points(
&self,
point: CirclePoint<SecureField>,
) -> TreeVec<ColumnVec<Vec<CirclePoint<SecureField>>>> {
let trace_step = CanonicCoset::new(self.eval.log_size()).step();
self.info.mask_offsets.as_ref().map_cols(|col_offsets| {
col_offsets
.iter()
.map(|offset| point + trace_step.mul_signed(*offset).into_ef())
.collect()
})
}
fn preproccessed_column_indices(&self) -> ColumnVec<usize> {
self.preprocessed_column_indices.clone()
}
fn evaluate_constraint_quotients_at_point(
&self,
point: CirclePoint<SecureField>,
mask: &TreeVec<ColumnVec<Vec<SecureField>>>,
evaluation_accumulator: &mut PointEvaluationAccumulator,
) {
let preprocessed_mask = self
.preprocessed_column_indices
.iter()
.map(|idx| &mask[PREPROCESSED_TRACE_IDX][*idx])
.collect_vec();
let mut mask_points = mask.sub_tree(&self.trace_locations);
mask_points[PREPROCESSED_TRACE_IDX] = preprocessed_mask;
self.eval.evaluate(PointEvaluator::new(
mask_points,
evaluation_accumulator,
coset_vanishing(CanonicCoset::new(self.eval.log_size()).coset, point).inverse(),
self.eval.log_size(),
self.logup_sums,
));
}
}
impl<E: FrameworkEval + Sync> ComponentProver<SimdBackend> for FrameworkComponent<E> {
fn evaluate_constraint_quotients_on_domain(
&self,
trace: &Trace<'_, SimdBackend>,
evaluation_accumulator: &mut DomainEvaluationAccumulator<SimdBackend>,
) {
if self.n_constraints() == 0 {
return;
}
let eval_domain = CanonicCoset::new(self.max_constraint_log_degree_bound()).circle_domain();
let trace_domain = CanonicCoset::new(self.eval.log_size());
let mut component_polys = trace.polys.sub_tree(&self.trace_locations);
component_polys[PREPROCESSED_TRACE_IDX] = self
.preprocessed_column_indices
.iter()
.map(|idx| &trace.polys[PREPROCESSED_TRACE_IDX][*idx])
.collect();
let mut component_evals = trace.evals.sub_tree(&self.trace_locations);
component_evals[PREPROCESSED_TRACE_IDX] = self
.preprocessed_column_indices
.iter()
.map(|idx| &trace.evals[PREPROCESSED_TRACE_IDX][*idx])
.collect();
// Extend trace if necessary.
// TODO: Don't extend when eval_size < committed_size. Instead, pick a good
// subdomain. (For larger blowup factors).
let need_to_extend = component_evals
.iter()
.flatten()
.any(|c| c.domain != eval_domain);
let trace: TreeVec<
Vec<Cow<'_, CircleEvaluation<SimdBackend, BaseField, BitReversedOrder>>>,
> = if need_to_extend {
let _span = span!(Level::INFO, "Extension").entered();
let twiddles = SimdBackend::precompute_twiddles(eval_domain.half_coset);
component_polys
.as_cols_ref()
.map_cols(|col| Cow::Owned(col.evaluate_with_twiddles(eval_domain, &twiddles)))
} else {
component_evals.clone().map_cols(|c| Cow::Borrowed(*c))
};
// Denom inverses.
let log_expand = eval_domain.log_size() - trace_domain.log_size();
let mut denom_inv = (0..1 << log_expand)
.map(|i| coset_vanishing(trace_domain.coset(), eval_domain.at(i)).inverse())
.collect_vec();
bit_reverse(&mut denom_inv);
// Accumulator.
let [mut accum] =
evaluation_accumulator.columns([(eval_domain.log_size(), self.n_constraints())]);
accum.random_coeff_powers.reverse();
let _span = span!(Level::INFO, "Constraint point-wise eval").entered();
if trace_domain.log_size() < LOG_N_LANES + LOG_N_VERY_PACKED_ELEMS {
// Fall back to CPU if the trace is too small.
let mut col = accum.col.to_cpu();
for row in 0..(1 << eval_domain.log_size()) {
let trace_cols = trace.as_cols_ref().map_cols(|c| c.to_cpu());
let trace_cols = trace_cols.as_cols_ref();
// Evaluate constrains at row.
let eval = CpuDomainEvaluator::new(
&trace_cols,
row,
&accum.random_coeff_powers,
trace_domain.log_size(),
eval_domain.log_size(),
self.eval.log_size(),
self.logup_sums,
);
let row_res = self.eval.evaluate(eval).row_res;
// Finalize row.
let denom_inv = denom_inv[row >> trace_domain.log_size()];
col.set(row, col.at(row) + row_res * denom_inv)
}
let col = SecureColumnByCoords::from_cpu(col);
*accum.col = col;
return;
}
let col = unsafe { VeryPackedSecureColumnByCoords::transform_under_mut(accum.col) };
let range = 0..(1 << (eval_domain.log_size() - LOG_N_LANES - LOG_N_VERY_PACKED_ELEMS));
#[cfg(not(feature = "parallel"))]
let iter = range.step_by(CHUNK_SIZE).zip(col.chunks_mut(CHUNK_SIZE));
#[cfg(feature = "parallel")]
let iter = range
.into_par_iter()
.step_by(CHUNK_SIZE)
.zip(col.chunks_mut(CHUNK_SIZE));
// Define any `self` values outside the loop to prevent the compiler thinking there is a
// `Sync` requirement on `Self`.
let self_eval = &self.eval;
let self_logup_sums = self.logup_sums;
iter.for_each(|(chunk_idx, mut chunk)| {
let trace_cols = trace.as_cols_ref().map_cols(|c| c.as_ref());
for idx_in_chunk in 0..CHUNK_SIZE {
let vec_row = chunk_idx * CHUNK_SIZE + idx_in_chunk;
// Evaluate constrains at row.
let eval = SimdDomainEvaluator::new(
&trace_cols,
vec_row,
&accum.random_coeff_powers,
trace_domain.log_size(),
eval_domain.log_size(),
self_eval.log_size(),
self_logup_sums,
);
let row_res = self_eval.evaluate(eval).row_res;
// Finalize row.
unsafe {
let denom_inv = VeryPackedBaseField::broadcast(
denom_inv[vec_row
>> (trace_domain.log_size() - LOG_N_LANES - LOG_N_VERY_PACKED_ELEMS)],
);
chunk.set_packed(
idx_in_chunk,
chunk.packed_at(idx_in_chunk) + row_res * denom_inv,
)
}
}
});
}
}
impl<E: FrameworkEval> Deref for FrameworkComponent<E> {
type Target = E;
fn deref(&self) -> &E {
&self.eval
}
}
impl<E: FrameworkEval> Display for FrameworkComponent<E> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let log_n_rows = self.log_size();
let mut n_cols = vec![];
self.trace_log_degree_bounds()
.0
.iter()
.for_each(|interaction| {
n_cols.push(interaction.len());
});
writeln!(f, "n_rows 2^{}", log_n_rows)?;
writeln!(f, "n_constraints {}", self.n_constraints())?;
writeln!(
f,
"constraint_log_degree_bound {}",
self.max_constraint_log_degree_bound()
)?;
writeln!(
f,
"total felts: 2^{} * {}",
log_n_rows,
n_cols.iter().sum::<usize>()
)?;
for (j, n_cols) in n_cols.into_iter().enumerate() {
writeln!(f, "\t Interaction {}: n_cols {}", j, n_cols)?;
}
Ok(())
}
}