Skip to content

Commit

Permalink
Create MaskEvaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 committed Feb 25, 2024
1 parent ae9b3c5 commit b8fa192
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 12 deletions.
10 changes: 6 additions & 4 deletions src/core/air/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,18 @@ impl<'a> ColumnAccumulator<'a, CPUBackend> {

/// Evaluates components' constraint polynomials and aggregates them into a composition polynomial.
pub struct ConstraintEvaluator<'a, B: Backend> {
traces: slice::Iter<'a, ComponentTrace<'a, B>>,
component_traces: slice::Iter<'a, ComponentTrace<'a, B>>,
evaluation_accumulator: DomainEvaluationAccumulator<B>,
}

impl<'a> ConstraintEvaluator<'a, CPUBackend> {
pub fn new(
traces: &'a [ComponentTrace<'a, CPUBackend>],
component_traces: &'a [ComponentTrace<'a, CPUBackend>],
max_log_size: u32,
random_coeff: SecureField,
) -> Self {
Self {
traces: traces.iter(),
component_traces: component_traces.iter(),
evaluation_accumulator: DomainEvaluationAccumulator::new(random_coeff, max_log_size),
}
}
Expand All @@ -189,7 +189,9 @@ impl<'a> ConstraintEvaluator<'a, CPUBackend> {
impl<'a, B: Backend> ComponentVisitor<B> for ConstraintEvaluator<'a, B> {
fn visit<C: Component<B>>(&mut self, component: &C) {
component.evaluate_constraint_quotients_on_domain(
self.traces.next().expect("no more component traces"),
self.component_traces
.next()
.expect("no more component traces"),
&mut self.evaluation_accumulator,
)
}
Expand Down
55 changes: 54 additions & 1 deletion src/core/air/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::slice;
use std::iter::zip;
use std::ops::Deref;

Expand All @@ -9,7 +10,7 @@ use super::circle::CirclePoint;
use super::fields::m31::BaseField;
use super::fields::qm31::SecureField;
use super::poly::circle::{CanonicCoset, CirclePoly};
use super::ColumnVec;
use super::{ColumnVec, ComponentVec};

pub mod evaluation;

Expand Down Expand Up @@ -43,6 +44,19 @@ pub trait AirExt: Air<CPUBackend> {
self.visit_components(&mut evaluator);
evaluator.finalize()
}

fn mask_points_and_values(
&self,
point: CirclePoint<SecureField>,
component_traces: &[ComponentTrace<'_, CPUBackend>],
) -> (
ComponentVec<Vec<CirclePoint<SecureField>>>,
ComponentVec<Vec<SecureField>>,
) {
let mut visitor = MaskEvaluator::new(point, component_traces);
self.visit_components(&mut visitor);
visitor.finalize()
}
}

impl<A: Air<CPUBackend>> AirExt for A {}
Expand All @@ -69,6 +83,45 @@ impl<B: Backend> ComponentVisitor<B> for MaxConstraintLogDegreeBoundVisitor {
}
}

struct MaskEvaluator<'a, B: Backend> {
point: CirclePoint<SecureField>,
component_traces: slice::Iter<'a, ComponentTrace<'a, B>>,
component_points: ComponentVec<Vec<CirclePoint<SecureField>>>,
component_values: ComponentVec<Vec<SecureField>>,
}

impl<'a, B: Backend> MaskEvaluator<'a, B> {
pub fn new(
point: CirclePoint<SecureField>,
component_traces: &'a [ComponentTrace<'a, B>],
) -> Self {
Self {
point,
component_traces: component_traces.iter(),
component_points: Vec::new(),
component_values: Vec::new(),
}
}

pub fn finalize(
self,
) -> (
ComponentVec<Vec<CirclePoint<SecureField>>>,
ComponentVec<Vec<SecureField>>,
) {
(self.component_points, self.component_values)
}
}

impl<'a, B: Backend> ComponentVisitor<B> for MaskEvaluator<'a, B> {
fn visit<C: Component<B>>(&mut self, component: &C) {
let trace = self.component_traces.next().unwrap();
let (points, values) = component.mask_points_and_values(self.point, trace);
self.component_points.push(points);
self.component_values.push(values);
}
}

/// Holds the mask offsets at each column.
/// Holds a vector with an entry for each column. Each entry holds the offsets
/// of the mask at that column.
Expand Down
2 changes: 2 additions & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ pub mod utils;

/// A vector in which each element relates (by index) to a column in the trace.
pub type ColumnVec<T> = Vec<T>;
/// A vector of [ColumnVec]s. Each [ColumnVec] relates (by index) to a component in the air.
pub type ComponentVec<T> = Vec<ColumnVec<T>>;
13 changes: 6 additions & 7 deletions src/fibonacci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::core::oods::{get_oods_quotient, get_pair_oods_quotient, quotient_log_
use crate::core::poly::circle::{CanonicCoset, CircleEvaluation};
use crate::core::poly::BitReversedOrder;
use crate::core::proof_of_work::{ProofOfWork, ProofOfWorkProof};
use crate::core::ColumnVec;
use crate::core::ComponentVec;

type Channel = Blake2sChannel;
type MerkleHasher = Blake2sHasher;
Expand Down Expand Up @@ -57,7 +57,7 @@ pub struct FibonacciProof {
pub trace_decommitments: Vec<MerkleDecommitment<BaseField, MerkleHasher>>,
pub composition_polynomial_commitment: <MerkleHasher as Hasher>::Hash,
pub composition_polynomial_decommitment: MerkleDecommitment<SecureField, MerkleHasher>,
pub trace_oods_values: ColumnVec<Vec<SecureField>>,
pub trace_oods_values: ComponentVec<Vec<SecureField>>,
pub composition_polynomial_opened_values: Vec<SecureField>,
pub trace_opened_values: Vec<BaseField>,
pub proof_of_work: ProofOfWorkProof,
Expand Down Expand Up @@ -128,8 +128,7 @@ impl Fibonacci {
let oods_point = CirclePoint::<SecureField>::get_random_point(channel);
let (trace_oods_points, trace_oods_values) = self
.air
.component
.mask_points_and_values(oods_point, &component_traces[0]);
.mask_points_and_values(oods_point, &component_traces);
let composition_polynomial_oods_value =
composition_polynomial_commitment_scheme.polynomials[0].eval_at_point(oods_point);

Expand All @@ -144,7 +143,7 @@ impl Fibonacci {
)
.bit_reverse(),
);
for (point, value) in zip(&trace_oods_points[0], &trace_oods_values[0]) {
for (point, value) in zip(&trace_oods_points[0][0], &trace_oods_values[0][0]) {
oods_quotients.push(
get_pair_oods_quotient(*point, *value, &trace_commitment_scheme.evaluations[0])
.bit_reverse(),
Expand Down Expand Up @@ -217,7 +216,7 @@ pub fn verify_proof<const N_BITS: u32>(proof: FibonacciProof) -> bool {
PointEvaluationAccumulator::new(random_coeff, fib.air.max_constraint_log_degree_bound());
fib.air.component.evaluate_quotients_by_mask(
oods_point,
&proof.trace_oods_values[0],
&proof.trace_oods_values[0][0],
&mut evaluation_accumulator,
);
let composition_polynomial_oods_value = evaluation_accumulator.finalize();
Expand Down Expand Up @@ -268,7 +267,7 @@ pub fn verify_proof<const N_BITS: u32>(proof: FibonacciProof) -> bool {
}
assert!(opened_values.next().is_none(), "Not all values were used.");
sparse_circle_evaluations.push(SparseCircleEvaluation::new(evaluation));
for (oods_point, oods_value) in zip(&trace_oods_points[0], &proof.trace_oods_values[0]) {
for (oods_point, oods_value) in zip(&trace_oods_points[0], &proof.trace_oods_values[0][0]) {
let mut evaluation = Vec::with_capacity(trace_opening_positions.len());
let mut opened_values = proof.trace_opened_values.iter().copied();
for sub_circle_domain in trace_opening_positions.iter() {
Expand Down

0 comments on commit b8fa192

Please sign in to comment.