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.

gen_exhaustive_groups.sage 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Define field size and field
  2. P = 2^256 - 2^32 - 977
  3. F = GF(P)
  4. BETA = F(0x7ae96a2b657c07106e64479eac3434e99cf0497512f58995c1396c28719501ee)
  5. assert(BETA != F(1) and BETA^3 == F(1))
  6. orders_done = set()
  7. results = {}
  8. first = True
  9. for b in range(1, P):
  10. # There are only 6 curves (up to isomorphism) of the form y^2=x^3+B. Stop once we have tried all.
  11. if len(orders_done) == 6:
  12. break
  13. E = EllipticCurve(F, [0, b])
  14. print("Analyzing curve y^2 = x^3 + %i" % b)
  15. n = E.order()
  16. # Skip curves with an order we've already tried
  17. if n in orders_done:
  18. print("- Isomorphic to earlier curve")
  19. continue
  20. orders_done.add(n)
  21. # Skip curves isomorphic to the real secp256k1
  22. if n.is_pseudoprime():
  23. print(" - Isomorphic to secp256k1")
  24. continue
  25. print("- Finding subgroups")
  26. # Find what prime subgroups exist
  27. for f, _ in n.factor():
  28. print("- Analyzing subgroup of order %i" % f)
  29. # Skip subgroups of order >1000
  30. if f < 4 or f > 1000:
  31. print(" - Bad size")
  32. continue
  33. # Iterate over X coordinates until we find one that is on the curve, has order f,
  34. # and for which curve isomorphism exists that maps it to X coordinate 1.
  35. for x in range(1, P):
  36. # Skip X coordinates not on the curve, and construct the full point otherwise.
  37. if not E.is_x_coord(x):
  38. continue
  39. G = E.lift_x(F(x))
  40. print(" - Analyzing (multiples of) point with X=%i" % x)
  41. # Skip points whose order is not a multiple of f. Project the point to have
  42. # order f otherwise.
  43. if (G.order() % f):
  44. print(" - Bad order")
  45. continue
  46. G = G * (G.order() // f)
  47. # Find lambda for endomorphism. Skip if none can be found.
  48. lam = None
  49. for l in Integers(f)(1).nth_root(3, all=True):
  50. if int(l)*G == E(BETA*G[0], G[1]):
  51. lam = int(l)
  52. break
  53. if lam is None:
  54. print(" - No endomorphism for this subgroup")
  55. break
  56. # Now look for an isomorphism of the curve that gives this point an X
  57. # coordinate equal to 1.
  58. # If (x,y) is on y^2 = x^3 + b, then (a^2*x, a^3*y) is on y^2 = x^3 + a^6*b.
  59. # So look for m=a^2=1/x.
  60. m = F(1)/G[0]
  61. if not m.is_square():
  62. print(" - No curve isomorphism maps it to a point with X=1")
  63. continue
  64. a = m.sqrt()
  65. rb = a^6*b
  66. RE = EllipticCurve(F, [0, rb])
  67. # Use as generator twice the image of G under the above isormorphism.
  68. # This means that generator*(1/2 mod f) will have X coordinate 1.
  69. RG = RE(1, a^3*G[1]) * 2
  70. # And even Y coordinate.
  71. if int(RG[1]) % 2:
  72. RG = -RG
  73. assert(RG.order() == f)
  74. assert(lam*RG == RE(BETA*RG[0], RG[1]))
  75. # We have found curve RE:y^2=x^3+rb with generator RG of order f. Remember it
  76. results[f] = {"b": rb, "G": RG, "lambda": lam}
  77. print(" - Found solution")
  78. break
  79. print("")
  80. print("")
  81. print("")
  82. print("/* To be put in src/group_impl.h: */")
  83. first = True
  84. for f in sorted(results.keys()):
  85. b = results[f]["b"]
  86. G = results[f]["G"]
  87. print("# %s EXHAUSTIVE_TEST_ORDER == %i" % ("if" if first else "elif", f))
  88. first = False
  89. print("static const secp256k1_ge secp256k1_ge_const_g = SECP256K1_GE_CONST(")
  90. print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(G[0]) >> (32 * (7 - i))) & 0xffffffff for i in range(4)))
  91. print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(G[0]) >> (32 * (7 - i))) & 0xffffffff for i in range(4, 8)))
  92. print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(G[1]) >> (32 * (7 - i))) & 0xffffffff for i in range(4)))
  93. print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x" % tuple((int(G[1]) >> (32 * (7 - i))) & 0xffffffff for i in range(4, 8)))
  94. print(");")
  95. print("static const secp256k1_fe secp256k1_fe_const_b = SECP256K1_FE_CONST(")
  96. print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x," % tuple((int(b) >> (32 * (7 - i))) & 0xffffffff for i in range(4)))
  97. print(" 0x%08x, 0x%08x, 0x%08x, 0x%08x" % tuple((int(b) >> (32 * (7 - i))) & 0xffffffff for i in range(4, 8)))
  98. print(");")
  99. print("# else")
  100. print("# error No known generator for the specified exhaustive test group order.")
  101. print("# endif")
  102. print("")
  103. print("")
  104. print("/* To be put in src/scalar_impl.h: */")
  105. first = True
  106. for f in sorted(results.keys()):
  107. lam = results[f]["lambda"]
  108. print("# %s EXHAUSTIVE_TEST_ORDER == %i" % ("if" if first else "elif", f))
  109. first = False
  110. print("# define EXHAUSTIVE_TEST_LAMBDA %i" % lam)
  111. print("# else")
  112. print("# error No known lambda for the specified exhaustive test group order.")
  113. print("# endif")
  114. print("")