Skip to content

Commit

Permalink
Create ConstraintPointEvaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 committed Feb 26, 2024
1 parent 9f30cac commit 076759a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
40 changes: 40 additions & 0 deletions src/core/air/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ use core::slice;

use super::{Component, ComponentTrace, ComponentVisitor};
use crate::core::backend::{Backend, CPUBackend};
use crate::core::circle::CirclePoint;
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::{Col, Field};
use crate::core::poly::circle::{CircleDomain, CircleEvaluation, CirclePoly};
use crate::core::{ColumnVec, ComponentVec};

/// Accumulates evaluations of u_i(P0) at a single point.
/// Computes f(P0), the combined polynomial at that point.
Expand Down Expand Up @@ -197,6 +199,44 @@ impl<'a, B: Backend> ComponentVisitor<B> for ConstraintEvaluator<'a, B> {
}
}

/// Evaluates components' constraint polynomials and aggregates them into a composition polynomial.
pub struct ConstraintPointEvaluator<'a> {
point: CirclePoint<SecureField>,
mask_values: slice::Iter<'a, ColumnVec<Vec<SecureField>>>,
evaluation_accumulator: PointEvaluationAccumulator,
}

impl<'a> ConstraintPointEvaluator<'a> {
pub fn new(
point: CirclePoint<SecureField>,
mask_values: &'a ComponentVec<Vec<SecureField>>,
max_log_size: u32,
random_coeff: SecureField,
) -> Self {
Self {
point,
mask_values: mask_values.iter(),
evaluation_accumulator: PointEvaluationAccumulator::new(random_coeff, max_log_size),
}
}

pub fn finalize(self) -> SecureField {
self.evaluation_accumulator.finalize()
}
}

impl<'a, B: Backend> ComponentVisitor<B> for ConstraintPointEvaluator<'a> {
fn visit<C: Component<B>>(&mut self, component: &C) {
component.evaluate_quotients_by_mask(
self.point,
self.mask_values
.next()
.expect("no more component mask values"),
&mut self.evaluation_accumulator,
)
}
}

#[cfg(test)]
mod tests {
use std::array;
Expand Down
21 changes: 19 additions & 2 deletions src/core/air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::ops::Deref;
use itertools::Itertools;

use self::evaluation::{
ConstraintEvaluator, DomainEvaluationAccumulator, PointEvaluationAccumulator,
ConstraintEvaluator, ConstraintPointEvaluator, DomainEvaluationAccumulator,
PointEvaluationAccumulator,
};
use super::backend::{Backend, CPUBackend};
use super::circle::CirclePoint;
Expand Down Expand Up @@ -68,6 +69,22 @@ pub trait AirExt: Air<CPUBackend> {
self.visit_components(&mut visitor);
visitor.finalize()
}

fn eval_composition_polynomial_at_point(
&self,
point: CirclePoint<SecureField>,
mask_values: &ComponentVec<Vec<SecureField>>,
random_coeff: SecureField,
) -> SecureField {
let mut evaluator = ConstraintPointEvaluator::new(
point,
mask_values,
self.max_constraint_log_degree_bound(),
random_coeff,
);
self.visit_components(&mut evaluator);
evaluator.finalize()
}
}

impl<A: Air<CPUBackend>> AirExt for A {}
Expand Down Expand Up @@ -247,7 +264,7 @@ pub trait Component<B: Backend> {
fn evaluate_quotients_by_mask(
&self,
point: CirclePoint<SecureField>,
mask: &[SecureField],
mask: &ColumnVec<Vec<SecureField>>,
evaluation_accumulator: &mut PointEvaluationAccumulator,
);

Expand Down
10 changes: 6 additions & 4 deletions src/fibonacci/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::{ExtensionOf, Field};
use crate::core::poly::circle::{CanonicCoset, CircleDomain};
use crate::core::ColumnVec;

pub struct FibonacciComponent {
pub log_size: u32,
Expand Down Expand Up @@ -135,14 +136,15 @@ impl Component<CPUBackend> for FibonacciComponent {
fn evaluate_quotients_by_mask(
&self,
point: CirclePoint<SecureField>,
mask: &[SecureField],
mask: &ColumnVec<Vec<SecureField>>,
evaluation_accumulator: &mut PointEvaluationAccumulator,
) {
let res = self.step_constraint_eval_quotient_by_mask(point, mask.try_into().unwrap());
let res =
self.step_constraint_eval_quotient_by_mask(point, &mask[0][..].try_into().unwrap());
let constraint_log_degree_bound = self.log_size + 1;
evaluation_accumulator.accumulate(constraint_log_degree_bound, res);
let res =
self.boundary_constraint_eval_quotient_by_mask(point, &mask[..1].try_into().unwrap());
let res = self
.boundary_constraint_eval_quotient_by_mask(point, &mask[0][..1].try_into().unwrap());
let constraint_log_degree_bound = self.log_size;
evaluation_accumulator.accumulate(constraint_log_degree_bound, res);
}
Expand Down
16 changes: 6 additions & 10 deletions src/fibonacci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use self::component::FibonacciComponent;
use crate::commitment_scheme::blake2_hash::Blake2sHasher;
use crate::commitment_scheme::hasher::Hasher;
use crate::commitment_scheme::merkle_decommitment::MerkleDecommitment;
use crate::core::air::evaluation::PointEvaluationAccumulator;
use crate::core::air::{AirExt, Component, ComponentTrace};
use crate::core::air::{AirExt, ComponentTrace};
use crate::core::backend::CPUBackend;
use crate::core::channel::{Blake2sChannel, Channel as ChannelTrait};
use crate::core::circle::CirclePoint;
Expand Down Expand Up @@ -207,14 +206,11 @@ pub fn verify_proof<const N_BITS: u32>(proof: FibonacciProof) -> bool {
let oods_point = CirclePoint::<SecureField>::get_random_point(channel);
let trace_oods_points = &fib.air.mask_points(oods_point)[0];

let mut evaluation_accumulator =
PointEvaluationAccumulator::new(random_coeff, fib.air.max_constraint_log_degree_bound());
fib.air.component.evaluate_quotients_by_mask(
let composition_polynomial_oods_value = fib.air.eval_composition_polynomial_at_point(
oods_point,
&proof.trace_oods_values[0][0],
&mut evaluation_accumulator,
&proof.trace_oods_values,
random_coeff,
);
let composition_polynomial_oods_value = evaluation_accumulator.finalize();

// TODO(AlonH): Get bounds from air.
let mut bounds = vec![CirclePolyDegreeBound::new(
Expand Down Expand Up @@ -334,7 +330,7 @@ mod tests {
);
fib.air.component.evaluate_quotients_by_mask(
point,
&mask_values[0],
&mask_values,
&mut evaluation_accumulator,
);
let oods_value = evaluation_accumulator.finalize();
Expand Down Expand Up @@ -420,7 +416,7 @@ mod tests {
);
fib.air.component.evaluate_quotients_by_mask(
oods_point,
&mask_values[0],
&mask_values,
&mut evaluation_accumulator,
);
let hz = evaluation_accumulator.finalize();
Expand Down

0 comments on commit 076759a

Please sign in to comment.