diff --git a/src/fri/fri.cairo b/src/fri/fri.cairo index 2eb1d66ac..e55ef361c 100644 --- a/src/fri/fri.cairo +++ b/src/fri/fri.cairo @@ -1,3 +1,4 @@ +use core::traits::Into; use core::option::OptionTrait; use core::traits::TryInto; use core::array::SpanTrait; @@ -40,8 +41,6 @@ struct FriCommitment { #[derive(Drop, Copy)] struct FriDecommitment { - // Number of queries. - n_values: felt252, // Array of size n_values, containing the values of the input layer at query indices. values: Span, // Array of size n_values, containing the field elements that correspond to the query indices @@ -61,7 +60,6 @@ struct FriWitness { #[derive(Drop, Copy)] struct FriLayerWitness { // Values for the sibling leaves required for decommitment. - n_leaves: felt252, leaves: Span, // Table commitment witnesses for decommiting all the leaves. table_witness: TableCommitmentWitness, @@ -104,8 +102,9 @@ fn fri_commit_rounds( if i == len { break; } - - commitments.append(table_commit(*(unsent_commitments.at(i)), *(configs.at(i)))); + // Read commitments. + commitments.append(table_commit(*unsent_commitments.at(i), *configs.at(i))); + // Send the next eval_points. eval_points.append(channel.random_felt_to_prover()); i += 1; @@ -131,6 +130,7 @@ fn fri_commit( let n_coefficients = math::pow(2, config.log_last_layer_degree_bound); let coefficients = channel .read_felt_vector_from_prover(unsent_commitment.last_layer_coefficients); + assert(n_coefficients == coefficients.len().into(), 'Invalid value'); FriCommitment { config: config, @@ -147,9 +147,8 @@ fn fri_decommit_layers( layer_witness: Span, eval_points: Span, step_sizes: Span, - mut queries: Span, + mut queries: Array, ) -> Array { - let last_queries = ArrayTrait::::new(); let len: u32 = n_layers.try_into().unwrap(); let mut i: u32 = 0; @@ -159,16 +158,15 @@ fn fri_decommit_layers( } // Params. - let coset_size = math::pow(2, *(step_sizes.at(i))); + 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)) + coset_size, 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.span(), *layer_witness.at(i).leaves, params ); - queries = next_queries.span(); // Table decommitment. table_decommit( @@ -178,10 +176,11 @@ fn fri_decommit_layers( *layer_witness.at(i).table_witness ); + queries = next_queries; i += 1; }; - last_queries + queries } // FRI protocol component decommitment. @@ -191,7 +190,7 @@ fn fri_decommit( decommitment: FriDecommitment, witness: FriWitness, ) { - assert(queries.len().into() == decommitment.n_values, 'Invalid value'); + assert(queries.len() == decommitment.values.len(), 'Invalid value'); // Compute first FRI layer queries. let fri_queries = gather_first_layer_queries( @@ -209,16 +208,15 @@ fn fri_decommit( 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(), + fri_queries, ); // Last layer. assert( commitment .last_layer_coefficients - .len() == math::pow(2, commitment.config.log_last_layer_degree_bound) - .try_into() - .unwrap(), + .len() + .into() == math::pow(2, commitment.config.log_last_layer_degree_bound), 'Invlid value' ); verify_last_layer(last_queries.span(), commitment.last_layer_coefficients); diff --git a/src/fri/fri_group.cairo b/src/fri/fri_group.cairo index c4a3133da..0cbfd8bc3 100644 --- a/src/fri/fri_group.cairo +++ b/src/fri/fri_group.cairo @@ -1,3 +1,5 @@ +// Returns the elements of the multiplicative subgroup of order 16, in bit-reversed order for the +// cairo prime field. Note that the first 2^k elements correspond to the group of size 2^k. fn get_fri_group() -> Array { array![ 0x1, diff --git a/src/fri/fri_last_layer.cairo b/src/fri/fri_last_layer.cairo index 133e0c883..7d7e768c3 100644 --- a/src/fri/fri_last_layer.cairo +++ b/src/fri/fri_last_layer.cairo @@ -8,7 +8,6 @@ use cairo_verifier::channel::channel::ChannelSentFelt; // Verifies FRI last layer by evaluating the given polynomial on the given points (=inverses of // x_inv_values), and comparing the results to the given values. - fn verify_last_layer(queries: Span, coefficients: Span) { let mut i: u32 = 0; let len: u32 = queries.len(); diff --git a/src/fri/fri_layer.cairo b/src/fri/fri_layer.cairo index 37cce1f08..a4fc946db 100644 --- a/src/fri/fri_layer.cairo +++ b/src/fri/fri_layer.cairo @@ -44,7 +44,7 @@ fn compute_coset_elements( let mut coset_elements = ArrayTrait::::new(); let mut coset_x_inv: felt252 = 0; - let len = queries.len(); + let i_len = queries.len(); let mut i: u32 = 0; let mut j: u32 = 0; @@ -53,14 +53,16 @@ fn compute_coset_elements( break; } - 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))); + if i != 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 + j)); i += 1; } else { coset_elements.append(*(sibling_witness.at(j))); j += 1; } + + offset_within_coset += 1; }; (coset_elements, coset_x_inv) @@ -104,6 +106,7 @@ fn compute_next_layer( queries, sibling_witness, coset_size, coset_index * coset_size, 0, params.fri_group ); + // Verify that at least one query was consumed. let coset_elements_len = coset_elements.len(); assert(0 <= coset_elements_len, 'Invalid value'); @@ -121,8 +124,8 @@ fn compute_next_layer( coset_elements_span, params.eval_point, coset_x_inv, coset_size, ); + // Write next layer query. let next_x_inv = math::pow(coset_x_inv, params.coset_size); - next_queries .append( FriLayerQuery {