コード例 #1
0
    def test_inplace(self):
        with tempfile.TemporaryDirectory() as dirname:
            path = os.path.join(dirname, "test.h5")
            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")),
                               meta={"thing": "ring"})
            other_trace = Trace(np.array([15, 7], dtype=np.dtype("i1")),
                                meta={"a": "b"})
            trace_set.append(test_trace)
            self.assertEqual(len(trace_set), 4)
            trace_set.append(other_trace)
            trace_set.remove(other_trace)
            self.assertEqual(len(trace_set), 4)
            trace_set.save()
            trace_set.close()

            test_set = HDF5TraceSet.read(path)
            self.assertEqual(test_set.get(3), test_set[3])
            self.assertTrue(
                np.array_equal(test_set[3].samples, test_trace.samples))
            self.assertEqual(test_set[3].meta["thing"],
                             test_trace.meta["thing"])
            self.assertEqual(test_set[3], test_trace)
コード例 #2
0
ファイル: test_align.py プロジェクト: ClarkSev/pyecsca
    def test_dtw_align_scale(self):
        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("f2"))
        second_arr = np.array([
            10, 10, 60, 40, 90, 20, 10, 17, 16, 10, 10, 10, 10, 10, 17, 12, 10
        ],
                              dtype=np.dtype("f2"))
        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("f2"))
        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))
        self.assertEqual(np.argmax(result[1].samples),
                         np.argmax(result[2].samples))
        self.plot(*result)

        result_other = align_dtw_scale(a, b, c, fast=False)

        self.assertEqual(np.argmax(result_other[0].samples),
                         np.argmax(result_other[1].samples))
        self.assertEqual(np.argmax(result_other[1].samples),
                         np.argmax(result_other[2].samples))
        self.plot(*result_other)
コード例 #3
0
ファイル: test_align.py プロジェクト: J08nY/pyecsca
 def test_align(self):
     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],
                           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)
     b = Trace(second_arr)
     c = Trace(third_arr)
     result, offsets = 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)
     np.testing.assert_equal(result[0].samples, first_arr)
     np.testing.assert_equal(
         result[1].samples,
         np.array([10, 50, 80, 50, 20, 0, 0, 0], dtype=np.dtype("i1")),
     )
     self.assertEqual(len(offsets), 2)
     self.assertEqual(offsets[0], 0)
     self.assertEqual(offsets[1], 3)
コード例 #4
0
ファイル: test_test.py プロジェクト: ClarkSev/pyecsca
    def test_ks_test(self):
        self.assertIsNone(ks_test([], []))

        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]))
コード例 #5
0
ファイル: test_match.py プロジェクト: ClarkSev/pyecsca
 def test_simple_match(self):
     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)
     filtered = match_part(base, 7, 9)
     self.assertListEqual(filtered, [7])
     self.plot(base=base, pattern=pad(pattern, (filtered[0], 0)))
コード例 #6
0
ファイル: test_align.py プロジェクト: ClarkSev/pyecsca
 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, 90, 40, 50, 20, 10, 17, 16, 10, 10],
                           dtype=np.dtype("i1"))
     a = Trace(first_arr)
     b = Trace(second_arr)
     result, offsets = align_sad(a,
                                 b,
                                 reference_offset=2,
                                 reference_length=5,
                                 max_offset=3)
     self.assertEqual(len(result), 2)
コード例 #7
0
ファイル: test_test.py プロジェクト: ClarkSev/pyecsca
    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]))
        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)
コード例 #8
0
 def test_multiple_match(self):
     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,
     )
     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)),
     )
コード例 #9
0
ファイル: test_align.py プロジェクト: ClarkSev/pyecsca
 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],
                           dtype=np.dtype("i1"))
     a = Trace(first_arr)
     b = Trace(second_arr)
     result, offsets = 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))
コード例 #10
0
 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)
コード例 #11
0
ファイル: utils.py プロジェクト: J08nY/pyecsca
 def plot(self, *traces: Trace, **kwtraces: Trace):
     if not force_plot and getenv("PYECSCA_TEST_PLOTS") is None:
         return
     fig = plt.figure()
     ax = fig.add_subplot(111)
     for i, trace in enumerate(traces):
         ax.plot(trace.samples, label=str(i))
     for name, trace in kwtraces.items():
         ax.plot(trace.samples, label=name)
     ax.legend(loc="best")
     plt.savefig(self.get_fname() + ".png")
コード例 #12
0
ファイル: test_sampling.py プロジェクト: ClarkSev/pyecsca
 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")))
     result = downsample_decimate(trace, 2)
     self.assertIsNotNone(result)
     self.assertIsInstance(result, Trace)
     self.assertEqual(len(result.samples), 15)
     self.plot(trace, result)
コード例 #13
0
 def test_downsample_min(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"),
         )
     )
     result = downsample_min(trace, 2)
     self.assertIsNotNone(result)
     self.assertIsInstance(result, Trace)
     self.assertEqual(len(result.samples), 15)
     self.assertEqual(
         list(result),
         [20, 18, 10, 24, 15, 8, -10, -12, -18, -34, -17, -12, -6, 4, 21],
     )
コード例 #14
0
ファイル: test_combine.py プロジェクト: ClarkSev/pyecsca
 def setUp(self):
     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"})
コード例 #15
0
ファイル: test_process.py プロジェクト: ClarkSev/pyecsca
 def setUp(self):
     self._trace = Trace(
         np.array([30, -60, 145, 247], dtype=np.dtype("i2")), None)
コード例 #16
0
ファイル: test_edit.py プロジェクト: ClarkSev/pyecsca
 def setUp(self):
     self._trace = Trace(
         np.array([10, 20, 30, 40, 50], dtype=np.dtype("i1")))
コード例 #17
0
ファイル: test_test.py プロジェクト: ClarkSev/pyecsca
 def setUp(self):
     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")))
コード例 #18
0
import os.path
import shutil
import tempfile
from copy import deepcopy
from unittest import TestCase

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")),
          {"something": 5}),
    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"}


class TraceSetTests(TestCase):
    def test_create(self):
        self.assertIsNotNone(TraceSet())
        self.assertIsNotNone(InspectorTraceSet())
        self.assertIsNotNone(ChipWhispererTraceSet())
        self.assertIsNotNone(PickleTraceSet())
        self.assertIsNotNone(HDF5TraceSet())


class InspectorTraceSetTests(TestCase):
    def test_load_fname(self):
コード例 #19
0
 def test_basic(self):
     trace = Trace(np.array([10, 15, 24], dtype=np.dtype("i1")))
     self.assertIsNotNone(trace)
     self.assertIn("Trace", str(trace))
     self.assertIsNone(trace.trace_set)