6 infinity = 100000000000
19 acc = acc + 2**(n-i-1)
24 """Convertit un nombre en binaire"""
26 res = [0 for i in xrange(n)]
39 return 1 if a != b else 0
42 e1b,e2b = bin(e1,h),bin(e2,h)
43 d = dec([xorb(e1b[j],e2b[j]) for j in xrange(h)],h)
46 def lit(d,(indx,indy)):
55 def forward(H_hat,x,message,lnm,rho):
56 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
59 wght = [infinity for _ in xrange(int(2**h))]
61 newwght = [0 for _ in xrange(int(2**h))]
63 # rho= [1 for _ in xrange(len(x))]
66 while i < nbblock: # pour chaque bit du message
67 for j in xrange(w): # pour chaque colonne de H_hat
69 print indx, "en entrant",wght
71 while k < int(2**h): # pour chaque ligne de H
72 w0 = wght[k] + x[indx]*rho[indx]
73 w1 = wght[xor(k,H_hat[j],h)] + (1-x[indx])*rho[indx]
79 newwght[k] = min(w0,w1)
82 wght = [t for t in newwght]
84 print " apres calcul",wght
86 for j in xrange(int(2**(h-1))): # pour chaque colonne de H
87 wght[j] = wght[2*j + message[indm]]
88 wght = wght[:int(pow(2,h-1))] + [infinity for _ in xrange(int(pow(2,h)-pow(2,h-1)))]
94 start = np.argmin(wght)
98 def backward(start,H_hat,x,message,lnm,path):
99 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
100 indx,indm = len(x)-1,lnm-1
101 state = 2*start + message[indm]
103 # l'initialisation de state n'est pas optimale...
110 for j in l: # pour chaque colonne de H_hat
111 y[indx] = lit(path,(indx,state))
112 state = xor(state,y[indx]*H_hat[j],h)
114 state = 2*state + message[indm]
117 return [int(t) for t in y]
124 def trouve_H_hat(n,m,h):
127 index = min(int(alpha),9)
131 4 : [81, 95, 107, 121],
132 5 : [75, 95, 97, 105, 117],
133 6 : [73, 83, 95, 103, 109, 123],
134 7 : [69, 77, 93, 107, 111, 115, 121],
135 8 : [69, 79, 81, 89, 93, 99, 107, 119],
136 9 : [69, 79, 81, 89, 93, 99, 107, 119, 125]
141 def stc(x,rho,message,hhat=-1):
143 (mat,taille_suff) = trouve_H_hat(len(x),lnm,7) \
144 if hhat == -1 else (hhat,len(hhat)*lnm)
145 x_b = x[:taille_suff]
146 (start,path) = forward(mat,x_b,message,lnm,rho)
147 return (x_b,backward(start,mat,x_b,message,lnm,path),mat)
167 def prod(H_hat,lnm,y):
168 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
171 V=[0 for _ in range(len(y))]
173 while i < lnm: # pour chaque ligne
174 V=[0 for _ in range(len(y))]
175 k = max([(i-h+1)*w,0])
177 for j in xrange(min([i+1,h])): #nbre de blocks presents sur la ligne i
178 for l in xrange(w): # pour chaque collone de H_hat
179 V[k] = bin(H_hat[l],h)[h-i-1+j+dec]
182 sol.append(np.dot(np.array(V),np.array(y)))
187 #print "dot",np.dot(H,y),H.shape
189 return sol#list(np.dot(H,y))
196 if x[i] % 2 != y[i]%2 :
204 x = [randint(0,1) for _ in xrange(65000)]
205 rho = [randint(1,9) for _ in xrange(65000)]
206 message = [randint(0,1) for _ in xrange(26000)]
210 #(x_b,y,H_hat) = stc(x,rho,message)
211 # x_b est la sous partie de x qui va etre modifiee
212 # y est le vecteur des bits modifies
213 # H_hat est la sous-matrice retenue qui est embarquee dans H
221 #message2 = [x%2 for x in prod(H_hat,len(message),y)]
223 print max([message[l]-message2[l] for l in xrange(len(message))])
224 print equiv(message,message2)
229 while count < 1000 and eval :
230 lx = randint(5000,10000)
231 x = [randint(0,1) for _ in xrange(lx)]
232 rho = [randint(1,9) for _ in xrange(lx)]
234 lm = randint(lx/10,lx/2)
235 message = [randint(0,1) for _ in xrange(lm)]
236 (x_b,y,H_hat) = stc(x,rho,message)
237 eval = equiv(message, prod(H_hat,len(message),y))
249 x = [0, 1, 0, 1, 1, 1, 0, 0]
250 rho = [4, 2, 5, 4, 9, 8, 7, 6]
253 print stc(x,rho,message,[1,3])