Skip to content

Commit

Permalink
Create MultiFibonacci. (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 authored Mar 20, 2024
1 parent 2b9653e commit 3bc4c79
Showing 1 changed file with 61 additions and 35 deletions.
96 changes: 61 additions & 35 deletions src/fibonacci/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use num_traits::One;

use self::air::FibonacciAir;
use self::air::{FibonacciAir, MultiFibonacciAir};
use self::component::FibonacciComponent;
use crate::commitment_scheme::blake2_hash::Blake2sHasher;
use crate::commitment_scheme::hasher::Hasher;
Expand All @@ -17,19 +17,17 @@ mod component;

pub struct Fibonacci {
pub air: FibonacciAir,
pub claim: BaseField,
}

impl Fibonacci {
pub fn new(log_size: u32, claim: BaseField) -> Self {
let component = FibonacciComponent::new(log_size, claim);
Self {
air: FibonacciAir::new(component),
claim,
}
}

fn get_trace(&self) -> CPUCircleEvaluation<BaseField, BitReversedOrder> {
pub fn get_trace(&self) -> CPUCircleEvaluation<BaseField, BitReversedOrder> {
// Trace.
let trace_domain = CanonicCoset::new(self.air.component.log_size);
// TODO(AlonH): Consider using Vec::new instead of Vec::with_capacity throughout file.
Expand All @@ -51,44 +49,79 @@ impl Fibonacci {

pub fn prove(&self) -> Result<StarkProof, ProvingError> {
let trace = self.get_trace();
let channel =
&mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[self.claim])));
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[self
.air
.component
.claim])));
prove(&self.air, channel, vec![trace])
}

pub fn verify(&self, proof: StarkProof) -> Result<(), VerificationError> {
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[self
.air
.component
.claim])));
verify(proof, &self.air, channel)
}
}

pub fn verify_proof<const N_BITS: u32>(
proof: StarkProof,
claim: BaseField,
) -> Result<(), VerificationError> {
let fib = Fibonacci::new(N_BITS, claim);
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[claim])));
verify(proof, &fib.air, channel)
pub struct MultiFibonacci {
pub air: MultiFibonacciAir,
single_fib: Fibonacci,
n_components: usize,
}

impl MultiFibonacci {
pub fn new(n_components: usize, log_size: u32, claim: BaseField) -> Self {
assert!(n_components > 0);
let single_fib = Fibonacci::new(log_size, claim);
let air = MultiFibonacciAir::new(n_components, log_size, claim);
Self {
air,
single_fib,
n_components,
}
}

pub fn get_trace(&self) -> Vec<CPUCircleEvaluation<BaseField, BitReversedOrder>> {
vec![self.single_fib.get_trace(); self.n_components]
}

pub fn prove(&self) -> Result<StarkProof, ProvingError> {
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[self
.single_fib
.air
.component
.claim])));
prove(&self.air, channel, self.get_trace())
}

pub fn verify(&self, proof: StarkProof) -> Result<(), VerificationError> {
let channel = &mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[self
.single_fib
.air
.component
.claim])));
verify(proof, &self.air, channel)
}
}

#[cfg(test)]
mod tests {
use itertools::Itertools;
use num_traits::One;

use super::Fibonacci;
use crate::commitment_scheme::blake2_hash::Blake2sHasher;
use crate::commitment_scheme::hasher::Hasher;
use super::{Fibonacci, MultiFibonacci};
use crate::commitment_scheme::utils::tests::generate_test_queries;
use crate::core::air::evaluation::PointEvaluationAccumulator;
use crate::core::air::{AirExt, Component, ComponentTrace};
use crate::core::channel::{Blake2sChannel, Channel};
use crate::core::circle::CirclePoint;
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::fields::IntoSlice;
use crate::core::poly::circle::CanonicCoset;
use crate::core::prover::{prove, verify};
use crate::core::queries::Queries;
use crate::core::test_utils::secure_eval_to_base_eval;
use crate::core::utils::bit_reverse;
use crate::fibonacci::air::MultiFibonacciAir;
use crate::fibonacci::verify_proof;
use crate::{m31, qm31};

#[test]
Expand Down Expand Up @@ -214,7 +247,7 @@ mod tests {
.composition_polynomial_oods_value,
hz
);
verify_proof::<FIB_LOG_SIZE>(proof, fib.claim).unwrap();
fib.verify(proof).unwrap();
}

// TODO(AlonH): Check the correct error occurs after introducing errors instead of
Expand All @@ -228,7 +261,7 @@ mod tests {
let mut invalid_proof = fib.prove().unwrap();
invalid_proof.opened_values.0[0][0][4] += BaseField::one();

verify_proof::<FIB_LOG_SIZE>(invalid_proof, fib.claim).unwrap();
fib.verify(invalid_proof).unwrap();
}

// TODO(AlonH): Check the correct error occurs after introducing errors instead of
Expand All @@ -242,7 +275,7 @@ mod tests {
let mut invalid_proof = fib.prove().unwrap();
invalid_proof.trace_oods_values.swap(0, 1);

verify_proof::<FIB_LOG_SIZE>(invalid_proof, fib.claim).unwrap();
fib.verify(invalid_proof).unwrap();
}

// TODO(AlonH): Check the correct error occurs after introducing errors instead of
Expand All @@ -256,20 +289,13 @@ mod tests {
let mut invalid_proof = fib.prove().unwrap();
invalid_proof.opened_values.0[0][0].pop();

verify_proof::<FIB_LOG_SIZE>(invalid_proof, fib.claim).unwrap();
fib.verify(invalid_proof).unwrap();
}

#[test]
fn test_multi_fibonacci() {
let log_size = 5;
let (n_components, fib) = (16, Fibonacci::new(log_size, m31!(443693538)));
let air = MultiFibonacciAir::new(n_components, log_size, fib.claim);
let prover_channel =
&mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[fib.claim])));
let trace = vec![fib.get_trace(); n_components];
let proof = prove(&air, prover_channel, trace).unwrap();
let verifier_channel =
&mut Blake2sChannel::new(Blake2sHasher::hash(BaseField::into_slice(&[fib.claim])));
verify(proof, &air, verifier_channel).unwrap();
let multi_fib = MultiFibonacci::new(16, 5, m31!(443693538));
let proof = multi_fib.prove().unwrap();
multi_fib.verify(proof).unwrap();
}
}

0 comments on commit 3bc4c79

Please sign in to comment.