diff options
| -rw-r--r-- | pyecsca/ec/configuration.py | 11 | ||||
| -rw-r--r-- | pyecsca/sca/trace/align.py | 8 | ||||
| -rw-r--r-- | pyecsca/sca/trace/combine.py | 8 | ||||
| -rw-r--r-- | pyecsca/sca/trace/edit.py | 8 | ||||
| -rw-r--r-- | pyecsca/sca/trace/filter.py | 4 | ||||
| -rw-r--r-- | pyecsca/sca/trace/process.py | 20 | ||||
| -rw-r--r-- | pyecsca/sca/trace/sampling.py | 7 | ||||
| -rw-r--r-- | pyecsca/sca/trace/test.py | 4 | ||||
| -rw-r--r-- | pyecsca/sca/trace/trace.py | 27 | ||||
| -rw-r--r-- | pyecsca/sca/trace_set/chipwhisperer.py | 2 | ||||
| -rw-r--r-- | pyecsca/sca/trace_set/hdf5.py | 4 | ||||
| -rw-r--r-- | pyecsca/sca/trace_set/inspector.py | 12 | ||||
| -rw-r--r-- | test/ec/test_configuration.py | 5 | ||||
| -rw-r--r-- | test/sca/test_align.py | 26 | ||||
| -rw-r--r-- | test/sca/test_combine.py | 8 | ||||
| -rw-r--r-- | test/sca/test_edit.py | 2 | ||||
| -rw-r--r-- | test/sca/test_filter.py | 2 | ||||
| -rw-r--r-- | test/sca/test_match.py | 10 | ||||
| -rw-r--r-- | test/sca/test_plot.py | 4 | ||||
| -rw-r--r-- | test/sca/test_process.py | 2 | ||||
| -rw-r--r-- | test/sca/test_sampling.py | 4 | ||||
| -rw-r--r-- | test/sca/test_test.py | 25 | ||||
| -rw-r--r-- | test/sca/test_trace.py | 2 | ||||
| -rw-r--r-- | test/sca/test_traceset.py | 8 |
24 files changed, 103 insertions, 110 deletions
diff --git a/pyecsca/ec/configuration.py b/pyecsca/ec/configuration.py index 24f3a7f..190ed61 100644 --- a/pyecsca/ec/configuration.py +++ b/pyecsca/ec/configuration.py @@ -55,6 +55,13 @@ class Reduction(EnumDefine): @public +class Inversion(EnumDefine): + """Inversion algorithm used.""" + GCD = "INV_GCD" + EULER = "INV_EULER" + + +@public class HashType(EnumDefine): """Hash algorithm used in ECDH and ECDSA.""" NONE = "HASH_NONE" @@ -85,6 +92,7 @@ class Configuration(object): mult: Multiplication sqr: Squaring red: Reduction + inv: Inversion @public @@ -128,7 +136,8 @@ def all_configurations(**kwargs) -> Generator[Configuration, Configuration, None "mod_rand": RandomMod, "mult": Multiplication, "sqr": Squaring, - "red": Reduction + "red": Reduction, + "inv": Inversion } keys = list(filter(lambda key: key not in kwargs, options.keys())) values = [options[key] for key in keys] diff --git a/pyecsca/sca/trace/align.py b/pyecsca/sca/trace/align.py index 1c9c216..f1bf259 100644 --- a/pyecsca/sca/trace/align.py +++ b/pyecsca/sca/trace/align.py @@ -2,7 +2,7 @@ This module provides functions for aligning traces in a trace set to a reference trace within it. """ import numpy as np -from copy import copy, deepcopy +from copy import deepcopy from fastdtw import fastdtw, dtw from public import public from typing import List, Callable, Tuple @@ -27,7 +27,7 @@ def align_reference(reference: Trace, *traces: Trace, result_samples[:length - offset] = trace.samples[offset:] else: result_samples[-offset:] = trace.samples[:length + offset] - result.append(Trace(result_samples, copy(trace.title), copy(trace.data))) + result.append(trace.with_samples(result_samples)) return result @@ -198,7 +198,7 @@ def align_dtw_scale(reference: Trace, *traces: Trace, radius: int = 1, scale[x] += 1 result_samples //= scale del scale - result.append(Trace(result_samples, copy(trace.title), copy(trace.data))) + result.append(trace.with_samples(result_samples)) return result @@ -233,5 +233,5 @@ def align_dtw(reference: Trace, *traces: Trace, radius: int = 1, fast: bool = Tr # or manually: # for x, y in path: # result_samples[x] = trace.samples[y] - result.append(Trace(result_samples, copy(trace.title), copy(trace.data))) + result.append(trace.with_samples(result_samples)) return result diff --git a/pyecsca/sca/trace/combine.py b/pyecsca/sca/trace/combine.py index 0e7f325..5eeaaec 100644 --- a/pyecsca/sca/trace/combine.py +++ b/pyecsca/sca/trace/combine.py @@ -17,10 +17,10 @@ def average(*traces: Trace) -> Optional[CombinedTrace]: if not traces: return None if len(traces) == 1: - return CombinedTrace(traces[0].samples.copy(), None, None) + return CombinedTrace(traces[0].samples.copy()) dtype = traces[0].samples.dtype result_samples = np.mean(np.array([trace.samples for trace in traces]), axis=0).astype(dtype) - return CombinedTrace(result_samples, None, None) + return CombinedTrace(result_samples) @public @@ -48,7 +48,7 @@ def standard_deviation(*traces: Trace) -> Optional[CombinedTrace]: return None dtype = traces[0].samples.dtype result_samples = np.std(np.array([trace.samples for trace in traces]), axis=0).astype(dtype) - return CombinedTrace(result_samples, None, None) + return CombinedTrace(result_samples) @public @@ -61,4 +61,4 @@ def subtract(one: Trace, other: Trace) -> CombinedTrace: :return: """ result_samples = one.samples - other.samples - return CombinedTrace(result_samples, None, None) + return CombinedTrace(result_samples) diff --git a/pyecsca/sca/trace/edit.py b/pyecsca/sca/trace/edit.py index f58ad57..cc27dcd 100644 --- a/pyecsca/sca/trace/edit.py +++ b/pyecsca/sca/trace/edit.py @@ -1,5 +1,4 @@ import numpy as np -from copy import copy from public import public from typing import Union, Tuple, Any @@ -22,7 +21,7 @@ def trim(trace: Trace, start: int = None, end: int = None) -> Trace: end = len(trace.samples) if start > end: raise ValueError("Invalid trim arguments.") - return Trace(trace.samples[start:end].copy(), copy(trace.title), copy(trace.data)) + return trace.with_samples(trace.samples[start:end].copy()) @public @@ -33,7 +32,7 @@ def reverse(trace: Trace) -> Trace: :param trace: :return: """ - return Trace(np.flipud(trace.samples), copy(trace.title), copy(trace.data)) + return trace.with_samples(np.flipud(trace.samples)) @public @@ -51,5 +50,4 @@ def pad(trace: Trace, lengths: Union[Tuple[int, int], int], lengths = (lengths, lengths) if not isinstance(values, tuple): values = (values, values) - return Trace(np.pad(trace.samples, lengths, "constant", constant_values=values), - copy(trace.title), copy(trace.data)) + return trace.with_samples(np.pad(trace.samples, lengths, "constant", constant_values=values)) diff --git a/pyecsca/sca/trace/filter.py b/pyecsca/sca/trace/filter.py index 6bb17b9..cb5dde6 100644 --- a/pyecsca/sca/trace/filter.py +++ b/pyecsca/sca/trace/filter.py @@ -1,4 +1,3 @@ -from copy import copy from public import public from scipy.signal import butter, lfilter from typing import Union, Tuple @@ -13,8 +12,7 @@ def filter_any(trace: Trace, sampling_frequency: int, b, a = butter(6, tuple(map(lambda x: x / nyq, cutoff)), btype=band_type, analog=False, output='ba') else: b, a = butter(6, cutoff / nyq, btype=band_type, analog=False, output='ba') - result_samples = lfilter(b, a, trace.samples) - return Trace(result_samples, copy(trace.title), copy(trace.data)) + return trace.with_samples(lfilter(b, a, trace.samples)) @public diff --git a/pyecsca/sca/trace/process.py b/pyecsca/sca/trace/process.py index 4cdbeff..dccccc2 100644 --- a/pyecsca/sca/trace/process.py +++ b/pyecsca/sca/trace/process.py @@ -1,5 +1,4 @@ import numpy as np -from copy import copy from public import public from .trace import Trace @@ -13,7 +12,7 @@ def absolute(trace: Trace) -> Trace: :param trace: :return: """ - return Trace(np.absolute(trace.samples), copy(trace.title), copy(trace.data)) + return trace.with_samples(np.absolute(trace.samples)) @public @@ -24,7 +23,7 @@ def invert(trace: Trace) -> Trace: :param trace: :return: """ - return Trace(np.negative(trace.samples), copy(trace.title), copy(trace.data)) + return trace.with_samples(np.negative(trace.samples)) @public @@ -39,7 +38,7 @@ def threshold(trace: Trace, value) -> Trace: result_samples = trace.samples.copy() result_samples[result_samples <= value] = 0 result_samples[np.nonzero(result_samples)] = 1 - return Trace(result_samples, copy(trace.title), copy(trace.data)) + return trace.with_samples(result_samples) def rolling_window(samples: np.ndarray, window: int) -> np.ndarray: @@ -57,8 +56,8 @@ def rolling_mean(trace: Trace, window: int) -> Trace: :param window: :return: """ - return Trace(np.mean(rolling_window(trace.samples, window), -1).astype( - dtype=trace.samples.dtype), copy(trace.title), copy(trace.data)) + return trace.with_samples(np.mean(rolling_window(trace.samples, window), -1).astype( + dtype=trace.samples.dtype)) @public @@ -70,7 +69,7 @@ def offset(trace: Trace, offset) -> Trace: :param offset: :return: """ - return Trace(trace.samples + offset, copy(trace.title), copy(trace.data)) + return trace.with_samples(trace.samples + offset) def root_mean_square(trace: Trace): @@ -91,11 +90,10 @@ def recenter(trace: Trace) -> Trace: @public def normalize(trace: Trace) -> Trace: - return Trace((trace.samples - np.mean(trace.samples)) / np.std(trace.samples), - copy(trace.title), copy(trace.data)) + return trace.with_samples((trace.samples - np.mean(trace.samples)) / np.std(trace.samples)) @public def normalize_wl(trace: Trace) -> Trace: - return Trace((trace.samples - np.mean(trace.samples)) / ( - np.std(trace.samples) * len(trace.samples)), copy(trace.title), copy(trace.data)) + return trace.with_samples((trace.samples - np.mean(trace.samples)) / ( + np.std(trace.samples) * len(trace.samples))) diff --git a/pyecsca/sca/trace/sampling.py b/pyecsca/sca/trace/sampling.py index fdd5e5b..0a32c28 100644 --- a/pyecsca/sca/trace/sampling.py +++ b/pyecsca/sca/trace/sampling.py @@ -1,5 +1,4 @@ import numpy as np -from copy import copy from public import public from scipy.signal import decimate @@ -18,7 +17,7 @@ def downsample_average(trace: Trace, factor: int = 2) -> Trace: """ resized = np.resize(trace.samples, len(trace.samples) - (len(trace.samples) % factor)) result_samples = resized.reshape(-1, factor).mean(axis=1).astype(trace.samples.dtype) - return Trace(result_samples, copy(trace.title), copy(trace.data)) + return trace.with_samples(result_samples) @public @@ -32,7 +31,7 @@ def downsample_pick(trace: Trace, factor: int = 2, offset: int = 0) -> Trace: :return: """ result_samples = trace.samples[offset::factor].copy() - return Trace(result_samples, copy(trace.title), copy(trace.data)) + return trace.with_samples(result_samples) @public @@ -45,4 +44,4 @@ def downsample_decimate(trace: Trace, factor: int = 2) -> Trace: :return: """ result_samples = decimate(trace.samples, factor) - return Trace(result_samples, copy(trace.title), copy(trace.data)) + return trace.with_samples(result_samples) diff --git a/pyecsca/sca/trace/test.py b/pyecsca/sca/trace/test.py index 4fc0236..65011e6 100644 --- a/pyecsca/sca/trace/test.py +++ b/pyecsca/sca/trace/test.py @@ -14,7 +14,7 @@ def ttest_func(first_set: Sequence[Trace], second_set: Sequence[Trace], first_stack = np.stack([first.samples for first in first_set]) second_stack = np.stack([second.samples for second in second_set]) result = ttest_ind(first_stack, second_stack, axis=0, equal_var=equal_var) - return CombinedTrace(result[0], None, None) + return CombinedTrace(result[0]) @public @@ -61,4 +61,4 @@ def ks_test(first_set: Sequence[Trace], second_set: Sequence[Trace]) -> Optional results = np.empty(len(first_set[0].samples), dtype=first_set[0].samples.dtype) for i in range(len(first_set[0].samples)): results[i] = ks_2samp(first_stack[..., i], second_stack[..., i])[0] - return CombinedTrace(results, None, None) + return CombinedTrace(results) diff --git a/pyecsca/sca/trace/trace.py b/pyecsca/sca/trace/trace.py index 99a6814..82d1ee4 100644 --- a/pyecsca/sca/trace/trace.py +++ b/pyecsca/sca/trace/trace.py @@ -1,5 +1,5 @@ import weakref -from typing import Optional, Any, Mapping, Sequence +from typing import Any, Mapping, Sequence from copy import copy, deepcopy from numpy import ndarray @@ -10,15 +10,10 @@ from public import public @public class Trace(object): """A trace, which has an optional title, optional data bytes and mandatory samples.""" - title: Optional[str] - data: Optional[bytes] meta: Mapping[str, Any] samples: ndarray - def __init__(self, samples: ndarray, title: Optional[str], data: Optional[bytes], - meta: Mapping[str, Any] = None, trace_set: Any = None): - self.title = title - self.data = data + def __init__(self, samples: ndarray, meta: Mapping[str, Any] = None, trace_set: Any = None): self.meta = meta self.samples = samples self.trace_set = trace_set @@ -60,32 +55,32 @@ class Trace(object): def __eq__(self, other): if not isinstance(other, Trace): return False - return np.array_equal(self.samples, other.samples) and self.title == other.title and self.data == other.data and self.meta == other.meta + return np.array_equal(self.samples, other.samples) and self.meta == other.meta def with_samples(self, samples: ndarray) -> "Trace": - return Trace(samples, deepcopy(self.title), deepcopy(self.data), deepcopy(self.meta), deepcopy(self.trace_set)) + return Trace(samples, deepcopy(self.meta), deepcopy(self.trace_set)) def __copy__(self): - return Trace(copy(self.samples), copy(self.title), copy(self.data), copy(self.meta), copy(self.trace_set)) + return Trace(copy(self.samples), copy(self.meta), copy(self.trace_set)) def __deepcopy__(self, memodict={}): - return Trace(deepcopy(self.samples, memo=memodict), deepcopy(self.title, memo=memodict), deepcopy(self.data, memo=memodict), deepcopy(self.meta, memo=memodict), deepcopy(self.trace_set, memo=memodict)) + return Trace(deepcopy(self.samples, memo=memodict), deepcopy(self.meta, memo=memodict), + deepcopy(self.trace_set, memo=memodict)) def __repr__(self): - return f"Trace(title={self.title!r}, data={self.data!r}, samples={self.samples!r}, trace_set={self.trace_set!r})" + return f"Trace(samples={self.samples!r}, trace_set={self.trace_set!r})" @public class CombinedTrace(Trace): """A trace that was combined from other traces, `parents`.""" - def __init__(self, samples: ndarray, title: Optional[str], data: Optional[bytes], - meta: Mapping[str, Any] = None, trace_set: Any = None, + def __init__(self, samples: ndarray, meta: Mapping[str, Any] = None, trace_set: Any = None, parents: Sequence[Trace] = None): - super().__init__(samples, title, data, meta, trace_set=trace_set) + super().__init__(samples, meta, trace_set=trace_set) self.parents = None if parents is not None: self.parents = weakref.WeakSet(parents) def __repr__(self): - return f"CombinedTrace(title={self.title!r}, data={self.data!r}, samples={self.samples!r}, trace_set={self.trace_set!r}, parents={self.parents})" + return f"CombinedTrace(samples={self.samples!r}, trace_set={self.trace_set!r}, parents={self.parents})" diff --git a/pyecsca/sca/trace_set/chipwhisperer.py b/pyecsca/sca/trace_set/chipwhisperer.py index e70c457..ea1fc99 100644 --- a/pyecsca/sca/trace_set/chipwhisperer.py +++ b/pyecsca/sca/trace_set/chipwhisperer.py @@ -44,7 +44,7 @@ class ChipWhispererTraceSet(TraceSet): for samples, key, textin, textout in zip_longest(data["traces"], data["keylist"], data["textin"], data["textout"]): traces.append( - Trace(samples, None, None, {"key": key, "textin": textin, "textout": textout})) + Trace(samples, {"key": key, "textin": textin, "textout": textout})) del data["traces"] del data["keylist"] del data["textin"] diff --git a/pyecsca/sca/trace_set/hdf5.py b/pyecsca/sca/trace_set/hdf5.py index b8a1ad1..561d3d3 100644 --- a/pyecsca/sca/trace_set/hdf5.py +++ b/pyecsca/sca/trace_set/hdf5.py @@ -27,7 +27,7 @@ class HDF5TraceSet(TraceSet): for k, v in hdf5.items(): meta = dict(hdf5[k].attrs) if hdf5[k].attrs else None samples = hdf5[k] - traces.append(Trace(np.array(samples, dtype=samples.dtype), None, None, meta)) + traces.append(Trace(np.array(samples, dtype=samples.dtype), meta)) hdf5.close() return HDF5TraceSet(*traces, **kwargs) @@ -44,7 +44,7 @@ class HDF5TraceSet(TraceSet): for k, v in hdf5.items(): meta = dict(hdf5[k].attrs) if hdf5[k].attrs else None samples = hdf5[k] - traces.append(Trace(samples, k, None, meta)) + traces.append(Trace(samples, meta)) return HDF5TraceSet(*traces, **kwargs, _file=hdf5) def __setitem__(self, key, value): diff --git a/pyecsca/sca/trace_set/inspector.py b/pyecsca/sca/trace_set/inspector.py index 4f45aa0..6d79f12 100644 --- a/pyecsca/sca/trace_set/inspector.py +++ b/pyecsca/sca/trace_set/inspector.py @@ -190,11 +190,11 @@ class InspectorTraceSet(TraceSet): samples = np.frombuffer( file.read(dtype.itemsize * tags["num_samples"]), dtype, tags["num_samples"]) - result.append(Trace(samples, title, data)) + result.append(Trace(samples, {"title": title, "data": data})) return result, tags @classmethod - def inplace(cls, input: Union[str, Path, bytes, RawIOBase, BufferedIOBase]) -> "PickleTraceSet": + def inplace(cls, input: Union[str, Path, bytes, RawIOBase, BufferedIOBase]) -> "TraceSet": raise NotImplementedError def write(self, output: Union[str, Path, RawIOBase, BufferedIOBase]): @@ -231,10 +231,10 @@ class InspectorTraceSet(TraceSet): file.write(b"\x5f\x00") for trace in self._traces: - if self.title_space != 0 and trace.title is not None: - file.write(Parsers.write_str(trace.title)) - if self.data_space != 0 and trace.data is not None: - file.write(trace.data) + if self.title_space != 0 and trace.meta["title"] is not None: + file.write(Parsers.write_str(trace.meta["title"])) + if self.data_space != 0 and trace.meta["data"] is not None: + file.write(trace.meta["data"]) unscaled = InspectorTraceSet.__unscale(trace.samples, self.y_scale, self.sample_coding) try: unscaled.tofile(file) diff --git a/test/ec/test_configuration.py b/test/ec/test_configuration.py index 54e4827..9c8e361 100644 --- a/test/ec/test_configuration.py +++ b/test/ec/test_configuration.py @@ -1,7 +1,7 @@ from unittest import TestCase from pyecsca.ec.configuration import (all_configurations, HashType, RandomMod, Multiplication, - Squaring, Reduction) + Squaring, Reduction, Inversion) from pyecsca.ec.model import ShortWeierstrassModel from pyecsca.ec.mult import LTRMultiplier from .utils import slow @@ -15,7 +15,8 @@ class ConfigurationTests(TestCase): "mod_rand": RandomMod.SAMPLE, "mult": Multiplication.BASE, "sqr": Squaring.BASE, - "red": Reduction.BASE + "red": Reduction.BASE, + "inv": Inversion.GCD } @slow diff --git a/test/sca/test_align.py b/test/sca/test_align.py index f446f17..8aa489b 100644 --- a/test/sca/test_align.py +++ b/test/sca/test_align.py @@ -12,9 +12,9 @@ class AlignTests(Plottable): first_arr = np.array([10, 64, 120, 64, 10, 10, 10, 10, 10], dtype=np.dtype("i1")) second_arr = np.array([10, 10, 10, 10, 50, 80, 50, 20, 10], dtype=np.dtype("i1")) third_arr = np.array([70, 30, 42, 35, 28, 21, 15, 10, 5], dtype=np.dtype("i1")) - a = Trace(first_arr, None, None) - b = Trace(second_arr, None, None) - c = Trace(third_arr, None, None) + a = Trace(first_arr) + b = Trace(second_arr) + c = Trace(third_arr) result = align_correlation(a, b, c, reference_offset=1, reference_length=3, max_offset=4, min_correlation=0.65) self.assertIsNotNone(result) self.assertEqual(len(result), 2) @@ -36,16 +36,16 @@ class AlignTests(Plottable): def test_peak_align(self): first_arr = np.array([10, 64, 14, 120, 15, 30, 10, 15, 20, 15, 15, 10, 10], dtype=np.dtype("i1")) second_arr = np.array([10, 10, 10, 10, 90, 40, 50, 20, 10, 17, 16, 10, 10], dtype=np.dtype("i1")) - a = Trace(first_arr, None, None) - b = Trace(second_arr, None, None) + a = Trace(first_arr) + b = Trace(second_arr) result = align_peaks(a, b, reference_offset=2, reference_length=5, max_offset=3) self.assertEqual(np.argmax(result[0].samples), np.argmax(result[1].samples)) def test_sad_align(self): first_arr = np.array([10, 64, 14, 120, 15, 30, 10, 15, 20, 15, 15, 10, 10], dtype=np.dtype("i1")) second_arr = np.array([10, 10, 10, 10, 90, 40, 50, 20, 10, 17, 16, 10, 10], dtype=np.dtype("i1")) - a = Trace(first_arr, None, None) - b = Trace(second_arr, None, None) + a = Trace(first_arr) + b = Trace(second_arr) result = align_sad(a, b, reference_offset=2, reference_length=5, max_offset=3) self.assertEqual(len(result), 2) @@ -53,9 +53,9 @@ class AlignTests(Plottable): first_arr = np.array([10, 64, 14, 120, 15, 30, 10, 15, 20, 15, 15, 10, 10, 8, 10, 12, 10, 13, 9], dtype=np.dtype("i1")) second_arr = np.array([10, 10, 10, 10, 60, 40, 90, 20, 10, 17, 16, 10, 10, 10, 10, 10, 17, 12, 10], dtype=np.dtype("i1")) third_arr = np.array([10, 30, 20, 21, 15, 8, 10, 37, 21, 77, 20, 28, 25, 10, 9, 10, 15, 9, 10], dtype=np.dtype("i1")) - a = Trace(first_arr, None, None) - b = Trace(second_arr, None, None) - c = Trace(third_arr, None, None) + a = Trace(first_arr) + b = Trace(second_arr) + c = Trace(third_arr) result = align_dtw_scale(a, b, c) self.assertEqual(np.argmax(result[0].samples), np.argmax(result[1].samples)) @@ -66,9 +66,9 @@ class AlignTests(Plottable): first_arr = np.array([10, 64, 14, 120, 15, 30, 10, 15, 20, 15, 15, 10, 10, 8, 10, 12, 10, 13, 9], dtype=np.dtype("i1")) second_arr = np.array([10, 10, 10, 10, 60, 40, 90, 20, 10, 17, 16, 10, 10, 10, 10, 10, 17, 12, 10], dtype=np.dtype("i1")) third_arr = np.array([10, 30, 20, 21, 15, 8, 10, 47, 21, 77, 20, 28, 25, 10, 9, 10, 15, 9, 10], dtype=np.dtype("i1")) - a = Trace(first_arr, None, None) - b = Trace(second_arr, None, None) - c = Trace(third_arr, None, None) + a = Trace(first_arr) + b = Trace(second_arr) + c = Trace(third_arr) result = align_dtw(a, b, c) self.assertEqual(np.argmax(result[0].samples), np.argmax(result[1].samples)) diff --git a/test/sca/test_combine.py b/test/sca/test_combine.py index 41df9fb..a35772b 100644 --- a/test/sca/test_combine.py +++ b/test/sca/test_combine.py @@ -7,9 +7,9 @@ from pyecsca.sca import Trace, CombinedTrace, average, conditional_average, stan class CombineTests(TestCase): def setUp(self): - self.a = Trace(np.array([20, 80], dtype=np.dtype("i1")), None, b"\xff", None) - self.b = Trace(np.array([30, 42], dtype=np.dtype("i1")), None, b"\xff", None) - self.c = Trace(np.array([78, 56], dtype=np.dtype("i1")), None, b"\x00", None) + self.a = Trace(np.array([20, 80], dtype=np.dtype("i1")), {"data": b"\xff"}) + self.b = Trace(np.array([30, 42], dtype=np.dtype("i1")), {"data": b"\xff"}) + self.c = Trace(np.array([78, 56], dtype=np.dtype("i1")), {"data": b"\x00"}) def test_average(self): self.assertIsNone(average()) @@ -22,7 +22,7 @@ class CombineTests(TestCase): def test_conditional_average(self): result = conditional_average(self.a, self.b, self.c, - condition=lambda trace: trace.data[0] == 0xff) + condition=lambda trace: trace.meta["data"] == b"\xff") self.assertIsInstance(result, CombinedTrace) self.assertEqual(len(result.samples), 2) self.assertEqual(result.samples[0], 25) diff --git a/test/sca/test_edit.py b/test/sca/test_edit.py index f5b529e..f701d83 100644 --- a/test/sca/test_edit.py +++ b/test/sca/test_edit.py @@ -8,7 +8,7 @@ from pyecsca.sca import Trace, trim, reverse, pad class EditTests(TestCase): def setUp(self): - self._trace = Trace(np.array([10, 20, 30, 40, 50], dtype=np.dtype("i1")), None, None, None) + self._trace = Trace(np.array([10, 20, 30, 40, 50], dtype=np.dtype("i1"))) def test_trim(self): result = trim(self._trace, 2) diff --git a/test/sca/test_filter.py b/test/sca/test_filter.py index fda5626..1c9e9ca 100644 --- a/test/sca/test_filter.py +++ b/test/sca/test_filter.py @@ -10,7 +10,7 @@ class FilterTests(Plottable): def setUp(self): self._trace = Trace( np.array([5, 12, 15, 13, 15, 11, 7, 2, -4, -8, -10, -8, -13, -9, -11, -8, -5], - dtype=np.dtype("i1")), None, None, None) + dtype=np.dtype("i1")), None) def test_lowpass(self): result = filter_lowpass(self._trace, 100, 20) diff --git a/test/sca/test_match.py b/test/sca/test_match.py index 1781d9d..cd0b780 100644 --- a/test/sca/test_match.py +++ b/test/sca/test_match.py @@ -9,22 +9,20 @@ from .utils import Plottable class MatchingTests(Plottable): def test_simple_match(self): - pattern = Trace(np.array([1, 15, 12, -10, 0, 13, 17, -1, 0], dtype=np.dtype("i1")), None, - None, None) + pattern = Trace(np.array([1, 15, 12, -10, 0, 13, 17, -1, 0], dtype=np.dtype("i1")), None) base = Trace(np.array( [0, 1, 3, 1, 2, -2, -3, 1, 15, 12, -10, 0, 13, 17, -1, 0, 3, 1], - dtype=np.dtype("i1")), None, None, None) + dtype=np.dtype("i1")), None) filtered = match_part(base, 7, 9) self.assertListEqual(filtered, [7]) self.plot(base=base, pattern=pad(pattern, (filtered[0], 0))) def test_multiple_match(self): - pattern = Trace(np.array([1, 15, 12, -10, 0, 13, 17, -1, 0], dtype=np.dtype("i1")), None, - None, None) + pattern = Trace(np.array([1, 15, 12, -10, 0, 13, 17, -1, 0], dtype=np.dtype("i1")), None) base = Trace(np.array( [0, 1, 3, 1, 2, -2, -3, 1, 18, 10, -5, 0, 13, 17, -1, 0, 3, 1, 2, 5, 13, 8, -8, 1, 11, 15, 0, 1, 5, 2, 4], - dtype=np.dtype("i1")), None, None, None) + dtype=np.dtype("i1")), None) filtered = match_pattern(base, pattern, 0.9) self.assertListEqual(filtered, [7, 19]) self.plot(base=base, pattern1=pad(pattern, (filtered[0], 0)), pattern2=pad(pattern, (filtered[1], 0))) diff --git a/test/sca/test_plot.py b/test/sca/test_plot.py index 9158e5c..5b65c87 100644 --- a/test/sca/test_plot.py +++ b/test/sca/test_plot.py @@ -11,8 +11,8 @@ from .utils import Plottable class PlotTests(Plottable): def setUp(self) -> None: - self.trace1 = Trace(np.array([6, 7, 3, -2, 5, 1], dtype=np.dtype("i1")), None, None) - self.trace2 = Trace(np.array([2, 3, 7, 0, -1, 0], dtype=np.dtype("i1")), None, None) + self.trace1 = Trace(np.array([6, 7, 3, -2, 5, 1], dtype=np.dtype("i1"))) + self.trace2 = Trace(np.array([2, 3, 7, 0, -1, 0], dtype=np.dtype("i1"))) def test_html(self): if getenv("PYECSCA_TEST_PLOTS") is None: diff --git a/test/sca/test_process.py b/test/sca/test_process.py index 49d520f..d1bbddb 100644 --- a/test/sca/test_process.py +++ b/test/sca/test_process.py @@ -7,7 +7,7 @@ from pyecsca.sca import Trace, absolute, invert, threshold, rolling_mean, offset class ProcessTests(TestCase): def setUp(self): - self._trace = Trace(np.array([30, -60, 145, 247], dtype=np.dtype("i2")), None, None, None) + self._trace = Trace(np.array([30, -60, 145, 247], dtype=np.dtype("i2")), None) def test_absolute(self): result = absolute(self._trace) diff --git a/test/sca/test_sampling.py b/test/sca/test_sampling.py index 1af1579..79f55b4 100644 --- a/test/sca/test_sampling.py +++ b/test/sca/test_sampling.py @@ -8,7 +8,7 @@ from .utils import Plottable class SamplingTests(Plottable): def setUp(self): - self._trace = Trace(np.array([20, 40, 50, 50, 10], dtype=np.dtype("i1")), None, None) + self._trace = Trace(np.array([20, 40, 50, 50, 10], dtype=np.dtype("i1"))) def test_downsample_average(self): result = downsample_average(self._trace, 2) @@ -29,7 +29,7 @@ class SamplingTests(Plottable): def test_downsample_decimate(self): trace = Trace(np.array([20, 30, 55, 18, 15, 10, 35, 24, 21, 15, 10, 8, -10, -5, -8, -12, -15, -18, -34, -21, -17, -10, -5, -12, -6, -2, - 4, 8, 21, 28], dtype=np.dtype("i1")), None, None, None) + 4, 8, 21, 28], dtype=np.dtype("i1"))) result = downsample_decimate(trace, 2) self.assertIsNotNone(result) self.assertIsInstance(result, Trace) diff --git a/test/sca/test_test.py b/test/sca/test_test.py index f012f7f..e2b3969 100644 --- a/test/sca/test_test.py +++ b/test/sca/test_test.py @@ -8,19 +8,16 @@ from pyecsca.sca import Trace, welch_ttest, student_ttest, ks_test class TTestTests(TestCase): def setUp(self): - self.a = Trace(np.array([20, 80], dtype=np.dtype("i1")), None, b"\xff", None) - self.b = Trace(np.array([30, 42], dtype=np.dtype("i1")), None, b"\xff", None) - self.c = Trace(np.array([78, 56], dtype=np.dtype("i1")), None, b"\x00", None) - self.d = Trace(np.array([98, 36], dtype=np.dtype("i1")), None, b"\x00", None) + self.a = Trace(np.array([20, 80], dtype=np.dtype("i1"))) + self.b = Trace(np.array([30, 42], dtype=np.dtype("i1"))) + self.c = Trace(np.array([78, 56], dtype=np.dtype("i1"))) + self.d = Trace(np.array([98, 36], dtype=np.dtype("i1"))) def test_welch_ttest(self): self.assertIsNotNone(welch_ttest([self.a, self.b], [self.c, self.d])) - a = Trace(np.array([19.8, 20.4, 19.6, 17.8, 18.5, 18.9, 18.3, 18.9, 19.5, 22.0]), None, - None, None) - b = Trace(np.array([28.2, 26.6, 20.1, 23.3, 25.2, 22.1, 17.7, 27.6, 20.6, 13.7]), None, - None, None) - c = Trace(np.array([20.2, 21.6, 27.1, 13.3, 24.2, 20.1, 11.7, 25.6, 26.6, 21.4]), None, - None, None) + a = Trace(np.array([19.8, 20.4, 19.6, 17.8, 18.5, 18.9, 18.3, 18.9, 19.5, 22.0])) + b = Trace(np.array([28.2, 26.6, 20.1, 23.3, 25.2, 22.1, 17.7, 27.6, 20.6, 13.7])) + c = Trace(np.array([20.2, 21.6, 27.1, 13.3, 24.2, 20.1, 11.7, 25.6, 26.6, 21.4])) result = welch_ttest([a, b], [b, c]) self.assertIsNotNone(result) @@ -35,8 +32,8 @@ class KolmogorovSmirnovTests(TestCase): def test_ks_test(self): self.assertIsNone(ks_test([], [])) - a = Trace(np.array([20, 80], dtype=np.dtype("i1")), None, b"\xff", None) - b = Trace(np.array([30, 42], dtype=np.dtype("i1")), None, b"\xff", None) - c = Trace(np.array([78, 56], dtype=np.dtype("i1")), None, b"\x00", None) - d = Trace(np.array([98, 36], dtype=np.dtype("i1")), None, b"\x00", None) + a = Trace(np.array([20, 80], dtype=np.dtype("i1"))) + b = Trace(np.array([30, 42], dtype=np.dtype("i1"))) + c = Trace(np.array([78, 56], dtype=np.dtype("i1"))) + d = Trace(np.array([98, 36], dtype=np.dtype("i1"))) self.assertIsNotNone(ks_test([a, b], [c, d])) diff --git a/test/sca/test_trace.py b/test/sca/test_trace.py index d09c4f5..91b640d 100644 --- a/test/sca/test_trace.py +++ b/test/sca/test_trace.py @@ -6,7 +6,7 @@ from pyecsca.sca import Trace class TraceTests(TestCase): def test_basic(self): - trace = Trace(np.array([10, 15, 24], dtype=np.dtype("i1")), "Name", b"\xff\xaa", None) + trace = Trace(np.array([10, 15, 24], dtype=np.dtype("i1"))) self.assertIsNotNone(trace) self.assertIn("Trace", str(trace)) self.assertIsNone(trace.trace_set) diff --git a/test/sca/test_traceset.py b/test/sca/test_traceset.py index 72eac47..351f025 100644 --- a/test/sca/test_traceset.py +++ b/test/sca/test_traceset.py @@ -9,9 +9,9 @@ import numpy as np from pyecsca.sca import (TraceSet, InspectorTraceSet, ChipWhispererTraceSet, PickleTraceSet, HDF5TraceSet, Trace) -EXAMPLE_TRACES = [Trace(np.array([20, 40, 50, 50, 10], dtype=np.dtype("i1")), None, None), - Trace(np.array([1, 2, 3, 4, 5], dtype=np.dtype("i1")), None, None), - Trace(np.array([6, 7, 8, 9, 10], dtype=np.dtype("i1")), None, None)] +EXAMPLE_TRACES = [Trace(np.array([20, 40, 50, 50, 10], dtype=np.dtype("i1"))), + Trace(np.array([1, 2, 3, 4, 5], dtype=np.dtype("i1"))), + Trace(np.array([6, 7, 8, 9, 10], dtype=np.dtype("i1")))] EXAMPLE_KWARGS = {"num_traces": 3, "thingy": "abc"} @@ -97,7 +97,7 @@ class HDF5TraceSetTests(TestCase): shutil.copy("test/data/test.h5", path) trace_set = HDF5TraceSet.inplace(path) self.assertIsNotNone(trace_set) - test_trace = Trace(np.array([6, 7], dtype=np.dtype("i1")), None, None, meta={"thing": "ring"}) + test_trace = Trace(np.array([6, 7], dtype=np.dtype("i1")), meta={"thing": "ring"}) trace_set[0] = deepcopy(test_trace) trace_set.save() trace_set.close() |
