2017-10-03 22:37:45 +00:00
|
|
|
# orignal version downloaded from https://ed25519.cr.yp.to/python/ed25519.py
|
|
|
|
# modified for Python 3 by Jochen Hoenicke <hoenicke@gmail.com>
|
|
|
|
|
|
|
|
import hashlib
|
2018-05-28 12:17:11 +00:00
|
|
|
from typing import Tuple, NewType
|
|
|
|
|
|
|
|
Point = NewType("Point", Tuple[int, int])
|
2017-10-03 22:37:45 +00:00
|
|
|
|
|
|
|
b = 256
|
|
|
|
q = 2 ** 255 - 19
|
|
|
|
l = 2 ** 252 + 27742317777372353535851937790883648493
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def H(m: bytes) -> bytes:
|
2017-10-03 22:37:45 +00:00
|
|
|
return hashlib.sha512(m).digest()
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def expmod(b: int, e: int, m: int) -> int:
|
2017-10-03 22:37:45 +00:00
|
|
|
if e < 0:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError('negative exponent')
|
2017-10-03 22:37:45 +00:00
|
|
|
if e == 0:
|
|
|
|
return 1
|
|
|
|
t = expmod(b, e >> 1, m) ** 2 % m
|
|
|
|
if e & 1:
|
|
|
|
t = (t * b) % m
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def inv(x: int) -> int:
|
2017-10-03 22:37:45 +00:00
|
|
|
return expmod(x, q - 2, q)
|
|
|
|
|
|
|
|
|
|
|
|
d = -121665 * inv(121666)
|
|
|
|
I = expmod(2, (q - 1) >> 2, q)
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def xrecover(y: int) -> int:
|
2017-10-03 22:37:45 +00:00
|
|
|
xx = (y * y - 1) * inv(d * y * y + 1)
|
|
|
|
x = expmod(xx, (q + 3) >> 3, q)
|
|
|
|
if (x * x - xx) % q != 0:
|
|
|
|
x = (x * I) % q
|
|
|
|
if x % 2 != 0:
|
|
|
|
x = q - x
|
|
|
|
return x
|
|
|
|
|
|
|
|
|
|
|
|
By = 4 * inv(5)
|
|
|
|
Bx = xrecover(By)
|
2018-05-28 12:17:11 +00:00
|
|
|
B = Point((Bx % q, By % q))
|
2017-10-03 22:37:45 +00:00
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def edwards(P: Point, Q: Point) -> Point:
|
2017-10-03 22:37:45 +00:00
|
|
|
x1 = P[0]
|
|
|
|
y1 = P[1]
|
|
|
|
x2 = Q[0]
|
|
|
|
y2 = Q[1]
|
|
|
|
x3 = (x1 * y2 + x2 * y1) * inv(1 + d * x1 * x2 * y1 * y2)
|
|
|
|
y3 = (y1 * y2 + x1 * x2) * inv(1 - d * x1 * x2 * y1 * y2)
|
2018-05-28 12:17:11 +00:00
|
|
|
return Point((x3 % q, y3 % q))
|
2017-10-03 22:37:45 +00:00
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def scalarmult(P: Point, e: int) -> Point:
|
2017-10-03 22:37:45 +00:00
|
|
|
if e == 0:
|
2018-05-28 12:17:11 +00:00
|
|
|
return Point((0, 1))
|
2017-10-03 22:37:45 +00:00
|
|
|
Q = scalarmult(P, e >> 1)
|
|
|
|
Q = edwards(Q, Q)
|
|
|
|
if e & 1:
|
|
|
|
Q = edwards(Q, P)
|
|
|
|
return Q
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def encodeint(y: int) -> bytes:
|
2017-10-03 22:37:45 +00:00
|
|
|
bits = [(y >> i) & 1 for i in range(b)]
|
2017-12-18 21:33:55 +00:00
|
|
|
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b >> 3)])
|
2017-10-03 22:37:45 +00:00
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def encodepoint(P: Point) -> bytes:
|
2017-10-03 22:37:45 +00:00
|
|
|
x = P[0]
|
|
|
|
y = P[1]
|
|
|
|
bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
|
2017-12-18 21:33:55 +00:00
|
|
|
return bytes([sum([bits[i * 8 + j] << j for j in range(8)]) for i in range(b >> 3)])
|
2017-10-03 22:37:45 +00:00
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def bit(h: bytes, i: int) -> int:
|
2017-12-18 21:33:55 +00:00
|
|
|
return (h[i >> 3] >> (i & 7)) & 1
|
2017-10-03 22:37:45 +00:00
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def publickey(sk: bytes) -> bytes:
|
2017-10-03 22:37:45 +00:00
|
|
|
h = H(sk)
|
|
|
|
a = 2 ** (b - 2) + sum(2 ** i * bit(h, i) for i in range(3, b - 2))
|
|
|
|
A = scalarmult(B, a)
|
|
|
|
return encodepoint(A)
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def Hint(m: bytes) -> int:
|
2017-10-03 22:37:45 +00:00
|
|
|
h = H(m)
|
|
|
|
return sum(2 ** i * bit(h, i) for i in range(2 * b))
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def signature(m: bytes, sk: bytes, pk: bytes) -> bytes:
|
2017-10-03 22:37:45 +00:00
|
|
|
h = H(sk)
|
|
|
|
a = 2 ** (b - 2) + sum(2 ** i * bit(h, i) for i in range(3, b - 2))
|
|
|
|
r = Hint(bytes([h[i] for i in range(b >> 3, b >> 2)]) + m)
|
|
|
|
R = scalarmult(B, r)
|
|
|
|
S = (r + Hint(encodepoint(R) + pk + m) * a) % l
|
|
|
|
return encodepoint(R) + encodeint(S)
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def isoncurve(P: Point) -> bool:
|
2017-10-03 22:37:45 +00:00
|
|
|
x = P[0]
|
|
|
|
y = P[1]
|
|
|
|
return (-x * x + y * y - 1 - d * x * x * y * y) % q == 0
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def decodeint(s: bytes) -> int:
|
2017-10-03 22:37:45 +00:00
|
|
|
return sum(2 ** i * bit(s, i) for i in range(0, b))
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def decodepoint(s: bytes) -> Point:
|
2017-10-03 22:37:45 +00:00
|
|
|
y = sum(2 ** i * bit(s, i) for i in range(0, b - 1))
|
|
|
|
x = xrecover(y)
|
|
|
|
if x & 1 != bit(s, b - 1):
|
|
|
|
x = q - x
|
2018-05-28 12:17:11 +00:00
|
|
|
P = Point((x, y))
|
2017-10-03 22:37:45 +00:00
|
|
|
if not isoncurve(P):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError('decoding point that is not on curve')
|
2017-10-03 22:37:45 +00:00
|
|
|
return P
|
|
|
|
|
|
|
|
|
2018-05-28 12:17:11 +00:00
|
|
|
def checkvalid(s: bytes, m: bytes, pk: bytes) -> None:
|
2017-10-03 22:37:45 +00:00
|
|
|
if len(s) != b >> 2:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError('signature length is wrong')
|
2017-10-03 22:37:45 +00:00
|
|
|
if len(pk) != b >> 3:
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError('public-key length is wrong')
|
2017-10-03 22:37:45 +00:00
|
|
|
R = decodepoint(s[0:b >> 3])
|
|
|
|
A = decodepoint(pk)
|
|
|
|
S = decodeint(s[b >> 3:b >> 2])
|
|
|
|
h = Hint(encodepoint(R) + pk + m)
|
|
|
|
if scalarmult(B, S) != edwards(R, scalarmult(A, h)):
|
2017-11-06 10:09:54 +00:00
|
|
|
raise ValueError('signature does not pass verification')
|