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

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

Issue 1578793002: Add experimental framework token generation tool (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add a header to satisfy licencecheck.pl, which can't check the LICENCE file. Created 4 years, 9 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
« no previous file with comments | « tools/origin_trials/third_party/ed25519/README.chromium ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 # The original version of this file was downloaded from
2 # http://ed25519.cr.yp.to/software.html, and came with the following copyright
3 # statement:
4 # The Ed25519 software is in the public domain.
5
6 import hashlib
7
8 b = 256
9 q = 2**255 - 19
10 l = 2**252 + 27742317777372353535851937790883648493
11
12 def H(m):
13 return hashlib.sha512(m).digest()
14
15 def expmod(b,e,m):
16 if e == 0: return 1
17 t = expmod(b,e/2,m)**2 % m
18 if e & 1: t = (t*b) % m
19 return t
20
21 def inv(x):
22 return expmod(x,q-2,q)
23
24 d = -121665 * inv(121666)
25 I = expmod(2,(q-1)/4,q)
26
27 def xrecover(y):
28 xx = (y*y-1) * inv(d*y*y+1)
29 x = expmod(xx,(q+3)/8,q)
30 if (x*x - xx) % q != 0: x = (x*I) % q
31 if x % 2 != 0: x = q-x
32 return x
33
34 By = 4 * inv(5)
35 Bx = xrecover(By)
36 B = [Bx % q,By % q]
37
38 def edwards(P,Q):
39 x1 = P[0]
40 y1 = P[1]
41 x2 = Q[0]
42 y2 = Q[1]
43 x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
44 y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
45 return [x3 % q,y3 % q]
46
47 def scalarmult(P,e):
48 if e == 0: return [0,1]
49 Q = scalarmult(P,e/2)
50 Q = edwards(Q,Q)
51 if e & 1: Q = edwards(Q,P)
52 return Q
53
54 def encodeint(y):
55 bits = [(y >> i) & 1 for i in range(b)]
56 return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in ra nge(b/8)])
57
58 def encodepoint(P):
59 x = P[0]
60 y = P[1]
61 bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
62 return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in ra nge(b/8)])
63
64 def bit(h,i):
65 return (ord(h[i/8]) >> (i%8)) & 1
66
67 def publickey(sk):
68 h = H(sk)
69 a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
70 A = scalarmult(B,a)
71 return encodepoint(A)
72
73 def Hint(m):
74 h = H(m)
75 return sum(2**i * bit(h,i) for i in range(2*b))
76
77 def signature(m,sk,pk):
78 h = H(sk)
79 a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
80 r = Hint(''.join([h[i] for i in range(b/8,b/4)]) + m)
81 R = scalarmult(B,r)
82 S = (r + Hint(encodepoint(R) + pk + m) * a) % l
83 return encodepoint(R) + encodeint(S)
84
85 def isoncurve(P):
86 x = P[0]
87 y = P[1]
88 return (-x*x + y*y - 1 - d*x*x*y*y) % q == 0
89
90 def decodeint(s):
91 return sum(2**i * bit(s,i) for i in range(0,b))
92
93 def decodepoint(s):
94 y = sum(2**i * bit(s,i) for i in range(0,b-1))
95 x = xrecover(y)
96 if x & 1 != bit(s,b-1): x = q-x
97 P = [x,y]
98 if not isoncurve(P): raise Exception("decoding point that is not on curve")
99 return P
100
101 def checkvalid(s,m,pk):
102 if len(s) != b/4: raise Exception("signature length is wrong")
103 if len(pk) != b/8: raise Exception("public-key length is wrong")
104 R = decodepoint(s[0:b/8])
105 A = decodepoint(pk)
106 S = decodeint(s[b/8:b/4])
107 h = Hint(encodepoint(R) + pk + m)
108 if scalarmult(B,S) != edwards(R,scalarmult(A,h)):
109 raise Exception("signature does not pass verification")
OLDNEW
« no previous file with comments | « tools/origin_trials/third_party/ed25519/README.chromium ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698