-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_helper.c
242 lines (210 loc) · 6.19 KB
/
checker_helper.c
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <immintrin.h>
#include <stdint.h>
#include <stdlib.h>
#include "checker.h"
#include "prob_desc.h"
#ifdef VECT
#include "popcount256_16.h"
#include <stdio.h>
void init_all(__m256i *probes_a_all[NB_PR], __m256i *probes_b_all[NB_PR],
uint64_t *probes_r_all[NB_PR])
{
uint64_t i, j;
uint64_t c = 0;
for (i = 0; i < NB_PR; i++) {
probes_a_all[i] = aligned_alloc(32, radices[i]*sizeof(__m256i));
probes_b_all[i] = aligned_alloc(32, radices[i]*sizeof(__m256i));
probes_r_all[i] = calloc(radices[i], sizeof(uint64_t));
for (j = 0; j < radices[i]; j++) {
probes_a_all[i][j] = _mm256_loadu_si256((__m256i*)(probes_sh_a[c]));
probes_b_all[i][j] = _mm256_loadu_si256((__m256i*)(probes_sh_b[c]));
probes_r_all[i][j] = probes_r[c];
c++;
}
}
}
void init_sh_curr(__m256i *probes_sh_curr, __m256i *probes_sh_all[NB_PR],
uint64_t *combination, uint64_t k)
{
*probes_sh_curr = _mm256_setzero_si256();
uint64_t i, j;
for (i = 0; i < k; i++) {
j = combination[i];
*probes_sh_curr = _mm256_xor_si256(*probes_sh_curr, probes_sh_all[j][0]);
}
}
void init_r_curr(uint64_t *probes_r_curr, uint64_t *probes_r_all[NB_PR],
uint64_t *combination, uint64_t k)
{
uint64_t i, j;
*probes_r_curr = 0;
for (i = 0; i < k; i++) {
j = combination[i];
*probes_r_curr ^= probes_r_all[j][0];
}
}
void free_all(__m256i *probes_a_all[NB_PR], __m256i *probes_b_all[NB_PR],
uint64_t *probes_r_all[NB_PR])
{
uint64_t i;
for (i = 0; i < NB_PR; i++) {
free(probes_a_all[i]);
free(probes_b_all[i]);
free(probes_r_all[i]);
}
}
int check_attack_ni(uint64_t nb_probes, uint64_t r_sum, __m256i sh_sum_a,
__m256i sh_sum_b)
{
uint64_t nb_rand = __builtin_popcountl(r_sum);
uint64_t nb_a;
uint64_t nb_b;
/* Is there enough probes left to cancel out remaining random? */
if (nb_rand + nb_probes <= D) {
/* Is the minimum number of probes needed to build an attack on the
* a_i strictly greater than D? */
nb_a = popcount256_16(sh_sum_a);
if (nb_rand + nb_probes + (NB_SH - nb_a) <= D) {
return 1;
}
/* Same on the b_j */
nb_b = popcount256_16(sh_sum_b);
if (nb_rand + nb_probes + (NB_SH - nb_b) <= D) {
return 1;
}
}
return 0;
}
int check_attack_sni(uint64_t nb_probes, uint64_t nb_internal, uint64_t r_sum,
__m256i sh_sum_a, __m256i sh_sum_b)
{
uint64_t nb_rand = __builtin_popcountl(r_sum);
uint64_t nb_a;
uint64_t nb_b;
/* Is there enough probes left to cancel out remaining random? */
if (nb_rand + nb_probes <= D) {
/* Probes used to eliminate random are internal probes */
nb_internal += nb_rand;
nb_a = popcount256_16(sh_sum_a);
if (nb_a > nb_internal) {
return 1;
}
nb_b = popcount256_16(sh_sum_b);
if (nb_b > nb_internal) {
return 1;
}
}
return 0;
}
#else
void probes_sh_xor(uint64_t probes_sh_dst[NB_SH][SIZE_SH],
uint64_t probes_sh_src[NB_SH][SIZE_SH])
{
uint64_t i, j;
for (i = 0; i < NB_SH; i++) {
for (j = 0; j < SIZE_SH; j++) {
probes_sh_dst[i][j] ^= probes_sh_src[i][j];
}
}
}
void probes_r_xor(uint64_t probes_r_dst[SIZE_R], uint64_t probes_r_src[SIZE_R])
{
uint64_t i;
for (i = 0; i < SIZE_R; i++) {
probes_r_dst[i] ^= probes_r_src[i];
}
}
uint64_t probes_r_count(uint64_t probes_r[SIZE_R])
{
uint64_t i, c = 0;
for (i = 0; i < SIZE_R; i++) {
c += __builtin_popcountl(probes_r[i]);
}
return c;
}
uint64_t probes_sh_count(uint64_t probes_sh[NB_SH][SIZE_SH])
{
uint64_t i, j, c = 0;
for (i = 0; i < NB_SH; i++) {
for (j = 0; j < SIZE_SH; j++) {
if (probes_sh[i][j] != 0) {
c += 1;
break;
}
}
}
return c;
}
void init_sh_curr(uint64_t probes_sh_curr[NB_SH][SIZE_SH],
uint64_t probes_sh_all[NB_PR][NB_SH][SIZE_SH], uint64_t *combination,
uint64_t k)
{
uint64_t i, j;
/* Zero init */
for (i = 0; i < NB_SH; i++) {
for (j = 0; j < SIZE_SH; j++) {
probes_sh_curr[i][j] = 0;
}
}
/* Adding the right probes */
for (i = 0; i < k; i++) {
j = combination[i];
probes_sh_xor(probes_sh_curr, probes_sh_all[j]);
}
}
void init_r_curr(uint64_t probes_r_curr[SIZE_R], uint64_t *combination, uint64_t k)
{
uint64_t i, j;
for (i = 0; i < SIZE_R; i++) {
probes_r_curr[i] = 0;
}
for (i = 0; i < k; i++) {
j = combination[i];
probes_r_xor(probes_r_curr, probes_r[j]);
}
}
int check_attack_ni(uint64_t nb_probes, uint64_t r_sum[SIZE_R],
uint64_t sh_sum_a[NB_SH][SIZE_SH], uint64_t sh_sum_b[NB_SH][SIZE_SH])
{
uint64_t nb_rand = probes_r_count(r_sum);
uint64_t nb_a;
uint64_t nb_b;
/* Is there enough probes left to cancel out remaining random? */
if (nb_rand + nb_probes <= D) {
/* Is the minimum number of probes needed to build an attack on the
* a_i strictly greater than D? */
nb_a = probes_sh_count(sh_sum_a);
if (nb_rand + nb_probes + (NB_SH - nb_a) <= D) {
return 1;
}
/* Same on the b_j */
nb_b = probes_sh_count(sh_sum_b);
if (nb_rand + nb_probes + (NB_SH - nb_b) <= D) {
return 1;
}
}
return 0;
}
int check_attack_sni(uint64_t nb_probes, uint64_t nb_internal,
uint64_t r_sum[SIZE_R], uint64_t sh_sum_a[NB_SH][SIZE_SH],
uint64_t sh_sum_b[NB_SH][SIZE_SH])
{
uint64_t nb_rand = probes_r_count(r_sum);
uint64_t nb_a;
uint64_t nb_b;
/* Is there enough probes left to cancel out remaining random? */
if (nb_rand + nb_probes <= D) {
/* Probes used to eliminate random are internal probes */
nb_internal += nb_rand;
nb_a = probes_sh_count(sh_sum_a);
if (nb_a > nb_internal) {
return 1;
}
nb_b = probes_sh_count(sh_sum_b);
if (nb_b > nb_internal) {
return 1;
}
}
return 0;
}
#endif /* VECT */