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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
| from sage.all import * from Crypto.Util.number import * from re import findall from subprocess import check_output import fgb_sage from itertools import *
def flatter(M): z = "[[" + "]\n[".join(" ".join(map(str, row)) for row in M) + "]]" ret = check_output(["flatter"], input=z.encode()) return matrix(M.nrows(), M.ncols(), map(int, findall(b"-?\\d+", ret))) def LLL_mononimals(ff,bounds,debug = False): G = Sequence([], ff[0].parent()) for i in range(len(ff)): G.append(ff[i]) B, monomials = G.coefficients_monomials() monomials = vector(monomials) factors = [monomial(*(bounds)) for monomial in monomials] for i, factor in enumerate(factors): B.rescale_col(i, factor) print("dim",B.nrows(),B.ncols()) print('s flatter') B = flatter(B) print('e flatter') B = B.change_ring(QQ) for i, factor in enumerate(factors): B.rescale_col(i, 1 / factor) R = B*monomials if debug: for i in range(len(R)): if R[i](*([e0]+EE)) == 0: print(i) Res = [] V = fgb_sage.groebner_basis(R[8:20]) XX = [x0,y1,y2,y3,y4,y5] for i in range(len(XX)): Res.append(XX[i] - V[i]) return Res
p = 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff a = 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc b = 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b E = EllipticCurve(GF(p),[a,b]) G = E.gens()[0] n = G.order() def leak(Point,k): t1 = (ZZ(Point.xy()[0]) >> k) << k t2 = ZZ(Point.xy()[0])-t1 return t2 def Get(AA,i,R): return leak(AA+i*R,192) def Coe(H,Qx,h0,a,b,p): inv = inverse(2^192,p) A = (H*(h0-Qx)^2-2*h0^2*Qx-2*(a+Qx^2)*h0-2*a*Qx-4*b)*inv^3 % p B = (2*(H*(h0-Qx)-2*h0*Qx-a-Qx^2))*inv^2 % p C = ((H-2*Qx))*inv^1 % p D = ((h0-Qx)^2)*inv^2 % p E = (2*(h0-Qx))*inv^1 % p return A%p,B%p,C%p,D%p,E%p
hint1 = 949922372122218046965376048123327949376059750438908466789 hint2 = 42661446485198521271563058189095752632693753444357982122758630843460108543281128080768183549656737420326730293402550080264919238271649443935367948621363611575578331951338206643961231301011934223378959132807605885282642130943670906793105685979458606431074604742691074803447316574328163268414104662367677573967 h = [4198417781636735201616143091518824554303140448918234022822, 5934409889673636438564616521101931172645172047323840506005, 3545002769746467918593131186352102479907282194938281480964, 2052948548433827202413664681039848372904955567280762860403, 2409094928081781569924602821161748044701954566232724831425, 1384139093745169579221979501259712235516420223327557162332, 2939419134440891551148261965652575020956010158334816580524, 494864169851388494465000363130678068052130861967409621970, 3170747752017462050285295477866296066114421905697920861241, 3947558462622235401389908529480430195424430102289969854250, 5945090099574459201888181110413355524959030561838939467012] h0 = h[0] Hs = [h[2*i+1] + h[2*i+2] for i in range(5)] Qs = []
n,d,t = 5,2,1 for i in range(1,n+1): Q = i*G Qs.append(Q) As,Bs,Cs,Ds,Es = [],[],[],[],[] for i in range(n): Ai,Bi,Ci,Di,Ei = Coe(Hs[i],Qs[i].xy()[0],h0,a,b,p) As.append(ZZ(Ai)) Bs.append(ZZ(Bi)) Cs.append(ZZ(Ci)) Ds.append(ZZ(Di)) Es.append(ZZ(Ei)) PR.<x0,y1,y2,y3,y4,y5> = PolynomialRing(ZZ) y = [y1,y2,y3,y4,y5] Fs = [] for i in range(n): fi = As[i] + Bs[i]*x0 + Cs[i]*x0^2 + Ds[i]*y[i] + Es[i]*x0*y[i] + x0^2*y[i] Fs.append(fi) Exp_res =[] for i in range(2^5): t = [int(j) for j in bin(i)[2:].zfill(5)] Exp_res.append(t) def Get_js(exp): j = [exp[i]*(i+1) for i in range(len(exp)) if exp[i]!=0] return j def get_row1(k,js): res = 1 for u in range(len(js)): if u!=k: res*=(x0^2+Es[js[u]-1]*x0+Ds[js[u]-1]) return res def get_row2(k,js): res = x0 for u in range(len(js)): if u!=k: res*=(x0^2+Es[js[u]-1]*x0+Ds[js[u]-1]) return res def get_M(js): G = Sequence([], Fs[0].parent()) for i in range(len(js)): G.append(get_row1(i,js)) for i in range(len(js)): G.append(get_row2(i,js)) M , mons = G.coefficients_monomials() M = M[:,::-1] return M def Get_prodF(u,js,y): ttf = y[js[u-1]-1] for i in range(len(js)): if i!=(u-1): ttf *= Fs[js[i]-1] return ttf ff = [] n,d,t = 5,2,1 for i0 in range(2*d): for l in range(d+1):
if 1 <= l <= d and 0 <= i0 <= (2*l-1): f_temp = p^(d+1-l) if 0 <= l <= d and 2*l <= i0 <= 2*d: f_temp = p^(d-l) if l == 0 and 0 <= i0 <= 2*d: ft = x0^i0 ft = f_temp*ft.change_ring(ZZ) ff.append(ft) if l == 1 and 0 <= i0 <= 1: for exps in Exp_res: if sum(exps) == l: ft = x0^i0*prod(map(pow,y,exps)) ft = f_temp*ft.change_ring(ZZ) ff.append(ft) if 1 <= l <= d and 2*l <= i0 <= 2*d: for exps in Exp_res: if sum(exps) == l: ft = x0^(i0-2*l)*prod(map(pow,Fs,exps)) ft = f_temp*ft.change_ring(ZZ)
ff.append(ft) if 2 <= l <= d and 0 <= i0 <= (2*l-1): for exps in Exp_res: if sum(exps) == l: ft = 0 js = Get_js(exps) M = get_M(js) M = M.change_ring(Zmod(p^(l-1))) W = M.inverse() W = W.change_ring(ZZ) for u in range(1,l+1): for v in range(2): ft += W[i0,u+l*v-1] * x0^v * Get_prodF(u,js,y) ft = f_temp*(ft.change_ring(ZZ) %p^(l-1)) ff.append(ft) def Get_H(u,js): ttf = y[js[u-1]-1] for i in range(len(js)): if i!=(u-1): ttf *= Fs[js[i]-1] return ttf def Get_J(u,js): ttf = Cs[js[u-1]-1] for i in range(len(js)): if i!=(u-1): ttf *= Fs[js[i]-1] return ttf def Get_K(u,js): ttf = Bs[js[u-1]-1]-Cs[js[u-1]-1]*Es[js[u-1]-1] for i in range(len(js)): if i!=(u-1): ttf *= Fs[js[i]-1] return ttf
for i0 in range(t+1): l = d+1
for exps in Exp_res: if sum(exps) == l:
ft = 0 js = Get_js(exps)
M = get_M(js) M = M.change_ring(Zmod(p^(l-1))) W = M.inverse() W = W.change_ring(ZZ) H,J,K = 0,0,0 for u in range(1,l+1): for v in range(2): H += W[i0,u+l*v-1] * x0^v * Get_H(u,js) for u in range(1,l+1): for v in range(2): J +=W[i0,u+l*v-1] * x0^v * Get_J(u,js) for u in range(1,l+1): K += W[i0,u+l-1] * Get_K(u,js)
ft = (H+J+K) %p^d ff.append(ft)
res = LLL_mononimals(ff,[2^(256-192)]*6) e0 = res[0] gx = h0 + 2^192*e0 print(gx)
q = 11091427946088586538326341642081147908870478369361079372527019919422720334995105276694411660643624940624081943057158631573866470376362760554084973766546671 p = 7164778143059850549407362450312125862993617607522974512258134374430678370277239436047396839637741848829691805270124657225690426144253093636119389335193487 e = gx e = next_prime(e) d = inverse(e,(p-1)*(q-1)) hint = pow(hint2,d,p*q)
m = 1 rho = 50 N = hint a = ["pad"] + [hint1]
def attack(): t,k = 30,10 R = 2^rho indices = [] for i in product([i for i in range(t+1)] , repeat=m): if(sum(list(i)) <= t): indices.append(["pad"] + list(i))
PR = ZZ[tuple(f"X{i}" for i in range(m))] X = ["pad"] + list(PR.gens()) poly = [] monomials=set() for i in indices: f = 1 for ij in range(1,len(i)): f *= (X[ij] - a[ij])^i[ij] l = max(k-sum(i[1:]),0) f *= N^l poly.append(f) for mono in f.monomials(): monomials.add(mono)
L = Matrix(ZZ,len(poly),len(monomials)) monomials = sorted(monomials) for row,shift in enumerate(poly): for col,monomial in enumerate(monomials): L[row,col] = shift.monomial_coefficient(monomial)*monomial(*([R]*m))
res = L.LLL() vec1 = res[0]
h = 0 for idx,monomial in enumerate(monomials): h += (vec1[idx] // monomial(*([R]*m))) * monomial h = h.change_ring(ZZ) res1 = h.monic().roots()
if(res1 != []): return res1
print(attack()) r = int(attack()[0][0]) k = GCD(hint1,hint-r)
print(k)
from hashlib import sha256 secret = sha256(str(k).encode()).hexdigest()[:32] print("NepCTF{" + secret + "}")
|