-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
185 lines (151 loc) · 5.61 KB
/
dataset.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
import os
import numpy as np
from PIL import Image
import json
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset
import torchvision
import torchvision.transforms as transforms
import utils
import mutual
__all__ = [
'RSADatasetMulti',
'RSADatasetMultiDec',
'RSADatasetMultiPre',
'RSADatasetMultiPP',
'ImageDatasetMulti',
]
def scale(x):
x -= x.min()
x /= x.max()
return x
def norm(x):
x = scale(x)
x -= 0.5
x /= 0.5
return x
def debias(x):
x -= x.min()
return x
class FullToSide(object):
def __init__(self):
pass
def __call__(self, addr, side):
if side in ['full', '']:
return addr
if side == 'cacheline':
cacheline = self.to_cacheline20(np.abs(addr))
# return (cacheline - 32) / 32
return cacheline
if side == 'cachebank':
cachebank = self.to_cachebank(np.abs(addr))
# return (cachebank - 512) / 512
return cachebank
def to_cacheline(self, addr, ASLR=False):
return (addr & 0xF_FFFF) >> 6
def to_cachebank(self, addr, ASLR=False):
return (addr & 0xF_FFFF) >> 2
class RSADatasetMulti(Dataset):
def __init__(self, args, key_dir, npz_dir, key_split, npz_split_list):
super(RSADatasetMulti).__init__()
self.args = args
self.key_dir = os.path.join(key_dir, key_split)
self.tool = FullToSide()
self.npz_path_list = []
for npz_split in npz_split_list:
split_dir = os.path.join(npz_dir, npz_split)
name_list = sorted(os.listdir(split_dir))
for name in name_list:
self.npz_path_list.append(
os.path.join(split_dir, name)
)
print('Total %d %s data.' % (len(self.npz_path_list), key_split))
def __len__(self):
return len(self.npz_path_list)
def load_trace(self, npz_path):
npz_trace = np.load(npz_path)
trace = npz_trace['arr_0']
return trace
def __getitem__(self, index):
npz_path = self.npz_path_list[index]
npz_name = npz_path.split(os.sep)[-1]
key_name = npz_name
trace = self.load_trace(npz_path)
trace = self.tool(trace, self.args.side)
trace = trace.astype(np.float32)
trace = torch.from_numpy(trace)
if self.args.use_norm:
trace = norm(trace)
elif self.args.use_bias:
trace = debias(trace)
trace = trace.view([self.args.nc, self.args.size, self.args.size])
npz_key = np.load(os.path.join(self.key_dir, key_name))
key = npz_key['arr_0']
key = key.astype(np.float32)
key = torch.from_numpy(key) # (1024, )
return trace, key, key_name.split('.')[0]
class RSADatasetMultiDec(RSADatasetMulti):
def load_trace(self, npz_path):
npz_trace = np.load(npz_path)
trace = npz_trace['arr_0'][self.args.start_index:]
return trace
class RSADatasetMultiPre(RSADatasetMulti):
def load_trace(self, npz_path):
npz_trace = np.load(npz_path)
trace = npz_trace['arr_0'][:self.args.end_index]
return trace
class RSADatasetMultiPP(RSADatasetMulti):
def load_trace(self, npz_path):
npz_trace = np.load(npz_path)
trace = npz_trace['arr_0']
length = (len(trace) // 16) * self.args.repeat_num
trace = trace[:length]
return trace
class ImageDatasetMulti(Dataset):
def __init__(self, args, image_dir, npz_dir, image_split, npz_split_list, data_num=None):
super(ImageDatasetMulti).__init__()
self.args = args
self.image_dir = os.path.join(image_dir, split)
self.image_dir = image_dir + ('%s/' % image_split)
self.tool = FullToSide()
self.transform = transforms.Compose([
transforms.Resize(args.image_size),
transforms.CenterCrop(args.image_size),
transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])
self.npz_path_list = []
for npz_split in npz_split_list:
split_dir = os.path.join(npz_dir, npz_split)
name_list = sorted(os.listdir(split_dir))
for name in name_list:
self.npz_path_list.append(
os.path.join(split_dir, name)
)
if data_num is not None:
self.npz_path_list = self.npz_path_list[:data_num]
print('Total %d %s data.' % (len(self.npz_path_list), image_split))
def __len__(self):
return len(self.npz_path_list)
def __getitem__(self, index):
npz_path = self.npz_path_list[index]
npz_name = npz_path.split(os.sep)[-1]
prefix = npz_name.split('.')[0]
image_name = prefix + '.jpg'
npz_trace = np.load(npz_path)
trace = npz_trace['arr_0']
trace = self.tool(trace, self.args.side)
trace = trace.astype(np.float32)
trace = torch.from_numpy(trace)
if self.args.use_norm:
trace = norm(trace)
elif self.args.use_bias:
trace = debias(trace)
trace = trace.view([self.args.nc, self.args.size, self.args.size])
image = Image.open(os.path.join(self.image_dir, image_name))
image = self.transform(image)
return trace, image, prefix
if __name__ == '__main__':
pass