forked from cypherstack/spark
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dumb25519.py
600 lines (508 loc) · 14.1 KB
/
dumb25519.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
# Dumb25519: a stupid implementation of ed25519
#
# Use this code only for prototyping
# -- putting this code into production would be dumb
# -- assuming this code is secure would also be dumb
import secrets
from hashlib import blake2s
# Curve parameters
q = 2**255 - 19
l = 2**252 + 27742317777372353535851937790883648493
cofactor = 8
b = 256 # bit length
# Internal helper methods
def exponent(b,e,m):
return pow(b,e,m)
def invert(x,p):
# Assumes `p` is prime
return exponent(x,p-2,p)
def xfromy(y):
temp = (y*y-1) * invert(d*y*y+1,q)
x = exponent(temp,(q+3)//8,q)
if (x*x - temp) % q != 0:
x = (x*I) % q
if x % 2 != 0:
x = q-x
return x
def bit(h,i):
return (int(h[i//8]) >> (i%8)) & 1
d = -121665 * invert(121666,q)
I = exponent(2,(q-1)//4,q)
# An element of the main subgroup scalar field
class Scalar:
def __init__(self,x):
# Generated from an integer value
if isinstance(x,int):
self.x = x % l
# Generated from a hex representation
elif isinstance(x,str):
try:
x = bytes.fromhex(x)
self.x = sum(2**i * bit(x,i) for i in range(0,b)) % l
except:
raise TypeError
else:
raise TypeError
# Multiplicative inversion, with an option to let 1/0 = 0 if you're into that
def invert(self,allow_zero=False):
if self.x == 0:
if allow_zero:
return Scalar(0)
else:
raise ZeroDivisionError
return Scalar(invert(self.x,l))
# Addition
def __add__(self,y):
if isinstance(y,Scalar):
return Scalar(self.x + y.x)
return NotImplemented
# Subtraction
def __sub__(self,y):
if isinstance(y,Scalar):
return Scalar(self.x - y.x)
return NotImplemented
# Multiplication (possibly by an integer)
def __mul__(self,y):
if isinstance(y,int):
return Scalar(self.x * y)
if isinstance(y,Scalar):
return Scalar(self.x * y.x)
return NotImplemented
def __rmul__(self,y):
if isinstance(y,int):
return self*y
return NotImplemented
# Truncated division (possibly by a positive integer)
def __truediv__(self,y):
if isinstance(y,int) and y >= 0:
return Scalar(self.x // y)
if isinstance(y,Scalar):
return Scalar(self.x // y.x)
raise NotImplemented
# Integer exponentiation
def __pow__(self,y):
if isinstance(y,int) and y >= 0:
return Scalar(self.x**y)
return NotImplemented
# Equality
def __eq__(self,y):
if isinstance(y,Scalar):
return self.x == y.x
raise TypeError
# Inequality
def __ne__(self,y):
if isinstance(y,Scalar):
return self.x != y.x
raise TypeError
# Less-than comparison (does not account for overflow)
def __lt__(self,y):
if isinstance(y,Scalar):
return self.x < y.x
raise TypeError
# Greater-than comparison (does not account for overflow)
def __gt__(self,y):
if isinstance(y,Scalar):
return self.x > y.x
raise TypeError
# Less-than-or-equal comparison (does not account for overflow)
def __le__(self,y):
if isinstance(y,Scalar):
return self.x <= y.x
raise TypeError
# Greater-than-or-equal comparison (does not account for overflow)
def __ge__(self,y):
if isinstance(y,Scalar):
return self.x >= y.x
raise TypeError
# Hex representation
def __repr__(self):
bits = [(self.x >> i) & 1 for i in range(b)]
return bytes.hex(bytes([sum([bits[i*8+j] << j for j in range(8)]) for i in range(b//8)]))
# Return underlying integer
def __int__(self):
return self.x
# Modulus (possibly by an integer)
def __mod__(self,mod):
if isinstance(mod,int) and mod > 0:
return Scalar(self.x % mod)
if isinstance(mod,Scalar) and mod != Scalar(0):
return Scalar(self.x % mod.x)
return NotImplemented
# Negation
def __neg__(self):
return Scalar(-self.x)
# An element of the curve group
class Point:
def __init__(self,x,y=None):
# Generated from integer values
if isinstance(x,int) and isinstance(y,int) and y is not None:
self.x = x
self.y = y
if not self.on_curve():
raise ValueError
# Generated from a hex representation
elif isinstance(x,str) and y is None:
try:
x = bytes.fromhex(x)
self.y = sum(2**i * bit(x,i) for i in range(0,b-1))
self.x = xfromy(self.y)
if self.x & 1 != bit(x,b-1):
self.x = q - self.x
except:
raise TypeError
if not self.on_curve():
raise ValueError
else:
raise TypeError
# Equality
def __eq__(self,Q):
if isinstance(Q,Point):
return self.x == Q.x and self.y == Q.y
raise TypeError
# Inequality
def __ne__(self,Q):
if isinstance(Q,Point):
return self.x != Q.x or self.y != Q.y
raise TypeError
# Addition
def __add__(self,Q):
if isinstance(Q,Point):
x1 = self.x
y1 = self.y
x2 = Q.x
y2 = Q.y
x3 = (x1*y2+x2*y1) * invert(1+d*x1*x2*y1*y2,q)
y3 = (y1*y2+x1*x2) * invert(1-d*x1*x2*y1*y2,q)
return Point(x3 % q, y3 % q)
return NotImplemented
# Subtraction
def __sub__(self,Q):
if isinstance(Q,Point):
x1 = self.x
y1 = self.y
x2 = -Q.x
y2 = Q.y
x3 = (x1*y2+x2*y1) * invert(1+d*x1*x2*y1*y2,q)
y3 = (y1*y2+x1*x2) * invert(1-d*x1*x2*y1*y2,q)
return Point(x3 % q, y3 % q)
return NotImplemented
# Multiplication
def __mul__(self,y):
# Point-Scalar: scalar multiplication
if isinstance(y,Scalar):
if y == Scalar(0):
return Point(0,1)
Q = self.__mul__(y/Scalar(2))
Q = Q.__add__(Q)
if y.x & 1:
Q = self.__add__(Q)
return Q
return NotImplemented
def __rmul__(self,y):
# Scalar-Point
if isinstance(y,Scalar):
return self*y
return NotImplemented
# Hex representation
def __repr__(self):
bits = [(self.y >> i) & 1 for i in range(b-1)] + [self.x & 1]
return bytes.hex(bytes([sum([bits[i*8+j] << j for j in range(8)]) for i in range(b//8)]))
# Curve membership (not main subgroup!)
def on_curve(self):
x = self.x
y = self.y
return (-x*x + y*y - 1 - d*x*x*y*y) % q == 0
# Negation
def __neg__(self):
return Z - self
# A vector of Points with superpowers
class PointVector:
def __init__(self,points=None):
if points is None:
points = []
for point in points:
if not isinstance(point,Point):
raise TypeError
self.points = points
# Equality
def __eq__(self,W):
if isinstance(W,PointVector):
return self.points == W.points
raise TypeError
# Inequality
def __ne__(self,W):
if isinstance(W,PointVector):
return self.points != W.points
raise TypeError
# Addition
def __add__(self,W):
if isinstance(W,PointVector) and len(self.points) == len(W.points):
return PointVector([self.points[i] + W.points[i] for i in range(len(self.points))])
return NotImplemented
# Subtraction
def __sub__(self,W):
if isinstance(W,PointVector) and len(self.points) == len(W.points):
return PointVector([self.points[i] - W.points[i] for i in range(len(self.points))])
return NotImplemented
# Multiplication
def __mul__(self,s):
# PointVector-Scalar: componentwise Point-Scalar multiplication
if isinstance(s,Scalar):
return PointVector([self.points[i]*s for i in range(len(self.points))])
# PointVector-ScalarVector: Hadamard product
if isinstance(s,ScalarVector) and len(self.points) == len(s.scalars):
return PointVector([s[i]*self[i] for i in range(len(self))])
return NotImplemented
def __rmul__(self,s):
# Scalar-PointVector
if isinstance(s,Scalar):
return self*s
# ScalarVector-PointVector
if isinstance(s,ScalarVector):
return self*s
return NotImplemented
# Multiscalar multiplication
def __pow__(self,s):
if isinstance(s,ScalarVector) and len(self.points) == len(s.scalars):
return multiexp(s,self)
return NotImplemented
# Length
def __len__(self):
return len(self.points)
# Get slice
def __getitem__(self,i):
if not isinstance(i,slice):
return self.points[i]
return PointVector(self.points[i])
# Set at index
def __setitem__(self,i,P):
if isinstance(P,Point):
self.points[i] = P
else:
raise TypeError
# Append
def append(self,item):
if isinstance(item,Point):
self.points.append(item)
else:
raise TypeError
# Extend
def extend(self,items):
if isinstance(items,PointVector):
for item in items.points:
self.points.append(item)
else:
raise TypeError
# Hex representation of underlying Points
def __repr__(self):
return repr(self.points)
# Negation
def __neg__(self):
return PointVector([-P for P in self.points])
# A vector of Scalars with superpowers
class ScalarVector:
def __init__(self,scalars=None):
if scalars is None:
scalars = []
for scalar in scalars:
if not isinstance(scalar,Scalar):
raise TypeError
self.scalars = scalars
# Equality
def __eq__(self,s):
if isinstance(s,ScalarVector):
return self.scalars == s.scalars
raise TypeError
# Inequality
def __ne__(self,s):
if isinstance(s,ScalarVector):
return self.scalars != s.scalars
raise TypeError
# Addition
def __add__(self,s):
if isinstance(s,ScalarVector) and len(self.scalars) == len(s.scalars):
return ScalarVector([self.scalars[i] + s.scalars[i] for i in range(len(self.scalars))])
return NotImplemented
# Subtraction
def __sub__(self,s):
if isinstance(s,ScalarVector) and len(self.scalars) == len(s.scalars):
return ScalarVector([self.scalars[i] - s.scalars[i] for i in range(len(self.scalars))])
return NotImplemented
# Multiplication
def __mul__(self,s):
# ScalarVector-Scalar: componentwise Scalar-Scalar multiplication
if isinstance(s,Scalar):
return ScalarVector([self.scalars[i]*s for i in range(len(self.scalars))])
# ScalarVector-ScalarVector: Hadamard product
if isinstance(s,ScalarVector) and len(self.scalars) == len(s.scalars):
return ScalarVector([self.scalars[i]*s.scalars[i] for i in range(len(self.scalars))])
return NotImplemented
def __rmul__(self,s):
# Scalar-ScalarVector
if isinstance(s,Scalar):
return self*s
return NotImplemented
# Sum of all Scalars
def sum(self):
r = Scalar(0)
for i in range(len(self.scalars)):
r += self.scalars[i]
return r
# Inner product and multiscalar multiplication
def __pow__(self,s):
# ScalarVector**ScalarVector: inner product
if isinstance(s,ScalarVector) and len(self.scalars) == len(s.scalars):
r = Scalar(0)
for i in range(len(self.scalars)):
r += self.scalars[i]*s.scalars[i]
return r
# ScalarVector**PointVector: multiscalar multiplication
if isinstance(s,PointVector):
return s**self
return NotImplemented
# Length
def __len__(self):
return len(self.scalars)
# Get slice
def __getitem__(self,i):
if not isinstance(i,slice):
return self.scalars[i]
return ScalarVector(self.scalars[i])
# Set at index
def __setitem__(self,i,s):
if isinstance(s,Scalar):
self.scalars[i] = s
else:
raise TypeError
# Append
def append(self,item):
if isinstance(item,Scalar):
self.scalars.append(item)
else:
raise TypeError
# Extend
def extend(self,items):
if isinstance(items,ScalarVector):
for item in items.scalars:
self.scalars.append(item)
else:
raise TypeError
# Hex representation of underlying Scalars
def __repr__(self):
return repr(self.scalars)
# Componentwise inversion (possibly with zero)
def invert(self,allow_zero=False):
# If we allow zero, the efficient method doesn't work
if allow_zero:
return ScalarVector([s.invert(allow_zero=True) for s in self.scalars])
# Don't allow zero
inputs = self.scalars[:]
n = len(inputs)
scratch = [Scalar(1)]*n
acc = Scalar(1)
for i in range(n):
if inputs[i] == Scalar(0):
raise ZeroDivisionError
scratch[i] = acc
acc *= inputs[i]
acc = Scalar(invert(acc.x,l))
for i in range(n-1,-1,-1):
temp = acc*inputs[i]
inputs[i] = acc*scratch[i]
acc = temp
return ScalarVector(inputs)
# Negation
def __neg__(self):
return ScalarVector([-s for s in self.scalars])
# Try to make a point from a given y-coordinate
def make_point(y):
if not y < q: # stay in the field
return None
x = xfromy(y)
try:
P = Point(x,y)
except ValueError:
return None
return P
# Hash data to get a Point in the main subgroup
def hash_to_point(*data):
result = ''
for datum in data:
if datum is None:
raise TypeError
result += blake2s(str(datum).encode('utf-8')).hexdigest()
# Continue hashing until we get a valid Point
while True:
result = blake2s(result.encode('utf-8')).hexdigest()
if make_point(int(result,16)) is not None:
return make_point(int(result,16))*Scalar(cofactor)
# Hash data to get a Scalar
def hash_to_scalar(*data):
result = ''
for datum in data:
if datum is None:
raise TypeError
result += blake2s(str(datum).encode('utf-8')).hexdigest()
# Continue hashing until we get a valid Scalar
while True:
result = blake2s(result.encode('utf-8')).hexdigest()
if int(result,16) < l:
return Scalar(int(result,16))
# Generate a random Scalar
def random_scalar(zero=True):
value = Scalar(secrets.randbelow(l))
if not zero and value == Scalar(0):
raise ValueError('Random scalar unexpectedly returned zero!')
return value
# Generate a random Point in the main subgroup
def random_point():
return hash_to_point(secrets.randbits(b))
# The main subgroup default generator
Gy = 4*invert(5,q)
Gx = xfromy(Gy)
G = Point(Gx % q, Gy % q)
# Neutral group element
Z = Point(0,1)
# Perform a multiscalar multiplication using a simplified Pippenger algorithm
def multiexp(scalars,points):
if not isinstance(scalars,ScalarVector) or not isinstance(points,PointVector):
raise TypeError
if len(scalars) != len(points):
raise IndexError
if len(scalars) == 0:
return Z
buckets = None
result = Z # zero point
c = 4 # window parameter; NOTE: the optimal value actually depends on len(points) empirically
# really we want to use the max bitlength to compute groups
maxscalar = int(max(scalars))
groups = 0
while maxscalar >= 2**groups:
groups += 1
groups = int((groups + c - 1) / c)
# loop is really (groups-1)..0
for k in range(groups-1,-1,-1):
if result != Z:
for i in range(c):
result += result
buckets = [Z]*(2**c) # clear all buckets
# partition scalars into buckets
for i in range(len(scalars)):
bucket = 0
for j in range(c):
if int(scalars[i]) & (1 << (k*c+j)): # test for bit
bucket |= 1 << j
if bucket == 0: # zero bucket is never used
continue
if buckets[bucket] != Z:
buckets[bucket] += points[i]
else:
buckets[bucket] = points[i]
# sum the buckets
pail = Z
for i in range(len(buckets)-1,0,-1):
if buckets[i] != Z:
pail += buckets[i]
if pail != Z:
result += pail
return result