Skip to content

Commit

Permalink
Implement trace generator for fibonacci.
Browse files Browse the repository at this point in the history
  • Loading branch information
alonh5 committed Jun 26, 2024
1 parent f39cfea commit 7635b6b
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 7 deletions.
4 changes: 4 additions & 0 deletions crates/prover/src/core/prover/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,10 @@ mod tests {
) -> ColumnVec<CircleEvaluation<CpuBackend, BaseField, BitReversedOrder>> {
vec![]
}

fn to_component(self) -> impl Component {
self
}
}

impl ComponentProver<CpuBackend> for TestComponent {
Expand Down
48 changes: 43 additions & 5 deletions crates/prover/src/examples/fibonacci/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::core::prover::BASE_TRACE;
use crate::core::utils::bit_reverse_index;
use crate::core::{ColumnVec, InteractionElements, LookupValues};
use crate::trace_generation::registry::ComponentRegistry;
use crate::trace_generation::ComponentTraceGenerator;
use crate::trace_generation::{ComponentGen, ComponentTraceGenerator};

pub struct FibonacciComponent {
pub log_size: u32,
Expand Down Expand Up @@ -130,16 +130,46 @@ impl Component for FibonacciComponent {
}
}

impl ComponentTraceGenerator<CpuBackend> for FibonacciComponent {
type ComponentInputs = ();
pub struct FibonacciInput(pub u32, pub BaseField);

fn add_inputs(&mut self, _inputs: &Self::ComponentInputs) {}
impl ComponentGen for FibonacciInput {}

pub struct FibonacciTraceGenerator {
input_is_set: bool,
}

impl ComponentTraceGenerator<CpuBackend> for FibonacciTraceGenerator {
type ComponentInputs = FibonacciInput;

fn add_inputs(&mut self, inputs: &Self::ComponentInputs) {
assert!(
!self.input_is_set,
"Fibonacci input already set."
);
self.log_size = Some(inputs.0);
self.claim = Some(inputs.1);
}

fn write_trace(
_component_id: &str,
_registry: &mut ComponentRegistry,
) -> ColumnVec<CircleEvaluation<CpuBackend, BaseField, BitReversedOrder>> {
vec![]
let trace_domain = CanonicCoset::new(self.log_size);
// TODO(AlonH): Consider using Vec::new instead of Vec::with_capacity throughout file.
let mut trace = Vec::with_capacity(trace_domain.size());

// Fill trace with fibonacci squared.
let mut a = BaseField::one();
let mut b = BaseField::one();
for _ in 0..trace_domain.size() {
trace.push(a);
let tmp = a.square() + b.square();
a = b;
b = tmp;
}

// Returns as a CircleEvaluation.
vec![CircleEvaluation::new_canonical_ordered(trace_domain, trace)]
}

fn write_interaction_trace(
Expand All @@ -149,6 +179,14 @@ impl ComponentTraceGenerator<CpuBackend> for FibonacciComponent {
) -> ColumnVec<CircleEvaluation<CpuBackend, BaseField, BitReversedOrder>> {
vec![]
}

fn to_component(self) -> impl Component {
assert!(
self.log_size.is_some() && self.claim.is_some(),
"Fibonacci input not set."
);
FibonacciComponent::new(self.log_size.unwrap(), self.claim.unwrap())
}
}

impl ComponentProver<CpuBackend> for FibonacciComponent {
Expand Down
4 changes: 4 additions & 0 deletions crates/prover/src/examples/wide_fibonacci/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ impl ComponentTraceGenerator<CpuBackend> for WideFibComponent {
})
.collect_vec()
}

fn to_component(self) -> impl Component {
self
}
}

// Input for the fibonacci claim.
Expand Down
4 changes: 4 additions & 0 deletions crates/prover/src/examples/wide_fibonacci/simd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ impl ComponentTraceGenerator<SimdBackend> for SimdWideFibComponent {
) -> ColumnVec<CircleEvaluation<SimdBackend, BaseField, BitReversedOrder>> {
vec![]
}

fn to_component(self) -> impl Component {
self
}
}

impl ComponentProver<SimdBackend> for SimdWideFibComponent {
Expand Down
4 changes: 3 additions & 1 deletion crates/prover/src/trace_generation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod registry;
use downcast_rs::{impl_downcast, Downcast};
use registry::ComponentRegistry;

use crate::core::air::AirProver;
use crate::core::air::{AirProver, Component};
use crate::core::backend::Backend;
use crate::core::channel::Blake2sChannel;
use crate::core::fields::m31::BaseField;
Expand Down Expand Up @@ -38,6 +38,8 @@ pub trait ComponentTraceGenerator<B: Backend> {
trace: &ColumnVec<&CircleEvaluation<B, BaseField, BitReversedOrder>>,
elements: &InteractionElements,
) -> ColumnVec<CircleEvaluation<B, BaseField, BitReversedOrder>>;

fn to_component(self) -> impl Component;
}

pub trait AirTraceVerifier {
Expand Down
51 changes: 50 additions & 1 deletion crates/prover/src/trace_generation/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,58 @@ impl ComponentRegistry {
#[cfg(test)]
mod tests {
use super::*;
use crate::core::air::accumulation::PointEvaluationAccumulator;
use crate::core::air::Component;
use crate::core::backend::CpuBackend;
use crate::core::circle::CirclePoint;
use crate::core::fields::m31::BaseField;
use crate::core::fields::qm31::SecureField;
use crate::core::pcs::TreeVec;
use crate::core::poly::circle::CircleEvaluation;
use crate::core::poly::BitReversedOrder;
use crate::core::{ColumnVec, InteractionElements};
use crate::core::{ColumnVec, InteractionElements, LookupValues};
use crate::trace_generation::ComponentTraceGenerator;

impl Component for () {
fn n_constraints(&self) -> usize {
unimplemented!()
}

fn max_constraint_log_degree_bound(&self) -> u32 {
unimplemented!()
}

fn n_interaction_phases(&self) -> u32 {
unimplemented!()
}

fn trace_log_degree_bounds(&self) -> TreeVec<ColumnVec<u32>> {
unimplemented!()
}

fn mask_points(
&self,
_point: CirclePoint<SecureField>,
) -> TreeVec<ColumnVec<Vec<CirclePoint<SecureField>>>> {
unimplemented!()
}

fn interaction_element_ids(&self) -> Vec<String> {
unimplemented!()
}

fn evaluate_constraint_quotients_at_point(
&self,
_point: CirclePoint<SecureField>,
_mask: &ColumnVec<Vec<SecureField>>,
_evaluation_accumulator: &mut PointEvaluationAccumulator,
_interaction_elements: &InteractionElements,
_lookup_values: &LookupValues,
) {
unimplemented!()
}
}

#[derive(Default)]
struct ComponentA {
inputs: Vec<u32>,
Expand Down Expand Up @@ -68,6 +113,10 @@ mod tests {
) -> ColumnVec<CircleEvaluation<CpuBackend, BaseField, BitReversedOrder>> {
unimplemented!("TestTraceGenerator::write_interaction_trace")
}

fn to_component(self) -> impl Component {
unimplemented!("TestTraceGenerator::to_component")
}
}

#[test]
Expand Down

0 comments on commit 7635b6b

Please sign in to comment.