-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEllipticCurve.py
89 lines (76 loc) · 2.37 KB
/
EllipticCurve.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import math
class EllipticCurve:
def __init__(self, a, b, p, order=""):
"""
y^2 = x^3 + ax + b (mod p)
"""
self.a = a
self.b = b
self.p = p
if order == "":
self.order = self.hasse_theorem()
else:
self.order = order
def add_PQ(self, P, Q):
"""
P -> (x0,y0)
Q -> (x1,y1)
a -> Coeficiente da Equação Cartesiana da Curva Elíptica
p -> módulo primo
"""
O = (0,0) # Elemento Neutro
if P == (0, 0):
return Q
if Q == (0, 0):
return P
x1, y1 = P
x2, y2 = Q
if x1 == x2 and y2 == -y1:
return O
if P != Q:
m = ((y2 - y1) * pow(x2 - x1, -1, self.p)) % self.p
else:
m = ((3 * x1**2 + self.a) * pow(2 * y1,-1, self.p)) % self.p
x3 = (m**2 - x1 - x2) % self.p
y3 = (m * (x1 - x3) - y1) % self.p
return x3, y3
def encontra_nP(self, k, P):
"""
Double-and-Add : Algoritmo eficiente para computar multiplicação escalar de Curvas Elípticas
"""
result = (0, 0)
addend = P
while k > 0:
if k % 2 == 1:
result = self.add_PQ(result, addend)
addend = self.add_PQ(addend, addend)
k //= 2
return result
def scaling_part1(self, P, Q, a, p):
if P == (0, 0):
return Q
if Q == (0, 0):
return P
x1, y1 = P
x2, y2 = Q
if P != Q:
m = ((y2 - y1) * pow(x2 - x1, -1, p)) % p
else:
m = ((3 * x1**2 + a) * pow(2 * y1,-1, p)) % p
x3 = (m**2 - x1 - x2) % p
y3 = (m * (x1 - x3) - y1) % p
return x3, y3
def scaling_part2(self,k, P, p):
result = (0, 0)
addend = P
while k > 0:
if k % 2 == 1:
result = self.scaling_part1(result, addend, self.a, p)
addend = self.scaling_part1(addend, addend, self.a, p)
k //= 2
return result
def hasse_theorem(self):
return (self.p+1) + 2*math.sqrt(self.p)
def on_curve(self, P):
x,y = P[0], P[1]
return (y**2 - x**3 - self.a*x - self.b) % self.p == 0