diff options
| author | J08nY | 2019-04-23 23:24:55 +0200 |
|---|---|---|
| committer | J08nY | 2019-04-23 23:24:55 +0200 |
| commit | f4bcb085cfc9ddac71fe8bb82e8f6719309b2637 (patch) | |
| tree | b07b30112fc2661f4fb5da7bfda30975e83077f9 | |
| parent | 28390ec1575e0af026be2bfea6fd0bca8f55c008 (diff) | |
| download | pyecsca-f4bcb085cfc9ddac71fe8bb82e8f6719309b2637.tar.gz pyecsca-f4bcb085cfc9ddac71fe8bb82e8f6719309b2637.tar.zst pyecsca-f4bcb085cfc9ddac71fe8bb82e8f6719309b2637.zip | |
| -rw-r--r-- | .coveragerc | 7 | ||||
| -rw-r--r-- | pyecsca/ec/context.py | 9 | ||||
| -rw-r--r-- | test/ec/test_context.py | 38 |
3 files changed, 47 insertions, 7 deletions
diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..542050b --- /dev/null +++ b/.coveragerc @@ -0,0 +1,7 @@ +[report] +exclude_lines = + pragma: no cover + raise AssertionError + raise NotImplementedError + if 0: + if __name__ == .__main__.:
\ No newline at end of file diff --git a/pyecsca/ec/context.py b/pyecsca/ec/context.py index 0c21942..48dcc1d 100644 --- a/pyecsca/ec/context.py +++ b/pyecsca/ec/context.py @@ -72,6 +72,9 @@ class Action(object): self.roots[k] = self.intermediates[k] self.output_points.append(point) + def __repr__(self): + return f"Action({self.formula}, {self.input_points}) = {self.output_points}" + @public class Context(object): @@ -86,11 +89,11 @@ class Context(object): def _execute(self, formula: Formula, *points: Point, **params: Mod) -> Tuple[Point, ...]: if len(points) != formula.num_inputs: - raise ValueError + raise ValueError(f"Wrong number of inputs for {formula}.") coords = {} for i, point in enumerate(points): if point.coordinate_model != formula.coordinate_model: - raise ValueError + raise ValueError(f"Wrong coordinate model of point {point}.") for coord, value in point.coords.items(): coords[coord + str(i + 1)] = value locals = {**coords, **params} @@ -106,8 +109,6 @@ class Context(object): full_resulting = {} for variable in formula.coordinate_model.variables: full_variable = variable + ind - if full_variable not in locals: - continue resulting[variable] = locals[full_variable] full_resulting[full_variable] = locals[full_variable] point = Point(formula.coordinate_model, **resulting) diff --git a/test/ec/test_context.py b/test/ec/test_context.py index 51fefd7..9e56afb 100644 --- a/test/ec/test_context.py +++ b/test/ec/test_context.py @@ -1,8 +1,21 @@ +import ast from unittest import TestCase -from pyecsca.ec.context import local, DefaultContext +from pyecsca.ec.context import local, DefaultContext, OpResult, NullContext, getcontext +from pyecsca.ec.coordinates import AffineCoordinateModel +from pyecsca.ec.mod import Mod from pyecsca.ec.mult import LTRMultiplier -from test.ec.curves import get_secp128r1 +from pyecsca.ec.point import Point +from .curves import get_secp128r1 + + +class OpResultTests(TestCase): + + def test_repr(self): + for op, char in zip((ast.Add(), ast.Sub(), ast.Mult(), ast.Div()), "+-*/"): + res = OpResult("X1", Mod(0, 5), op, Mod(2, 5), Mod(3, 5)) + self.assertEqual(str(res), "X1") + self.assertEqual(repr(res), "X1 = 2{}3".format(char)) class ContextTests(TestCase): @@ -15,9 +28,28 @@ class ContextTests(TestCase): self.coords.formulas["dbl-1998-cmo"], self.coords.formulas["z"]) def test_null(self): - self.mult.multiply(59, self.base) + with local() as ctx: + self.mult.multiply(59, self.base) + self.assertIsInstance(ctx, NullContext) def test_default(self): with local(DefaultContext()) as ctx: self.mult.multiply(59, self.base) self.assertEqual(len(ctx.actions), 10) + + def test_execute(self): + with self.assertRaises(ValueError): + getcontext().execute(self.coords.formulas["z"], self.base, self.base) + with self.assertRaises(ValueError): + getcontext().execute(self.coords.formulas["z"], + Point(AffineCoordinateModel(self.secp128r1.curve.model), + x=Mod(1, 5), y=Mod(2, 5))) + + def test_repr(self): + with local(DefaultContext()) as default: + self.mult.multiply(59, self.base) + str(default) + str(default.actions) + with local(NullContext()) as null: + self.mult.multiply(59, self.base) + str(null) |
