-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTVLA.py
206 lines (143 loc) · 5.11 KB
/
TVLA.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
import numpy as np
import os
import matplotlib.pyplot as plt
from numpy.core.fromnumeric import mean
#-------------------------------------------------------------------------------
def files_by_parameter(path = None, message = None, cipher = None, key = None):
if(message == None and cipher == None and key == None):
print("have to provide atleast one parameter")
return -1
if(path == None):
print("path is not given")
return -1
parameters = []
files = os.listdir(path)
filtered_files = []
ids = []
for f in files:
temp = f.split("_")
pick = True
if(message != None):
if(temp[-2][2:] == message):
pick = pick and True
else:
pick = pick and False
if(cipher != None):
if(temp[-1][2:-4] == cipher):
pick = pick and True
else:
pick = pick and False
if(key != None):
if(temp[-3][2:] == key):
pick = pick and True
else:
pick = pick and False
if (pick == True):
filtered_files.append(f)
ids.append(int(temp[4][2:]))
return filtered_files, ids
#-------------------------------------------------------------------------------
def get_traces(path, files):
traces = {}
count = 0
for f in files:
if(f[:4]!="wave"):
continue
tfile = open(path + f)
trace = []
for line in tfile:
if(line[0]!="#"):
raw_data = int(line)
# raw_data = raw_data* 0.0000141351
raw_data = raw_data* 0.00141351
trace.append(raw_data)
temp_file_name = f.split("_")
id = int(temp_file_name[4][2:])
if(len(trace) == 3253):
traces[id] = trace
if(count%1000 == 0):
print("loaded "+ str(count)+ "traces")
count = count + 1
return traces
#-------------------------------------------------------------------------------
def split_dict(d, ratio):
l = len(d)
n = int(l*ratio)
d1 = dict( list(d.items())[:n] )
d2 = dict( list(d.items())[n:] )
return d1, d2
#-------------------------------------------------------------------------------
def preprocess(d1, d2, attack_order):
trace1 = []
trace2 = []
_, x = next(iter(d1.items()))
n1 = len(x)
_, x = next(iter(d2.items()))
n2 = len(x)
for time_t1 in range(n1):
temp1 = []
for _, t1 in d1.items():
temp1.append(t1[time_t1])
trace1.append(temp1)
for time_t2 in range(n2):
temp2 = []
for _, t2 in d2.items():
temp2.append(t2[time_t2])
trace2.append(temp2)
#ORDER of attack
# print(trace1[0][0])
trace1 = np.power(trace1, attack_order)
# print(trace1[0][0])
trace2 = np.power(trace2, attack_order)
return np.array(trace1), np.array(trace2)
#-------------------------------------------------------------------------------
def TVLA(trace1, trace2):
n1 = trace1.shape[1]
n2 = trace2.shape[1]
mean_trace1 = np.mean(trace1, axis = 1)
mean_trace2 = np.mean(trace2, axis = 1)
var_trace1 = np.var(trace1, axis = 1)
var_trace2 = np.var(trace2, axis = 1)
t_test = (mean_trace1 - mean_trace2) / np.sqrt((var_trace1/n1) + (var_trace2/n2))
return t_test
#-------------------------------------------------------------------------------
def MF_TVLA(trace1, trace2):
n1 = trace1.shape[1]
n2 = trace2.shape[1]
# -----------------
mean_trace1 = np.mean(trace1, axis = 1)
mean_trace2 = np.mean(trace2, axis = 1)
# trace1.shape==(3253,10000) & mean_trace1==(3253,)
for i in range(len(mean_trace1)):
for j in range(n1):
trace1[i][j] = trace1[i][j] - mean_trace1[i]
for j in range(n2):
trace2[i][j] = trace2[i][j] - mean_trace2[i]
#------------------
mean_trace1 = np.mean(trace1, axis = 1)
mean_trace2 = np.mean(trace2, axis = 1)
var_trace1 = np.var(trace1, axis = 1)
var_trace2 = np.var(trace2, axis = 1)
t_test = (mean_trace1 - mean_trace2) / np.sqrt((var_trace1/n1) + (var_trace2/n2))
return t_test
#-------------------------------------------------------------------------------
def run(path1, path2, name, attack_order):
files1 = os.listdir(path1)
files2 = os.listdir(path2)
t1 = get_traces(path1,files1)
t2 = get_traces(path2,files2)
t1, t2 = preprocess(t1, t2, attack_order)
my_tvla = TVLA(t1, t2)
plt.plot(my_tvla)
plt.axhline(y = 4.5, color='r', linestyle='-')
plt.axhline(y = -4.5, color='r', linestyle='-')
plt.title("t-statistics vs time steps - "+ name)
plt.xlabel("Time Steps")
plt.ylabel("t-statistic values")
plt.savefig("results/first_order_"+name+".png")
# print("saved " + name + ".png")
# if __name__ == "__main__":
# path1 = "./p1/"
# path2 = "./p2/"
# run(path1, path2, "abc")
#-------------------------------------------------------------------------------