Skip to content

Commit

Permalink
Fix genericity
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Nov 8, 2024
1 parent 848af0e commit 7b1b916
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 11 additions & 0 deletions src/src/definitions.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,17 @@ fn get_min_one(curve_index: usize) -> u384 {
return u384 { limb0: 0, limb1: 0, limb2: 0, limb3: 0 };
}


fn get_modulus(curve_index: usize) -> CircuitModulus {
match curve_index {
0 => get_BN254_modulus(),
1 => get_BLS12_381_modulus(),
2 => get_SECP256K1_modulus(),
3 => get_SECP256R1_modulus(),
4 => get_ED25519_modulus(),
_ => panic_with_felt252('Invalid curve index'),
}
}
// Returns the modulus of BLS12_381
#[inline(always)]
fn get_BLS12_381_modulus() -> CircuitModulus {
Expand Down
16 changes: 9 additions & 7 deletions src/src/ec_ops_g2.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use garaga::circuits::tower_circuits::{run_BLS12_381_FP2_MUL_circuit, run_BN254_

use core::option::Option;
use garaga::core::circuit::AddInputResultTrait2;
use garaga::definitions::{G2Point, G2PointZero, get_BLS12_381_modulus, get_b2, get_a, get_p};
use garaga::definitions::{
G2Point, G2PointZero, get_BLS12_381_modulus, get_b2, get_a, get_p, get_modulus
};
use garaga::circuits::ec;
use garaga::utils::u384_assert_zero;
use garaga::basic_field_ops::neg_mod_p;
Expand Down Expand Up @@ -254,10 +256,10 @@ fn ec_safe_add_with_options(
fn ec_safe_add(P: G2Point, Q: G2Point, curve_index: usize) -> Option<G2Point> {
// assumes that the points are on the curve and not the point at infinity.
// Returns None if the points are the same and opposite y coordinates (Point at infinity)
let same_x = eq_mod_p(P.x0, P.x1, Q.x0, Q.x1);
let same_x = eq_mod_p(P.x0, P.x1, Q.x0, Q.x1, curve_index);

if same_x {
let opposite_y = eq_neg_mod_p(P.y0, P.y1, Q.y0, Q.y1);
let opposite_y = eq_neg_mod_p(P.y0, P.y1, Q.y0, Q.y1, curve_index);

if opposite_y {
return Option::None;
Expand Down Expand Up @@ -293,15 +295,15 @@ fn ec_mul_inner(pt: G2Point, mut bits: Array<felt252>, curve_index: usize) -> Op

// returns true if a == b mod p bls12-381
#[inline]
pub fn eq_mod_p(a0: u384, a1: u384, b0: u384, b1: u384) -> bool {
pub fn eq_mod_p(a0: u384, a1: u384, b0: u384, b1: u384, curve_index: usize) -> bool {
let _a0 = CE::<CI<0>> {};
let _a1 = CE::<CI<1>> {};
let _b0 = CE::<CI<2>> {};
let _b1 = CE::<CI<3>> {};
let sub0 = circuit_sub(_a0, _b0);
let sub1 = circuit_sub(_a1, _b1);

let modulus = get_BLS12_381_modulus();
let modulus = get_modulus(curve_index);

let outputs = (sub0, sub1)
.new_inputs()
Expand All @@ -318,15 +320,15 @@ pub fn eq_mod_p(a0: u384, a1: u384, b0: u384, b1: u384) -> bool {

// returns true if a == -b mod p bls12-381
#[inline]
pub fn eq_neg_mod_p(a0: u384, a1: u384, b0: u384, b1: u384) -> bool {
pub fn eq_neg_mod_p(a0: u384, a1: u384, b0: u384, b1: u384, curve_index: usize) -> bool {
let _a0 = CE::<CI<0>> {};
let _a1 = CE::<CI<1>> {};
let _b0 = CE::<CI<2>> {};
let _b1 = CE::<CI<3>> {};
let check0 = circuit_add(_a0, _b0);
let check1 = circuit_add(_a1, _b1);

let modulus = get_BLS12_381_modulus();
let modulus = get_modulus(curve_index);
let outputs = (check0, check1)
.new_inputs()
.next_2(a0)
Expand Down

0 comments on commit 7b1b916

Please sign in to comment.