From 3a62939cf96633e78dffecde1074d781569abe4c Mon Sep 17 00:00:00 2001 From: Neo Date: Thu, 4 Jan 2024 23:31:51 +0100 Subject: [PATCH 1/7] basic validation passes --- src/input_structs/stark_config.cairo | 10 ++---- src/lib.cairo | 15 +++++---- 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, 100 insertions(+), 14 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 e724d95fb..edf6e4986 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -1,18 +1,21 @@ +mod air; mod channel; mod common; +mod fri; mod input_structs; -mod structs; -mod air; mod oods; -mod fri; -mod table_commitment; -mod vector_commitment; mod queries; mod proof_of_work; +mod structs; +mod table_commitment; +mod validation; +mod vector_commitment; + +use cairo_verifier::{input_structs::stark_proof::StarkProof, validation::stark::verify_stark_proof}; -use cairo_verifier::input_structs::stark_proof::StarkProof; 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, From 8f990fe923840823cbf20c1ff5605e54de266c13 Mon Sep 17 00:00:00 2001 From: Neo Date: Fri, 5 Jan 2024 09:17:48 +0100 Subject: [PATCH 2/7] security bits adjust --- src/validation/config.cairo | 12 ++++++------ src/validation/stark.cairo | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/validation/config.cairo b/src/validation/config.cairo index 20945db3b..633e50719 100644 --- a/src/validation/config.cairo +++ b/src/validation/config.cairo @@ -22,10 +22,10 @@ fn traces_config_validate( 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, - // ); + 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 index 0faa2267c..0cb248d8a 100644 --- a/src/validation/stark.cairo +++ b/src/validation/stark.cairo @@ -3,7 +3,7 @@ use cairo_verifier::{ validation::stark_config::stark_config_validate }; -const SECURITY_BITS: felt252 = 96; +const SECURITY_BITS: felt252 = 9; fn verify_stark_proof(proof: StarkProof) { From bbc6c6463b32b4a22ec177a7cf22879baf8fa58a Mon Sep 17 00:00:00 2001 From: Neo Date: Fri, 5 Jan 2024 09:20:05 +0100 Subject: [PATCH 3/7] before fri config validation --- src/validation/stark_config.cairo | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/validation/stark_config.cairo b/src/validation/stark_config.cairo index d88e5230d..a1d7a916c 100644 --- a/src/validation/stark_config.cairo +++ b/src/validation/stark_config.cairo @@ -11,11 +11,11 @@ fn stark_config_validate(stark_config: StarkConfig, security_bits: felt252) { 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 - // ); + 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, From b352a1099e5f79b75fe1c11df59d69ddafc8a70a Mon Sep 17 00:00:00 2001 From: Neo Date: Fri, 5 Jan 2024 09:21:38 +0100 Subject: [PATCH 4/7] fmt --- src/validation/stark.cairo | 3 +-- src/validation/stark_config.cairo | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/validation/stark.cairo b/src/validation/stark.cairo index 0cb248d8a..f66f954e5 100644 --- a/src/validation/stark.cairo +++ b/src/validation/stark.cairo @@ -1,6 +1,5 @@ use cairo_verifier::{ - input_structs::stark_proof::StarkProof, - validation::stark_config::stark_config_validate + input_structs::stark_proof::StarkProof, validation::stark_config::stark_config_validate }; const SECURITY_BITS: felt252 = 9; diff --git a/src/validation/stark_config.cairo b/src/validation/stark_config.cairo index a1d7a916c..42135e6a8 100644 --- a/src/validation/stark_config.cairo +++ b/src/validation/stark_config.cairo @@ -16,9 +16,9 @@ fn stark_config_validate(stark_config: StarkConfig, security_bits: felt252) { 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 - // ); +// fri_config_validate( +// stark_config.fri, +// stark_config.log_n_cosets, +// stark_config.n_verifier_friendly_commitment_layers +// ); } From bf8e1966171a6cd72c75947a3a0d1f0858426933 Mon Sep 17 00:00:00 2001 From: Neo Date: Fri, 5 Jan 2024 11:37:14 +0100 Subject: [PATCH 5/7] fri config validation working --- run.sh | 4 +- src/fri/fri_config.cairo | 76 +++++++++++++------ src/input_structs.cairo | 5 -- src/input_structs/public_input.cairo | 16 ---- src/input_structs/stark_config.cairo | 51 ------------- src/input_structs/stark_proof.cairo | 13 ---- src/lib.cairo | 5 +- src/structs.cairo | 7 +- src/structs/proof_of_work_config.cairo | 4 - src/structs/public_input.cairo | 20 +++-- src/structs/stark_config.cairo | 39 +++++++++- src/structs/stark_proof.cairo | 13 +++- .../stark_unsent_commitment.cairo | 0 .../stark_witness.cairo | 0 src/structs/traces_config.cairo | 6 -- src/structs/unsent_commitment.cairo | 1 - src/structs/witness.cairo | 1 - src/validation/config.cairo | 2 +- src/validation/proof_of_work.cairo | 2 +- src/validation/stark.cairo | 2 +- src/validation/stark_config.cairo | 12 +-- 21 files changed, 127 insertions(+), 152 deletions(-) delete mode 100644 src/input_structs.cairo delete mode 100644 src/input_structs/public_input.cairo delete mode 100644 src/input_structs/stark_config.cairo delete mode 100644 src/input_structs/stark_proof.cairo delete mode 100644 src/structs/proof_of_work_config.cairo rename src/{input_structs => structs}/stark_unsent_commitment.cairo (100%) rename src/{input_structs => structs}/stark_witness.cairo (100%) delete mode 100644 src/structs/traces_config.cairo delete mode 100644 src/structs/unsent_commitment.cairo delete mode 100644 src/structs/witness.cairo diff --git a/run.sh b/run.sh index 0070727a7..5cd5a1925 100755 --- a/run.sh +++ b/run.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -scarb build -cd runner +scarb build && \ +cd runner && \ cargo run --release -- ../target/dev/cairo_verifier.sierra.json < resources/parserin.txt diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo index 6ccf51965..a070c5db4 100644 --- a/src/fri/fri_config.cairo +++ b/src/fri/fri_config.cairo @@ -1,11 +1,14 @@ +use core::traits::TryInto; +use core::traits::Into; use cairo_verifier::{ - table_commitment::TableCommitmentConfig, + common::math::{pow, Felt252PartialOrd}, table_commitment::TableCommitmentConfig, + structs::stark_config::FriConfig as FriConfigInputStruct, validation::asserts::assert_in_range, vector_commitment::vector_commitment::{validate_vector_commitment, VectorCommitmentConfig}, }; -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 { @@ -22,46 +25,71 @@ struct FriConfig { log_last_layer_degree_bound: felt252, } +impl FriConfigFrom of Into { + fn into(self: FriConfigInputStruct) -> FriConfig { + let mut inner_layers = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == self.inner_layers.len() { + break; + } + + inner_layers + .append( + TableCommitmentConfig { + n_columns: *self.inner_layers.at(i), + vector: VectorCommitmentConfig { + height: *self.inner_layers.at(i + 1), + n_verifier_friendly_commitment_layers: *self.inner_layers.at(i + 2), + } + } + ); + i += 3; + }; + + FriConfig { + log_input_size: self.log_input_size, + n_layers: self.n_layers, + inner_layers: inner_layers.span(), + fri_step_sizes: self.fri_step_sizes.span(), + log_last_layer_degree_bound: self.log_last_layer_degree_bound, + } + } +} + 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(); - - assert(log_last_layer_degree_bound <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, 'Value too big'); - + assert_in_range(config.n_layers, 2, MAX_FRI_LAYERS + 1); + assert(config.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(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 = config.n_layers.try_into().unwrap(); + let mut sum_of_step_sizes = 0; let mut log_input_size = config.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_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; + let fri_step = *config.fri_step_sizes.at(i); + let table_commitment = *config.inner_layers.at(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'); validate_vector_commitment( table_commitment.vector, log_input_size, n_verifier_friendly_commitment_layers, ); + + i += 1; }; 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, ''); + assert( + log_expected_input_degree + log_n_cosets == config.log_input_size, 'Log input size mismatch' + ); log_expected_input_degree } - diff --git a/src/input_structs.cairo b/src/input_structs.cairo deleted file mode 100644 index f0e05a778..000000000 --- a/src/input_structs.cairo +++ /dev/null @@ -1,5 +0,0 @@ -mod public_input; -mod stark_config; -mod stark_proof; -mod stark_unsent_commitment; -mod stark_witness; diff --git a/src/input_structs/public_input.cairo b/src/input_structs/public_input.cairo deleted file mode 100644 index 63250014d..000000000 --- a/src/input_structs/public_input.cairo +++ /dev/null @@ -1,16 +0,0 @@ -#[derive(Drop, Serde)] -struct PublicInput { - log_n_steps: felt252, - range_check_min: felt252, - range_check_max: felt252, - layout: felt252, - dynamic_params: Array, - n_segments: felt252, - segments: Array, - padding_addr: felt252, - padding_value: felt252, - main_page_len: felt252, - main_page: Array, - n_continuous_pages: felt252, - continuous_page_headers: Array, -} diff --git a/src/input_structs/stark_config.cairo b/src/input_structs/stark_config.cairo deleted file mode 100644 index a54444441..000000000 --- a/src/input_structs/stark_config.cairo +++ /dev/null @@ -1,51 +0,0 @@ -use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfig; - -#[derive(Drop, Serde)] -struct StarkConfig { - traces: TracesConfig, - composition: TableCommitmentConfig, - fri: FriConfig, - proof_of_work: ProofOfWorkConfig, - // Log2 of the trace domain size. - log_trace_domain_size: felt252, - // Number of queries to the last component, FRI. - n_queries: felt252, - // Log2 of the number of cosets composing the evaluation domain, where the coset size is the - // trace length. - log_n_cosets: felt252, - // Number of layers that use a verifier friendly hash in each commitment. - n_verifier_friendly_commitment_layers: felt252, -} - -#[derive(Drop, Serde)] -struct TracesConfig { - original: TableCommitmentConfig, - interaction: TableCommitmentConfig, -} - -#[derive(Drop, Serde)] -struct TableCommitmentConfig { - n_columns: felt252, - vector: VectorCommitmentConfig -} - -#[derive(Drop, Serde)] -struct FriConfig { - // Log2 of the size of the input layer to FRI. - log_input_size: felt252, - // Number of layers in the FRI. Inner + last layer. - n_layers: felt252, - // Array of size n_layers - 1, each entry is a configuration of a table commitment for the - // corresponding inner layer. - inner_layers: Array, - // Array of size n_layers, each entry represents the FRI step size, - // i.e. the number of FRI-foldings between layer i and i+1. - fri_step_sizes: Array, - log_last_layer_degree_bound: felt252, -} - -#[derive(Drop, Serde)] -struct ProofOfWorkConfig { - // Proof of work difficulty (number of bits required to be 0). - n_bits: felt252, -} diff --git a/src/input_structs/stark_proof.cairo b/src/input_structs/stark_proof.cairo deleted file mode 100644 index b81942347..000000000 --- a/src/input_structs/stark_proof.cairo +++ /dev/null @@ -1,13 +0,0 @@ -use cairo_verifier::input_structs::{ - stark_config::StarkConfig, public_input::PublicInput, - stark_unsent_commitment::StarkUnsentCommitment, stark_witness::StarkWitness, -}; - - -#[derive(Drop, Serde)] -struct StarkProof { - config: StarkConfig, - public_input: PublicInput, - unsent_commitment: StarkUnsentCommitment, - witness: StarkWitness, -} diff --git a/src/lib.cairo b/src/lib.cairo index edf6e4986..b00022b4c 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -2,16 +2,15 @@ mod air; mod channel; mod common; mod fri; -mod input_structs; +mod structs; mod oods; mod queries; mod proof_of_work; -mod structs; mod table_commitment; mod validation; mod vector_commitment; -use cairo_verifier::{input_structs::stark_proof::StarkProof, validation::stark::verify_stark_proof}; +use cairo_verifier::{structs::stark_proof::StarkProof, validation::stark::verify_stark_proof}; fn main(x: Array) { diff --git a/src/structs.cairo b/src/structs.cairo index b421df63d..f0e05a778 100644 --- a/src/structs.cairo +++ b/src/structs.cairo @@ -1,6 +1,5 @@ -mod proof_of_work_config; +mod public_input; mod stark_config; mod stark_proof; -mod traces_config; -mod unsent_commitment; -mod witness; +mod stark_unsent_commitment; +mod stark_witness; diff --git a/src/structs/proof_of_work_config.cairo b/src/structs/proof_of_work_config.cairo deleted file mode 100644 index 6c09b64d4..000000000 --- a/src/structs/proof_of_work_config.cairo +++ /dev/null @@ -1,4 +0,0 @@ -struct ProofOfWorkConfig { - // Proof of work difficulty (number of bits required to be 0). - n_bits: felt252, -} diff --git a/src/structs/public_input.cairo b/src/structs/public_input.cairo index def548509..63250014d 100644 --- a/src/structs/public_input.cairo +++ b/src/structs/public_input.cairo @@ -1,6 +1,16 @@ -struct StarkProof { - config: StarkConfig, -// public_input: PublicInput, -// unsent_commitment: StarkUnsentCommitment, -// witness: StarkWitness, +#[derive(Drop, Serde)] +struct PublicInput { + log_n_steps: felt252, + range_check_min: felt252, + range_check_max: felt252, + layout: felt252, + dynamic_params: Array, + n_segments: felt252, + segments: Array, + padding_addr: felt252, + padding_value: felt252, + main_page_len: felt252, + main_page: Array, + n_continuous_pages: felt252, + continuous_page_headers: Array, } diff --git a/src/structs/stark_config.cairo b/src/structs/stark_config.cairo index 49e29d814..a54444441 100644 --- a/src/structs/stark_config.cairo +++ b/src/structs/stark_config.cairo @@ -1,8 +1,6 @@ -use cairo_verifier::{ - structs::traces_config::TracesConfig, table_commitment::TableCommitmentConfig, - fri::fri_config::FriConfig, structs::proof_of_work_config::ProofOfWorkConfig -}; +use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfig; +#[derive(Drop, Serde)] struct StarkConfig { traces: TracesConfig, composition: TableCommitmentConfig, @@ -18,3 +16,36 @@ struct StarkConfig { // Number of layers that use a verifier friendly hash in each commitment. n_verifier_friendly_commitment_layers: felt252, } + +#[derive(Drop, Serde)] +struct TracesConfig { + original: TableCommitmentConfig, + interaction: TableCommitmentConfig, +} + +#[derive(Drop, Serde)] +struct TableCommitmentConfig { + n_columns: felt252, + vector: VectorCommitmentConfig +} + +#[derive(Drop, Serde)] +struct FriConfig { + // Log2 of the size of the input layer to FRI. + log_input_size: felt252, + // Number of layers in the FRI. Inner + last layer. + n_layers: felt252, + // Array of size n_layers - 1, each entry is a configuration of a table commitment for the + // corresponding inner layer. + inner_layers: Array, + // Array of size n_layers, each entry represents the FRI step size, + // i.e. the number of FRI-foldings between layer i and i+1. + fri_step_sizes: Array, + log_last_layer_degree_bound: felt252, +} + +#[derive(Drop, Serde)] +struct ProofOfWorkConfig { + // Proof of work difficulty (number of bits required to be 0). + n_bits: felt252, +} diff --git a/src/structs/stark_proof.cairo b/src/structs/stark_proof.cairo index dd1a97d85..7363248af 100644 --- a/src/structs/stark_proof.cairo +++ b/src/structs/stark_proof.cairo @@ -1,8 +1,13 @@ -use cairo_verifier::structs::stark_config::StarkConfig; +use cairo_verifier::structs::{ + stark_config::StarkConfig, public_input::PublicInput, + stark_unsent_commitment::StarkUnsentCommitment, stark_witness::StarkWitness, +}; + +#[derive(Drop, Serde)] struct StarkProof { config: StarkConfig, -// public_input: PublicInput, -// unsent_commitment: StarkUnsentCommitment, -// witness: StarkWitness, + public_input: PublicInput, + unsent_commitment: StarkUnsentCommitment, + witness: StarkWitness, } diff --git a/src/input_structs/stark_unsent_commitment.cairo b/src/structs/stark_unsent_commitment.cairo similarity index 100% rename from src/input_structs/stark_unsent_commitment.cairo rename to src/structs/stark_unsent_commitment.cairo diff --git a/src/input_structs/stark_witness.cairo b/src/structs/stark_witness.cairo similarity index 100% rename from src/input_structs/stark_witness.cairo rename to src/structs/stark_witness.cairo diff --git a/src/structs/traces_config.cairo b/src/structs/traces_config.cairo deleted file mode 100644 index 7cbf55fe7..000000000 --- a/src/structs/traces_config.cairo +++ /dev/null @@ -1,6 +0,0 @@ -use cairo_verifier::table_commitment::TableCommitmentConfig; - -struct TracesConfig { - original: TableCommitmentConfig, - interaction: TableCommitmentConfig, -} diff --git a/src/structs/unsent_commitment.cairo b/src/structs/unsent_commitment.cairo deleted file mode 100644 index 8b1378917..000000000 --- a/src/structs/unsent_commitment.cairo +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/structs/witness.cairo b/src/structs/witness.cairo deleted file mode 100644 index 8b1378917..000000000 --- a/src/structs/witness.cairo +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/validation/config.cairo b/src/validation/config.cairo index 633e50719..dbfc9c222 100644 --- a/src/validation/config.cairo +++ b/src/validation/config.cairo @@ -1,5 +1,5 @@ use cairo_verifier::{ - input_structs::stark_config::TracesConfig, validation::asserts::assert_in_range, + structs::stark_config::TracesConfig, validation::asserts::assert_in_range, vector_commitment::vector_commitment::validate_vector_commitment, fri::fri_config::fri_config_validate, }; diff --git a/src/validation/proof_of_work.cairo b/src/validation/proof_of_work.cairo index e325d5d45..06c8ef831 100644 --- a/src/validation/proof_of_work.cairo +++ b/src/validation/proof_of_work.cairo @@ -1,4 +1,4 @@ -use cairo_verifier::input_structs::stark_config::ProofOfWorkConfig; +use cairo_verifier::structs::stark_config::ProofOfWorkConfig; use cairo_verifier::common::math::Felt252PartialOrd; const MIN_PROOF_OF_WORK_BITS: felt252 = 30; diff --git a/src/validation/stark.cairo b/src/validation/stark.cairo index f66f954e5..27734f71a 100644 --- a/src/validation/stark.cairo +++ b/src/validation/stark.cairo @@ -1,5 +1,5 @@ use cairo_verifier::{ - input_structs::stark_proof::StarkProof, validation::stark_config::stark_config_validate + structs::stark_proof::StarkProof, validation::stark_config::stark_config_validate }; const SECURITY_BITS: felt252 = 9; diff --git a/src/validation/stark_config.cairo b/src/validation/stark_config.cairo index 42135e6a8..57872df5c 100644 --- a/src/validation/stark_config.cairo +++ b/src/validation/stark_config.cairo @@ -1,5 +1,5 @@ use cairo_verifier::{ - input_structs::stark_config::StarkConfig, + 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, @@ -16,9 +16,9 @@ fn stark_config_validate(stark_config: StarkConfig, security_bits: felt252) { 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 -// ); + fri_config_validate( + stark_config.fri.into(), + stark_config.log_n_cosets, + stark_config.n_verifier_friendly_commitment_layers + ); } From d72715c15bebf9956c855b859b27196342defc0e Mon Sep 17 00:00:00 2001 From: Neo Date: Fri, 5 Jan 2024 11:44:40 +0100 Subject: [PATCH 6/7] move into for FriConfig impl --- src/fri/fri_config.cairo | 32 ------------------------- src/structs/stark_config.cairo | 44 ++++++++++++++++++++++++++++------ src/table_commitment.cairo | 2 +- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo index a070c5db4..3d3fe6060 100644 --- a/src/fri/fri_config.cairo +++ b/src/fri/fri_config.cairo @@ -25,38 +25,6 @@ struct FriConfig { log_last_layer_degree_bound: felt252, } -impl FriConfigFrom of Into { - fn into(self: FriConfigInputStruct) -> FriConfig { - let mut inner_layers = ArrayTrait::::new(); - let mut i = 0; - loop { - if i == self.inner_layers.len() { - break; - } - - inner_layers - .append( - TableCommitmentConfig { - n_columns: *self.inner_layers.at(i), - vector: VectorCommitmentConfig { - height: *self.inner_layers.at(i + 1), - n_verifier_friendly_commitment_layers: *self.inner_layers.at(i + 2), - } - } - ); - i += 3; - }; - - FriConfig { - log_input_size: self.log_input_size, - n_layers: self.n_layers, - inner_layers: inner_layers.span(), - fri_step_sizes: self.fri_step_sizes.span(), - log_last_layer_degree_bound: self.log_last_layer_degree_bound, - } - } -} - fn fri_config_validate( config: FriConfig, log_n_cosets: felt252, n_verifier_friendly_commitment_layers: felt252 ) -> felt252 { diff --git a/src/structs/stark_config.cairo b/src/structs/stark_config.cairo index a54444441..03d842f7b 100644 --- a/src/structs/stark_config.cairo +++ b/src/structs/stark_config.cairo @@ -1,4 +1,8 @@ -use cairo_verifier::vector_commitment::vector_commitment::VectorCommitmentConfig; +use cairo_verifier::{ + fri::fri_config::FriConfig as DeserializationUnfriendlyFriConfig, + table_commitment::TableCommitmentConfig, + vector_commitment::vector_commitment::VectorCommitmentConfig, +}; #[derive(Drop, Serde)] struct StarkConfig { @@ -23,12 +27,6 @@ struct TracesConfig { interaction: TableCommitmentConfig, } -#[derive(Drop, Serde)] -struct TableCommitmentConfig { - n_columns: felt252, - vector: VectorCommitmentConfig -} - #[derive(Drop, Serde)] struct FriConfig { // Log2 of the size of the input layer to FRI. @@ -44,6 +42,38 @@ struct FriConfig { log_last_layer_degree_bound: felt252, } +impl IntoDeserializationUnfriendlyFriConfig of Into { + fn into(self: FriConfig) -> DeserializationUnfriendlyFriConfig { + let mut inner_layers = ArrayTrait::::new(); + let mut i = 0; + loop { + if i == self.inner_layers.len() { + break; + } + + inner_layers + .append( + TableCommitmentConfig { + n_columns: *self.inner_layers.at(i), + vector: VectorCommitmentConfig { + height: *self.inner_layers.at(i + 1), + n_verifier_friendly_commitment_layers: *self.inner_layers.at(i + 2), + } + } + ); + i += 3; + }; + + DeserializationUnfriendlyFriConfig { + log_input_size: self.log_input_size, + n_layers: self.n_layers, + inner_layers: inner_layers.span(), + fri_step_sizes: self.fri_step_sizes.span(), + log_last_layer_degree_bound: self.log_last_layer_degree_bound, + } + } +} + #[derive(Drop, Serde)] struct ProofOfWorkConfig { // Proof of work difficulty (number of bits required to be 0). diff --git a/src/table_commitment.cairo b/src/table_commitment.cairo index 79775baa4..c68bb107a 100644 --- a/src/table_commitment.cairo +++ b/src/table_commitment.cairo @@ -16,7 +16,7 @@ struct TableCommitment { vector_commitment: VectorCommitment, } -#[derive(Drop, Copy)] +#[derive(Drop, Copy, Serde)] struct TableCommitmentConfig { n_columns: felt252, vector: VectorCommitmentConfig, From 87a98bc272f4e0fc5878c679b9764e4c188258c4 Mon Sep 17 00:00:00 2001 From: Neo Date: Mon, 8 Jan 2024 22:39:41 +0100 Subject: [PATCH 7/7] move validation to relevant structs --- src/common.cairo | 1 + src/{validation => common}/asserts.cairo | 0 src/fri/fri_config.cairo | 2 +- src/lib.cairo | 2 +- src/structs/stark_config.cairo | 48 +++++++++++++++++++++++- src/structs/stark_proof.cairo | 8 +++- src/validation.cairo | 4 -- src/validation/config.cairo | 31 --------------- src/validation/stark.cairo | 10 ----- src/validation/stark_config.cairo | 24 ------------ 10 files changed, 57 insertions(+), 73 deletions(-) rename src/{validation => common}/asserts.cairo (100%) delete mode 100644 src/validation/config.cairo delete mode 100644 src/validation/stark.cairo delete mode 100644 src/validation/stark_config.cairo diff --git a/src/common.cairo b/src/common.cairo index 01f9ca5a5..f06197a42 100644 --- a/src/common.cairo +++ b/src/common.cairo @@ -11,6 +11,7 @@ mod array_split; mod consts; mod merge_sort; mod powers_array; +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 b00022b4c..82bd81137 100644 --- a/src/lib.cairo +++ b/src/lib.cairo @@ -10,7 +10,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 - ); -}