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
| from sage.stats.distributions.discrete_gaussian_integer import DiscreteGaussianDistributionIntegerSampler from sage.modules.free_module_integer import IntegerLattice import numpy as np from Crypto.Util.number import * from hashlib import sha1
def Babai_closest_vector(M, G, target): small = target for _ in range(5): for i in reversed(range(M.nrows())): c = ((small * G[i]) / (G[i] * G[i])).round() small -= M[i] * c return target - small
q = 401 m = 64 n = 16 a = 0.025 num = 48 data=open('data.txt','r').readlines() A=Matrix(ZZ,eval(data[0].strip())) cipher=eval(data[1].strip()) print(len(cipher))
''' #find the same s vector that output bit '1' res='' for k in range(num): rt=cipher[k][0] Lattice = matrix(ZZ, m + n, m) for i in range(m): for j in range(n): Lattice[m + j, i] = A[i][j] Lattice[i, i] = q
lattice = IntegerLattice(Lattice, lll_reduce=True) print("LLL done") gram = lattice.reduced_basis.gram_schmidt()[0] target = vector(ZZ,rt) cv = Babai_closest_vector(lattice.reduced_basis, gram, target) #print("Closest Vector: {}".format(cv)) error=cv-target print("Error: {}".format(error)) if any([abs(e)>40 for e in error]): res+='0' else: res+='1' print(res) A = matrix(Zmod(q), A) try: s = A.solve_right(cv) print('s: {}'.format(s)) except: print("no solution") continue print(res) ''' s=(195, 202, 322, 287, 230, 311, 396, 58, 242, 191, 117, 41, 248, 264, 139, 291) s=vector(GF(q),s) A=Matrix(GF(q),A) print(A*s) res='' for k in range(num): tmp=[] for l in range(num*8): c=vector(ZZ,cipher[k][l]) As=vector(GF(q),A*s) t=c-As t=[tt if tt<(q//2) else q-tt for tt in t] tmp.extend(t) if all([ tt<40 for tt in t]): res+='1' else: res+='0' print(res)
my_secret_bits=list(map(int,res)) flag = 'flag{' + sha1(str(my_secret_bits).encode()).hexdigest() + '}' print(flag)
|