-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElliptic Curve.py
66 lines (53 loc) · 1.51 KB
/
Elliptic Curve.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def add(P,Q,A,B,p):
"""Returns P+Q on the curve y^2=x^3+Ax+b over Fp."""
if P is None:
return Q
if Q is None:
return P
xP, yP = P
xQ, yQ = Q
if xP == xQ and (yP + yQ) % p == 0:
return None
m = 0
if xP != xQ:
m = ((yQ - yP) * pow(xQ - xP, -1, p)) % p
else:
if yP == -1 * yQ:
return None
else:
m = ((3 * xP**2 + A) * pow(2 * yP, -1, p)) % p
xR = (m**2 - xP - xQ) % p
yR = (m * (xP - xR) - yP) % p
return (xR,yR)
def dbl(P,A,B,p):
"""Returns 2P on the curve y^2=x^3+Ax+b over Fp."""
xP, yP = P
m = ((3 * xP**2 + A) % p * pow((2 * yP) % p, -1, p)) % p
x = (m**2 - 2 * xP) % p
y = (m * (xP - x) - yP) % p
return (x, y)
def neg(P,p):
"""Returns -P on the curve y^2=x^3+Ax+b over Fp."""
return (P[0], ((-1 * P[1]) % p))
def smul(n, P, A, B, p):
"""Returns nP on the curve y^2=x^3+Ax+b over Fp."""
if n == 0:
return None
Q = None
R = P
flag = False
if n < 0:
flag = True
n = abs(n)
# Used double add algorithm from
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#:~:text=Elliptic%20curve%20scalar%20multiplication%20is,form%20of%20an%20elliptic%20curve.
while n > 0:
if n % 2 == 1:
Q = add(Q, R, A, B, p)
if Q is None:
return None
R = dbl(R, A, B, p)
n = n // 2
if flag:
Q = neg(Q,p)
return Q