-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaiinputs.py
More file actions
184 lines (137 loc) · 5.24 KB
/
aiinputs.py
File metadata and controls
184 lines (137 loc) · 5.24 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
# ----------------------------------------------------
# Electromagnetic Mining Array (EMMA)
# Copyright 2018, Pieter Robyns
# ----------------------------------------------------
import numpy as np
from emutils import EMMAException, int_to_one_hot
from leakagemodels import LeakageModel
class AIInputType:
"""
Class that defines all possible types of inputs for the ML models. Input classes must have an attribute
'input_type' with one of the values defined in this class.
"""
SIGNAL = 'signal'
SIGNAL_PLAINTEXT = 'signal_plaintext'
# For testing purposes
SIGNAL_KEY = 'signal_key'
SIGNAL_PLAINTEXT_KEY = 'signal_plaintext_key'
PLAINTEXT_KEY = 'plaintext_key'
PLAINTEXT_KEY_OH = 'plaintext_key_oh'
SIGNAL_LEAKAGE = 'signal_leakage'
RANDOM = 'random'
@classmethod
def choices(cls):
"""
Get all possible AIInputTypes in list form
:return:
"""
c = []
for k, v in cls.__dict__.items():
if k[:2] != '__' and type(v) is str:
c.append(v)
return c
class AIInputMeta(type):
"""
Metaclass used for checking whether the child class contains a valid input_type attribute.
"""
class BadAIInputClassException(EMMAException):
pass
class InvalidInputTypeException(EMMAException):
pass
def __new__(mcs, name, bases, class_dict):
if bases != (object,): # Do not validate LeakageModel class
if 'input_type' not in class_dict:
raise AIInputMeta.BadAIInputClassException
if class_dict['input_type'] not in AIInputType.choices():
raise AIInputMeta.InvalidInputTypeException
return type.__new__(mcs, name, bases, class_dict)
class AIInput(object, metaclass=AIInputMeta):
"""
AI input base class.
"""
class UnknownAIInputException(EMMAException):
pass
def __new__(cls, conf):
"""
Called when instantiating an AIInput object. Returns an instance of the appropriate class depending on the
input_type parameter.
:param conf:
:return:
"""
for subclass in cls._get_subclasses():
if subclass.input_type == conf.input_type:
return object.__new__(subclass) # Avoid recursion by calling object.__new__ instead of cls.__new__
raise AIInput.UnknownAIInputException
def __init__(self, conf):
self.conf = conf
@classmethod
def _get_subclasses(cls):
for subclass in cls.__subclasses__():
if cls is not object:
for subsubclass in subclass._get_subclasses(): # Also yield children of children
yield subsubclass
yield subclass
def get_trace_inputs(self, trace):
raise NotImplementedError
def get_trace_set_inputs(self, trace_set):
"""
Givem a trace set, returns inputs suitable for training an AI model.
:param trace_set:
:return:
"""
inputs = []
for trace in trace_set.traces:
inputs.append(self.get_trace_inputs(trace))
result = np.array(inputs)
# CNNs expect a channels dimension
if self.conf.cnn:
result = np.expand_dims(result, axis=-1)
return result
class SignalAIInput(AIInput):
input_type = AIInputType.SIGNAL
def get_trace_inputs(self, trace):
return trace.signal
class SignalPlaintextAIInput(AIInput):
input_type = AIInputType.SIGNAL_PLAINTEXT
def get_trace_inputs(self, trace):
return np.concatenate((trace.signal, trace.plaintext))
class SignalKeyAIInput(AIInput):
input_type = AIInputType.SIGNAL_KEY
def get_trace_inputs(self, trace):
return np.concatenate((trace.signal, trace.key))
class SignalPlaintextKeyAIInput(AIInput):
input_type = AIInputType.SIGNAL_PLAINTEXT_KEY
def get_trace_inputs(self, trace):
return np.concatenate((trace.signal, trace.plaintext, trace.key))
class PlaintextKeyAIInput(AIInput):
input_type = AIInputType.PLAINTEXT_KEY
def get_trace_inputs(self, trace):
return np.concatenate((trace.plaintext, trace.key))
class PlaintextKeyOHAIInput(AIInput):
input_type = AIInputType.PLAINTEXT_KEY_OH
def get_trace_inputs(self, trace):
result = []
for p in trace.plaintext:
result.append(int_to_one_hot(p, 256))
for k in trace.key:
result.append(int_to_one_hot(k, 256))
return np.concatenate(result)
class SignalLeakageAIInput(AIInput):
input_type = AIInputType.SIGNAL_LEAKAGE
def __init__(self, conf):
super().__init__(conf)
self.leakage_model = LeakageModel(conf)
def get_trace_inputs(self, trace):
leakages = []
for k in range(16):
leakage = self.leakage_model.get_trace_leakages(trace, k)
if isinstance(leakage, list) or isinstance(leakage, np.ndarray):
leakages.extend(list(leakage))
else:
leakages.append(leakage)
leakages = np.array(leakages)
return np.concatenate((trace.signal, leakages))
class RandomInput(AIInput):
input_type = AIInputType.RANDOM
def get_trace_inputs(self, trace):
return np.random.uniform(0.0, 1.0, len(trace.signal))