From e66c616ae33aa856c1f2568f7438bc4e858d0e15 Mon Sep 17 00:00:00 2001 From: Neo Date: Thu, 4 Jan 2024 23:31:51 +0100 Subject: [PATCH] basic validation passes --- src/input_structs/stark_config.cairo | 10 ++---- src/lib.cairo | 13 +++++--- src/validation.cairo | 5 +++ src/validation/asserts.cairo | 6 ++++ src/validation/config.cairo | 31 +++++++++++++++++++ src/validation/proof_of_work.cairo | 10 ++++++ src/validation/stark.cairo | 11 +++++++ src/validation/stark_config.cairo | 24 ++++++++++++++ src/vector_commitment/vector_commitment.cairo | 2 +- 9 files changed, 99 insertions(+), 13 deletions(-) create mode 100644 src/validation.cairo create mode 100644 src/validation/asserts.cairo create mode 100644 src/validation/config.cairo create mode 100644 src/validation/proof_of_work.cairo create mode 100644 src/validation/stark.cairo create mode 100644 src/validation/stark_config.cairo diff --git a/src/input_structs/stark_config.cairo b/src/input_structs/stark_config.cairo index 92b6e4192..a54444441 100644 --- a/src/input_structs/stark_config.cairo +++ b/src/input_structs/stark_config.cairo @@ -1,3 +1,5 @@ +use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfig; + #[derive(Drop, Serde)] struct StarkConfig { traces: TracesConfig, @@ -23,16 +25,10 @@ struct TracesConfig { #[derive(Drop, Serde)] struct TableCommitmentConfig { - columns: felt252, + n_columns: felt252, vector: VectorCommitmentConfig } -#[derive(Drop, Serde)] -struct VectorCommitmentConfig { - height: felt252, - verifier_friendly_commitment_layers: felt252, -} - #[derive(Drop, Serde)] struct FriConfig { // Log2 of the size of the input layer to FRI. diff --git a/src/lib.cairo b/src/lib.cairo index f73e223d9..59ef8771c 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,17 +1,20 @@ +mod air; mod channel; mod common; +mod fri; mod input_structs; -mod structs; -mod air; mod oods; -mod fri; +mod queries; +mod structs; mod table_commitment; +mod validation; mod vector_commitment; -mod queries; -use cairo_verifier::input_structs::stark_proof::StarkProof; +use cairo_verifier::{input_structs::stark_proof::StarkProof, validation::stark::verify_stark_proof}; + fn main(x: Array) { let mut x_span = x.span(); let stark_proof: StarkProof = Serde::deserialize(ref x_span).unwrap(); + verify_stark_proof(stark_proof); } diff --git a/src/validation.cairo b/src/validation.cairo new file mode 100644 index 000000000..1ee69521e --- /dev/null +++ b/src/validation.cairo @@ -0,0 +1,5 @@ +mod asserts; +mod config; +mod proof_of_work; +mod stark_config; +mod stark; diff --git a/src/validation/asserts.cairo b/src/validation/asserts.cairo new file mode 100644 index 000000000..5603bf777 --- /dev/null +++ b/src/validation/asserts.cairo @@ -0,0 +1,6 @@ +use cairo_verifier::common::math::Felt252PartialOrd; + +fn assert_in_range(x: felt252, min: felt252, max: felt252) { + assert(min <= x, 'Value too small'); + assert(x < max, 'Value too large'); +} diff --git a/src/validation/config.cairo b/src/validation/config.cairo new file mode 100644 index 000000000..20945db3b --- /dev/null +++ b/src/validation/config.cairo @@ -0,0 +1,31 @@ +use cairo_verifier::{ + input_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/proof_of_work.cairo b/src/validation/proof_of_work.cairo new file mode 100644 index 000000000..e325d5d45 --- /dev/null +++ b/src/validation/proof_of_work.cairo @@ -0,0 +1,10 @@ +use cairo_verifier::input_structs::stark_config::ProofOfWorkConfig; +use cairo_verifier::common::math::Felt252PartialOrd; + +const MIN_PROOF_OF_WORK_BITS: felt252 = 30; +const MAX_PROOF_OF_WORK_BITS: felt252 = 50; + +fn proof_of_work_config_validate(config: ProofOfWorkConfig) { + assert(MIN_PROOF_OF_WORK_BITS <= config.n_bits, 'Too few bits for proof of work'); + assert(config.n_bits < MAX_PROOF_OF_WORK_BITS, 'Too many bits for proof of work'); +} diff --git a/src/validation/stark.cairo b/src/validation/stark.cairo new file mode 100644 index 000000000..0faa2267c --- /dev/null +++ b/src/validation/stark.cairo @@ -0,0 +1,11 @@ +use cairo_verifier::{ + input_structs::stark_proof::StarkProof, + validation::stark_config::stark_config_validate +}; + +const SECURITY_BITS: felt252 = 96; + + +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 new file mode 100644 index 000000000..d88e5230d --- /dev/null +++ b/src/validation/stark_config.cairo @@ -0,0 +1,24 @@ +use cairo_verifier::{ + input_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, + // stark_config.log_n_cosets, + // stark_config.n_verifier_friendly_commitment_layers + // ); +} diff --git a/src/vector_commitment/vector_commitment.cairo b/src/vector_commitment/vector_commitment.cairo index 255cc3e38..7274680fa 100644 --- a/src/vector_commitment/vector_commitment.cairo +++ b/src/vector_commitment/vector_commitment.cairo @@ -12,7 +12,7 @@ struct VectorCommitment { commitment_hash: felt252 } -#[derive(Drop, Copy)] +#[derive(Drop, Copy, Serde)] struct VectorCommitmentConfig { height: felt252, n_verifier_friendly_commitment_layers: felt252,