Skip to content

Commit 0e99522

Browse files
chore: avoid iterating over constraints twice (#25)
Make `get_max_constraint_degree` take a slice of constraints instead of calling `get_symbolic_constraints` again (which was already called from outside).
1 parent dae0377 commit 0e99522

File tree

2 files changed

+21
-67
lines changed

2 files changed

+21
-67
lines changed

src/builder/symbolic.rs

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -322,63 +322,6 @@ impl<F: Field, T: Into<Self>> Product<T> for SymbolicExpression<F> {
322322
}
323323
}
324324

325-
pub fn get_log_quotient_degree<F, A>(
326-
air: &A,
327-
preprocessed_width: usize,
328-
stage_1_width: usize,
329-
stage_2_width: usize,
330-
num_public_values: usize,
331-
num_stage_2_public_values: usize,
332-
333-
is_zk: usize,
334-
) -> usize
335-
where
336-
F: Field,
337-
A: Air<SymbolicAirBuilder>,
338-
{
339-
assert!(is_zk <= 1, "is_zk must be either 0 or 1");
340-
// We pad to at least degree 2, since a quotient argument doesn't make sense with smaller degrees.
341-
let constraint_degree = (get_max_constraint_degree(
342-
air,
343-
preprocessed_width,
344-
stage_1_width,
345-
stage_2_width,
346-
num_public_values,
347-
num_stage_2_public_values,
348-
) + is_zk)
349-
.max(2);
350-
351-
// The quotient's actual degree is approximately (max_constraint_degree - 1) n,
352-
// where subtracting 1 comes from division by the vanishing polynomial.
353-
// But we pad it to a power of two so that we can efficiently decompose the quotient.
354-
log2_ceil_usize(constraint_degree - 1)
355-
}
356-
357-
pub fn get_max_constraint_degree<A>(
358-
air: &A,
359-
preprocessed_width: usize,
360-
stage_1_width: usize,
361-
stage_2_width: usize,
362-
num_public_values: usize,
363-
num_stage_2_public_values: usize,
364-
) -> usize
365-
where
366-
A: Air<SymbolicAirBuilder>,
367-
{
368-
get_symbolic_constraints(
369-
air,
370-
preprocessed_width,
371-
stage_1_width,
372-
stage_2_width,
373-
num_public_values,
374-
num_stage_2_public_values,
375-
)
376-
.iter()
377-
.map(|c| c.degree_multiple())
378-
.max()
379-
.unwrap_or(0)
380-
}
381-
382325
pub fn get_symbolic_constraints<A>(
383326
air: &A,
384327
preprocessed_width: usize,
@@ -401,6 +344,24 @@ where
401344
builder.constraints
402345
}
403346

347+
pub fn get_max_constraint_degree(constraints: &[SymbolicExpression<ExtVal>]) -> usize {
348+
constraints
349+
.iter()
350+
.map(|c| c.degree_multiple())
351+
.max()
352+
.unwrap_or(0)
353+
}
354+
355+
pub fn get_log_quotient_degree(max_constraint_degree: usize, is_zk: bool) -> usize {
356+
// We pad to at least degree 2, since a quotient argument doesn't make sense with smaller degrees.
357+
let constraint_degree = (max_constraint_degree + usize::from(is_zk)).max(2);
358+
359+
// The quotient's actual degree is approximately (max_constraint_degree - 1) n,
360+
// where subtracting 1 comes from division by the vanishing polynomial.
361+
// But we pad it to a power of two so that we can efficiently decompose the quotient.
362+
log2_ceil_usize(constraint_degree - 1)
363+
}
364+
404365
/// An `AirBuilder` for evaluating constraints symbolically, and recording them for later use.
405366
#[derive(Debug)]
406367
pub struct SymbolicAirBuilder {

src/system.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,16 @@ impl<A: BaseAir<Val> + Air<SymbolicAirBuilder>> Circuit<A> {
118118
let preprocessed_trace = air.preprocessed_trace();
119119
let preprocessed_height = preprocessed_trace.as_ref().map_or(0, |mat| mat.height());
120120
let preprocessed_width = preprocessed_trace.as_ref().map_or(0, |mat| mat.width());
121-
let constraint_count = get_symbolic_constraints(
122-
&air,
123-
preprocessed_width,
124-
stage_1_width,
125-
stage_2_width,
126-
io_size,
127-
LOOKUP_PUBLIC_SIZE,
128-
)
129-
.len();
130-
let max_constraint_degree = get_max_constraint_degree(
121+
let symbolic_constraints = get_symbolic_constraints(
131122
&air,
132123
preprocessed_width,
133124
stage_1_width,
134125
stage_2_width,
135126
io_size,
136127
LOOKUP_PUBLIC_SIZE,
137128
);
129+
let constraint_count = symbolic_constraints.len();
130+
let max_constraint_degree = get_max_constraint_degree(&symbolic_constraints);
138131
let circuit = Self {
139132
air,
140133
max_constraint_degree,

0 commit comments

Comments
 (0)