defgao_one(self): self.conn.sendlineafter('[Q]uit\n', 'm') for i inrange(1100 // 16): s = self.conn.recvline().strip().decode() wtf = re.findall(r'\((.*?)\)', s) wtf = [list(map(int, x.split(' '))) for x in wtf] self.wtf.extend(wtf)
defgao(self): for i in trange(10): self.gao_one() B, D = 5, 110 keys = [] for i inrange(len(self.wtf[0])): min_wtf = min([x[i] for x in self.wtf]) key_i = min_wtf + B * D keys.append(key_i) self.submit(keys) self.conn.interactive()
import sys from Crypto.Util.number import * from collections import Counter from tqdm import trange import pickle
B, n = 5, 19 D, t = 110, 128
defrandroad(B): return vector(ZZ,[randint(-B, B) for _ inrange(n)])
defroadband(): return randroad(B * (D + 1))
defsilky(key): whileTrue: R = roadband() _R = R - key ifmin(_R) >= - B * D andmax(_R) <= B * D: return R
ans = [[Counter() for j inrange(11)] for i inrange(t)]
defget_data(): global B, n, D, t l = int(4 * D * B / t) c, key = 0, randroad(B) motherfucker = [] for i inrange(10): R = [silky(key) for _ inrange(int(l * t // 2))] R = R[:len(R) // 16 * 16] motherfucker.extend(R)
return key, motherfucker
deftrain(key, motherfucker): for i inrange(n): wtf = [mf[i] for mf in motherfucker] ct = Counter(wtf) ki = key[i] ans[i][ki + 5].update(ct)
import math
defcounter_to_prob(counter): """将Counter转换为概率分布(归一化)""" total = sum(counter.values()) return {k: v / total for k, v in counter.items()}
defload_ans(self): withopen('2.pickle', 'rb') as f: self.ans = pickle.load(f)
defgao_one(self): self.conn.sendlineafter('[Q]uit\n', 'm') for i inrange(1100 // 16): s = self.conn.recvline().strip().decode() wtf = re.findall(r'\((.*?)\)', s) wtf = [list(map(int, x.split(' '))) for x in wtf] self.wtf.extend(wtf) defget_key(self): ans, wtf = self.ans, self.wtf # Transpose wtf wtf = list(zip(*wtf)) key_ = [] for i inrange(self.t): ct = Counter(wtf[i]) current_min_ce = 1e4 current_key = -999 for j inrange(11): ce = chi_square_distance(ct, ans[i][j]) # ce = cross_entropy(ct, ans [i][j]) if ce < current_min_ce: current_min_ce = ce current_key = j - 5
defpollig_hellman_add_group(y, g, group_order, identity=None, verbose=True): # we do not handle the case p^e with large e (e >= 2) fs = factor(group_order) mods = [] dlogs = [] for p, e in fs: if verbose: print(f"[+] Sub dlog in group with order {p}^{e}") sub_order = ZZ(p ** e) sub_mulc = group_order // sub_order # new_y = sub_mulc * y # new_g = sub_mulc * g # sub_log = bsgs_add_group(new_y, new_g, sub_order, identity, False) # sub_log = bsgs_add_group(new_y, new_g, sub_order, identity, False) new_y = y**sub_mulc new_g = g**sub_mulc sub_log = new_y.log(new_g)
if verbose: print(f" Sub dlog x = {sub_log} % {sub_order}") mods.append(sub_order) dlogs.append(sub_log) return crt(dlogs, mods)
p = d =
R.<x> = PolynomialRing(GF(p)) f = x^2 + 13 * x + 37 f = R(f) F.<g> = GF(p^2, modulus = f) E = EllipticCurve(F, [0, d])
encP =
a = b =
a = F(a) b = F(b)
# gen P 随机生成一个阶为 p+1 的点 # while True: # P = E.random_element() # if (p+1)//2 * P != E(0): # break
# 发送给服务器的点 P = P =
P = E(P)
P_order = P.order() if P_order != p + 1: raise Exception(f'GG: {P_order = }') exit()
whileTrue: Q = E.random_element() if (p+1)//2 * Q != E(0) and P.weil_pairing(Q, p+1) != 1: print(Q) break # send P and get cP # 从服务器的点 kP cP
defgao_one(self): m = self.msg self.conn.sendlineafter('[Q]uit\n', 's') self.conn.sendlineafter('message: \n', m) self.conn.recvuntil('r = ') r = eval(self.conn.recvline()) self.conn.recvuntil('s = ') s = eval(self.conn.recvline()) self.rlist.append(r) self.slist.append(s)
defgao_uv(self): self.conn.sendlineafter('[Q]uit\n', 'g') self.conn.sendlineafter('seed: \n', '0') v = eval(self.conn.recvline().strip().decode().split('=')[1])
self.conn.sendlineafter('[Q]uit\n', 'g') self.conn.sendlineafter('seed: \n', '1') u = eval(self.conn.recvline().strip().decode().split('=')[1]) - v
self.v = v % self._n self.u = u % self._n print(f'{self.u = }') print(f'{self.v = }')
defsolve_k(self): u, v = self.u, self.v r0, r1 = self.rlist s0, s1 = self.slist n = self._n h = self.h A = matrix(Zmod(n), [[ s0, -r0], [u*s1, -r1]]) b = vector(Zmod(n), [h, h - v * s1]) k, x = A.solve_right(b) print(f'{long_to_bytes(int(x)) = }')
defgao(self): self.gao_uv() for i in trange(2): self.gao_one() self.solve_k()