-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmitm.cpp
More file actions
410 lines (341 loc) · 11.8 KB
/
mitm.cpp
File metadata and controls
410 lines (341 loc) · 11.8 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include <gtest/gtest.h>
#include <iostream>
#include <omp.h>
#include "decoding/challenges/100.h"
#include "mitm.h"
using ::testing::EmptyTestEventListener;
using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestEventListeners;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
TEST(EnumHashMap, alloc) {
constexpr uint32_t p = 2, l = 10;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint16_t, p>;
constexpr static SimpleHashMapConfig s{.bucketsize=10u, .nrbuckets=1u<<l, .threads=1};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
HM *hm = new HM{};
static constexpr ConfigEnumHashMap config{k+l, p, l, 1};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
EnumHashMap<config, HM> mitm{data, hm};
free(data);
delete hm;
}
TEST(EnumHashMap, enumeration_changelist) {
// as k = 50 you cannot choose l to big
constexpr uint32_t p = 2, l = 5, epsilon=10;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint16_t, p>;
constexpr static SimpleHashMapConfig s{.bucketsize=4u, .nrbuckets=1u<<l, .threads=1};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
HM *hm = new HM{};
static constexpr ConfigEnumHashMap config{k+l, p, l, 1, epsilon, false, true};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) {
data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
}
/// create the changelist
chase<(k+l)/2 + epsilon, p, 2> c;
std::vector<std::pair<uint16_t, uint16_t>> cL;
cL.resize(c.list_size());
size_t ctr = 0;
c.enumerate([&, this](const uint16_t p1, const uint16_t p2){
cL[ctr] = std::pair<uint16_t, uint16_t>{p1, p2};
ctr += 1;
});
EnumHashMap<config, HM> mitm{data, hm, cL.data()};
mitm.print();
for (uint32_t simd = 0; simd < 2; ++simd) {
mitm.step(0, 0, simd);
for (size_t i = 0; i < (k+l)/2; i++){
HM::load_type load = 0;
size_t k = hm->find(data[i], load);
EXPECT_GT(load, 0);
if constexpr (p == 1) {
EXPECT_EQ(k, data[i] * s.bucketsize);
}
}
}
free(data);
delete hm;
}
TEST(EnumHashMap, enumeration) {
// as k = 50 you cannot choose l to big
constexpr uint32_t p = 2, l = 5, epsilon=10;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint16_t, p>;
constexpr static SimpleHashMapConfig s{.bucketsize=10u, .nrbuckets=1u<<l, .threads=1};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
HM *hm = new HM{};
static constexpr ConfigEnumHashMap config{k+l, p, l, 1, epsilon};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) {
data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
}
EnumHashMap<config, HM> mitm{data, hm};
mitm.print();
for (uint32_t simd = 0; simd < 2; ++simd) {
mitm.step(0, 0, simd);
for (size_t i = 0; i < (k+l)/2 + epsilon; i++){
HM::load_type load = 0;
size_t k = hm->find(data[i], load);
EXPECT_GT(load, 0);
if constexpr (p == 1) {
EXPECT_EQ(k, data[i] * s.bucketsize);
}
}
}
free(data);
delete hm;
}
TEST(EnumHashMap, enumeration_index) {
constexpr uint32_t p = 2, l = 5;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint32_t, 1>;
constexpr static SimpleHashMapConfig s{.bucketsize=10u, .nrbuckets=1u<<l, .threads=1};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
HM *hm = new HM{};
static constexpr ConfigEnumHashMap config{k+l, p, l, 1, true};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) {
data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
}
EnumHashMap<config, HM> mitm{data, hm};
mitm.print();
for (uint32_t simd = 0; simd < 2; ++simd) {
mitm.step(0, 0, simd);
for (size_t i = 0; i < (k+l)/2; i++){
HM::load_type load = 0;
size_t k = hm->find(data[i], load);
EXPECT_GT(load, 0);
EXPECT_EQ(k, data[i] * s.bucketsize);
}
}
free(data);
delete hm;
}
TEST(EnumHashMap, multithreaded_enumeration) {
constexpr uint32_t p = 2, l = 17, threads=2;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint16_t, p>;
constexpr static SimpleHashMapConfig s{.bucketsize=10u, .nrbuckets=1u<<l, .threads=threads};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
HM *hm = new HM{};
hm->print();
static constexpr ConfigEnumHashMap config{k+l, p, l, threads};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) {
data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
}
auto *mitm = new EnumHashMap<config, HM>{data, hm};
for (uint32_t simd = 0; simd < 2; ++simd) {
#pragma omp parallel default(none) shared(mitm, hm) num_threads(threads)
{
const uint32_t tid = omp_get_thread_num();
mitm->step(0, tid, false);
}
for (size_t i = 0; i < (k+l)/2; i++){
HM::load_type load = 0;
size_t k = hm->find(data[i], load);
// NOTE: only valid if p == 1
if constexpr (p == 1) {
EXPECT_GT(load, 0);
EXPECT_EQ(k, data[i] * s.bucketsize);
}
}
}
free(data);
delete hm;
delete mitm;
}
TEST(CollisionHashMap, enumeration) {
constexpr uint32_t p = 2, l = 5;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint16_t, p>;
constexpr static SimpleHashMapConfig s{.bucketsize=10u, .nrbuckets=1u<<l, .threads=1};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
using HM_DataType = HM::data_type;
using HM_DataType_IndexType = HM_DataType::index_type;
HM *hm = new HM{};
static constexpr ConfigEnumHashMap config{k+l, p, l, 1};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) {
data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
}
EnumHashMap<config, HM> base{data, hm};
base.step(0);
CollisionHashMap<config, HM> mitm{data, hm};
mitm.print();
auto f = [&](const T lp1, const T lp2,
HM_DataType_IndexType *index1,
HM_DataType_IndexType *index2,
const uint32_t nr_cols) {
(void) nr_cols;
bool found = false;
const T a = lp1 ^ lp2;
for (size_t i = 0; i < k + l; ++i) {
if (a == data[i]) {
found = true;
break;
}
}
if (!found) {
std::cout << a << " " << index1[0] << "," << index2[0] << std::endl;
}
EXPECT_EQ(found, true);
};
mitm.step(0, f, 0, false);
for (size_t i = 0; i < (k+l)/2; i++){
HM::load_type load = 0;
const size_t m = hm->find(data[i], load);
EXPECT_GT(load, 0);
EXPECT_EQ(m, data[i] * s.bucketsize);
}
free(data);
delete hm;
}
TEST(CollisionHashMap, enumerationSIMD) {
constexpr uint32_t p = 1, l = 4;
using T = MinLogTypeTemplate<l, 32>;
using V = CollisionType<T, uint32_t, 1>;
constexpr static SimpleHashMapConfig s{.bucketsize=2u, .nrbuckets=1u<<l, .threads=1};
using HM = SimpleHashMap<T, V, s, Hash<T, 0, l, 2>>;
using HM_DataType = HM::data_type;
using HM_DataType_IndexType = HM_DataType::index_type;
HM *hm = new HM{};
static constexpr ConfigEnumHashMap config{k+l, p, l, 1, true};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) {
data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
}
EnumHashMap<config, HM> base{data, hm};
base.step(0);
CollisionHashMap<config, HM> mitm{data, hm};
mitm.print();
auto f = [&](const T lp1, const T lp2,
HM_DataType_IndexType *index1,
HM_DataType_IndexType *index2,
const uint32_t nr_cols) __attribute__((always_inline)) {
bool found = true;
(void) lp1;
(void) lp2;
for (uint32_t i = 0; i < nr_cols; ++i) {
found &= data[index1[i]] == data[index2[i]];
}
EXPECT_EQ(found, true);
};
mitm.step(0, f, 0, true);
for (size_t i = 0; i < (k+l)/2; i++){
HM::load_type load = 0;
const size_t m = hm->find(data[i], load);
EXPECT_GT(load, 0);
EXPECT_EQ(m, data[i] * s.bucketsize);
}
free(data);
delete hm;
}
//TEST(CollisionHashMapD2, enumeration) {
// constexpr uint32_t p = 1, l1=4, l2=6, l=l1+l2;
// using T = MinLogTypeTemplate<l, 32>;
// using V1 = CollisionType<T, 1>;
// using V2 = CollisionType<T, 2>;
//
// constexpr static SimpleHashMapConfig s1{.bucketsize=8u, .nrbuckets=1u<<l1, .threads=1};
// constexpr static SimpleHashMapConfig s2{.bucketsize=16u, .nrbuckets=1u<<l2, .threads=1};
// using HM1 = SimpleHashMap<T, V1, s1, Hash<T, 0, l1>>;
// using HM2 = SimpleHashMap<T, V2, s2, Hash<T, 0, l2>>;
// HM1 *hm1 = new HM1{};
// HM2 *hm2 = new HM2{};
//
// using HM_DataType = HM2::data_type;
// using HM_DataType_IndexType = HM_DataType::index_type;
//
// static constexpr ConfigEnumHashMap configD1{k+l, p, l1, 1};
// static constexpr ConfigEnumHashMapD2 configD2{configD1, l2};
// T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
// for (uint32_t i = 0; i < k+l; ++i) {
// data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);
// }
//
// EnumHashMap<configD1, HM1> mitmD1{data, hm1};
// CollisionHashMapD2<configD2, HM1, HM2> mitmD2{data, hm1, hm2};
//
// auto f = [](const T k, HM_DataType_IndexType *rows){
// std::cout << k << std::endl;
// };
// mitmD1.step(0, 0, false);
// mitmD2.step(0, f, false);
//
// for (size_t i = 0; i < (k+l)/2; i++){
// //size_t load = 0;
// //size_t k = hm->find(data[i], load);
// //EXPECT_GT(load, 0);
// //EXPECT_EQ(k, data[i] * s.bucketsize);
// }
//
//
// free(data);
// delete hm1;
// delete hm2;
//}
//
TEST(CollisionHashMapD3, enumeration) {
constexpr uint32_t d=3,p = 2, l1=3, l2=4, l3=5, l=l1+l2+l3, threads=1;
using T = MinLogTypeTemplate<l, 32>;
using V1 = CollisionType<T, uint16_t, 1*p>;
using V2 = CollisionType<T, uint16_t, 2*p>;
using V3 = CollisionType<T, uint16_t, 4*p>;
constexpr static SimpleHashMapConfig s1{.bucketsize=16u, .nrbuckets=1u<<l1, .threads=threads};
constexpr static SimpleHashMapConfig s2{.bucketsize=12u, .nrbuckets=1u<<l2, .threads=threads};
constexpr static SimpleHashMapConfig s3{.bucketsize=8u, .nrbuckets=1u<<l3, .threads=threads};
using HM1 = SimpleHashMap<T, V1, s1, Hash<T, 0, l1, 2>>;
using HM2 = SimpleHashMap<T, V2, s2, Hash<T, 0, l2, 2>>;
using HM3 = SimpleHashMap<T, V3, s3, Hash<T, 0, l3, 2>>;
HM1 *hm1 = new HM1{};
HM2 *hm2 = new HM2{};
HM3 *hm3 = new HM3{};
using HM_DataType = HM3::data_type;
using HM_DataType_IndexType = HM_DataType::index_type;
static constexpr ConfigEnumHashMap configD1{k+l, p, l1, threads};
// static constexpr ConfigEnumHashMapD2 configD2{configD1, l2};
static constexpr ConfigEnumHashMapD<3> configD3{configD1, {l1, l2, l3}};
T *data = (T *)cryptanalysislib::aligned_alloc(1024, roundToAligned<1024>(sizeof(T) * (k+l)));
for (uint32_t i = 0; i < k+l; ++i) { data[i] = fastrandombytes_uint64() & ((1u << l) - 1u);}
const uint32_t syndrome = 0xff;
EnumHashMap<configD1, HM1> enumD1{data, hm1};
//CollisionHashMap<configD1, HM1> mitmD1{data, hm1};
//CollisionHashMapD2<configD2, HM1, HM2> mitmD2{data, hm1, hm2};
CollisionHashMapD<d, configD3, HM1, HM2, HM3> mitmD3{
data, std::ref(*hm1), std::ref(*hm2), std::ref(*hm3),
};
mitmD3.print();
auto f1 = [&](const T a, uint16_t *index){
// ASSERT(enumD1.check_hashmap2(iT1, index, 2*p, l1));
hm2->insert(a>>l1, V2::create(a, index));
};
auto f2 = [&](const T a, uint16_t *index){
// ASSERT(enumD1.check_hashmap2(iT2, index, 4*p, l1 + l2));
hm3->insert(a>>l1, V3::create(a, index));
};
auto f3 = [&](const T a, uint16_t *index){
(void)index;
// ASSERT(enumD1.check_hashmap2(iT2, index, 8*p, l));
std::cout << a << std::endl;
};
const uint32_t tid = 0;
enumD1.step(0, tid, false);
//mitmD1.step(iT1, f1, tid, false);
//mitmD2.step(iT2, f2, tid, false);
mitmD3.step(syndrome, tid, false, f1, f2, f3);
free(data);
delete hm1;
delete hm2;
}
int main(int argc, char **argv) {
InitGoogleTest(&argc, argv);
srand(time(NULL));
random_seed(rand());
return RUN_ALL_TESTS();
}