aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pyecsca/ec/context.py6
-rw-r--r--pyecsca/sca/re/rpa.py8
-rw-r--r--test/ec/test_context.py16
-rw-r--r--test/sca/test_rpa.py8
4 files changed, 24 insertions, 14 deletions
diff --git a/pyecsca/ec/context.py b/pyecsca/ec/context.py
index de6e202..b58db2a 100644
--- a/pyecsca/ec/context.py
+++ b/pyecsca/ec/context.py
@@ -10,6 +10,7 @@ multiplication actions has as its children an ordered list of the individual for
A :py:class:`PathContext` works like a :py:class:`DefaultContext` that only traces an action on a particular path
in the tree.
"""
+
from abc import abstractmethod, ABC
from copy import deepcopy
from typing import List, Optional, ContextManager, Any, Sequence, Callable
@@ -249,7 +250,9 @@ class DefaultContext(Context):
self.actions.append(Node(action))
else:
if self.current:
- root = next(filter(lambda node: node.action == self.current[0], self.actions))
+ root = next(
+ filter(lambda node: node.action == self.current[0], self.actions)
+ )
Node(action, parent=root.get_by_key(self.current[1:]))
else:
self.actions.append(Node(action))
@@ -310,6 +313,7 @@ current: Optional[Context] = None
class _ContextManager:
old_context: Optional[Context]
new_context: Optional[Context]
+
def __init__(self, new_context: Optional[Context] = None, copy: bool = True):
if copy:
if new_context is not None:
diff --git a/pyecsca/sca/re/rpa.py b/pyecsca/sca/re/rpa.py
index 395f698..b8ed88c 100644
--- a/pyecsca/sca/re/rpa.py
+++ b/pyecsca/sca/re/rpa.py
@@ -1,6 +1,7 @@
"""
Provides functionality inspired by the Refined-Power Analysis attack by Goubin [RPA]_.
"""
+
from copy import copy, deepcopy
from public import public
@@ -21,7 +22,8 @@ from pyecsca.ec.formula import (
TriplingFormula,
NegationFormula,
DifferentialAdditionFormula,
- LadderFormula, )
+ LadderFormula,
+)
from pyecsca.ec.mod import Mod, mod
from pyecsca.ec.mult import (
ScalarMultiplicationAction,
@@ -390,7 +392,7 @@ def multiples_computed(
mult_class: Type[ScalarMultiplier],
mult_factory: Callable,
use_init: bool = False,
- use_multiply: bool = True
+ use_multiply: bool = True,
) -> set[int]:
"""
Compute the multiples computed for a given scalar and multiplier (quickly).
@@ -419,5 +421,3 @@ def multiples_computed(
mult.multiply(scalar)
return set(ctx.points.values()) - {0}
-
-
diff --git a/test/ec/test_context.py b/test/ec/test_context.py
index f76995b..e82f852 100644
--- a/test/ec/test_context.py
+++ b/test/ec/test_context.py
@@ -1,11 +1,6 @@
import pytest
-from pyecsca.ec.context import (
- local,
- DefaultContext,
- Node,
- PathContext, Action
-)
+from pyecsca.ec.context import local, DefaultContext, Node, PathContext, Action
from pyecsca.ec.key_generation import KeyGeneration
from pyecsca.ec.mod import RandomModAction
from pyecsca.ec.mult import LTRMultiplier, ScalarMultiplicationAction
@@ -51,10 +46,13 @@ def test_render():
Node(other_a, parent=a)
txt = tree.render()
- assert txt == """Action()
+ assert (
+ txt
+ == """Action()
└──Action()
├──Action()
└──Action()"""
+ )
@pytest.fixture()
@@ -89,6 +87,7 @@ def test_default_no_enter():
with local(DefaultContext()) as default, pytest.raises(ValueError):
default.exit_action(RandomModAction(7))
+
def test_multiple_enter(mult):
default = DefaultContext()
with local(default) as ctx1:
@@ -100,6 +99,7 @@ def test_multiple_enter(mult):
assert len(default.actions) == 0
assert len(ctx1.actions) == len(ctx2.actions)
+
def test_multiple_enter_chained(mult):
default = DefaultContext()
with local(default) as ctx1:
@@ -111,6 +111,7 @@ def test_multiple_enter_chained(mult):
assert len(default.actions) == 0
assert 2 * len(ctx1.actions) == len(ctx2.actions)
+
def test_multiple_enter_no_copy(mult):
default = DefaultContext()
with local(default, copy=False) as ctx1:
@@ -122,6 +123,7 @@ def test_multiple_enter_no_copy(mult):
assert len(default.actions) == len(ctx1.actions)
assert len(ctx1.actions) == len(ctx2.actions)
+
def test_path(mult, secp128r1):
with local(PathContext([0, 1])) as ctx:
key_generator = KeyGeneration(mult, secp128r1, True)
diff --git a/test/sca/test_rpa.py b/test/sca/test_rpa.py
index 0d2860b..b17c6d4 100644
--- a/test/sca/test_rpa.py
+++ b/test/sca/test_rpa.py
@@ -28,7 +28,8 @@ from pyecsca.sca.re.rpa import (
MultipleContext,
rpa_point_0y,
rpa_point_x0,
- rpa_distinguish, multiples_computed,
+ rpa_distinguish,
+ multiples_computed,
)
@@ -72,11 +73,14 @@ def rpa_params(model, coords):
def test_multiples(rpa_params):
- multiples = multiples_computed(17, rpa_params, LTRMultiplier, LTRMultiplier, True, True)
+ multiples = multiples_computed(
+ 17, rpa_params, LTRMultiplier, LTRMultiplier, True, True
+ )
assert 1 in multiples
assert 17 in multiples
assert 0 not in multiples
+
def test_x0_point(rpa_params):
res = rpa_point_x0(rpa_params)
assert res is not None