-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve.py
438 lines (310 loc) · 10.9 KB
/
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
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
432
433
434
435
436
437
438
from __future__ import annotations
from dataclasses import dataclass
from .fields import Fp, Fr
import copy
A = Fp(-5)
d_num = Fp(138827208126141220649022263972958607803)
d_den = Fp(171449701953573178309673572579671231137)
d_den.inv(d_den)
D = d_num * d_den
# Bandersnatch using affine co-ordinates
@dataclass
class BandersnatchAffinePoint():
"""
An implementation of the bandersnatch curve point in affine coordinate.
The bandersnatch curve is a twisted-edwards curve
Bandersnatch paper: https://ia.cr/2021/1152
"""
x: Fp
y: Fp
def __init__(self, gx: Fp, gy: Fp) -> None:
if isinstance(gx, Fp) == False:
raise Exception(
"coordinates must have type basefield, please check the x coordinate")
if isinstance(gy, Fp) == False:
raise Exception(
"coordinates must have type basefield, please check the y coordinate")
self.x = gx
self.y = gy
if self.is_on_curve() == False:
raise Exception("point not on curve")
def generator():
# Generator point was taken from the bandersnatch paper
yTe = Fp(0x2a6c669eda123e0f157d8b50badcd586358cad81eee464605e3167b6cc974166)
xTe = Fp(0x29c132cc2c0b34c5743711777bbe42f32b79c022ad998465e1e71866a252ae18)
return BandersnatchAffinePoint(xTe, yTe)
def neg(self, p: 'BandersnatchAffinePoint'):
self.y = p.y
self.x = -p.x
def add(self, p: 'BandersnatchAffinePoint',
q: 'BandersnatchAffinePoint') -> 'BandersnatchAffinePoint':
"""
Formula:
(x1, y1) + (x2, y2) = (((x1 * y2) + (y1 * x2)/(1 + D * x1 * x2 * y1 * y2)) , ((y1 * y2) - (A * x1 * x2)/(1 - D * x1 * x2 * y1 * y2)))
Reference: "Twisted Edwards Curves Revisited" (https: // eprint.iacr.org/2008/522.pdf)
"""
x1 = p.x
y1 = p.y
x2 = q.x
y2 = q.y
one = Fp.one()
x1y2 = x1 * y2
y1x2 = y1 * x2
y1y2 = y1 * y2
ax1x2 = x1 * x2 * A
dx1x2y1y2 = x1y2 * y1x2 * D
x_num = x1y2 + y1x2
x_den = one + dx1x2y1y2
y_num = y1y2 - ax1x2
y_den = one - dx1x2y1y2
x = x_num / x_den
y = y_num / y_den
self.x = x
self.y = y
return self
def sub(self, p: 'BandersnatchAffinePoint',
q: 'BandersnatchAffinePoint') -> 'BandersnatchAffinePoint':
neg_q = -q
self.add(p, neg_q)
return self
def double(
self,
p: 'BandersnatchAffinePoint') -> 'BandersnatchAffinePoint':
"""
Formula:
2(x1, y1) = ((2(x1 * y1)) / ((y1 ** 2) + (A(x1 ** 2))) , ((((y1 ** 2) - A(x1 ** 2)) / (2 - (y1 ** 2) - A(x1 ** 2))))
Reference: "Twisted Edwards Curves Revisited" (https: // eprint.iacr.org/2008/522.pdf)
"""
x1 = p.x
y1 = p.y
two = Fp(2)
x1y1 = x1 * y1
x1y1_2 = two * x1y1
y1_exp_2 = y1.exp(2)
x1_exp_2 = x1.exp(2)
a_x1_exp_2 = A * x1_exp_2
x2 = x1y1_2 / (y1_exp_2 + a_x1_exp_2)
y2 = (y1_exp_2 - a_x1_exp_2) / (two - y1_exp_2 - a_x1_exp_2)
self.x = x2
self.y = y2
return self
def is_on_curve(self):
"""
To check if a point is on the curve, we check that the lhs of the equation below is equal to it's rhs:
A(x ** 2) + y ** 2 = 1 + D(x ** 2)(y ** 2)
"""
x_exp_2 = self.x.exp(2)
y_exp_2 = self.y.exp(2)
dxy_sq = x_exp_2 * y_exp_2 * D
a_x_sq = A * x_exp_2
one = Fp.one()
rhs = one + dxy_sq
lhs = a_x_sq + y_exp_2
return lhs == rhs
def to_bytes(self):
# This is here to test that we have the correct generator element
# banderwagon uses a different serialisation algorithm
mCompressedNegative = 0x80
mCompressedPositive = 0x00
x_bytes = bytearray(self.x.to_bytes())
mask = mCompressedPositive
if self.y.lexographically_largest():
mask = mCompressedNegative
x_bytes[31] |= mask
return bytes(x_bytes)
def from_bytes(self):
# This is not needed, see `to_bytes`
return NotImplemented
def dup(self) -> 'BandersnatchAffinePoint':
return copy.deepcopy(self)
def scalar_mul(self, point: 'BandersnatchAffinePoint',
scalar: Fr) -> 'BandersnatchAffinePoint':
"""
Using Double and Add : https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
"""
scalar_bits = format(scalar.value, 'b')
result = BandersnatchAffinePoint.identity()
temp = point.dup()
num_bits = len(scalar_bits)
for i in reversed(range(num_bits)):
if scalar_bits[i] == str(1):
result.add(result, temp)
temp.double(temp)
self.x = result.x
self.y = result.y
return self
def identity() -> 'BandersnatchAffinePoint':
zero = Fp.zero()
one = Fp.one()
return BandersnatchAffinePoint(zero, one)
def get_y_coordinate(x, return_positive_y):
one = Fp.one()
num = x * x
den = (num * D) - one
num = (num * A) - one
y = num / den # y^2
# This can only be None if the denominator is zero
if y is None:
return None
y.sqrt(y)
# This means that the square root does not exist
if y is None:
return None
is_largest = y.lexographically_largest()
if return_positive_y == is_largest:
return y
else:
return -y
# Method overloads
def __add__(self, other):
result = BandersnatchAffinePoint.identity()
result.add(self, other)
return result
def __sub__(self, other):
result = BandersnatchAffinePoint.identity()
result.sub(self, other)
return result
def __neg__(self):
result = BandersnatchAffinePoint.identity()
result.neg(self)
return result
def __mul__(self, other):
if isinstance(other, Fr) == False:
raise TypeError(
"[additive notation]: can only multiply a point by a scalar")
result = BandersnatchAffinePoint.generator()
result.scalar_mul(self, other)
return result
def __eq__(self, other):
if isinstance(other, BandersnatchAffinePoint):
return self.x == other.x and self.y == other.y
raise TypeError("can only check if a Point is equal to a Point")
@dataclass
class BandersnatchExtendedPoint():
x: Fp
y: Fp
t: Fp
z: Fp
def __init__(self, affine_point: BandersnatchAffinePoint) -> None:
self.x = affine_point.x
self.y = affine_point.y
self.t = affine_point.x * affine_point.y
self.z = Fp.one()
pass
def identity():
affine_point = BandersnatchAffinePoint.identity()
return BandersnatchExtendedPoint(affine_point)
def generator():
affine_point = BandersnatchAffinePoint.generator()
return BandersnatchExtendedPoint(affine_point)
def neg(self, p):
self.x = -p.x
self.y = p.y
self.t = -p.t
self.z = p.z
def is_zero(self):
"""
Identity is {x=0, y=1, t = 0, z =1}
The equivalence class is therefore is {x=0, y=k, t = 0, z=k} for all k where k!=0
"""
condition_1 = self.x.is_zero()
condition_2 = self.y == self.z
condition_3 = not self.y.is_zero()
condition_4 = self.t.is_zero()
return condition_1 and condition_2 and condition_3 and condition_4
def equal(p: BandersnatchExtendedPoint, q: BandersnatchExtendedPoint):
if p.is_zero():
return q.is_zero()
if q.is_zero():
return False
return (p.x * q.z == p.z * q.x) and (p.y * q.z == q.y * p.z)
def add(self, p, q):
# See "Twisted Edwards Curves Revisited" (https: // eprint.iacr.org/2008/522.pdf)
# by Huseyin Hisil, Kenneth Koon-Ho Wong, Gary Carter, and Ed Dawson
# 3.1 Unified Addition in E^e
x1 = p.x
y1 = p.y
t1 = p.t
z1 = p.z
x2 = q.x
y2 = q.y
t2 = q.t
z2 = q.z
a = x1 * x2
b = y1 * y2
c = D * t1 * t2
d = z1 * z2
h = b - (a * A)
e = (x1 + y1) * (x2 + y2) - a - b
f = d - c
g = d + c
self.x = e * f
self.y = g * h
self.t = e * h
self.z = f * g
return self
def sub(self, p, q):
neg_q = -q
self.add(p, neg_q)
return self
def double(self, p):
# TODO: can replace this with dedicated doubling formula
return self.add(p, p)
def scalar_mul(self, point, scalar: Fr):
# Same as AffinePoint's equivalent method
# using double and add :
# https://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication#Double-and-add
scalar_bits = format(scalar.value, 'b')
result = BandersnatchExtendedPoint.identity()
temp = point.dup()
num_bits = len(scalar_bits)
for i in reversed(range(num_bits)):
if scalar_bits[i] == str(1):
result.add(result, temp)
temp.double(temp)
self.x = result.x
self.y = result.y
self.t = result.t
self.z = result.z
return self
def to_affine(self):
if self.is_zero():
return BandersnatchAffinePoint.identity()
elif self.z.is_one():
return BandersnatchAffinePoint(self.x, self.y)
else:
assert self.z.is_zero() == False
z_inv = Fp.zero()
z_inv.inv(self.z)
x_aff = self.x * z_inv
y_aff = self.y * z_inv
return BandersnatchAffinePoint(x_aff, y_aff)
# Only used for testing purposes.
def to_bytes(self):
return self.to_affine().to_bytes()
def dup(self):
return copy.deepcopy(self)
# Method overloads
def __add__(self, other):
result = BandersnatchExtendedPoint.identity()
result.add(self, other)
return result
def __sub__(self, other):
result = BandersnatchExtendedPoint.identity()
result.sub(self, other)
return result
def __neg__(self):
result = BandersnatchExtendedPoint.identity()
result.neg(self)
return result
def __mul__(self, other):
if isinstance(other, Fr) == False:
raise TypeError(
"[additive notation]: can only multiply a point by a scalar")
result = BandersnatchExtendedPoint.generator()
result.scalar_mul(self, other)
return result
def __eq__(self, other):
if isinstance(other, BandersnatchExtendedPoint):
return BandersnatchExtendedPoint.equal(self, other)
raise TypeError("can only check if a Point is equal to a Point")