-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathutils.rs
283 lines (239 loc) · 9.31 KB
/
utils.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
use alloc::vec::Vec;
use core::slice;
use miden_air::{trace::main_trace::MainTrace, RowIndex};
#[cfg(test)]
use vm_core::{utils::ToElements, Operation};
use super::{Felt, FieldElement, NUM_RAND_ROWS};
use crate::{chiplets::Chiplets, utils::uninit_vector};
// TRACE FRAGMENT
// ================================================================================================
/// TODO: add docs
pub struct TraceFragment<'a> {
data: Vec<&'a mut [Felt]>,
}
impl<'a> TraceFragment<'a> {
/// Creates a new TraceFragment with its data allocated to the specified capacity.
pub fn new(capacity: usize) -> Self {
TraceFragment { data: Vec::with_capacity(capacity) }
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns the number of columns in this execution trace fragment.
pub fn width(&self) -> usize {
self.data.len()
}
/// Returns the number of rows in this execution trace fragment.
pub fn len(&self) -> usize {
self.data[0].len()
}
// DATA MUTATORS
// --------------------------------------------------------------------------------------------
/// Updates a single cell in this fragment with provided value.
#[inline(always)]
pub fn set(&mut self, row_idx: RowIndex, col_idx: usize, value: Felt) {
self.data[col_idx][row_idx] = value;
}
/// Returns a mutable iterator to the columns of this fragment.
pub fn columns(&mut self) -> slice::IterMut<'_, &'a mut [Felt]> {
self.data.iter_mut()
}
/// Adds a new column to this fragment by pushing a mutable slice with the first `len`
/// elements of the provided column. Returns the rest of the provided column as a separate
/// mutable slice.
pub fn push_column_slice(&mut self, column: &'a mut [Felt], len: usize) -> &'a mut [Felt] {
let (column_fragment, rest) = column.split_at_mut(len);
self.data.push(column_fragment);
rest
}
// TEST METHODS
// --------------------------------------------------------------------------------------------
#[cfg(test)]
pub fn trace_to_fragment(trace: &'a mut [Vec<Felt>]) -> Self {
let mut data = Vec::new();
for column in trace.iter_mut() {
data.push(column.as_mut_slice());
}
Self { data }
}
}
// TRACE LENGTH SUMMARY
// ================================================================================================
/// Contains the data about lengths of the trace parts.
///
/// - `main_trace_len` contains the length of the main trace.
/// - `range_trace_len` contains the length of the range checker trace.
/// - `chiplets_trace_len` contains the trace lengths of the all chiplets (hash, bitwise, memory,
/// kernel ROM)
#[derive(Debug, Default, Eq, PartialEq, Clone, Copy)]
pub struct TraceLenSummary {
main_trace_len: usize,
range_trace_len: usize,
chiplets_trace_len: ChipletsLengths,
}
impl TraceLenSummary {
pub fn new(
main_trace_len: usize,
range_trace_len: usize,
chiplets_trace_len: ChipletsLengths,
) -> Self {
TraceLenSummary {
main_trace_len,
range_trace_len,
chiplets_trace_len,
}
}
/// Returns length of the main trace.
pub fn main_trace_len(&self) -> usize {
self.main_trace_len
}
/// Returns length of the range checker trace.
pub fn range_trace_len(&self) -> usize {
self.range_trace_len
}
/// Returns [ChipletsLengths] which contains trace lengths of all chilplets.
pub fn chiplets_trace_len(&self) -> ChipletsLengths {
self.chiplets_trace_len
}
/// Returns the maximum of all component lengths.
pub fn trace_len(&self) -> usize {
self.range_trace_len
.max(self.main_trace_len)
.max(self.chiplets_trace_len.trace_len())
}
/// Returns `trace_len` rounded up to the next power of two.
pub fn padded_trace_len(&self) -> usize {
(self.trace_len() + NUM_RAND_ROWS).next_power_of_two()
}
/// Returns the percent (0 - 100) of the steps that were added to the trace to pad it to the
/// next power of tow.
pub fn padding_percentage(&self) -> usize {
(self.padded_trace_len() - self.trace_len()) * 100 / self.padded_trace_len()
}
}
/// Contains trace lengths of all chilplets: hash, bitwise, memory and kernel ROM trace
/// lengths.
#[derive(Default, Clone, Copy, Debug, PartialEq, Eq)]
pub struct ChipletsLengths {
hash_chiplet_len: usize,
bitwise_chiplet_len: usize,
memory_chiplet_len: usize,
kernel_rom_len: usize,
}
impl ChipletsLengths {
pub fn new(chiplets: &Chiplets) -> Self {
ChipletsLengths {
hash_chiplet_len: chiplets.bitwise_start().into(),
bitwise_chiplet_len: chiplets.memory_start() - chiplets.bitwise_start(),
memory_chiplet_len: chiplets.kernel_rom_start() - chiplets.memory_start(),
kernel_rom_len: chiplets.padding_start() - chiplets.kernel_rom_start(),
}
}
pub fn from_parts(
hash_len: usize,
bitwise_len: usize,
memory_len: usize,
kernel_len: usize,
) -> Self {
ChipletsLengths {
hash_chiplet_len: hash_len,
bitwise_chiplet_len: bitwise_len,
memory_chiplet_len: memory_len,
kernel_rom_len: kernel_len,
}
}
/// Returns the length of the hash chiplet trace
pub fn hash_chiplet_len(&self) -> usize {
self.hash_chiplet_len
}
/// Returns the length of the bitwise trace
pub fn bitwise_chiplet_len(&self) -> usize {
self.bitwise_chiplet_len
}
/// Returns the length of the memory trace
pub fn memory_chiplet_len(&self) -> usize {
self.memory_chiplet_len
}
/// Returns the length of the kernel ROM trace
pub fn kernel_rom_len(&self) -> usize {
self.kernel_rom_len
}
/// Returns the length of the trace required to accommodate chiplet components and 1
/// mandatory padding row required for ensuring sufficient trace length for auxiliary connector
/// columns that rely on the memory chiplet.
pub fn trace_len(&self) -> usize {
self.hash_chiplet_len()
+ self.bitwise_chiplet_len()
+ self.memory_chiplet_len()
+ self.kernel_rom_len()
+ 1
}
}
// AUXILIARY COLUMN BUILDER
// ================================================================================================
/// Defines a builder responsible for building a single column in an auxiliary segment of the
/// execution trace.
pub trait AuxColumnBuilder<E: FieldElement<BaseField = Felt>> {
// REQUIRED METHODS
// --------------------------------------------------------------------------------------------
fn get_requests_at(&self, main_trace: &MainTrace, alphas: &[E], row: RowIndex) -> E;
fn get_responses_at(&self, main_trace: &MainTrace, alphas: &[E], row: RowIndex) -> E;
// PROVIDED METHODS
// --------------------------------------------------------------------------------------------
fn init_requests(&self, _main_trace: &MainTrace, _alphas: &[E]) -> E {
E::ONE
}
fn init_responses(&self, _main_trace: &MainTrace, _alphas: &[E]) -> E {
E::ONE
}
/// Builds the chiplets bus auxiliary trace column.
fn build_aux_column(&self, main_trace: &MainTrace, alphas: &[E]) -> Vec<E> {
let mut responses_prod: Vec<E> = unsafe { uninit_vector(main_trace.num_rows()) };
let mut requests: Vec<E> = unsafe { uninit_vector(main_trace.num_rows()) };
responses_prod[0] = self.init_responses(main_trace, alphas);
requests[0] = self.init_requests(main_trace, alphas);
let mut requests_running_prod = E::ONE;
for row_idx in 0..main_trace.num_rows() - 1 {
let row = row_idx.into();
responses_prod[row_idx + 1] =
responses_prod[row_idx] * self.get_responses_at(main_trace, alphas, row);
requests[row_idx + 1] = self.get_requests_at(main_trace, alphas, row);
requests_running_prod *= requests[row_idx + 1];
}
let mut requests_running_divisor = requests_running_prod.inv();
let mut result_aux_column = responses_prod;
for i in (0..main_trace.num_rows()).rev() {
result_aux_column[i] *= requests_running_divisor;
requests_running_divisor *= requests[i];
}
result_aux_column
}
}
// TEST HELPERS
// ================================================================================================
#[cfg(test)]
pub fn build_span_with_respan_ops() -> (Vec<Operation>, Vec<Felt>) {
let iv = [1, 3, 5, 7, 9, 11, 13, 15, 17].to_elements();
let ops = vec![
Operation::Push(iv[0]),
Operation::Push(iv[1]),
Operation::Push(iv[2]),
Operation::Push(iv[3]),
Operation::Push(iv[4]),
Operation::Push(iv[5]),
Operation::Push(iv[6]),
// next batch
Operation::Push(iv[7]),
Operation::Push(iv[8]),
Operation::Add,
// drops to make sure stack overflow is empty on exit
Operation::Drop,
Operation::Drop,
Operation::Drop,
Operation::Drop,
Operation::Drop,
Operation::Drop,
Operation::Drop,
Operation::Drop,
];
(ops, iv)
}