Skip to content

Commit

Permalink
using optimized pow and moved felt252 operations to math
Browse files Browse the repository at this point in the history
  • Loading branch information
tiagofneto committed Dec 29, 2023
1 parent bf9bb31 commit e09fe34
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/air/autogenerated.cairo
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cairo_verifier::air::global_values::GlobalValues;
use cairo_verifier::common::felt252::{Felt252Div, pow};
use cairo_verifier::common::math::{Felt252Div, pow};

fn eval_composition_polynomial_inner(
mask_values: Array<felt252>,
Expand Down
2 changes: 1 addition & 1 deletion src/air/composition.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cairo_verifier::air::public_input::{PublicInput, PublicInputTrait};
use cairo_verifier::air::diluted::get_diluted_product;
use cairo_verifier::air::pedersen::{eval_pedersen_x, eval_pedersen_y};
use cairo_verifier::air::autogenerated::eval_composition_polynomial_inner;
use cairo_verifier::common::felt252::{Felt252Div, Felt252PartialOrd, pow};
use cairo_verifier::common::math::{Felt252Div, Felt252PartialOrd, pow};

const SHIFT_POINT_X: felt252 = 0x49ee3eba8c1600700ee1b87eb599f16716b0b1022947733551fde4050ca6804;
const SHIFT_POINT_Y: felt252 = 0x3ca0cfe4b3bc6ddf346d49d06ea0ed34e621062c0e056c1d0405d266e10268a;
Expand Down
2 changes: 1 addition & 1 deletion src/air/diluted.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cairo_verifier::common::felt252::pow;
use cairo_verifier::common::math::pow;

// The cumulative value is defined using the next recursive formula:
// r_1 = 1, r_{j+1} = r_j * (1 + z * u_j) + alpha * u_j^2
Expand Down
2 changes: 1 addition & 1 deletion src/air/public_input.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cairo_verifier::air::public_memory::{
Page, PageTrait, ContinuousPageHeader, get_continuous_pages_product
};
use cairo_verifier::common::felt252::{pow, Felt252PartialOrd, Felt252Div};
use cairo_verifier::common::math::{pow, Felt252PartialOrd, Felt252Div};

#[derive(Drop)]
struct SegmentInfo {
Expand Down
1 change: 0 additions & 1 deletion src/common.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod blake2s;
mod flip_endianness;
mod from_span;
mod horner_eval;
mod felt252;
mod array_append;
mod math;
mod array_print;
Expand Down
41 changes: 0 additions & 41 deletions src/common/felt252.cairo

This file was deleted.

34 changes: 34 additions & 0 deletions src/common/math.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ fn mul_inverse(x: felt252) -> felt252 {
// a ^ (p - 2) is the multiplicative inverse of a modulo p.
pow(x, STARK_PRIME_MINUS_TWO)
}

impl Felt252Div of Div<felt252> {
fn div(lhs: felt252, rhs: felt252) -> felt252 {
let lhs_u256: u256 = lhs.into();
let rhs_u256: u256 = rhs.into();
(lhs_u256 / rhs_u256).try_into().unwrap()
}
}

impl Felt252PartialOrd of PartialOrd<felt252> {
fn le(lhs: felt252, rhs: felt252) -> bool {
let lhs_u256: u256 = lhs.into();
let rhs_u256: u256 = rhs.into();
lhs <= rhs
}

fn ge(lhs: felt252, rhs: felt252) -> bool {
let lhs_u256: u256 = lhs.into();
let rhs_u256: u256 = rhs.into();
lhs >= rhs
}

fn lt(lhs: felt252, rhs: felt252) -> bool {
let lhs_u256: u256 = lhs.into();
let rhs_u256: u256 = rhs.into();
lhs < rhs
}

fn gt(lhs: felt252, rhs: felt252) -> bool {
let lhs_u256: u256 = lhs.into();
let rhs_u256: u256 = rhs.into();
lhs > rhs
}
}

0 comments on commit e09fe34

Please sign in to comment.