I have an elliptice curve in the form
y² = x³ + ax + b (mod p)
And I have a multiplication algortihm which uses only x and z coordinate
How can I recover the Y coordinate ?
I tried to use the curve equation, but because of sqrt_mod, there's 2 possibles solutions and I don't know which one to choose
def xDBLADD(P, Q, PQ):
(X1, Z1), (X2, Z2), (X3, Z3) = PQ, P, Q
X4 = (X2 ** 2 - a * Z2 ** 2) ** 2 - 8 * b * X2 * Z2 ** 3
Z4 = 4 * (X2 * Z2 * (X2 ** 2 + a * Z2 ** 2) + b * Z2 ** 4)
X5 = Z1 * ((X2 * X3 - a * Z2 * Z3) ** 2 - 4 * b * Z2 * Z3 * (X2 * Z3 + X3 * Z2))
Z5 = X1 * (X2 * Z3 - X3 * Z2) ** 2
X4, Z4, X5, Z5 = (c % q for c in (X4, Z4, X5, Z5))
return (X4, Z4), (X5, Z5)
def xMUL(P, k) -> int: # use xz coordinate
Q, R = (1, 0), P
for i in reversed(range(k.bit_length() + 1)):
if k >> i & 1:
R, Q = Q, R
Q, R = xDBLADD(Q, R, P)
if k >> i & 1:
R, Q = Q, R
return Q[0]
def mul(P, k):
Pz = (P[0], 1)
Qz = xMUL(Pz, k)
return Qz[0] * pow(Qz[1], -1, q) % q
Note: a similar question has been already posted here, but for Montgomery curve, so the equation is not the same