@@ -18,6 +18,35 @@ def is_share_better(x1_s, x2_s):
18
18
return apply_mask (x1_s , x2_s ) == x1_s
19
19
20
20
21
+ def number_missing_share (x1_s , x2_s ):
22
+ res = 0
23
+ for i1 , i2 in zip (x1_s , x2_s ):
24
+ if i1 and not i2 :
25
+ res += 1
26
+ return res
27
+
28
+
29
+ def xor (x1_r , x2_r ):
30
+ return [i1 ^ ^ i2 for i1 ,i2 in zip (x1_r , x2_r )]
31
+
32
+
33
+ def hamming_weight (x ):
34
+ res = 0
35
+ for i in x :
36
+ if i :
37
+ res += 1
38
+ return res
39
+
40
+ def share_completion (res , x1_s , x_r_str , i , j , n_r = 0 ):
41
+ for x2_s in res [j ][x_r_str ]:
42
+ n_s = number_missing_share (x1_s , x2_s )
43
+ # using n elementary probes
44
+ if n_r + n_s + j <= i :
45
+ return True
46
+
47
+ return False
48
+
49
+
21
50
def check (M , Mb , mask_r , mask_s , d ):
22
51
"""
23
52
For given matrices `M` and `Mb` containing the probe description before
@@ -65,17 +94,30 @@ def check(M, Mb, mask_r, mask_s, d):
65
94
66
95
found = False
67
96
for j in range (i , - 1 , - 1 ):
68
- if x1_r_str not in res [j ]:
69
- continue
70
- for x2_s in res [j ][x1_r_str ]:
71
- if is_share_better (x1_s , x2_s ):
72
- found = True
97
+ if x1_r_str in res [j ]:
98
+ # Check for already correct randomness
99
+ found = share_completion (res , x1_s , x1_r_str , i , j )
100
+ if found :
73
101
break
102
+
103
+ # Tries to fix randomness with elementary probes
104
+ for x2_r_str in res [j ]:
105
+ x2_r = [int (v ) for v in x2_r_str ]
106
+ xf_r = xor (x2_r , x1_r )
107
+ n_r = hamming_weight (xf_r )
108
+ # number of elementary random probes already too high
109
+ if n_r + j > i :
110
+ continue
111
+ # x2_r corrected with xf_r
112
+ found = share_completion (res , x1_s , x2_r_str , i , j , n_r )
113
+ if found :
114
+ break
115
+
74
116
if found :
75
117
break
76
118
77
119
if not found :
78
- print ("Contradiction found! " )
120
+ print ("Cannot remove the probes " )
79
121
return False
80
122
return True
81
123
@@ -110,7 +152,7 @@ def gen_matrices_and_masks(filename):
110
152
# Filtering out probes
111
153
for i in range (len (line )):
112
154
s1 = ' ' .join (line [:i + 1 ])
113
- # HERE IS the criterion to filter out probes
155
+ # HERE IS the criterion to filter out probes (see Section 5)
114
156
if not s1 .count ('s' ) % 2 and i + 1 < len (line ) and 's' in line [i + 1 ]:
115
157
to_del .append (i )
116
158
probes_todel .append (s1 )
0 commit comments