1 # This is the ed25519 implementation from
2 # http://ed25519.cr.yp.to/python/ed25519.py .
3 # It is in the public domain.
5 # It isn't constant-time. Don't use it except for testing. Also, see
6 # warnings about how very slow it is. Only use this for generating
7 # test vectors, I'd suggest.
9 # Don't edit this file. Mess with ed25519_ref.py
15 l
= 2**252 + 27742317777372353535851937790883648493
18 return hashlib
.sha512(m
).digest()
22 t
= expmod(b
,e
/2,m
)**2 % m
23 if e
& 1: t
= (t
*b
) % m
27 return expmod(x
,q
-2,q
)
29 d
= -121665 * inv(121666)
30 I
= expmod(2,(q
-1)/4,q
)
33 xx
= (y
*y
-1) * inv(d
*y
*y
+1)
34 x
= expmod(xx
,(q
+3)/8,q
)
35 if (x
*x
- xx
) % q
!= 0: x
= (x
*I
) % q
36 if x
% 2 != 0: x
= q
-x
48 x3
= (x1
*y2
+x2
*y1
) * inv(1+d
*x1
*x2
*y1
*y2
)
49 y3
= (y1
*y2
+x1
*x2
) * inv(1-d
*x1
*x2
*y1
*y2
)
50 return [x3
% q
,y3
% q
]
53 if e
== 0: return [0,1]
56 if e
& 1: Q
= edwards(Q
,P
)
60 bits
= [(y
>> i
) & 1 for i
in range(b
)]
61 return ''.join([chr(sum([bits
[i
* 8 + j
] << j
for j
in range(8)])) for i
in range(b
/8)])
66 bits
= [(y
>> i
) & 1 for i
in range(b
- 1)] + [x
& 1]
67 return ''.join([chr(sum([bits
[i
* 8 + j
] << j
for j
in range(8)])) for i
in range(b
/8)])
70 return (ord(h
[i
/8]) >> (i
%8)) & 1
74 a
= 2**(b
-2) + sum(2**i
* bit(h
,i
) for i
in range(3,b
-2))
80 return sum(2**i
* bit(h
,i
) for i
in range(2*b
))
82 def signature(m
,sk
,pk
):
84 a
= 2**(b
-2) + sum(2**i
* bit(h
,i
) for i
in range(3,b
-2))
85 r
= Hint(''.join([h
[i
] for i
in range(b
/8,b
/4)]) + m
)
87 S
= (r
+ Hint(encodepoint(R
) + pk
+ m
) * a
) % l
88 return encodepoint(R
) + encodeint(S
)
93 return (-x
*x
+ y
*y
- 1 - d
*x
*x
*y
*y
) % q
== 0
96 return sum(2**i
* bit(s
,i
) for i
in range(0,b
))
99 y
= sum(2**i
* bit(s
,i
) for i
in range(0,b
-1))
101 if x
& 1 != bit(s
,b
-1): x
= q
-x
103 if not isoncurve(P
): raise Exception("decoding point that is not on curve")
106 def checkvalid(s
,m
,pk
):
107 if len(s
) != b
/4: raise Exception("signature length is wrong")
108 if len(pk
) != b
/8: raise Exception("public-key length is wrong")
109 R
= decodepoint(s
[0:b
/8])
111 S
= decodeint(s
[b
/8:b
/4])
112 h
= Hint(encodepoint(R
) + pk
+ m
)
113 if scalarmult(B
,S
) != edwards(R
,scalarmult(A
,h
)):
114 raise Exception("signature does not pass verification")