-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdo_show_results_templates_a2_bat_fb.py
168 lines (136 loc) · 5.64 KB
/
do_show_results_templates_a2_bat_fb.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
import os
import numpy as np
import matplotlib.pyplot as plt
# from scipy.io import loadmat
# import pickle
# import time
def get_ge_from_success_info(sinfo, nr_traces_vec):
"""
Returns guessing entropy data from the given success info structure.
Parameters:
- sinfo: Structured array or nested numpy array containing depth data for both
".avg" and ".joint" scores.
- nr_traces_vec: List or numpy array of length nr_test_groups, with the number of
attack traces used in the experiments.
Returns:
- g: Dictionary with two fields:
- 'avg': Guessing entropy data from the ".avg" scores in sinfo.
- 'joint': Guessing entropy data from the ".joint" scores in sinfo.
Both 'avg' and 'joint' are numpy arrays of length nr_test_groups.
"""
d_avg = sinfo['depth']['avg']
d_joint = sinfo['depth']['joint']
nr_test_groups = len(nr_traces_vec)
g = {
'avg': np.zeros(nr_test_groups),
'joint': np.zeros(nr_test_groups)
}
# Compute the mean guessing entropy for all data
for k in range(nr_test_groups):
nr_iter = d_avg[f'group{k+1}'].shape[1]
avg_sum = 0
joint_sum = 0
for j in range(nr_iter):
avg_sum += np.log2(np.mean(d_avg[f'group{k+1}'][:, j]))
joint_sum += np.log2(np.mean(d_joint[f'group{k+1}'][:, j]))
# in the original code, the values for
# sinfo['depth']['avg'][f'group{k+1}'][:, j]
# decrease as k increases, which is not the case here
# TODO: figure out why and fix
# print(d_avg[f'group{k+1}'][:, j])
# print(d_joint[f'group{k+1}'][:, j])
g['avg'][k] = avg_sum / nr_iter
g['joint'][k] = joint_sum / nr_iter
# print(g)
# print(g)
return g
def get_line_properties_templates(uid, style):
"""
Returns line properties for plots of template attack results.
Parameters:
- uid: Integer uniquely identifying the desired line properties (from 1 to 6).
- style: String specifying the kind of style ('normal' or 'fancy').
Returns:
- slines: Dictionary containing line properties ('Color', 'LineStyle', 'LineWidth', 'Marker').
"""
if style == 'normal':
if uid == 0:
slines = {'Color': 'm', 'LineStyle': '-', 'LineWidth': 4, 'Marker': 'none'}
elif uid == 1:
slines = {'Color': 'c', 'LineStyle': '-.', 'LineWidth': 4, 'Marker': 'none'}
elif uid == 2:
slines = {'Color': 'g', 'LineStyle': '-', 'LineWidth': 4, 'Marker': 'none'}
elif uid == 3:
slines = {'Color': 'k', 'LineStyle': '-.', 'LineWidth': 4, 'Marker': 'none'}
elif uid == 4:
slines = {'Color': 'b', 'LineStyle': '-', 'LineWidth': 4, 'Marker': 'none'}
elif uid == 5:
slines = {'Color': 'r', 'LineStyle': '-.', 'LineWidth': 4, 'Marker': 'none'}
else:
raise ValueError('uid not supported')
elif style == 'fancy':
if uid == 0:
slines = {'Color': 'm', 'LineStyle': '-', 'LineWidth': 1, 'Marker': 'o'}
elif uid == 1:
slines = {'Color': 'c', 'LineStyle': '--', 'LineWidth': 1, 'Marker': '+'}
elif uid == 2:
slines = {'Color': 'g', 'LineStyle': '-.', 'LineWidth': 1, 'Marker': '*'}
elif uid == 3:
slines = {'Color': 'k', 'LineStyle': '-', 'LineWidth': 1, 'Marker': '.'}
elif uid == 4:
slines = {'Color': 'b', 'LineStyle': '--', 'LineWidth': 1, 'Marker': 'x'}
elif uid == 5:
slines = {'Color': 'r', 'LineStyle': '-.', 'LineWidth': 1, 'Marker': 's'}
else:
raise ValueError('uid not supported')
else:
raise ValueError('Unknown style')
return slines
def make_figures_ge(G, nr_traces_vec, rpath, rprefix, title_ge, slegend, font_size,
slines, options, yrange):
"""
Creates figures for guessing entropy data.
Parameters:
- G: 2D numpy array of shape (nr_exp, nr_test_groups).
- nr_traces_vec: List or numpy array of number of test traces (x-axis).
- rpath: Path to save the figures.
- rprefix: Prefix for saved figure filenames.
- title_ge: Title for the figures.
- slegend: List of legends for each experiment.
- font_size: Font size for text in the plot.
- slines: List of dictionaries containing line properties.
- options: String containing plot options (e.g., 'y', 'g', 'L').
- yrange: Tuple specifying y-axis limits.
"""
nr_exp = G.shape[0]
if font_size is None:
font_size = 24
nr_test_groups = len(nr_traces_vec)
if G.shape[1] != nr_test_groups:
raise ValueError('Incompatible nr_test_groups')
xl_str = '$n_a$ (log axis)'
font_small = 14
fig_size = (10.24, 8.68) if 'L' in options else (6.4, 4.8)
# Plot guessing entropy data
plt.figure(figsize=fig_size)
for i in range(nr_exp):
plt.semilogx(nr_traces_vec, G[i, :],
color=slines[i]['Color'],
linestyle=slines[i]['LineStyle'],
linewidth=slines[i]['LineWidth'],
marker=slines[i]['Marker'])
if 'y' not in options:
plt.ylim(yrange)
plt.xlabel(xl_str, fontsize=font_size)
plt.ylabel('Guessing entropy (bits)', fontsize=font_size)
if title_ge:
plt.title(f'Guessing entropy\n{title_ge}', fontsize=font_size)
if slegend:
plt.legend(slegend, fontsize=font_small)
if 'g' in options:
plt.grid(True)
if not os.path.exists(rpath):
os.makedirs(rpath, exist_ok=True)
plt.tight_layout()
plt.savefig(f"{rpath}{rprefix}guess_entropy.pdf", format='pdf')
plt.show()