-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.py
32 lines (27 loc) · 1.39 KB
/
convert.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
# This is a script for converting between the CSV traces provided
# by the authors of CS23 into the hdf5 format we use.
import h5py
import numpy as np
from binascii import unhexlify
import os
import csv
def convert(path, num_traces=50000):
name = os.path.splitext(os.path.basename(path))[0]
metadata = name.split("-")
with open(path) as csv_file:
with h5py.File(f"traces/{name}.hdf5", "w") as f:
f.attrs["trace_type"] = "attack"
f.attrs["side"] = "start"
f.attrs["implementation"] = "circuit"
f.attrs["tk1"] = np.array(bytearray(unhexlify(metadata[0])), dtype=np.uint8)
f.attrs["tk2"] = np.array(bytearray(unhexlify(metadata[1])), dtype=np.uint8)
f.attrs["key"] = np.array(bytearray(unhexlify(metadata[2])), dtype=np.uint8)
plaintext_set = f.create_dataset("plaintext", (num_traces, 16), dtype=np.uint8)
ciphertext_set = f.create_dataset(
"ciphertext", (num_traces, 16), dtype=np.uint8
)
trace_set = f.create_dataset("trace", (num_traces, 5000), dtype=np.float64)
for i, row in enumerate(csv.reader(csv_file, delimiter=";")):
plaintext_set[i] = np.array(bytearray(unhexlify(row[0])))
ciphertext_set[i] = np.array(bytearray(unhexlify(row[1])))
trace_set[i] = np.array(np.fromstring(row[2], sep=","))