Skip to content

Commit

Permalink
fri_decommit developed -> audit
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Dec 21, 2023
1 parent 0d2d9ec commit d4a870a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
6 changes: 4 additions & 2 deletions src/common/horner_eval.cairo
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use cairo_verifier::channel::channel::ChannelSentFelt;

// `horner_eval` is a function that evaluates a polynomial at a given point using Horner's method.
// `coefs` is an array of coefficients representing the polynomial in the format a0, a1, a2, ... an.
// `point` is the value at which the polynomial will be evaluated.
// The function returns the polynomial evaluation as `felt252`.

fn horner_eval(coefs: Span<felt252>, point: felt252) -> felt252 {
fn horner_eval(coefs: Span<ChannelSentFelt>, point: felt252) -> felt252 {
let mut res = 0;
let mut i = coefs.len();
loop {
if i != 0 {
i -= 1;
res = *coefs.at(i) + point * res;
res = (*(coefs.at(i))).value + point * res;
} else {
break;
}
Expand Down
49 changes: 25 additions & 24 deletions src/common/tests/test_horner_eval.cairo
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
use core::array::ArrayTrait;
use cairo_verifier::channel::channel::ChannelSentFelt;
use cairo_verifier::common::horner_eval::horner_eval;

#[test]
#[available_gas(9999999999)]
fn test_horner_eval_0() {
let mut coefs = ArrayTrait::<felt252>::new();
let mut coefs = ArrayTrait::<ChannelSentFelt>::new();
let eval = horner_eval(coefs.span(), 1);
assert(eval == 0, 'invalid evaluation result');
}

#[test]
#[available_gas(9999999999)]
fn test_horner_eval_1() {
let mut coefs = ArrayTrait::<felt252>::new();
coefs.append(1);
let mut coefs = ArrayTrait::<ChannelSentFelt>::new();
coefs.append(ChannelSentFelt { value: 1 });
let eval = horner_eval(coefs.span(), 7);
assert(eval == 1, 'invalid evaluation result');
}

#[test]
#[available_gas(9999999999)]
fn test_horner_eval_2() {
let mut coefs = ArrayTrait::<felt252>::new();
coefs.append(4);
coefs.append(10);
coefs.append(19);
coefs.append(1);
coefs.append(9);
let mut coefs = ArrayTrait::<ChannelSentFelt>::new();
coefs.append(ChannelSentFelt { value: 4 });
coefs.append(ChannelSentFelt { value: 10 });
coefs.append(ChannelSentFelt { value: 19 });
coefs.append(ChannelSentFelt { value: 1 });
coefs.append(ChannelSentFelt { value: 9 });
let eval = horner_eval(coefs.span(), 13);
assert(eval == 262591, 'invalid evaluation result');
}

#[test]
#[available_gas(9999999999)]
fn test_horner_eval_3() {
let mut coefs = ArrayTrait::<felt252>::new();
coefs.append(4);
coefs.append(10);
coefs.append(19);
coefs.append(1);
coefs.append(9);
coefs.append(99);
coefs.append(1);
coefs.append(7);
coefs.append(13);
coefs.append(2);
coefs.append(5);
coefs.append(7);
coefs.append(111);
coefs.append(1);
let mut coefs = ArrayTrait::<ChannelSentFelt>::new();
coefs.append(ChannelSentFelt { value: 4 });
coefs.append(ChannelSentFelt { value: 10 });
coefs.append(ChannelSentFelt { value: 19 });
coefs.append(ChannelSentFelt { value: 1 });
coefs.append(ChannelSentFelt { value: 9 });
coefs.append(ChannelSentFelt { value: 99 });
coefs.append(ChannelSentFelt { value: 1 });
coefs.append(ChannelSentFelt { value: 7 });
coefs.append(ChannelSentFelt { value: 13 });
coefs.append(ChannelSentFelt { value: 2 });
coefs.append(ChannelSentFelt { value: 5 });
coefs.append(ChannelSentFelt { value: 7 });
coefs.append(ChannelSentFelt { value: 111 });
coefs.append(ChannelSentFelt { value: 1 });
let eval = horner_eval(coefs.span(), 19);
assert(eval == 288577899334361215, 'invalid evaluation result');
}
16 changes: 15 additions & 1 deletion src/fri/fri.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ 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, FriLayerComputationParams, compute_next_layer};
use cairo_verifier::fri::fri_last_layer::verify_last_layer;

// Commitment values for FRI. Used to generate a commitment by "reading" these values
// from the channel.
Expand Down Expand Up @@ -194,14 +195,16 @@ fn fri_decommit(
witness: FriWitness,
) {
assert(queries.len().into() == decommitment.n_values, 'Invalid value');
let fri_first_layer_evaluations = decommitment.values;

// Compute first FRI layer queries.
let fri_queries = gather_first_layer_queries(
queries, decommitment.values, decommitment.points,
);

// Compute fri_group.
let fri_group = get_fri_group();

// Decommit inner layers.
let last_queries = fri_decommit_layers(
fri_group.span(),
commitment.config.n_layers - 1,
Expand All @@ -211,4 +214,15 @@ fn fri_decommit(
commitment.config.fri_step_sizes.slice(1, commitment.config.fri_step_sizes.len() - 1),
fri_queries.span(),
);

// Last layer.
assert(
commitment
.last_layer_coefficients
.len() == math::pow(2, commitment.config.log_last_layer_degree_bound)
.try_into()
.unwrap(),
'Invlid value'
);
verify_last_layer(last_queries.span(), commitment.last_layer_coefficients);
}
9 changes: 5 additions & 4 deletions src/fri/fri_last_layer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ use core::traits::TryInto;
use cairo_verifier::common::horner_eval;
use cairo_verifier::common::math;
use cairo_verifier::fri::fri_layer::FriLayerQuery;
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(
n_queries: felt252, queries: Span<FriLayerQuery>, coefficients: Span<felt252>
) {

fn verify_last_layer(queries: Span<FriLayerQuery>, coefficients: Span<ChannelSentFelt>) {
let mut i: u32 = 0;
let len: u32 = n_queries.try_into().unwrap();
let len: u32 = queries.len();
loop {
if i == len {
break;
Expand Down

0 comments on commit d4a870a

Please sign in to comment.