Skip to content

Commit

Permalink
code checked for correctness 1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Dec 21, 2023
1 parent cc7b5c5 commit 7d07c4c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
32 changes: 15 additions & 17 deletions src/fri/fri.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use core::traits::Into;
use core::option::OptionTrait;
use core::traits::TryInto;
use core::array::SpanTrait;
Expand Down Expand Up @@ -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<felt252>,
// Array of size n_values, containing the field elements that correspond to the query indices
Expand All @@ -61,7 +60,6 @@ struct FriWitness {
#[derive(Drop, Copy)]
struct FriLayerWitness {
// Values for the sibling leaves required for decommitment.
n_leaves: felt252,
leaves: Span<felt252>,
// Table commitment witnesses for decommiting all the leaves.
table_witness: TableCommitmentWitness,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -147,9 +147,8 @@ fn fri_decommit_layers(
layer_witness: Span<FriLayerWitness>,
eval_points: Span<felt252>,
step_sizes: Span<felt252>,
mut queries: Span<FriLayerQuery>,
mut queries: Array<FriLayerQuery>,
) -> Array<FriLayerQuery> {
let last_queries = ArrayTrait::<FriLayerQuery>::new();
let len: u32 = n_layers.try_into().unwrap();
let mut i: u32 = 0;

Expand All @@ -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(
Expand All @@ -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.
Expand All @@ -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(
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/fri/fri_group.cairo
Original file line number Diff line number Diff line change
@@ -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<felt252> {
array![
0x1,
Expand Down
1 change: 0 additions & 1 deletion src/fri/fri_last_layer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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<FriLayerQuery>, coefficients: Span<ChannelSentFelt>) {
let mut i: u32 = 0;
let len: u32 = queries.len();
Expand Down
13 changes: 8 additions & 5 deletions src/fri/fri_layer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn compute_coset_elements(
let mut coset_elements = ArrayTrait::<felt252>::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;

Expand All @@ -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)
Expand Down Expand Up @@ -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');

Expand All @@ -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 {
Expand Down

0 comments on commit 7d07c4c

Please sign in to comment.