Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor STARK for small fields #945

Merged
merged 10 commits into from
Dec 20, 2024
9 changes: 7 additions & 2 deletions provers/stark/src/constraints/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::boundary::BoundaryConstraints;
use crate::debug::check_boundary_polys_divisibility;
use crate::domain::Domain;
use crate::trace::LDETraceTable;
use crate::traits::AIR;
use crate::traits::{TransitionEvaluationContext, AIR};
use crate::{frame::Frame, prover::evaluate_polynomial_on_lde_domain};
use itertools::Itertools;
#[cfg(not(feature = "parallel"))]
Expand All @@ -14,6 +14,7 @@ use rayon::{
iter::IndexedParallelIterator,
prelude::{IntoParallelIterator, ParallelIterator},
};

#[cfg(feature = "instruments")]
use std::time::Instant;

Expand Down Expand Up @@ -184,7 +185,11 @@ impl<A: AIR> ConstraintEvaluator<A> {

// Compute all the transition constraints at this point of the LDE domain.
let evaluations_transition =
air.compute_transition_prover(&frame, &periodic_values, rap_challenges);
air.compute_transition(&TransitionEvaluationContext::Prover {
frame: &frame,
periodic_values: &periodic_values,
rap_challenges,
});
entropidelic marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(all(debug_assertions, not(feature = "parallel")))]
transition_evaluations.push(evaluations_transition.clone());
Expand Down
6 changes: 2 additions & 4 deletions provers/stark/src/constraints/transition.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::ops::Div;

use crate::domain::Domain;
use crate::frame::Frame;
use crate::prover::evaluate_polynomial_on_lde_domain;
use crate::traits::TransitionEvaluationContext;
use itertools::Itertools;
use lambdaworks_math::field::element::FieldElement;
use lambdaworks_math::field::traits::{IsFFTField, IsField, IsSubFieldOf};
Expand Down Expand Up @@ -33,10 +33,8 @@ where
/// vector, in the index corresponding to the constraint as given by `constraint_idx()`.
fn evaluate(
&self,
frame: &Frame<F, E>,
evaluation_context: &TransitionEvaluationContext<F, E>,
transition_evaluations: &mut [FieldElement<E>],
periodic_values: &[FieldElement<F>],
rap_challenges: &[FieldElement<E>],
);

/// The periodicity the constraint is applied over the trace.
Expand Down
8 changes: 6 additions & 2 deletions provers/stark/src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::domain::Domain;
use super::traits::AIR;
use super::traits::{TransitionEvaluationContext, AIR};
use crate::{frame::Frame, trace::LDETraceTable};
use lambdaworks_math::{
field::{
Expand Down Expand Up @@ -93,7 +93,11 @@ pub fn validate_trace<A: AIR>(
.iter()
.map(|col| col[step].clone())
.collect();
let evaluations = air.compute_transition_prover(&frame, &periodic_values, rap_challenges);
let evaluations = air.compute_transition(&TransitionEvaluationContext::Prover {
frame: &frame,
periodic_values: &periodic_values,
rap_challenges,
});

// Iterate over each transition evaluation. When the evaluated step is not from
// the exemption steps corresponding to the transition, it should have zero as a
Expand Down
50 changes: 31 additions & 19 deletions provers/stark/src/examples/bit_flags.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::{
constraints::{boundary::BoundaryConstraints, transition::TransitionConstraint},
context::AirContext,
frame::Frame,
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
traits::{TransitionEvaluationContext, AIR},
Felt252,
};
use lambdaworks_math::field::{
Expand Down Expand Up @@ -45,11 +44,22 @@ impl TransitionConstraint<StarkField, StarkField> for BitConstraint {

fn evaluate(
&self,
frame: &Frame<StarkField, StarkField>,
transition_evaluations: &mut [Felt252],
_periodic_values: &[Felt252],
_rap_challenges: &[Felt252],
evaluation_context: &TransitionEvaluationContext<StarkField, StarkField>,
transition_evaluations: &mut [FieldElement<StarkField>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};
entropidelic marked this conversation as resolved.
Show resolved Hide resolved

let step = frame.get_evaluation_step(0);

let prefix_flag = step.get_main_evaluation_element(0, 0);
Expand Down Expand Up @@ -92,11 +102,22 @@ impl TransitionConstraint<StarkField, StarkField> for ZeroFlagConstraint {

fn evaluate(
&self,
frame: &Frame<StarkField, StarkField>,
transition_evaluations: &mut [FieldElement<Stark252PrimeField>],
_periodic_values: &[FieldElement<Stark252PrimeField>],
_rap_challenges: &[FieldElement<Stark252PrimeField>],
evaluation_context: &TransitionEvaluationContext<StarkField, StarkField>,
transition_evaluations: &mut [FieldElement<StarkField>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let step = frame.get_evaluation_step(0);
let zero_flag = step.get_main_evaluation_element(15, 0);

Expand Down Expand Up @@ -149,15 +170,6 @@ impl AIR for BitFlagsAIR {
&self.constraints
}

fn compute_transition_verifier(
&self,
frame: &Frame<Self::FieldExtension, Self::FieldExtension>,
periodic_values: &[FieldElement<Self::FieldExtension>],
rap_challenges: &[FieldElement<Self::FieldExtension>],
) -> Vec<FieldElement<Self::FieldExtension>> {
self.compute_transition_prover(frame, periodic_values, rap_challenges)
}

fn boundary_constraints(
&self,
_rap_challenges: &[FieldElement<Self::FieldExtension>],
Expand Down
46 changes: 29 additions & 17 deletions provers/stark/src/examples/dummy_air.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use crate::{
transition::TransitionConstraint,
},
context::AirContext,
frame::Frame,
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
traits::{TransitionEvaluationContext, AIR},
};
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
Expand Down Expand Up @@ -48,11 +47,22 @@ where

fn evaluate(
&self,
frame: &Frame<F, F>,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
_periodic_values: &[FieldElement<F>],
_rap_challenges: &[FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let first_step = frame.get_evaluation_step(0);
let second_step = frame.get_evaluation_step(1);
let third_step = frame.get_evaluation_step(2);
Expand Down Expand Up @@ -97,11 +107,22 @@ where

fn evaluate(
&self,
frame: &Frame<F, F>,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
_periodic_values: &[FieldElement<F>],
_rap_challenges: &[FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let first_step = frame.get_evaluation_step(0);

let bit = first_step.get_main_evaluation_element(0, 0);
Expand Down Expand Up @@ -186,15 +207,6 @@ impl AIR for DummyAIR {
fn pub_inputs(&self) -> &Self::PublicInputs {
&()
}

fn compute_transition_verifier(
&self,
frame: &Frame<Self::FieldExtension, Self::FieldExtension>,
periodic_values: &[FieldElement<Self::FieldExtension>],
rap_challenges: &[FieldElement<Self::FieldExtension>],
) -> Vec<FieldElement<Self::Field>> {
self.compute_transition_prover(frame, periodic_values, rap_challenges)
}
}

pub fn dummy_trace<F: IsFFTField>(trace_length: usize) -> TraceTable<F, F> {
Expand Down
46 changes: 29 additions & 17 deletions provers/stark/src/examples/fibonacci_2_cols_shifted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ use crate::{
transition::TransitionConstraint,
},
context::AirContext,
frame::Frame,
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
traits::{TransitionEvaluationContext, AIR},
};
use lambdaworks_math::{
field::{element::FieldElement, traits::IsFFTField},
Expand Down Expand Up @@ -46,11 +45,22 @@ where

fn evaluate(
&self,
frame: &Frame<F, F>,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
_periodic_values: &[FieldElement<F>],
_rap_challenges: &[FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let first_row = frame.get_evaluation_step(0);
let second_row = frame.get_evaluation_step(1);

Expand Down Expand Up @@ -94,11 +104,22 @@ where

fn evaluate(
&self,
frame: &Frame<F, F>,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
_periodic_values: &[FieldElement<F>],
_rap_challenges: &[FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let first_row = frame.get_evaluation_step(0);
let second_row = frame.get_evaluation_step(1);

Expand Down Expand Up @@ -223,15 +244,6 @@ where
fn pub_inputs(&self) -> &Self::PublicInputs {
&self.pub_inputs
}

fn compute_transition_verifier(
&self,
frame: &Frame<Self::FieldExtension, Self::FieldExtension>,
periodic_values: &[FieldElement<Self::FieldExtension>],
rap_challenges: &[FieldElement<Self::FieldExtension>],
) -> Vec<FieldElement<Self::Field>> {
self.compute_transition_prover(frame, periodic_values, rap_challenges)
}
}

pub fn compute_trace<F: IsFFTField>(
Expand Down
46 changes: 29 additions & 17 deletions provers/stark/src/examples/fibonacci_2_columns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use crate::{
transition::TransitionConstraint,
},
context::AirContext,
frame::Frame,
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
traits::{TransitionEvaluationContext, AIR},
};
use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField};

Expand Down Expand Up @@ -45,11 +44,22 @@ where

fn evaluate(
&self,
frame: &Frame<F, F>,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
_periodic_values: &[FieldElement<F>],
_rap_challenges: &[FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let first_step = frame.get_evaluation_step(0);
let second_step = frame.get_evaluation_step(1);

Expand Down Expand Up @@ -95,11 +105,22 @@ where

fn evaluate(
&self,
frame: &Frame<F, F>,
evaluation_context: &TransitionEvaluationContext<F, F>,
transition_evaluations: &mut [FieldElement<F>],
_periodic_values: &[FieldElement<F>],
_rap_challenges: &[FieldElement<F>],
) {
let (frame, _periodic_values, _rap_challenges) = match evaluation_context {
TransitionEvaluationContext::Prover {
frame,
periodic_values,
rap_challenges,
}
| TransitionEvaluationContext::Verifier {
frame,
periodic_values,
rap_challenges,
} => (frame, periodic_values, rap_challenges),
};

let first_step = frame.get_evaluation_step(0);
let second_step = frame.get_evaluation_step(1);

Expand Down Expand Up @@ -194,15 +215,6 @@ where
fn pub_inputs(&self) -> &Self::PublicInputs {
&self.pub_inputs
}

fn compute_transition_verifier(
&self,
frame: &Frame<Self::FieldExtension, Self::FieldExtension>,
periodic_values: &[FieldElement<Self::FieldExtension>],
rap_challenges: &[FieldElement<Self::FieldExtension>],
) -> Vec<FieldElement<Self::Field>> {
self.compute_transition_prover(frame, periodic_values, rap_challenges)
}
}

pub fn compute_trace<F: IsFFTField>(
Expand Down
Loading
Loading