Loading scared/__init__.py +1 −1 Original line number Diff line number Diff line Loading @@ -5,7 +5,7 @@ import logging import estraces as traces # noqa: F401 from .selection_functions.base import selection_function, attack_selection_function, reverse_selection_function # noqa: F401 from .selection_functions.base import selection_function, attack_selection_function, reverse_selection_function, SelectionFunctionError # noqa: F401 from .models import HammingWeight, Value, Monobit, Model # noqa: F401 from .discriminants import discriminant, nanmax, maxabs, opposite_min, nansum, abssum # noqa: F401 from .distinguishers import ( # noqa: F401 Loading scared/selection_functions/base.py +9 −5 Original line number Diff line number Diff line Loading @@ -6,6 +6,10 @@ from .._utils import _is_bytes_array logger = logging.getLogger(__name__) class SelectionFunctionError(Exception): pass class SelectionFunction: """Base class for selection function used by analysis framework for intermediate value computation. Loading Loading @@ -40,18 +44,18 @@ class SelectionFunction: try: self._base_kwargs[name] = kwargs[name] self._ref_shape = kwargs[name].shape except KeyError: if arg.default is None: logger.info(f'Arg {name} has no value for this selection function.') except KeyError as e: if name not in self._base_kwargs: raise SelectionFunctionError(f'Missing values in metadata {list(kwargs.keys())} for expected argument {e} of selection function {self}.') values = self._function(**self._base_kwargs) if values.shape[0] != self._ref_shape[0]: raise ValueError(f'Shape of selection function output should begin with {self._ref_shape[0]}, not {values.shape[0]}.') raise SelectionFunctionError(f'Shape of selection function output should begin with {self._ref_shape[0]}, not {values.shape[0]}.') if self.words is not None: try: values = values.swapaxes(0, -1)[self.words].swapaxes(0, -1) except IndexError: raise ValueError(f'Words selection {self.words} can\'t be applied for this selection function with shape {values.shape}.') raise SelectionFunctionError(f'Words selection {self.words} can\'t be applied for this selection function with shape {values.shape}.') return values Loading tests/test_selection_function.py +9 −9 Original line number Diff line number Diff line Loading @@ -45,19 +45,19 @@ def test_selection_function_call_raises_exception_if_missing_kwargs(): @scared.selection_function def sf(plain): return plain with pytest.raises(TypeError): with pytest.raises(scared.SelectionFunctionError): sf(keys=np.random.randint(0, 255, (16), dtype='uint8')) @scared.attack_selection_function def sf(plain, guesses): return plain with pytest.raises(TypeError): with pytest.raises(scared.SelectionFunctionError): sf(keys=np.random.randint(0, 255, (16), dtype='uint8')) @scared.reverse_selection_function def sf(plain, guesses): return plain with pytest.raises(TypeError): with pytest.raises(scared.SelectionFunctionError): sf(keys=np.random.randint(0, 255, (16), dtype='uint8')) Loading Loading @@ -108,7 +108,7 @@ def test_selection_function_raises_exceptions_if_intermediate_values_has_inconsi out[guess] = np.bitwise_xor(plaintext, guess) return out[:, 0] with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(**datas) @scared.selection_function Loading @@ -117,7 +117,7 @@ def test_selection_function_raises_exceptions_if_intermediate_values_has_inconsi out[0] = np.bitwise_xor(plaintext, 0) return out[:, 0] with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(**datas) @scared.reverse_selection_function Loading @@ -126,7 +126,7 @@ def test_selection_function_raises_exceptions_if_intermediate_values_has_inconsi out[0] = np.bitwise_xor(plaintext, 0) return out[:, 0] with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(**datas) Loading Loading @@ -202,7 +202,7 @@ def test_selection_function_compute_raises_exception_if_words_selection_is_incon for guess in guesses: out[guess] = np.bitwise_xor(plaintext, guess) return out.swapaxes(0, 1) with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(plaintext=np.random.randint(0, 255, (200, 16), dtype='uint8')) @scared.selection_function(words=18) Loading @@ -210,7 +210,7 @@ def test_selection_function_compute_raises_exception_if_words_selection_is_incon out = np.empty(tuple([1, plaintext.shape[0], 16]), dtype='uint8') out[0] = np.bitwise_xor(plaintext, 0) return out.swapaxes(0, 1) with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(plaintext=np.random.randint(0, 255, (200, 16), dtype='uint8')) @scared.reverse_selection_function(words=18) Loading @@ -218,5 +218,5 @@ def test_selection_function_compute_raises_exception_if_words_selection_is_incon out = np.empty(tuple([1, plaintext.shape[0], 16]), dtype='uint8') out[0] = np.bitwise_xor(plaintext, 0) return out.swapaxes(0, 1) with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(plaintext=np.random.randint(0, 255, (200, 16), dtype='uint8')) Loading
scared/__init__.py +1 −1 Original line number Diff line number Diff line Loading @@ -5,7 +5,7 @@ import logging import estraces as traces # noqa: F401 from .selection_functions.base import selection_function, attack_selection_function, reverse_selection_function # noqa: F401 from .selection_functions.base import selection_function, attack_selection_function, reverse_selection_function, SelectionFunctionError # noqa: F401 from .models import HammingWeight, Value, Monobit, Model # noqa: F401 from .discriminants import discriminant, nanmax, maxabs, opposite_min, nansum, abssum # noqa: F401 from .distinguishers import ( # noqa: F401 Loading
scared/selection_functions/base.py +9 −5 Original line number Diff line number Diff line Loading @@ -6,6 +6,10 @@ from .._utils import _is_bytes_array logger = logging.getLogger(__name__) class SelectionFunctionError(Exception): pass class SelectionFunction: """Base class for selection function used by analysis framework for intermediate value computation. Loading Loading @@ -40,18 +44,18 @@ class SelectionFunction: try: self._base_kwargs[name] = kwargs[name] self._ref_shape = kwargs[name].shape except KeyError: if arg.default is None: logger.info(f'Arg {name} has no value for this selection function.') except KeyError as e: if name not in self._base_kwargs: raise SelectionFunctionError(f'Missing values in metadata {list(kwargs.keys())} for expected argument {e} of selection function {self}.') values = self._function(**self._base_kwargs) if values.shape[0] != self._ref_shape[0]: raise ValueError(f'Shape of selection function output should begin with {self._ref_shape[0]}, not {values.shape[0]}.') raise SelectionFunctionError(f'Shape of selection function output should begin with {self._ref_shape[0]}, not {values.shape[0]}.') if self.words is not None: try: values = values.swapaxes(0, -1)[self.words].swapaxes(0, -1) except IndexError: raise ValueError(f'Words selection {self.words} can\'t be applied for this selection function with shape {values.shape}.') raise SelectionFunctionError(f'Words selection {self.words} can\'t be applied for this selection function with shape {values.shape}.') return values Loading
tests/test_selection_function.py +9 −9 Original line number Diff line number Diff line Loading @@ -45,19 +45,19 @@ def test_selection_function_call_raises_exception_if_missing_kwargs(): @scared.selection_function def sf(plain): return plain with pytest.raises(TypeError): with pytest.raises(scared.SelectionFunctionError): sf(keys=np.random.randint(0, 255, (16), dtype='uint8')) @scared.attack_selection_function def sf(plain, guesses): return plain with pytest.raises(TypeError): with pytest.raises(scared.SelectionFunctionError): sf(keys=np.random.randint(0, 255, (16), dtype='uint8')) @scared.reverse_selection_function def sf(plain, guesses): return plain with pytest.raises(TypeError): with pytest.raises(scared.SelectionFunctionError): sf(keys=np.random.randint(0, 255, (16), dtype='uint8')) Loading Loading @@ -108,7 +108,7 @@ def test_selection_function_raises_exceptions_if_intermediate_values_has_inconsi out[guess] = np.bitwise_xor(plaintext, guess) return out[:, 0] with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(**datas) @scared.selection_function Loading @@ -117,7 +117,7 @@ def test_selection_function_raises_exceptions_if_intermediate_values_has_inconsi out[0] = np.bitwise_xor(plaintext, 0) return out[:, 0] with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(**datas) @scared.reverse_selection_function Loading @@ -126,7 +126,7 @@ def test_selection_function_raises_exceptions_if_intermediate_values_has_inconsi out[0] = np.bitwise_xor(plaintext, 0) return out[:, 0] with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(**datas) Loading Loading @@ -202,7 +202,7 @@ def test_selection_function_compute_raises_exception_if_words_selection_is_incon for guess in guesses: out[guess] = np.bitwise_xor(plaintext, guess) return out.swapaxes(0, 1) with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(plaintext=np.random.randint(0, 255, (200, 16), dtype='uint8')) @scared.selection_function(words=18) Loading @@ -210,7 +210,7 @@ def test_selection_function_compute_raises_exception_if_words_selection_is_incon out = np.empty(tuple([1, plaintext.shape[0], 16]), dtype='uint8') out[0] = np.bitwise_xor(plaintext, 0) return out.swapaxes(0, 1) with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(plaintext=np.random.randint(0, 255, (200, 16), dtype='uint8')) @scared.reverse_selection_function(words=18) Loading @@ -218,5 +218,5 @@ def test_selection_function_compute_raises_exception_if_words_selection_is_incon out = np.empty(tuple([1, plaintext.shape[0], 16]), dtype='uint8') out[0] = np.bitwise_xor(plaintext, 0) return out.swapaxes(0, 1) with pytest.raises(ValueError): with pytest.raises(scared.SelectionFunctionError): sf(plaintext=np.random.randint(0, 255, (200, 16), dtype='uint8'))