forked from lambdaclass/lambdaworks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.rs
387 lines (338 loc) · 11.4 KB
/
trace.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
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
use crate::table::Table;
use itertools::Itertools;
use lambdaworks_math::fft::errors::FFTError;
use lambdaworks_math::field::traits::{IsField, IsSubFieldOf};
use lambdaworks_math::{
field::{element::FieldElement, traits::IsFFTField},
polynomial::Polynomial,
};
#[cfg(feature = "parallel")]
use rayon::prelude::{IntoParallelRefIterator, ParallelIterator};
/// A two-dimensional representation of an execution trace of the STARK
/// protocol.
///
/// For the moment it is mostly a wrapper around the `Table` struct. It is a
/// layer above the raw two-dimensional table, with functionality relevant to the
/// STARK protocol, such as the step size (number of consecutive rows of the table)
/// of the computation being proven.
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct TraceTable<F, E>
where
E: IsField,
F: IsSubFieldOf<E> + IsField,
{
pub main_table: Table<F>,
pub aux_table: Table<E>,
pub num_main_columns: usize,
pub num_aux_columns: usize,
pub step_size: usize,
}
impl<F, E> TraceTable<F, E>
where
E: IsField,
F: IsSubFieldOf<E> + IsFFTField,
{
pub fn new(
main_data: Vec<FieldElement<F>>,
aux_data: Vec<FieldElement<E>>,
num_main_columns: usize,
num_aux_columns: usize,
step_size: usize,
) -> Self {
let main_table = Table::new(main_data, num_main_columns);
let aux_table = Table::new(aux_data, num_aux_columns);
Self {
main_table,
aux_table,
num_main_columns,
num_aux_columns,
step_size,
}
}
/// Creates a new TraceTable from from a one-dimensional array in row major order and the intended width of the table.
/// Step size is how many are needed to represent a state of the VM
pub fn new_main(
main_data: Vec<FieldElement<F>>,
num_main_columns: usize,
step_size: usize,
) -> Self {
let num_aux_columns = 0;
let main_table = Table::new(main_data, num_main_columns);
let aux_table = Table::new(Vec::new(), num_aux_columns);
Self {
main_table,
aux_table,
num_main_columns,
num_aux_columns,
step_size,
}
}
/// Creates a new TraceTable from its colummns
/// Step size is how many are needed to represent a state of the VM
pub fn from_columns(
main_columns: Vec<Vec<FieldElement<F>>>,
aux_columns: Vec<Vec<FieldElement<E>>>,
step_size: usize,
) -> Self {
let num_main_columns = main_columns.len();
let num_aux_columns = aux_columns.len();
let main_table = Table::from_columns(main_columns);
let aux_table = Table::from_columns(aux_columns);
Self {
main_table,
aux_table,
num_main_columns,
num_aux_columns,
step_size,
}
}
pub fn from_columns_main(columns: Vec<Vec<FieldElement<F>>>, step_size: usize) -> Self {
let num_main_columns = columns.len();
let num_aux_columns = 0;
let main_table = Table::from_columns(columns);
let aux_table = Table::from_columns(Vec::new());
Self {
main_table,
aux_table,
num_main_columns,
num_aux_columns,
step_size,
}
}
pub fn empty() -> Self {
Self::new(Vec::new(), Vec::new(), 0, 0, 0)
}
pub fn is_empty(&self) -> bool {
self.main_table.width == 0 && self.aux_table.width == 0
}
pub fn num_rows(&self) -> usize {
self.main_table.height
}
pub fn num_steps(&self) -> usize {
debug_assert!((self.main_table.height % self.step_size) == 0);
self.main_table.height / self.step_size
}
/// Given a particular step of the computation represented on the trace,
/// returns the row of the underlying table.
pub fn step_to_row(&self, step: usize) -> usize {
self.step_size * step
}
pub fn num_cols(&self) -> usize {
self.main_table.width + self.aux_table.width
}
pub fn columns_main(&self) -> Vec<Vec<FieldElement<F>>> {
self.main_table.columns()
}
pub fn columns_aux(&self) -> Vec<Vec<FieldElement<E>>> {
self.aux_table.columns()
}
/// Given a row and a column index, gives stored value in that position
pub fn get_main(&self, row: usize, col: usize) -> &FieldElement<F> {
self.main_table.get(row, col)
}
/// Given a row and a column index, gives stored value in that position
pub fn get_aux(&self, row: usize, col: usize) -> &FieldElement<E> {
self.aux_table.get(row, col)
}
pub fn set_main(&mut self, row: usize, col: usize, value: FieldElement<F>) {
self.main_table.set(row, col, value);
}
pub fn set_aux(&mut self, row: usize, col: usize, value: FieldElement<E>) {
self.aux_table.set(row, col, value);
}
pub fn allocate_with_zeros(
num_steps: usize,
num_main_columns: usize,
num_aux_columns: usize,
step_size: usize,
) -> TraceTable<F, E> {
let main_data = vec![FieldElement::<F>::zero(); step_size * num_steps * num_main_columns];
let aux_data = vec![FieldElement::<E>::zero(); step_size * num_steps * num_aux_columns];
TraceTable::new(
main_data,
aux_data,
num_main_columns,
num_aux_columns,
step_size,
)
}
pub fn compute_trace_polys_main<S>(&self) -> Vec<Polynomial<FieldElement<F>>>
where
S: IsFFTField + IsSubFieldOf<F>,
FieldElement<F>: Send + Sync,
{
let columns = self.columns_main();
#[cfg(feature = "parallel")]
let iter = columns.par_iter();
#[cfg(not(feature = "parallel"))]
let iter = columns.iter();
iter.map(|col| Polynomial::interpolate_fft::<S>(col))
.collect::<Result<Vec<Polynomial<FieldElement<F>>>, FFTError>>()
.unwrap()
}
pub fn compute_trace_polys_aux<S>(&self) -> Vec<Polynomial<FieldElement<E>>>
where
S: IsFFTField + IsSubFieldOf<F>,
FieldElement<E>: Send + Sync,
{
let columns = self.columns_aux();
#[cfg(feature = "parallel")]
let iter = columns.par_iter();
#[cfg(not(feature = "parallel"))]
let iter = columns.iter();
iter.map(|col| Polynomial::interpolate_fft::<F>(col))
.collect::<Result<Vec<Polynomial<FieldElement<E>>>, FFTError>>()
.unwrap()
}
pub fn get_column_main(&self, col_idx: usize) -> Vec<FieldElement<F>> {
self.main_table.get_column(col_idx)
}
pub fn get_column_aux(&self, col_idx: usize) -> Vec<FieldElement<E>> {
self.aux_table.get_column(col_idx)
}
}
pub struct LDETraceTable<F, E>
where
E: IsField,
F: IsSubFieldOf<E> + IsField,
{
pub(crate) main_table: Table<F>,
pub(crate) aux_table: Table<E>,
pub(crate) lde_step_size: usize,
pub(crate) blowup_factor: usize,
}
impl<F, E> LDETraceTable<F, E>
where
E: IsField,
F: IsSubFieldOf<E>,
{
pub fn new(
main_data: Vec<FieldElement<F>>,
aux_data: Vec<FieldElement<E>>,
n_columns: usize,
trace_step_size: usize,
blowup_factor: usize,
) -> Self {
let main_table = Table::new(main_data, n_columns);
let aux_table = Table::new(aux_data, n_columns);
let lde_step_size = trace_step_size * blowup_factor;
Self {
main_table,
aux_table,
lde_step_size,
blowup_factor,
}
}
pub fn from_columns(
main_columns: Vec<Vec<FieldElement<F>>>,
aux_columns: Vec<Vec<FieldElement<E>>>,
trace_step_size: usize,
blowup_factor: usize,
) -> Self {
let main_table = Table::from_columns(main_columns);
let aux_table = Table::from_columns(aux_columns);
let lde_step_size = trace_step_size * blowup_factor;
Self {
main_table,
aux_table,
lde_step_size,
blowup_factor,
}
}
pub fn num_cols(&self) -> usize {
self.main_table.width + self.aux_table.width
}
pub fn num_rows(&self) -> usize {
self.main_table.height
}
pub fn get_main_row(&self, row_idx: usize) -> &[FieldElement<F>] {
self.main_table.get_row(row_idx)
}
pub fn get_aux_row(&self, row_idx: usize) -> &[FieldElement<E>] {
self.aux_table.get_row(row_idx)
}
pub fn get_main(&self, row: usize, col: usize) -> &FieldElement<F> {
self.main_table.get(row, col)
}
pub fn get_aux(&self, row: usize, col: usize) -> &FieldElement<E> {
self.aux_table.get(row, col)
}
pub fn num_steps(&self) -> usize {
debug_assert!((self.main_table.height % self.lde_step_size) == 0);
self.main_table.height / self.lde_step_size
}
pub fn step_to_row(&self, step: usize) -> usize {
self.lde_step_size * step
}
}
/// Given a slice of trace polynomials, an evaluation point `x`, the frame offsets
/// corresponding to the computation of the transitions, and a primitive root,
/// outputs the trace evaluations of each trace polynomial over the values used to
/// compute a transition.
/// Example: For a simple Fibonacci computation, if t(x) is the trace polynomial of
/// the computation, this will output evaluations t(x), t(g * x), t(g^2 * z).
pub fn get_trace_evaluations<F, E>(
main_trace_polys: &[Polynomial<FieldElement<F>>],
aux_trace_polys: &[Polynomial<FieldElement<E>>],
x: &FieldElement<E>,
frame_offsets: &[usize],
primitive_root: &FieldElement<F>,
step_size: usize,
) -> Table<E>
where
F: IsSubFieldOf<E>,
E: IsField,
{
let evaluation_points = frame_offsets
.iter()
.flat_map(|offset| {
let exponents_range_start = offset * step_size;
let exponents_range_end = (offset + 1) * step_size;
(exponents_range_start..exponents_range_end).collect_vec()
})
.map(|exponent| primitive_root.pow(exponent) * x)
.collect_vec();
let main_evaluations = evaluation_points
.iter()
.map(|eval_point| {
main_trace_polys
.iter()
.map(|main_poly| main_poly.evaluate(eval_point))
.collect_vec()
})
.collect_vec();
let aux_evaluations = evaluation_points
.iter()
.map(|eval_point| {
aux_trace_polys
.iter()
.map(|aux_poly| aux_poly.evaluate(eval_point))
.collect_vec()
})
.collect_vec();
debug_assert_eq!(main_evaluations.len(), aux_evaluations.len());
let mut main_evaluations = main_evaluations;
let mut table_data = Vec::new();
for (main_row, aux_row) in main_evaluations.iter_mut().zip(aux_evaluations) {
main_row.extend_from_slice(&aux_row);
table_data.extend_from_slice(main_row);
}
let main_trace_width = main_trace_polys.len();
let aux_trace_width = aux_trace_polys.len();
let table_width = main_trace_width + aux_trace_width;
Table::new(table_data, table_width)
}
pub fn columns2rows<F>(columns: Vec<Vec<F>>) -> Vec<Vec<F>>
where
F: Clone,
{
let num_rows = columns[0].len();
let num_cols = columns.len();
(0..num_rows)
.map(|row_index| {
(0..num_cols)
.map(|col_index| columns[col_index][row_index].clone())
.collect()
})
.collect()
}