Skip to content

Commit f124db1

Browse files
committed
Merge remote-tracking branch 'origin/main' into range_from_fixed
2 parents 7a5e180 + 379267d commit f124db1

27 files changed

+752
-422
lines changed

backend/src/halo2/circuit_builder.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use powdr_ast::analyzed::{
1818
AlgebraicExpression, AlgebraicReferenceThin, Identity, PolynomialIdentity, PolynomialType,
1919
SelectedExpressions,
2020
};
21-
use powdr_executor_utils::expression_evaluator::{ExpressionEvaluator, GlobalValues, TraceValues};
21+
use powdr_executor_utils::expression_evaluator::{ExpressionEvaluator, TerminalAccess};
2222
use powdr_number::FieldElement;
2323

2424
const FIRST_STEP_NAME: &str = "__first_step";
@@ -553,8 +553,8 @@ impl<'a, F: PrimeField<Repr = [u8; 32]>> Data<'a, '_, '_, F> {
553553
fn evaluator<T: FieldElement>(
554554
&self,
555555
intermediate_definitions: &'a BTreeMap<AlgebraicReferenceThin, AlgebraicExpression<T>>,
556-
) -> ExpressionEvaluator<'a, T, Expression<F>, &Self, &Self> {
557-
ExpressionEvaluator::new_with_custom_expr(self, self, intermediate_definitions, |n| {
556+
) -> ExpressionEvaluator<'a, T, Expression<F>, &Self> {
557+
ExpressionEvaluator::new_with_custom_expr(self, intermediate_definitions, |n| {
558558
Expression::Constant(convert_field(*n))
559559
})
560560
}
@@ -564,7 +564,7 @@ impl<'a, F: PrimeField<Repr = [u8; 32]>> Data<'a, '_, '_, F> {
564564
}
565565
}
566566

567-
impl<F: Field> TraceValues<Expression<F>> for &Data<'_, '_, '_, F> {
567+
impl<F: Field> TerminalAccess<Expression<F>> for &Data<'_, '_, '_, F> {
568568
fn get(&self, poly_ref: &powdr_ast::analyzed::AlgebraicReference) -> Expression<F> {
569569
let rotation = match poly_ref.next {
570570
false => Rotation::cur(),
@@ -578,9 +578,7 @@ impl<F: Field> TraceValues<Expression<F>> for &Data<'_, '_, '_, F> {
578578
panic!("Unknown reference: {}", poly_ref.name)
579579
}
580580
}
581-
}
582581

583-
impl<F: Field> GlobalValues<Expression<F>> for &Data<'_, '_, '_, F> {
584582
fn get_public(&self, _public: &str) -> Expression<F> {
585583
unimplemented!()
586584
}

backend/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub const DEFAULT_ESTARK_OPTIONS: &str = "stark_gl";
7373
impl BackendType {
7474
pub fn factory<T: FieldElement>(&self) -> Box<dyn BackendFactory<T>> {
7575
match self {
76-
BackendType::Mock => Box::new(mock::MockBackendFactory::new()),
76+
BackendType::Mock => Box::new(mock::MockBackendFactory),
7777
#[cfg(feature = "halo2")]
7878
BackendType::Halo2 => Box::new(halo2::Halo2ProverFactory),
7979
#[cfg(feature = "halo2")]

backend/src/mock/connection_constraint_checker.rs

+4-24
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::ops::ControlFlow;
55

66
use itertools::Itertools;
77
use powdr_ast::analyzed::AlgebraicExpression;
8-
use powdr_ast::analyzed::AlgebraicReference;
98
use powdr_ast::analyzed::Analyzed;
109
use powdr_ast::analyzed::{
1110
Identity, LookupIdentity, PermutationIdentity, PhantomLookupIdentity,
@@ -15,8 +14,7 @@ use powdr_ast::parsed::visitor::ExpressionVisitable;
1514
use powdr_ast::parsed::visitor::VisitOrder;
1615
use powdr_backend_utils::referenced_namespaces_algebraic_expression;
1716
use powdr_executor_utils::expression_evaluator::ExpressionEvaluator;
18-
use powdr_executor_utils::expression_evaluator::OwnedGlobalValues;
19-
use powdr_executor_utils::expression_evaluator::TraceValues;
17+
use powdr_executor_utils::expression_evaluator::TerminalAccess;
2018
use powdr_number::FieldElement;
2119
use rayon::iter::IntoParallelIterator;
2220
use rayon::iter::ParallelIterator;
@@ -152,25 +150,17 @@ impl<F: FieldElement> Connection<F> {
152150
pub struct ConnectionConstraintChecker<'a, F: FieldElement> {
153151
connections: &'a [Connection<F>],
154152
machines: BTreeMap<String, Machine<'a, F>>,
155-
global_values: OwnedGlobalValues<F>,
156153
}
157154

158155
impl<'a, F: FieldElement> ConnectionConstraintChecker<'a, F> {
159156
/// Creates a new connection constraint checker.
160157
pub fn new(
161158
connections: &'a [Connection<F>],
162159
machines: BTreeMap<String, Machine<'a, F>>,
163-
challenges: &'a BTreeMap<u64, F>,
164160
) -> Self {
165-
let global_values = OwnedGlobalValues {
166-
// TODO: Support publics.
167-
public_values: BTreeMap::new(),
168-
challenge_values: challenges.clone(),
169-
};
170161
Self {
171162
connections,
172163
machines,
173-
global_values,
174164
}
175165
}
176166
}
@@ -276,8 +266,7 @@ impl<'a, F: FieldElement> ConnectionConstraintChecker<'a, F> {
276266
.into_par_iter()
277267
.filter_map(|row| {
278268
let mut evaluator = ExpressionEvaluator::new(
279-
machine.trace_values.row(row),
280-
&self.global_values,
269+
machine.values.row(row),
281270
&machine.intermediate_definitions,
282271
);
283272
let result = evaluator.evaluate(&selected_expressions.selector);
@@ -300,9 +289,7 @@ impl<'a, F: FieldElement> ConnectionConstraintChecker<'a, F> {
300289
None => {
301290
let empty_variables = EmptyVariables {};
302291
let empty_definitions = BTreeMap::new();
303-
let empty_globals = OwnedGlobalValues::default();
304-
let mut evaluator =
305-
ExpressionEvaluator::new(empty_variables, &empty_globals, &empty_definitions);
292+
let mut evaluator = ExpressionEvaluator::new(empty_variables, &empty_definitions);
306293
let selector_value: F = evaluator.evaluate(&selected_expressions.selector);
307294

308295
match selector_value.to_degree() {
@@ -339,14 +326,7 @@ impl<'a, F: FieldElement> ConnectionConstraintChecker<'a, F> {
339326

340327
struct EmptyVariables;
341328

342-
impl<T> TraceValues<T> for EmptyVariables
343-
where
344-
T: FieldElement,
345-
{
346-
fn get(&self, _reference: &AlgebraicReference) -> T {
347-
panic!()
348-
}
349-
}
329+
impl<T: FieldElement> TerminalAccess<T> for EmptyVariables {}
350330

351331
/// Converts a slice to a multi-set, represented as a map from elements to their count.
352332
fn to_multi_set<T: Ord>(a: &[T]) -> BTreeMap<&T, usize> {

backend/src/mock/machine.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use itertools::Itertools;
44
use powdr_ast::analyzed::{AlgebraicExpression, AlgebraicReferenceThin, Analyzed};
55
use powdr_backend_utils::{machine_fixed_columns, machine_witness_columns};
66
use powdr_executor::constant_evaluator::VariablySizedColumn;
7-
use powdr_executor_utils::{expression_evaluator::OwnedTraceValues, WitgenCallback};
7+
use powdr_executor_utils::{expression_evaluator::OwnedTerminalValues, WitgenCallback};
88
use powdr_number::{DegreeType, FieldElement};
99

1010
/// A collection of columns with self-contained constraints.
1111
pub struct Machine<'a, F> {
1212
pub machine_name: String,
1313
pub size: usize,
14-
pub trace_values: OwnedTraceValues<F>,
14+
pub values: OwnedTerminalValues<F>,
1515
pub pil: &'a Analyzed<F>,
1616
pub intermediate_definitions: BTreeMap<AlgebraicReferenceThin, AlgebraicExpression<F>>,
1717
}
@@ -55,12 +55,14 @@ impl<'a, F: FieldElement> Machine<'a, F> {
5555

5656
let intermediate_definitions = pil.intermediate_definitions();
5757

58-
let trace_values = OwnedTraceValues::new(pil, witness, fixed);
58+
// TODO: Supports publics.
59+
let values =
60+
OwnedTerminalValues::new(pil, witness, fixed).with_challenges(challenges.clone());
5961

6062
Some(Self {
6163
machine_name,
6264
size,
63-
trace_values,
65+
values,
6466
pil,
6567
intermediate_definitions,
6668
})

backend/src/mock/mod.rs

+8-20
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::{
22
collections::BTreeMap,
33
hash::{DefaultHasher, Hash, Hasher},
44
io,
5-
marker::PhantomData,
65
path::PathBuf,
76
sync::Arc,
87
};
@@ -24,19 +23,9 @@ mod connection_constraint_checker;
2423
mod machine;
2524
mod polynomial_constraint_checker;
2625

27-
pub(crate) struct MockBackendFactory<F: FieldElement> {
28-
_marker: PhantomData<F>,
29-
}
30-
31-
impl<F: FieldElement> MockBackendFactory<F> {
32-
pub(crate) const fn new() -> Self {
33-
Self {
34-
_marker: PhantomData,
35-
}
36-
}
37-
}
26+
pub(crate) struct MockBackendFactory;
3827

39-
impl<F: FieldElement> BackendFactory<F> for MockBackendFactory<F> {
28+
impl<F: FieldElement> BackendFactory<F> for MockBackendFactory {
4029
fn create(
4130
&self,
4231
pil: Arc<Analyzed<F>>,
@@ -128,14 +117,13 @@ impl<F: FieldElement> Backend<F> for MockBackend<F> {
128117
);
129118
}
130119

131-
let is_ok =
132-
machines.values().all(|machine| {
133-
!PolynomialConstraintChecker::new(machine, &challenges)
134-
.check()
135-
.has_errors()
136-
}) && ConnectionConstraintChecker::new(&self.connections, machines, &challenges)
120+
let is_ok = machines.values().all(|machine| {
121+
!PolynomialConstraintChecker::new(machine)
137122
.check()
138-
.is_ok();
123+
.has_errors()
124+
}) && ConnectionConstraintChecker::new(&self.connections, machines)
125+
.check()
126+
.is_ok();
139127

140128
match is_ok {
141129
true => Ok(Vec::new()),

backend/src/mock/polynomial_constraint_checker.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,19 @@ use powdr_ast::{
44
analyzed::{AlgebraicExpression, Identity, PolynomialIdentity},
55
parsed::visitor::AllChildren,
66
};
7-
use powdr_executor_utils::expression_evaluator::{ExpressionEvaluator, OwnedGlobalValues};
7+
use powdr_executor_utils::expression_evaluator::ExpressionEvaluator;
88
use powdr_number::FieldElement;
99
use rayon::iter::{IntoParallelIterator, ParallelIterator};
1010

1111
use super::machine::Machine;
1212

1313
pub struct PolynomialConstraintChecker<'a, F> {
1414
machine: &'a Machine<'a, F>,
15-
global_values: OwnedGlobalValues<F>,
1615
}
1716

1817
impl<'a, F: FieldElement> PolynomialConstraintChecker<'a, F> {
19-
pub fn new(machine: &'a Machine<'a, F>, challenges: &'a BTreeMap<u64, F>) -> Self {
20-
let global_values = OwnedGlobalValues {
21-
// TODO: Support publics
22-
public_values: Default::default(),
23-
challenge_values: challenges.clone(),
24-
};
25-
Self {
26-
machine,
27-
global_values,
28-
}
18+
pub fn new(machine: &'a Machine<'a, F>) -> Self {
19+
Self { machine }
2920
}
3021

3122
pub fn check(&self) -> MachineResult<'a, F> {
@@ -59,8 +50,7 @@ impl<'a, F: FieldElement> PolynomialConstraintChecker<'a, F> {
5950
identities: &[&'a Identity<F>],
6051
) -> Vec<FailingPolynomialConstraint<'a, F>> {
6152
let mut evaluator = ExpressionEvaluator::new(
62-
self.machine.trace_values.row(row),
63-
&self.global_values,
53+
self.machine.values.row(row),
6454
&self.machine.intermediate_definitions,
6555
);
6656
identities

backend/src/stwo/circuit_builder.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use core::unreachable;
22
use powdr_ast::parsed::visitor::AllChildren;
3-
use powdr_executor_utils::expression_evaluator::{ExpressionEvaluator, GlobalValues, TraceValues};
3+
use powdr_executor_utils::expression_evaluator::{ExpressionEvaluator, TerminalAccess};
44
use std::collections::HashSet;
55
use std::sync::Arc;
66

@@ -100,7 +100,7 @@ struct Data<'a, F> {
100100
constant_eval: &'a BTreeMap<PolyID, F>,
101101
}
102102

103-
impl<F: Clone> TraceValues<F> for &Data<'_, F> {
103+
impl<F: Clone> TerminalAccess<F> for &Data<'_, F> {
104104
fn get(&self, poly_ref: &AlgebraicReference) -> F {
105105
match poly_ref.poly_id.ptype {
106106
PolynomialType::Committed => match poly_ref.next {
@@ -114,12 +114,11 @@ impl<F: Clone> TraceValues<F> for &Data<'_, F> {
114114
PolynomialType::Intermediate => unreachable!(),
115115
}
116116
}
117-
}
118117

119-
impl<F> GlobalValues<F> for &Data<'_, F> {
120118
fn get_public(&self, _public: &str) -> F {
121119
unimplemented!("Public references are not supported in stwo yet")
122120
}
121+
123122
fn get_challenge(&self, _challenge: &Challenge) -> F {
124123
unimplemented!("challenges are not supported in stwo yet")
125124
}
@@ -182,12 +181,10 @@ impl<T: FieldElement> FrameworkEval for PowdrEval<T> {
182181
constant_shifted_eval: &constant_shifted_eval,
183182
constant_eval: &constant_eval,
184183
};
185-
let mut evaluator = ExpressionEvaluator::new_with_custom_expr(
186-
&data,
187-
&data,
188-
&intermediate_definitions,
189-
|v| E::F::from(M31::from(v.try_into_i32().unwrap())),
190-
);
184+
let mut evaluator =
185+
ExpressionEvaluator::new_with_custom_expr(&data, &intermediate_definitions, |v| {
186+
E::F::from(M31::from(v.try_into_i32().unwrap()))
187+
});
191188

192189
for id in &self.analyzed.identities {
193190
match id {

0 commit comments

Comments
 (0)