-
Notifications
You must be signed in to change notification settings - Fork 3
/
subset_traces.py
executable file
·57 lines (49 loc) · 1.45 KB
/
subset_traces.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
#! /usr/bin/env python3
import argparse
import h5py
parser = argparse.ArgumentParser(
description=
"Export a subsed of the raw ASCAD traces in a new file. "
"Useful for fast iteration on small attacks. "
"Exported file has a structure identical to the original file."
)
parser.add_argument(
"-n",
"--ntraces",
type=int,
help=
"Number of traces in the exported dataset "
"(always takes the n first traces).",
required=True,
)
parser.add_argument(
"-w",
"--window",
type=str,
default="0,250000",
help="'start,end' window for taking only a part of the traces (default: whole trace).",
)
parser.add_argument(
"input",
type=str,
help="Location of the 'raw traces' ASCAD database file.",
)
parser.add_argument(
"output",
type=str,
help="Location of the resulting dataset file.",
)
args = parser.parse_args()
W_START, W_END = [int(x.strip()) for x in args.window.split(",")]
f_database = h5py.File(args.input, "r")
l = args.ntraces
traces = f_database["traces"][:l,W_START:W_END]
masks = f_database["metadata"]["masks"][:l,:]
key = f_database["metadata"]["key"][:l,:]
plaintext = f_database["metadata"]["plaintext"][:l,]
with h5py.File(args.output, "w") as f:
f.create_dataset("traces", data=traces)
md = f.create_group("metadata")
md.create_dataset("masks", data=masks)
md.create_dataset("key", data=key)
md.create_dataset("plaintext", data=plaintext)