Skip to content

Commit

Permalink
refactor validation
Browse files Browse the repository at this point in the history
  • Loading branch information
neotheprogramist committed Jan 9, 2024
1 parent 2a84a41 commit 4e07ae9
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 92 deletions.
49 changes: 27 additions & 22 deletions src/air/config.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use cairo_verifier::{
common::asserts::assert_in_range, table_commitment::TableCommitmentConfig,
vector_commitment::vector_commitment::validate_vector_commitment
};
use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfigTrait;
use cairo_verifier::{common::asserts::assert_in_range, table_commitment::TableCommitmentConfig};

const MAX_N_COLUMNS: felt252 = 128;
const AIR_LAYOUT_N_ORIGINAL_COLUMNS: felt252 = 12;
Expand All @@ -14,24 +12,31 @@ struct TracesConfig {
interaction: TableCommitmentConfig,
}

#[generate_trait]
impl TracesConfigImpl of TracesConfigTrait {
fn validate(
self: @TracesConfig,
log_eval_domain_size: felt252,
n_verifier_friendly_commitment_layers: felt252,
) {
assert_in_range(*self.original.n_columns, 1, MAX_N_COLUMNS + 1);
assert_in_range(*self.interaction.n_columns, 1, MAX_N_COLUMNS + 1);
assert(
*self.original.n_columns == AIR_LAYOUT_N_ORIGINAL_COLUMNS, 'Wrong number of columns'
);
assert(
*self.interaction.n_columns == AIR_LAYOUT_N_INTERACTION_COLUMNS,
'Wrong number of columns'
);

self.original.vector.validate(log_eval_domain_size, n_verifier_friendly_commitment_layers);

self
.interaction
.vector
.validate(log_eval_domain_size, n_verifier_friendly_commitment_layers);
}
}
// Validates the configuration of the traces.
// log_eval_domain_size - Log2 of the evaluation domain size.
fn traces_config_validate(
config: TracesConfig,
log_eval_domain_size: felt252,
n_verifier_friendly_commitment_layers: felt252,
) {
assert_in_range(config.original.n_columns, 1, MAX_N_COLUMNS + 1);
assert_in_range(config.interaction.n_columns, 1, MAX_N_COLUMNS + 1);
assert(config.original.n_columns == AIR_LAYOUT_N_ORIGINAL_COLUMNS, 'Wrong number of columns');
assert(
config.interaction.n_columns == AIR_LAYOUT_N_INTERACTION_COLUMNS, 'Wrong number of columns'
);

validate_vector_commitment(
config.original.vector, log_eval_domain_size, n_verifier_friendly_commitment_layers,
);
validate_vector_commitment(
config.interaction.vector, log_eval_domain_size, n_verifier_friendly_commitment_layers,
);
}
72 changes: 38 additions & 34 deletions src/fri/fri_config.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfigTrait;
use cairo_verifier::{
table_commitment::TableCommitmentConfig,
vector_commitment::vector_commitment::{validate_vector_commitment, VectorCommitmentConfig},
vector_commitment::vector_commitment::VectorCommitmentConfig,
};

const MAX_LAST_LAYER_LOG_DEGREE_BOUND: u32 = 15;
Expand All @@ -22,46 +23,49 @@ struct FriConfig {
log_last_layer_degree_bound: felt252,
}

fn fri_config_validate(
config: FriConfig, log_n_cosets: felt252, n_verifier_friendly_commitment_layers: felt252
) -> felt252 {
let n_layers: u32 = config.n_layers.try_into().unwrap();
let log_last_layer_degree_bound: u32 = config.log_last_layer_degree_bound.try_into().unwrap();
#[generate_trait]
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(log_last_layer_degree_bound <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, 'Value too big');

assert(*config.fri_step_sizes.at(0) == 0, 'Invalid value');
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');
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 mut log_input_size = config.log_input_size;
loop {
if i == n_layers {
break;
}
let mut i: u32 = 1;
let mut sum_of_step_sizes: felt252 = 0;
let mut log_input_size = *self.log_input_size;
loop {
if i == n_layers {
break;
}

let fri_step: felt252 = *config.fri_step_sizes.at(i);
let table_commitment = *config.inner_layers.at(i);
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_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');

i += 1;
log_input_size -= fri_step;
sum_of_step_sizes += fri_step;
log_input_size -= fri_step;
sum_of_step_sizes += fri_step;

validate_vector_commitment(
table_commitment.vector, log_input_size, n_verifier_friendly_commitment_layers,
);
};
table_commitment.vector.validate(log_input_size, n_verifier_friendly_commitment_layers);

let log_expected_input_degree = sum_of_step_sizes + config.log_last_layer_degree_bound;
assert(log_expected_input_degree + log_n_cosets == config.log_input_size, '');
log_expected_input_degree
}
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, '');
log_expected_input_degree
}
}
46 changes: 29 additions & 17 deletions src/stark.cairo
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use cairo_verifier::fri::fri_config::FriConfigTrait;
use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfigTrait;
use cairo_verifier::air::config::TracesConfigTrait;
use cairo_verifier::proof_of_work::config::ProofOfWorkConfigTrait;
use cairo_verifier::{
air::{
config::{traces_config_validate, TracesConfig}, public_input::PublicInput,
config::{TracesConfig}, public_input::PublicInput,
traces::{TracesUnsentCommitment, TracesDecommitment, TracesWitness}
},
fri::{fri_config::{FriConfig, fri_config_validate}, fri::{FriUnsentCommitment, FriWitness}},
fri::{fri_config::FriConfig, fri::{FriUnsentCommitment, FriWitness}},
table_commitment::{TableCommitmentConfig, TableCommitmentWitness, TableDecommitment},
proof_of_work::{config::ProofOfWorkConfig, proof_of_work::ProofOfWorkUnsentCommitment},
vector_commitment::vector_commitment::validate_vector_commitment,
};

mod stark_commit;
Expand All @@ -16,6 +18,9 @@ mod stark_verify;
#[cfg(test)]
mod tests;

const SECURITY_BITS: felt252 = 9;


#[derive(Drop)]
struct StarkProof {
config: StarkConfig,
Expand All @@ -24,6 +29,14 @@ struct StarkProof {
witness: StarkWitness,
}

#[generate_trait]
impl StarkProofImpl of StarkProofTrait {
fn verify(self: @StarkProof) {
self.config.validate(SECURITY_BITS);
}
}


#[derive(Drop, Copy)]
struct StarkConfig {
traces: TracesConfig,
Expand All @@ -41,22 +54,21 @@ struct StarkConfig {
n_verifier_friendly_commitment_layers: felt252,
}

fn stark_config_validate(stark_config: StarkConfig, security_bits: felt252) {
stark_config.proof_of_work.config_validate();
#[generate_trait]
impl StarkConfigImpl of StarkConfigTrait {
fn validate(self: @StarkConfig, security_bits: felt252) {
self.proof_of_work.config_validate();

let log_eval_domain_size = *self.log_trace_domain_size + *self.log_n_cosets;
self.traces.validate(log_eval_domain_size, security_bits);

let log_eval_domain_size = stark_config.log_trace_domain_size + stark_config.log_n_cosets;
traces_config_validate(stark_config.traces, log_eval_domain_size, security_bits);
self
.composition
.vector
.validate(log_eval_domain_size, *self.n_verifier_friendly_commitment_layers);

validate_vector_commitment(
stark_config.composition.vector,
log_eval_domain_size,
stark_config.n_verifier_friendly_commitment_layers
);
fri_config_validate(
stark_config.fri.into(),
stark_config.log_n_cosets,
stark_config.n_verifier_friendly_commitment_layers
);
self.fri.validate(*self.log_n_cosets, *self.n_verifier_friendly_commitment_layers);
}
}

#[derive(Drop)]
Expand Down
10 changes: 5 additions & 5 deletions src/vector_commitment/tests/test_vector_commitment.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfigTrait;
use cairo_verifier::vector_commitment::vector_commitment::{
VectorCommitment, VectorCommitmentConfig, vector_commit, validate_vector_commitment
VectorCommitment, VectorCommitmentConfig, vector_commit
};
use cairo_verifier::channel::channel::Channel;

Expand Down Expand Up @@ -34,22 +35,21 @@ fn test_vector_commit() {
#[available_gas(9999999999)]
fn test_validate_vector_commitment() {
let config = VectorCommitmentConfig { height: 21, n_verifier_friendly_commitment_layers: 7, };
validate_vector_commitment(config, 21, 7);
config.validate(21, 7);
}

#[test]
#[should_panic]
#[available_gas(9999999999)]
fn test_invalid_validate_vector_commitment_1() {
let config = VectorCommitmentConfig { height: 21, n_verifier_friendly_commitment_layers: 7, };
validate_vector_commitment(config, 21, 8);
config.validate(21, 8);
}

#[test]
#[should_panic]
#[available_gas(9999999999)]
fn test_invalid_validate_vector_commitment_2() {
let config = VectorCommitmentConfig { height: 21, n_verifier_friendly_commitment_layers: 7, };
validate_vector_commitment(config, 22, 7);
config.validate(22, 7);
}

31 changes: 17 additions & 14 deletions src/vector_commitment/vector_commitment.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ struct VectorCommitmentConfig {
n_verifier_friendly_commitment_layers: felt252,
}

#[generate_trait]
impl VectorCommitmentConfigImpl of VectorCommitmentConfigTrait {
fn validate(
self: @VectorCommitmentConfig,
expected_height: felt252,
n_verifier_friendly_commitment_layers: felt252,
) {
assert(*self.height == expected_height, 'height mismatch');
// Note that n_verifier_friendly_commitment_layers can be greater than height (in such a case,
// all Merkle layers use the verifier-friendly hash).
assert(
*self.n_verifier_friendly_commitment_layers == n_verifier_friendly_commitment_layers,
'n_verifier_friendly... mismatch'
);
}
}

// A query to the vector commitment.
#[derive(Drop, Copy)]
struct VectorQuery {
Expand All @@ -41,20 +58,6 @@ struct VectorCommitmentWitness {
authentications: Span<felt252>,
}

fn validate_vector_commitment(
config: VectorCommitmentConfig,
expected_height: felt252,
n_verifier_friendly_commitment_layers: felt252,
) {
assert(config.height == expected_height, 'height mismatch');
// Note that n_verifier_friendly_commitment_layers can be greater than height (in such a case,
// all Merkle layers use the verifier-friendly hash).
assert(
config.n_verifier_friendly_commitment_layers == n_verifier_friendly_commitment_layers,
'n_verifier_friendly... mismatch'
);
}

fn vector_commit(
ref channel: Channel, unsent_commitment: felt252, config: VectorCommitmentConfig
) -> VectorCommitment {
Expand Down

0 comments on commit 4e07ae9

Please sign in to comment.