diff options
| author | J08nY | 2025-06-19 15:20:50 +0200 |
|---|---|---|
| committer | J08nY | 2025-06-19 15:20:50 +0200 |
| commit | e355d741135d22a8fcfe620d6961e71aa6c38b31 (patch) | |
| tree | baeb5a82ca6a23c5cc32fd0ed23f42defaa2ddb3 | |
| parent | 248a3aefa3616b11bcb4d9b613328d69b2d8e076 (diff) | |
| download | pyecsca-e355d741135d22a8fcfe620d6961e71aa6c38b31.tar.gz pyecsca-e355d741135d22a8fcfe620d6961e71aa6c38b31.tar.zst pyecsca-e355d741135d22a8fcfe620d6961e71aa6c38b31.zip | |
| -rw-r--r-- | pyecsca/ec/formula/fake.py | 20 | ||||
| -rw-r--r-- | pyecsca/sca/re/rpa.py | 15 |
2 files changed, 21 insertions, 14 deletions
diff --git a/pyecsca/ec/formula/fake.py b/pyecsca/ec/formula/fake.py index a07c5ed..9ea7934 100644 --- a/pyecsca/ec/formula/fake.py +++ b/pyecsca/ec/formula/fake.py @@ -2,6 +2,7 @@ from abc import ABC from typing import Any, Tuple +from functools import cache from public import public @@ -26,7 +27,7 @@ class FakeFormula(Formula, ABC): No matter what the input point is, it just returns the right amount of FakePoints. Useful for computing with the scalar multipliers without having concrete formulas - and points (for example to get the addition chain via the :py:class:`~.MultipleContext`). + and points (for example, to get the addition chain via the :py:class:`~.MultipleContext`). """ def __init__(self, coordinate_model): @@ -36,12 +37,10 @@ class FakeFormula(Formula, ABC): def __call__(self, field: int, *points: Any, **params: Mod) -> Tuple[Any, ...]: with FormulaAction(self, *points, **params) as action: - result = [] for i in range(self.num_outputs): res = FakePoint(self.coordinate_model) action.output_points.append(res) - result.append(res) - return action.exit(tuple(result)) + return action.exit(tuple(action.output_points)) @public @@ -79,13 +78,20 @@ class FakeLadderFormula(FakeFormula, LadderFormula): name = "fake" +@cache +def _fake_coords(model): + """Return a dictionary of fake coordinates for the given model.""" + return {key: Undefined() for key in model.variables} + + @public class FakePoint(Point): """Just a fake point.""" - def __init__(self, model): - coords = {key: Undefined() for key in model.variables} - super().__init__(model, **coords) + def __init__(self, model): # noqa: We initialize everything here + self.coords = _fake_coords(model) + self.coordinate_model = model + self.field = 0 def __str__(self): return f"FakePoint{id(self)}" diff --git a/pyecsca/sca/re/rpa.py b/pyecsca/sca/re/rpa.py index dbfb844..adef84d 100644 --- a/pyecsca/sca/re/rpa.py +++ b/pyecsca/sca/re/rpa.py @@ -112,37 +112,38 @@ class MultipleContext(Context): self.precomp.update(action.result) if isinstance(action, FormulaAction) and self.inside: action = cast(FormulaAction, action) - if isinstance(action.formula, DoublingFormula): + shortname = action.formula.shortname + if shortname == "dbl": inp = action.input_points[0] out = action.output_points[0] self.points[out] = 2 * self.points[inp] self.parents[out] = [inp] self.formulas[out] = action.formula.shortname - elif isinstance(action.formula, TriplingFormula): + elif shortname == "tpl": inp = action.input_points[0] out = action.output_points[0] self.points[out] = 3 * self.points[inp] self.parents[out] = [inp] self.formulas[out] = action.formula.shortname - elif isinstance(action.formula, AdditionFormula): + elif shortname == "add": one, other = action.input_points out = action.output_points[0] self.points[out] = self.points[one] + self.points[other] self.parents[out] = [one, other] self.formulas[out] = action.formula.shortname - elif isinstance(action.formula, NegationFormula): + elif shortname == "neg": inp = action.input_points[0] out = action.output_points[0] self.points[out] = -self.points[inp] self.parents[out] = [inp] self.formulas[out] = action.formula.shortname - elif isinstance(action.formula, DifferentialAdditionFormula): + elif shortname == "dadd": _, one, other = action.input_points out = action.output_points[0] self.points[out] = self.points[one] + self.points[other] self.parents[out] = [one, other] self.formulas[out] = action.formula.shortname - elif isinstance(action.formula, LadderFormula): + elif shortname == "ladd": _, one, other = action.input_points dbl, add = action.output_points self.points[add] = self.points[one] + self.points[other] @@ -151,7 +152,7 @@ class MultipleContext(Context): self.points[dbl] = 2 * self.points[one] self.parents[dbl] = [one] self.formulas[dbl] = action.formula.shortname - elif isinstance(action.formula, ScalingFormula): + elif shortname == "scl": inp = action.input_points[0] out = action.output_points[0] self.points[out] = self.points[inp] |
