slow_ed25519.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # This is the ed25519 implementation from
  2. # http://ed25519.cr.yp.to/python/ed25519.py .
  3. # It is in the public domain.
  4. #
  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.
  8. #
  9. # Don't edit this file. Mess with ed25519_ref.py
  10. import hashlib
  11. b = 256
  12. q = 2**255 - 19
  13. l = 2**252 + 27742317777372353535851937790883648493
  14. def H(m):
  15. return hashlib.sha512(m).digest()
  16. def expmod(b,e,m):
  17. if e == 0: return 1
  18. t = expmod(b,e/2,m)**2 % m
  19. if e & 1: t = (t*b) % m
  20. return t
  21. def inv(x):
  22. return expmod(x,q-2,q)
  23. d = -121665 * inv(121666)
  24. I = expmod(2,(q-1)/4,q)
  25. def xrecover(y):
  26. xx = (y*y-1) * inv(d*y*y+1)
  27. x = expmod(xx,(q+3)/8,q)
  28. if (x*x - xx) % q != 0: x = (x*I) % q
  29. if x % 2 != 0: x = q-x
  30. return x
  31. By = 4 * inv(5)
  32. Bx = xrecover(By)
  33. B = [Bx % q,By % q]
  34. def edwards(P,Q):
  35. x1 = P[0]
  36. y1 = P[1]
  37. x2 = Q[0]
  38. y2 = Q[1]
  39. x3 = (x1*y2+x2*y1) * inv(1+d*x1*x2*y1*y2)
  40. y3 = (y1*y2+x1*x2) * inv(1-d*x1*x2*y1*y2)
  41. return [x3 % q,y3 % q]
  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. def encodeint(y):
  49. bits = [(y >> i) & 1 for i in range(b)]
  50. return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b/8)])
  51. def encodepoint(P):
  52. x = P[0]
  53. y = P[1]
  54. bits = [(y >> i) & 1 for i in range(b - 1)] + [x & 1]
  55. return ''.join([chr(sum([bits[i * 8 + j] << j for j in range(8)])) for i in range(b/8)])
  56. def bit(h,i):
  57. return (ord(h[i/8]) >> (i%8)) & 1
  58. def publickey(sk):
  59. h = H(sk)
  60. a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
  61. A = scalarmult(B,a)
  62. return encodepoint(A)
  63. def Hint(m):
  64. h = H(m)
  65. return sum(2**i * bit(h,i) for i in range(2*b))
  66. def signature(m,sk,pk):
  67. h = H(sk)
  68. a = 2**(b-2) + sum(2**i * bit(h,i) for i in range(3,b-2))
  69. r = Hint(''.join([h[i] for i in range(b/8,b/4)]) + m)
  70. R = scalarmult(B,r)
  71. S = (r + Hint(encodepoint(R) + pk + m) * a) % l
  72. return encodepoint(R) + encodeint(S)
  73. def isoncurve(P):
  74. x = P[0]
  75. y = P[1]
  76. return (-x*x + y*y - 1 - d*x*x*y*y) % q == 0
  77. def decodeint(s):
  78. return sum(2**i * bit(s,i) for i in range(0,b))
  79. def decodepoint(s):
  80. y = sum(2**i * bit(s,i) for i in range(0,b-1))
  81. x = xrecover(y)
  82. if x & 1 != bit(s,b-1): x = q-x
  83. P = [x,y]
  84. if not isoncurve(P): raise Exception("decoding point that is not on curve")
  85. return P
  86. def checkvalid(s,m,pk):
  87. if len(s) != b/4: raise Exception("signature length is wrong")
  88. if len(pk) != b/8: raise Exception("public-key length is wrong")
  89. R = decodepoint(s[0:b/8])
  90. A = decodepoint(pk)
  91. S = decodeint(s[b/8:b/4])
  92. h = Hint(encodepoint(R) + pk + m)
  93. if scalarmult(B,S) != edwards(R,scalarmult(A,h)):
  94. raise Exception("signature does not pass verification")