Skip to content

Commit

Permalink
add StarkTranscript trait and implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
schouhy committed Sep 21, 2023
1 parent a190b17 commit e6005d8
Show file tree
Hide file tree
Showing 15 changed files with 631 additions and 287 deletions.
6 changes: 6 additions & 0 deletions exercises/message/src/starks/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,12 @@ where
.collect()
}

strct StoneProverTranscript {
counter: usize,
spare_bytes: Vec<u8>,
hash: dyn Transcript,
}

// FIXME remove unwrap() calls and return errors
pub fn prove<F, A>(
main_trace: &TraceTable<F>,
Expand Down
28 changes: 20 additions & 8 deletions provers/cairo/src/air.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ops::Range;

use cairo_vm::without_std::collections::HashMap;
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::{
errors::DeserializationError,
field::{
Expand All @@ -17,7 +16,7 @@ use stark_platinum_prover::{
prover::{prove, ProvingError},
trace::TraceTable,
traits::AIR,
transcript::transcript_to_field,
transcript::{IsStarkTranscript, StoneProverTranscript},
verifier::verify,
};

Expand Down Expand Up @@ -790,11 +789,14 @@ impl AIR for CairoAIR {
TraceTable::new(aux_table, self.number_auxiliary_rap_columns())
}

fn build_rap_challenges<T: Transcript>(&self, transcript: &mut T) -> Self::RAPChallenges {
fn build_rap_challenges(
&self,
transcript: &mut impl IsStarkTranscript<Self::Field>,
) -> Self::RAPChallenges {
CairoRAPChallenges {
alpha_memory: transcript_to_field(transcript),
z_memory: transcript_to_field(transcript),
z_range_check: transcript_to_field(transcript),
alpha_memory: transcript.sample_field_element(),
z_memory: transcript.sample_field_element(),
z_range_check: transcript.sample_field_element(),
}
}

Expand Down Expand Up @@ -1252,7 +1254,12 @@ pub fn generate_cairo_proof(
pub_input: &PublicInputs,
proof_options: &ProofOptions,
) -> Result<StarkProof<Stark252PrimeField>, ProvingError> {
prove::<Stark252PrimeField, CairoAIR>(trace, pub_input, proof_options)
prove::<Stark252PrimeField, CairoAIR>(
trace,
pub_input,
proof_options,
StoneProverTranscript::new(&[]),
)
}

/// Wrapper function for verifying Cairo proofs without the need to specify
Expand All @@ -1263,7 +1270,12 @@ pub fn verify_cairo_proof(
pub_input: &PublicInputs,
proof_options: &ProofOptions,
) -> bool {
verify::<Stark252PrimeField, CairoAIR>(proof, pub_input, proof_options)
verify::<Stark252PrimeField, CairoAIR>(
proof,
pub_input,
proof_options,
StoneProverTranscript::new(&[]),
)
}

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions provers/cairo/src/tests/integration_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lambdaworks_crypto::fiat_shamir::default_transcript::DefaultTranscript;
use lambdaworks_math::{
errors::DeserializationError,
field::fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
Expand All @@ -13,6 +12,7 @@ use stark_platinum_prover::{
},
trace::TraceTable,
traits::AIR,
transcript::StoneProverTranscript,
};

use crate::{
Expand Down Expand Up @@ -240,7 +240,7 @@ fn check_simple_cairo_trace_evaluates_to_zero() {
let (main_trace, public_input) =
generate_prover_args(&program_content, &None, CairoLayout::Plain).unwrap();
let mut trace_polys = main_trace.compute_trace_polys();
let mut transcript = DefaultTranscript::new();
let mut transcript = StoneProverTranscript::new(&[]);

let proof_options = ProofOptions::default_test_options();
let cairo_air = CairoAIR::new(main_trace.n_rows(), &public_input, &proof_options);
Expand Down
8 changes: 6 additions & 2 deletions provers/stark/src/examples/dummy_air.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::{
element::FieldElement, fields::fft_friendly::stark_252_prime_field::Stark252PrimeField,
traits::IsFFTField,
Expand All @@ -11,6 +10,7 @@ use crate::{
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
transcript::IsStarkTranscript,
};

#[derive(Clone)]
Expand Down Expand Up @@ -53,7 +53,11 @@ impl AIR for DummyAIR {
TraceTable::empty()
}

fn build_rap_challenges<T: Transcript>(&self, _transcript: &mut T) -> Self::RAPChallenges {}
fn build_rap_challenges(
&self,
_transcript: &mut impl IsStarkTranscript<Self::Field>,
) -> Self::RAPChallenges {
}
fn compute_transition(
&self,
frame: &Frame<Self::Field>,
Expand Down
8 changes: 6 additions & 2 deletions provers/stark/src/examples/fibonacci_2_cols_shifted.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField};

use crate::{
Expand All @@ -8,6 +7,7 @@ use crate::{
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
transcript::IsStarkTranscript,
};

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -71,7 +71,11 @@ where
TraceTable::empty()
}

fn build_rap_challenges<T: Transcript>(&self, _transcript: &mut T) -> Self::RAPChallenges {}
fn build_rap_challenges(
&self,
_transcript: &mut impl IsStarkTranscript<Self::Field>,
) -> Self::RAPChallenges {
}

fn compute_transition(
&self,
Expand Down
8 changes: 6 additions & 2 deletions provers/stark/src/examples/fibonacci_2_columns.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField};

use crate::{
Expand All @@ -8,6 +7,7 @@ use crate::{
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
transcript::IsStarkTranscript,
};

use super::simple_fibonacci::FibonacciPublicInputs;
Expand Down Expand Up @@ -62,7 +62,11 @@ where
TraceTable::empty()
}

fn build_rap_challenges<T: Transcript>(&self, _transcript: &mut T) -> Self::RAPChallenges {}
fn build_rap_challenges(
&self,
_transcript: &mut impl IsStarkTranscript<Self::Field>,
) -> Self::RAPChallenges {
}

fn compute_transition(
&self,
Expand Down
10 changes: 6 additions & 4 deletions provers/stark/src/examples/fibonacci_rap.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ops::Div;

use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::{
field::{element::FieldElement, traits::IsFFTField},
helpers::resize_to_next_power_of_two,
Expand All @@ -14,7 +13,7 @@ use crate::{
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
transcript::transcript_to_field,
transcript::IsStarkTranscript,
};

#[derive(Clone)]
Expand Down Expand Up @@ -96,8 +95,11 @@ where
TraceTable::new_from_cols(&[aux_col])
}

fn build_rap_challenges<T: Transcript>(&self, transcript: &mut T) -> Self::RAPChallenges {
transcript_to_field(transcript)
fn build_rap_challenges(
&self,
transcript: &mut impl IsStarkTranscript<F>,
) -> Self::RAPChallenges {
transcript.sample_field_element()
}

fn number_auxiliary_rap_columns(&self) -> usize {
Expand Down
8 changes: 6 additions & 2 deletions provers/stark/src/examples/quadratic_air.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField};

use crate::{
Expand All @@ -8,6 +7,7 @@ use crate::{
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
transcript::IsStarkTranscript,
};

#[derive(Clone)]
Expand Down Expand Up @@ -66,7 +66,11 @@ where
TraceTable::empty()
}

fn build_rap_challenges<T: Transcript>(&self, _transcript: &mut T) -> Self::RAPChallenges {}
fn build_rap_challenges(
&self,
_transcript: &mut impl IsStarkTranscript<Self::Field>,
) -> Self::RAPChallenges {
}

fn compute_transition(
&self,
Expand Down
8 changes: 6 additions & 2 deletions provers/stark/src/examples/simple_fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::{element::FieldElement, traits::IsFFTField};

use crate::{
Expand All @@ -8,6 +7,7 @@ use crate::{
proof::options::ProofOptions,
trace::TraceTable,
traits::AIR,
transcript::IsStarkTranscript,
};

#[derive(Clone)]
Expand Down Expand Up @@ -71,7 +71,11 @@ where
TraceTable::empty()
}

fn build_rap_challenges<T: Transcript>(&self, _transcript: &mut T) -> Self::RAPChallenges {}
fn build_rap_challenges(
&self,
_transcript: &mut impl IsStarkTranscript<Self::Field>,
) -> Self::RAPChallenges {
}

fn compute_transition(
&self,
Expand Down
19 changes: 9 additions & 10 deletions provers/stark/src/fri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ pub mod fri_commitment;
pub mod fri_decommit;
mod fri_functions;

use lambdaworks_crypto::fiat_shamir::transcript::Transcript;
use lambdaworks_math::field::traits::{IsFFTField, IsField};
use lambdaworks_math::traits::ByteConversion;
pub use lambdaworks_math::{
field::{element::FieldElement, fields::u64_prime_field::U64PrimeField},
polynomial::Polynomial,
};

use crate::transcript::IsStarkTranscript;

use self::fri_commitment::FriLayer;
use self::fri_decommit::FriDecommitment;
use self::fri_functions::fold_polynomial;

use super::traits::AIR;
use super::transcript::{transcript_to_field, transcript_to_u32};

pub fn fri_commit_phase<F: IsField + IsFFTField, T: Transcript>(
pub fn fri_commit_phase<F: IsField + IsFFTField>(
number_layers: usize,
p_0: Polynomial<FieldElement<F>>,
transcript: &mut T,
transcript: &mut impl IsStarkTranscript<F>,
coset_offset: &FieldElement<F>,
domain_size: usize,
) -> (FieldElement<F>, Vec<FriLayer<F>>)
Expand All @@ -40,7 +40,7 @@ where

for _ in 1..number_layers {
// <<<< Receive challenge 𝜁ₖ₋₁
let zeta = transcript_to_field(transcript);
let zeta = transcript.sample_field_element();
coset_offset = coset_offset.square();
domain_size /= 2;

Expand All @@ -55,7 +55,7 @@ where
}

// <<<< Receive challenge: 𝜁ₙ₋₁
let zeta = transcript_to_field(transcript);
let zeta = transcript.sample_field_element();

let last_poly = fold_polynomial(&current_poly, &zeta);

Expand All @@ -71,22 +71,21 @@ where
(last_value, fri_layer_list)
}

pub fn fri_query_phase<F, A, T>(
pub fn fri_query_phase<F, A>(
air: &A,
domain_size: usize,
fri_layers: &Vec<FriLayer<F>>,
transcript: &mut T,
transcript: &mut impl IsStarkTranscript<F>,
) -> (Vec<FriDecommitment<F>>, Vec<usize>)
where
F: IsFFTField,
A: AIR<Field = F>,
T: Transcript,
FieldElement<F>: ByteConversion,
{
if !fri_layers.is_empty() {
let number_of_queries = air.options().fri_number_of_queries;
let iotas = (0..number_of_queries)
.map(|_| (transcript_to_u32(transcript) as usize) % domain_size)
.map(|_| (transcript.sample_u64(domain_size as u64)) as usize)
.collect::<Vec<usize>>();
let query_list = iotas
.iter()
Expand Down
Loading

0 comments on commit e6005d8

Please sign in to comment.