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