Skip to content

Commit

Permalink
rename tuples operations
Browse files Browse the repository at this point in the history
  • Loading branch information
zebra-lucky committed Jul 25, 2019
1 parent b24d0a5 commit 3b1f306
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 354 deletions.
128 changes: 62 additions & 66 deletions bls_py/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)


Expand Down
Loading

0 comments on commit 3b1f306

Please sign in to comment.