Skip to content

Commit

Permalink
chore: remaining cleanup + make rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
petscheit committed Dec 18, 2024
1 parent 45ee928 commit 1901b49
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 744 deletions.
67 changes: 0 additions & 67 deletions hydra/garaga/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,73 +448,6 @@ def bit(value, index):
),
}

ISOGENY_MAP_G2 = {
BLS12_381_ID: {
"x_map_num": [
[
889424345604814976315064405719089812568196182208668418962679585805340366775741747653930584250892369786198727235542,
889424345604814976315064405719089812568196182208668418962679585805340366775741747653930584250892369786198727235542,
],
[
0,
2668273036814444928945193217157269437704588546626005256888038757416021100327225242961791752752677109358596181706522,
],
[
2668273036814444928945193217157269437704588546626005256888038757416021100327225242961791752752677109358596181706526,
1334136518407222464472596608578634718852294273313002628444019378708010550163612621480895876376338554679298090853261,
],
[
3557697382419259905260257622876359250272784728834673675850718343221361467102966990615722337003569479144794908942033,
0,
],
],
"x_map_den": [
[
0,
4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559715,
],
[
12,
4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559775,
],
[1, 0],
],
"y_map_num": [
[
3261222600550988246488569487636662646083386001431784202863158481286248011511053074731078808919938689216061999863558,
3261222600550988246488569487636662646083386001431784202863158481286248011511053074731078808919938689216061999863558,
],
[
0,
889424345604814976315064405719089812568196182208668418962679585805340366775741747653930584250892369786198727235518,
],
[
2668273036814444928945193217157269437704588546626005256888038757416021100327225242961791752752677109358596181706524,
1334136518407222464472596608578634718852294273313002628444019378708010550163612621480895876376338554679298090853263,
],
[
2816510427748580758331037284777117739799287910327449993381818688383577828123182200904113516794492504322962636245776,
0,
],
],
"y_map_den": [
[
4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559355,
4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559355,
],
[
0,
4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559571,
],
[
18,
4002409555221667393417789825735904156556882819939007885332058136124031650490837864442687629129015664037894272559769,
],
[1, 0],
],
},
}


def is_generator(g: int, p: int) -> bool:
"""
Expand Down
21 changes: 0 additions & 21 deletions hydra/garaga/modulo_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -864,27 +864,6 @@ def fp2_sub(self, X: list[ModuloCircuitElement], Y: list[ModuloCircuitElement]):
self.sub(X[1], Y[1], comment="Fp2 sub imag part"),
]

def fp2_inv(
self, element: list[ModuloCircuitElement]
) -> list[ModuloCircuitElement]:
assert len(element) == 2 and all(
isinstance(x, ModuloCircuitElement) for x in element
)
# For element a + bi, compute (a - bi)/(a² + b²)
a, b = element[0], element[1]
# Compute conjugate (a - bi)
conj = [a, self.neg(b)]
# Compute norm (a² + b²)
a_squared = self.mul(a, a)
b_squared = self.mul(b, b)
norm = self.add(a_squared, b_squared)

# Compute 1/norm
norm_inv = self.inv(norm)

# Multiply conjugate by inverse of norm
return [self.mul(conj[0], norm_inv), self.mul(conj[1], norm_inv)]

def fp2_parity(
self, element: list[ModuloCircuitElement]
) -> list[ModuloCircuitElement]:
Expand Down
36 changes: 18 additions & 18 deletions src/src/circuits/ec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ pub fn run_ADD_EC_POINTS_G2_circuit(p: G2Point, q: G2Point, curve_index: usize)
let t1 = circuit_sub(in4, in8); // Fp2 sub coeff 1/1
let t2 = circuit_sub(in1, in5); // Fp2 sub coeff 0/1
let t3 = circuit_sub(in2, in6); // Fp2 sub coeff 1/1
let t4 = circuit_sub(in0, t3);
let t5 = circuit_mul(t2, t2);
let t6 = circuit_mul(t3, t3);
let t7 = circuit_add(t5, t6);
let t8 = circuit_inverse(t7);
let t9 = circuit_mul(t2, t8);
let t10 = circuit_mul(t4, t8);
let t11 = circuit_mul(t0, t9); // Fp2 mul start
let t4 = circuit_mul(t2, t2); // Fp2 Inv start
let t5 = circuit_mul(t3, t3);
let t6 = circuit_add(t4, t5);
let t7 = circuit_inverse(t6);
let t8 = circuit_mul(t2, t7); // Fp2 Inv real part end
let t9 = circuit_mul(t3, t7);
let t10 = circuit_sub(in0, t9); // Fp2 Inv imag part end
let t11 = circuit_mul(t0, t8); // Fp2 mul start
let t12 = circuit_mul(t1, t10);
let t13 = circuit_sub(t11, t12); // Fp2 mul real part end
let t14 = circuit_mul(t0, t10);
let t15 = circuit_mul(t1, t9);
let t15 = circuit_mul(t1, t8);
let t16 = circuit_add(t14, t15); // Fp2 mul imag part end
let t17 = circuit_add(t13, t16);
let t18 = circuit_sub(t13, t16);
Expand Down Expand Up @@ -277,18 +277,18 @@ pub fn run_DOUBLE_EC_POINT_G2_A_EQ_0_circuit(p: G2Point, curve_index: usize) ->
let t6 = circuit_mul(t4, in0); // Fp2 scalar mul coeff 1/1
let t7 = circuit_add(in4, in4); // Fp2 add coeff 0/1
let t8 = circuit_add(in5, in5); // Fp2 add coeff 1/1
let t9 = circuit_sub(in1, t8);
let t10 = circuit_mul(t7, t7);
let t11 = circuit_mul(t8, t8);
let t12 = circuit_add(t10, t11);
let t13 = circuit_inverse(t12);
let t14 = circuit_mul(t7, t13);
let t15 = circuit_mul(t9, t13);
let t16 = circuit_mul(t5, t14); // Fp2 mul start
let t9 = circuit_mul(t7, t7); // Fp2 Inv start
let t10 = circuit_mul(t8, t8);
let t11 = circuit_add(t9, t10);
let t12 = circuit_inverse(t11);
let t13 = circuit_mul(t7, t12); // Fp2 Inv real part end
let t14 = circuit_mul(t8, t12);
let t15 = circuit_sub(in1, t14); // Fp2 Inv imag part end
let t16 = circuit_mul(t5, t13); // Fp2 mul start
let t17 = circuit_mul(t6, t15);
let t18 = circuit_sub(t16, t17); // Fp2 mul real part end
let t19 = circuit_mul(t5, t15);
let t20 = circuit_mul(t6, t14);
let t20 = circuit_mul(t6, t13);
let t21 = circuit_add(t19, t20); // Fp2 mul imag part end
let t22 = circuit_add(t18, t21);
let t23 = circuit_sub(t18, t21);
Expand Down
34 changes: 4 additions & 30 deletions src/src/circuits/extf_mul.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,7 @@ pub fn run_BLS12_381_EVAL_E12D_circuit(f: E12D<u384>, z: u384) -> (u384,) {
let t20 = circuit_mul(t19, in12); // Eval X Horner step: multiply by z
let t21 = circuit_add(in0, t20); // Eval X Horner step: add coefficient_0

let modulus = TryInto::<
_, CircuitModulus
>::try_into(
[
0xb153ffffb9feffffffffaaab,
0x6730d2a0f6b0f6241eabfffe,
0x434bacd764774b84f38512bf,
0x1a0111ea397fe69a4b1ba7b6
]
)
.unwrap(); // BLS12_381 prime field modulus
let modulus = get_BLS12_381_modulus(); // BLS12_381 prime field modulus

let mut circuit_inputs = (t21,).new_inputs();
// Prefill constants:
Expand Down Expand Up @@ -183,17 +173,7 @@ pub fn run_BLS12_381_FP12_MUL_ASSERT_ONE_circuit(
let t80 = circuit_sub(t78, t79); // (X(z) * Y(z)) - (Q(z) * P(z))
let t81 = circuit_sub(t80, in2); // (X(z) * Y(z) - Q(z) * P(z)) - 1

let modulus = TryInto::<
_, CircuitModulus
>::try_into(
[
0xb153ffffb9feffffffffaaab,
0x6730d2a0f6b0f6241eabfffe,
0x434bacd764774b84f38512bf,
0x1a0111ea397fe69a4b1ba7b6
]
)
.unwrap(); // BLS12_381 prime field modulus
let modulus = get_BLS12_381_modulus(); // BLS12_381 prime field modulus

let mut circuit_inputs = (t81,).new_inputs();
// Prefill constants:
Expand Down Expand Up @@ -279,10 +259,7 @@ pub fn run_BN254_EVAL_E12D_circuit(f: E12D<u288>, z: u384) -> (u384,) {
let t20 = circuit_mul(t19, in12); // Eval X Horner step: multiply by z
let t21 = circuit_add(in0, t20); // Eval X Horner step: add coefficient_0

let modulus = TryInto::<
_, CircuitModulus
>::try_into([0x6871ca8d3c208c16d87cfd47, 0xb85045b68181585d97816a91, 0x30644e72e131a029, 0x0])
.unwrap(); // BN254 prime field modulus
let modulus = get_BN254_modulus(); // BN254 prime field modulus

let mut circuit_inputs = (t21,).new_inputs();
// Prefill constants:
Expand Down Expand Up @@ -411,10 +388,7 @@ pub fn run_BN254_FP12_MUL_ASSERT_ONE_circuit(
let t80 = circuit_sub(t78, t79); // (X(z) * Y(z)) - (Q(z) * P(z))
let t81 = circuit_sub(t80, in2); // (X(z) * Y(z) - Q(z) * P(z)) - 1

let modulus = TryInto::<
_, CircuitModulus
>::try_into([0x6871ca8d3c208c16d87cfd47, 0xb85045b68181585d97816a91, 0x30644e72e131a029, 0x0])
.unwrap(); // BN254 prime field modulus
let modulus = get_BN254_modulus(); // BN254 prime field modulus

let mut circuit_inputs = (t81,).new_inputs();
// Prefill constants:
Expand Down
12 changes: 1 addition & 11 deletions src/src/circuits/isogeny.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -287,17 +287,7 @@ pub fn run_BLS12_381_APPLY_ISOGENY_BLS12_381_circuit(pt: G1Point) -> (G1Point,)
let t105 = circuit_mul(t73, t104);
let t106 = circuit_mul(t105, in55);

let modulus = TryInto::<
_, CircuitModulus
>::try_into(
[
0xb153ffffb9feffffffffaaab,
0x6730d2a0f6b0f6241eabfffe,
0x434bacd764774b84f38512bf,
0x1a0111ea397fe69a4b1ba7b6
]
)
.unwrap(); // BLS12_381 prime field modulus
let modulus = get_BLS12_381_modulus(); // BLS12_381 prime field modulus

let mut circuit_inputs = (t43, t106).new_inputs();
// Prefill constants:
Expand Down
Loading

0 comments on commit 1901b49

Please sign in to comment.