Skip to content

Commit

Permalink
Full Rewrite WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
feltroidprime committed Feb 5, 2024
1 parent 861b155 commit 58f56ca
Show file tree
Hide file tree
Showing 64 changed files with 2,107 additions and 6,995 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ clean:
mkdir -p build
mkdir build/compiled_cairo_files

cython:
./tools/make/cython.sh

go:
./tools/make/go.sh

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
171 changes: 6 additions & 165 deletions src/bn254/pairing.cairo → archive_tmp/bn254/pairing.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ from src.bn254.towers.e6 import (
get_powers_of_z5,
eval_E6_plus_v_unreduced,
eval_E5,
eval_unreduced_poly6,
eval_irreducible_poly6,
)
from src.bn254.fq import (
BigInt3,
Expand All @@ -61,6 +61,9 @@ from src.bn254.fq import (
from starkware.cairo.common.cairo_builtins import PoseidonBuiltin, BitwiseBuiltin
from starkware.cairo.common.builtin_poseidon.poseidon import poseidon_hash

from src.extension_field_tricks.fp12 import verify_12th_extension_tricks
from src.extension_field_tricks.fp6 import verify_6th_extension_tricks

const ate_loop_count = 29793968203157093288;
const log_ate_loop_count = 63;
const naf_count = 66;
Expand Down Expand Up @@ -205,130 +208,7 @@ func multi_miller_loop{
assert Z.d1 - z_pow1_11_ptr.z_1.d1 = 0;
assert Z.d2 - z_pow1_11_ptr.z_1.d2 = 0;
%{ print("Verifying Σc_i*A_i(z)*B_i(z) == P(z)Σc_i*Q_i(z) + Σc_i*R_i(z)") %}
let z_12 = fq_bigint3.mul(z_pow1_11_ptr.z_1, z_pow1_11_ptr.z_11);
let p_of_z = eval_irreducible_poly12(z_pow1_11_ptr.z_6, z_12);
let sum_r_of_z = eval_E12_unreduced(
E12DU(
UnreducedBigInt3(
poly_acc_12.r.w0.d0 + poly_acc_034.r.w0.d0 + poly_acc_034034.r.w0.d0,
poly_acc_12.r.w0.d1 + poly_acc_034.r.w0.d1 + poly_acc_034034.r.w0.d1,
poly_acc_12.r.w0.d2 + poly_acc_034.r.w0.d2 + poly_acc_034034.r.w0.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w1.d0 + poly_acc_034.r.w1.d0 + poly_acc_034034.r.w1.d0,
poly_acc_12.r.w1.d1 + poly_acc_034.r.w1.d1 + poly_acc_034034.r.w1.d1,
poly_acc_12.r.w1.d2 + poly_acc_034.r.w1.d2 + poly_acc_034034.r.w1.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w2.d0 + poly_acc_034.r.w2.d0 + poly_acc_034034.r.w2.d0,
poly_acc_12.r.w2.d1 + poly_acc_034.r.w2.d1 + poly_acc_034034.r.w2.d1,
poly_acc_12.r.w2.d2 + poly_acc_034.r.w2.d2 + poly_acc_034034.r.w2.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w3.d0 + poly_acc_034.r.w3.d0 + poly_acc_034034.r.w3.d0,
poly_acc_12.r.w3.d1 + poly_acc_034.r.w3.d1 + poly_acc_034034.r.w3.d1,
poly_acc_12.r.w3.d2 + poly_acc_034.r.w3.d2 + poly_acc_034034.r.w3.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w4.d0 + poly_acc_034.r.w4.d0 + poly_acc_034034.r.w4.d0,
poly_acc_12.r.w4.d1 + poly_acc_034.r.w4.d1 + poly_acc_034034.r.w4.d1,
poly_acc_12.r.w4.d2 + poly_acc_034.r.w4.d2 + poly_acc_034034.r.w4.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w5.d0 + poly_acc_034.r.w5.d0,
poly_acc_12.r.w5.d1 + poly_acc_034.r.w5.d1,
poly_acc_12.r.w5.d2 + poly_acc_034.r.w5.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w6.d0 + poly_acc_034.r.w6.d0 + poly_acc_034034.r.w6.d0,
poly_acc_12.r.w6.d1 + poly_acc_034.r.w6.d1 + poly_acc_034034.r.w6.d1,
poly_acc_12.r.w6.d2 + poly_acc_034.r.w6.d2 + poly_acc_034034.r.w6.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w7.d0 + poly_acc_034.r.w7.d0 + poly_acc_034034.r.w7.d0,
poly_acc_12.r.w7.d1 + poly_acc_034.r.w7.d1 + poly_acc_034034.r.w7.d1,
poly_acc_12.r.w7.d2 + poly_acc_034.r.w7.d2 + poly_acc_034034.r.w7.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w8.d0 + poly_acc_034.r.w8.d0 + poly_acc_034034.r.w8.d0,
poly_acc_12.r.w8.d1 + poly_acc_034.r.w8.d1 + poly_acc_034034.r.w8.d1,
poly_acc_12.r.w8.d2 + poly_acc_034.r.w8.d2 + poly_acc_034034.r.w8.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w9.d0 + poly_acc_034.r.w9.d0 + poly_acc_034034.r.w9.d0,
poly_acc_12.r.w9.d1 + poly_acc_034.r.w9.d1 + poly_acc_034034.r.w9.d1,
poly_acc_12.r.w9.d2 + poly_acc_034.r.w9.d2 + poly_acc_034034.r.w9.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w10.d0 + poly_acc_034.r.w10.d0 + poly_acc_034034.r.w10.d0,
poly_acc_12.r.w10.d1 + poly_acc_034.r.w10.d1 + poly_acc_034034.r.w10.d1,
poly_acc_12.r.w10.d2 + poly_acc_034.r.w10.d2 + poly_acc_034034.r.w10.d2,
),
UnreducedBigInt3(
poly_acc_12.r.w11.d0 + poly_acc_034.r.w11.d0 + poly_acc_034034.r.w11.d0,
poly_acc_12.r.w11.d1 + poly_acc_034.r.w11.d1 + poly_acc_034034.r.w11.d1,
poly_acc_12.r.w11.d2 + poly_acc_034.r.w11.d2 + poly_acc_034034.r.w11.d2,
),
),
z_pow1_11_ptr,
);
let sum_q_of_z = eval_E11(
E11DU(
Uint256(
poly_acc_12.q.w0.low + poly_acc_034.q.w0.low + poly_acc_034034.q.w0.low,
poly_acc_12.q.w0.high + poly_acc_034.q.w0.high + poly_acc_034034.q.w0.high,
),
Uint256(
poly_acc_12.q.w1.low + poly_acc_034.q.w1.low + poly_acc_034034.q.w1.low,
poly_acc_12.q.w1.high + poly_acc_034.q.w1.high + poly_acc_034034.q.w1.high,
),
Uint256(
poly_acc_12.q.w2.low + poly_acc_034.q.w2.low + poly_acc_034034.q.w2.low,
poly_acc_12.q.w2.high + poly_acc_034.q.w2.high + poly_acc_034034.q.w2.high,
),
Uint256(
poly_acc_12.q.w3.low + poly_acc_034.q.w3.low + poly_acc_034034.q.w3.low,
poly_acc_12.q.w3.high + poly_acc_034.q.w3.high + poly_acc_034034.q.w3.high,
),
Uint256(
poly_acc_12.q.w4.low + poly_acc_034.q.w4.low + poly_acc_034034.q.w4.low,
poly_acc_12.q.w4.high + poly_acc_034.q.w4.high + poly_acc_034034.q.w4.high,
),
Uint256(
poly_acc_12.q.w5.low + poly_acc_034.q.w5.low + poly_acc_034034.q.w5.low,
poly_acc_12.q.w5.high + poly_acc_034.q.w5.high + poly_acc_034034.q.w5.high,
),
Uint256(
poly_acc_12.q.w6.low + poly_acc_034.q.w6.low + poly_acc_034034.q.w6.low,
poly_acc_12.q.w6.high + poly_acc_034.q.w6.high + poly_acc_034034.q.w6.high,
),
Uint256(
poly_acc_12.q.w7.low + poly_acc_034.q.w7.low,
poly_acc_12.q.w7.high + poly_acc_034.q.w7.high,
),
Uint256(
poly_acc_12.q.w8.low + poly_acc_034.q.w8.low,
poly_acc_12.q.w8.high + poly_acc_034.q.w8.high,
),
poly_acc_12.q.w9,
poly_acc_12.q.w10,
),
z_pow1_11_ptr,
);
let (sum_qP_of_z) = bigint_mul(sum_q_of_z, p_of_z);

verify_zero5(
UnreducedBigInt5(
d0=poly_acc_12.xy.d0 + poly_acc_034.xy.d0 + poly_acc_034034.xy.d0 - sum_qP_of_z.d0 -
sum_r_of_z.d0,
d1=poly_acc_12.xy.d1 + poly_acc_034.xy.d1 + poly_acc_034034.xy.d1 - sum_qP_of_z.d1 -
sum_r_of_z.d1,
d2=poly_acc_12.xy.d2 + poly_acc_034.xy.d2 + poly_acc_034034.xy.d2 - sum_qP_of_z.d2 -
sum_r_of_z.d2,
d3=-sum_qP_of_z.d3 - sum_r_of_z.d3,
d4=-sum_qP_of_z.d4 - sum_r_of_z.d4,
),
);
verify_12th_extension_tricks();
}
%{ print("Ok! \n") %}

Expand Down Expand Up @@ -907,46 +787,7 @@ func final_exponentiation{
assert Z.d1 - z_pow1_5_ptr.z_1.d1 = 0;
assert Z.d2 - z_pow1_5_ptr.z_1.d2 = 0;
%{ print(f"Verifying Σc_i*A_i(z)*B_i(z) == P(z)Σc_i*Q_i(z) + Σc_i*R_i(z)") %}

let sum_r_of_z = eval_E6_plus_v_unreduced(poly_acc.r, poly_acc_sq.r, z_pow1_5_ptr);
let sum_q_of_z = eval_E5(
E5full(
Uint256(
poly_acc.q.v0.low + poly_acc_sq.q.v0.low,
poly_acc.q.v0.high + poly_acc_sq.q.v0.high,
),
Uint256(
poly_acc.q.v1.low + poly_acc_sq.q.v1.low,
poly_acc.q.v1.high + poly_acc_sq.q.v1.high,
),
Uint256(
poly_acc.q.v2.low + poly_acc_sq.q.v2.low,
poly_acc.q.v2.high + poly_acc_sq.q.v2.high,
),
Uint256(
poly_acc.q.v3.low + poly_acc_sq.q.v3.low,
poly_acc.q.v3.high + poly_acc_sq.q.v3.high,
),
Uint256(
poly_acc.q.v4.low + poly_acc_sq.q.v4.low,
poly_acc.q.v4.high + poly_acc_sq.q.v4.high,
),
),
z_pow1_5_ptr,
);
let z_6 = fq_bigint3.mul(z_pow1_5_ptr.z_1, z_pow1_5_ptr.z_5);
let p_of_z: BigInt3 = eval_unreduced_poly6(z_pow1_5_ptr.z_3, z_6);
let (sum_qP_of_z) = bigint_mul(sum_q_of_z, p_of_z);

verify_zero5(
UnreducedBigInt5(
d0=poly_acc.xy.d0 + poly_acc_sq.xy.d0 - sum_qP_of_z.d0 - sum_r_of_z.d0,
d1=poly_acc.xy.d1 + poly_acc_sq.xy.d1 - sum_qP_of_z.d1 - sum_r_of_z.d1,
d2=poly_acc.xy.d2 + poly_acc_sq.xy.d2 - sum_qP_of_z.d2 - sum_r_of_z.d2,
d3=-sum_qP_of_z.d3 - sum_r_of_z.d3,
d4=-sum_qP_of_z.d4 - sum_r_of_z.d4,
),
);
verify_6th_extension_tricks();
%{ print(f"Ok!") %}

return final_res;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from tools.py.polynomial import Polynomial
from tools.py.field import BaseFieldElement, BaseField
from tools.py.extension_trick import (
from algebra import Polynomial
from algebra import FieldElement, BaseField
from tools.extension_trick import (
gnark_to_v,
gnark_to_v_bigint3,
flatten,
Expand All @@ -27,10 +27,10 @@


coeffs = [
BaseFieldElement(82, field),
FieldElement(82, field),
field.zero(),
field.zero(),
BaseFieldElement(-18 % p, field),
FieldElement(-18 % p, field),
field.zero(),
field.zero(),
field.one(),
Expand All @@ -53,10 +53,8 @@ def split(x):
return coeffs[::-1]




def to_fp6(x: list) -> Polynomial:
return Polynomial([BaseFieldElement(xi, field) for xi in x])
return Polynomial([FieldElement(xi, field) for xi in x])


def mul_torus(
Expand Down Expand Up @@ -399,7 +397,7 @@ def final_exponentiation(
if is_zero:
t0t = Polynomial(
[
BaseFieldElement(1, field),
FieldElement(1, field),
field.zero(),
field.zero(),
field.zero(),
Expand Down
Loading

0 comments on commit 58f56ca

Please sign in to comment.