-
Notifications
You must be signed in to change notification settings - Fork 0
/
bls.py
431 lines (338 loc) · 11.1 KB
/
bls.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
N = 0x1a0111ea397fe69a4b1ba7b6434bacd764774b84f38512bf6730d2a0f6b0f6241eabfffeb153ffffb9feffffffffaaab
R = 52435875175126190479447740508185965837690552500527637822603658699938581184513
class FP:
def __init__(self, value):
self.value = value % N
def __add__(self, other):
return FP(self.value + other.value)
def __mul__(self, other):
if isinstance(other, int):
return FP(self.value * other)
# global num_mul
# num_mul += 1
return FP(self.value * other.value)
def __rmul__(self, other):
return self * other
def __sub__(self, other):
return FP(self.value - other.value)
def __str__(self):
# return str(self.value)
return hex(self.value)
def __repr__(self):
return self.__str__()
def __eq__(self, other):
if isinstance(other, int):
return self.value == other
assert isinstance(other, self.__class__)
return self.value == other.value
def __ne__(self, other):
return not self == other
def __neg__(self):
return self.__class__(-self.value)
def __truediv__(self, other):
on = other.value if isinstance(other, FP) else other
assert isinstance(on, (int))
return FP(self.value * prime_field_inv(on, N))
@classmethod
def one(cls):
return cls(1)
@classmethod
def zero(cls):
return cls(0)
def __pow__(self, other):
o = self.__class__.one()
t = self
while other > 0:
if other & 1:
o = o * t
other >>= 1
t = t * t
return o
def inv(self):
n = N
a = self.value
if a == 0:
return 0
lm, hm = 1, 0
low, high = a % n, n
while low > 1:
r = high // low
nm, new = hm - lm * r, high - low * r
lm, low, hm, high = nm, new, lm, low
return FP(lm % n)
def deg(p):
d = len(p) - 1
while p[d] == 0 and d:
d -= 1
return d
# Extended euclidean algorithm to find modular inverses for
# integers
def prime_field_inv(a, n):
if a == 0:
return 0
lm, hm = 1, 0
low, high = a % n, n
while low > 1:
r = high // low
nm, new = hm - lm * r, high - low * r
lm, low, hm, high = nm, new, lm, low
return lm % n
def poly_rounded_div(a, b):
dega = deg(a)
degb = deg(b)
temp = [x for x in a]
o = [FP(0) for x in a]
for i in range(dega - degb, -1, -1):
o[i] = (o[i] + temp[degb + i] * FP(prime_field_inv(b[degb].value, N)))
for c in range(degb + 1):
temp[c + i] = (temp[c + i] - o[c])
return [x for x in o[:deg(o) + 1]]
class FQ2:
FIELD_COEFFS = {0: FP(1)}
DEGREE = 2
def __init__(self, coeffs):
assert len(coeffs) == self.DEGREE
self.coeffs = [c if isinstance(c, FP) else FP(c) for c in coeffs]
def mul(cls, a, b):
r = [FP(0) for i in range(0, cls.DEGREE * 2 - 1)]
for j in range(cls.DEGREE):
for i in range(cls.DEGREE):
r[i + j] = r[i + j] + a.coeffs[i] * b.coeffs[j]
for i in range(len(r) - 1, cls.DEGREE - 1, -1):
d = i - cls.DEGREE
for j in cls.FIELD_COEFFS.keys():
r[d + j] = r[d + j] - r[i] * cls.FIELD_COEFFS[j]
return FQ2(r[:cls.DEGREE])
# https://cr.yp.to/papers/m3-20010811-retypeset-20220327.pdf
def kmul(cls, a, b):
global red_mul_fq2
r = [FP(0) for i in range(0, cls.DEGREE * 2 - 1)]
t = a.coeffs[0] * b.coeffs[0]
u = a.coeffs[1] * b.coeffs[1]
r[0] = t
r[1] = (a.coeffs[0] + a.coeffs[1]) * (b.coeffs[0] + b.coeffs[1]) - u - t
r[2] = u
for i in range(len(r) - 1, cls.DEGREE - 1, -1):
d = i - cls.DEGREE
for j in cls.FIELD_COEFFS.keys():
# red_mul_fq2 = red_mul_fq2 + 1
r[d + j] = r[d + j] - r[i] * cls.FIELD_COEFFS[j]
return FQ2(r[:cls.DEGREE])
def inv(self):
FQ2_modulus_coeffs = [1, 0]
FQ2_modulus_coeffs = [FP(c) for c in FQ2_modulus_coeffs]
p12 = self.coeffs
degree = 2
lm, hm = [FP(1)] + [FP(0)] * degree, [FP(0)] * (degree + 1)
low, high = p12 + [FP(0)], FQ2_modulus_coeffs + [FP(1)]
while deg(low):
r = poly_rounded_div(high, low)
r += [FP(0)] * (degree + 1 - len(r))
nm = [x for x in hm]
new = [x for x in high]
# assert len(lm) == len(hm) == len(low) == len(high) == len(nm) == len(new) == self.degree + 1
for i in range(degree + 1):
for j in range(degree + 1 - i):
nm[i + j] -= lm[i] * r[j]
new[i + j] -= low[i] * r[j]
# nm = [x % N for x in nm]
# new = [x % N for x in new]
lm, low, hm, high = nm, new, lm, low
return FQ2([c / low[0] for c in lm[:degree]])
# return self.__class__(lm[:degree]) / low[0]
def __mul__(self, other):
if isinstance(other, (int)):
return self.__class__([c * other for c in self.coeffs])
if isinstance(other, (FP)):
return self.__class__([c * other.value for c in self.coeffs])
return self.kmul(self, other)
def __rmul__(self, other):
return self * other
def __add__(self, other):
return FQ2([self.coeffs[i] + other.coeffs[i] for i in range(self.DEGREE)])
def __sub__(self, other):
assert isinstance(other, FQ2)
return FQ2([(x - y) for x, y in zip(self.coeffs, other.coeffs)])
def __str__(self):
return str([c for c in self.coeffs])
def __repr__(self):
return self.__str__()
def __eq__(self, other):
assert isinstance(other, self.__class__)
return self.coeffs[0] == other.coeffs[0] and self.coeffs[1] == other.coeffs[1]
def __ne__(self, other):
return not self == other
def __neg__(self):
return self.__class__([-c for c in self.coeffs])
@classmethod
def one(cls):
return cls([1, 0])
@classmethod
def zero(cls):
return cls([0, 0])
def deg(self):
return 1 if self.coeffs[1] != 0 else 0
def __pow__(self, other):
o = self.__class__.one()
t = self
while other > 0:
if other & 1:
o = o * t
other >>= 1
t = t * t
return o
class FP:
def __init__(self, value):
self.value = value % N
def __add__(self, other):
return FP(self.value + other.value)
def __mul__(self, other):
if isinstance(other, int):
return FP(self.value * other)
# global num_mul
# num_mul += 1
return FP(self.value * other.value)
def __rmul__(self, other):
return self * other
def __sub__(self, other):
return FP(self.value - other.value)
def __str__(self):
# return str(self.value)
return hex(self.value)
def __repr__(self):
return self.__str__()
def __eq__(self, other):
if isinstance(other, int):
return self.value == other
assert isinstance(other, self.__class__)
return self.value == other.value
def __ne__(self, other):
return not self == other
def __neg__(self):
return self.__class__(-self.value)
def __truediv__(self, other):
on = other.value if isinstance(other, FP) else other
assert isinstance(on, (int))
return FP(self.value * prime_field_inv(on, N))
@classmethod
def one(cls):
return cls(1)
@classmethod
def zero(cls):
return cls(0)
def __pow__(self, other):
o = self.__class__.one()
t = self
while other > 0:
if other & 1:
o = o * t
other >>= 1
t = t * t
return o
def inv(self):
n = N
a = self.value
if a == 0:
return 0
lm, hm = 1, 0
low, high = a % n, n
while low > 1:
r = high // low
nm, new = hm - lm * r, high - low * r
lm, low, hm, high = nm, new, lm, low
return FP(lm % n)
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __str__(self):
return "x: " + str(self.x) + "\n" + "y: " + str(self.y) + "\n" + "z: " + str(self.z) + "\n"
def __repr__(self):
return self.__str__()
def __neg__(self):
return self.__class__(self.x, -self.y, self.z)
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
def to_affine(self):
z_inv = self.z.inv()
return Point(self.x * z_inv, self.y * z_inv, self.y.__class__.one())
def double(pt):
x, y, z = pt.x, pt.y, pt.z
W = x * x
W = W * 3
S = y * z
B = x * y * S
H = W * W - 8 * B
S_squared = S * S
newx = 2 * H * S
newy = W * (4 * B - H) - 8 * y * y * S_squared
newz = 8 * S * S_squared
return Point(newx, newy, newz)
def add(p1, p2):
one, zero = p1.x.__class__.one(), p1.x.__class__.zero()
if p1.z == zero or p2.z == zero:
return p1 if p2.z == zero else p2
x1, y1, z1 = p1.x, p1.y, p1.z
x2, y2, z2 = p2.x, p2.y, p2.z
U1 = y2 * z1
U2 = y1 * z2
V1 = x2 * z1
V2 = x1 * z2
if V1 == V2 and U1 == U2:
return double(p1)
elif V1 == V2:
return Point(one, one, zero)
U = U1 - U2
V = V1 - V2
V_squared = V * V
V_squared_times_V2 = V_squared * V2
V_cubed = V * V_squared
W = z1 * z2
A = U * U * W - V_cubed - 2 * V_squared_times_V2
newx = V * A
newy = U * (V_squared_times_V2 - A) - V_cubed * U2
newz = V_cubed * W
return Point(newx, newy, newz)
def multiply(pt, n):
if n == 0:
return Point(pt[0].__class__.one(), pt[0].__class__.one(), pt[0].__class__.zero())
elif n == 1:
return pt
elif not n % 2:
return multiply(double(pt), n // 2)
else:
return add(multiply(double(pt), int(n // 2)), pt)
def is_on_curve(p: Point):
return (p.y ** 2) == (p.x ** 3) + FP(4)
def is_on_twisted_curve(p: Point):
return (p.y ** 2) == (p.x ** 3) + 4 * FQ2([1,1])
assert (N % 4 == 3)
assert ((N + 1) % 4 == 0)
def sqrt(v: FP):
if v ** ((N - 1) // 2) == 1:
return v ** ((N + 1) // 4)
else:
return False
assert(sqrt(FP(N - 1)) == False)
assert(sqrt(FP(N // 2)) * sqrt(FP(N // 2)) == FP(N // 2))
def sqrt_fq2(a: FQ2):
a1 = (a ** ((N - 3) // 4))
alpha = a1 * (a1 * a)
a0 = (alpha ** N) * alpha
if a0 == FQ2([-1, 0]):
return False
x0 = a1 * a
if alpha == FQ2([-1, 0]):
return FQ2([0, 1]) * x0
else:
b = (FQ2.one() + alpha) ** ((N - 1) // 2)
return b * x0
def gen_twisted_curve_point(x: FQ2):
y2 = x ** 3 + 4 * FQ2([1,1])
y = sqrt_fq2(y2)
return Point(x, y, FQ2.one()) if y else Point(FQ2.zero(), FQ2.zero(), FQ2.zero())
def gen_curve_point(x: FP):
y2 = x ** 3 + FP(4)
y = sqrt(y2)
return Point(x, y, FP.one()) if y else Point(FP.zero(), FP.zero(), FP.zero())