Upload assemble.py
Browse files- assemble.py +66 -0
assemble.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import h5py
|
| 2 |
+
import numpy as np
|
| 3 |
+
import argparse
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
CHUNK_SHAPE = 4_000
|
| 7 |
+
NSW = 128_000
|
| 8 |
+
TRACE_LENGHT = 200_000
|
| 9 |
+
|
| 10 |
+
if __name__ == '__main__':
|
| 11 |
+
|
| 12 |
+
parser = argparse.ArgumentParser()
|
| 13 |
+
parser.add_argument("-dd", "--dataset_dir", required=True,
|
| 14 |
+
help="Directory containing the dataset chunks")
|
| 15 |
+
args = vars(parser.parse_args())
|
| 16 |
+
dataset_dir = args['dataset_dir']
|
| 17 |
+
|
| 18 |
+
# Create virtual layouts for profiling and attack traces, labels, and metadata
|
| 19 |
+
profile_traces_layout = h5py.VirtualLayout(
|
| 20 |
+
shape=(NSW, TRACE_LENGHT), dtype=np.float32)
|
| 21 |
+
attack_traces_layout = h5py.VirtualLayout(
|
| 22 |
+
shape=(NSW, TRACE_LENGHT), dtype=np.float32)
|
| 23 |
+
|
| 24 |
+
dt = h5py.vlen_dtype(np.uint32)
|
| 25 |
+
label_dtype = np.dtype([('sample', dt), ('frequency', dt)])
|
| 26 |
+
profile_labels_layout = h5py.VirtualLayout(shape=(NSW,), dtype=label_dtype)
|
| 27 |
+
attack_labels_layout = h5py.VirtualLayout(shape=(NSW,), dtype=label_dtype)
|
| 28 |
+
|
| 29 |
+
dt = np.dtype([('key', np.uint8, (16,)), ('plaintext', np.uint8, (16,))])
|
| 30 |
+
profile_meta_layout = h5py.VirtualLayout(shape=(NSW,), dtype=dt)
|
| 31 |
+
attack_meta_layout = h5py.VirtualLayout(shape=(NSW,), dtype=dt)
|
| 32 |
+
|
| 33 |
+
# Iterate over the chunks and assign virtual sources to the layouts
|
| 34 |
+
for n in range(NSW//CHUNK_SHAPE):
|
| 35 |
+
filename = os.path.join(dataset_dir, f'dfs_desynch_chunk_{n}.h5')
|
| 36 |
+
profile_traces_layout[n*CHUNK_SHAPE:(n+1)*CHUNK_SHAPE] = h5py.VirtualSource(
|
| 37 |
+
filename, 'profiling/traces', shape=(CHUNK_SHAPE, TRACE_LENGHT))
|
| 38 |
+
attack_traces_layout[n*CHUNK_SHAPE:(n+1)*CHUNK_SHAPE] = h5py.VirtualSource(
|
| 39 |
+
filename, 'attack/traces', shape=(CHUNK_SHAPE, TRACE_LENGHT))
|
| 40 |
+
|
| 41 |
+
profile_labels_layout[n*CHUNK_SHAPE:(n+1)*CHUNK_SHAPE] = h5py.VirtualSource(
|
| 42 |
+
filename, 'profiling/labels', shape=(CHUNK_SHAPE,))
|
| 43 |
+
attack_labels_layout[n*CHUNK_SHAPE:(n+1)*CHUNK_SHAPE] = h5py.VirtualSource(
|
| 44 |
+
filename, 'attack/labels', shape=(CHUNK_SHAPE,))
|
| 45 |
+
|
| 46 |
+
profile_meta_layout[n*CHUNK_SHAPE:(n+1)*CHUNK_SHAPE] = h5py.VirtualSource(
|
| 47 |
+
filename, 'profiling/metadata', shape=(CHUNK_SHAPE,))
|
| 48 |
+
attack_meta_layout[n*CHUNK_SHAPE:(n+1)*CHUNK_SHAPE] = h5py.VirtualSource(
|
| 49 |
+
filename, 'attack/metadata', shape=(CHUNK_SHAPE,))
|
| 50 |
+
|
| 51 |
+
# Add virtual dataset to output file
|
| 52 |
+
dfs_desynch_path = os.path.join(dataset_dir, 'dfs_desynch.h5')
|
| 53 |
+
with h5py.File(dfs_desynch_path, 'w', libver='latest') as f:
|
| 54 |
+
profiling_group = f.create_group('profiling')
|
| 55 |
+
attack_group = f.create_group('attack')
|
| 56 |
+
|
| 57 |
+
# Create virtual datasets within the groups
|
| 58 |
+
profiling_group.create_virtual_dataset('traces', profile_traces_layout)
|
| 59 |
+
profiling_group.create_virtual_dataset('labels', profile_labels_layout)
|
| 60 |
+
profiling_group.create_virtual_dataset('metadata', profile_meta_layout)
|
| 61 |
+
|
| 62 |
+
attack_group.create_virtual_dataset('traces', attack_traces_layout)
|
| 63 |
+
attack_group.create_virtual_dataset('labels', attack_labels_layout)
|
| 64 |
+
attack_group.create_virtual_dataset('metadata', attack_meta_layout)
|
| 65 |
+
|
| 66 |
+
print(f"DFS_DESYNCH dataset assembled in {dfs_desynch_path}")
|