From 400eb5067004c670111072e100bd2375ef1050be Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Fri, 29 Nov 2024 15:14:33 -0300 Subject: [PATCH 1/7] add enum for evaluate transitions constraints --- provers/stark/src/debug.rs | 8 +++++-- provers/stark/src/traits.rs | 45 +++++++++++++++++++++-------------- provers/stark/src/verifier.rs | 13 +++++----- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/provers/stark/src/debug.rs b/provers/stark/src/debug.rs index 36bee2922..fe3553614 100644 --- a/provers/stark/src/debug.rs +++ b/provers/stark/src/debug.rs @@ -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::{ @@ -93,7 +93,11 @@ pub fn validate_trace( .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 diff --git a/provers/stark/src/traits.rs b/provers/stark/src/traits.rs index 56a96b89b..dcddde4ec 100644 --- a/provers/stark/src/traits.rs +++ b/provers/stark/src/traits.rs @@ -18,6 +18,29 @@ use super::{ type ZerofierGroupKey = (usize, usize, Option, Option, usize); +/// This enum is necessary because, while both the prover and verifier perform the same operations +/// to compute transition constraints, their frames differ. +/// The prover uses a frame containing elements from both the base field and its extension +/// (common when working with small fields and challengers in the extension). +/// In contrast, the verifier, lacking access to the trace and relying solely on evaluations at the challengers, +/// works with a frame that contains only elements from the extension. +pub enum TransitionEvaluationContext<'a, F, E> +where + F: IsSubFieldOf, + E: IsField, +{ + Prover { + frame: &'a Frame<'a, F, E>, + periodic_values: &'a [FieldElement], + rap_challenges: &'a [FieldElement], + }, + Verifier { + frame: &'a Frame<'a, E, E>, + periodic_values: &'a [FieldElement], + rap_challenges: &'a [FieldElement], + }, +} + /// AIR is a representation of the Constraints pub trait AIR { type Field: IsFFTField + IsSubFieldOf + Send + Sync; @@ -65,17 +88,16 @@ pub trait AIR { /// The method called by the prover to evaluate the transitions corresponding to an evaluation frame. /// In the case of the prover, the main evaluation table of the frame takes values in /// `Self::Field`, since they are the evaluations of the main trace at the LDE domain. - fn compute_transition_prover( + /// In the case of the verifier, the frame take elements of Self::FieldExtension. + fn compute_transition( &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], + evaluation_context: &TransitionEvaluationContext, ) -> Vec> { let mut evaluations = vec![FieldElement::::zero(); self.num_transition_constraints()]; self.transition_constraints() .iter() - .for_each(|c| c.evaluate(frame, &mut evaluations, periodic_values, rap_challenges)); + .for_each(|c| c.evaluate(evaluation_context, &mut evaluations)); evaluations } @@ -85,19 +107,6 @@ pub trait AIR { rap_challenges: &[FieldElement], ) -> BoundaryConstraints; - /// The method called by the verifier to evaluate the transitions at the out of domain frame. - /// In the case of the verifier, both main and auxiliary tables of the evaluation frame take - /// values in `Self::FieldExtension`, since they are the evaluations of the trace polynomials - /// at the out of domain challenge. - /// In case `Self::Field` coincides with `Self::FieldExtension`, this method and - /// `compute_transition_prover` should return the same values. - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec>; - fn context(&self) -> &AirContext; fn trace_length(&self) -> usize; diff --git a/provers/stark/src/verifier.rs b/provers/stark/src/verifier.rs index 9f7a32df3..a1ca0effd 100644 --- a/provers/stark/src/verifier.rs +++ b/provers/stark/src/verifier.rs @@ -4,7 +4,7 @@ use super::{ fri::fri_decommit::FriDecommitment, grinding, proof::{options::ProofOptions, stark::StarkProof}, - traits::AIR, + traits::{TransitionEvaluationContext, AIR}, }; use crate::{config::Commitment, proof::stark::DeepPolynomialOpening}; use lambdaworks_crypto::{fiat_shamir::is_transcript::IsTranscript, merkle_tree::proof::Proof}; @@ -274,11 +274,12 @@ pub trait IsStarkVerifier { let ood_frame = (proof.trace_ood_evaluations).into_frame(num_main_trace_columns, A::STEP_SIZE); - let transition_ood_frame_evaluations = air.compute_transition_verifier( - &ood_frame, - &periodic_values, - &challenges.rap_challenges, - ); + let transition_ood_frame_evaluations = + air.compute_transition(&TransitionEvaluationContext::Verifier { + frame: &ood_frame, + periodic_values: &periodic_values, + rap_challenges: &challenges.rap_challenges, + }); let mut denominators = vec![FieldElement::::zero(); air.num_transition_constraints()]; From e0032be10688bc822a920a7535ac35ca223f590d Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Fri, 29 Nov 2024 15:16:13 -0300 Subject: [PATCH 2/7] change constraints evaluator fn --- provers/stark/src/constraints/evaluator.rs | 9 +++++++-- provers/stark/src/constraints/transition.rs | 5 ++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/provers/stark/src/constraints/evaluator.rs b/provers/stark/src/constraints/evaluator.rs index ff3b042a6..defac9fe2 100644 --- a/provers/stark/src/constraints/evaluator.rs +++ b/provers/stark/src/constraints/evaluator.rs @@ -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"))] @@ -14,6 +14,7 @@ use rayon::{ iter::IndexedParallelIterator, prelude::{IntoParallelIterator, ParallelIterator}, }; + #[cfg(feature = "instruments")] use std::time::Instant; @@ -184,7 +185,11 @@ impl ConstraintEvaluator { // 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, + }); #[cfg(all(debug_assertions, not(feature = "parallel")))] transition_evaluations.push(evaluations_transition.clone()); diff --git a/provers/stark/src/constraints/transition.rs b/provers/stark/src/constraints/transition.rs index c69c4b7b6..8c49cbd44 100644 --- a/provers/stark/src/constraints/transition.rs +++ b/provers/stark/src/constraints/transition.rs @@ -3,6 +3,7 @@ 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}; @@ -33,10 +34,8 @@ where /// vector, in the index corresponding to the constraint as given by `constraint_idx()`. fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], ); /// The periodicity the constraint is applied over the trace. From eb535c3686352bc070c9dbdadd4640f0b5c1daf6 Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Fri, 29 Nov 2024 15:28:39 -0300 Subject: [PATCH 3/7] fix fib example --- provers/stark/src/examples/simple_fibonacci.rs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/provers/stark/src/examples/simple_fibonacci.rs b/provers/stark/src/examples/simple_fibonacci.rs index c3b052fca..466ae4f10 100644 --- a/provers/stark/src/examples/simple_fibonacci.rs +++ b/provers/stark/src/examples/simple_fibonacci.rs @@ -7,7 +7,7 @@ use crate::{ frame::Frame, proof::options::ProofOptions, trace::TraceTable, - traits::AIR, + traits::{TransitionEvaluationContext, AIR}, }; use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField}; use std::marker::PhantomData; @@ -43,10 +43,8 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { let first_step = frame.get_evaluation_step(0); let second_step = frame.get_evaluation_step(1); @@ -147,15 +145,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn fibonacci_trace( From 369c923208fe3c998fd95879b1aa46d4ecc9f8cd Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Fri, 29 Nov 2024 15:38:41 -0300 Subject: [PATCH 4/7] fix simple fib and dummy air examples --- provers/stark/src/examples/dummy_air.rs | 46 ++++++++++++------- .../stark/src/examples/simple_fibonacci.rs | 14 +++++- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/provers/stark/src/examples/dummy_air.rs b/provers/stark/src/examples/dummy_air.rs index 7ad45beb0..8501eb866 100644 --- a/provers/stark/src/examples/dummy_air.rs +++ b/provers/stark/src/examples/dummy_air.rs @@ -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, @@ -48,11 +47,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -97,11 +107,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -186,15 +207,6 @@ impl AIR for DummyAIR { fn pub_inputs(&self) -> &Self::PublicInputs { &() } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn dummy_trace(trace_length: usize) -> TraceTable { diff --git a/provers/stark/src/examples/simple_fibonacci.rs b/provers/stark/src/examples/simple_fibonacci.rs index 466ae4f10..123b32d53 100644 --- a/provers/stark/src/examples/simple_fibonacci.rs +++ b/provers/stark/src/examples/simple_fibonacci.rs @@ -4,7 +4,6 @@ use crate::{ transition::TransitionConstraint, }, context::AirContext, - frame::Frame, proof::options::ProofOptions, trace::TraceTable, traits::{TransitionEvaluationContext, AIR}, @@ -46,6 +45,19 @@ where evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], ) { + 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); From 9fc564f475e11a3fe9a9b103d50eb76e2626dc60 Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Fri, 29 Nov 2024 15:50:44 -0300 Subject: [PATCH 5/7] fix examples --- provers/stark/src/constraints/transition.rs | 1 - provers/stark/src/examples/bit_flags.rs | 50 ++++++++++++------- .../src/examples/fibonacci_2_cols_shifted.rs | 46 ++++++++++------- .../stark/src/examples/fibonacci_2_columns.rs | 46 ++++++++++------- provers/stark/src/examples/fibonacci_rap.rs | 46 ++++++++++------- provers/stark/src/examples/quadratic_air.rs | 29 +++++------ .../src/examples/simple_periodic_cols.rs | 29 +++++------ 7 files changed, 148 insertions(+), 99 deletions(-) diff --git a/provers/stark/src/constraints/transition.rs b/provers/stark/src/constraints/transition.rs index 8c49cbd44..c35d4da90 100644 --- a/provers/stark/src/constraints/transition.rs +++ b/provers/stark/src/constraints/transition.rs @@ -1,7 +1,6 @@ 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; diff --git a/provers/stark/src/examples/bit_flags.rs b/provers/stark/src/examples/bit_flags.rs index c1a2faa6b..cd6d9f14c 100644 --- a/provers/stark/src/examples/bit_flags.rs +++ b/provers/stark/src/examples/bit_flags.rs @@ -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::{ @@ -45,11 +44,22 @@ impl TransitionConstraint for BitConstraint { fn evaluate( &self, - frame: &Frame, - transition_evaluations: &mut [Felt252], - _periodic_values: &[Felt252], - _rap_challenges: &[Felt252], + evaluation_context: &TransitionEvaluationContext, + transition_evaluations: &mut [FieldElement], ) { + 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 prefix_flag = step.get_main_evaluation_element(0, 0); @@ -92,11 +102,22 @@ impl TransitionConstraint for ZeroFlagConstraint { fn evaluate( &self, - frame: &Frame, - transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], + evaluation_context: &TransitionEvaluationContext, + transition_evaluations: &mut [FieldElement], ) { + 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); @@ -149,15 +170,6 @@ impl AIR for BitFlagsAIR { &self.constraints } - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } - fn boundary_constraints( &self, _rap_challenges: &[FieldElement], diff --git a/provers/stark/src/examples/fibonacci_2_cols_shifted.rs b/provers/stark/src/examples/fibonacci_2_cols_shifted.rs index d7183bbd0..e4fc03b10 100644 --- a/provers/stark/src/examples/fibonacci_2_cols_shifted.rs +++ b/provers/stark/src/examples/fibonacci_2_cols_shifted.rs @@ -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}, @@ -46,11 +45,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -94,11 +104,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -223,15 +244,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn compute_trace( diff --git a/provers/stark/src/examples/fibonacci_2_columns.rs b/provers/stark/src/examples/fibonacci_2_columns.rs index 0d01efec5..9e375756a 100644 --- a/provers/stark/src/examples/fibonacci_2_columns.rs +++ b/provers/stark/src/examples/fibonacci_2_columns.rs @@ -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}; @@ -45,11 +44,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -95,11 +105,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -194,15 +215,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn compute_trace( diff --git a/provers/stark/src/examples/fibonacci_rap.rs b/provers/stark/src/examples/fibonacci_rap.rs index 1186a7d24..b3ee3d496 100644 --- a/provers/stark/src/examples/fibonacci_rap.rs +++ b/provers/stark/src/examples/fibonacci_rap.rs @@ -6,10 +6,9 @@ use crate::{ transition::TransitionConstraint, }, context::AirContext, - frame::Frame, proof::options::ProofOptions, trace::TraceTable, - traits::AIR, + traits::{TransitionEvaluationContext, AIR}, }; use lambdaworks_crypto::fiat_shamir::is_transcript::IsTranscript; use lambdaworks_math::{ @@ -51,11 +50,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -101,11 +111,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], ) { + 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); @@ -259,15 +280,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn fibonacci_rap_trace( diff --git a/provers/stark/src/examples/quadratic_air.rs b/provers/stark/src/examples/quadratic_air.rs index 4ac019043..b9d0d8f99 100644 --- a/provers/stark/src/examples/quadratic_air.rs +++ b/provers/stark/src/examples/quadratic_air.rs @@ -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, traits::IsFFTField}; @@ -44,11 +43,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -146,15 +156,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn quadratic_trace( diff --git a/provers/stark/src/examples/simple_periodic_cols.rs b/provers/stark/src/examples/simple_periodic_cols.rs index 233929ea0..7b7f5cefe 100644 --- a/provers/stark/src/examples/simple_periodic_cols.rs +++ b/provers/stark/src/examples/simple_periodic_cols.rs @@ -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, traits::IsFFTField}; @@ -47,11 +46,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -175,15 +185,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } pub fn simple_periodic_trace(trace_length: usize) -> TraceTable { From 291bcfddfe398c5edd35404b00edda5f57952652 Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Thu, 5 Dec 2024 17:42:25 -0300 Subject: [PATCH 6/7] fix mem example --- .../stark/src/examples/read_only_memory.rs | 63 +++++++++++++------ 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/provers/stark/src/examples/read_only_memory.rs b/provers/stark/src/examples/read_only_memory.rs index 8b5b01b07..57fecd1f7 100644 --- a/provers/stark/src/examples/read_only_memory.rs +++ b/provers/stark/src/examples/read_only_memory.rs @@ -6,10 +6,9 @@ use crate::{ transition::TransitionConstraint, }, context::AirContext, - frame::Frame, proof::options::ProofOptions, trace::TraceTable, - traits::AIR, + traits::{TransitionEvaluationContext, AIR}, }; use lambdaworks_crypto::fiat_shamir::is_transcript::IsTranscript; use lambdaworks_math::field::traits::IsPrimeField; @@ -52,11 +51,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -105,11 +115,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - _rap_challenges: &[FieldElement], ) { + 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); @@ -159,11 +180,22 @@ where fn evaluate( &self, - frame: &Frame, + evaluation_context: &TransitionEvaluationContext, transition_evaluations: &mut [FieldElement], - _periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], ) { + 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); @@ -344,15 +376,6 @@ where fn pub_inputs(&self) -> &Self::PublicInputs { &self.pub_inputs } - - fn compute_transition_verifier( - &self, - frame: &Frame, - periodic_values: &[FieldElement], - rap_challenges: &[FieldElement], - ) -> Vec> { - self.compute_transition_prover(frame, periodic_values, rap_challenges) - } } /// Given the adress and value columns, it returns the trace table with 5 columns, which are: From 605149de7c1f20cad143a5cb1316b6ae16735a4f Mon Sep 17 00:00:00 2001 From: Joaquin Carletti Date: Fri, 20 Dec 2024 12:44:44 -0300 Subject: [PATCH 7/7] crate new methods for evaluation_context --- provers/stark/src/constraints/evaluator.rs | 12 ++++----- provers/stark/src/debug.rs | 8 +++--- provers/stark/src/traits.rs | 30 ++++++++++++++++++++++ provers/stark/src/verifier.rs | 11 ++++---- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/provers/stark/src/constraints/evaluator.rs b/provers/stark/src/constraints/evaluator.rs index defac9fe2..8a4423187 100644 --- a/provers/stark/src/constraints/evaluator.rs +++ b/provers/stark/src/constraints/evaluator.rs @@ -184,12 +184,12 @@ impl ConstraintEvaluator { .collect(); // Compute all the transition constraints at this point of the LDE domain. - let evaluations_transition = - air.compute_transition(&TransitionEvaluationContext::Prover { - frame: &frame, - periodic_values: &periodic_values, - rap_challenges, - }); + let transition_evaluation_context = TransitionEvaluationContext::new_prover( + &frame, + &periodic_values, + rap_challenges, + ); + let evaluations_transition = air.compute_transition(&transition_evaluation_context); #[cfg(all(debug_assertions, not(feature = "parallel")))] transition_evaluations.push(evaluations_transition.clone()); diff --git a/provers/stark/src/debug.rs b/provers/stark/src/debug.rs index fe3553614..b2c4e52f2 100644 --- a/provers/stark/src/debug.rs +++ b/provers/stark/src/debug.rs @@ -93,11 +93,9 @@ pub fn validate_trace( .iter() .map(|col| col[step].clone()) .collect(); - let evaluations = air.compute_transition(&TransitionEvaluationContext::Prover { - frame: &frame, - periodic_values: &periodic_values, - rap_challenges, - }); + let transition_evaluation_context = + TransitionEvaluationContext::new_prover(&frame, &periodic_values, rap_challenges); + let evaluations = air.compute_transition(&transition_evaluation_context); // 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 diff --git a/provers/stark/src/traits.rs b/provers/stark/src/traits.rs index dcddde4ec..afb1b3014 100644 --- a/provers/stark/src/traits.rs +++ b/provers/stark/src/traits.rs @@ -41,6 +41,36 @@ where }, } +impl<'a, F, E> TransitionEvaluationContext<'a, F, E> +where + F: IsSubFieldOf, + E: IsField, +{ + pub fn new_prover( + frame: &'a Frame<'a, F, E>, + periodic_values: &'a [FieldElement], + rap_challenges: &'a [FieldElement], + ) -> Self { + Self::Prover { + frame, + periodic_values, + rap_challenges, + } + } + + pub fn new_verifier( + frame: &'a Frame<'a, E, E>, + periodic_values: &'a [FieldElement], + rap_challenges: &'a [FieldElement], + ) -> Self { + Self::Verifier { + frame, + periodic_values, + rap_challenges, + } + } +} + /// AIR is a representation of the Constraints pub trait AIR { type Field: IsFFTField + IsSubFieldOf + Send + Sync; diff --git a/provers/stark/src/verifier.rs b/provers/stark/src/verifier.rs index a1ca0effd..57c1bcbda 100644 --- a/provers/stark/src/verifier.rs +++ b/provers/stark/src/verifier.rs @@ -274,12 +274,13 @@ pub trait IsStarkVerifier { let ood_frame = (proof.trace_ood_evaluations).into_frame(num_main_trace_columns, A::STEP_SIZE); + let transition_evaluation_context = TransitionEvaluationContext::new_verifier( + &ood_frame, + &periodic_values, + &challenges.rap_challenges, + ); let transition_ood_frame_evaluations = - air.compute_transition(&TransitionEvaluationContext::Verifier { - frame: &ood_frame, - periodic_values: &periodic_values, - rap_challenges: &challenges.rap_challenges, - }); + air.compute_transition(&transition_evaluation_context); let mut denominators = vec![FieldElement::::zero(); air.num_transition_constraints()];