aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ08nY2020-03-16 19:33:15 +0100
committerJ08nY2020-03-16 19:33:15 +0100
commit9d4d881d6d847b044959b3c080dac1c9488445e8 (patch)
tree312e9b1bc8d5aeb1b89942a9043368bec8b871b2
parentf84a5abc37d5803123ee291d7bc3ba30fd9545e0 (diff)
downloadpyecsca-9d4d881d6d847b044959b3c080dac1c9488445e8.tar.gz
pyecsca-9d4d881d6d847b044959b3c080dac1c9488445e8.tar.bz2
pyecsca-9d4d881d6d847b044959b3c080dac1c9488445e8.zip
Add tests for affine coords.
-rw-r--r--Makefile2
-rw-r--r--pyecsca/ec/curve.py2
-rw-r--r--pyecsca/ec/params.py31
-rw-r--r--test/ec/test_curves.py47
-rw-r--r--test/ec/test_params.py46
5 files changed, 67 insertions, 61 deletions
diff --git a/Makefile b/Makefile
index cc7409c..bd30ab1 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-EC_TESTS = ec.test_context ec.test_configuration ec.test_curve ec.test_curves ec.test_formula \
+EC_TESTS = ec.test_context ec.test_configuration ec.test_curve ec.test_formula \
ec.test_params ec.test_key_agreement ec.test_key_generation ec.test_mod ec.test_model \
ec.test_mult ec.test_naf ec.test_op ec.test_point ec.test_signature
diff --git a/pyecsca/ec/curve.py b/pyecsca/ec/curve.py
index 5a99c92..e8ae66c 100644
--- a/pyecsca/ec/curve.py
+++ b/pyecsca/ec/curve.py
@@ -21,7 +21,7 @@ class EllipticCurve(object):
def __init__(self, model: CurveModel, coordinate_model: CoordinateModel,
prime: int, neutral: Point, parameters: MutableMapping[str, Union[Mod, int]]):
- if coordinate_model not in model.coordinates.values():
+ if coordinate_model not in model.coordinates.values() and not isinstance(coordinate_model, AffineCoordinateModel):
raise ValueError
if set(model.parameter_names).symmetric_difference(parameters.keys()):
raise ValueError
diff --git a/pyecsca/ec/params.py b/pyecsca/ec/params.py
index 7cf36a3..29d431d 100644
--- a/pyecsca/ec/params.py
+++ b/pyecsca/ec/params.py
@@ -103,19 +103,23 @@ def get_params(category: str, name: str, coords: str, infty: bool = True) -> Dom
param_names = ["a", "d"]
else:
raise ValueError("Unknown curve model.")
+ params = {name: Mod(int(curve["params"][name], 16), field) for name in param_names}
# 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}).")
+ if coords == "affine":
+ coord_model = AffineCoordinateModel(model)
+ else:
+ if coords not in model.coordinates:
+ raise ValueError("Coordinate model not supported for curve.")
+ coord_model = model.coordinates[coords]
+ 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:
@@ -137,5 +141,8 @@ def get_params(category: str, name: str, coords: str, infty: bool = True) -> Dom
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)
+ if not isinstance(coord_model, AffineCoordinateModel):
+ generator = Point.from_affine(coord_model, affine)
+ else:
+ generator = affine
return DomainParameters(elliptic_curve, generator, order, cofactor, name, category) \ No newline at end of file
diff --git a/test/ec/test_curves.py b/test/ec/test_curves.py
deleted file mode 100644
index 37d4bd7..0000000
--- a/test/ec/test_curves.py
+++ /dev/null
@@ -1,47 +0,0 @@
-from unittest import TestCase
-
-from parameterized import parameterized
-
-from pyecsca.ec.params import get_params
-
-
-class CurvesTests(TestCase):
-
- @parameterized.expand([
- ("secg/secp128r1", "projective"),
- ("secg/secp256r1", "projective"),
- ("secg/secp521r1", "projective"),
- ("other/Curve25519", "xz"),
- ("other/Ed25519", "projective"),
- ("other/Ed448", "projective"),
- ("other/E-222", "projective")
- ])
- def test_get_params(self, name, coords):
- params = get_params(*name.split("/"), coords)
- try:
- assert params.curve.is_on_curve(params.generator)
- except NotImplementedError:
- pass
-
- @parameterized.expand([
- ("no_category/some", "else"),
- ("secg/no_curve", "else"),
- ("secg/secp128r1", "some")
- ])
- def test_unknown(self, name, coords):
- with self.assertRaises(ValueError):
- get_params(*name.split("/"), coords)
-
- def test_assumption(self):
- with self.assertRaises(ValueError):
- get_params("secg", "secp128r1", "projective-1")
- self.assertIsNotNone(get_params("secg", "secp128r1", "projective-3"))
-
- def test_infty(self):
- with self.assertRaises(ValueError):
- get_params("secg", "secp128r1", "modified", False)
- self.assertIsNotNone(get_params("secg", "secp128r1", "projective", False))
-
- def test_no_binary(self):
- with self.assertRaises(ValueError):
- get_params("secg", "sect163r1", "something") \ No newline at end of file
diff --git a/test/ec/test_params.py b/test/ec/test_params.py
index 3dfb0c2..9de813b 100644
--- a/test/ec/test_params.py
+++ b/test/ec/test_params.py
@@ -1,5 +1,8 @@
from unittest import TestCase
+from parameterized import parameterized
+
+from pyecsca.ec.coordinates import AffineCoordinateModel
from pyecsca.ec.params import get_params
@@ -16,3 +19,46 @@ class DomainParameterTests(TestCase):
def test_str(self):
self.assertEqual(str(self.secp128r1), "DomainParameters(secg/secp128r1)")
+
+ @parameterized.expand([
+ ("secg/secp128r1", "projective"),
+ ("secg/secp256r1", "projective"),
+ ("secg/secp521r1", "projective"),
+ ("other/Curve25519", "xz"),
+ ("other/Ed25519", "projective"),
+ ("other/Ed448", "projective"),
+ ("other/E-222", "projective")
+ ])
+ def test_get_params(self, name, coords):
+ params = get_params(*name.split("/"), coords)
+ try:
+ assert params.curve.is_on_curve(params.generator)
+ except NotImplementedError:
+ pass
+
+ @parameterized.expand([
+ ("no_category/some", "else"),
+ ("secg/no_curve", "else"),
+ ("secg/secp128r1", "some")
+ ])
+ def test_unknown(self, name, coords):
+ with self.assertRaises(ValueError):
+ get_params(*name.split("/"), coords)
+
+ def test_assumption(self):
+ with self.assertRaises(ValueError):
+ get_params("secg", "secp128r1", "projective-1")
+ self.assertIsNotNone(get_params("secg", "secp128r1", "projective-3"))
+
+ def test_infty(self):
+ with self.assertRaises(ValueError):
+ get_params("secg", "secp128r1", "modified", False)
+ self.assertIsNotNone(get_params("secg", "secp128r1", "projective", False))
+
+ def test_no_binary(self):
+ with self.assertRaises(ValueError):
+ get_params("secg", "sect163r1", "something")
+
+ def test_affine(self):
+ aff = get_params("secg", "secp128r1", "affine")
+ self.assertIsInstance(aff.curve.coordinate_model, AffineCoordinateModel) \ No newline at end of file