I am trying to implement the "double and add" algorithm to quickly multiply points on an elliptic curve in Python (3, please).
Based off this previous answer (about addition and doubling),
Elliptic curve point addition over a finite field in Python
the Wikipedia page,
https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication
and my textbook (Information Security, by Mark Stamp), I came up with the following code:
def point_add(N_x, N_y, Q_x, Q_y, p):
m = (Q_y - N_y) * pow((Q_x-N_x), p-2, p)
ret_x = (m ** 2 - N_x - Q_x) % p
ret_y = (m*(N_x - ret_x) - N_y) % p
return ret_x, ret_y
def point_double(N_x, N_y, a, p):
m = (3*(N_x ** 2)+a) * pow(2*N_y, p-2, p)
ret_x = (m ** 2 - N_x - Q_x) % p
ret_y = (m*(N_x - ret_x) - N_y) % p
return ret_x, ret_y
multiplier_A = 44
multiplier_B = 57
a = 10
b = -21
p = 41
N_x = 3
N_y = 6
Q_x = 0
Q_y = 0
multiplier_A = list(bin(multiplier_A)[2:])
multiplier_B = list(bin(multiplier_B)[2:])
for x_a in multiplier_A:
if x_a == '1':
Q_x, Q_y = point_add(N_x, N_y, Q_x, Q_y, p)
N_x, N_y = point_double(N_x, N_y, a, p)
print(Q_x, Q_y)
Which sure enough returns a wrong result.
Where do I go wrong? A new implementation from scratch is also more than welcome.
UPDATE
I want to multiply the point (3,6) times multiplier_A
and multiplier_B
. For now I'm trying to only do multiplier_A(3,6)
.
a
, b
, and p
are the elliptic curve paramethers