From 4e07ae9c953a5d7735ebaad34142fce5a8fabc83 Mon Sep 17 00:00:00 2001 From: Neo Date: Tue, 9 Jan 2024 23:36:05 +0100 Subject: [PATCH] refactor validation --- src/air/config.cairo | 49 +++++++------ src/fri/fri_config.cairo | 72 ++++++++++--------- src/stark.cairo | 46 +++++++----- .../tests/test_vector_commitment.cairo | 10 +-- src/vector_commitment/vector_commitment.cairo | 31 ++++---- 5 files changed, 116 insertions(+), 92 deletions(-) diff --git a/src/air/config.cairo b/src/air/config.cairo index fa1a7f6be..16b63652b 100644 --- a/src/air/config.cairo +++ b/src/air/config.cairo @@ -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; @@ -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, - ); -} diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo index 6ccf51965..21232cb59 100644 --- a/src/fri/fri_config.cairo +++ b/src/fri/fri_config.cairo @@ -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; @@ -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 + } +} diff --git a/src/stark.cairo b/src/stark.cairo index 61b28a86d..4fa1da390 100644 --- a/src/stark.cairo +++ b/src/stark.cairo @@ -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; @@ -16,6 +18,9 @@ mod stark_verify; #[cfg(test)] mod tests; +const SECURITY_BITS: felt252 = 9; + + #[derive(Drop)] struct StarkProof { config: StarkConfig, @@ -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, @@ -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)] diff --git a/src/vector_commitment/tests/test_vector_commitment.cairo b/src/vector_commitment/tests/test_vector_commitment.cairo index e0d7d34d2..49be2b2e5 100644 --- a/src/vector_commitment/tests/test_vector_commitment.cairo +++ b/src/vector_commitment/tests/test_vector_commitment.cairo @@ -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; @@ -34,7 +35,7 @@ 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] @@ -42,7 +43,7 @@ fn test_validate_vector_commitment() { #[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] @@ -50,6 +51,5 @@ fn test_invalid_validate_vector_commitment_1() { #[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); } - diff --git a/src/vector_commitment/vector_commitment.cairo b/src/vector_commitment/vector_commitment.cairo index 255cc3e38..84196a4c1 100644 --- a/src/vector_commitment/vector_commitment.cairo +++ b/src/vector_commitment/vector_commitment.cairo @@ -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 { @@ -41,20 +58,6 @@ struct VectorCommitmentWitness { authentications: Span, } -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 {