1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| from sage.all import EllipticCurve from Crypto.Util.number import *
c = 1 eG = (34120664973166619886120801966861368419497948422807175421202190709822232354059, 11301243831592615312624457443883283529467532390028216735072818875052648928463) p = 64141017538026690847507665744072764126523219720088055136531450296140542176327 a = 362 d = 7 e=0x10001
assert (a * eG[0] ** 2 + eG[1] ** 2) % p == c ** 2 * (1 + d * eG[0] ** 2 * eG[1] ** 2) % p print("符合曲线方程 ax^2+y^2 = c^2(1+dx^2y^2)")
def TEdwards_to_TEdwards(G, a, c, d): x, y = G x1 = x * pow(c, -1, p) y1 = y * pow(c, -1, p) d = d * c ** 4 G = (x1, y1) return (G, a, d)
def TEdwards_to_Montgomery(G, a, d): x, y = G A = 2 * (a + d) * pow(a - d, -1, p) B = 4 * pow(a - d, -1, p) u = (1 + y) * pow(1 - y, -1, p) v = u * pow(x, -1, p) G = (u, v) return (G, A, B)
def Montgomery_to_Weierstrass(G, A, B): x, y = G u = x * pow(B, -1, p) + A * pow(3 * B, -1, p) v = y * pow(B, -1, p) a = (3 - A ** 2) * pow(3 * B ** 2, -1, p) b = (2 * A ** 3 - 9 * A) * pow(27 * B ** 3, -1, p) G = (u, v) return (G, a, b)
S = eG
S, a2, d2 = TEdwards_to_TEdwards(S, a, c, d) S, A2, B2 = TEdwards_to_Montgomery(S, a2, d2) S, a2, b2 = Montgomery_to_Weierstrass(S, A2, B2)
assert S[1] ** 2 % p == (S[0] ** 3 + a2 * S[0] + b2) % p print("符合曲线方程 y^2 = x^3+ax+b")
print(a2)
E = EllipticCurve(GF(p), [a2, b2])
S = E(S)
order1 = E.order()
d = gmpy2.invert(e,order1)
mx,my = (int(d) * S).xy() print(mx) print(my) print(long_to_bytes(int(mx))) print(long_to_bytes(int(my)))
print(A2) print(B2)
|