-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
801 lines (655 loc) · 31.5 KB
/
util.py
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
import argparse
import itertools
from decimal import Decimal
import numpy as np
import sys
import h5py
import os.path
import torch
from enum import Enum
device = torch.device('cuda:0')
req_kernel_size = ['ConvNetKernel', 'ConvNetKernelAscad', 'ConvNetKernelMasked', 'ConvNetKernelAscad2']
SBOX = np.array([
0x63, 0x7C, 0x77, 0x7B, 0xF2, 0x6B, 0x6F, 0xC5, 0x30, 0x01, 0x67, 0x2B, 0xFE, 0xD7, 0xAB, 0x76,
0xCA, 0x82, 0xC9, 0x7D, 0xFA, 0x59, 0x47, 0xF0, 0xAD, 0xD4, 0xA2, 0xAF, 0x9C, 0xA4, 0x72, 0xC0,
0xB7, 0xFD, 0x93, 0x26, 0x36, 0x3F, 0xF7, 0xCC, 0x34, 0xA5, 0xE5, 0xF1,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9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
])
HW = [bin(x).count("1") for x in range(256)]
C8 = [HW.count(HW[x]) / 256 for x in range(256)]
def HD(x, y):
return bin(x ^ y).count("1")
def check_file_exists(file_path):
if not os.path.exists(file_path):
print("Error: provided file path '%s' does not exist!" % file_path)
sys.exit(-1)
return
def load_ascad(ascad_database_file, load_metadata=False):
check_file_exists(ascad_database_file)
# Open the ASCAD database HDF5 for reading
try:
in_file = h5py.File(ascad_database_file, "r")
except:
print("Error: can't open HDF5 file '%s' for reading (it might be malformed) ..." % ascad_database_file)
sys.exit(-1)
# Load profiling traces
x_profiling = np.array(in_file['Profiling_traces/traces'], dtype=np.int8)
# Load profiling labels
y_profiling = np.array(in_file['Profiling_traces/labels'])
# Load attacking traces
x_attack = np.array(in_file['Attack_traces/traces'], dtype=np.int8)
# Load attacking labels
y_attack = np.array(in_file['Attack_traces/labels'])
if not load_metadata:
return (x_profiling, y_profiling), (x_attack, y_attack)
else:
return (x_profiling, y_profiling), (x_attack, y_attack), \
(in_file['Profiling_traces/metadata'], in_file['Attack_traces/metadata'])
def test_model(predictions, metadata, sub_key_index, use_hw=False, rank_step=10, unmask=False):
real_key = metadata[0]['key'][sub_key_index]
min_trace_idx = 0
num_traces = len(metadata)
ranks = full_ranks(predictions, real_key, metadata, min_trace_idx,
num_traces, rank_step, sub_key_index, use_hw, unmask)
# We plot the results
x = [ranks[i][0] for i in range(0, ranks.shape[0])]
y = [ranks[i][1] for i in range(0, ranks.shape[0])]
return x, y
def full_ranks(predictions, real_key, metadata, min_trace_idx, max_trace_idx, rank_step, sub_key_index, use_hw,
unmask=False):
index = np.arange(min_trace_idx + rank_step, max_trace_idx, rank_step)
f_ranks = np.zeros((len(index), 2), dtype=np.uint32)
key_bytes_proba = []
f = rank_hw if use_hw else rank
for t, i in zip(index, range(0, len(index))):
real_key_rank, key_bytes_proba = f(predictions[t - rank_step:t], metadata, real_key, t - rank_step, t,
key_bytes_proba, sub_key_index, unmask)
f_ranks[i] = [t - min_trace_idx, real_key_rank]
return f_ranks
def rank(predictions, metadata, real_key, min_trace_idx, max_trace_idx, last_key_bytes_proba, sub_key_index,
unmask=False):
# TODO: use unmask to unmask the data as with rank_hw
# Compute the rank
if len(last_key_bytes_proba) == 0:
# If this is the first rank we compute, initialize all the estimates to zero
key_bytes_proba = np.zeros(256)
else:
# This is not the first rank we compute: we optimize things by using the
# previous computations to save time!
key_bytes_proba = last_key_bytes_proba
for p in range(0, max_trace_idx - min_trace_idx):
# Go back from the class to the key byte. '2' is the index of the byte (third byte) of interest.
plaintext = metadata[min_trace_idx + p]['plaintext'][sub_key_index]
if unmask:
mask = metadata[min_trace_idx + p]['masks'][sub_key_index - 2]
else:
mask = 0
for i in range(0, 256):
# Our candidate key byte probability is the sum of the predictions logs
proba = predictions[p][SBOX[plaintext ^ i] ^ mask]
if proba != 0:
key_bytes_proba[i] += np.log(proba)
else:
# We do not want an -inf here, put a very small epsilon
# that correspondis to a power of our min non zero proba
min_proba_predictions = predictions[p][np.array(predictions[p]) != 0]
if len(min_proba_predictions) == 0:
print("Error: got a prediction with only zeroes ... this should not happen!")
sys.exit(-1)
min_proba = min(min_proba_predictions)
key_bytes_proba[i] += np.log(min_proba ** 2)
# Now we find where our real key candidate lies in the estimation.
# We do this by sorting our estimates and find the rank in the sorted array.
sorted_proba = np.array(list(map(lambda a: key_bytes_proba[a], key_bytes_proba.argsort()[::-1])))
real_key_rank = np.where(sorted_proba == key_bytes_proba[real_key])[0][0]
return real_key_rank, key_bytes_proba
def rank_hw(predictions, metadata, real_key, min_trace_idx, max_trace_idx, last_key_bytes_proba, sub_key_index,
unmask=False):
# Compute the rank
if len(last_key_bytes_proba) == 0:
# If this is the first rank we compute, initialize all the estimates to zero
key_bytes_proba = np.zeros(256)
else:
# This is not the first rank we compute: we optimize things by using the
# previous computations to save time!
key_bytes_proba = last_key_bytes_proba
for p in range(0, max_trace_idx - min_trace_idx):
# Go back from the class to the key byte. '2' is the index of the byte (third byte) of interest.
plaintext = metadata[min_trace_idx + p]['plaintext'][sub_key_index]
if unmask:
mask = metadata[min_trace_idx + p]['masks'][sub_key_index - 2]
# real_key = real_key ^ mask
else:
mask = 0
for i in range(0, 256):
# Our candidate key byte probability is the sum of the predictions logs
# Original:
# j = i ^ mask
# proba = predictions[p][HW[j]] / C8[j]
# index = SBOX_INV[j] ^ plaintext
index = i
proba = predictions[p][HW[SBOX[plaintext ^ i] ^ mask]]
if proba != 0:
key_bytes_proba[index] += np.log(proba)
else:
# We do not want an -inf here, put a very small epsilon
# that correspondis to a power of our min non zero proba
min_proba_predictions = predictions[p][np.array(predictions[p]) != 0]
if len(min_proba_predictions) == 0:
print("Error: got a prediction with only zeroes ... this should not happen!")
sys.exit(-1)
min_proba = min(min_proba_predictions)
key_bytes_proba[index] += np.log(min_proba ** 2)
# Now we find where our real key candidate lies in the estimation.
# We do this by sorting our estimates and find the rank in the sorted array.
sorted_proba = np.array(list(map(lambda a: key_bytes_proba[a], key_bytes_proba.argsort()[::-1])))
real_key_rank = np.where(sorted_proba == key_bytes_proba[real_key])[0][0]
return real_key_rank, key_bytes_proba
def shuffle_permutation(permutation, to_shuffle):
# Shuffle the arrays by giving the permutation in the square brackets.
shuffled_a = to_shuffle[permutation]
return shuffled_a
def save_model(network, model_save_file):
# Make sure the path where the model is saved is stored
os.makedirs(os.path.dirname(model_save_file), exist_ok=True)
network.save(model_save_file)
class BoolAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
super().__init__(option_strings, dest, nargs)
self.default = kwargs['default']
if nargs is not None:
raise ValueError("nargs not allowed")
def __call__(self, parser, namespace, values, option_string=None):
if not (values in ['True', 'true', '1'] or values in ['False', 'false', '0']):
print("aaaaaaaaaaaaaaaaaaaaaa")
raise ValueError("arg should be either true or false")
setattr(namespace, self.dest, values in ['True', 'true', '1'])
def load_csv(file, delimiter=',', dtype=np.float, start=None, size=None):
if size is None:
return np.genfromtxt(file, delimiter=delimiter, dtype=dtype)
elif start is None and size is not None:
with open(file) as t_in:
return np.genfromtxt(itertools.islice(t_in, size), delimiter=delimiter, dtype=dtype)
elif start is not None and size is not None:
with open(file) as t_in:
return np.genfromtxt(itertools.islice(t_in, start, start + size), delimiter=delimiter, dtype=dtype)
else:
raise ValueError('Error loading data set')
def load_ascad_train_traces(args):
print(args)
traces_file = '{}/ASCAD/ASCAD_{}_desync{}.h5'.format(args['traces_path'], args['sub_key_index'], args['desync'])
print('Loading {}'.format(traces_file))
(x_train, y_train), (_, _), (metadata_profiling, _) = load_ascad(traces_file, load_metadata=True)
plain = None
if args['domain_knowledge']:
plain = metadata_profiling[:]['plaintext'][:, args['sub_key_index']]
if args['use_hw']:
plain = np.array([HW[val] for val in plain])
plain = hot_encode(plain, 9 if args['use_hw'] else 256, dtype=np.float)
if args['unmask']:
y_train = np.array(
[y_train[i] ^ metadata_profiling[i]['masks'][args['sub_key_index'] - 2] for i in range(len(y_train))])
# [y_profiling[i] ^ metadata_profiling[i]['masks'][15] for i in range(len(y_profiling))])
# Convert values to hamming weight if asked for
if args['use_hw']:
y_train = np.array([HW[val] for val in y_train])
return x_train, y_train, plain
def load_ascad_test_traces(args):
print(args)
traces_file = '{}/ASCAD/ASCAD_{}_desync{}.h5'.format(args['traces_path'], args['sub_key_index'], args['desync'])
print('Loading {}'.format(traces_file))
(_, _), (x_test, y_test), (_, metadata_attack) = load_ascad(traces_file, load_metadata=True)
plain = metadata_attack[:]['plaintext'][:, args['sub_key_index']]
if args['unmask']:
y_test = np.array(
[y_test[i] ^ metadata_attack[i]['masks'][args['sub_key_index'] - 2] for i in range(len(y_test))])
# [y_profiling[i] ^ metadata_profiling[i]['masks'][15] for i in range(len(y_profiling))])
# Convert values to hamming weight if asked for
if args['use_hw']:
y_test = np.array([HW[val] for val in y_test])
key = metadata_attack[0]['key'][args['sub_key_index']]
key_guesses = np.load('{}/ASCAD/key_guesses_{}masked_{}.npy'.format(
args['traces_path'],
'un' if args['unmask'] else '',
args['desync']
))
return x_test, y_test, plain, key, key_guesses
def load_aes_hd(args):
print(args)
hw = 'HW' if args['use_hw'] else 'Value'
x_train = load_csv('{}/AES_HD/traces/traces_50_{}.csv'.format(args['traces_path'], hw),
delimiter=' ',
start=args.get('start'),
size=args.get('size'))
y_train = load_csv('{}/AES_HD/{}/model.csv'.format(args['traces_path'], hw),
delimiter=' ',
dtype=np.long,
start=args.get('start'),
size=args.get('size'))
return x_train, y_train, []
def load_dpav4(args):
print(args)
hw = 'HW' if args['use_hw'] else 'Value'
if args['raw_traces']:
x_train = load_csv('{}/DPAv4/traces/traces_complete.csv'.format(args['traces_path']),
delimiter=' ',
start=args.get('start'),
size=args.get('size'))
else:
x_train = load_csv('{}/DPAv4/traces/traces_50_{}.csv'.format(args['traces_path'], hw),
delimiter=' ',
start=args.get('start'),
size=args.get('size'))
y_train = load_csv('{}/DPAv4/{}/model.csv'.format(args['traces_path'], hw),
delimiter=' ',
dtype=np.long,
start=args.get('start'),
size=args.get('size'))
return x_train, y_train, None
def load_dpa_npy(args):
print(args)
x_train = np.load('{}/DPAv4/traces/traces_complete.csv.npy'.format(args['traces_path']))
x_train = x_train[args['start']:args['start'] + args.get('size')]
import gc
gc.collect()
print("Loaded and cut x")
y_train = np.load('{}/DPAv4/Value/model.csv.npy'.format(args['traces_path']))
y_train = y_train[args['start']:args['start'] + args.get('size')]
import gc
gc.collect()
return x_train, y_train, None
def load_random_delay(args):
print(args)
hw = 'HW' if args['use_hw'] else 'Value'
if args['raw_traces']:
f = "traces_complete"
if 'use_noise_data' in args and args['use_noise_data']:
f = "test"
x_train = load_csv('{}/Random_Delay/traces/{}.csv'.format(args['traces_path'], f),
delimiter=' ',
start=args.get('start'),
size=args.get('size'))
else:
x_train = load_csv('{}/Random_Delay/traces/traces_50_{}.csv'.format(args['traces_path'], hw),
delimiter=' ',
start=args.get('start'),
size=args.get('size'))
y_train = load_csv('{}/Random_Delay/{}/model.csv'.format(args['traces_path'], hw),
delimiter=' ',
dtype=np.long,
start=args.get('start'),
size=args.get('size'))
if args['domain_knowledge']:
# plain = load_csv('{}/Random_Delay/{}/plain_0.csv'.format(args['traces_path'], hw),
# delimiter=' ',
# dtype=np.int,
# start=args.get('start'),
# size=args.get('size'))
# plain = hot_encode(plain, 9 if args['use_hw'] else 256, dtype=np.float)
# TODO: fix this for domain knowledge
plain = None
else:
plain = None
return x_train, y_train, plain
def load_random_delay_npy(args):
print(args)
x_train = np.load('{}/Random_Delay/traces/traces_complete.csv.npy'.format(args['traces_path']))
y_train = np.load('{}/Random_Delay/Value/model.csv.npy'.format(args['traces_path']))
x_train = x_train[args['start']:args['start'] + args.get('size')]
y_train = y_train[args['start']:args['start'] + args.get('size')]
y_train = np.reshape(y_train, (args.get('size')))
return x_train, y_train, None
def load_random_delay_large(args):
print(args)
traces_step = 20000
total_steps = np.math.ceil((args['start'] + args['size']) / traces_step)
x_train = np.zeros((args['size'], 6250))
y_train = np.zeros((args['size']))
start_step = int(args['start'] / traces_step)
index_start = 0
for step in range(start_step, total_steps):
x_file = '{}/Random_Delay_Large/traces/traces_{}.csv.npy'.format(args['traces_path'], traces_step * (step + 1))
y_file = '{}/Random_Delay_Large/Value/model_{}.csv.npy'.format(args['traces_path'], traces_step * (step + 1))
x = np.load(x_file)
y = np.load(y_file)
# Begin step
if step == start_step:
# There is only one step
if step == total_steps - 1:
x_train[0:args['size']] = x[args['start'] % traces_step:(args['start'] + args['size']) % traces_step]
y_train[0:args['size']] = y[args['start'] % traces_step:(args['start'] + args['size']) % traces_step]
# More steps to come
else:
x_train[0:traces_step - (args['start'] % traces_step)] = x[(args['start'] % traces_step):traces_step]
y_train[0:traces_step - (args['start'] % traces_step)] = y[(args['start'] % traces_step):traces_step]
index_start = traces_step - (args['start'] % traces_step)
# Last step
elif step == total_steps - 1:
x_train[index_start:args['size']] = x[0:args['size'] - index_start]
y_train[index_start:args['size']] = y[0:args['size'] - index_start]
# More steps to come
else:
x_train[index_start:index_start + traces_step] = x[0:traces_step]
y_train[index_start:index_start + traces_step] = y[0:traces_step]
index_start += traces_step
return x_train, y_train, None
def load_random_delay_large_key_guesses(traces_path, start, size):
traces_step = 20000
total_steps = np.math.ceil((start + size) / traces_step)
key_guesses = np.zeros((size, 256))
start_step = int(start / traces_step)
index_start = 0
for step in range(start_step, total_steps):
file = '{}/Random_Delay_Large/Value/key_guesses_{}.csv.npy'.format(traces_path, traces_step * (step + 1))
step_key_guesses = np.load(file)
# Begin step
if step == start_step:
if step == total_steps - 1:
key_guesses[0:size] = step_key_guesses[start % traces_step:(start + size) % traces_step]
# More steps to come
else:
key_guesses[0:traces_step - (start % traces_step)] = step_key_guesses[(start % traces_step):traces_step]
index_start = traces_step - (start % traces_step)
# Last step
elif step == total_steps - 1:
key_guesses[index_start:size] = step_key_guesses[0:size - index_start]
# More steps to come
else:
key_guesses[index_start:index_start + traces_step] = step_key_guesses[0:traces_step]
index_start += traces_step
return key_guesses.astype(np.int)
def load_data_generic(args):
print(args)
x_train_file = '{}/{}/traces/traces_complete.csv.npy'.format(args['traces_path'], str(args['data_set']))
if args['use_noise_data']:
x_train_file = '{}/{}/traces/traces_noise_{}.npy'.format(
args['traces_path'], str(args['data_set']), args['noise_level'])
print(f"Loading {x_train_file}")
x_train = np.load(x_train_file)
y_train = np.load('{}/{}/Value/model.csv.npy'.format(args['traces_path'], str(args['data_set'])))
x_train = x_train[args['start']:args['start'] + args.get('size')]
y_train = y_train[args['start']:args['start'] + args.get('size')]
y_train = np.reshape(y_train, (args.get('size')))
return x_train, y_train, None
def load_ascad_keys(args):
print(args)
path = f"{args['traces_path']}/{str(args['data_set'])}/"
x_train_file = f'{path}traces/train_traces.npy'
print(f"Loading {x_train_file}")
x_train = np.load(x_train_file)
y_file = '{}/Value/train_model{}_{}masked.csv.npy'.format(
path,
'_hw' if args['use_hw'] else '',
'un' if args['unmask'] else ''
)
print(f"Loading y file {y_file}")
y_train = np.load(y_file)
plaintexts = np.load(f"{path}/Value/train_plaintexts.npy")
x_train = x_train[args['start']:args['start'] + args.get('size')]
y_train = y_train[args['start']:args['start'] + args.get('size')]
plaintexts = plaintexts[args['start']:args['start'] + args.get('size')]
if args['use_hw']:
plaintexts = [HW[plaintexts[i]] for i in range(len(plaintexts))]
plaintexts = hot_encode(plaintexts, 9 if args['use_hw'] else 256, dtype=np.float)
y_train = np.reshape(y_train, (args.get('size')))
return x_train, y_train, plaintexts
def load_ascad_keys_test(args):
print(args)
path = f"{args['traces_path']}/{str(args['data_set'])}/"
x_test_file = f'{path}/traces/test_traces.npy'
print(f"Loading {x_test_file}")
x_test = np.load(x_test_file)
y_file = '{}/Value/test_model{}_{}masked.csv.npy'.format(
path,
'_hw' if args['use_hw'] else '',
'un' if args['unmask'] else ''
)
print(f"Loading y file {y_file}")
y_test = np.load(y_file)
x_test = x_test[0:args.get('size')]
y_test = y_test[0:args.get('size')]
y_test = np.reshape(y_test, (args.get('size')))
key_guesses_file = '{}/Value/key_guesses_{}masked.csv.npy'.format(
path,
'un' if args['unmask'] else ''
)
key_guesses = np.load(key_guesses_file)
plaintexts = np.load(f"{path}/Value/test_plaintexts.npy")
plaintexts = plaintexts[0:args.get('size')]
if args['use_hw']:
plaintexts = [HW[plaintexts[i]] for i in range(len(plaintexts))]
plaintexts = hot_encode(plaintexts, 9 if args['use_hw'] else 256, dtype=np.float)
return x_test, y_test, key_guesses, 34, plaintexts
def load_ascad_normalized(args):
print(args)
x_train = np.load('{}/{}/traces/traces_normalized_t{}_v{}_{}.csv.npy'.format
(args['traces_path'], str(args['data_set']),
args['train_size'], args['validation_size'], args['desync']))
y_train = np.load('{}/{}/Value/model_{}masked.npy'.format(args['traces_path'], str(args['data_set']),
'un' if args['unmask'] else ''))
x_train = x_train[args['start']:args['start'] + args.get('size')]
y_train = y_train[args['start']:args['start'] + args.get('size')]
y_train = np.reshape(y_train, (args.get('size')))
if args['use_hw']:
y_train = np.array([HW[val] for val in y_train])
return x_train, y_train, None
def load_ascad_normalized_test_traces(args):
print(args)
x = np.load('{}/{}/traces/traces_normalized_t{}_v{}_{}.csv.npy'.format
(args['traces_path'], str(args['data_set']),
args['train_size'], args['validation_size'], args['desync']))
y = np.load('{}/{}/Value/model_{}masked.npy'.format(args['traces_path'], str(args['data_set']),
'un' if args['unmask'] else ''))
key_guesses = np.load('{}/{}/Value/key_guesses_{}masked.npy'.format(args['traces_path'], str(args['data_set']),
'un' if args['unmask'] else ''))
x_test = x[args['start']:args['start'] + args['size']]
y_test = y[50000:50000 + args['size']]
print("y shape {}".format(y.shape))
# Convert values to hamming weight if asked for
if args['use_hw']:
y_test = np.array([HW[val] for val in y_test])
return x_test, y_test, key_guesses, 224
def load_sim_mask_test_traces(args):
print(args)
x = np.load('{}/{}/traces/traces_complete.csv.npy'.format
(args['traces_path'], str(args['data_set'])))
y = np.load('{}/{}/Value/model.csv.npy'.format(args['traces_path'], str(args['data_set'])))
key_guesses = np.load('{}/{}/Value/key_guesses_ALL_transposed.csv.npy'.format(args['traces_path'],
str(args['data_set'])))
x_test = x[args['start']:args['start'] + args['size']]
y_test = y[args['start']:args['start'] + args['size']]
# print("y shape {}".format(y.shape))
# Convert values to hamming weight if asked for
# if args['use_hw']:
# y_test = np.array([HW[val] for val in y_test])
return x_test, y_test, key_guesses, 23
def load_random_delay_dk(args):
print(args)
x_train = load_csv('{}/Random_Delay_DK/traces/traces.csv'.format(args['traces_path']),
delimiter=' ',
start=args.get('start'),
size=args.get('size'))
y_train = load_csv('{}/Random_Delay_DK/Value/model.csv'.format(args['traces_path']),
delimiter=' ',
dtype=np.long,
start=args.get('start'),
size=args.get('size'))
plain = load_csv('{}/Random_Delay_DK/Value/plaintexts.csv'.format(args['traces_path']),
delimiter=' ',
dtype=np.long,
start=args.get('start'),
size=args.get('size'))
plain = hot_encode(plain, 9 if args['use_hw'] else 256, dtype=np.float)
return x_train, y_train, plain
class DataSet(Enum):
ASCAD = 1
AES_HD = 2
DPA_V4 = 3
RANDOM_DELAY = 4
RANDOM_DELAY_LARGE = 5
RANDOM_DELAY_DK = 6
RANDOM_DELAY_NORMALIZED = 7
ASCAD_NORMALIZED = 8
SIM_MASK = 9
ASCAD_KEYS = 10
ASCAD_KEYS_NORMALIZED = 11
def __str__(self):
if self.value == 1:
return "ASCAD"
elif self.value == 2:
return "AES_HD"
elif self.value == 3:
return "DPAv4"
elif self.value == 4:
return "Random_Delay"
elif self.value == 5:
return "Random_Delay_Large"
elif self.value == 6:
return "Random_Delay_DK"
elif self.value == 7:
return "Random_Delay_Normalized"
elif self.value == 8:
return "ASCAD_Normalized"
elif self.value == 9:
return "Simulated_Mask"
elif self.value == 10:
return "ASCAD_Keys"
elif self.value == 11:
return "ASCAD_Keys_Normalized"
else:
print("ERROR {}".format(self.value))
@staticmethod
def from_string(s):
try:
return DataSet[s]
except KeyError:
raise ValueError()
def load_data_set(data_set):
table = {DataSet.ASCAD: load_ascad_train_traces,
DataSet.AES_HD: load_aes_hd,
DataSet.DPA_V4: load_dpa_npy,
DataSet.RANDOM_DELAY: load_random_delay_npy,
DataSet.RANDOM_DELAY_LARGE: load_random_delay_large,
DataSet.RANDOM_DELAY_DK: load_random_delay_dk,
DataSet.RANDOM_DELAY_NORMALIZED: load_data_generic,
DataSet.ASCAD_NORMALIZED: load_ascad_normalized,
DataSet.SIM_MASK: load_data_generic,
DataSet.ASCAD_KEYS: load_ascad_keys,
DataSet.ASCAD_KEYS_NORMALIZED: load_ascad_keys}
return table[data_set]
def hot_encode(vector, num_classes, dtype=np.int):
return np.eye(num_classes)[vector].astype(dtype)
def func_in_list(func, l):
for f in l:
if f == func:
return True
return False
def save_np(path, data, f="%i"):
np.savetxt(path, data, delimiter=' ', fmt=f)
def generate_permutations(n, size):
permutations = []
for i in range(n):
permutations.append(np.random.permutation(size))
return permutations
def loop_at_least_once(data, func, do):
i = 0
while True:
if len(data) != 0:
func(data[i])
do()
i += 1
if i >= len(data):
break
def loop_at_least_once_with_arg(data, func, do, arg):
i = 0
while True:
if len(data) != 0:
func(data[i])
do(arg)
i += 1
if i >= len(data):
break
def save_loss_acc(path, filename, res):
path = os.path.dirname(path)
os.makedirs(path, exist_ok=True)
np.save("{}/{}.ta".format(path, filename), res[0])
np.save("{}/{}.va".format(path, filename), res[1])
np.save("{}/{}.tl".format(path, filename), res[2])
np.save("{}/{}.vl".format(path, filename), res[3])
def load_loss_acc(file):
ta = np.load("{}.ta.npy".format(file))
va = np.load("{}.va.npy".format(file))
tl = np.load("{}.tl.npy".format(file))
vl = np.load("{}.vl.npy".format(file))
return ta, va, tl, vl
def generate_folder_name(args):
return '{}/subkey_{}/{}{}{}_SF{}_E{}_BZ{}_LR{}{}{}/train{}'.format(
str(args.data_set),
args.subkey_index,
'' if args.unmask else 'masked/',
'' if args.desync is 0 else 'desync{}/'.format(args.desync),
'HW' if args.use_hw else 'ID',
args.spread_factor,
args.epochs,
args.batch_size,
'%.2E' % Decimal(args.lr),
'' if np.math.ceil(args.l2_penalty) <= 0 else '_L2_{}'.format(args.l2_penalty),
'' if not args.init_weights else '_{}'.format(args.init_weights),
args.train_size,
)
def get_memory():
import os
import psutil
process = psutil.Process(os.getpid())
return process.memory_info().rss
def format_bytes(size):
power = 2 ** 10
n = 0
power_labels = {0: '', 1: 'kilo', 2: 'mega', 3: 'giga', 4: 'tera'}
while size > power:
size /= power
n += 1
return size, power_labels[n] + 'bytes'
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
class EmptySpace(object):
pass
class BColors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'