diff --git a/src/common.cairo b/src/common.cairo index 68b1bdd9c..55c44bf54 100644 --- a/src/common.cairo +++ b/src/common.cairo @@ -9,6 +9,7 @@ mod array_extend; mod array_split; mod consts; mod merge_sort; +mod asserts; #[cfg(test)] mod tests; diff --git a/src/validation/asserts.cairo b/src/common/asserts.cairo similarity index 100% rename from src/validation/asserts.cairo rename to src/common/asserts.cairo diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo index 3d3fe6060..90241221a 100644 --- a/src/fri/fri_config.cairo +++ b/src/fri/fri_config.cairo @@ -2,7 +2,7 @@ use core::traits::TryInto; use core::traits::Into; use cairo_verifier::{ common::math::{pow, Felt252PartialOrd}, table_commitment::TableCommitmentConfig, - structs::stark_config::FriConfig as FriConfigInputStruct, validation::asserts::assert_in_range, + structs::stark_config::FriConfig as FriConfigInputStruct, common::asserts::assert_in_range, vector_commitment::vector_commitment::{validate_vector_commitment, VectorCommitmentConfig}, }; diff --git a/src/lib.cairo b/src/lib.cairo index f374d5798..6e7836827 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -9,7 +9,7 @@ mod table_commitment; mod validation; mod vector_commitment; -use cairo_verifier::{structs::stark_proof::StarkProof, validation::stark::verify_stark_proof}; +use cairo_verifier::{structs::stark_proof::StarkProof, structs::stark_proof::verify_stark_proof}; fn main(x: Array) { diff --git a/src/structs/stark_config.cairo b/src/structs/stark_config.cairo index 03d842f7b..9991c94f6 100644 --- a/src/structs/stark_config.cairo +++ b/src/structs/stark_config.cairo @@ -1,9 +1,15 @@ use cairo_verifier::{ + common::asserts::assert_in_range, fri::fri_config::FriConfig as DeserializationUnfriendlyFriConfig, table_commitment::TableCommitmentConfig, - vector_commitment::vector_commitment::VectorCommitmentConfig, + vector_commitment::vector_commitment::{VectorCommitmentConfig, validate_vector_commitment}, + validation::proof_of_work::proof_of_work_config_validate, fri::fri_config::fri_config_validate, }; +const MAX_N_COLUMNS: felt252 = 128; +const AIR_LAYOUT_N_ORIGINAL_COLUMNS: felt252 = 12; +const AIR_LAYOUT_N_INTERACTION_COLUMNS: felt252 = 3; + #[derive(Drop, Serde)] struct StarkConfig { traces: TracesConfig, @@ -21,12 +27,52 @@ struct StarkConfig { n_verifier_friendly_commitment_layers: felt252, } +fn stark_config_validate(stark_config: StarkConfig, security_bits: felt252) { + proof_of_work_config_validate(stark_config.proof_of_work); + + 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); + + 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 + ); +} + #[derive(Drop, Serde)] struct TracesConfig { original: TableCommitmentConfig, interaction: TableCommitmentConfig, } +// 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, + ); +} + #[derive(Drop, Serde)] struct FriConfig { // Log2 of the size of the input layer to FRI. diff --git a/src/structs/stark_proof.cairo b/src/structs/stark_proof.cairo index 7363248af..33b427feb 100644 --- a/src/structs/stark_proof.cairo +++ b/src/structs/stark_proof.cairo @@ -1,8 +1,10 @@ use cairo_verifier::structs::{ - stark_config::StarkConfig, public_input::PublicInput, + stark_config::{StarkConfig, stark_config_validate}, public_input::PublicInput, stark_unsent_commitment::StarkUnsentCommitment, stark_witness::StarkWitness, }; +const SECURITY_BITS: felt252 = 9; + #[derive(Drop, Serde)] struct StarkProof { @@ -11,3 +13,7 @@ struct StarkProof { unsent_commitment: StarkUnsentCommitment, witness: StarkWitness, } + +fn verify_stark_proof(proof: StarkProof) { + stark_config_validate(proof.config, SECURITY_BITS); +} diff --git a/src/validation.cairo b/src/validation.cairo index 1ee69521e..e5855ad8e 100644 --- a/src/validation.cairo +++ b/src/validation.cairo @@ -1,5 +1 @@ -mod asserts; -mod config; mod proof_of_work; -mod stark_config; -mod stark; diff --git a/src/validation/config.cairo b/src/validation/config.cairo deleted file mode 100644 index dbfc9c222..000000000 --- a/src/validation/config.cairo +++ /dev/null @@ -1,31 +0,0 @@ -use cairo_verifier::{ - structs::stark_config::TracesConfig, validation::asserts::assert_in_range, - vector_commitment::vector_commitment::validate_vector_commitment, - fri::fri_config::fri_config_validate, -}; - -const MAX_N_COLUMNS: felt252 = 128; -const AIR_LAYOUT_N_ORIGINAL_COLUMNS: felt252 = 12; -const AIR_LAYOUT_N_INTERACTION_COLUMNS: felt252 = 3; - -// 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, - ); -} diff --git a/src/validation/stark.cairo b/src/validation/stark.cairo deleted file mode 100644 index 27734f71a..000000000 --- a/src/validation/stark.cairo +++ /dev/null @@ -1,10 +0,0 @@ -use cairo_verifier::{ - structs::stark_proof::StarkProof, validation::stark_config::stark_config_validate -}; - -const SECURITY_BITS: felt252 = 9; - - -fn verify_stark_proof(proof: StarkProof) { - stark_config_validate(proof.config, SECURITY_BITS); -} diff --git a/src/validation/stark_config.cairo b/src/validation/stark_config.cairo deleted file mode 100644 index 57872df5c..000000000 --- a/src/validation/stark_config.cairo +++ /dev/null @@ -1,24 +0,0 @@ -use cairo_verifier::{ - structs::stark_config::StarkConfig, - validation::{proof_of_work::proof_of_work_config_validate, config::traces_config_validate}, - vector_commitment::vector_commitment::validate_vector_commitment, - fri::fri_config::fri_config_validate, -}; - -fn stark_config_validate(stark_config: StarkConfig, security_bits: felt252) { - proof_of_work_config_validate(stark_config.proof_of_work); - - 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); - - 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 - ); -}