Skip to content
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
38a6b2a
docs: add AirBuilder constraint evaluation spec and plan
diegokingston Apr 17, 2026
fde3928
feat: add AirBuilder trait, ProverBuilder, and VerifierBuilder
diegokingston Apr 17, 2026
60cc489
feat: add uses_builder and eval_constraints_with_builder to AIR trait
diegokingston Apr 17, 2026
dfdd4fc
fix: make AirBuilder parameterized for dyn-compatibility
diegokingston Apr 17, 2026
bc6c6b2
feat: add AirBuilder path to evaluator hot loop
diegokingston Apr 17, 2026
3858851
feat: add AirBuilder path to verifier
diegokingston Apr 17, 2026
a55147e
feat: add constraint helper functions for AirBuilder
diegokingston Apr 17, 2026
b0df4ae
feat: migrate MEMW_R to AirBuilder (3 constraints)
diegokingston Apr 17, 2026
0b19ae6
feat: migrate BRANCH and MEMW_A to AirBuilder (4+4 constraints)
diegokingston Apr 17, 2026
e9be4a8
feat: migrate MUL to AirBuilder (6 constraints)
diegokingston Apr 17, 2026
1330ad1
feat: migrate COMMIT, LOAD, MEMW, SHIFT to AirBuilder (45 constraints)
diegokingston Apr 17, 2026
6f879b5
feat: migrate DVRM to AirBuilder constraint evaluation
diegokingston Apr 17, 2026
04dd7da
feat: migrate CPU table to AirBuilder constraint evaluation (66 const…
diegokingston Apr 17, 2026
2ee2d37
refactor: remove old virtual-dispatch constraint evaluation paths
diegokingston Apr 18, 2026
c9534b2
fix: suppress clippy type_complexity for builder_fn field
diegokingston Apr 18, 2026
8cdc2d3
feat: dual-field AirBuilder infrastructure for base-field constraint …
diegokingston Apr 18, 2026
5b82ce3
perf: migrate all table constraint closures to base-field MainAirBuilder
diegokingston Apr 18, 2026
c18da78
fix: clippy warnings in main_builder closures
diegokingston Apr 18, 2026
ea69e3c
fix: add fallback paths for test AIRs without builders
diegokingston Apr 18, 2026
bd6a81e
perf: pre-compute alpha powers to eliminate E×E multiply per constraint
diegokingston Apr 18, 2026
2bd552f
perf: unfuse constraint accumulation for better pipelining
diegokingston Apr 18, 2026
b1a1d73
perf: in-place row cache fill + verifier alpha powers fix
diegokingston Apr 18, 2026
d9a6337
perf: eliminate dyn dispatch in constraint evaluation
diegokingston Apr 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions crypto/stark/src/air_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
use crate::frame::Frame;
use crate::trace::LDETraceTable;
use math::field::element::FieldElement;
use math::field::traits::{IsFFTField, IsField, IsSubFieldOf};

/// Plonky3-style builder for fused constraint evaluation + alpha combination.
///
/// Constraints call `assert_zero(expr)` which internally accumulates
/// alpha^i * expr into a running sum. No intermediate buffer, no vtable dispatch.
///
/// Parameterized by field type `F` (not an associated type) so that `dyn AirBuilder<F>`
/// is object-safe. This allows the AIR trait to remain dyn-compatible while supporting
/// the builder pattern: `eval_constraints(&self, builder: &mut dyn AirBuilder<E>)`.
pub trait AirBuilder<F: IsField> {
/// Read main trace column at (row_offset, col). offset=0 is current row.
fn main(&self, offset: usize, col: usize) -> FieldElement<F>;

/// Read aux trace column at (row_offset, col).
fn aux(&self, offset: usize, col: usize) -> FieldElement<F>;

/// Assert expr == 0. Internally: accumulator += alpha^constraint_idx * expr.
fn assert_zero(&mut self, expr: FieldElement<F>);

/// RAP challenge by index.
fn challenge(&self, idx: usize) -> &FieldElement<F>;

/// Pre-computed LogUp alpha powers.
fn logup_alpha_power(&self, idx: usize) -> &FieldElement<F>;

/// LogUp table offset (L/N).
fn logup_table_offset(&self) -> &FieldElement<F>;
}

pub struct ProverBuilder<'a, F: IsSubFieldOf<E> + IsFFTField, E: IsField> {
lde_trace: &'a LDETraceTable<F, E>,
row: usize,
step_size: usize,
num_rows: usize,
accumulator: FieldElement<E>,
alpha: FieldElement<E>,
alpha_power: FieldElement<E>,
rap_challenges: &'a [FieldElement<E>],
logup_alpha_powers: &'a [FieldElement<E>],
logup_table_offset_val: &'a FieldElement<E>,
}

impl<'a, F, E> ProverBuilder<'a, F, E>
where
F: IsSubFieldOf<E> + IsFFTField + Send + Sync,
E: IsField + Send + Sync,
{
pub fn new(
lde_trace: &'a LDETraceTable<F, E>,
row: usize,
alpha: &FieldElement<E>,
rap_challenges: &'a [FieldElement<E>],
logup_alpha_powers: &'a [FieldElement<E>],
logup_table_offset: &'a FieldElement<E>,
) -> Self {
Self {
lde_trace,
row,
step_size: lde_trace.lde_step_size,
num_rows: lde_trace.num_rows(),
accumulator: FieldElement::zero(),
alpha: alpha.clone(),
alpha_power: FieldElement::one(),
rap_challenges,
logup_alpha_powers,
logup_table_offset_val: logup_table_offset,
}
}

pub fn finish(self) -> FieldElement<E> {
self.accumulator
}
}

impl<'a, F, E> AirBuilder<E> for ProverBuilder<'a, F, E>
where
F: IsSubFieldOf<E> + IsFFTField + Send + Sync,
E: IsField + Send + Sync,
{
#[inline]
fn main(&self, offset: usize, col: usize) -> FieldElement<E> {
let lde_row = (self.row + offset * self.step_size) % self.num_rows;
self.lde_trace.get_main(lde_row, col).clone().to_extension()
}

#[inline]
fn aux(&self, offset: usize, col: usize) -> FieldElement<E> {
let lde_row = (self.row + offset * self.step_size) % self.num_rows;
self.lde_trace.get_aux(lde_row, col).clone()
}

#[inline]
fn assert_zero(&mut self, expr: FieldElement<E>) {
self.accumulator = &self.accumulator + &self.alpha_power * &expr;
self.alpha_power = &self.alpha_power * &self.alpha;
}

fn challenge(&self, idx: usize) -> &FieldElement<E> {
&self.rap_challenges[idx]
}

fn logup_alpha_power(&self, idx: usize) -> &FieldElement<E> {
&self.logup_alpha_powers[idx]
}

fn logup_table_offset(&self) -> &FieldElement<E> {
self.logup_table_offset_val
}
}

pub struct VerifierBuilder<'a, E: IsField> {
frame: &'a Frame<E, E>,
accumulator: FieldElement<E>,
alpha: FieldElement<E>,
alpha_power: FieldElement<E>,
rap_challenges: &'a [FieldElement<E>],
logup_alpha_powers: &'a [FieldElement<E>],
logup_table_offset_val: &'a FieldElement<E>,
}

impl<'a, E: IsField> VerifierBuilder<'a, E> {
pub fn new(
frame: &'a Frame<E, E>,
alpha: &FieldElement<E>,
rap_challenges: &'a [FieldElement<E>],
logup_alpha_powers: &'a [FieldElement<E>],
logup_table_offset: &'a FieldElement<E>,
) -> Self {
Self {
frame,
accumulator: FieldElement::zero(),
alpha: alpha.clone(),
alpha_power: FieldElement::one(),
rap_challenges,
logup_alpha_powers,
logup_table_offset_val: logup_table_offset,
}
}

pub fn finish(self) -> FieldElement<E> {
self.accumulator
}
}

impl<'a, E: IsField> AirBuilder<E> for VerifierBuilder<'a, E> {
#[inline]
fn main(&self, offset: usize, col: usize) -> FieldElement<E> {
self.frame
.get_evaluation_step(offset)
.get_main_evaluation_element(0, col)
.clone()
}

#[inline]
fn aux(&self, offset: usize, col: usize) -> FieldElement<E> {
self.frame
.get_evaluation_step(offset)
.get_aux_evaluation_element(0, col)
.clone()
}

#[inline]
fn assert_zero(&mut self, expr: FieldElement<E>) {
self.accumulator = &self.accumulator + &self.alpha_power * &expr;
self.alpha_power = &self.alpha_power * &self.alpha;
}

fn challenge(&self, idx: usize) -> &FieldElement<E> {
&self.rap_challenges[idx]
}

fn logup_alpha_power(&self, idx: usize) -> &FieldElement<E> {
&self.logup_alpha_powers[idx]
}

fn logup_table_offset(&self) -> &FieldElement<E> {
self.logup_table_offset_val
}
}
47 changes: 47 additions & 0 deletions crypto/stark/src/constraints/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ where
num_periodic: usize,
offsets: &[usize],
logup_table_offset: &FieldElement<FieldExtension>,
composition_alpha: &FieldElement<FieldExtension>,
) -> Vec<FieldElement<FieldExtension>> {
let is_uniform = zerofier_data.is_uniform();
let use_builder = air.uses_builder();

// Pre-compute LogUp alpha powers once for all LDE domain points.
let logup_alpha_powers: Vec<FieldElement<FieldExtension>> =
Expand Down Expand Up @@ -96,6 +98,24 @@ where
)
},
|(transition_buf, periodic_buf, frame), (i, boundary)| {
if use_builder {
let mut builder = crate::air_builder::ProverBuilder::new(
lde_trace,
i,
composition_alpha,
rap_challenges,
&logup_alpha_powers,
logup_table_offset,
);
air.eval_constraints_with_builder(&mut builder);
let acc = if is_uniform {
zerofier_data.get_uniform(i) * &builder.finish()
} else {
panic!("AirBuilder requires uniform zerofiers")
};
return acc + boundary;
}

frame.fill_from_lde(lde_trace, i, offsets);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: panic! inside the hot loop

panic!("AirBuilder requires uniform zerofiers") is evaluated once per LDE row (inside the parallel map_init closure). The same check appears again in the non-parallel path. Since is_uniform and use_builder are both fixed for the lifetime of this call, this should be validated once before the loop starts, not on every row. A production proof with millions of rows would panic mid-way through the computation rather than failing fast.

Move the check to the top of evaluate_transitions, before the iterator is constructed:

if use_builder && !is_uniform {
    panic!("AirBuilder requires uniform zerofiers");
}

Then the inner branches can use zerofier_data.get_uniform(i) unconditionally.


for (j, col) in lde_periodic_columns.iter().enumerate() {
Expand Down Expand Up @@ -148,6 +168,24 @@ where
.into_iter()
.enumerate()
.map(|(i, boundary)| {
if use_builder {
let mut builder = crate::air_builder::ProverBuilder::new(
lde_trace,
i,
composition_alpha,
rap_challenges,
&logup_alpha_powers,
logup_table_offset,
);
air.eval_constraints_with_builder(&mut builder);
let acc = if is_uniform {
zerofier_data.get_uniform(i) * &builder.finish()
} else {
panic!("AirBuilder requires uniform zerofiers")
};
return acc + boundary;
}

frame.fill_from_lde(lde_trace, i, offsets);

for (j, col) in lde_periodic_columns.iter().enumerate() {
Expand Down Expand Up @@ -315,6 +353,14 @@ where
let num_periodic = lde_periodic_columns.len();
let offsets = &air.context().transition_offsets;

// Extract the raw composition alpha (β) from the pre-computed powers [1, β, β², ...].
// The ProverBuilder generates its own powers internally via assert_zero().
let composition_alpha = if transition_coefficients.len() >= 2 {
transition_coefficients[1].clone()
} else {
FieldElement::one()
};

Self::evaluate_transitions(
air,
lde_trace,
Expand All @@ -327,6 +373,7 @@ where
num_periodic,
offsets,
&self.logup_table_offset,
&composition_alpha,
)
}
}
1 change: 1 addition & 0 deletions crypto/stark/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod air_builder;
#[cfg(feature = "debug-checks")]
pub mod bus_debug;
pub mod constraints;
Expand Down
Loading
Loading