Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize prover boundary evaluations in construction of composition polynomial #22

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 46 additions & 10 deletions src/air/constraints/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use lambdaworks_math::{
};

use super::{boundary::BoundaryConstraints, evaluation_table::ConstraintEvaluationTable};
use crate::air::{frame::Frame, trace::TraceTable, AIR};
use crate::{
air::{frame::Frame, trace::TraceTable, AIR},
prover::evaluate_polynomial_on_lde_domain,
Domain,
};
use std::iter::zip;

pub struct ConstraintEvaluator<'poly, F: IsFFTField, A: AIR> {
Expand Down Expand Up @@ -36,7 +40,7 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
pub fn evaluate(
&self,
lde_trace: &TraceTable<F>,
lde_domain: &[FieldElement<F>],
domain: &Domain<F>,
alpha_and_beta_transition_coefficients: &[(FieldElement<F>, FieldElement<F>)],
alpha_and_beta_boundary_coefficients: &[(FieldElement<F>, FieldElement<F>)],
rap_challenges: &A::RAPChallenges,
Expand All @@ -47,7 +51,7 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
// The + 1 is for the boundary constraints column
let mut evaluation_table = ConstraintEvaluationTable::new(
self.air.context().num_transition_constraints() + 1,
lde_domain,
&domain.lde_roots_of_unity_coset,
);
let n_trace_colums = self.trace_polys.len();
let boundary_constraints = &self.boundary_constraints;
Expand Down Expand Up @@ -83,11 +87,37 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
#[cfg(debug_assertions)]
let mut transition_evaluations = Vec::new();

let mut boundary_polys_evaluations = Vec::with_capacity(boundary_polys.len());
for poly in boundary_polys.iter() {
let evaluations = evaluate_polynomial_on_lde_domain(
poly,
domain.blowup_factor,
domain.interpolation_domain_size,
&domain.coset_offset,
)
.unwrap();
boundary_polys_evaluations.push(evaluations);
}

let mut boundary_zerofiers_inverse_evaluations =
Vec::with_capacity(boundary_zerofiers.len());
for poly in boundary_zerofiers.iter() {
let mut evaluations = evaluate_polynomial_on_lde_domain(
poly,
domain.blowup_factor,
domain.interpolation_domain_size,
&domain.coset_offset,
)
.unwrap();
FieldElement::inplace_batch_inverse(&mut evaluations);
boundary_zerofiers_inverse_evaluations.push(evaluations);
}
Copy link
Member

Choose a reason for hiding this comment

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

We can evaluate the polynomials before collecting the original vectors, so we halve the memory taken by the whole operation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good idea. Done!


let divisors = self.air.transition_divisors();
let boundary_term_degree_adjustment =
self.air.composition_poly_degree_bound() - self.air.context().trace_length;
// Iterate over trace and domain and compute transitions
for (i, d) in lde_domain.iter().enumerate() {
for (i, d) in domain.lde_roots_of_unity_coset.iter().enumerate() {
let frame = Frame::read_from_trace(
lde_trace,
i,
Expand All @@ -109,16 +139,22 @@ impl<'poly, F: IsFFTField, A: AIR + AIR<Field = F>> ConstraintEvaluator<'poly, F
);

let d_adjustment_power = d.pow(boundary_term_degree_adjustment);
let boundary_evaluation = zip(&boundary_polys, &boundary_zerofiers)
.enumerate()
.map(|(index, (boundary_poly, boundary_zerofier))| {
let boundary_evaluation = zip(
&boundary_polys_evaluations,
&boundary_zerofiers_inverse_evaluations,
)
.enumerate()
.map(
|(index, (boundary_poly_evaluation, zerofier_inverse_evaluation))| {
let (boundary_alpha, boundary_beta) =
alpha_and_beta_boundary_coefficients[index].clone();

(boundary_poly.evaluate(d) / boundary_zerofier.evaluate(d))
&boundary_poly_evaluation[i]
* &zerofier_inverse_evaluation[i]
* (&boundary_alpha * &d_adjustment_power + &boundary_beta)
})
.fold(FieldElement::<F>::zero(), |acc, eval| acc + eval);
},
)
.fold(FieldElement::<F>::zero(), |acc, eval| acc + eval);

evaluations.push(boundary_evaluation);

Expand Down
4 changes: 2 additions & 2 deletions src/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ where
(trees, roots)
}

fn evaluate_polynomial_on_lde_domain<F>(
pub fn evaluate_polynomial_on_lde_domain<F>(
p: &Polynomial<FieldElement<F>>,
blowup_factor: usize,
domain_size: usize,
Expand Down Expand Up @@ -224,7 +224,7 @@ where

let constraint_evaluations = evaluator.evaluate(
&round_1_result.lde_trace,
&domain.lde_roots_of_unity_coset,
domain,
transition_coeffs,
boundary_coeffs,
&round_1_result.rap_challenges,
Expand Down