-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder.py
executable file
·78 lines (54 loc) · 2.44 KB
/
recorder.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
import h5py # Add support for hdf5 files
import numpy as np # Add support for matrix manipulations
from hamming import *
# base path for datasets
basepath = './traces/'
class Recorder:
"""Class used to load traces measured with measuring script."""
def __init__(self):
self.trace = []
self.traces = {}
self.intermediate = []
self.intermediates = {}
self.inputs = []
self.d = []
def save_dataset(self, filename):
self.hdf5_file = h5py.File(basepath + filename + ".h5", "w")
for sd in self.traces.keys():
self.hdf5_file.create_dataset("traces_" + str(sd), data=self.traces[sd], dtype=np.float64)
for sd in self.intermediates.keys():
self.hdf5_file.create_dataset("intermediates_" + str(sd), data=self.intermediates[sd], dtype=np.float64)
self.hdf5_file.create_dataset("inputs", data=self.inputs, dtype=np.uint8)
self.hdf5_file.create_dataset("d", data=self.d, dtype=np.uint8)
self.hdf5_file.close()
def record_order(self, d):
self.d = [d]
def record_values(self, values):
for value in values:
self.trace.append(get_hamming_weight(value))
self.intermediate.append(float(value))
def record_input(self, input):
self.inputs.append(input)
def save_trace(self):
trace = np.array(self.trace)
intermediate = np.array(self.intermediate)
for l in range(0, 300, 2):
sd = round(l/100, 2)
noise = np.random.normal(0, sd, trace.shape)
if sd not in self.traces.keys():
self.traces[sd] = []
self.traces[sd].append(trace + noise)
for l in range(0, 200, 2):
sd = round(l/10, 2)
intermediate_noise = np.random.normal(0, sd, intermediate.shape)
if sd not in self.intermediates.keys():
self.intermediates[sd] = []
self.intermediates[sd].append(intermediate + intermediate_noise)
self.trace = []
self.intermediate = []
def get_trace_hypothesis(self):
# TODO: Print warning because this function can be only used properly with d=0
return np.array(self.traces[0.0])
def get_intermediate_hypothesis(self):
# TODO: Print warning because this function can be only used properly with d=0
return np.array(self.intermediates[0.0])