17 acc = acc + 2**(n-i-1)
22 """Convertit un nombre en binaire"""
24 res = [0 for i in range(n)]
37 return 1 if a != b else 0
40 e1b,e2b = bin(e1,w),bin(e2,w)
41 d = dec([xorb(e1b[j],e2b[j]) for j in range(w)],w)
45 def forward(H_hat,x,message):
46 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
47 path = [[0 for _ in range(len(message))] for _ in range(len(x))]
48 nbblock = len(message)
49 wght = [infinity for _ in range(int(2**h))]
51 newwght = [0 for _ in range(int(2**h))]
52 rho = [1 for _ in range(len(x))]
55 for i in range(nbblock): # pour chaque bit du message
56 for j in range(w): # pour chaque colonne de H_hat
57 for k in range(nbblock): # pour chaque ligne de H
58 w0 = wght[k] + x[indx]*rho[indx]
59 w1 = wght[xor(k,H_hat[j],w)] + (1-x[indx])*rho[indx]
60 path[indx][k] = 1 if w1 < w0 else 0
61 newwght[k] = min(w0,w1)
63 wght = [t for t in newwght]
65 for j in range(int(2**(h-1))): # pour chaque colonne de H
66 wght[j] = wght[2*j + message[indm]]
67 wght = wght[:int(pow(2,h-1))] + [infinity for _ in range(int(pow(2,h)-pow(2,h-1)))]
75 def backward(H_hat,x,message,path):
76 (h,w) = int(log(max(H_hat),2))+1, len(H_hat)
77 state,indx,indm = message[len(message)-1],len(x)-1,len(message)-2
78 # l'initialisation de state n'est pas optimale...
79 nbblock = len(message)
80 y=[0 for _ in range(len(x))]
81 for i in range(nbblock):
84 for j in l: # pour chaque colonne de H_hat
85 y[indx] = path[indx][state]
86 state = xor(state,y[indx]*H_hat[j],w)
89 state = 2*state + message[indm]
98 # reflechir a une optimisation du la matrice H_hat
99 path = forward(H_hat,x,message)
101 return backward(H_hat,x,message,path)
105 x = [randint(0,1) for _ in range(50000)]
106 message = [randint(0,1) for _ in range(25000)]