Skip to content

Commit

Permalink
verify_last_layer
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Dec 19, 2023
1 parent a3d6ddd commit 9fcec24
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/common/horner_eval.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// `point` is the value at which the polynomial will be evaluated.
// The function returns the polynomial evaluation as `felt252`.

fn horner_eval(coefs: Array<felt252>, point: felt252) -> felt252 {
fn horner_eval(coefs: Span<felt252>, point: felt252) -> felt252 {
let mut res = 0;
let mut i = coefs.len();
loop {
Expand Down
7 changes: 6 additions & 1 deletion src/common/math.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ fn pow(base: felt252, exp: felt252) -> felt252 {
exp = exp / 2; // Divide exponent by 2
}
};

res
}

fn mul_inverse(x: felt252) -> felt252 {
// From Fermat's little theorem, a ^ (p - 1) = 1 when p is prime and a != 0. Since a ^ (p - 1) = a · a ^ (p - 2) we have that
// a ^ (p - 2) is the multiplicative inverse of a modulo p.
pow(x, 3618502788666131213697322783095070105623107215331596699973092056135872020479)
}
9 changes: 5 additions & 4 deletions src/common/tests/test_horner_eval.cairo
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use core::array::ArrayTrait;
use cairo_verifier::common::horner_eval::horner_eval;

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

Expand All @@ -13,7 +14,7 @@ fn test_horner_eval_0() {
fn test_horner_eval_1() {
let mut coefs = ArrayTrait::<felt252>::new();
coefs.append(1);
let eval = horner_eval(coefs, 7);
let eval = horner_eval(coefs.span(), 7);
assert(eval == 1, 'invalid evaluation result');
}

Expand All @@ -26,7 +27,7 @@ fn test_horner_eval_2() {
coefs.append(19);
coefs.append(1);
coefs.append(9);
let eval = horner_eval(coefs, 13);
let eval = horner_eval(coefs.span(), 13);
assert(eval == 262591, 'invalid evaluation result');
}

Expand All @@ -48,6 +49,6 @@ fn test_horner_eval_3() {
coefs.append(7);
coefs.append(111);
coefs.append(1);
let eval = horner_eval(coefs, 19);
let eval = horner_eval(coefs.span(), 19);
assert(eval == 288577899334361215, 'invalid evaluation result');
}
42 changes: 41 additions & 1 deletion src/common/tests/test_math.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cairo_verifier::common::math::pow;
use cairo_verifier::common::math::{pow, mul_inverse};

#[test]
#[available_gas(9999999999)]
Expand All @@ -25,3 +25,43 @@ fn test_pow_2() {
'Invalid value'
);
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_1() {
let x = 9751091999414713;
let inv_x = mul_inverse(x);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_2() {
let x = 97199414713;
let inv_x = mul_inverse(x);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_3() {
let x = 92011457780;
let inv_x = mul_inverse(x);
assert(x * inv_x == 1, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_4() {
let x = 193456804421077096570009938751278224656090409051406060084;
let inv_inv_x = mul_inverse(mul_inverse(x));
assert(x == inv_inv_x, 'Invalid value');
}

#[test]
#[available_gas(9999999999)]
fn test_mul_inverse_5() {
let x = 19345680409051406060084;
let inv_inv_x = mul_inverse(mul_inverse(x));
assert(x == inv_inv_x, 'Invalid value');
}
2 changes: 2 additions & 0 deletions src/fri.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ mod fri_formula;
mod fri_group;
mod fri_layer;
mod fri_config;
mod fri_last_layer;
mod fri;

#[cfg(test)]
mod tests;
Empty file added src/fri/fri.cairo
Empty file.
26 changes: 26 additions & 0 deletions src/fri/fri_last_layer.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use core::option::OptionTrait;
use core::traits::TryInto;

use cairo_verifier::common::horner_eval;
use cairo_verifier::common::math;
use cairo_verifier::fri::fri_layer::FriLayerQuery;
// 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>
) {
let mut i: u32 = 0;
let len: u32 = n_queries.try_into().unwrap();
loop {
if i == len {
break;
}
let value = horner_eval::horner_eval(
coefficients, math::mul_inverse(*(queries.at(i)).x_inv_value)
);
assert(value == *(queries.at(i)).y_value, '');
i += 1;
}
}

0 comments on commit 9fcec24

Please sign in to comment.