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

Validation, Parser, FriConfig Validation Fixes #55

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/deserialization/fri.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ impl IntoFriWitness of Into<FriWitnessWithSerde, FriWitness> {
},
}
);

i += 1;
};

FriWitness { layers: layers.span(), }
Expand Down
2 changes: 0 additions & 2 deletions src/deserialization/stark.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ impl IntoStarkUnsentCommitment of Into<StarkUnsentCommitmentWithSerde, StarkUnse
struct StarkWitnessWithSerde {
traces_decommitment: TracesDecommitmentWithSerde,
traces_witness: TracesWitnessWithSerde,
interaction: TableCommitmentWitnessWithSerde,
composition_decommitment: TableDecommitmentWithSerde,
composition_witness: TableCommitmentWitnessWithSerde,
fri_witness: FriWitnessWithSerde,
Expand All @@ -176,7 +175,6 @@ impl IntoStarkWitness of Into<StarkWitnessWithSerde, StarkWitness> {
StarkWitness {
traces_decommitment: self.traces_decommitment.into(),
traces_witness: self.traces_witness.into(),
interaction: self.interaction.into(),
composition_decommitment: self.composition_decommitment.into(),
composition_witness: self.composition_witness.into(),
fri_witness: self.fri_witness.into(),
Expand Down
44 changes: 19 additions & 25 deletions src/fri/fri_config.cairo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfigTrait;
use cairo_verifier::{
common::{asserts::assert_in_range, math::{pow, Felt252PartialOrd}},
table_commitment::TableCommitmentConfig,
vector_commitment::vector_commitment::VectorCommitmentConfig,
vector_commitment::vector_commitment::{VectorCommitmentConfig, VectorCommitmentConfigTrait},
};

const MAX_LAST_LAYER_LOG_DEGREE_BOUND: u32 = 15;
const MAX_FRI_LAYERS: u32 = 15;
const MAX_FRI_STEP: u32 = 4;
const MAX_LAST_LAYER_LOG_DEGREE_BOUND: felt252 = 15;
const MAX_FRI_LAYERS: felt252 = 15;
const MAX_FRI_STEP: felt252 = 4;

#[derive(Drop, Copy)]
struct FriConfig {
Expand All @@ -28,44 +28,38 @@ impl FriConfigImpl of FriConfigTrait {
fn validate(
self: @FriConfig, log_n_cosets: felt252, n_verifier_friendly_commitment_layers: felt252
) -> felt252 {
let n_layers: u32 = (*self.n_layers).try_into().unwrap();
let log_last_layer_degree_bound: u32 = (*self.log_last_layer_degree_bound)
.try_into()
.unwrap();

assert(log_last_layer_degree_bound <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, 'Value too big');

assert_in_range(*self.n_layers, 2, MAX_FRI_LAYERS + 1);
assert(
*self.log_last_layer_degree_bound <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, 'Value too big'
);
assert(*self.fri_step_sizes[0] == 0, 'Invalid value');

assert(n_layers >= 2, 'Value too small');
assert(n_layers <= MAX_FRI_LAYERS + 1, 'Value too big');

let mut i: u32 = 1;
let mut sum_of_step_sizes: felt252 = 0;
let n_layers: u32 = (*self.n_layers).try_into().unwrap();
let mut sum_of_step_sizes = 0;
let mut log_input_size = *self.log_input_size;
loop {
if i == n_layers {
break;
}

let fri_step: felt252 = *self.fri_step_sizes[i];
let table_commitment = *self.inner_layers[i];

let fri_step_u32: u32 = fri_step.try_into().unwrap();
assert(fri_step_u32 >= 1, 'Value too small');
assert(fri_step_u32 <= MAX_FRI_STEP + 1, 'Value too big');
assert(table_commitment.n_columns == fri_step * fri_step, 'Invalid value');

let fri_step = *self.fri_step_sizes[i];
let table_commitment: TableCommitmentConfig = *self.inner_layers[i - 1];
log_input_size -= fri_step;
sum_of_step_sizes += fri_step;

assert_in_range(fri_step, 1, MAX_FRI_STEP + 1);
assert(table_commitment.n_columns == pow(2, fri_step), 'Invalid value');
table_commitment.vector.validate(log_input_size, n_verifier_friendly_commitment_layers);

i += 1;
};

let log_expected_input_degree = sum_of_step_sizes + *self.log_last_layer_degree_bound;
assert(log_expected_input_degree + log_n_cosets == *self.log_input_size, '');
assert(
log_expected_input_degree + log_n_cosets == *self.log_input_size,
'Log input size mismatch'
);
log_expected_input_degree
}
}
9 changes: 7 additions & 2 deletions src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ mod stark;
mod table_commitment;
mod vector_commitment;

use cairo_verifier::deserialization::stark::StarkProofWithSerde;
use cairo_verifier::{
deserialization::stark::StarkProofWithSerde, stark::{StarkProof, StarkProofImpl}
};

fn main(x: Array<felt252>) {
let mut x_span = x.span();
let stark_proof: StarkProofWithSerde = Serde::deserialize(ref x_span).unwrap();
let stark_proof: StarkProof = Serde::<StarkProofWithSerde>::deserialize(ref x_span)
.unwrap()
.into();
stark_proof.verify();
}
1 change: 0 additions & 1 deletion src/stark.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ struct StarkUnsentCommitment {
struct StarkWitness {
traces_decommitment: TracesDecommitment,
traces_witness: TracesWitness,
interaction: TableCommitmentWitness,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is already fixed on stark-verify branch but we can resolve it

composition_decommitment: TableDecommitment,
composition_witness: TableCommitmentWitness,
fri_witness: FriWitness,
Expand Down