aboutsummaryrefslogtreecommitdiffhomepage
path: root/pyecsca/ec/curves.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--pyecsca/ec/curves.py23
1 files changed, 16 insertions, 7 deletions
diff --git a/pyecsca/ec/curves.py b/pyecsca/ec/curves.py
index bc8329f..aa5e146 100644
--- a/pyecsca/ec/curves.py
+++ b/pyecsca/ec/curves.py
@@ -1,5 +1,6 @@
import json
from os.path import join
+from typing import Dict, Union
from pkg_resources import resource_listdir, resource_isdir, resource_stream
from public import public
@@ -41,6 +42,7 @@ def get_params(category: str, name: str, coords: str, infty: bool = True) -> Dom
if curve["field"]["type"] == "Binary":
raise ValueError("Binary field curves are currently not supported.")
+ # Get model and param names
model: CurveModel
field = int(curve["field"]["p"], 16)
order = int(curve["order"], 16)
@@ -59,29 +61,36 @@ def get_params(category: str, name: str, coords: str, infty: bool = True) -> Dom
param_names = ["a", "d"]
else:
raise ValueError("Unknown curve model.")
+
+ # Check coordinate model name and assumptions
if coords not in model.coordinates:
raise ValueError("Coordinate model not supported for curve.")
coord_model = model.coordinates[coords]
params = {name: Mod(int(curve["params"][name], 16), field) for name in param_names}
for assumption in coord_model.assumptions:
- locals = {}
+ alocals: Dict[str, Union[Mod, int]] = {}
compiled = compile(assumption, "", mode="exec")
- exec(compiled, None, locals)
- for param, value in locals.items():
+ exec(compiled, None, alocals)
+ for param, value in alocals.items():
if params[param] != value:
raise ValueError(f"Coordinate model {coord_model} has an unsatisifed assumption on the {param} parameter (= {value}).")
+ # Construct the point at infinity
+ infinity: Point
if infty:
infinity = InfinityPoint(coord_model)
else:
- locals = {**params}
+ ilocals: Dict[str, Union[Mod, int]] = {**params}
for line in coord_model.neutral:
compiled = compile(line, "", mode="exec")
- exec(compiled, None, locals)
+ exec(compiled, None, ilocals)
infinity_coords = {}
for coordinate in coord_model.variables:
- if coordinate not in locals:
+ if coordinate not in ilocals:
raise ValueError(f"Coordinate model {coord_model} requires infty option.")
- infinity_coords[coordinate] = Mod(locals[coordinate], field)
+ value = ilocals[coordinate]
+ if isinstance(value, int):
+ value = Mod(value, field)
+ infinity_coords[coordinate] = value
infinity = Point(coord_model, **infinity_coords)
elliptic_curve = EllipticCurve(model, coord_model, field, infinity, params)
affine = Point(AffineCoordinateModel(model), x=Mod(int(curve["generator"]["x"], 16), field),