6 infinity = 100000000000
18 acc = acc + 2**(n-i-1)
23 """Convertit un nombre en binaire"""
25 res = [0 for i in xrange(n)]
38 return 1 if a != b else 0
41 e1b,e2b = bin(e1,h),bin(e2,h)
42 d = dec([xorb(e1b[j],e2b[j]) for j in xrange(h)],h)
45 def lit(d,(indx,indy)):
54 def forward(H_hat,x,message,lnm,rho):
55 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
58 wght = [infinity for _ in xrange(int(2**h))]
60 newwght = [0 for _ in xrange(int(2**h))]
62 # rho= [1 for _ in xrange(len(x))]
65 while i < nbblock: # pour chaque bit du message
66 for j in xrange(w): # pour chaque colonne de H_hat
67 #print indx, "en entrant",wght
69 while k < int(2**h): # pour chaque ligne de H
70 w0 = wght[k] + x[indx]*rho[indx]
71 w1 = wght[xor(k,H_hat[j],h)] + (1-x[indx])*rho[indx]
77 newwght[k] = min(w0,w1)
80 wght = [t for t in newwght]
81 #print " apres calcul",wght
83 for j in xrange(int(2**(h-1))): # pour chaque colonne de H
84 wght[j] = wght[2*j + message[indm]]
85 wght = wght[:int(pow(2,h-1))] + [infinity for _ in xrange(int(pow(2,h)-pow(2,h-1)))]
88 start = np.argmin(wght)
92 def backward(start,H_hat,x,message,lnm,path):
93 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
94 indx,indm = len(x)-1,lnm-1
95 state = 2*start + message[indm]
97 # l'initialisation de state n'est pas optimale...
104 for j in l: # pour chaque colonne de H_hat
105 y[indx] = lit(path,(indx,state))
106 state = xor(state,y[indx]*H_hat[j],h)
108 state = 2*state + message[indm]
111 return [int(t) for t in y]
118 def trouve_H_hat(n,m,h):
122 index = min(int(alpha),9)
126 4 : [81, 95, 107, 121],
127 5 : [75, 95, 97, 105, 117],
128 6 : [73, 83, 95, 103, 109, 123],
129 7 : [69, 77, 93, 107, 111, 115, 121],
130 8 : [69, 79, 81, 89, 93, 99, 107, 119],
131 9 : [69, 79, 81, 89, 93, 99, 107, 119, 125]
136 def stc(x,rho,message):
138 (mat,taille_suff) = trouve_H_hat(len(x),len(message),7)
139 x_b = x[:taille_suff]
140 (start,path) = forward(mat,x_b,message,lnm,rho)
141 return (x_b,backward(start,mat,x_b,message,lnm,path),mat)
161 def prod(H_hat,lnm,y):
162 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
165 V=[0 for _ in range(len(y))]
167 while i < lnm: # pour chaque ligne
168 V=[0 for _ in range(len(y))]
169 k = max([(i-h+1)*w,0])
171 for j in xrange(min([i+1,h])): #nbre de blocks presents sur la ligne i
172 for l in xrange(w): # pour chaque collone de H_hat
173 V[k] = bin(H_hat[l],h)[h-i-1+j+dec]
176 sol.append(np.dot(np.array(V),np.array(y)))
181 #print "dot",np.dot(H,y),H.shape
183 return sol#list(np.dot(H,y))
190 if x[i] % 2 != y[i]%2 :
198 x = [randint(0,1) for _ in xrange(65000)]
199 rho = [randint(1,9) for _ in xrange(65000)]
200 message = [randint(0,1) for _ in xrange(26000)]
204 #(x_b,y,H_hat) = stc(x,rho,message)
205 # x_b est la sous partie de x qui va etre modifiee
206 # y est le vecteur des bits modifies
207 # H_hat est la sous-matrice retenue qui est embarquee dans H
215 #message2 = [x%2 for x in prod(H_hat,len(message),y)]
217 print max([message[l]-message2[l] for l in xrange(len(message))])
218 print equiv(message,message2)
223 while count < 100 and eval :
224 lx = randint(500,1000)
225 x = [randint(0,1) for _ in xrange(lx)]
226 rho = [randint(1,9) for _ in xrange(lx)]
228 lm = randint(lx/10,lx/2)
229 message = [randint(0,1) for _ in xrange(lm)]
230 (x_b,y,H_hat) = stc(x,rho,message)
231 eval = equiv(message, prod(H_hat,len(message),y))