Commit 8e9e3e13 authored by Rémi Huguet's avatar Rémi Huguet
Browse files

feat: rename t-Test container arguments to ths_1 and ths_2

parent 5123d833
Loading
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -7,17 +7,17 @@ class TTestContainer:
    """Wrapper container for trace header sets dedicated to TTest analysis.

    Args:
        ths_fix, ths_random (:class:`TraceHeaderSet`): the two trace header set to use for the TTest.
        ths_1, ths_2 (:class:`TraceHeaderSet`): the two trace header set to use for the TTest.

    Attributes:
        containers (list): list of two Container.

    """

    def __init__(self, ths_fix, ths_random, frame=None, preprocesses=[]):
    def __init__(self, ths_1, ths_2, frame=None, preprocesses=[]):
        self.containers = [
            _container.Container(ths=ths_fix, frame=frame, preprocesses=preprocesses),
            _container.Container(ths=ths_random, frame=frame, preprocesses=preprocesses)
            _container.Container(ths=ths_1, frame=frame, preprocesses=preprocesses),
            _container.Container(ths=ths_2, frame=frame, preprocesses=preprocesses)
        ]


+47 −47
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import numpy as np


@pytest.fixture
def ths_fix():
def ths_1():
    shape = (2000, 1000)
    sample = np.random.randint(0, 255, (1000,), dtype='uint8')
    plain = np.random.randint(0, 255, (16), dtype='uint8')
@@ -14,38 +14,38 @@ def ths_fix():


@pytest.fixture
def ths_random():
def ths_2():
    shape = (2000, 1000)
    samples = np.random.randint(0, 255, shape, dtype='uint8')
    plaintext = np.random.randint(0, 255, (shape[0], 16), dtype='uint8')
    return scared.traces.formats.read_ths_from_ram(samples=samples, plaintext=plaintext)


def test_ttest_container_raises_exception_if_incorrect_ths_provided(ths_fix, ths_random):
def test_ttest_container_raises_exception_if_incorrect_ths_provided(ths_1, ths_2):
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix='foo', ths_random='bar')
        scared.TTestContainer(ths_1='foo', ths_2='bar')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix=ths_fix, ths_random='bar')
        scared.TTestContainer(ths_1=ths_1, ths_2='bar')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix='foo', ths_random=ths_random)
        scared.TTestContainer(ths_1='foo', ths_2=ths_2)


def test_ttest_container_raises_exception_if_incorrect_frame_provided(ths_fix, ths_random):
def test_ttest_container_raises_exception_if_incorrect_frame_provided(ths_1, ths_2):
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix=ths_fix, ths_random=ths_random, frame='foo')
        scared.TTestContainer(ths_1=ths_1, ths_2=ths_2, frame='foo')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, frame=2121.1)
        scared.TTestContainer(ths_1, ths_2, frame=2121.1)
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, frame={})
        scared.TTestContainer(ths_1, ths_2, frame={})


def test_container_raises_error_if_bad_preprocesses(ths_fix, ths_random):
def test_container_raises_error_if_bad_preprocesses(ths_1, ths_2):
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, preprocesses='foo')
        scared.TTestContainer(ths_1, ths_2, preprocesses='foo')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, preprocesses=['foo', 123])
        scared.TTestContainer(ths_1, ths_2, preprocesses=['foo', 123])
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, preprocesses=134)
        scared.TTestContainer(ths_1, ths_2, preprocesses=134)


def test_ttest_analysis_raises_exception_if_invalid_precision_is_passed():
@@ -55,7 +55,7 @@ def test_ttest_analysis_raises_exception_if_invalid_precision_is_passed():
        scared.TTestAnalysis(precision='int8')


def test_ttest_analysis_initialize_two_ttest_accumulator(ths_fix, ths_random):
def test_ttest_analysis_initialize_two_ttest_accumulator(ths_1, ths_2):
    analysis = scared.TTestAnalysis()
    assert analysis.accumulators is not None
    assert len(analysis.accumulators) == 2
@@ -71,15 +71,15 @@ def test_accumulator_update_raises_exception_if_invalid_traces():
        accu.update('foo')


def test_ttest_accumulator_update_its_accumulator(ths_fix):
def test_ttest_accumulator_update_its_accumulator(ths_1):
    accu = scared.TTestAccumulator(precision='float32')
    accu.update(ths_fix.samples[:100, :])
    traces = ths_fix.samples[:100, :].astype('float32')
    accu.update(ths_1.samples[:100, :])
    traces = ths_1.samples[:100, :].astype('float32')
    assert np.array_equal(accu.sum, np.sum(traces, axis=0))
    assert np.array_equal(accu.sum_squared, np.sum(traces ** 2, axis=0))
    assert accu.processed_traces == 100
    accu.update(ths_fix.samples[100:151, :])
    traces = ths_fix.samples[:151, :].astype('float32')
    accu.update(ths_1.samples[100:151, :])
    traces = ths_1.samples[:151, :].astype('float32')
    assert np.array_equal(accu.sum, np.sum(traces, axis=0))
    assert np.array_equal(accu.sum_squared, np.sum(traces ** 2, axis=0))
    assert accu.processed_traces == 151
@@ -91,10 +91,10 @@ def test_ttest_compute_raise_exception_if_no_trace_are_processed_or_state_not_in
        d.compute()


def test_ttest_compute(ths_fix, ths_random):
def test_ttest_compute(ths_1, ths_2):
    accu = scared.TTestAccumulator(precision='float32')
    accu.update(ths_fix.samples[:100, :])
    traces = ths_fix.samples[:100, :].astype('float32')
    accu.update(ths_1.samples[:100, :])
    traces = ths_1.samples[:100, :].astype('float32')
    assert np.array_equal(accu.sum, np.sum(traces, axis=0))
    assert np.array_equal(accu.sum_squared, np.sum(traces ** 2, axis=0))
    assert accu.processed_traces == 100
@@ -104,18 +104,18 @@ def test_ttest_compute(ths_fix, ths_random):
    assert np.array_equal(accu.var, np.sum(traces ** 2, axis=0) / 100 - (np.sum(traces, axis=0) / 100) ** 2)


def test_ttest_analysis_run(ths_fix, ths_random):
    cont = scared.TTestContainer(ths_fix, ths_random)
def test_ttest_analysis_run(ths_1, ths_2):
    cont = scared.TTestContainer(ths_1, ths_2)
    analysis = scared.TTestAnalysis(precision='float64')
    analysis.run(cont)

    t_1 = ths_fix.samples[:].astype('float64')
    t_2 = ths_random.samples[:].astype('float64')
    t_1 = ths_1.samples[:].astype('float64')
    t_2 = ths_2.samples[:].astype('float64')

    mean_1 = np.sum(t_1, axis=0) / len(ths_fix)
    mean_2 = np.sum(t_2, axis=0) / len(ths_random)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_fix) - mean_1 ** 2) / len(ths_fix)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_random) - mean_2 ** 2) / len(ths_random)
    mean_1 = np.sum(t_1, axis=0) / len(ths_1)
    mean_2 = np.sum(t_2, axis=0) / len(ths_2)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_1) - mean_1 ** 2) / len(ths_1)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_2) - mean_2 ** 2) / len(ths_2)
    expected = (mean_1 - mean_2) / np.sqrt(var_1 + var_2)

    assert analysis.result is not None
@@ -128,36 +128,36 @@ def test_ttest_analysis_run_raises_exception_if_container_not_ttest_container():
        analysis.run('foo')


def test_ttest_analysis_run_with_frame(ths_fix, ths_random):
    cont = scared.TTestContainer(ths_fix, ths_random, frame=slice(0, 100))
def test_ttest_analysis_run_with_frame(ths_1, ths_2):
    cont = scared.TTestContainer(ths_1, ths_2, frame=slice(0, 100))
    analysis = scared.TTestAnalysis(precision='float64')
    analysis.run(cont)

    t_1 = ths_fix.samples[:, :100].astype('float64')
    t_2 = ths_random.samples[:, :100].astype('float64')
    t_1 = ths_1.samples[:, :100].astype('float64')
    t_2 = ths_2.samples[:, :100].astype('float64')

    mean_1 = np.sum(t_1, axis=0) / len(ths_fix)
    mean_2 = np.sum(t_2, axis=0) / len(ths_random)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_fix) - mean_1 ** 2) / len(ths_fix)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_random) - mean_2 ** 2) / len(ths_random)
    mean_1 = np.sum(t_1, axis=0) / len(ths_1)
    mean_2 = np.sum(t_2, axis=0) / len(ths_2)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_1) - mean_1 ** 2) / len(ths_1)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_2) - mean_2 ** 2) / len(ths_2)
    expected = (mean_1 - mean_2) / np.sqrt(var_1 + var_2)

    assert analysis.result is not None
    assert np.array_equal(expected, analysis.result)


def test_ttest_analysis_run_with_preprocesses(ths_fix, ths_random):
    cont = scared.TTestContainer(ths_fix, ths_random, preprocesses=[scared.preprocesses.square])
def test_ttest_analysis_run_with_preprocesses(ths_1, ths_2):
    cont = scared.TTestContainer(ths_1, ths_2, preprocesses=[scared.preprocesses.square])
    analysis = scared.TTestAnalysis(precision='float64')
    analysis.run(cont)

    t_1 = scared.preprocesses.square(ths_fix.samples[:]).astype('float64')
    t_2 = scared.preprocesses.square(ths_random.samples[:]).astype('float64')
    t_1 = scared.preprocesses.square(ths_1.samples[:]).astype('float64')
    t_2 = scared.preprocesses.square(ths_2.samples[:]).astype('float64')

    mean_1 = np.sum(t_1, axis=0) / len(ths_fix)
    mean_2 = np.sum(t_2, axis=0) / len(ths_random)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_fix) - mean_1 ** 2) / len(ths_fix)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_random) - mean_2 ** 2) / len(ths_random)
    mean_1 = np.sum(t_1, axis=0) / len(ths_1)
    mean_2 = np.sum(t_2, axis=0) / len(ths_2)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_1) - mean_1 ** 2) / len(ths_1)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_2) - mean_2 ** 2) / len(ths_2)
    expected = (mean_1 - mean_2) / np.sqrt(var_1 + var_2)

    assert analysis.result is not None
+31 −31
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import numpy as np


@pytest.fixture
def ths_fix():
def ths_1():
    shape = (2000, 1000)
    sample = np.random.randint(0, 255, (1000,), dtype='uint8')
    plain = np.random.randint(0, 255, (16), dtype='uint8')
@@ -14,38 +14,38 @@ def ths_fix():


@pytest.fixture
def ths_random():
def ths_2():
    shape = (2000, 1000)
    samples = np.random.randint(0, 255, shape, dtype='uint8')
    plaintext = np.random.randint(0, 255, (shape[0], 16), dtype='uint8')
    return scared.traces.formats.read_ths_from_ram(samples=samples, plaintext=plaintext)


def test_ttest_container_raises_exception_if_incorrect_ths_provided(ths_fix, ths_random):
def test_ttest_container_raises_exception_if_incorrect_ths_provided(ths_1, ths_2):
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix='foo', ths_random='bar')
        scared.TTestContainer(ths_1='foo', ths_2='bar')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix=ths_fix, ths_random='bar')
        scared.TTestContainer(ths_1=ths_1, ths_2='bar')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix='foo', ths_random=ths_random)
        scared.TTestContainer(ths_1='foo', ths_2=ths_2)


def test_ttest_container_raises_exception_if_incorrect_frame_provided(ths_fix, ths_random):
def test_ttest_container_raises_exception_if_incorrect_frame_provided(ths_1, ths_2):
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix=ths_fix, ths_random=ths_random, frame='foo')
        scared.TTestContainer(ths_1=ths_1, ths_2=ths_2, frame='foo')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, frame=2121.1)
        scared.TTestContainer(ths_1, ths_2, frame=2121.1)
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, frame={})
        scared.TTestContainer(ths_1, ths_2, frame={})


def test_container_raises_error_if_bad_preprocesses(ths_fix, ths_random):
def test_container_raises_error_if_bad_preprocesses(ths_1, ths_2):
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, preprocesses='foo')
        scared.TTestContainer(ths_1, ths_2, preprocesses='foo')
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, preprocesses=['foo', 123])
        scared.TTestContainer(ths_1, ths_2, preprocesses=['foo', 123])
    with pytest.raises(TypeError):
        scared.TTestContainer(ths_fix, ths_random, preprocesses=134)
        scared.TTestContainer(ths_1, ths_2, preprocesses=134)


def test_ttest_analysis_raises_exception_if_invalid_precision_is_passed():
@@ -55,7 +55,7 @@ def test_ttest_analysis_raises_exception_if_invalid_precision_is_passed():
        scared.TTestAnalysis(precision='int8')


def test_ttest_analysis_initialize_two_ttest_accumulator(ths_fix, ths_random):
def test_ttest_analysis_initialize_two_ttest_accumulator(ths_1, ths_2):
    analysis = scared.TTestAnalysis()
    assert analysis.accumulators is not None
    assert len(analysis.accumulators) == 2
@@ -71,15 +71,15 @@ def test_accumulator_update_raises_exception_if_invalid_traces():
        accu.update('foo')


def test_ttest_accumulator_update_its_accumulator(ths_fix):
def test_ttest_accumulator_update_its_accumulator(ths_1):
    accu = scared.TTestAccumulator(precision='float32')
    accu.update(ths_fix.samples[:100, :])
    traces = ths_fix.samples[:100, :].astype('float32')
    accu.update(ths_1.samples[:100, :])
    traces = ths_1.samples[:100, :].astype('float32')
    assert np.array_equal(accu.sum, np.sum(traces, axis=0))
    assert np.array_equal(accu.sum_squared, np.sum(traces ** 2, axis=0))
    assert accu.processed_traces == 100
    accu.update(ths_fix.samples[100:151, :])
    traces = ths_fix.samples[:151, :].astype('float32')
    accu.update(ths_1.samples[100:151, :])
    traces = ths_1.samples[:151, :].astype('float32')
    assert np.array_equal(accu.sum, np.sum(traces, axis=0))
    assert np.array_equal(accu.sum_squared, np.sum(traces ** 2, axis=0))
    assert accu.processed_traces == 151
@@ -91,10 +91,10 @@ def test_ttest_compute_raise_exception_if_no_trace_are_processed_or_state_not_in
        d.compute()


def test_ttest_compute(ths_fix, ths_random):
def test_ttest_compute(ths_1, ths_2):
    accu = scared.TTestAccumulator(precision='float32')
    accu.update(ths_fix.samples[:100, :])
    traces = ths_fix.samples[:100, :].astype('float32')
    accu.update(ths_1.samples[:100, :])
    traces = ths_1.samples[:100, :].astype('float32')
    assert np.array_equal(accu.sum, np.sum(traces, axis=0))
    assert np.array_equal(accu.sum_squared, np.sum(traces ** 2, axis=0))
    assert accu.processed_traces == 100
@@ -104,18 +104,18 @@ def test_ttest_compute(ths_fix, ths_random):
    assert np.array_equal(accu.var, np.sum(traces ** 2, axis=0) / 100 - (np.sum(traces, axis=0) / 100) ** 2)


def test_ttest_analysis_run(ths_fix, ths_random):
    cont = scared.TTestContainer(ths_fix, ths_random)
def test_ttest_analysis_run(ths_1, ths_2):
    cont = scared.TTestContainer(ths_1, ths_2)
    analysis = scared.TTestAnalysis(precision='float64')
    analysis.run(cont)

    t_1 = ths_fix.samples[:].astype('float64')
    t_2 = ths_random.samples[:].astype('float64')
    t_1 = ths_1.samples[:].astype('float64')
    t_2 = ths_2.samples[:].astype('float64')

    mean_1 = np.sum(t_1, axis=0) / len(ths_fix)
    mean_2 = np.sum(t_2, axis=0) / len(ths_random)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_fix) - mean_1 ** 2) / len(ths_fix)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_random) - mean_2 ** 2) / len(ths_random)
    mean_1 = np.sum(t_1, axis=0) / len(ths_1)
    mean_2 = np.sum(t_2, axis=0) / len(ths_2)
    var_1 = (np.sum(t_1 ** 2, axis=0) / len(ths_1) - mean_1 ** 2) / len(ths_1)
    var_2 = (np.sum(t_2 ** 2, axis=0) / len(ths_2) - mean_2 ** 2) / len(ths_2)
    expected = (mean_1 - mean_2) / np.sqrt(var_1 + var_2)

    assert analysis.result is not None