aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/ec/curve.py
diff options
context:
space:
mode:
Diffstat (limited to 'pyecsca/ec/curve.py')
-rw-r--r--pyecsca/ec/curve.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/pyecsca/ec/curve.py b/pyecsca/ec/curve.py
index b161f7b..b9a4770 100644
--- a/pyecsca/ec/curve.py
+++ b/pyecsca/ec/curve.py
@@ -1,8 +1,9 @@
-from typing import MutableMapping, Union
+from ast import Module
+from typing import MutableMapping, Union, List
from public import public
-from .coordinates import CoordinateModel
+from .coordinates import CoordinateModel, AffineCoordinateModel
from .mod import Mod
from .model import CurveModel
from .point import Point
@@ -33,6 +34,32 @@ class EllipticCurve(object):
value = Mod(value, prime)
self.parameters[name] = value
+ def _execute_base_formulas(self, formulas: List[Module], *points: Point) -> Point:
+ for point in points:
+ if point.coordinate_model.curve_model != self.model:
+ raise ValueError
+ if not isinstance(point.coordinate_model, AffineCoordinateModel):
+ raise ValueError
+ locals = {var + str(i + 1): point.coords[var]
+ for i, point in enumerate(points) for var in point.coords}
+ locals.update(self.parameters)
+ for line in formulas:
+ exec(compile(line, "", mode="exec"), None, locals)
+ return Point(AffineCoordinateModel(self), x=locals["x"], y=locals["y"])
+
+ def affine_add(self, one: Point, other: Point) -> Point:
+ return self._execute_base_formulas(self.model.base_addition, one, other)
+
+ def affine_double(self, one: Point) -> Point:
+ return self._execute_base_formulas(self.model.base_doubling, one)
+
+ def affine_negate(self, one: Point) -> Point:
+ return self._execute_base_formulas(self.model.base_negation, one)
+
+ @property
+ def neutral_is_affine(self):
+ return bool(self.model.base_neutral)
+
def is_on_curve(self, point: Point) -> bool:
if point.coordinate_model.curve_model != self.model:
return False