Skip to content

Commit

Permalink
stark commit in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
neotheprogramist committed Jan 12, 2024
1 parent 88a57cf commit 622833c
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/air.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ mod pedersen;
mod autogenerated;
mod traces;
mod traces_config;
mod layout;
34 changes: 34 additions & 0 deletions src/air/layout.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#[derive(Drop)]
struct Layout {
// Virtual functions.
// Each should be a pointer to a function with the same interface as the function in this file.
// eval_oods_polynomial: felt*,
// Constants.
// n_original_columns: felt252,
// n_interaction_columns: felt252,
n_interaction_elements: felt252,
}

#[derive(Drop)]
struct AirInstance {
// Virtual functions.
// Each should be a pointer to a function with the same interface as the function in this file.
// public_input_hash: felt*,
// public_input_validate: felt*,
// traces_config_validate: felt*,
// traces_commit: felt*,
// traces_decommit: felt*,
// traces_eval_composition_polynomial: felt*,
// eval_oods_boundary_poly_at_points: felt*,
// Constants.
// n_dynamic_params: felt252,
n_constraints: felt252,
constraint_degree: felt252,
mask_size: felt252,
}

#[derive(Drop)]
struct AirWithLayout {
air: AirInstance,
layout: Layout,
}
19 changes: 19 additions & 0 deletions src/stark.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use cairo_verifier::air::public_input::PublicInputTrait;
use cairo_verifier::{
air::{
layout::{AirInstance, AirWithLayout, Layout},
traces_config::{TracesConfig, TracesConfigTrait}, public_input::PublicInput,
traces::{TracesUnsentCommitment, TracesCommitment, TracesDecommitment, TracesWitness}
},
channel::channel::{Channel, ChannelImpl},
fri::{
fri_config::{FriConfig, FriConfigTrait},
fri::{FriUnsentCommitment, FriWitness, FriCommitment}
Expand Down Expand Up @@ -39,8 +42,24 @@ struct StarkProof {
#[generate_trait]
impl StarkProofImpl of StarkProofTrait {
fn verify(self: @StarkProof) {
let air = AirWithLayout {
air: AirInstance { n_constraints: 8, constraint_degree: 1, mask_size: 133 },
layout: Layout { n_interaction_elements: 8 }
};
self.config.validate(SECURITY_BITS);
let stark_domains = StarkDomainsImpl::new(self.config);

let digest = self.public_input.get_public_input_hash();
let mut channel = ChannelImpl::new(digest);

stark_commit::stark_commit(
ref channel,
@air,
self.public_input,
self.unsent_commitment,
self.config,
@stark_domains,
);
}
}

Expand Down
143 changes: 143 additions & 0 deletions src/stark/stark_commit.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,144 @@
use core::array::ArrayTrait;
use core::option::OptionTrait;
use core::traits::Into;
use cairo_verifier::channel::channel::ChannelTrait;
use cairo_verifier::{
air::{layout::{AirWithLayout, Layout}, public_input::PublicInput, traces::traces_commit,},
channel::channel::Channel, common::powers_array::powers_array, domains::StarkDomains,
fri::fri::fri_commit, stark::{StarkUnsentCommitment, StarkConfig, StarkCommitment},
proof_of_work::proof_of_work::proof_of_work_commit, table_commitment::table_commit,
oods::verify_oods,
};

struct InteractionValuesAfterTraces {
// n_constraints Coefficients for the AIR constraints.
coefficients: Span<felt252>,
}

struct InteractionValuesAfterOods {
// n_oods_values coefficients for the boundary polynomial validating the OODS values.
coefficients: Span<felt252>,
}

// STARK commitment phase.
fn stark_commit(
ref channel: Channel,
air: @AirWithLayout,
public_input: @PublicInput,
unsent_commitment: @StarkUnsentCommitment,
config: @StarkConfig,
stark_domains: @StarkDomains,
) -> StarkCommitment {
// alloc_locals;

// Read the commitment of the 'traces' component.
// let (traces_commitment) = traces_commit(
// air=air,
// public_input=public_input,
// unsent_commitment=unsent_commitment.traces,
// config=config.traces,
// );
let traces_commitment = traces_commit(
ref channel,
*air.layout.n_interaction_elements,
*public_input,
*unsent_commitment.traces,
*config.traces,
);

// Generate interaction values after traces commitment.
// let (composition_alpha: felt*) = alloc();
// random_felts_to_prover(n_elements=1, elements=composition_alpha);
// let (traces_coefficients: felt*) = alloc();
// compute_powers_array(
// data_ptr=traces_coefficients, alpha=[composition_alpha], cur=1, n=air.n_constraints
// );
let composition_alpha = channel.random_felt_to_prover();
let traces_coefficients = powers_array(
1, composition_alpha, (*air.air.n_constraints).try_into().unwrap()
);

// let (interaction_after_traces: InteractionValuesAfterTraces*) = alloc();
// assert [interaction_after_traces] = InteractionValuesAfterTraces(
// coefficients=traces_coefficients
// );
let interaction_after_traces = InteractionValuesAfterTraces {
coefficients: traces_coefficients.span(),
};

// Read composition commitment.
// let (composition_commitment: TableCommitment*) = table_commit(
// unsent_commitment=unsent_commitment.composition, config=config.composition
// );
let composition_commitment = table_commit(*unsent_commitment.composition, *config.composition,);

// Generate interaction values after composition.
// let (interaction_after_composition: InteractionValuesAfterComposition*) = alloc();
// random_felts_to_prover(
// n_elements=InteractionValuesAfterComposition.SIZE,
// elements=cast(interaction_after_composition, felt*),
// );
let interaction_after_composition = channel.random_felt_to_prover();

// Read OODS values.
// local n_oods_values = air.mask_size + air.constraint_degree;
// let (sent_oods_values) = read_felts_from_prover(
// n_values=n_oods_values, values=unsent_commitment.oods_values
// );
let n_oods_values = *air.air.mask_size + *air.air.constraint_degree;
let sent_oods_values = channel.read_felts_from_prover(*unsent_commitment.oods_values);

// Check that the trace and the composition agree at oods_point.
// verify_oods(
// air=air,
// oods_values=sent_oods_values,
// traces_commitment=traces_commitment,
// traces_coefficients=traces_coefficients,
// oods_point=interaction_after_composition.oods_point,
// trace_domain_size=stark_domains.trace_domain_size,
// trace_generator=stark_domains.trace_generator,
// );
verify_oods(sent_oods_values);

// Generate interaction values after OODS.
// let (oods_alpha: felt*) = alloc();
// random_felts_to_prover(n_elements=1, elements=oods_alpha);
// let (oods_coefficients: felt*) = alloc();
// compute_powers_array(data_ptr=oods_coefficients, alpha=[oods_alpha], cur=1, n=n_oods_values);
// tempvar interaction_after_oods = new InteractionValuesAfterOods(coefficients=oods_coefficients);
let oods_alpha = channel.random_felt_to_prover();
let oods_coefficients = powers_array(1, oods_alpha, n_oods_values.try_into().unwrap());
let interaction_after_oods = InteractionValuesAfterOods {
coefficients: oods_coefficients.span()
};

// Read fri commitment.
// let (fri_commitment) = fri_commit(unsent_commitment=unsent_commitment.fri, config=config.fri);
let fri_commitment = fri_commit(ref channel, *unsent_commitment.fri, *config.fri);

// Proof of work commitment phase.
// proof_of_work_commit(
// unsent_commitment=unsent_commitment.proof_of_work, config=config.proof_of_work
// );
proof_of_work_commit(ref channel, *unsent_commitment.proof_of_work, *config.proof_of_work);

// Return commitment.
// return (
// res=new StarkCommitment(
// traces=traces_commitment,
// composition=composition_commitment,
// interaction_after_composition=interaction_after_composition,
// oods_values=sent_oods_values,
// interaction_after_oods=interaction_after_oods,
// fri=fri_commitment,
// ),
// );
StarkCommitment {
traces: traces_commitment,
composition: composition_commitment,
interaction_after_composition: interaction_after_composition,
oods_values: sent_oods_values,
interaction_after_oods: interaction_after_oods.coefficients,
fri: fri_commitment,
}
}

0 comments on commit 622833c

Please sign in to comment.