diff options
Diffstat (limited to 'pyecsca/ec')
| -rw-r--r-- | pyecsca/ec/context.py | 21 | ||||
| -rw-r--r-- | pyecsca/ec/mult/comb.py | 2 | ||||
| -rw-r--r-- | pyecsca/ec/mult/naf.py | 3 | ||||
| -rw-r--r-- | pyecsca/ec/mult/window.py | 6 |
4 files changed, 28 insertions, 4 deletions
diff --git a/pyecsca/ec/context.py b/pyecsca/ec/context.py index b58db2a..33c961d 100644 --- a/pyecsca/ec/context.py +++ b/pyecsca/ec/context.py @@ -352,3 +352,24 @@ def local(ctx: Optional[Context] = None, copy: bool = True) -> ContextManager: :return: A context manager. """ return _ContextManager(ctx, copy) + + +@public +def compound(*contexts: Context) -> Context: + """ + Create a context that compounds multiple contexts. + + :param contexts: The contexts to compound. + :return: The compounded context. + """ + + class CompoundContext(Context): + def enter_action(self, action: Action) -> None: + for ctx in contexts: + ctx.enter_action(action) + + def exit_action(self, action: Action) -> None: + for ctx in contexts: + ctx.exit_action(action) + + return CompoundContext() diff --git a/pyecsca/ec/mult/comb.py b/pyecsca/ec/mult/comb.py index d24f557..ae6d0c1 100644 --- a/pyecsca/ec/mult/comb.py +++ b/pyecsca/ec/mult/comb.py @@ -198,7 +198,7 @@ class CombMultiplier(AccumulatorMultiplier, PrecompMultiplier, ScalarMultiplier) current_point = point for i in range(self.width): base_points[i] = current_point - if i != d - 1: + if i != self.width - 1: for _ in range(d): current_point = self._dbl(current_point) self._points = {} diff --git a/pyecsca/ec/mult/naf.py b/pyecsca/ec/mult/naf.py index 1d77056..4c6e0c2 100644 --- a/pyecsca/ec/mult/naf.py +++ b/pyecsca/ec/mult/naf.py @@ -270,7 +270,8 @@ class WindowNAFMultiplier(AccumulatorMultiplier, PrecompMultiplier, ScalarMultip self._points[2 * i + 1] = current_point if self.precompute_negation: self._points_neg[2 * i + 1] = self._neg(current_point) - current_point = self._add(current_point, double_point) + if i != 2 ** (self.width - 2) - 1: + current_point = self._add(current_point, double_point) result = {**self._points} if self.precompute_negation: result.update({-k: v for k, v in self._points_neg.items()}) diff --git a/pyecsca/ec/mult/window.py b/pyecsca/ec/mult/window.py index ed0cd05..e46179a 100644 --- a/pyecsca/ec/mult/window.py +++ b/pyecsca/ec/mult/window.py @@ -104,7 +104,8 @@ class SlidingWindowMultiplier( double_point = self._dbl(point) for i in range(0, 2 ** (self.width - 1)): self._points[2 * i + 1] = current_point - current_point = self._add(current_point, double_point) + if i != 2 ** (self.width - 1) - 1: + current_point = self._add(current_point, double_point) action.exit(self._points) def multiply(self, scalar: int) -> Point: @@ -232,7 +233,8 @@ class FixedWindowLTRMultiplier( converted = convert_base(scalar, self.m) q = copy(self._params.curve.neutral) for digit in reversed(converted): - q = self._mult_m(q) + if q != self._params.curve.neutral: + q = self._mult_m(q) if digit != 0: q = self._accumulate(q, self._points[digit]) if "scl" in self.formulas: |
