#sage p = a = b = E = EllipticCurve(GF(p),[a,b]) #Weierstrasss形式(椭圆曲线标准形式) P = E(, ) Q = E(, ) k = discrete_log(Q, P, operation='+') #或 discrete_log_rho(a,base,ord,operation) #或 bsgs(base,a,bounds,operation) #或 k = Q.log(P)
from Crypto.Util.number import * from Crypto.Cipher import AES from Crypto.Util.Padding import pad from random import randint import hashlib from secrets import flag
# https://github.com/Neobeo/HackTM2023/blob/main/solve420.sage # faster LLL reduction to replace `M.LLL()` wiith `flatter(M)` defflatter(M, **kwds): from subprocess import check_output from re import findall M = matrix(ZZ,M) # compile https://github.com/keeganryan/flatter and put it in [imath:0]PATH 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)))
defcheckMatrix(M, wl=[-1, 1]): M = [list(_) for _ inlist(M)] ml = list(set(flatten(M))) logging.debug(ml) returnsorted(ml) == sorted(wl)
defNguyen_Stern(h, m, n, M): B = matrix(ZZ, m) B[0, 0] = M h0i = Integer(h[0]).inverse_mod(M) for i inrange(1, m): B[i, 0] = - h[i] * h0i B[i, i] = 1 #L = B.BKZ() # slooooooow L = flatter(B) logging.info('flatter done.')
''' vh = vector(Zmod(M), h) logging.debug([vector(Zmod(M), list(l)) * vh for l in L]) '''
''' try: Lx_real = matrix(ZZ, [xi + [0] * (m - len(xi)) for xi in X]) rsc = Lxc.row_space() logging.debug([xi in rsc for xi in Lx_real]) except: pass '''
e = matrix(ZZ, [1] * m) B = block_matrix([[-e], [2*Lxc]]) Lx = B.BKZ() logging.info('BKZ done.') assert checkMatrix(Lx) assertlen(set(Lx[0])) == 1
Lx = Lx[1:] E = matrix(ZZ, [[1for c inrange(Lxc.ncols())] for r inrange(Lxc.nrows())]) Lx = (Lx + E) / 2
Lx2 = [] e = vector(ZZ, [1] * m) rsc = Lxc.row_space() for lx in Lx: if lx in rsc: Lx2 += [lx] continue lx = e - lx if lx in rsc: Lx2 += [lx] continue logging.warning('Something wrong?') Lx = matrix(Zmod(M), Lx2)
vh = vector(Zmod(M), h) va = Lx.solve_left(vh) return Lx, va
m = 200#x是n个m维向量组成 n = 100#n是x_i和a_i的个数 M = #模数 h = #给定的最终向量h
Lx, va = Nguyen_Stern(h, m, n, M) print("向量a:",va) print("矩阵x:",Lx)
(代码这里先偷了,后续会自己实现的qwq)
附上Arch Linux安装flatter步骤:
1 2 3 4 5 6 7
sudo pacman -S gmp mpfr eigen base-devel gcc git cmake git clone https://github.com/keeganryan/flatter.git cd flatter mkdir build && cd ./build cmake -DCMAKE_INSTALL_PREFIX=/usr .. make sudo make install
DSA
密钥生成
选择一个哈希函数(通常为SHA1)
选择密钥长度L和N
选择N比特的素数q
选择L比特的素数p,使p-1是q的倍数
选择g,使满足$g^k\equiv 1\ mod p$ 的最小正整数k为q。可用$g=h^{\frac {p-1}{q}}$来获得g,其中1 < h <p-1