-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathcircuit_builder.rs
330 lines (302 loc) · 12.1 KB
/
circuit_builder.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
use core::unreachable;
use powdr_ast::parsed::visitor::AllChildren;
use powdr_executor_utils::expression_evaluator::{ExpressionEvaluator, TerminalAccess};
use std::collections::HashSet;
extern crate alloc;
use crate::stwo::prover::into_stwo_field;
use alloc::collections::btree_map::BTreeMap;
use powdr_ast::analyzed::{AlgebraicExpression, AlgebraicReference, Analyzed, Challenge, Identity};
use powdr_ast::analyzed::{PolyID, PolynomialType};
use powdr_number::Mersenne31Field as M31;
use stwo_prover::constraint_framework::preprocessed_columns::PreProcessedColumnId;
use stwo_prover::constraint_framework::{EvalAtRow, FrameworkComponent, FrameworkEval};
use stwo_prover::core::backend::{Column, ColumnOps};
use stwo_prover::core::fields::m31::BaseField;
use stwo_prover::core::poly::circle::{CircleDomain, CircleEvaluation};
use stwo_prover::core::poly::BitReversedOrder;
use stwo_prover::core::utils::{bit_reverse_index, coset_index_to_circle_domain_index};
pub const PREPROCESSED_TRACE_IDX: usize = 0;
pub const STAGE0_TRACE_IDX: usize = 1;
pub const STAGE1_TRACE_IDX: usize = 2;
pub type PowdrComponent = FrameworkComponent<PowdrEval>;
pub fn gen_stwo_circle_column<B>(
domain: CircleDomain,
slice: &[M31],
) -> CircleEvaluation<B, BaseField, BitReversedOrder>
where
B: ColumnOps<BaseField>,
{
assert!(
slice.len().ilog2() == domain.size().ilog2(),
"column size must be equal to domain size"
);
let mut column: <B as ColumnOps<BaseField>>::Column =
<B as ColumnOps<BaseField>>::Column::zeros(slice.len());
slice.iter().enumerate().for_each(|(i, v)| {
column.set(
bit_reverse_index(
coset_index_to_circle_domain_index(i, slice.len().ilog2()),
slice.len().ilog2(),
),
into_stwo_field(v),
);
});
CircleEvaluation::new(domain, column)
}
#[derive(Default, Clone)]
pub struct PowdrEval {
log_degree: u32,
pub analyzed: Analyzed<M31>,
// the pre-processed are indexed in the whole proof, instead of in each component.
// this offset represents the index of the first pre-processed column in this component
preprocess_col_offset: usize,
// The name of the public, the poly-id of the witness poly that this public is related to, the public value
pub(crate) publics_values: Vec<(String, PolyID, M31)>,
pub stage0_witness_columns: BTreeMap<PolyID, usize>,
stage1_witness_columns: BTreeMap<PolyID, usize>,
constant_shifted: BTreeMap<PolyID, usize>,
constant_columns: BTreeMap<PolyID, usize>,
// stwo supports maximum 2 stages, challenges are only created after stage 0
challenges: BTreeMap<u64, M31>,
pub poly_stage_map: BTreeMap<PolyID, usize>,
}
impl PowdrEval {
pub fn new(
analyzed: Analyzed<M31>,
preprocess_col_offset: usize,
log_degree: u32,
challenges: BTreeMap<u64, M31>,
public_values: BTreeMap<String, M31>,
) -> Self {
let stage0_witness_columns: BTreeMap<PolyID, usize> = analyzed
.definitions_in_source_order(PolynomialType::Committed)
.filter(|(symbol, _)| symbol.stage.unwrap_or(0) == 0)
.flat_map(|(symbol, _)| symbol.array_elements())
.enumerate()
.map(|(index, (_, id))| (id, index))
.collect();
let stage1_witness_columns: BTreeMap<PolyID, usize> = analyzed
.definitions_in_source_order(PolynomialType::Committed)
.filter(|(symbol, _)| symbol.stage.unwrap_or(0) == 1)
.flat_map(|(symbol, _)| symbol.array_elements())
.enumerate()
.map(|(index, (_, id))| (id, index))
.collect();
let constant_with_next_list = get_constant_with_next_list(&analyzed);
let constant_shifted: BTreeMap<PolyID, usize> = analyzed
.definitions_in_source_order(PolynomialType::Constant)
.flat_map(|(symbol, _)| symbol.array_elements())
.enumerate()
.filter(|(_, (name, _))| constant_with_next_list.contains(name))
.map(|(index, (_, id))| (id, index))
.collect();
let constant_columns: BTreeMap<PolyID, usize> = analyzed
.definitions_in_source_order(PolynomialType::Constant)
.flat_map(|(symbol, _)| symbol.array_elements())
.enumerate()
.map(|(index, (_, id))| (id, index))
.collect();
let publics_values = analyzed
.get_publics()
.into_iter()
.map(|(name, _, id, _, _)| (name.clone(), id, *public_values.get(&name).unwrap()))
.collect();
let poly_stage_map: BTreeMap<PolyID, usize> = stage0_witness_columns
.keys()
.map(|k| (*k, 0))
.chain(stage1_witness_columns.keys().map(|k| (*k, 1)))
.collect();
Self {
log_degree,
analyzed,
preprocess_col_offset,
publics_values,
stage0_witness_columns,
stage1_witness_columns,
constant_shifted,
constant_columns,
challenges,
poly_stage_map,
}
}
}
pub struct Data<'a, F> {
pub stage0_witness_eval: &'a BTreeMap<PolyID, [F; 2]>,
pub stage1_witness_eval: &'a BTreeMap<PolyID, [F; 2]>,
pub constant_shifted_eval: &'a BTreeMap<PolyID, F>,
pub constant_eval: &'a BTreeMap<PolyID, F>,
pub publics_values: &'a BTreeMap<String, F>,
// challenges for stage 1
pub challenges: &'a BTreeMap<u64, F>,
pub poly_stage_map: &'a BTreeMap<PolyID, usize>,
}
impl<F: Clone> TerminalAccess<F> for &Data<'_, F> {
fn get(&self, poly_ref: &AlgebraicReference) -> F {
match poly_ref.poly_id.ptype {
PolynomialType::Committed => {
match (self.poly_stage_map[&poly_ref.poly_id], poly_ref.next) {
(0, false) => self.stage0_witness_eval[&poly_ref.poly_id][0].clone(),
(0, true) => self.stage0_witness_eval[&poly_ref.poly_id][1].clone(),
(1, false) => self.stage1_witness_eval[&poly_ref.poly_id][0].clone(),
(1, true) => self.stage1_witness_eval[&poly_ref.poly_id][1].clone(),
_ => unreachable!(),
}
}
PolynomialType::Constant => match poly_ref.next {
false => self.constant_eval[&poly_ref.poly_id].clone(),
true => self.constant_shifted_eval[&poly_ref.poly_id].clone(),
},
PolynomialType::Intermediate => unreachable!(),
}
}
fn get_public(&self, public: &str) -> F {
self.publics_values
.get(public)
.expect("Referenced public value does not exist")
.clone()
}
fn get_challenge(&self, challenge: &Challenge) -> F {
self.challenges[&challenge.id].clone()
}
}
impl FrameworkEval for PowdrEval {
fn log_size(&self) -> u32 {
self.log_degree
}
fn max_constraint_log_degree_bound(&self) -> u32 {
self.log_degree + 1
}
fn evaluate<E: EvalAtRow>(&self, mut eval: E) -> E {
let stage0_witness_eval: BTreeMap<PolyID, [<E as EvalAtRow>::F; 2]> = self
.stage0_witness_columns
.keys()
.map(|poly_id| {
(
*poly_id,
eval.next_interaction_mask(STAGE0_TRACE_IDX, [0, 1]),
)
})
.collect();
let stage1_witness_eval: BTreeMap<PolyID, [<E as EvalAtRow>::F; 2]> = self
.stage1_witness_columns
.keys()
.map(|poly_id| {
(
*poly_id,
eval.next_interaction_mask(STAGE1_TRACE_IDX, [0, 1]),
)
})
.collect();
let constant_eval: BTreeMap<_, _> = self
.constant_columns
.keys()
.enumerate()
.map(|(i, poly_id)| {
(
*poly_id,
eval.get_preprocessed_column(PreProcessedColumnId {
id: (i + self.preprocess_col_offset).to_string(),
}),
)
})
.collect();
let constant_shifted_eval: BTreeMap<_, _> = self
.constant_shifted
.keys()
.enumerate()
.map(|(i, poly_id)| {
(
*poly_id,
eval.get_preprocessed_column(PreProcessedColumnId {
id: (i + constant_eval.len() + self.preprocess_col_offset).to_string(),
}),
)
})
.collect();
let challenges = self
.challenges
.iter()
.map(|(k, v)| (*k, E::F::from(into_stwo_field(v))))
.collect();
let intermediate_definitions = self.analyzed.intermediate_definitions();
let public_values_terminal = self
.publics_values
.iter()
.map(|(name, _, value)| (name.clone(), E::F::from(into_stwo_field(value))))
.collect();
let data = Data {
stage0_witness_eval: &stage0_witness_eval,
stage1_witness_eval: &stage1_witness_eval,
publics_values: &public_values_terminal,
constant_shifted_eval: &constant_shifted_eval,
constant_eval: &constant_eval,
challenges: &challenges,
poly_stage_map: &self.poly_stage_map,
};
// build selector columns and constraints for publics
self.publics_values
.iter()
.enumerate()
.for_each(|(index, (_, poly_id, value))| {
let selector = eval.get_preprocessed_column(PreProcessedColumnId {
id: (index
+ constant_eval.len()
+ self.preprocess_col_offset
+ constant_shifted_eval.len())
.to_string(),
});
let stage = self.poly_stage_map[poly_id];
let witness_col = match stage {
0 => stage0_witness_eval[poly_id][0].clone(),
1 => stage1_witness_eval[poly_id][0].clone(),
_ => unreachable!(),
};
// constraining s(i) * (pub[i] - x(i)) = 0
eval.add_constraint(selector * (E::F::from(into_stwo_field(value)) - witness_col));
});
let mut evaluator =
ExpressionEvaluator::new_with_custom_expr(&data, &intermediate_definitions, |v| {
E::F::from(into_stwo_field(v))
});
for id in &self.analyzed.identities {
match id {
Identity::Polynomial(identity) => {
let expr = evaluator.evaluate(&identity.expression);
eval.add_constraint(expr);
}
Identity::Connect(..) => {
unimplemented!("Connect is not implemented in stwo yet")
}
Identity::Lookup(..) => {
unimplemented!("Lookup is not implemented in stwo yet")
}
Identity::Permutation(..) => {
unimplemented!("Permutation is not implemented in stwo yet")
}
Identity::BusInteraction(..)
| Identity::PhantomPermutation(..)
| Identity::PhantomLookup(..)
| Identity::PhantomBusInteraction(..) => {}
}
}
eval
}
}
// This function creates a list of the names of the constant polynomials that have next references constraint
pub fn get_constant_with_next_list(analyzed: &Analyzed<M31>) -> HashSet<&String> {
let mut constant_with_next_list: HashSet<&String> = HashSet::new();
analyzed.all_children().for_each(|e| {
if let AlgebraicExpression::Reference(AlgebraicReference {
name,
poly_id,
next,
}) = e
{
if matches!(poly_id.ptype, PolynomialType::Constant) && *next {
// add the name of the constant polynomial to the list
constant_with_next_list.insert(name);
}
};
});
constant_with_next_list
}