diff options
| -rw-r--r-- | pyecsca/ec/coordinates.py | 53 | ||||
| -rw-r--r-- | pyecsca/ec/formula.py | 16 | ||||
| -rw-r--r-- | pyecsca/ec/model.py | 23 | ||||
| -rw-r--r-- | pyecsca/ec/params.py | 23 | ||||
| -rw-r--r-- | pyproject.toml | 3 | ||||
| -rw-r--r-- | test/ec/test_divpoly.py | 2 | ||||
| -rw-r--r-- | test/ec/test_params.py | 5 | ||||
| -rw-r--r-- | test/sca/perf_combine.py | 2 | ||||
| -rw-r--r-- | test/sca/test_align.py | 2 | ||||
| -rw-r--r-- | test/sca/test_target.py | 2 | ||||
| -rw-r--r-- | test/sca/test_traceset.py | 2 |
11 files changed, 63 insertions, 70 deletions
diff --git a/pyecsca/ec/coordinates.py b/pyecsca/ec/coordinates.py index 73ffe43..10ea400 100644 --- a/pyecsca/ec/coordinates.py +++ b/pyecsca/ec/coordinates.py @@ -1,9 +1,9 @@ """Provides a coordinate model class.""" from ast import parse, Module -from os.path import join +from importlib_resources.abc import Traversable +from importlib_resources import as_file from typing import List, Any, MutableMapping -from pkg_resources import resource_listdir, resource_isdir, resource_stream from public import public from .formula import ( @@ -71,7 +71,7 @@ class AffineCoordinateModel(CoordinateModel): class EFDCoordinateModel(CoordinateModel): - def __init__(self, dir_path: str, name: str, curve_model: Any): + def __init__(self, dir_path: Traversable, name: str, curve_model: Any): self.name = name self.curve_model = curve_model self.variables = [] @@ -80,31 +80,32 @@ class EFDCoordinateModel(CoordinateModel): self.assumptions = [] self.neutral = [] self.formulas = {} - for fname in resource_listdir(__name__, dir_path): - file_path = join(dir_path, fname) - if resource_isdir(__name__, file_path): - self.__read_formula_dir(file_path, fname) - else: - self.__read_coordinates_file(file_path) + for entry in dir_path.iterdir(): + with as_file(entry) as file_path: + if entry.is_dir(): + self.__read_formula_dir(file_path, file_path.stem) + else: + self.__read_coordinates_file(file_path) - def __read_formula_dir(self, dir_path, formula_type): - for fname in resource_listdir(__name__, dir_path): - if fname.endswith(".op3"): - continue - formula_types = { - "addition": AdditionEFDFormula, - "doubling": DoublingEFDFormula, - "tripling": TriplingEFDFormula, - "diffadd": DifferentialAdditionEFDFormula, - "ladder": LadderEFDFormula, - "scaling": ScalingEFDFormula, - "negation": NegationEFDFormula, - } - cls = formula_types.get(formula_type, EFDFormula) - self.formulas[fname] = cls(join(dir_path, fname), fname, self) + def __read_formula_dir(self, dir_path: Traversable, formula_type): + for entry in dir_path.iterdir(): + with as_file(entry) as fpath: + if fpath.suffix == ".op3": + continue + formula_types = { + "addition": AdditionEFDFormula, + "doubling": DoublingEFDFormula, + "tripling": TriplingEFDFormula, + "diffadd": DifferentialAdditionEFDFormula, + "ladder": LadderEFDFormula, + "scaling": ScalingEFDFormula, + "negation": NegationEFDFormula, + } + cls = formula_types.get(formula_type, EFDFormula) + self.formulas[fpath.stem] = cls(fpath, fpath.with_suffix(".op3"), fpath.stem, self) - def __read_coordinates_file(self, file_path): - with resource_stream(__name__, file_path) as f: + def __read_coordinates_file(self, file_path: Traversable): + with file_path.open("rb") as f: line = f.readline().decode("ascii").rstrip() while line: if line.startswith("name"): diff --git a/pyecsca/ec/formula.py b/pyecsca/ec/formula.py index 7855622..3951c49 100644 --- a/pyecsca/ec/formula.py +++ b/pyecsca/ec/formula.py @@ -7,7 +7,7 @@ from astunparse import unparse from itertools import product from typing import List, Set, Any, ClassVar, MutableMapping, Tuple, Union, Dict -from pkg_resources import resource_stream +from importlib_resources.abc import Traversable from public import public from sympy import sympify, FF, symbols, Poly, Rational @@ -339,7 +339,7 @@ class Formula(ABC): class EFDFormula(Formula): """Formula from the `Explicit-Formulas Database <https://www.hyperelliptic.org/EFD/>`_.""" - def __init__(self, path: str, name: str, coordinate_model: Any): + def __init__(self, meta_path: Traversable, op3_path: Traversable, name: str, coordinate_model: Any): self.name = name self.coordinate_model = coordinate_model self.meta = {} @@ -347,11 +347,11 @@ class EFDFormula(Formula): self.assumptions = [] self.code = [] self.unified = False - self.__read_meta_file(path) - self.__read_op3_file(path + ".op3") + self.__read_meta_file(meta_path) + self.__read_op3_file(op3_path) - def __read_meta_file(self, path): - with resource_stream(__name__, path) as f: + def __read_meta_file(self, path: Traversable): + with path.open("rb") as f: line = f.readline().decode("ascii").rstrip() while line: if line.startswith("source"): @@ -368,8 +368,8 @@ class EFDFormula(Formula): self.unified = True line = f.readline().decode("ascii").rstrip() - def __read_op3_file(self, path): - with resource_stream(__name__, path) as f: + def __read_op3_file(self, path: Traversable): + with path.open("rb") as f: for line in f.readlines(): code_module = parse( line.decode("ascii").replace("^", "**"), path, mode="exec" diff --git a/pyecsca/ec/model.py b/pyecsca/ec/model.py index 55bcb18..7827a51 100644 --- a/pyecsca/ec/model.py +++ b/pyecsca/ec/model.py @@ -1,9 +1,9 @@ """Provides curve model classes for the supported curve models.""" from ast import parse, Expression, Module -from os.path import join from typing import List, MutableMapping +from importlib_resources import files, as_file +from importlib_resources.abc import Traversable -from pkg_resources import resource_listdir, resource_isdir, resource_stream from public import public from .coordinates import EFDCoordinateModel, CoordinateModel @@ -50,19 +50,18 @@ class EFDCurveModel(CurveModel): self.__class__.to_weierstrass = [] self.__class__.from_weierstrass = [] - files = resource_listdir(__name__, join("efd", efd_name)) - for fname in files: - file_path = join("efd", efd_name, fname) - if resource_isdir(__name__, file_path): - self.__read_coordinate_dir(self.__class__, file_path, fname) - else: - self.__read_curve_file(self.__class__, file_path) + for entry in files("pyecsca.ec").joinpath("efd", efd_name).iterdir(): + with as_file(entry) as file_path: + if entry.is_dir(): + self.__read_coordinate_dir(self.__class__, file_path, file_path.stem) + else: + self.__read_curve_file(self.__class__, file_path) - def __read_curve_file(self, cls, file_path): + def __read_curve_file(self, cls, file_path: Traversable): def format_eq(line, mode="exec"): return parse(line.replace("^", "**"), mode=mode) - with resource_stream(__name__, file_path) as f: + with file_path.open("rb") as f: for raw in f.readlines(): line = raw.decode("ascii").rstrip() if line.startswith("name"): @@ -90,7 +89,7 @@ class EFDCurveModel(CurveModel): else: cls.full_weierstrass.append(format_eq(line)) - def __read_coordinate_dir(self, cls, dir_path, name): + def __read_coordinate_dir(self, cls, dir_path: Traversable, name: str): cls.coordinates[name] = EFDCoordinateModel(dir_path, name, self) def __eq__(self, other): diff --git a/pyecsca/ec/params.py b/pyecsca/ec/params.py index 4637abb..4141dfa 100644 --- a/pyecsca/ec/params.py +++ b/pyecsca/ec/params.py @@ -8,11 +8,10 @@ import csv from sympy import Poly, FF, symbols, sympify from astunparse import unparse from io import RawIOBase, BufferedIOBase -from os.path import join from pathlib import Path from typing import Optional, Dict, Union, BinaryIO, List, Callable, IO +from importlib_resources import files -from pkg_resources import resource_listdir, resource_isdir, resource_stream from public import public from .coordinates import AffineCoordinateModel, CoordinateModel @@ -442,14 +441,12 @@ def get_category( as argument the name of the curve and returns the infinity option to use for that curve. :return: The category. """ - listing = resource_listdir(__name__, "std") - categories = [ - entry for entry in listing if resource_isdir(__name__, join("std", entry)) - ] + categories = { + 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.") - json_path = join("std", category, "curves.json") - with resource_stream(__name__, json_path) as f: + with categories[category].joinpath("curves.json").open("rb") as f: return load_category(f, coords, infty) @@ -469,14 +466,12 @@ def get_params( point at infinity of the coordinate system. :return: The curve. """ - listing = resource_listdir(__name__, "std") - categories = [ - entry for entry in listing if resource_isdir(__name__, join("std", entry)) - ] + categories = { + 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.") - json_path = join("std", category, "curves.json") - with resource_stream(__name__, json_path) as f: + with categories[category].joinpath("curves.json").open("rb") as f: category_json = json.load(f) curve = None for curve in category_json["curves"]: diff --git a/pyproject.toml b/pyproject.toml index 12b681c..5093e0f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,8 @@ "xarray", "astunparse", "numba==0.57.1", - "networkx" + "networkx", + "importlib-resources" ] [project.urls] diff --git a/test/ec/test_divpoly.py b/test/ec/test_divpoly.py index aace00b..07eed76 100644 --- a/test/ec/test_divpoly.py +++ b/test/ec/test_divpoly.py @@ -1,5 +1,5 @@ import json -from importlib.resources import files +from importlib_resources import files import test.data.divpoly from sympy import FF diff --git a/test/ec/test_params.py b/test/ec/test_params.py index 5441ed0..ceaef48 100644 --- a/test/ec/test_params.py +++ b/test/ec/test_params.py @@ -1,7 +1,4 @@ -from unittest import TestCase - -from parameterized import parameterized -from importlib.resources import files, as_file +from importlib_resources import files, as_file import pytest diff --git a/test/sca/perf_combine.py b/test/sca/perf_combine.py index 42163c8..b76acb1 100644 --- a/test/sca/perf_combine.py +++ b/test/sca/perf_combine.py @@ -1,7 +1,7 @@ #!/usr/bin/env python import click -from importlib.resources import files, as_file +from importlib_resources import files, as_file from test.utils import Profiler import test.data.sca from pyecsca.sca import ( diff --git a/test/sca/test_align.py b/test/sca/test_align.py index 5820171..db3874e 100644 --- a/test/sca/test_align.py +++ b/test/sca/test_align.py @@ -1,6 +1,6 @@ import numpy as np import pytest -from importlib.resources import files, as_file +from importlib_resources import files, as_file from pyecsca.sca import ( align_correlation, align_peaks, diff --git a/test/sca/test_target.py b/test/sca/test_target.py index fa09eb7..3ca92fb 100644 --- a/test/sca/test_target.py +++ b/test/sca/test_target.py @@ -4,7 +4,7 @@ from copy import copy from os.path import realpath, dirname, join from typing import Optional from unittest import TestCase, SkipTest -from importlib.resources import files, as_file +from importlib_resources import files, as_file from smartcard.pcsc.PCSCExceptions import BaseSCardException import test.data.sca diff --git a/test/sca/test_traceset.py b/test/sca/test_traceset.py index 01fadda..55b9a0a 100644 --- a/test/sca/test_traceset.py +++ b/test/sca/test_traceset.py @@ -1,7 +1,7 @@ import os.path import shutil import tempfile -from importlib.resources import files, as_file +from importlib_resources import files, as_file from unittest import TestCase import numpy as np |
