Skip to content

Commit dacb25f

Browse files
Schaeffleonardoaltgzanittigeorgwiese
authored
powdr changes (#5)
* balance accumulator tree * avoid eval_fri_log_up_phase * Tests & Dummy airs * ColumnsAir --------- Co-authored-by: Leo Alt <leo@powdrlabs.com> Co-authored-by: Gaston Zanitti <gzanitti@gmail.com> Co-authored-by: Georg Wiese <georgwiese@gmail.com>
1 parent 92171ba commit dacb25f

File tree

10 files changed

+57
-19
lines changed

10 files changed

+57
-19
lines changed

crates/stark-backend/src/air_builders/symbolic/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,14 +407,15 @@ impl<F: Field> InteractionPhaseAirBuilder for SymbolicRapBuilder<F> {
407407
self.interaction_partitions.replace(interaction_partitions);
408408
let perm_width = num_chunks + 1;
409409
self.after_challenge = Self::new_after_challenge(&[perm_width]);
410-
}
411410

412-
let phases_shapes = self.rap_phase_seq_kind.shape();
413-
let phase_shape = phases_shapes.first().unwrap();
411+
let phases_shapes = self.rap_phase_seq_kind.shape();
412+
413+
let phase_shape = phases_shapes.first().unwrap();
414414

415-
self.challenges = Self::new_challenges(&[phase_shape.num_challenges]);
416-
self.exposed_values_after_challenge =
417-
Self::new_exposed_values_after_challenge(&[phase_shape.num_exposed_values]);
415+
self.challenges = Self::new_challenges(&[phase_shape.num_challenges]);
416+
self.exposed_values_after_challenge =
417+
Self::new_exposed_values_after_challenge(&[phase_shape.num_exposed_values]);
418+
}
418419
}
419420
}
420421

crates/stark-backend/src/interaction/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ pub enum RapPhaseSeqKind {
254254
/// Up to one phase with prover/verifier given by [[fri_log_up::FriLogUpPhase]] and
255255
/// constraints given by [[fri_log_up::eval_fri_log_up_phase]].
256256
FriLogUp,
257+
None,
257258
}
258259

259260
impl RapPhaseSeqKind {
@@ -264,6 +265,7 @@ impl RapPhaseSeqKind {
264265
num_exposed_values: STARK_LU_NUM_EXPOSED_VALUES,
265266
extra_opening_rots: vec![],
266267
}],
268+
RapPhaseSeqKind::None => vec![],
267269
}
268270
}
269271
}

crates/stark-backend/src/interaction/rap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ where
3838
builder.max_constraint_degree(),
3939
);
4040
}
41+
RapPhaseSeqKind::None => {}
4142
}
4243
}
4344
}

crates/stark-backend/src/rap.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,32 @@ Rap<SymbolicRapBuilder<Val<SC>>> // for keygen to extract fixed data about the R
7373
+ for<'a> Rap<DebugConstraintBuilder<'a, SC>> // for debugging
7474
+ BaseAirWithPublicValues<Val<SC>>
7575
+ PartitionedBaseAir<Val<SC>>
76+
+ ColumnsAir<Val<SC>>
7677
+ Send + Sync
7778
{
7879
fn as_any(&self) -> &dyn Any;
7980
/// Name for display purposes
8081
fn name(&self) -> String;
8182
}
8283

84+
/// Trait for AIRs that can provide column names
85+
pub trait ColumnsAir<F>: BaseAir<F> {
86+
/// If available, returns the names of columns used in this AIR.
87+
/// If the result is `Some(names)`, `names.len() == air.width()` should always
88+
/// be true
89+
fn columns(&self) -> Option<Vec<String>> {
90+
None
91+
}
92+
}
93+
8394
impl<SC, T> AnyRap<SC> for T
8495
where
8596
SC: StarkGenericConfig,
8697
T: Rap<SymbolicRapBuilder<Val<SC>>>
8798
+ for<'a> Rap<DebugConstraintBuilder<'a, SC>>
8899
+ BaseAirWithPublicValues<Val<SC>>
89100
+ PartitionedBaseAir<Val<SC>>
101+
+ ColumnsAir<Val<SC>>
90102
+ Send
91103
+ Sync
92104
+ 'static,

crates/stark-backend/src/verifier/folder.rs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
marker::PhantomData,
3-
ops::{AddAssign, MulAssign},
3+
ops::{Add, AddAssign, MulAssign},
44
};
55

66
use p3_field::{ExtensionField, Field, FieldAlgebra};
@@ -59,15 +59,27 @@ where
5959
// We do a simple serial evaluation in topological order.
6060
// This can be parallelized if necessary.
6161
let exprs = self.eval_nodes(&dag.nodes);
62-
for &idx in &dag.constraint_idx {
63-
self.assert_zero(exprs[idx].clone());
64-
}
62+
let v: Vec<Expr> = dag
63+
.constraint_idx
64+
.iter()
65+
.map(|idx| exprs[*idx].clone())
66+
.rev()
67+
.scan(F::ONE.into(), |state: &mut Expr, next_elem| {
68+
let r = next_elem * state.clone();
69+
*state *= self.alpha;
70+
Some(r)
71+
})
72+
.collect();
73+
self.accumulator = balanced_sum_rec(&v);
6574
}
75+
}
6676

67-
pub fn assert_zero(&mut self, x: impl Into<Expr>) {
68-
let x = x.into();
69-
self.accumulator *= self.alpha;
70-
self.accumulator += x;
77+
fn balanced_sum_rec<E: Clone + Add<Output = E>>(v: &[E]) -> E {
78+
if v.len() == 1 {
79+
v[0].clone()
80+
} else {
81+
let (left, right) = v.split_at(v.len() / 2);
82+
balanced_sum_rec(left) + balanced_sum_rec(right)
7183
}
7284
}
7385

crates/stark-backend/tests/fib_selector_air/air.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Borrow;
33
use openvm_stark_backend::{
44
interaction::{InteractionBuilder, LookupBus},
55
p3_field::{Field, FieldAlgebra},
6-
rap::{BaseAirWithPublicValues, PartitionedBaseAir},
6+
rap::{BaseAirWithPublicValues, ColumnsAir, PartitionedBaseAir},
77
};
88
use openvm_stark_sdk::dummy_airs::fib_air::columns::{FibonacciCols, NUM_FIBONACCI_COLS};
99
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir, PairBuilder};
@@ -41,6 +41,8 @@ impl<F: Field> BaseAir<F> for FibonacciSelectorAir {
4141
}
4242
}
4343

44+
impl<F: Field> ColumnsAir<F> for FibonacciSelectorAir {}
45+
4446
impl<F: Field> BaseAirWithPublicValues<F> for FibonacciSelectorAir {
4547
fn num_public_values(&self) -> usize {
4648
3

crates/stark-backend/tests/fib_triples_air/air.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Borrow;
22

3-
use openvm_stark_backend::rap::{BaseAirWithPublicValues, PartitionedBaseAir};
3+
use openvm_stark_backend::rap::{BaseAirWithPublicValues, ColumnsAir, PartitionedBaseAir};
44
use p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir};
55
use p3_matrix::Matrix;
66

@@ -15,6 +15,8 @@ impl<F> BaseAir<F> for FibonacciAir {
1515
}
1616
}
1717

18+
impl<F> ColumnsAir<F> for FibonacciAir {}
19+
1820
impl<F> BaseAirWithPublicValues<F> for FibonacciAir {
1921
fn num_public_values(&self) -> usize {
2022
3

crates/stark-backend/tests/partitioned_sum_air/air.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use openvm_stark_backend::{
77
air_builders::PartitionedAirBuilder,
88
p3_field::FieldAlgebra,
9-
rap::{BaseAirWithPublicValues, PartitionedBaseAir},
9+
rap::{BaseAirWithPublicValues, ColumnsAir, PartitionedBaseAir},
1010
};
1111
use p3_air::{Air, BaseAir};
1212
use p3_matrix::Matrix;
@@ -29,6 +29,8 @@ impl<F> BaseAir<F> for SumAir {
2929
}
3030
}
3131

32+
impl<F> ColumnsAir<F> for SumAir {}
33+
3234
impl<AB: PartitionedAirBuilder> Air<AB> for SumAir {
3335
fn eval(&self, builder: &mut AB) {
3436
assert_eq!(builder.cached_mains().len(), 1);

crates/stark-sdk/src/dummy_airs/fib_air/air.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::borrow::Borrow;
33
use openvm_stark_backend::{
44
p3_air::{Air, AirBuilder, AirBuilderWithPublicValues, BaseAir},
55
p3_matrix::Matrix,
6-
rap::{BaseAirWithPublicValues, PartitionedBaseAir},
6+
rap::{BaseAirWithPublicValues, ColumnsAir, PartitionedBaseAir},
77
};
88

99
use super::columns::{FibonacciCols, NUM_FIBONACCI_COLS};
@@ -24,6 +24,8 @@ impl<F> BaseAirWithPublicValues<F> for FibonacciAir {
2424
}
2525
}
2626

27+
impl<F> ColumnsAir<F> for FibonacciAir {}
28+
2729
impl<AB: AirBuilderWithPublicValues> Air<AB> for FibonacciAir {
2830
fn eval(&self, builder: &mut AB) {
2931
let main = builder.main();

crates/stark-sdk/src/dummy_airs/interaction/dummy_interaction_air.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use openvm_stark_backend::{
2020
hal::TraceCommitter,
2121
types::{AirProofInput, AirProofRawInput, CommittedTraceData},
2222
},
23-
rap::{AnyRap, BaseAirWithPublicValues, PartitionedBaseAir},
23+
rap::{AnyRap, BaseAirWithPublicValues, ColumnsAir, PartitionedBaseAir},
2424
Chip, ChipUsageGetter,
2525
};
2626

@@ -45,6 +45,8 @@ pub struct DummyInteractionAir {
4545
pub partition: bool,
4646
}
4747

48+
impl<F: Field> ColumnsAir<F> for DummyInteractionAir {}
49+
4850
impl DummyInteractionAir {
4951
pub fn new(field_width: usize, is_send: bool, bus_index: BusIndex) -> Self {
5052
Self {

0 commit comments

Comments
 (0)