aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca
diff options
context:
space:
mode:
authorJ08nY2023-12-22 11:21:34 +0100
committerJ08nY2023-12-22 11:29:09 +0100
commit7fe43202fc4a16a643f0ad45f291b3119aa36f8b (patch)
treec21a930f0ac752cb4f39d78f55524dc6d2468676 /pyecsca
parente40b359454a3728fe38a28612fb2ac31979506ef (diff)
downloadpyecsca-7fe43202fc4a16a643f0ad45f291b3119aa36f8b.tar.gz
pyecsca-7fe43202fc4a16a643f0ad45f291b3119aa36f8b.tar.zst
pyecsca-7fe43202fc4a16a643f0ad45f291b3119aa36f8b.zip
Add fake addition formulas.
Diffstat (limited to 'pyecsca')
-rw-r--r--pyecsca/ec/formula/base.py38
-rw-r--r--pyecsca/ec/formula/efd.py27
-rw-r--r--pyecsca/ec/formula/fake.py87
-rw-r--r--pyecsca/sca/re/zvp.py2
4 files changed, 111 insertions, 43 deletions
diff --git a/pyecsca/ec/formula/base.py b/pyecsca/ec/formula/base.py
index 733a82d..b059822 100644
--- a/pyecsca/ec/formula/base.py
+++ b/pyecsca/ec/formula/base.py
@@ -2,6 +2,7 @@
from abc import ABC, abstractmethod
from ast import Expression
from functools import cached_property
+from itertools import product
from astunparse import unparse
from typing import List, Set, Any, ClassVar, MutableMapping, Tuple, Union, Dict
@@ -287,29 +288,36 @@ class Formula(ABC):
state["assumptions"] = list(map(peval, state["assumptions"]))
self.__dict__.update(state)
- @property
- @abstractmethod
+ @cached_property
def input_index(self):
"""Return the starting index where this formula reads its inputs."""
- raise NotImplementedError
+ return 1
- @property
- @abstractmethod
- def output_index(self) -> int:
+ @cached_property
+ def output_index(self):
"""Return the starting index where this formula stores its outputs."""
- raise NotImplementedError
+ return max(self.num_inputs + 1, 3)
- @property
- @abstractmethod
- def inputs(self) -> Set[str]:
+ @cached_property
+ def inputs(self):
"""Return the input variables of the formula."""
- raise NotImplementedError
+ return {
+ var + str(i)
+ for var, i in product(
+ self.coordinate_model.variables, range(1, 1 + self.num_inputs)
+ )
+ }
- @property
- @abstractmethod
- def outputs(self) -> Set[str]:
+ @cached_property
+ def outputs(self):
"""Return the output variables of the formula."""
- raise NotImplementedError
+ return {
+ var + str(i)
+ for var, i in product(
+ self.coordinate_model.variables,
+ range(self.output_index, self.output_index + self.num_outputs),
+ )
+ }
@property
def num_operations(self) -> int:
diff --git a/pyecsca/ec/formula/efd.py b/pyecsca/ec/formula/efd.py
index 0156fb3..4e3a82d 100644
--- a/pyecsca/ec/formula/efd.py
+++ b/pyecsca/ec/formula/efd.py
@@ -69,33 +69,6 @@ class EFDFormula(Formula):
def __str__(self):
return f"{self.coordinate_model!s}/{self.name}"
- @cached_property
- def input_index(self):
- return 1
-
- @cached_property
- def output_index(self):
- return max(self.num_inputs + 1, 3)
-
- @cached_property
- def inputs(self):
- return {
- var + str(i)
- for var, i in product(
- self.coordinate_model.variables, range(1, 1 + self.num_inputs)
- )
- }
-
- @cached_property
- def outputs(self):
- return {
- var + str(i)
- for var, i in product(
- self.coordinate_model.variables,
- range(self.output_index, self.output_index + self.num_outputs),
- )
- }
-
def __eq__(self, other):
if not isinstance(other, EFDFormula):
return False
diff --git a/pyecsca/ec/formula/fake.py b/pyecsca/ec/formula/fake.py
new file mode 100644
index 0000000..837e2d8
--- /dev/null
+++ b/pyecsca/ec/formula/fake.py
@@ -0,0 +1,87 @@
+from abc import ABC
+from typing import Any, Tuple
+
+from public import public
+
+from .base import (
+ AdditionFormula,
+ FormulaAction,
+ Formula,
+ DoublingFormula,
+ TriplingFormula,
+ LadderFormula,
+ NegationFormula,
+ ScalingFormula,
+ DifferentialAdditionFormula,
+)
+from ..mod import Mod, Undefined
+from ..point import Point
+
+
+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 MultipleContext).
+ """
+ def __init__(self, coordinate_model):
+ self.coordinate_model = coordinate_model
+ self.code = []
+
+ 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))
+
+
+@public
+class FakeAdditionFormula(FakeFormula, AdditionFormula):
+ name = "fake"
+
+
+@public
+class FakeDoublingFormula(FakeFormula, DoublingFormula):
+ name = "fake"
+
+
+@public
+class FakeTriplingFormula(FakeFormula, TriplingFormula):
+ name = "fake"
+
+
+@public
+class FakeNegationFormula(FakeFormula, NegationFormula):
+ name = "fake"
+
+
+@public
+class FakeScalingFormula(FakeFormula, ScalingFormula):
+ name = "fake"
+
+
+@public
+class FakeDifferentialAdditionFormula(FakeFormula, DifferentialAdditionFormula):
+ name = "fake"
+
+
+@public
+class FakeLadderFormula(FakeFormula, LadderFormula):
+ name = "fake"
+
+
+@public
+class FakePoint(Point):
+ def __init__(self, model):
+ coords = {key: Undefined() for key in model.variables}
+ super().__init__(model, **coords)
+
+ def __str__(self):
+ return "FakePoint"
+
+ def __repr__(self):
+ return str(self)
diff --git a/pyecsca/sca/re/zvp.py b/pyecsca/sca/re/zvp.py
index 4e12549..c71ace8 100644
--- a/pyecsca/sca/re/zvp.py
+++ b/pyecsca/sca/re/zvp.py
@@ -3,7 +3,7 @@ Provides functionality inspired by the Zero-value point attack [ZVP]_.
Implements ZVP point construction from [FFD]_.
"""
-from typing import List, Set, Tuple, Dict, Union
+from typing import List, Set, Tuple, Dict
from public import public
from astunparse import unparse