-
Notifications
You must be signed in to change notification settings - Fork 2
/
ECTools.py
329 lines (269 loc) · 11.5 KB
/
ECTools.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import numpy as np
import matplotlib.pyplot as plt
class Point():
def __init__(self,x,y):
"""
Initializes a new instance of Point class.
Args:
x (int): The value to assign to the x instance variable.
y (int): The value to assign to the y instance variable.
"""
self.x = x
self.y = y
def isInfinity(self):
return False
def isValid(self,group,curve):
return True if self.isInfinity() else (self.y**2 % group.p) == ((self.x ** 3 + curve.a * self.x + curve.b) % group.p)
def __eq__(self, __value: object) -> bool:
return self.x == __value.x and self.y == __value.y
def __str__(self):
return f"({self.x},{self.y})"
class PointAtInfinity(Point):
def __init__(self):
"""
Initializes a new instance of Point class for point at infinity.
"""
self.x = None
self.y = None
def isInfinity(self):
return True
def __str__(self):
return f"(Inf,Inf)"
class ECPoint():
def __init__(self, point,group,curve, k = 1):
"""
Initializes an instance of the class with a valid point, group, curve and optional k value.
Args:
point: A valid point.
group: The group to which the point belongs.
curve: The curve on which the point resides.
k (optional): An integer representing the value of k. Defaults to 1.
Raises:
Exception: If the given point is invalid.
Returns:
None
"""
if not point.isValid(group,curve):
raise Exception("Invalid point")
self.point = point
self.k = k
def isInfinity(self):
return (self.k == 0)
def __eq__(self, __value: object) -> bool:
return self.point == __value.point
def __str__(self):
return f"{self.point}.{self.k}"
class ECPointInfinity(ECPoint):
def __init__(self):
"""
Initializes a new instance of ECPoint class for point at infinity.
"""
self.point = PointAtInfinity()
self.k = 0
class ECCurve():
def __init__(self,a,b):
"""
Initialize a new instance of ECCurve with representation y^2 = x^3 + a*x + b with the given values.
Args:
a (any): The value to assign to `self.a`.
b (any): The value to assign to `self.b`.
"""
self.a = a
self.b = b
def ec_add(self,P,Q,group):
if P == ECPointInfinity():
return Q
elif Q == ECPointInfinity():
return P
if P.point.x == Q.point.x:
if P.point.y != Q.point.y:
return ECPointInfinity()
elif P.point.y == 0:
return ECPointInfinity()
else:
t = (3*P.point.x**2+self.a)*pow(2 * P.point.y,group.p-2,group.p) % group.p
else:
t = (Q.point.y - P.point.y) * pow(Q.point.x - P.point.x, group.p-2, group.p) % group.p
x = (t**2 - P.point.x - Q.point.x) % group.p
y = (t*(P.point.x - x) - P.point.y) % group.p
return ECPoint(Point(x,y),group,self,P.k+Q.k)
def ec_neg(self,P,group):
return ECPoint(Point(P.point.x,-P.point.y % group.p),group,self,-P.k)
def ec_sub(self,P,Q,group):
return self.ec_add(P,self.ec_neg(Q,group),group)
def __str__(self):
if self.a == 0 and self.b != 0:
return f"ECCurve : y^2 = x^3 + {self.b}"
elif self.b == 0 and self.a != 0:
return f"ECCurve : y^2 = x^3 + {self.a}"
elif self.a == 0 and self.b == 0:
return f"ECCurve : y^2 = x^3"
else:
return f"ECCurve : y^2 = x^3 + {self.a}x + {self.b}"
def order(self,group,origin):
return self.get_generated_points(group,origin)[1]
def get_generated_points(self,group,origin):
"""
Compute the list of points generated by repeatedly adding the given origin
point to itself using the given group's elliptic curve addition operation.
:param self: the current instance of the class
:param group: the elliptic curve group to use for the operation
:param origin: the starting point to generate more points from
:return: a tuple containing the list of generated points and its length
"""
P = origin
generatedPoints = list()
generatedPoints.append(ECPointInfinity())
while P not in generatedPoints and P != ECPointInfinity():
generatedPoints.append(P)
P = self.ec_add(P,origin,group)
return generatedPoints,len(generatedPoints)
def get_valid_points(self,group):
"""
Computes the valid points on an elliptic curve defined by the parameters 'a', 'b'
and the modulus 'p', given a group of points.
:param self: A reference to the instance of the class containing the function.
:param group: A group of points on the elliptic curve.
:return: A list containing the valid points on the elliptic curve.
The function starts by creating an empty list and appending the point at infinity.
Then, for each x-coordinate in the range [0, p), it computes the corresponding y-coordinate
using the elliptic curve equation. If the y-coordinate is zero, it adds the point (x,0) to the list.
Otherwise, it searches for a modular square root of the y-coordinate modulo p by iterating over
the range [1, p) and checking if y^2 is congruent to the y-coordinate modulo p. If a modular square root
is found, it adds the points (x,y) and (x,p-y) to the list. Finally, it returns the list of valid points.
"""
ret = list()
ret.append(PointAtInfinity())
for x in range(0,group.p):
ysq = (x ** 3 + self.a * x + self.b) % group.p
if ysq == 0:
ret.append(Point(x,0))
else:
# Probably not the smartest way to get modular sqrt ...
for y in range(1, group.p):
if y*y % group.p == ysq:
ret.append(Point(x,y))
ret.append(Point(x,group.p-y))
break
return ret
class ECGroup():
def __init__(self, p):
"""
Initializes a new instance of the class with the given prime number p.
Raises:
- Exception: if p is not prime. p must be prime as we use a shortcut for modular maths:
pow(a, p-2, p).
"""
self.p = p
if not self.is_prime(p):
raise Exception("p is not prime") # p must be prime as we use some shortcut for modular maths : pow(a,p-2,p)
def is_prime(self,n):
if n < 2:
return False
if n == 2:
return True
for i in range(2,int(n**0.5)+1):
if n % i == 0:
return False
return True
def __str__(self):
return f"ECGroup : prime = {self.p}"
class ECFactory():
def __init__(self):
pass
def print_infos(self,group,curve):
print(group)
print(curve)
def print_valid_points(self,group,curve):
"""
Print and return the valid points on a given curve for a given group.
:param group: the group of points to consider
:param curve: the curve to search for valid points
:return: a list of valid points on the curve for the group
:rtype: list
"""
pointList = curve.get_valid_points(group)
print(f"{len(pointList)} valid points : ",end="")
print(*pointList)
return pointList
def print_all_groups(self,group,curve):
"""
Prints information about the given group and curve.
Then, prints the valid points in the group.
Finally, for each valid point, calculates the order and prints the origin,
order, and generated sequence.
Args:
group: The group to print information about.
curve: The curve to print information about.
"""
self.print_infos(group,curve)
pointList = self.print_valid_points(group,curve)
for item in pointList:
origin = ECPoint(item,group,curve)
result,order = curve.get_generated_points(group,origin)
print("origin : "+str(item)+" / order : " + str(order)+ " / sequence : ",end="")
print(*result)
def print_group(self,group,curve,origin):
"""
Prints out information about the given group and curve with respect to the specified origin.
Args:
group: The group to print information about.
curve: The curve to print information about.
origin: The origin to use for generating points.
Returns:
None
Prints out the following information:
- Information about the group and curve using self.print_infos().
- Valid points for the group and curve using self.print_valid_points().
- The generated points using curve.get_generated_points() with the specified group and origin.
- The origin, order, and sequence of the generated points.
"""
self.print_infos(group,curve)
self.print_valid_points(group,curve)
org = ECPoint(origin,group,curve)
result,order = curve.get_generated_points(group,org)
print("origin : "+str(org)+" / order : " + str(order)+ " / sequence : ",end="")
print(*result)
def plot_group(self,group,curve,origin):
org = ECPoint(origin,group,curve)
result,order = curve.get_generated_points(group,org)
xy = list()
for item in result:
if not item.isInfinity():
xy.append([item.point.x,item.point.y,item.k])
data = np.array(xy)
x, y, k = data.T
plt.scatter(x,y) # If you just need points
plt.xlabel("X")
plt.ylabel("Y",rotation='horizontal')
plt.title(str(curve)+" ["+str(group.p)+"] with origin "+str(origin)+" : order is "+str(order))
# https://queirozf.com/entries/add-labels-and-text-to-matplotlib-plots-annotation-examples
for x,y,k in zip(x,y,k):
label = "1 Origin" if (k==1) else "{:d}".format(k)
plt.annotate(label, # this is the text
(x,y), # these are the coordinates to position the label
textcoords="offset points", # how to position the text
xytext=(0,10), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
if (k != 1):
n = 10*((x-ox)**2+(y-oy)**2)**0.5
if len(xy) % 2 == 1: # res = factory.print_group(ECGroup(11),ECCurve(0,7),Point(4,4))
if k < (len(xy)+1)//2:
color = 'b'
elif k > (len(xy)+1)//2 + 1:
color = 'g'
else:
color = 'r'
else:
if k < (len(xy)+1)//2 + 1: # res = factory.print_group(ECGroup(31),ECCurve(0,7),Point(25,16))
color = 'b'
elif k > (len(xy)+1)//2 + 1:
color = 'g'
else:
color = 'r'
plt.arrow((x+ox)/2,(y+oy)/2,(x-ox)/n,(y-oy)/n, shape='full', lw=1, length_includes_head=True, head_width=0.1, color = color)
plt.plot([ox, x],[oy, y], color = color)
ox = x
oy = y
plt.grid()
plt.show()