Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(470)

Side by Side Diff: tools/experiments/third_party/ed25519.py

Issue 1578793002: Add experimental framework token generation tool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add OWNERS file for tool directory Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 import hashlib
2
3 b = 256
4 q = 2**255 - 19
5 l = 2**252 + 27742317777372353535851937790883648493
6
7 def H(m):
8 return hashlib.sha512(m).digest()
9
10 def expmod(b,e,m):
11 if e == 0: return 1
12 t = expmod(b,e/2,m)**2 % m
13 if e & 1: t = (t*b) % m
14 return t
15
16 def inv(x):
17 return expmod(x,q-2,q)
18
19 d = -121665 * inv(121666)
20 I = expmod(2,(q-1)/4,q)
21
22 def xrecover(y):
23 xx = (y*y-1) * inv(d*y*y+1)
24 x = expmod(xx,(q+3)/8,q)
25 if (x*x - xx) % q != 0: x = (x*I) % q
26 if x % 2 != 0: x = q-x
27 return x
28
29 By = 4 * inv(5)
30 Bx = xrecover(By)
31 B = [Bx % q,By % q]
32
33 def edwards(P,Q):
34 x1 = P[0]
35 y1 = P[1]
36 x2 = Q[0]
37 y2 = Q[1]
38 x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
39 y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
40 return [x3 % q,y3 % q]
41
42 def scalarmult(P,e):
43 if e == 0: return [0,1]
44 Q = scalarmult(P,e/2)
45 Q = edwards(Q,Q)
46 if e & 1: Q = edwards(Q,P)
47 return Q
48
49 def encodeint(y):
50 bits = [(y >> i) & 1 for i in range(b)]
51 return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in ra nge(b/8)])
52
53 def encodepoint(P):
54 x = P[0]
55 y = P[1]
56 bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
57 return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in ra nge(b/8)])
58
59 def bit(h,i):
60 return (ord(h[i/8]) >> (i%8)) & 1
61
62 def publickey(sk):
63 h = H(sk)
64 a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
65 A = scalarmult(B,a)
66 return encodepoint(A)
67
68 def Hint(m):
69 h = H(m)
70 return sum(2**i * bit(h,i) for i in range(2*b))
71
72 def signature(m,sk,pk):
73 h = H(sk)
74 a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
75 r = Hint(''.join([h[i] for i in range(b/8,b/4)]) + m)
76 R = scalarmult(B,r)
77 S = (r + Hint(encodepoint(R) + pk + m) * a) % l
78 return encodepoint(R) + encodeint(S)
79
80 def isoncurve(P):
81 x = P[0]
82 y = P[1]
83 return (-x*x + y*y - 1 - d*x*x*y*y) % q == 0
84
85 def decodeint(s):
86 return sum(2**i * bit(s,i) for i in range(0,b))
87
88 def decodepoint(s):
89 y = sum(2**i * bit(s,i) for i in range(0,b-1))
90 x = xrecover(y)
91 if x & 1 != bit(s,b-1): x = q-x
92 P = [x,y]
93 if not isoncurve(P): raise Exception("decoding point that is not on curve")
94 return P
95
96 def checkvalid(s,m,pk):
97 if len(s) != b/4: raise Exception("signature length is wrong")
98 if len(pk) != b/8: raise Exception("public-key length is wrong")
99 R = decodepoint(s[0:b/8])
100 A = decodepoint(pk)
101 S = decodeint(s[b/8:b/4])
102 h = Hint(encodepoint(R) + pk + m)
103 if scalarmult(B,S) != edwards(R,scalarmult(A,h)):
104 raise Exception("signature does not pass verification")
OLDNEW
« tools/experiments/third_party/README.chromium ('K') | « tools/experiments/third_party/__init__.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698