You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

134 lines
2.8 KiB

  1. # This is as sketch of how to decaffeinate Curve25519
  2. F = GF(2^255-19)
  3. doPinkBikeShed = True
  4. if doPinkBikeShed: d = -89747 # PinkBikeShed
  5. else: d = -121665 # Curve25519
  6. M = EllipticCurve(F,[0,2-4*d,0,1,0])
  7. sqrtN1 = sqrt(F(-1))
  8. def maybe(): return randint(0,1)
  9. def qpositive(x):
  10. return int(x) <= (2^255-19-1)/2
  11. def M_to_E(P):
  12. # P must be even
  13. (x,y) = P.xy()
  14. assert x.is_square()
  15. s = sqrt(x)
  16. if s == 0: t = 1
  17. else: t = y/s
  18. X,Y = 2*s / (1+s^2), (1-s^2) / t
  19. if maybe(): X,Y = -X,-Y
  20. if maybe(): X,Y = Y,-X
  21. # OK, have point in ed
  22. return X,Y
  23. def decaf_encode_from_E(X,Y):
  24. assert X^2 + Y^2 == 1 + d*X^2*Y^2
  25. if not (qpositive(X*Y) or doPinkBikeShed): X,Y = Y,-X
  26. assert qpositive(X*Y)
  27. assert (1-X^2).is_square()
  28. sx = sqrt(1-X^2)
  29. tos = -2*sx/X/Y
  30. if not qpositive(tos): sx = -sx
  31. s = (1 + sx) / X
  32. if not qpositive(s): s = -s
  33. return s
  34. def isqrt(x):
  35. ops = [(1,2),(1,2),(3,1),(6,0),(1,2),(12,1),(25,1),(25,1),(50,0),(125,0),(2,2),(1,2)]
  36. st = [x,x,x]
  37. for i,(sh,add) in enumerate(ops):
  38. od = i&1
  39. st[od] = st[od^^1]^(2^sh)*st[add]
  40. # assert st[2] == x^(2^252-3)
  41. assert st[1] == 1 or st[1] == -1
  42. if st[1] == 1: return st[0]
  43. else: return st[0] * sqrtN1
  44. def decaf_encode_from_E_c(X,Y):
  45. Z = F.random_element()
  46. T = X*Y*Z
  47. X = X*Z
  48. Y = Y*Z
  49. assert X^2 + Y^2 == Z^2 + d*T^2
  50. # Precompute
  51. sd = sqrt(F(1-d))
  52. zx = Z^2-X^2
  53. TZ = T*Z
  54. assert zx.is_square
  55. ooAll = isqrt(zx*TZ^2)
  56. osx = ooAll * TZ
  57. ooTZ = ooAll * zx * osx
  58. floop = qpositive(T^2 * ooTZ) or doPinkBikeShed
  59. if floop:
  60. frob = zx * ooTZ
  61. else:
  62. frob = sd
  63. Y = -X
  64. osx *= frob
  65. if qpositive(-2*osx*Z) != floop: osx = -osx
  66. s = Y*(ooTZ*Z + osx)
  67. if not qpositive(s): s = -s
  68. return s
  69. def is_rotation((X,Y),(x,y)):
  70. return x*Y == X*y or x*X == -y*Y
  71. def decaf_decode_to_E(s):
  72. assert qpositive(s)
  73. t = sqrt(s^4 + (2-4*d)*s^2 + 1)
  74. if not qpositive(t/s): t = -t
  75. X,Y = 2*s / (1+s^2), (1-s^2) / t
  76. assert qpositive(X*Y) or doPinkBikeShed
  77. return X,Y
  78. def decaf_decode_to_E_c(s):
  79. assert qpositive(s)
  80. s2 = s^2
  81. s21 = 1+s2
  82. t2 = s21^2 - 4*d*s2
  83. alt = s21*s
  84. the = isqrt(t2*alt^2)
  85. oot = the * alt
  86. the *= t2
  87. tos = the * s21
  88. X = 2 * (tos-the) * oot
  89. Y = (1-s2) * oot
  90. if not qpositive(tos): Y = -Y
  91. assert qpositive(X*Y) or doPinkBikeShed
  92. return X,Y
  93. def test():
  94. P = 2*M.random_point()
  95. X,Y = M_to_E(P)
  96. s = decaf_encode_from_E(X,Y)
  97. assert s == decaf_encode_from_E_c(X,Y)
  98. XX,YY = decaf_decode_to_E(s)
  99. XX2,YY2 = decaf_decode_to_E_c(s)
  100. assert is_rotation((X,Y),(XX,YY))
  101. assert is_rotation((X,Y),(XX2,YY2))