From 3b1f3069de85a7d4ba7519d75daa0d3f9d021770 Mon Sep 17 00:00:00 2001 From: zebra-lucky Date: Thu, 25 Jul 2019 02:21:48 +0300 Subject: [PATCH] rename tuples operations --- bls_py/ec.py | 128 +++++++++--------- bls_py/fields.py | 207 +++++++++++++---------------- bls_py/fields_t.py | 320 +++++++++++++++++++++------------------------ bls_py/pairing.py | 12 +- 4 files changed, 313 insertions(+), 354 deletions(-) diff --git a/bls_py/ec.py b/bls_py/ec.py index e9561e7..7c7e722 100644 --- a/bls_py/ec.py +++ b/bls_py/ec.py @@ -3,12 +3,9 @@ from . import bls12381 from .fields import (FieldExtBase, Fq, Fq2, Fq6, Fq12, - fq2_t_add_fq_int, fq2_t_sub_fq_int, - fq2_t_mul_fq2_t, fq2_t_mul_fq_int, fq_int_sub_fq2_t, - fq2_t_add_fq2_t, fq2_t_sub_fq2_t, fq6_t_mul_fq6_t, - fq6_t_mul_fq2_t, fq6_t_mul_fq_int, fq6_t_add_fq6_t, - fq6_t_sub_fq6_t, fq12_t_mul_fq12_t, fq12_t_mul_fq2_t, - fq12_t_mul_fq_int, fq12_t_add_fq12_t, fq12_t_sub_fq12_t) + fq2_add_fq, fq2_sub_fq, fq2_mul_fq2, fq2_mul_fq, + fq_sub_fq2, fq2_add_fq2, fq2_sub_fq2, fq12_mul_fq12, + fq12_mul_fq2, fq12_mul_fq, fq12_add_fq12, fq12_sub_fq12) from .util import hash256, hash512 @@ -263,97 +260,96 @@ def double_point_jacobian(p1, ec=default_ec, FE=Fq): FE.zero(ec.q), True, ec) Q = ec.q if FE == Fq2: - xr, yr, zr = double_point_jacobian_fq2_t(X.ZT, Y.ZT, Z.ZT, ec) + xr, yr, zr = double_point_jacobian_fq2(X.ZT, Y.ZT, Z.ZT, ec) return JacobianPoint(Fq2(Q, xr), Fq2(Q, yr), Fq2(Q, zr), False, ec) elif FE == Fq and ec == default_ec: - xr, yr, zr = double_point_jacobian_fq_int(X.Z, Y.Z, Z.Z, ec) + xr, yr, zr = double_point_jacobian_fq(X.Z, Y.Z, Z.Z, ec) return JacobianPoint(Fq(Q, xr), Fq(Q, yr), Fq(Q, zr), False, ec) elif FE == Fq and ec == default_ec_twist: - xr, yr, zr = double_point_jacobian_fq_int_twist(X.Z, Y.Z, Z.Z, ec) + xr, yr, zr = double_point_jacobian_fq_twist(X.Z, Y.Z, Z.Z, ec) return JacobianPoint(Fq2(Q, xr), Fq2(Q, yr), Fq2(Q, zr), False, ec) elif FE == Fq12: - xr, yr, zr = double_point_jacobian_fq12_t(X.ZT, Y.ZT, Z.ZT, ec) + xr, yr, zr = double_point_jacobian_fq12(X.ZT, Y.ZT, Z.ZT, ec) return JacobianPoint(Fq12(Q, xr), Fq12(Q, yr), Fq12(Q, zr), False, ec) else: raise ValueError('FE must be Fq, Fq2 or Fq12') -def double_point_jacobian_fq_int(X, Y, Z, ec): +def double_point_jacobian_fq(X, Y, Z, ec): '''dobule point with fq int X, Y, Z, returning tuple''' P = ec.q ec_a = ec.a.Z # S = 4*X*Y^2 - S = 4*X*Y*Y%P + S = 4*X*Y*Y % P - Z_sq = Z*Z%P - Z_4th = Z_sq*Z_sq%P - Y_sq = Y*Y%P - Y_4th = Y_sq*Y_sq%P + Z_sq = Z*Z % P + Z_4th = Z_sq*Z_sq % P + Y_sq = Y*Y % P + Y_4th = Y_sq*Y_sq % P # M = 3*X^2 + a*Z^4 - M = (3*X*X%P + ec_a*Z_4th%P)%P + M = (3*X*X % P + ec_a*Z_4th % P) % P # X' = M^2 - 2*S - X_p = (M*M%P - 2*S%P)%P + X_p = (M*M % P - 2*S % P) % P # Y' = M*(S - X') - 8*Y^4 - Y_p = (M*((S - X_p)%P)%P - 8*Y_4th%P)%P + Y_p = (M*((S - X_p) % P) % P - 8*Y_4th % P) % P # Z' = 2*Y*Z - Z_p = 2*Y*Z%P + Z_p = 2*Y*Z % P return X_p, Y_p, Z_p -def double_point_jacobian_fq_int_twist(X, Y, Z, ec): +def double_point_jacobian_fq_twist(X, Y, Z, ec): '''dobule point with fq int X, Y, Z, returning tuple''' P = ec.q ec_a = ec.a.ZT - addi_f = fq2_t_add_fq_int - subi_f = fq2_t_sub_fq_int - muli_f = fq2_t_mul_fq_int - mul_f = fq2_t_mul_fq2_t + addi_f = fq2_add_fq + subi_f = fq2_sub_fq + muli_f = fq2_mul_fq + mul_f = fq2_mul_fq2 # S = 4*X*Y^2 - S = 4*X*Y*Y%P + S = 4*X*Y*Y % P - Z_sq = Z*Z%P - Z_4th = Z_sq*Z_sq%P - Y_sq = Y*Y%P - Y_4th = Y_sq*Y_sq%P + Z_sq = Z*Z % P + Z_4th = Z_sq*Z_sq % P + Y_sq = Y*Y % P + Y_4th = Y_sq*Y_sq % P # M = 3*X^2 + a*Z^4 - M = addi_f(P, muli_f(P, ec_a, Z_4th), 3*X*X%P) + M = addi_f(P, muli_f(P, ec_a, Z_4th), 3*X*X % P) # X' = M^2 - 2*S - X_p = subi_f(P, mul_f(P, M, M), 2*S%P) + X_p = subi_f(P, mul_f(P, M, M), 2*S % P) # Y' = M*(S - X') - 8*Y^4 - Y_p = subi_f(P, mul_f(P, M, fq_int_sub_fq2_t(P, S, X_p)) , 8*Y_4th%P) + Y_p = subi_f(P, mul_f(P, M, fq_sub_fq2(P, S, X_p)), 8*Y_4th % P) # Z' = 2*Y*Z - Z_p = addi_f(P, (0,0), 2*Y*Z%P) + Z_p = addi_f(P, (0, 0), 2*Y*Z % P) return X_p, Y_p, Z_p -def double_point_jacobian_fq2_t(X, Y, Z, ec): +def double_point_jacobian_fq2(X, Y, Z, ec): '''dobule point with fq2 tuples X, Y, Z, returning tuple of tuples''' if ec == default_ec_twist: - mul_ec_a = fq2_t_mul_fq2_t + mul_ec_a = fq2_mul_fq2 ec_a = ec.a.ZT else: - mul_ec_a = fq2_t_mul_fq_int + mul_ec_a = fq2_mul_fq ec_a = ec.a.Z - func_t = (fq2_t_mul_fq2_t, fq2_t_mul_fq_int, mul_ec_a, - fq2_t_add_fq2_t, fq2_t_sub_fq2_t) + func_t = (fq2_mul_fq2, fq2_mul_fq, mul_ec_a, fq2_add_fq2, fq2_sub_fq2) return double_point_jacobian_fqx_t(func_t, X, Y, Z, ec.q, ec_a) -def double_point_jacobian_fq12_t(X, Y, Z, ec): +def double_point_jacobian_fq12(X, Y, Z, ec): '''dobule point with fq12 tuples X, Y, Z, returning tuple of tuples''' if ec == default_ec_twist: - mul_ec_a = fq12_t_mul_fq2_t + mul_ec_a = fq12_mul_fq2 ec_a = ec.a.ZT else: - mul_ec_a = fq12_t_mul_fq_int + mul_ec_a = fq12_mul_fq ec_a = ec.a.Z - func_t = (fq12_t_mul_fq12_t, fq12_t_mul_fq_int, mul_ec_a, - fq12_t_add_fq12_t, fq12_t_sub_fq12_t) + func_t = (fq12_mul_fq12, fq12_mul_fq, mul_ec_a, + fq12_add_fq12, fq12_sub_fq12) return double_point_jacobian_fqx_t(func_t, X, Y, Z, ec.q, ec_a) @@ -393,16 +389,16 @@ def add_points_jacobian(p1, p2, ec=default_ec, FE=Fq): return p1 if FE == Fq: - U1, U2, S1, S2 = calc_u1_u2_s1_s2_fq_int(p1.x.Z, p1.y.Z, p1.z.Z, - p2.x.Z, p2.y.Z, p2.z.Z, - ec) + U1, U2, S1, S2 = calc_u1_u2_s1_s2_fq(p1.x.Z, p1.y.Z, p1.z.Z, + p2.x.Z, p2.y.Z, p2.z.Z, + ec) elif FE == Fq2: - U1, U2, S1, S2 = calc_u1_u2_s1_s2_fqx_t(fq2_t_mul_fq2_t, + U1, U2, S1, S2 = calc_u1_u2_s1_s2_fqx_t(fq2_mul_fq2, p1.x.ZT, p1.y.ZT, p1.z.ZT, p2.x.ZT, p2.y.ZT, p2.z.ZT, ec) elif FE == Fq12: - U1, U2, S1, S2 = calc_u1_u2_s1_s2_fqx_t(fq12_t_mul_fq12_t, + U1, U2, S1, S2 = calc_u1_u2_s1_s2_fqx_t(fq12_mul_fq12, p1.x.ZT, p1.y.ZT, p1.z.ZT, p2.x.ZT, p2.y.ZT, p2.z.ZT, ec) @@ -418,35 +414,35 @@ def add_points_jacobian(p1, p2, ec=default_ec, FE=Fq): type_u1 = type(U1) if type_u1 == int: - return calc_jp_on_fq_int_us(U1, U2, S1, S2, p1.z.Z, p2.z.Z, ec) + return calc_jp_on_fq_us(U1, U2, S1, S2, p1.z.Z, p2.z.Z, ec) elif type_u1 == tuple and len(U1) == 2: - func_t = (fq2_t_mul_fq2_t, fq2_t_sub_fq2_t, fq2_t_mul_fq_int) + func_t = (fq2_mul_fq2, fq2_sub_fq2, fq2_mul_fq) return calc_jp_on_fqx_t_us(func_t, U1, U2, S1, S2, p1.z.ZT, p2.z.ZT, ec) elif type_u1 == tuple and len(U1) == 12: - func_t = (fq12_t_mul_fq12_t, fq12_t_sub_fq12_t, fq12_t_mul_fq_int) + func_t = (fq12_mul_fq12, fq12_sub_fq12, fq12_mul_fq) return calc_jp_on_fqx_t_us(func_t, U1, U2, S1, S2, p1.z.ZT, p2.z.ZT, ec) else: raise ValueError('FE must be Fq, Fq2 or Fq12') -def calc_u1_u2_s1_s2_fq_int(x1, y1, z1, x2, y2, z2, ec): +def calc_u1_u2_s1_s2_fq(x1, y1, z1, x2, y2, z2, ec): '''x, y, z inputs of type int, returning tuple of int''' P = ec.q # U1 = X1*Z2^2 - U1 = x1*z2*z2%P + U1 = x1*z2*z2 % P # U2 = X2*Z1^2 - U2 = x2*z1*z1%P + U2 = x2*z1*z1 % P # S1 = Y1*Z2^3 - S1 = y1*z2*z2*z2%P + S1 = y1*z2*z2*z2 % P # S2 = Y2*Z1^3 - S2 = y2*z1*z1*z1%P + S2 = y2*z1*z1*z1 % P return(U1, U2, S1, S2) def calc_u1_u2_s1_s2_fqx_t(mul_f, x1_t, y1_t, z1_t, x2_t, y2_t, z2_t, ec): - '''x, y, z inputs of type fq2_t, returning tuple of fq2_t tuples''' + '''x, y, z inputs of type fq2, returning tuple of fq2 tuples''' P = ec.q # U1 = X1*Z2^2 U1 = mul_f(P, mul_f(P, x1_t, z2_t), z2_t) @@ -459,21 +455,21 @@ def calc_u1_u2_s1_s2_fqx_t(mul_f, x1_t, y1_t, z1_t, x2_t, y2_t, z2_t, ec): return(U1, U2, S1, S2) -def calc_jp_on_fq_int_us(U1, U2, S1, S2, Z1, Z2, ec): +def calc_jp_on_fq_us(U1, U2, S1, S2, Z1, Z2, ec): '''calc jacobian point with int U1, U2, S1, S2, Z1, Z2''' P = ec.q # H = U2 - U1 - H = (U2-U1)%P + H = (U2-U1) % P # R = S2 - S1 - R = (S2-S1)%P - H_sq = H*H%P - H_cu = H*H_sq%P + R = (S2-S1) % P + H_sq = H*H % P + H_cu = H*H_sq % P # X3 = R^2 - H^3 - 2*U1*H^2 - X3 = (R*R%P - H_cu - 2*U1*H_sq%P)%P + X3 = (R*R % P - H_cu - 2*U1*H_sq % P) % P # Y3 = R*(U1*H^2 - X3) - S1*H^3 - Y3 = (R*(U1*H_sq%P - X3)%P - S1*H_cu%P)%P + Y3 = (R*(U1*H_sq % P - X3) % P - S1*H_cu % P) % P # Z3 = H*Z1*Z2 - Z3 = H*Z1*Z2%P + Z3 = H*Z1*Z2 % P return JacobianPoint(Fq(P, X3), Fq(P, Y3), Fq(P, Z3), False, ec) diff --git a/bls_py/fields.py b/bls_py/fields.py index eb8bc76..1388488 100644 --- a/bls_py/fields.py +++ b/bls_py/fields.py @@ -1,22 +1,18 @@ # -*- coding: utf-8 -*- -from .fields_t import (fq_int_invert, fq_int_pow, fq_int_floordiv, fq2_t_neg, - fq2_t_invert, fq2_t_pow, fq2_t_qi_pow, - fq2_t_mul_by_nonresidue, fq2_t_add_fq2_t, - fq2_t_add_fq_int, fq2_t_sub_fq2_t, fq2_t_sub_fq_int, - fq_int_sub_fq2_t, fq2_t_mul_fq2_t, fq2_t_mul_fq_int, - fq12_t_mul_fq2_t, fq12_t_invert, fq6_t_mul_fq2_t, - fq6_t_neg, fq6_t_invert, fq6_t_pow, fq6_t_qi_pow, - fq6_t_mul_by_nonresidue, fq6_t_add_fq6_t, - fq6_t_add_fq2_t, fq6_t_add_fq_int, fq6_t_sub_fq6_t, - fq6_t_sub_fq2_t, fq6_t_sub_fq_int, fq2_t_sub_fq6_t, - fq_int_sub_fq6_t, fq6_t_mul_fq6_t, fq6_t_mul_fq_int, - fq12_t_mul_fq6_t, fq12_t_neg, fq12_t_pow, fq12_t_qi_pow, - fq12_t_add_fq12_t, fq12_t_add_fq_int, fq12_t_add_fq6_t, - fq12_t_add_fq2_t, fq12_t_sub_fq12_t, fq12_t_sub_fq_int, - fq12_t_sub_fq6_t, fq12_t_sub_fq2_t, fq_int_sub_fq12_t, - fq6_t_sub_fq12_t, fq2_t_sub_fq12_t, fq12_t_mul_fq12_t, - fq12_t_mul_fq_int) +from .fields_t import (fq_invert, fq_pow, fq_floordiv, fq2_neg, fq2_invert, + fq2_pow, fq2_qi_pow, fq2_mul_by_nonresidue, fq2_add_fq2, + fq2_add_fq, fq2_sub_fq2, fq2_sub_fq, fq_sub_fq2, + fq2_mul_fq2, fq2_mul_fq, fq12_mul_fq2, fq12_invert, + fq6_mul_fq2, fq6_neg, fq6_invert, fq6_pow, fq6_qi_pow, + fq6_mul_by_nonresidue, fq6_add_fq6, fq6_add_fq2, + fq6_add_fq, fq6_sub_fq6, fq6_sub_fq2, fq6_sub_fq, + fq2_sub_fq6, fq_sub_fq6, fq6_mul_fq6, fq6_mul_fq, + fq12_mul_fq6, fq12_neg, fq12_pow, fq12_qi_pow, + fq12_add_fq12, fq12_add_fq, fq12_add_fq6, fq12_add_fq2, + fq12_sub_fq12, fq12_sub_fq, fq12_sub_fq6, fq12_sub_fq2, + fq_sub_fq12, fq6_sub_fq12, fq2_sub_fq12, fq12_mul_fq12, + fq12_mul_fq) bls12381_q = int('0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf' @@ -102,10 +98,10 @@ def __neg__(self): return Fq(self.Q, -self.Z) def __invert__(self): - return Fq(self.Q, fq_int_invert(self.Q, self.Z)) + return Fq(self.Q, fq_invert(self.Q, self.Z)) def __pow__(self, X): - return Fq(self.Q, fq_int_pow(self.Q, self.Z, X)) + return Fq(self.Q, fq_pow(self.Q, self.Z, X)) def __add__(self, X): tx = type(X) @@ -162,9 +158,9 @@ def __rmul__(self, X): def __floordiv__(self, X): tx = type(X) if tx == Fq: - return Fq(self.Q, fq_int_floordiv(self.Q, self.Z, X.Z)) + return Fq(self.Q, fq_floordiv(self.Q, self.Z, X.Z)) elif tx == int: - return Fq(self.Q, fq_int_floordiv(self.Q, self.Z, X)) + return Fq(self.Q, fq_floordiv(self.Q, self.Z, X)) else: return NotImplemented @@ -374,13 +370,13 @@ def __repr__(self): return ('Fq2(Q, %s)' % ', '.join(repr(fq) for fq in self)) def __neg__(self): - return Fq2(self.Q, fq2_t_neg(self.Q, self.ZT)) + return Fq2(self.Q, fq2_neg(self.Q, self.ZT)) def __invert__(self): - return Fq2(self.Q, fq2_t_invert(self.Q, self.ZT)) + return Fq2(self.Q, fq2_invert(self.Q, self.ZT)) def __pow__(self, e): - return Fq2(self.Q, fq2_t_pow(self.Q, self.ZT, e)) + return Fq2(self.Q, fq2_pow(self.Q, self.ZT, e)) def qi_power(self, i): global bls12381_q @@ -389,21 +385,21 @@ def qi_power(self, i): i %= 2 if i == 0: return self - return Fq2(self.Q, fq2_t_qi_pow(self.Q, self.ZT, i)) + return Fq2(self.Q, fq2_qi_pow(self.Q, self.ZT, i)) def mul_by_nonresidue(self): # multiply by u + 1 - return Fq2(self.Q, fq2_t_mul_by_nonresidue(self.Q, self.ZT)) + return Fq2(self.Q, fq2_mul_by_nonresidue(self.Q, self.ZT)) def __add__(self, other): Q = self.Q tx = type(other) if tx == Fq2: - return Fq2(Q, fq2_t_add_fq2_t(Q, self.ZT, other.ZT)) + return Fq2(Q, fq2_add_fq2(Q, self.ZT, other.ZT)) elif tx == int: - return Fq2(Q, fq2_t_add_fq_int(Q, self.ZT, other)) + return Fq2(Q, fq2_add_fq(Q, self.ZT, other)) elif tx == Fq: - return Fq2(Q, fq2_t_add_fq_int(Q, self.ZT, other.Z)) + return Fq2(Q, fq2_add_fq(Q, self.ZT, other.Z)) else: return NotImplemented @@ -411,11 +407,11 @@ def __sub__(self, other): Q = self.Q tx = type(other) if tx == Fq2: - return Fq2(Q, fq2_t_sub_fq2_t(Q, self.ZT, other.ZT)) + return Fq2(Q, fq2_sub_fq2(Q, self.ZT, other.ZT)) elif tx == int: - return Fq2(Q, fq2_t_sub_fq_int(Q, self.ZT, other)) + return Fq2(Q, fq2_sub_fq(Q, self.ZT, other)) elif tx == Fq: - return Fq2(Q, fq2_t_sub_fq_int(Q, self.ZT, other.Z)) + return Fq2(Q, fq2_sub_fq(Q, self.ZT, other.Z)) else: return NotImplemented @@ -423,11 +419,11 @@ def __rsub__(self, other): Q = self.Q tx = type(other) if tx == Fq2: - return Fq2(Q, fq2_t_sub_fq2_t(Q, other.ZT, self.ZT)) + return Fq2(Q, fq2_sub_fq2(Q, other.ZT, self.ZT)) elif tx == int: - return Fq2(Q, fq_int_sub_fq2_t(Q, other, self.ZT)) + return Fq2(Q, fq_sub_fq2(Q, other, self.ZT)) elif tx == Fq: - return Fq2(Q, fq_int_sub_fq2_t(Q, other.Z, self.ZT)) + return Fq2(Q, fq_sub_fq2(Q, other.Z, self.ZT)) else: return NotImplemented @@ -435,11 +431,11 @@ def __mul__(self, other): Q = self.Q tx = type(other) if tx == Fq2: - return Fq2(Q, fq2_t_mul_fq2_t(Q, self.ZT, other.ZT)) + return Fq2(Q, fq2_mul_fq2(Q, self.ZT, other.ZT)) elif tx == int: - return Fq2(Q, fq2_t_mul_fq_int(Q, self.ZT, other)) + return Fq2(Q, fq2_mul_fq(Q, self.ZT, other)) elif tx == Fq: - return Fq2(Q, fq2_t_mul_fq_int(Q, self.ZT, other.Z)) + return Fq2(Q, fq2_mul_fq(Q, self.ZT, other.Z)) else: return NotImplemented @@ -447,20 +443,15 @@ def __floordiv__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_mul_fq2_t(Q, fq12_t_invert(Q, other.ZT), - self.ZT)) + return Fq12(Q, fq12_mul_fq2(Q, fq12_invert(Q, other.ZT), self.ZT)) elif tx == Fq6: - return Fq6(Q, fq6_t_mul_fq2_t(Q, fq2_t_invert(Q, other.ZT), - self.ZT)) + return Fq6(Q, fq6_mul_fq2(Q, fq2_invert(Q, other.ZT), self.ZT)) elif tx == Fq2: - return Fq2(Q, fq2_t_mul_fq2_t(Q, self.ZT, - fq2_t_invert(Q, other.ZT))) + return Fq2(Q, fq2_mul_fq2(Q, self.ZT, fq2_invert(Q, other.ZT))) elif tx == int: - return Fq2(Q, fq2_t_mul_fq_int(Q, self.ZT, - fq_int_invert(Q, other))) + return Fq2(Q, fq2_mul_fq(Q, self.ZT, fq_invert(Q, other))) elif tx == Fq: - return Fq2(Q, fq2_t_mul_fq_int(Q, self.ZT, - fq_int_invert(Q, other.Z))) + return Fq2(Q, fq2_mul_fq(Q, self.ZT, fq_invert(Q, other.Z))) else: return NotImplemented @@ -531,13 +522,13 @@ def __repr__(self): return ('Fq6(Q, %s)' % ', '.join(repr(fq2) for fq2 in self)) def __neg__(self): - return Fq6(self.Q, fq6_t_neg(self.Q, self.ZT)) + return Fq6(self.Q, fq6_neg(self.Q, self.ZT)) def __invert__(self): - return Fq6(self.Q, fq6_t_invert(self.Q, self.ZT)) + return Fq6(self.Q, fq6_invert(self.Q, self.ZT)) def __pow__(self, e): - return Fq6(self.Q, fq6_t_pow(self.Q, self.ZT, e)) + return Fq6(self.Q, fq6_pow(self.Q, self.ZT, e)) def qi_power(self, i): global bls12381_q @@ -546,23 +537,23 @@ def qi_power(self, i): i %= 6 if i == 0: return self - return Fq6(self.Q, fq6_t_qi_pow(self.Q, self.ZT, i)) + return Fq6(self.Q, fq6_qi_pow(self.Q, self.ZT, i)) def mul_by_nonresidue(self): # multiply by v - return Fq6(self.Q, fq6_t_mul_by_nonresidue(self.Q, self.ZT)) + return Fq6(self.Q, fq6_mul_by_nonresidue(self.Q, self.ZT)) def __add__(self, other): Q = self.Q tx = type(other) if tx == Fq6: - return Fq6(Q, fq6_t_add_fq6_t(Q, self.ZT, other.ZT)) + return Fq6(Q, fq6_add_fq6(Q, self.ZT, other.ZT)) elif tx == Fq2: - return Fq6(Q, fq6_t_add_fq2_t(Q, self.ZT, other.ZT)) + return Fq6(Q, fq6_add_fq2(Q, self.ZT, other.ZT)) elif tx == Fq: - return Fq6(Q, fq6_t_add_fq_int(Q, self.ZT, other.Z)) + return Fq6(Q, fq6_add_fq(Q, self.ZT, other.Z)) elif tx == int: - return Fq6(Q, fq6_t_add_fq_int(Q, self.ZT, other)) + return Fq6(Q, fq6_add_fq(Q, self.ZT, other)) else: return NotImplemented @@ -570,13 +561,13 @@ def __sub__(self, other): Q = self.Q tx = type(other) if tx == Fq6: - return Fq6(Q, fq6_t_sub_fq6_t(Q, self.ZT, other.ZT)) + return Fq6(Q, fq6_sub_fq6(Q, self.ZT, other.ZT)) elif tx == Fq2: - return Fq6(Q, fq6_t_sub_fq2_t(Q, self.ZT, other.ZT)) + return Fq6(Q, fq6_sub_fq2(Q, self.ZT, other.ZT)) elif tx == Fq: - return Fq6(Q, fq6_t_sub_fq_int(Q, self.ZT, other.Z)) + return Fq6(Q, fq6_sub_fq(Q, self.ZT, other.Z)) elif tx == int: - return Fq6(Q, fq6_t_sub_fq_int(Q, self.ZT, other)) + return Fq6(Q, fq6_sub_fq(Q, self.ZT, other)) else: return NotImplemented @@ -584,13 +575,13 @@ def __rsub__(self, other): Q = self.Q tx = type(other) if tx == Fq6: - return Fq6(Q, fq6_t_sub_fq6_t(Q, other.ZT, self.ZT)) + return Fq6(Q, fq6_sub_fq6(Q, other.ZT, self.ZT)) elif tx == Fq2: - return Fq6(Q, fq2_t_sub_fq6_t(Q, other.ZT, self.ZT)) + return Fq6(Q, fq2_sub_fq6(Q, other.ZT, self.ZT)) elif tx == Fq: - return Fq6(Q, fq_int_sub_fq6_t(Q, other.Z, self.ZT)) + return Fq6(Q, fq_sub_fq6(Q, other.Z, self.ZT)) elif tx == int: - return Fq6(Q, fq_int_sub_fq6_t(Q, other, self.ZT)) + return Fq6(Q, fq_sub_fq6(Q, other, self.ZT)) else: return NotImplemented @@ -598,13 +589,13 @@ def __mul__(self, other): Q = self.Q tx = type(other) if tx == Fq6: - return Fq6(Q, fq6_t_mul_fq6_t(Q, self.ZT, other.ZT)) + return Fq6(Q, fq6_mul_fq6(Q, self.ZT, other.ZT)) elif tx == Fq2: - return Fq6(Q, fq6_t_mul_fq2_t(Q, self.ZT, other.ZT)) + return Fq6(Q, fq6_mul_fq2(Q, self.ZT, other.ZT)) elif tx == Fq: - return Fq6(Q, fq6_t_mul_fq_int(Q, self.ZT, other.Z)) + return Fq6(Q, fq6_mul_fq(Q, self.ZT, other.Z)) elif tx == int: - return Fq6(Q, fq6_t_mul_fq_int(Q, self.ZT, other)) + return Fq6(Q, fq6_mul_fq(Q, self.ZT, other)) else: return NotImplemented @@ -612,20 +603,15 @@ def __floordiv__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_mul_fq6_t(Q, fq12_t_invert(Q, other.ZT), - self.ZT)) + return Fq12(Q, fq12_mul_fq6(Q, fq12_invert(Q, other.ZT), self.ZT)) elif tx == Fq6: - return Fq6(Q, fq6_t_mul_fq6_t(Q, self.ZT, - fq6_t_invert(Q, other.ZT))) + return Fq6(Q, fq6_mul_fq6(Q, self.ZT, fq6_invert(Q, other.ZT))) elif tx == Fq2: - return Fq6(Q, fq6_t_mul_fq2_t(Q, self.ZT, - fq2_t_invert(Q, other.ZT))) + return Fq6(Q, fq6_mul_fq2(Q, self.ZT, fq2_invert(Q, other.ZT))) elif tx == Fq: - return Fq6(Q, fq6_t_mul_fq_int(Q, self.ZT, - fq_int_invert(Q, other.Z))) + return Fq6(Q, fq6_mul_fq(Q, self.ZT, fq_invert(Q, other.Z))) elif tx == int: - return Fq6(Q, fq6_t_mul_fq_int(Q, self.ZT, - fq_int_invert(Q, other))) + return Fq6(Q, fq6_mul_fq(Q, self.ZT, fq_invert(Q, other))) else: return NotImplemented @@ -675,13 +661,13 @@ def __repr__(self): return ('Fq12(Q, %s)' % ', '.join(repr(fq6) for fq6 in self)) def __neg__(self): - return Fq12(self.Q, fq12_t_neg(self.Q, self.ZT)) + return Fq12(self.Q, fq12_neg(self.Q, self.ZT)) def __invert__(self): - return Fq12(self.Q, fq12_t_invert(self.Q, self.ZT)) + return Fq12(self.Q, fq12_invert(self.Q, self.ZT)) def __pow__(self, e): - return Fq12(self.Q, fq12_t_pow(self.Q, self.ZT, e)) + return Fq12(self.Q, fq12_pow(self.Q, self.ZT, e)) def qi_power(self, i): global bls12381_q @@ -690,21 +676,21 @@ def qi_power(self, i): i %= 12 if i == 0: return self - return Fq12(self.Q, fq12_t_qi_pow(self.Q, self.ZT, i)) + return Fq12(self.Q, fq12_qi_pow(self.Q, self.ZT, i)) def __add__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_add_fq12_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_add_fq12(Q, self.ZT, other.ZT)) elif tx == Fq: - return Fq12(Q, fq12_t_add_fq_int(Q, self.ZT, other.Z)) + return Fq12(Q, fq12_add_fq(Q, self.ZT, other.Z)) elif tx == Fq6: - return Fq12(Q, fq12_t_add_fq6_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_add_fq6(Q, self.ZT, other.ZT)) elif tx == Fq2: - return Fq12(Q, fq12_t_add_fq2_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_add_fq2(Q, self.ZT, other.ZT)) elif tx == int: - return Fq12(Q, fq12_t_add_fq_int(Q, self.ZT, other)) + return Fq12(Q, fq12_add_fq(Q, self.ZT, other)) else: return NotImplemented @@ -712,15 +698,15 @@ def __sub__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_sub_fq12_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_sub_fq12(Q, self.ZT, other.ZT)) elif tx == Fq: - return Fq12(Q, fq12_t_sub_fq_int(Q, self.ZT, other.Z)) + return Fq12(Q, fq12_sub_fq(Q, self.ZT, other.Z)) elif tx == Fq6: - return Fq12(Q, fq12_t_sub_fq6_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_sub_fq6(Q, self.ZT, other.ZT)) elif tx == Fq2: - return Fq12(Q, fq12_t_sub_fq2_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_sub_fq2(Q, self.ZT, other.ZT)) elif tx == int: - return Fq12(Q, fq12_t_sub_fq_int(Q, self.ZT, other)) + return Fq12(Q, fq12_sub_fq(Q, self.ZT, other)) else: return NotImplemented @@ -728,15 +714,15 @@ def __rsub__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_sub_fq12_t(Q, other.ZT, self.ZT)) + return Fq12(Q, fq12_sub_fq12(Q, other.ZT, self.ZT)) elif tx == Fq: - return Fq12(Q, fq_int_sub_fq12_t(Q, other.Z, self.ZT)) + return Fq12(Q, fq_sub_fq12(Q, other.Z, self.ZT)) elif tx == Fq6: - return Fq12(Q, fq6_t_sub_fq12_t(Q, other.ZT, self.ZT)) + return Fq12(Q, fq6_sub_fq12(Q, other.ZT, self.ZT)) elif tx == Fq2: - return Fq12(Q, fq2_t_sub_fq12_t(Q, other.ZT, self.ZT)) + return Fq12(Q, fq2_sub_fq12(Q, other.ZT, self.ZT)) elif tx == int: - return Fq12(Q, fq_int_sub_fq12_t(Q, other, self.ZT)) + return Fq12(Q, fq_sub_fq12(Q, other, self.ZT)) else: return NotImplemented @@ -744,15 +730,15 @@ def __mul__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_mul_fq12_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_mul_fq12(Q, self.ZT, other.ZT)) elif tx == int: - return Fq12(Q, fq12_t_mul_fq_int(Q, self.ZT, other)) + return Fq12(Q, fq12_mul_fq(Q, self.ZT, other)) elif tx == Fq: - return Fq12(Q, fq12_t_mul_fq_int(Q, self.ZT, other.Z)) + return Fq12(Q, fq12_mul_fq(Q, self.ZT, other.Z)) elif tx == Fq2: - return Fq12(Q, fq12_t_mul_fq2_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_mul_fq2(Q, self.ZT, other.ZT)) elif tx == Fq6: - return Fq12(Q, fq12_t_mul_fq6_t(Q, self.ZT, other.ZT)) + return Fq12(Q, fq12_mul_fq6(Q, self.ZT, other.ZT)) else: return NotImplemented @@ -760,20 +746,15 @@ def __floordiv__(self, other): Q = self.Q tx = type(other) if tx == Fq12: - return Fq12(Q, fq12_t_mul_fq12_t(Q, self.ZT, - fq12_t_invert(Q, other.ZT))) + return Fq12(Q, fq12_mul_fq12(Q, self.ZT, fq12_invert(Q, other.ZT))) elif tx == int: - return Fq12(Q, fq12_t_mul_fq12_t(Q, self.ZT, - fq_int_invert(Q, other))) + return Fq12(Q, fq12_mul_fq12(Q, self.ZT, fq_invert(Q, other))) elif tx == Fq: - return Fq12(Q, fq12_t_mul_fq12_t(Q, self.ZT, - fq_int_invert(Q, other.Z))) + return Fq12(Q, fq12_mul_fq12(Q, self.ZT, fq_invert(Q, other.Z))) elif tx == Fq2: - return Fq12(Q, fq12_t_mul_fq2_t(Q, self.ZT, - fq2_t_invert(Q, other.ZT))) + return Fq12(Q, fq12_mul_fq2(Q, self.ZT, fq2_invert(Q, other.ZT))) elif tx == Fq6: - return Fq12(Q, fq12_t_mul_fq6_t(Q, self.ZT, - fq6_t_invert(Q, other.ZT))) + return Fq12(Q, fq12_mul_fq6(Q, self.ZT, fq6_invert(Q, other.ZT))) else: return NotImplemented diff --git a/bls_py/fields_t.py b/bls_py/fields_t.py index 4c28b03..9fc3665 100644 --- a/bls_py/fields_t.py +++ b/bls_py/fields_t.py @@ -18,7 +18,7 @@ '6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab', 16) -FQ2_ROOT = -1%bls12381_q +FQ2_ROOT = -1 % bls12381_q FQ2_ONE_TUPLE = (1, 0) FQ2_ZERO_TUPLE = (0, 0) FQ6_ROOT_TUPLE = (1, 1) @@ -29,7 +29,7 @@ FQ12_ZERO_TUPLE = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) -def fq_int_invert(P, a): +def fq_invert(P, a): '''Ivnert int value using extended euclidian algorithm for inversion''' p = P x0, x1, y0, y1 = 1, 0, 0, 1 @@ -37,13 +37,13 @@ def fq_int_invert(P, a): q, a, p = a // p, p, a % p x0, x1 = x1, x0 - q * x1 y0, y1 = y1, y0 - q * y1 - return x0%P + return x0 % P -def fq_int_pow(P, a, X): +def fq_pow(P, a, X): '''Pow a to X mod P ''' if X == 0: - return 1%P + return 1 % P res = 1 while X > 0: if X & 1: @@ -53,37 +53,37 @@ def fq_int_pow(P, a, X): return res -def fq_int_floordiv(P, a, X): - return a * fq_int_invert(P, X) % P +def fq_floordiv(P, a, X): + return a * fq_invert(P, X) % P -def fq2_t_neg(P, t_a): +def fq2_neg(P, t_a): '''Neg tuple t_a returning tuple''' a, b = t_a - return (-a%P, -b%P) + return (-a % P, -b % P) -def fq2_t_invert(P, t_a): +def fq2_invert(P, t_a): '''Invert tuple t_a returning tuple''' a, b = t_a - factor = fq_int_invert(P, a * a + b * b) - return ((a*factor)%P, (-b*factor)%P) + factor = fq_invert(P, a * a + b * b) + return ((a*factor) % P, (-b*factor) % P) -def fq2_t_pow(P, t_a, e): +def fq2_pow(P, t_a, e): '''Pow tuple t_a returning tuple''' m, n = t_a a, b = 1, 0 fq2r = FQ2_ROOT while e: if e & 1: - a, b = (a*m + b*n*fq2r)%P, (a*n + b*m)%P - m, n = (m*m + n*n*fq2r)%P, (m*n + n*m)%P + a, b = (a*m + b*n*fq2r) % P, (a*n + b*m) % P + m, n = (m*m + n*n*fq2r) % P, (m*n + n*m) % P e >>= 1 return (a, b) -def fq2_t_qi_pow(P, t_x, i): +def fq2_qi_pow(P, t_x, i): '''Calc qi_power on t_x tuple returning tuple''' global bls12381_q, frob_coeffs if P != bls12381_q: @@ -91,107 +91,97 @@ def fq2_t_qi_pow(P, t_x, i): i %= 2 if i == 0: return t_x - return (t_x[0], t_x[1]*frob_coeffs[2, i, 1]%P) + return (t_x[0], t_x[1]*frob_coeffs[2, i, 1] % P) -def fq2_t_mul_by_nonresidue(P, t_a): +def fq2_mul_by_nonresidue(P, t_a): '''Mul by nonresidue on tuple t_a returning tuple''' a, b = t_a - return ((a-b)%P, (a+b)%P) + return ((a-b) % P, (a+b) % P) -def fq2_t_add_fq_int(P, t_a, m): +def fq2_add_fq(P, t_a, m): '''Add tuple t_a and int m returning tuple''' a, b = t_a - return ((a+m)%P, b) + return ((a+m) % P, b) -def fq2_t_add_fq2_t(P, t_a, t_m): +def fq2_add_fq2(P, t_a, t_m): '''Add tuple t_a and tuple t_m returning tuple''' a, b = t_a m, n = t_m - return ((a+m)%P, (b+n)%P) + return ((a+m) % P, (b+n) % P) -def fq_int_sub_fq2_t(P, a, t_m): +def fq_sub_fq2(P, a, t_m): '''Sub tuple t_m from int a returning tuple''' m, n = t_m - return ((a-m)%P, -n%P) + return ((a-m) % P, -n % P) -def fq2_t_sub_fq_int(P, t_a, m): +def fq2_sub_fq(P, t_a, m): '''Sub int m from tuple t_a returning tuple''' a, b = t_a - return ((a-m)%P, b) + return ((a-m) % P, b) -def fq2_t_sub_fq2_t(P, t_a, t_m): +def fq2_sub_fq2(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b = t_a m, n = t_m - return ((a-m)%P, (b-n)%P) + return ((a-m) % P, (b-n) % P) -def fq2_t_mul_fq_int(P, t_a, m): +def fq2_mul_fq(P, t_a, m): '''Multiple tuple t_a on int m returning tuple''' a, b = t_a - return (a*m%P, b*m%P) + return (a*m % P, b*m % P) -def fq2_t_mul_fq2_t(P, t_a, t_m): +def fq2_mul_fq2(P, t_a, t_m): '''Multiple tuple t_a on tuple t_m returning tuple''' a, b = t_a m, n = t_m - return ((a*m + b*n*FQ2_ROOT)%P, (a*n + b*m)%P) + return ((a*m + b*n*FQ2_ROOT) % P, (a*n + b*m) % P) -def fq6_t_neg(P, t_a): +def fq6_neg(P, t_a): '''Neg tuple t_a returning tuple''' a, b, c, d, e, f = t_a - return (-a%P, -b%P, -c%P, -d%P, -e%P, -f%P) + return (-a % P, -b % P, -c % P, -d % P, -e % P, -f % P) -def fq6_t_invert(P, t_x): +def fq6_invert(P, t_x): '''Invert tuple t_a returning tuple''' a, b, c = t_x[:2], t_x[2:4], t_x[4:] - g0 = fq2_t_mul_fq2_t(P, a, a) - g0 = fq2_t_sub_fq2_t(P, g0, - fq2_t_mul_fq2_t(P, b, - fq2_t_mul_by_nonresidue(P, c))) - g1 = fq2_t_sub_fq2_t(P, - fq2_t_mul_by_nonresidue(P, - fq2_t_mul_fq2_t(P, c, c)), - fq2_t_mul_fq2_t(P, a, b)) - g2 = fq2_t_sub_fq2_t(P, - fq2_t_mul_fq2_t(P, b, b), - fq2_t_mul_fq2_t(P, a, c)) - g0a = fq2_t_mul_fq2_t(P, g0, a) - g1cpg2b = fq2_t_add_fq2_t(P, - fq2_t_mul_fq2_t(P, g1, c), - fq2_t_mul_fq2_t(P, g2, b)) - factor = fq2_t_invert(P, - fq2_t_add_fq2_t(P, - g0a, - fq2_t_mul_by_nonresidue(P, g1cpg2b))) - ar, br = fq2_t_mul_fq2_t(P, g0, factor) - cr, dr = fq2_t_mul_fq2_t(P, g1, factor) - er, fr = fq2_t_mul_fq2_t(P, g2, factor) - return (ar%P, br%P, cr%P, dr%P, er%P, fr%P) - - -def fq6_t_pow(P, t_a, e): + g0 = fq2_mul_fq2(P, a, a) + g0 = fq2_sub_fq2(P, g0, fq2_mul_fq2(P, b, fq2_mul_by_nonresidue(P, c))) + g1 = fq2_sub_fq2(P, fq2_mul_by_nonresidue(P, fq2_mul_fq2(P, c, c)), + fq2_mul_fq2(P, a, b)) + g2 = fq2_sub_fq2(P, fq2_mul_fq2(P, b, b), fq2_mul_fq2(P, a, c)) + g0a = fq2_mul_fq2(P, g0, a) + g1cpg2b = fq2_add_fq2(P, fq2_mul_fq2(P, g1, c), fq2_mul_fq2(P, g2, b)) + factor = fq2_invert(P, fq2_add_fq2(P, g0a, + fq2_mul_by_nonresidue(P, g1cpg2b))) + ar, br = fq2_mul_fq2(P, g0, factor) + cr, dr = fq2_mul_fq2(P, g1, factor) + er, fr = fq2_mul_fq2(P, g2, factor) + return (ar % P, br % P, cr % P, dr % P, er % P, fr % P) + + +def fq6_pow(P, t_a, e): '''Pow tuple t_a returning tuple''' t_ans = FQ6_ONE_TUPLE while e: if e & 1: - t_ans = fq6_t_mul_fq6_t(P, t_ans, t_a) - t_a = fq6_t_mul_fq6_t(P, t_a, t_a) + t_ans = fq6_mul_fq6(P, t_ans, t_a) + t_a = fq6_mul_fq6(P, t_a, t_a) e >>= 1 a, b, c, d, e, f = t_ans - return (a%P, b%P, c%P, d%P, e%P, f%P) + return (a % P, b % P, c % P, d % P, e % P, f % P) -def fq6_t_qi_pow(P, t_x, i): +def fq6_qi_pow(P, t_x, i): '''Calc qi_power on t_x tuple returning tuple''' global bls12381_q, frob_coeffs if P != bls12381_q: @@ -199,94 +189,92 @@ def fq6_t_qi_pow(P, t_x, i): i %= 6 if i == 0: return t_x - a, b = fq2_t_qi_pow(P, t_x[:2], i) - c, d = fq2_t_mul_fq2_t(P, fq2_t_qi_pow(P, t_x[2:4], i), - frob_coeffs[6, i, 1]) - e, f = fq2_t_mul_fq2_t(P, fq2_t_qi_pow(P, t_x[4:6], i), - frob_coeffs[6, i, 2]) + a, b = fq2_qi_pow(P, t_x[:2], i) + c, d = fq2_mul_fq2(P, fq2_qi_pow(P, t_x[2:4], i), frob_coeffs[6, i, 1]) + e, f = fq2_mul_fq2(P, fq2_qi_pow(P, t_x[4:6], i), frob_coeffs[6, i, 2]) return (a, b, c, d, e, f) -def fq6_t_mul_by_nonresidue(P, t_x): +def fq6_mul_by_nonresidue(P, t_x): '''Mul by nonresidue on tuple t_a returning tuple''' - ar, br = fq2_t_mul_fq2_t(P, t_x[4:], FQ6_ROOT_TUPLE) + ar, br = fq2_mul_fq2(P, t_x[4:], FQ6_ROOT_TUPLE) cr, dr = t_x[:2] er, fr = t_x[2:4] return (ar, br, cr, dr, er, fr) -def fq6_t_add_fq_int(P, t_a, m): +def fq6_add_fq(P, t_a, m): '''Add tuple t_a and int m returning tuple''' a, b, c, d, e, f = t_a - return ((a+m)%P, b, c, d, e, f) + return ((a+m) % P, b, c, d, e, f) -def fq6_t_add_fq2_t(P, t_a, t_m): +def fq6_add_fq2(P, t_a, t_m): '''Add tuple t_a and tuple t_m returning tuple''' a, b, c, d, e, f = t_a m, n = t_m - return ((a+m)%P, (b+n)%P, c, d, e, f) + return ((a+m) % P, (b+n) % P, c, d, e, f) -def fq6_t_add_fq6_t(P, t_a, t_m): +def fq6_add_fq6(P, t_a, t_m): '''Add tuple t_a and tuple t_m returning tuple''' a, b, c, d, e, f = t_a m, n, o, p, q, r = t_m - return ((a+m)%P, (b+n)%P, (c+o)%P, - (d+p)%P, (e+q)%P, (f+r)%P) + return ((a+m) % P, (b+n) % P, (c+o) % P, + (d+p) % P, (e+q) % P, (f+r) % P) -def fq_int_sub_fq6_t(P, a, t_m): +def fq_sub_fq6(P, a, t_m): '''Sub tuple t_m from int a returning tuple''' m, n, o, p, q, r = t_m - return ((a-m)%P, -n%P, -o%P, -p%P, -q%P, -r%P) + return ((a-m) % P, -n % P, -o % P, -p % P, -q % P, -r % P) -def fq6_t_sub_fq_int(P, t_a, m): +def fq6_sub_fq(P, t_a, m): '''Sub int m from tuple t_a returning tuple''' a, b, c, d, e, f = t_a - return ((a-m)%P, b, c, d, e, f) + return ((a-m) % P, b, c, d, e, f) -def fq2_t_sub_fq6_t(P, t_a, t_m): +def fq2_sub_fq6(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b = t_a m, n, o, p, q, r = t_m - return ((a-m)%P, (b-n)%P, -o%P, -p%P, -q%P, -r%P) + return ((a-m) % P, (b-n) % P, -o % P, -p % P, -q % P, -r % P) -def fq6_t_sub_fq2_t(P, t_a, t_m): +def fq6_sub_fq2(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b, c, d, e, f = t_a m, n = t_m - return ((a-m)%P, (b-n)%P, c, d, e, f) + return ((a-m) % P, (b-n) % P, c, d, e, f) -def fq6_t_sub_fq6_t(P, t_a, t_m): +def fq6_sub_fq6(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b, c, d, e, f = t_a m, n, o, p, q, r = t_m - return ((a-m)%P, (b-n)%P, (c-o)%P, - (d-p)%P, (e-q)%P, (f-r)%P) + return ((a-m) % P, (b-n) % P, (c-o) % P, + (d-p) % P, (e-q) % P, (f-r) % P) -def fq6_t_mul_fq_int(P, t_a, m): +def fq6_mul_fq(P, t_a, m): '''Multiple tuple t_a on int m returning tuple''' a, b, c, d, e, f = t_a - return (a*m%P, b*m%P, c*m%P, d*m%P, e*m%P, f*m%P) + return (a*m % P, b*m % P, c*m % P, d*m % P, e*m % P, f*m % P) -def fq6_t_mul_fq2_t(P, t_a, t_m): +def fq6_mul_fq2(P, t_a, t_m): '''Multiple tuple t_a on tuple t_m returning tuple''' a, b, c, d, e, f = t_a m, n = t_m fq2r = FQ2_ROOT - return ((a*m + b*n*fq2r)%P, (a*n + b*m)%P, - (c*m + d*n*fq2r)%P, (c*n + d*m)%P, - (e*m + f*n*fq2r)%P, (e*n + f*m)%P) + return ((a*m + b*n*fq2r) % P, (a*n + b*m) % P, + (c*m + d*n*fq2r) % P, (c*n + d*m) % P, + (e*m + f*n*fq2r) % P, (e*n + f*m) % P) -def fq6_t_mul_fq6_t(P, t_a, t_m): +def fq6_mul_fq6(P, t_a, t_m): '''Multiple tuple t_a on tuple t_m returning tuple''' a, b, c, d, e, f = t_a m, n, o, p, q, r = t_m @@ -302,45 +290,40 @@ def fq6_t_mul_fq6_t(P, t_a, t_m): mul_d = (a*p + b*o + cn + dm + eq + frfq2r + er + fq) mul_e = (a*q + b*r*fq2r + c*o + d*p*fq2r + e*m + f*n*fq2r) mul_f = (a*r + b*q + c*p + d*o + e*n + f*m) - return (mul_a%P, mul_b%P, mul_c%P, mul_d%P, mul_e%P, mul_f%P) + return (mul_a % P, mul_b % P, mul_c % P, mul_d % P, mul_e % P, mul_f % P) -def fq12_t_neg(P, t_a): +def fq12_neg(P, t_a): '''Neg tuple t_a returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a - return (-a%P, -b%P, -c%P, -d%P, -e%P, -f%P, - -g%P, -h%P, -i%P, -j%P, -k%P, -l%P) + return (-a % P, -b % P, -c % P, -d % P, -e % P, -f % P, + -g % P, -h % P, -i % P, -j % P, -k % P, -l % P) -def fq12_t_invert(P, t_x): +def fq12_invert(P, t_x): '''Invert tuple t_a returning tuple''' a, b = t_x[:6], t_x[6:12] - aa = fq6_t_mul_fq6_t(P, a, a) - bb = fq6_t_mul_fq6_t(P, b, b) - factor = fq6_t_invert(P, - fq6_t_sub_fq6_t(P, - aa, - fq6_t_mul_by_nonresidue(P, bb))) - ar, br, cr, dr, er, fr = fq6_t_mul_fq6_t(P, a, factor) - gr, hr, ir, jr, kr, lr = fq6_t_mul_fq6_t(P, - fq6_t_neg(P, b), - factor) - return (ar%P, br%P, cr%P, dr%P, er%P, fr%P, - gr%P, hr%P, ir%P, jr%P, kr%P, lr%P) - - -def fq12_t_pow(P, t_a, e): + aa = fq6_mul_fq6(P, a, a) + bb = fq6_mul_fq6(P, b, b) + factor = fq6_invert(P, fq6_sub_fq6(P, aa, fq6_mul_by_nonresidue(P, bb))) + ar, br, cr, dr, er, fr = fq6_mul_fq6(P, a, factor) + gr, hr, ir, jr, kr, lr = fq6_mul_fq6(P, fq6_neg(P, b), factor) + return (ar % P, br % P, cr % P, dr % P, er % P, fr % P, + gr % P, hr % P, ir % P, jr % P, kr % P, lr % P) + + +def fq12_pow(P, t_a, e): '''Pow tuple t_a returning tuple''' t_ans = FQ12_ONE_TUPLE while e: if e & 1: - t_ans = fq12_t_mul_fq12_t(P, t_ans, t_a) - t_a = fq12_t_mul_fq12_t(P, t_a, t_a) + t_ans = fq12_mul_fq12(P, t_ans, t_a) + t_a = fq12_mul_fq12(P, t_a, t_a) e >>= 1 return t_ans -def fq12_t_qi_pow(P, t_x, i): +def fq12_qi_pow(P, t_x, i): '''Calc qi_power on t_x tuple returning tuple''' global bls12381_q, frob_coeffs if P != bls12381_q: @@ -348,102 +331,101 @@ def fq12_t_qi_pow(P, t_x, i): i %= 12 if i == 0: return t_x - a, b, c, d, e, f = fq6_t_qi_pow(P, t_x[:6], i) - g, h, i, j, k, l = fq6_t_mul_fq6_t(P, - fq6_t_qi_pow(P, t_x[6:12], i), - frob_coeffs[12, i, 1]) + a, b, c, d, e, f = fq6_qi_pow(P, t_x[:6], i) + g, h, i, j, k, l = fq6_mul_fq6(P, fq6_qi_pow(P, t_x[6:12], i), + frob_coeffs[12, i, 1]) return (a, b, c, d, e, f, g, h, i, j, k, l) -def fq12_t_add_fq_int(P, t_a, m): +def fq12_add_fq(P, t_a, m): '''Add tuple t_a and int m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a - return ((a+m)%P, b, c, d, e, f, g, h, i, j, k, l) + return ((a+m) % P, b, c, d, e, f, g, h, i, j, k, l) -def fq12_t_add_fq2_t(P, t_a, t_m): +def fq12_add_fq2(P, t_a, t_m): '''Add tuple t_a and tuple t_m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n = t_m - return ((a+m)%P, (b+n)%P, c, d, e, f, g, h, i, j, k, l) + return ((a+m) % P, (b+n) % P, c, d, e, f, g, h, i, j, k, l) -def fq12_t_add_fq6_t(P, t_a, t_m): +def fq12_add_fq6(P, t_a, t_m): '''Add tuple t_a and tuple t_m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n, o, p, q, r = t_m - return ((a+m)%P, (b+n)%P, (c+o)%P, (d+p)%P, (e+q)%P, (f+r)%P, + return ((a+m) % P, (b+n) % P, (c+o) % P, (d+p) % P, (e+q) % P, (f+r) % P, g, h, i, j, k, l) -def fq12_t_add_fq12_t(P, t_a, t_m): +def fq12_add_fq12(P, t_a, t_m): '''Add tuple t_a and tuple t_m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n, o, p, q, r, s, t, u, v, w, x = t_m - return ((a+m)%P, (b+n)%P, (c+o)%P, (d+p)%P, (e+q)%P, (f+r)%P, - (g+s)%P, (h+t)%P, (i+u)%P, (j+v)%P, (k+w)%P, (l+x)%P) + return ((a+m) % P, (b+n) % P, (c+o) % P, (d+p) % P, (e+q) % P, (f+r) % P, + (g+s) % P, (h+t) % P, (i+u) % P, (j+v) % P, (k+w) % P, (l+x) % P) -def fq12_t_sub_fq_int(P, t_a, m): +def fq12_sub_fq(P, t_a, m): '''Sub int m from tuple t_a returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a - return ((a-m)%P, b, c, d, e, f, g, h, i, j, k, l) + return ((a-m) % P, b, c, d, e, f, g, h, i, j, k, l) -def fq_int_sub_fq12_t(P, a, t_m): +def fq_sub_fq12(P, a, t_m): '''Sub tuple t_m from int a returning tuple''' m, n, o, p, q, r, s, t, u, v, w, x = t_m - return ((a-m)%P, -n%P, -o%P, -p%P, -q%P, -r%P, - -s%P, -t%P, -u%P, -v%P, -w%P, -x%P) + return ((a-m) % P, -n % P, -o % P, -p % P, -q % P, -r % P, + -s % P, -t % P, -u % P, -v % P, -w % P, -x % P) -def fq12_t_sub_fq2_t(P, t_a, t_m): +def fq12_sub_fq2(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n = t_m - return ((a-m)%P, (b-n)%P, c, d, e, f, g, h, i, j, k, l) + return ((a-m) % P, (b-n) % P, c, d, e, f, g, h, i, j, k, l) -def fq2_t_sub_fq12_t(P, t_a, t_m): +def fq2_sub_fq12(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b = t_a m, n, o, p, q, r, s, t, u, v, w, x = t_m - return ((a-m)%P, (b-n)%P, -o%P, -p%P, -q%P, -r%P, - -s%P, -t%P, -u%P, -v%P, -w%P, -x%P) + return ((a-m) % P, (b-n) % P, -o % P, -p % P, -q % P, -r % P, + -s % P, -t % P, -u % P, -v % P, -w % P, -x % P) -def fq12_t_sub_fq6_t(P, t_a, t_m): +def fq12_sub_fq6(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n, o, p, q, r = t_m - return ((a-m)%P, (b-n)%P, (c-o)%P, (d-p)%P, (e-q)%P, (f-r)%P, + return ((a-m) % P, (b-n) % P, (c-o) % P, (d-p) % P, (e-q) % P, (f-r) % P, g, h, i, j, k, l) -def fq6_t_sub_fq12_t(P, t_a, t_m): +def fq6_sub_fq12(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b, c, d, e, f = t_a m, n, o, p, q, r, s, t, u, v, w, x = t_m - return ((a-m)%P, (b-n)%P, (c-o)%P, (d-p)%P, (e-q)%P, (f-r)%P, - -s%P, -t%P, -u%P, -v%P, -w%P, -x%P) + return ((a-m) % P, (b-n) % P, (c-o) % P, (d-p) % P, (e-q) % P, (f-r) % P, + -s % P, -t % P, -u % P, -v % P, -w % P, -x % P) -def fq12_t_sub_fq12_t(P, t_a, t_m): +def fq12_sub_fq12(P, t_a, t_m): '''Sub tuple t_m from tuple t_a returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n, o, p, q, r, s, t, u, v, w, x = t_m - return ((a-m)%P, (b-n)%P, (c-o)%P, (d-p)%P, (e-q)%P, (f-r)%P, - (g-s)%P, (h-t)%P, (i-u)%P, (j-v)%P, (k-w)%P, (l-x)%P) + return ((a-m) % P, (b-n) % P, (c-o) % P, (d-p) % P, (e-q) % P, (f-r) % P, + (g-s) % P, (h-t) % P, (i-u) % P, (j-v) % P, (k-w) % P, (l-x) % P) -def fq12_t_mul_fq_int(P, t_a, m): +def fq12_mul_fq(P, t_a, m): '''Multiple tuple t_a on int m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a - return (a*m%P, b*m%P, c*m%P, d*m%P, e*m%P, f*m%P, - g*m%P, h*m%P, i*m%P, j*m%P, k*m%P, l*m%P) + return (a*m % P, b*m % P, c*m % P, d*m % P, e*m % P, f*m % P, + g*m % P, h*m % P, i*m % P, j*m % P, k*m % P, l*m % P) -def fq12_t_mul_fq2_t(P, t_a, t_m): +def fq12_mul_fq2(P, t_a, t_m): '''Multiple tuple t_a on tuple t_m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n = t_m @@ -454,11 +436,11 @@ def fq12_t_mul_fq2_t(P, t_a, t_m): mul_g = g*m + h*n*fq2r; mul_h = g*n + h*m mul_i = i*m + j*n*fq2r; mul_j = i*n + j*m mul_k = k*m + l*n*fq2r; mul_l = k*n + l*m - return (mul_a%P, mul_b%P, mul_c%P, mul_d%P, mul_e%P, mul_f%P, - mul_g%P, mul_h%P, mul_i%P, mul_j%P, mul_k%P, mul_l%P) + return (mul_a % P, mul_b % P, mul_c % P, mul_d % P, mul_e % P, mul_f % P, + mul_g % P, mul_h % P, mul_i % P, mul_j % P, mul_k % P, mul_l % P) -def fq12_t_mul_fq6_t(P, t_a, t_m): +def fq12_mul_fq6(P, t_a, t_m): '''Multiple tuple t_a on tuple t_m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n, o, p, q, r = t_m @@ -485,11 +467,11 @@ def fq12_t_mul_fq6_t(P, t_a, t_m): mul_j = g*p + h*o + i*n + j*m + kq + lrfq2r + kr + lq mul_k = g*q + h*r*fq2r + i*o + j*p*fq2r + k*m + l*n*fq2r mul_l = g*r + h*q + i*p + j*o + k*n + l*m - return (mul_a%P, mul_b%P, mul_c%P, mul_d%P, mul_e%P, mul_f%P, - mul_g%P, mul_h%P, mul_i%P, mul_j%P, mul_k%P, mul_l%P) + return (mul_a % P, mul_b % P, mul_c % P, mul_d % P, mul_e % P, mul_f % P, + mul_g % P, mul_h % P, mul_i % P, mul_j % P, mul_k % P, mul_l % P) -def fq12_t_mul_fq12_t(P, t_a, t_m): +def fq12_mul_fq12(P, t_a, t_m): '''Multiple tuple t_a on tuple t_m returning tuple''' a, b, c, d, e, f, g, h, i, j, k, l = t_a m, n, o, p, q, r, s, t, u, v, w, x = t_m @@ -540,18 +522,18 @@ def fq12_t_mul_fq12_t(P, t_a, t_m): mul_k2 = g*q + h*r*fq2r + i*o + j*p*fq2r + k*m + l*n*fq2r mul_l1 = a*x + b*w + c*v + d*u + e*t + f*s mul_l2 = g*r + h*q + i*p + j*o + k*n + l*m - return ((mul_a1 + mul_a2)%P, (mul_b1 + mul_b2)%P, - (mul_c1 + mul_c2)%P, (mul_d1 + mul_d2)%P, - (mul_e1 + mul_e2)%P, (mul_f1 + mul_f2)%P, - (mul_g1 + mul_g2)%P, (mul_h1 + mul_h2)%P, - (mul_i1 + mul_i2)%P, (mul_j1 + mul_j2)%P, - (mul_k1 + mul_k2)%P, (mul_l1 + mul_l2)%P) + return ((mul_a1 + mul_a2) % P, (mul_b1 + mul_b2) % P, + (mul_c1 + mul_c2) % P, (mul_d1 + mul_d2) % P, + (mul_e1 + mul_e2) % P, (mul_f1 + mul_f2) % P, + (mul_g1 + mul_g2) % P, (mul_h1 + mul_h2) % P, + (mul_i1 + mul_i2) % P, (mul_j1 + mul_j2) % P, + (mul_k1 + mul_k2) % P, (mul_l1 + mul_l2) % P) # Frobenius coefficients for raising elements to q**i -th powers # These are specific to this given bls12381_q frob_coeffs = { - (2, 1, 1): -1%bls12381_q, + (2, 1, 1): -1 % bls12381_q, (6, 1, 1): (0, int('0x1a0111ea397fe699ec02408663d4de85aa0d85' '7d89759ad4897d29650fb85f9b409427eb4f49ff' diff --git a/bls_py/pairing.py b/bls_py/pairing.py index 8d0c6de..ac5519d 100644 --- a/bls_py/pairing.py +++ b/bls_py/pairing.py @@ -2,7 +2,7 @@ from . import bls12381 from .ec import untwist -from .fields import Fq12, FQ12_ONE_TUPLE, fq12_t_mul_fq12_t +from .fields import Fq12, FQ12_ONE_TUPLE, fq12_mul_fq12 # Struct for elliptic curve parameters @@ -68,14 +68,14 @@ def miller_loop(T, P, Q, ec=default_ec): for i in range(1, len(T_bits)): # Compute sloped line lrr lrr = double_line_eval(R, P, ec) - f = fq12_t_mul_fq12_t(ec.q, f, f) - f = fq12_t_mul_fq12_t(ec.q, f, lrr.ZT) + f = fq12_mul_fq12(ec.q, f, f) + f = fq12_mul_fq12(ec.q, f, lrr.ZT) R = 2 * R if T_bits[i] == 1: # Compute sloped line lrq lrq = add_line_eval(R, Q, P, ec) - f = fq12_t_mul_fq12_t(ec.q, f, lrq.ZT) + f = fq12_mul_fq12(ec.q, f, lrq.ZT) R = R + Q return Fq12(ec.q, f) @@ -86,7 +86,7 @@ def final_exponentiation(element, ec=default_ec): loop to a unique element of Fq12. """ if ec.k == 12: - ans = pow(element, (pow(ec.q,4) - pow(ec.q,2) + 1) // ec.n) + ans = pow(element, (pow(ec.q, 4) - pow(ec.q, 2) + 1) // ec.n) ans = ans.qi_power(2) * ans ans = ans.qi_power(6) / ans return ans @@ -115,7 +115,7 @@ def ate_pairing_multi(Ps, Qs, ec=default_ec): prod = FQ12_ONE_TUPLE for i in range(len(Qs)): ml_res = miller_loop(T, Ps[i], Qs[i], ec) - prod = fq12_t_mul_fq12_t(ec.q, prod, ml_res.ZT) + prod = fq12_mul_fq12(ec.q, prod, ml_res.ZT) return final_exponentiation(Fq12(ec.q, prod), ec)