diff options
| author | J08nY | 2020-07-10 18:54:15 +0200 |
|---|---|---|
| committer | J08nY | 2020-07-10 18:54:15 +0200 |
| commit | fcdea31b2a6efb49357ef8b2e69212ec412feaa0 (patch) | |
| tree | 3a986c2bb7010bfdb59a9062de3cd78de8dc4b51 /pyecsca/ec | |
| parent | a10e4c4339c11d5ff6b438569e3af96c1beae3e5 (diff) | |
| download | pyecsca-fcdea31b2a6efb49357ef8b2e69212ec412feaa0.tar.gz pyecsca-fcdea31b2a6efb49357ef8b2e69212ec412feaa0.tar.zst pyecsca-fcdea31b2a6efb49357ef8b2e69212ec412feaa0.zip | |
Fix modified coords to_model transformation.
Diffstat (limited to 'pyecsca/ec')
| -rw-r--r-- | pyecsca/ec/params.py | 2 | ||||
| -rw-r--r-- | pyecsca/ec/point.py | 49 |
2 files changed, 31 insertions, 20 deletions
diff --git a/pyecsca/ec/params.py b/pyecsca/ec/params.py index 9f1865f..abe716a 100644 --- a/pyecsca/ec/params.py +++ b/pyecsca/ec/params.py @@ -143,7 +143,7 @@ def get_params(category: str, name: str, coords: str, infty: bool = True) -> Dom affine = Point(AffineCoordinateModel(model), x=Mod(int(curve["generator"]["x"], 16), field), y=Mod(int(curve["generator"]["y"], 16), field)) if not isinstance(coord_model, AffineCoordinateModel): - generator = Point.from_affine(coord_model, affine) + generator = affine.to_model(coord_model, elliptic_curve) else: generator = affine return DomainParameters(elliptic_curve, generator, order, cofactor, name, category) diff --git a/pyecsca/ec/point.py b/pyecsca/ec/point.py index 4484626..2f9c29a 100644 --- a/pyecsca/ec/point.py +++ b/pyecsca/ec/point.py @@ -51,7 +51,7 @@ class Point(object): with CoordinateMappingAction(self.coordinate_model, affine_model, self) as action: if isinstance(self.coordinate_model, AffineCoordinateModel): return action.exit(copy(self)) - ops = list() + ops = [] for s in self.coordinate_model.satisfying: try: ops.append(CodeOp(s)) @@ -61,36 +61,48 @@ class Point(object): if not result_variables.issuperset(affine_model.variables): raise NotImplementedError result = {} - locals = {**self.coords} + locls = {**self.coords} for op in ops: try: - locals[op.result] = op(**locals) + locls[op.result] = op(**locls) except NameError as e: if op.result in affine_model.variables: raise e else: continue if op.result in affine_model.variables: - result[op.result] = locals[op.result] + result[op.result] = locls[op.result] return action.exit(Point(affine_model, **result)) - @staticmethod - def from_affine(coordinate_model: CoordinateModel, affine_point: "Point") -> "Point": + def to_model(self, coordinate_model: CoordinateModel, curve: "EllipticCurve") -> "Point": """Convert an affine point into a given coordinate model, if possible.""" - with CoordinateMappingAction(affine_point.coordinate_model, coordinate_model, affine_point) as action: - if not isinstance(affine_point.coordinate_model, AffineCoordinateModel): - raise ValueError + if not isinstance(self.coordinate_model, AffineCoordinateModel): + raise ValueError + with CoordinateMappingAction(self.coordinate_model, coordinate_model, self) as action: + ops = [] + for s in coordinate_model.satisfying: + try: + ops.append(CodeOp(s)) + except Exception: + pass + locls = {**self.coords, **curve.parameters, "Z": Mod(1, curve.prime)} + for op in ops: + try: + locls[op.result] = op(**locls) + except: + continue result = {} - n = affine_point.coords["x"].n - for var in coordinate_model.variables: # XXX: This just works for the stuff currently in EFD. - if var == "X": - result[var] = affine_point.coords["x"] + for var in coordinate_model.variables: + if var in locls: # Try this first. + result[var] = locls[var] + elif var == "X": # XXX: This just works for the stuff currently in EFD. + result[var] = self.coords["x"] elif var == "Y": - result[var] = affine_point.coords["y"] + result[var] = self.coords["y"] elif var.startswith("Z"): - result[var] = Mod(1, n) + result[var] = Mod(1, curve.prime) elif var == "T": - result[var] = Mod(int(affine_point.coords["x"] * affine_point.coords["y"]), n) + result[var] = Mod(int(affine_point.coords["x"] * affine_point.coords["y"]), curve.prime) else: raise NotImplementedError return action.exit(Point(coordinate_model, **result)) @@ -138,9 +150,8 @@ class InfinityPoint(Point): def to_affine(self) -> "InfinityPoint": return InfinityPoint(AffineCoordinateModel(self.coordinate_model.curve_model)) - @staticmethod - def from_affine(coordinate_model: CoordinateModel, affine_point: "Point") -> "InfinityPoint": - raise NotImplementedError + def to_model(self, coordinate_model: CoordinateModel, curve: "EllipticCurve") -> "InfinityPoint": + return InfinityPoint(coordinate_model) def equals(self, other) -> bool: return self == other |
