diff --git a/src/fri.cairo b/src/fri.cairo index 4cafa5e60..93bcc141f 100644 --- a/src/fri.cairo +++ b/src/fri.cairo @@ -1,5 +1,7 @@ mod fri_formula; mod fri_group; +mod fri_layer; +mod fri_config; #[cfg(test)] mod tests; diff --git a/src/fri/fri_config.cairo b/src/fri/fri_config.cairo new file mode 100644 index 000000000..c5e89ac07 --- /dev/null +++ b/src/fri/fri_config.cairo @@ -0,0 +1,59 @@ +use core::option::OptionTrait; +use core::array::SpanTrait; +use core::traits::Into; +use cairo_verifier::structs::table_commitment_config::TableCommitmentConfig; + +const MAX_LAST_LAYER_LOG_DEGREE_BOUND: u32 = 15; +const MAX_FRI_LAYERS: u32 = 15; +const MAX_FRI_STEP: u32 = 4; + +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: Span, + // 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: Span, + log_last_layer_degree_bound: felt252, +} + +fn fri_config_validate( + config: FriConfig, log_n_cosets: felt252, n_verifier_friendly_commitment_layers: felt252 +) -> felt252 { + assert(0_u256 <= config.log_last_layer_degree_bound.into(), ''); + assert(config.log_last_layer_degree_bound.try_into().unwrap() <= MAX_LAST_LAYER_LOG_DEGREE_BOUND, ''); + + assert(2_u256 <= config.n_layers.into(), ''); + assert(config.n_layers.try_into().unwrap() <= MAX_FRI_LAYERS + 1, ''); + + assert(*(config.fri_step_sizes[0]) == 0, ''); + + let len: u32 = config.n_layers.try_into().unwrap(); + let mut i = 0_u32; + let mut sum_of_step_sizes: felt252 = 0; + let mut log_input_size = config.log_input_size; + loop { + if i == len { break; } + let fri_step: felt252 = *(config.fri_step_sizes.at(i)); + assert(1_u32 <= fri_step.try_into().unwrap(), ''); + assert(fri_step.try_into().unwrap() <= MAX_FRI_STEP + 1, ''); + assert((*(config.inner_layers.at(i))).columns == fri_step * fri_step, ''); + i += 1; + log_input_size -= fri_step; + sum_of_step_sizes += fri_step; + // validate_vector_commitment( + // config=layers[0].vector, + // expected_height=log_input_size, + // n_verifier_friendly_commitment_layers=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 +} \ No newline at end of file diff --git a/src/fri/fri_layer.cairo b/src/fri/fri_layer.cairo new file mode 100644 index 000000000..e70e3687d --- /dev/null +++ b/src/fri/fri_layer.cairo @@ -0,0 +1,12 @@ +// Constant parameters for computing the next FRI layer. +struct FriLayerComputationParams { + coset_size: felt252, + fri_group: Span, + eval_point: felt252, +} + +struct FriLayerQuery { + index: felt252, + y_value: felt252, + x_inv_value: felt252, +} \ No newline at end of file diff --git a/src/structs.cairo b/src/structs.cairo index b978c4505..000d62f6e 100644 --- a/src/structs.cairo +++ b/src/structs.cairo @@ -1,4 +1,3 @@ -mod fri_config; mod proof_of_work_config; mod stark_config; mod stark_proof; diff --git a/src/structs/fri_config.cairo b/src/structs/fri_config.cairo deleted file mode 100644 index 35ebe0816..000000000 --- a/src/structs/fri_config.cairo +++ /dev/null @@ -1,15 +0,0 @@ -use cairo_verifier::structs::table_commitment_config::TableCommitmentConfig; - -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: TableCommitmentConfig, - // 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: felt252, - log_last_layer_degree_bound: felt252, -} diff --git a/src/structs/stark_config.cairo b/src/structs/stark_config.cairo index af63fec11..9e960da6b 100644 --- a/src/structs/stark_config.cairo +++ b/src/structs/stark_config.cairo @@ -1,6 +1,6 @@ use cairo_verifier::structs::traces_config::TracesConfig; use cairo_verifier::structs::table_commitment_config::TableCommitmentConfig; -use cairo_verifier::structs::fri_config::FriConfig; +use cairo_verifier::fri::fri_config::FriConfig; use cairo_verifier::structs::proof_of_work_config::ProofOfWorkConfig; struct StarkConfig { diff --git a/src/structs/table_commitment_config.cairo b/src/structs/table_commitment_config.cairo index 45ed8906b..30238bfed 100644 --- a/src/structs/table_commitment_config.cairo +++ b/src/structs/table_commitment_config.cairo @@ -1,5 +1,6 @@ use cairo_verifier::structs::vector_commitment_config::VectorCommitmentConfig; +#[derive(Drop, Copy)] struct TableCommitmentConfig { columns: felt252, vector: VectorCommitmentConfig diff --git a/src/structs/vector_commitment_config.cairo b/src/structs/vector_commitment_config.cairo index 578cd82ec..b7079ed6a 100644 --- a/src/structs/vector_commitment_config.cairo +++ b/src/structs/vector_commitment_config.cairo @@ -1,3 +1,4 @@ +#[derive(Drop, Copy)] struct VectorCommitmentConfig { height: felt252, verifier_friendly_commitment_layers: felt252,