diff options
| -rw-r--r-- | pyecsca/ec/curves.py | 99 | ||||
| -rw-r--r-- | pyecsca/ec/params.py | 96 | ||||
| -rw-r--r-- | pyecsca/sca/scope/picoscope_sdk.py | 2 | ||||
| -rw-r--r-- | test/ec/test_context.py | 2 | ||||
| -rw-r--r-- | test/ec/test_curve.py | 2 | ||||
| -rw-r--r-- | test/ec/test_curves.py | 2 | ||||
| -rw-r--r-- | test/ec/test_formula.py | 2 | ||||
| -rw-r--r-- | test/ec/test_key_agreement.py | 2 | ||||
| -rw-r--r-- | test/ec/test_key_generation.py | 2 | ||||
| -rw-r--r-- | test/ec/test_mult.py | 2 | ||||
| -rw-r--r-- | test/ec/test_params.py | 2 | ||||
| -rw-r--r-- | test/ec/test_point.py | 2 | ||||
| -rw-r--r-- | test/ec/test_signature.py | 2 |
13 files changed, 106 insertions, 111 deletions
diff --git a/pyecsca/ec/curves.py b/pyecsca/ec/curves.py deleted file mode 100644 index aa5e146..0000000 --- a/pyecsca/ec/curves.py +++ /dev/null @@ -1,99 +0,0 @@ -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 - -from .coordinates import AffineCoordinateModel -from .curve import EllipticCurve -from .mod import Mod -from .model import (ShortWeierstrassModel, MontgomeryModel, TwistedEdwardsModel, - EdwardsModel, CurveModel) -from .params import DomainParameters -from .point import Point, InfinityPoint - - -@public -def get_params(category: str, name: str, coords: str, infty: bool = True) -> DomainParameters: - """ - Retrieve a curve from a set of stored parameters. Uses the std-curves database at - https://github.com/J08nY/std-curves. - - :param category: The category of the curve. - :param name: The name of the curve. - :param coords: The name of the coordinate system to use. - :param infty: Whether to use the special :py:class:InfinityPoint (`True`) or try to use the - point at infinity of the coordinate system. - :return: The curve. - """ - listing = resource_listdir(__name__, "std") - categories = list(entry for entry in listing if resource_isdir(__name__, join("std", entry))) - if category not in categories: - raise ValueError("Category {} not found.".format(category)) - json_path = join("std", category, "curves.json") - with resource_stream(__name__, json_path) as f: - category_json = json.load(f) - for curve in category_json["curves"]: - if curve["name"] == name: - break - else: - raise ValueError("Curve {} not found in category {}.".format(name, category)) - 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) - cofactor = int(curve["cofactor"], 16) - if curve["form"] == "Weierstrass": - model = ShortWeierstrassModel() - param_names = ["a", "b"] - elif curve["form"] == "Montgomery": - model = MontgomeryModel() - param_names = ["a", "b"] - elif curve["form"] == "Edwards": - model = EdwardsModel() - param_names = ["c", "d"] - elif curve["form"] == "TwistedEdwards": - model = TwistedEdwardsModel() - 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: - alocals: Dict[str, Union[Mod, int]] = {} - compiled = compile(assumption, "", mode="exec") - 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: - ilocals: Dict[str, Union[Mod, int]] = {**params} - for line in coord_model.neutral: - compiled = compile(line, "", mode="exec") - exec(compiled, None, ilocals) - infinity_coords = {} - for coordinate in coord_model.variables: - if coordinate not in ilocals: - raise ValueError(f"Coordinate model {coord_model} requires infty option.") - 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), - y=Mod(int(curve["generator"]["y"], 16), field)) - generator = Point.from_affine(coord_model, affine) - return DomainParameters(elliptic_curve, generator, order, cofactor, name, category) diff --git a/pyecsca/ec/params.py b/pyecsca/ec/params.py index 2b0538e..7cf36a3 100644 --- a/pyecsca/ec/params.py +++ b/pyecsca/ec/params.py @@ -1,9 +1,16 @@ -from typing import Optional +import json +from os.path import join +from typing import Optional, Dict, Union +from pkg_resources import resource_listdir, resource_isdir, resource_stream from public import public +from .coordinates import AffineCoordinateModel from .curve import EllipticCurve -from .point import Point +from .mod import Mod +from .model import (CurveModel, ShortWeierstrassModel, MontgomeryModel, EdwardsModel, + TwistedEdwardsModel) +from .point import Point, InfinityPoint @public @@ -47,3 +54,88 @@ class DomainParameters(object): def __repr__(self): return f"{self.__class__.__name__}({self.curve!r}, {self.generator!r}, {self.order}, {self.cofactor})" + + +@public +def get_params(category: str, name: str, coords: str, infty: bool = True) -> DomainParameters: + """ + Retrieve a curve from a set of stored parameters. Uses the std-curves database at + https://github.com/J08nY/std-curves. + + :param category: The category of the curve. + :param name: The name of the curve. + :param coords: The name of the coordinate system to use. + :param infty: Whether to use the special :py:class:InfinityPoint (`True`) or try to use the + point at infinity of the coordinate system. + :return: The curve. + """ + listing = resource_listdir(__name__, "std") + categories = list(entry for entry in listing if resource_isdir(__name__, join("std", entry))) + if category not in categories: + raise ValueError("Category {} not found.".format(category)) + json_path = join("std", category, "curves.json") + with resource_stream(__name__, json_path) as f: + category_json = json.load(f) + for curve in category_json["curves"]: + if curve["name"] == name: + break + else: + raise ValueError("Curve {} not found in category {}.".format(name, category)) + 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) + cofactor = int(curve["cofactor"], 16) + if curve["form"] == "Weierstrass": + model = ShortWeierstrassModel() + param_names = ["a", "b"] + elif curve["form"] == "Montgomery": + model = MontgomeryModel() + param_names = ["a", "b"] + elif curve["form"] == "Edwards": + model = EdwardsModel() + param_names = ["c", "d"] + elif curve["form"] == "TwistedEdwards": + model = TwistedEdwardsModel() + 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: + alocals: Dict[str, Union[Mod, int]] = {} + compiled = compile(assumption, "", mode="exec") + 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: + ilocals: Dict[str, Union[Mod, int]] = {**params} + for line in coord_model.neutral: + compiled = compile(line, "", mode="exec") + exec(compiled, None, ilocals) + infinity_coords = {} + for coordinate in coord_model.variables: + if coordinate not in ilocals: + raise ValueError(f"Coordinate model {coord_model} requires infty option.") + 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), + y=Mod(int(curve["generator"]["y"], 16), field)) + generator = Point.from_affine(coord_model, affine) + return DomainParameters(elliptic_curve, generator, order, cofactor, name, category)
\ No newline at end of file diff --git a/pyecsca/sca/scope/picoscope_sdk.py b/pyecsca/sca/scope/picoscope_sdk.py index 014680c..ac5c349 100644 --- a/pyecsca/sca/scope/picoscope_sdk.py +++ b/pyecsca/sca/scope/picoscope_sdk.py @@ -19,6 +19,8 @@ def adc2volt(adc: Union[np.ndarray, ctypes.c_int16], volt_range: float, adc_minmax: int) -> Union[np.ndarray, float]: # pragma: no cover if isinstance(adc, ctypes.c_int16): adc = adc.value + if isinstance(adc, np.ndarray): + adc = adc.astype(np.dtype("f2")) return (adc / adc_minmax) * volt_range diff --git a/test/ec/test_context.py b/test/ec/test_context.py index 8701046..20fcb7e 100644 --- a/test/ec/test_context.py +++ b/test/ec/test_context.py @@ -2,7 +2,7 @@ from unittest import TestCase from pyecsca.ec.context import (local, DefaultContext, NullContext, getcontext, setcontext, resetcontext, Tree) -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.mod import RandomModAction from pyecsca.ec.mult import LTRMultiplier, ScalarMultiplicationAction diff --git a/test/ec/test_curve.py b/test/ec/test_curve.py index b8eb916..91d9f16 100644 --- a/test/ec/test_curve.py +++ b/test/ec/test_curve.py @@ -1,7 +1,7 @@ from unittest import TestCase from pyecsca.ec.curve import EllipticCurve -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.mod import Mod from pyecsca.ec.model import MontgomeryModel from pyecsca.ec.point import Point, InfinityPoint diff --git a/test/ec/test_curves.py b/test/ec/test_curves.py index fba932a..37d4bd7 100644 --- a/test/ec/test_curves.py +++ b/test/ec/test_curves.py @@ -2,7 +2,7 @@ from unittest import TestCase from parameterized import parameterized -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params class CurvesTests(TestCase): diff --git a/test/ec/test_formula.py b/test/ec/test_formula.py index 6f8037a..c0eed28 100644 --- a/test/ec/test_formula.py +++ b/test/ec/test_formula.py @@ -1,6 +1,6 @@ from unittest import TestCase -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params class FormulaTests(TestCase): diff --git a/test/ec/test_key_agreement.py b/test/ec/test_key_agreement.py index adffbab..cbdb1c8 100644 --- a/test/ec/test_key_agreement.py +++ b/test/ec/test_key_agreement.py @@ -2,7 +2,7 @@ from unittest import TestCase from parameterized import parameterized -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.key_agreement import (ECDH_NONE, ECDH_SHA1, ECDH_SHA224, ECDH_SHA256, ECDH_SHA384, ECDH_SHA512) from pyecsca.ec.mod import Mod diff --git a/test/ec/test_key_generation.py b/test/ec/test_key_generation.py index 59f3b23..f0d926c 100644 --- a/test/ec/test_key_generation.py +++ b/test/ec/test_key_generation.py @@ -1,6 +1,6 @@ from unittest import TestCase -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.key_generation import KeyGeneration from pyecsca.ec.mult import LTRMultiplier diff --git a/test/ec/test_mult.py b/test/ec/test_mult.py index 5d7e66b..a1c6725 100644 --- a/test/ec/test_mult.py +++ b/test/ec/test_mult.py @@ -2,7 +2,7 @@ from unittest import TestCase from parameterized import parameterized -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.mult import (LTRMultiplier, RTLMultiplier, LadderMultiplier, BinaryNAFMultiplier, WindowNAFMultiplier, SimpleLadderMultiplier, DifferentialLadderMultiplier, diff --git a/test/ec/test_params.py b/test/ec/test_params.py index 57cabe3..3dfb0c2 100644 --- a/test/ec/test_params.py +++ b/test/ec/test_params.py @@ -1,6 +1,6 @@ from unittest import TestCase -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params class DomainParameterTests(TestCase): diff --git a/test/ec/test_point.py b/test/ec/test_point.py index a8b9fd7..b0ed051 100644 --- a/test/ec/test_point.py +++ b/test/ec/test_point.py @@ -1,7 +1,7 @@ from unittest import TestCase from pyecsca.ec.coordinates import AffineCoordinateModel -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.mod import Mod from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel from pyecsca.ec.point import Point, InfinityPoint diff --git a/test/ec/test_signature.py b/test/ec/test_signature.py index 125c280..0d3c5a9 100644 --- a/test/ec/test_signature.py +++ b/test/ec/test_signature.py @@ -2,7 +2,7 @@ from unittest import TestCase from parameterized import parameterized -from pyecsca.ec.curves import get_params +from pyecsca.ec.params import get_params from pyecsca.ec.mod import Mod from pyecsca.ec.mult import LTRMultiplier from pyecsca.ec.signature import (Signature, SignatureResult, ECDSA_NONE, ECDSA_SHA1, ECDSA_SHA224, |
