diff --git a/src/fri/fri.cairo b/src/fri/fri.cairo index 3b0baa559..83627c040 100644 --- a/src/fri/fri.cairo +++ b/src/fri/fri.cairo @@ -7,15 +7,15 @@ use cairo_verifier::table_commitment::{ TableCommitment, TableCommitmentConfig, TableUnsentCommitment }; use core::array::ArrayTrait; -use cairo_verifier::table_commitment::table_commit; +use cairo_verifier::table_commitment::{table_commit, table_decommit}; use cairo_verifier::channel::channel::Channel; use cairo_verifier::channel::channel::{ChannelUnsentFelt, ChannelSentFelt}; use cairo_verifier::fri::fri_config::FriConfig; use cairo_verifier::common::math; -use cairo_verifier::table_commitment::TableCommitmentWitness; +use cairo_verifier::table_commitment::{TableCommitmentWitness, TableDecommitment}; use cairo_verifier::fri::fri_first_layer::gather_first_layer_queries; use cairo_verifier::fri::fri_group::get_fri_group; -use cairo_verifier::fri::fri_layer::FriLayerQuery; +use cairo_verifier::fri::fri_layer::{FriLayerQuery, FriLayerComputationParams, compute_next_layer}; // Commitment values for FRI. Used to generate a commitment by "reading" these values // from the channel. @@ -149,7 +149,7 @@ fn fri_decommit_layers( layer_witness: Span, eval_points: Span, step_sizes: Span, - queries: Span, + mut queries: Span, ) -> Array { let last_queries = ArrayTrait::::new(); let len: u32 = n_layers.try_into().unwrap(); @@ -159,8 +159,28 @@ fn fri_decommit_layers( if i == len { break; } - // + // Params. + let coset_size = math::pow(2, *(step_sizes.at(i))); + let params = FriLayerComputationParams { + coset_size: coset_size, fri_group: fri_group, eval_point: *(eval_points.at(i)) + }; + + // Compute next layer queries. + let (next_queries, verify_indices, verify_y_values) = compute_next_layer( + queries, *(layer_witness.at(i)).leaves, params + ); + queries = next_queries.span(); + + // Table decommitment. + table_decommit( + *commitment.at(i), + verify_indices.span(), + TableDecommitment { values: verify_y_values.span() }, + *layer_witness.at(i).table_witness + ); + + i += 1; }; last_queries @@ -181,4 +201,14 @@ fn fri_decommit( ); let fri_group = get_fri_group(); + + let last_queries = fri_decommit_layers( + fri_group.span(), + commitment.config.n_layers - 1, + commitment.inner_layers, + witness.layers, + commitment.eval_points.slice(1, commitment.eval_points.len() - 1), + commitment.config.fri_step_sizes.slice(1, commitment.config.fri_step_sizes.len() - 1), + fri_queries.span(), + ); } diff --git a/src/fri/fri_layer.cairo b/src/fri/fri_layer.cairo index 05a8b8f9b..37cce1f08 100644 --- a/src/fri/fri_layer.cairo +++ b/src/fri/fri_layer.cairo @@ -34,7 +34,6 @@ struct FriLayerQuery { // query was consumed by this function. fn compute_coset_elements( - mut n_queries: felt252, queries: Span, sibling_witness: Span, coset_size: felt252, @@ -45,6 +44,7 @@ fn compute_coset_elements( let mut coset_elements = ArrayTrait::::new(); let mut coset_x_inv: felt252 = 0; + let len = queries.len(); let mut i: u32 = 0; let mut j: u32 = 0; @@ -53,10 +53,9 @@ fn compute_coset_elements( break; } - if n_queries != 0 && *(queries.at(i)).index == coset_start_index + offset_within_coset { + if i != len && *(queries.at(i)).index == coset_start_index + offset_within_coset { coset_elements.append(*(queries.at(i)).y_value); coset_x_inv = (*(queries.at(i)).x_inv_value) * (*(fri_group.at(i))); - n_queries -= 1; i += 1; } else { coset_elements.append(*(sibling_witness.at(j))); @@ -81,10 +80,7 @@ fn compute_coset_elements( // - verify_indices: query indices of the given layer for Merkle verification. // - verify_y_values: query y values of the given layer for Merkle verification. fn compute_next_layer( - mut n_queries: felt252, - queries: Span, - sibling_witness: Span, - params: FriLayerComputationParams, + queries: Span, sibling_witness: Span, params: FriLayerComputationParams, ) -> (Array, Array, Array) { let mut next_queries = ArrayTrait::::new(); let mut verify_indices = ArrayTrait::::new(); @@ -92,9 +88,10 @@ fn compute_next_layer( let coset_size = params.coset_size; + let len = queries.len(); let mut i: u32 = 0; loop { - if n_queries == 0 { + if i == len { break; } @@ -104,13 +101,7 @@ fn compute_next_layer( verify_indices.append(coset_index); let (coset_elements, coset_x_inv) = compute_coset_elements( - n_queries, - queries, - sibling_witness, - coset_size, - coset_index * coset_size, - 0, - params.fri_group + queries, sibling_witness, coset_size, coset_index * coset_size, 0, params.fri_group ); let coset_elements_len = coset_elements.len(); diff --git a/src/table_commitment.cairo b/src/table_commitment.cairo index a0189fccb..d5dbf77eb 100644 --- a/src/table_commitment.cairo +++ b/src/table_commitment.cairo @@ -1,4 +1,6 @@ -use cairo_verifier::vector_commitment::{VectorCommitmentConfig, VectorUnsentCommitment, VectorCommitment, VectorCommitmentWitness}; +use cairo_verifier::vector_commitment::{ + VectorCommitmentConfig, VectorUnsentCommitment, VectorCommitment, VectorCommitmentWitness +}; use cairo_verifier::channel::channel::ChannelSentFelt; // Commitment values for a table commitment protocol. Used to generate a commitment by "reading" @@ -26,7 +28,6 @@ struct TableCommitmentConfig { #[derive(Drop, Copy)] struct TableDecommitment { // n_columns * n_queries values to decommit. - n_values: felt252, values: Span, } @@ -42,19 +43,11 @@ fn table_commit( TableCommitment { config: TableCommitmentConfig { n_columns: 0, - vector: VectorCommitmentConfig { - height: 0, - n_verifier_friendly_commitment_layers: 0, - } + vector: VectorCommitmentConfig { height: 0, n_verifier_friendly_commitment_layers: 0, } }, vector_commitment: VectorCommitment { - config: VectorCommitmentConfig { - height: 0, - n_verifier_friendly_commitment_layers: 0, - }, - commitment_hash: ChannelSentFelt { - value: 0, - } + config: VectorCommitmentConfig { height: 0, n_verifier_friendly_commitment_layers: 0, }, + commitment_hash: ChannelSentFelt { value: 0, } } } } @@ -69,10 +62,7 @@ fn table_commit( // witness - the decommitment witness. fn table_decommit( commitment: TableCommitment, - n_queries: felt252, - queries: felt252, + queries: Span, decommitment: TableDecommitment, witness: TableCommitmentWitness, -) { - -} \ No newline at end of file +) {}