Skip to content

Commit

Permalink
Create FibonnaciAir.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 committed Feb 20, 2024
1 parent 2a3e8c0 commit 7b934d7
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 29 deletions.
34 changes: 34 additions & 0 deletions src/core/air/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//! Given a random alpha, the combined polynomial is defined as
//! f(p) = sum_i alpha^{N-1-i} u_i (P).
use core::slice;

use super::{ComponentTrace, ComponentVisitor};
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::Field;
Expand Down Expand Up @@ -158,6 +161,37 @@ impl<'a> ColumnAccumulator<'a> {
}
}

pub struct ConstraintEvaluator<'a> {
pub traces: slice::Iter<'a, &'a ComponentTrace<'a>>,
pub evaluation_accumulator: DomainEvaluationAccumulator,
}

impl<'a> ConstraintEvaluator<'a> {
pub fn new(
traces: &'a [&ComponentTrace<'a>],
max_log_size: u32,
random_coeff: SecureField,
) -> Self {
Self {
traces: traces.iter(),
evaluation_accumulator: DomainEvaluationAccumulator::new(random_coeff, max_log_size),
}
}

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

impl<'a> ComponentVisitor for ConstraintEvaluator<'a> {
fn visit<C: crate::core::air::Component>(&mut self, component: &C) {
component.evaluate_constraint_quotients_on_domain(
self.traces.next().expect("no more component traces"),
&mut self.evaluation_accumulator,
)
}
}

#[cfg(test)]
mod tests {
use std::array;
Expand Down
1 change: 1 addition & 0 deletions src/core/air/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub mod evaluation;
pub trait Air {
fn visit_components<V: ComponentVisitor>(&self, v: &mut V);
}

pub trait ComponentVisitor {
fn visit<C: Component>(&mut self, component: &C);
}
Expand Down
18 changes: 18 additions & 0 deletions src/fibonacci/air.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use super::component::FibonacciComponent;
use crate::core::air::{Air, ComponentVisitor};

pub struct FibonacciAir {
pub component: FibonacciComponent,
}

impl FibonacciAir {
pub fn new(component: FibonacciComponent) -> Self {
Self { component }
}
}

impl Air for FibonacciAir {
fn visit_components<V: ComponentVisitor>(&self, v: &mut V) {
v.visit(&self.component);
}
}
75 changes: 46 additions & 29 deletions src/fibonacci/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ use std::iter::zip;

use num_traits::One;

use self::air::FibonacciAir;
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::{DomainEvaluationAccumulator, PointEvaluationAccumulator};
use crate::core::air::{Component, ComponentExt, ComponentTrace};
use crate::core::air::evaluation::{
ConstraintEvaluator, DomainEvaluationAccumulator, PointEvaluationAccumulator,
};
use crate::core::air::{Air, Component, ComponentExt, ComponentTrace};
use crate::core::channel::{Blake2sChannel, Channel as ChannelTrait};
use crate::core::circle::CirclePoint;
use crate::core::commitment_scheme::{CommitmentSchemeProver, CommitmentSchemeVerifier};
Expand All @@ -25,6 +28,7 @@ use crate::core::proof_of_work::{ProofOfWork, ProofOfWorkProof};
type Channel = Blake2sChannel;
type MerkleHasher = Blake2sHasher;

mod air;
mod component;

const LOG_BLOWUP_FACTOR: u32 = 1;
Expand All @@ -34,7 +38,7 @@ const PROOF_OF_WORK_BITS: u32 = 12;
const N_QUERIES: usize = 3;

pub struct Fibonacci {
pub component: FibonacciComponent,
pub air: FibonacciAir,
pub trace_commitment_domain: CanonicCoset,
pub composition_polynomial_commitment_domain: CanonicCoset,
pub claim: BaseField,
Expand Down Expand Up @@ -66,8 +70,9 @@ impl Fibonacci {
let trace_commitment_domain = CanonicCoset::new(log_size + LOG_BLOWUP_FACTOR);
let composition_polynomial_commitment_domain =
CanonicCoset::new(log_size + 1 + LOG_BLOWUP_FACTOR);
let component = FibonacciComponent::new(log_size, claim);
Self {
component: FibonacciComponent { log_size, claim },
air: FibonacciAir::new(component),
trace_commitment_domain,
composition_polynomial_commitment_domain,
claim,
Expand All @@ -76,7 +81,7 @@ impl Fibonacci {

fn get_trace(&self) -> CircleEvaluation<BaseField> {
// Trace.
let trace_domain = CanonicCoset::new(self.component.log_size);
let trace_domain = CanonicCoset::new(self.air.component.log_size);
// TODO(AlonH): Consider using Vec::new instead of Vec::with_capacity throughout file.
let mut trace = Vec::with_capacity(trace_domain.size());

Expand All @@ -97,16 +102,10 @@ impl Fibonacci {
/// Returns the composition polynomial evaluations using the trace and a random coefficient.
fn compute_composition_polynomial(
&self,
random_coeff: SecureField,
trace: &ComponentTrace<'_>,
mut evaluator: ConstraintEvaluator<'_>,
) -> CirclePoly<SecureField> {
let mut accumulator = DomainEvaluationAccumulator::new(
random_coeff,
self.component.max_constraint_log_degree_bound(),
);
self.component
.evaluate_constraint_quotients_on_domain(trace, &mut accumulator);
accumulator.finalize()
self.air.visit_components(&mut evaluator);
evaluator.finalize()
}

pub fn prove(&self) -> FibonacciProof {
Expand All @@ -124,8 +123,15 @@ impl Fibonacci {
// Evaluate and commit on composition polynomial.
let random_coeff = channel.draw_random_secure_felts()[0];
let component_trace = ComponentTrace::new(vec![&trace_commitment_scheme.polynomials[0]]);
let composition_polynomial_poly =
self.compute_composition_polynomial(random_coeff, &component_trace);
let component_trace_slice = [&component_trace];
let evaluator = ConstraintEvaluator {
traces: component_trace_slice.iter(),
evaluation_accumulator: DomainEvaluationAccumulator::new(
random_coeff,
self.air.component.max_constraint_log_degree_bound(),
),
};
let composition_polynomial_poly = self.compute_composition_polynomial(evaluator);
let composition_polynomial_commitment_scheme = CommitmentSchemeProver::new(
vec![composition_polynomial_poly],
vec![self.composition_polynomial_commitment_domain],
Expand All @@ -135,6 +141,7 @@ impl Fibonacci {
// Evaluate the trace mask and the composition polynomial on the OODS point.
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_trace);
let composition_polynomial_oods_value =
Expand Down Expand Up @@ -213,13 +220,13 @@ pub fn verify_proof<const N_BITS: u32>(proof: FibonacciProof) -> bool {
let composition_polynomial_commitment_scheme =
CommitmentSchemeVerifier::new(proof.composition_polynomial_commitment, channel);
let oods_point = CirclePoint::<SecureField>::get_random_point(channel);
let trace_oods_points = fib.component.mask_points(oods_point);
let trace_oods_points = fib.air.component.mask_points(oods_point);

let mut evaluation_accumulator = PointEvaluationAccumulator::new(
random_coeff,
fib.component.max_constraint_log_degree_bound(),
fib.air.component.max_constraint_log_degree_bound(),
);
fib.component.evaluate_quotients_by_mask(
fib.air.component.evaluate_quotients_by_mask(
oods_point,
&proof.trace_oods_values[0],
&mut evaluation_accumulator,
Expand All @@ -228,9 +235,9 @@ pub fn verify_proof<const N_BITS: u32>(proof: FibonacciProof) -> bool {

// TODO(AlonH): Get bounds from air.
let mut bounds = vec![CirclePolyDegreeBound::new(
fib.component.max_constraint_log_degree_bound(),
fib.air.component.max_constraint_log_degree_bound(),
)];
bounds.append(&mut fib.component.get_quotient_log_bounds());
bounds.append(&mut fib.air.component.get_quotient_log_bounds());
let fri_config = FriConfig::new(LOG_LAST_LAYER_DEGREE_BOUND, LOG_BLOWUP_FACTOR, N_QUERIES);
let mut fri_verifier =
FriVerifier::commit(channel, fri_config, proof.fri_proof, bounds).unwrap();
Expand Down Expand Up @@ -304,7 +311,9 @@ mod tests {

use super::Fibonacci;
use crate::commitment_scheme::utils::tests::generate_test_queries;
use crate::core::air::evaluation::PointEvaluationAccumulator;
use crate::core::air::evaluation::{
ConstraintEvaluator, DomainEvaluationAccumulator, PointEvaluationAccumulator,
};
use crate::core::air::{Component, ComponentTrace};
use crate::core::circle::CirclePoint;
use crate::core::fields::m31::{BaseField, M31};
Expand All @@ -325,18 +334,26 @@ mod tests {
// TODO(ShaharS), Change to a channel implementation to retrieve the random
// coefficients from extension field.
let random_coeff = qm31!(2213980, 2213981, 2213982, 2213983);
let composition_polynomial_poly = fib.compute_composition_polynomial(random_coeff, &trace);
let component_trace_slice = [&trace];
let evaluator = ConstraintEvaluator {
traces: component_trace_slice.iter(),
evaluation_accumulator: DomainEvaluationAccumulator::new(
random_coeff,
fib.air.component.max_constraint_log_degree_bound(),
),
};
let composition_polynomial_poly = fib.compute_composition_polynomial(evaluator);

// Evaluate this polynomial at another point out of the evaluation domain and compare to
// what we expect.
let point = CirclePoint::<SecureField>::get_point(98989892);

let (_, mask_values) = fib.component.mask_points_and_values(point, &trace);
let (_, mask_values) = fib.air.component.mask_points_and_values(point, &trace);
let mut evaluation_accumulator = PointEvaluationAccumulator::new(
random_coeff,
fib.component.max_constraint_log_degree_bound(),
fib.air.component.max_constraint_log_degree_bound(),
);
fib.component.evaluate_quotients_by_mask(
fib.air.component.evaluate_quotients_by_mask(
point,
&mask_values[0],
&mut evaluation_accumulator,
Expand Down Expand Up @@ -415,14 +432,14 @@ mod tests {
let proof = fib.prove();
let oods_point = proof.additional_proof_data.oods_point;

let (_, mask_values) = fib.component.mask_points_and_values(oods_point, &trace);
let (_, mask_values) = fib.air.component.mask_points_and_values(oods_point, &trace);
let mut evaluation_accumulator = PointEvaluationAccumulator::new(
proof
.additional_proof_data
.composition_polynomial_random_coeff,
fib.component.max_constraint_log_degree_bound(),
fib.air.component.max_constraint_log_degree_bound(),
);
fib.component.evaluate_quotients_by_mask(
fib.air.component.evaluate_quotients_by_mask(
oods_point,
&mask_values[0],
&mut evaluation_accumulator,
Expand Down

0 comments on commit 7b934d7

Please sign in to comment.