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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| from math import gcd
def ison(C, P): """ Verification points are on the curve """ c, d, p = C u, v = P return (u**2 + v**2 - cc * (1 + d * u**2*v**2)) % p == 0
def a_and_b(u1,u2,v1,v2): """ Helper function used to simplify calculations """ a12 = u1**2 - u2**2 + v1**2 - v2**2 b12 = u1**2 * v1**2 - u2**2 * v2**2 return a12, b12
def find_modulus(u1,u2,u3,u4,v1,v2,v3,v4): """ Compute the modulus from four points """ a12, b12 = a_and_b(u1,u2,v1,v2) a13, b13 = a_and_b(u1,u3,v1,v3) a23, b23 = a_and_b(u2,u3,v2,v3) a24, b24 = a_and_b(u2,u4,v2,v4)
p_almost = gcd(a12*b13 - a13*b12, a23*b24 - a24*b23)
for i in range(2,1000): if p_almost % i == 0: p_almost = p_almost // i
return p_almost
def c_sq_d(u1,u2,v1,v2,p): """ Helper function to computer c^2 d """ a1,b1 = a_and_b(u1,u2,v1,v2) return a1 * pow(b1,-1,p) % p
def c(u1,u2,v1,v2,p): """ Compute c^2, d from two points and known modulus """ ccd = c_sq_d(u1,u2,v1,v2,p) cc = (u1**2 + v1**2 - ccd*u1**2*v1**2) % p d = ccd * pow(cc, -1, p) % p return cc, d
P = (398011447251267732058427934569710020713094, 548950454294712661054528329798266699762662) Q = (139255151342889674616838168412769112246165, 649791718379009629228240558980851356197207) sP = (730393937659426993430595540476247076383331, 461597565155009635099537158476419433012710) tQ = (500532897653416664117493978883484252869079, 620853965501593867437705135137758828401933)
u1, v1 = P u2, v2 = Q u3, v3 = sP u4, v4 = tQ
p = find_modulus(u1,u2,u3,u4,v1,v2,v3,v4) cc, d = c(u1,u2,v1,v2,p)
C = cc, d, p assert ison(C, P) assert ison(C, Q) assert ison(C, sP) assert ison(C, tQ)
print(f'Found curve parameters') print(f'p = {p}') print(f'c^2 = {cc}') print(f'd = {d}')
p = 903968861315877429495243431349919213155709 cc = 495368774702871559312404847312353912297284 d = 540431316779988345188678880301417602675534
from Crypto.Util.number import *
p = 903968861315877429495243431349919213155709 F = GF(p) cc = 495368774702871559312404847312353912297284 c = F(cc).sqrt() d = 540431316779988345188678880301417602675534
x1, y1 = P x2, y2 = Q x3, y3 = sP x4, y4 = tQ
R.<x,y> = PolynomialRing(F) g = (x^2 + y^2 - cc * (1 + d * x^2*y^2))
assert g(x=x1, y=y1) == 0 assert g(x=x2, y=y2) == 0 assert g(x=x3, y=y3) == 0 assert g(x=x4, y=y4) == 0
d = F(d) * F(cc)^2 x1, y1 = F(x1) / F(c), F(y1) / F(c) x2, y2 = F(x2) / F(c), F(y2) / F(c) x3, y3 = F(x3) / F(c), F(y3) / F(c) x4, y4 = F(x4) / F(c), F(y4) / F(c)
h = (x^2 + y^2 - (1 + d * x^2*y^2))
assert h(x=x1, y=y1) == 0 assert h(x=x2, y=y2) == 0 assert h(x=x3, y=y3) == 0 assert h(x=x4, y=y4) == 0
def ed_to_mont(x,y): u = F(1 + y) / F(1 - y) v = 2*F(1 + y) / F(x*(1 - y)) return u,v
u1, v1 = ed_to_mont(x1, y1) u2, v2 = ed_to_mont(x2, y2) u3, v3 = ed_to_mont(x3, y3) u4, v4 = ed_to_mont(x4, y4)
e_curve = 1 - F(d) A = (4/e_curve - 2) B = (1/e_curve)
R.<u,v> = PolynomialRing(ZZ) f = B*v^2 - u^3 - A* u^2 - u
assert f(u=u1, v=v1) == 0 assert f(u=u2, v=v2) == 0 assert f(u=u3, v=v3) == 0 assert f(u=u4, v=v4) == 0
a = F(3 - A^2) / F(3*B^2) b = (2*A^3 - 9*A) / F(27*B^3) E = EllipticCurve(F, [a,b])
def mont_to_wei(u,v): t = (F(u) / F(B)) + (F(A) / F(3*B)) s = (F(v) / F(B)) return t,s
X1, Y1 = mont_to_wei(u1, v1) X2, Y2 = mont_to_wei(u2, v2) X3, Y3 = mont_to_wei(u3, v3) X4, Y4 = mont_to_wei(u4, v4)
P = E(X1, Y1) Q = E(X2, Y2) sP = E(X3, Y3) tQ = E(X4, Y4)
s = P.discrete_log(sP) t = Q.discrete_log(tQ)
print(long_to_bytes(s)) print(long_to_bytes(t))
print(long_to_bytes(s % Q.order())) print(long_to_bytes(t))
|