diff options
| author | J08nY | 2024-05-16 13:53:30 +0200 |
|---|---|---|
| committer | J08nY | 2024-05-16 13:53:30 +0200 |
| commit | b8fb8dfab8230ebd879f445fc9fda8938f61a8e2 (patch) | |
| tree | 784237cf791816057ab6ff9cb50b4301022a5bf2 /pyecsca | |
| parent | 0ec63f1be2be96de4e103b0120626ff750fb70e2 (diff) | |
| download | pyecsca-b8fb8dfab8230ebd879f445fc9fda8938f61a8e2.tar.gz pyecsca-b8fb8dfab8230ebd879f445fc9fda8938f61a8e2.tar.zst pyecsca-b8fb8dfab8230ebd879f445fc9fda8938f61a8e2.zip | |
Add check for mis-install with missing std an efd submodules.
Diffstat (limited to 'pyecsca')
| -rw-r--r-- | pyecsca/ec/model.py | 9 | ||||
| -rw-r--r-- | pyecsca/ec/params.py | 125 |
2 files changed, 65 insertions, 69 deletions
diff --git a/pyecsca/ec/model.py b/pyecsca/ec/model.py index df80e76..b4f62d3 100644 --- a/pyecsca/ec/model.py +++ b/pyecsca/ec/model.py @@ -176,3 +176,12 @@ class TwistedEdwardsModel(EFDCurveModel): def __init__(self): super().__init__("twisted") + + +if not ShortWeierstrassModel.coordinates: + import warnings + + warnings.warn( + "EFD not available, pyecsca is mis-installed. " + "Make sure that you check out the git submodules prior to install (when installing from git)." + ) diff --git a/pyecsca/ec/params.py b/pyecsca/ec/params.py index db4ed2e..abcac46 100644 --- a/pyecsca/ec/params.py +++ b/pyecsca/ec/params.py @@ -42,13 +42,13 @@ class DomainParameters: category: Optional[str] def __init__( - self, - curve: EllipticCurve, - generator: Point, - order: int, - cofactor: int, - name: Optional[str] = None, - category: Optional[str] = None, + self, + curve: EllipticCurve, + generator: Point, + order: int, + cofactor: int, + name: Optional[str] = None, + category: Optional[str] = None, ): self.curve = curve self.generator = generator @@ -61,10 +61,10 @@ class DomainParameters: if not isinstance(other, DomainParameters): return False return ( - self.curve == other.curve - and self.generator == other.generator - and self.order == other.order - and self.cofactor == other.cofactor + self.curve == other.curve + and self.generator == other.generator + and self.order == other.order + and self.cofactor == other.cofactor ) def __hash__(self): @@ -81,7 +81,9 @@ class DomainParameters: raise ValueError curve = self.curve.to_coords(coordinate_model) generator = self.generator.to_model(coordinate_model, curve) - return DomainParameters(curve, generator, self.order, self.cofactor, self.name, self.category) + return DomainParameters( + curve, generator, self.order, self.cofactor, self.name, self.category + ) def to_affine(self) -> "DomainParameters": """ @@ -91,7 +93,9 @@ class DomainParameters: """ curve = self.curve.to_affine() generator = self.generator.to_affine() - return DomainParameters(curve, generator, self.order, self.cofactor, self.name, self.category) + return DomainParameters( + curve, generator, self.order, self.cofactor, self.name, self.category + ) def __get_name(self): if self.name and self.category: @@ -200,9 +204,9 @@ def _create_params(curve, coords, infty): for curve_param, value in params.items(): expr = expr.subs(curve_param, k(value)) if ( - len(expr.free_symbols) > 1 - or (param := str(expr.free_symbols.pop())) - not in coord_model.parameters + len(expr.free_symbols) > 1 + or (param := str(expr.free_symbols.pop())) + not in coord_model.parameters ): raise ValueError( f"This coordinate model couldn't be loaded due to an unsupported assumption ({assumption_string})." @@ -215,7 +219,7 @@ def _create_params(curve, coords, infty): else: raise_unsatisified_assumption( getconfig().ec.unsatisfied_coordinate_assumption_action, - f"Coordinate model {coord_model} has an unsatisifed assumption on the {param} parameter (0 = {expr} mod {field})." + f"Coordinate model {coord_model} has an unsatisifed assumption on the {param} parameter (0 = {expr} mod {field}).", ) # Construct the point at infinity @@ -257,9 +261,9 @@ def _create_params(curve, coords, infty): @public def load_category( - file: Union[str, Path, BinaryIO, IO[bytes]], - coords: Union[str, Callable[[str], str]], - infty: Union[bool, Callable[[str], bool]] = True, + file: Union[str, Path, BinaryIO, IO[bytes]], + coords: Union[str, Callable[[str], str]], + infty: Union[bool, Callable[[str], bool]] = True, ) -> DomainParameterCategory: """ Load a category of domain parameters containing several curves from a JSON file. @@ -295,7 +299,7 @@ def load_category( @public def load_params( - file: Union[str, Path, BinaryIO], coords: str, infty: bool = True + file: Union[str, Path, BinaryIO], coords: str, infty: bool = True ) -> DomainParameters: """ Load a curve from a JSON file. @@ -319,7 +323,7 @@ def load_params( @public def load_params_ecgen( - file: Union[str, Path, BinaryIO], coords: str, infty: bool = True + file: Union[str, Path, BinaryIO], coords: str, infty: bool = True ) -> DomainParameters: """ Load a curve from a file that is output of `ecgen <https://github.com/J08nY/ecgen>`_. @@ -344,37 +348,23 @@ def load_params_ecgen( raise ValueError("Can not represent curve with two subgroups.") curve_dict = { "form": "Weierstrass", - "field": { - "type": "Prime", - "p": ecgen["field"]["p"] - }, + "field": {"type": "Prime", "p": ecgen["field"]["p"]}, "order": ecgen["subgroups"][0]["order"], # Take just the first subgroup "cofactor": ecgen["subgroups"][0]["cofactor"], - "params": { - "a": { - "raw": ecgen["a"] - }, - "b": { - "raw": ecgen["b"] - } - }, + "params": {"a": {"raw": ecgen["a"]}, "b": {"raw": ecgen["b"]}}, "generator": { - "x": { - "raw": ecgen["subgroups"][0]["x"] - }, - "y": { - "raw": ecgen["subgroups"][0]["y"] - } + "x": {"raw": ecgen["subgroups"][0]["x"]}, + "y": {"raw": ecgen["subgroups"][0]["y"]}, }, "name": None, - "category": None + "category": None, } return _create_params(curve_dict, coords, infty) @public def load_params_ectester( - file: Union[str, Path, BinaryIO], coords: str, infty: bool = True + file: Union[str, Path, BinaryIO], coords: str, infty: bool = True ) -> DomainParameters: """ Load a curve from a file that uses the format of `ECTester <https://github.com/crocs-muni/ECTester>`_. @@ -399,39 +389,22 @@ def load_params_ectester( # p,a,b,gx,gy,n,h (all in hex) curve_dict = { "form": "Weierstrass", - "field": { - "type": "Prime", - "p": line[0] - }, + "field": {"type": "Prime", "p": line[0]}, "order": line[5], "cofactor": line[6], - "params": { - "a": { - "raw": line[1] - }, - "b": { - "raw": line[2] - } - }, - "generator": { - "x": { - "raw": line[3] - }, - "y": { - "raw": line[4] - } - }, + "params": {"a": {"raw": line[1]}, "b": {"raw": line[2]}}, + "generator": {"x": {"raw": line[3]}, "y": {"raw": line[4]}}, "name": None, - "category": None + "category": None, } return _create_params(curve_dict, coords, infty) @public def get_category( - category: str, - coords: Union[str, Callable[[str], str]], - infty: Union[bool, Callable[[str], bool]] = True, + category: str, + coords: Union[str, Callable[[str], str]], + infty: Union[bool, Callable[[str], bool]] = True, ) -> DomainParameterCategory: """ Retrieve a category from the std-curves database at https://github.com/J08nY/std-curves. @@ -445,7 +418,9 @@ def get_category( :return: The category. """ categories = { - entry.name: entry for entry in files("pyecsca.ec").joinpath("std").iterdir() if entry.is_dir() + entry.name: entry + for entry in files("pyecsca.ec").joinpath("std").iterdir() + if entry.is_dir() } if category not in categories: raise ValueError(f"Category {category} not found.") @@ -455,7 +430,7 @@ def get_category( @public def get_params( - category: str, name: str, coords: str, infty: bool = True + category: str, name: str, coords: str, infty: bool = True ) -> DomainParameters: """ Retrieve a curve from a set of stored parameters. @@ -470,7 +445,9 @@ def get_params( :return: The curve. """ categories = { - entry.name: entry for entry in files("pyecsca.ec").joinpath("std").iterdir() if entry.is_dir() + entry.name: entry + for entry in files("pyecsca.ec").joinpath("std").iterdir() + if entry.is_dir() } if category not in categories: raise ValueError(f"Category {category} not found.") @@ -484,3 +461,13 @@ def get_params( raise ValueError(f"Curve {name} not found in category {category}.") return _create_params(curve, coords, infty) + + +_dirs = list(files("pyecsca.ec").joinpath("std").iterdir()) +if not _dirs: + import warnings + + warnings.warn( + "std-curves repository is not available. pyecsca is mis-installed. " + "Make sure that you check out the git submodules prior to install (when installing from git)." + ) |
