aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/ec/curves.py26
-rw-r--r--test/ec/test_coordinates.py7
-rw-r--r--test/ec/test_curve.py20
-rw-r--r--test/ec/test_mult.py36
4 files changed, 63 insertions, 26 deletions
diff --git a/test/ec/curves.py b/test/ec/curves.py
new file mode 100644
index 0000000..d36e702
--- /dev/null
+++ b/test/ec/curves.py
@@ -0,0 +1,26 @@
+from pyecsca.ec.curve import EllipticCurve
+from pyecsca.ec.mod import Mod
+from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel
+from pyecsca.ec.point import InfinityPoint, Point
+
+
+def get_secp128r1():
+ prime = 0xfffffffdffffffffffffffffffffffff
+ model = ShortWeierstrassModel()
+ coords = ShortWeierstrassModel().coordinates["projective"]
+ return (EllipticCurve(model, coords, prime, dict(a=0xfffffffdfffffffffffffffffffffffc,
+ b=0xe87579c11079f43dd824993c2cee5ed3),
+ InfinityPoint(coords)),
+ Point(coords, X=Mod(0x161ff7528b899b2d0c28607ca52c5b86, prime),
+ Y=Mod(0xcf5ac8395bafeb13c02da292dded7a83, prime),
+ Z=Mod(1, prime)))
+
+
+def get_curve25519():
+ prime = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed
+ model = MontgomeryModel()
+ coords = model.coordinates["xz"]
+ return (EllipticCurve(model, coords, prime,
+ dict(a=486662, b=1),
+ InfinityPoint(coords)),
+ Point(coords, X=Mod(9, prime), Z=Mod(1, prime)))
diff --git a/test/ec/test_coordinates.py b/test/ec/test_coordinates.py
index 7532308..31f3edf 100644
--- a/test/ec/test_coordinates.py
+++ b/test/ec/test_coordinates.py
@@ -3,7 +3,7 @@ from unittest import TestCase
from pyecsca.ec.curve import EllipticCurve
from pyecsca.ec.mod import Mod
from pyecsca.ec.model import ShortWeierstrassModel
-from pyecsca.ec.point import Point
+from pyecsca.ec.point import Point, InfinityPoint
class CoordinateTests(TestCase):
@@ -11,11 +11,10 @@ class CoordinateTests(TestCase):
def setUp(self):
self.p = 0xfffffffdffffffffffffffffffffffff
self.coords = ShortWeierstrassModel().coordinates["projective"]
- self.secp128r1 = EllipticCurve(ShortWeierstrassModel(), self.coords,
+ self.secp128r1 = EllipticCurve(ShortWeierstrassModel(), self.coords, self.p,
dict(a=0xfffffffdfffffffffffffffffffffffc,
b=0xe87579c11079f43dd824993c2cee5ed3),
- Point(self.coords, X=Mod(0, self.p), Y=Mod(1, self.p),
- Z=Mod(0, self.p)))
+ InfinityPoint(self.coords))
def test_affine(self):
pt = Point(self.coords, X=Mod(0x161ff7528b899b2d0c28607ca52c5b86, self.p),
diff --git a/test/ec/test_curve.py b/test/ec/test_curve.py
new file mode 100644
index 0000000..7e8e7eb
--- /dev/null
+++ b/test/ec/test_curve.py
@@ -0,0 +1,20 @@
+from unittest import TestCase
+
+from pyecsca.ec.mod import Mod
+from pyecsca.ec.point import Point, InfinityPoint
+from test.ec.curves import get_secp128r1
+
+
+class CurveTests(TestCase):
+ def setUp(self):
+ self.secp128r1, self.base = get_secp128r1()
+
+ def test_is_on_curve(self):
+ pt = Point(self.secp128r1.coordinate_model,
+ X=Mod(0x161ff7528b899b2d0c28607ca52c5b86, self.secp128r1.prime),
+ Y=Mod(0xcf5ac8395bafeb13c02da292dded7a83, self.secp128r1.prime),
+ Z=Mod(1, self.secp128r1.prime))
+ assert self.secp128r1.is_on_curve(pt)
+
+ def test_is_neutral(self):
+ assert self.secp128r1.is_neutral(InfinityPoint(self.secp128r1.coordinate_model))
diff --git a/test/ec/test_mult.py b/test/ec/test_mult.py
index 29e23ba..8a82e3a 100644
--- a/test/ec/test_mult.py
+++ b/test/ec/test_mult.py
@@ -1,35 +1,19 @@
from unittest import TestCase
-from pyecsca.ec.curve import EllipticCurve
-from pyecsca.ec.mod import Mod
-from pyecsca.ec.model import ShortWeierstrassModel, MontgomeryModel
from pyecsca.ec.mult import (LTRMultiplier, RTLMultiplier, LadderMultiplier, BinaryNAFMultiplier,
WindowNAFMultiplier, SimpleLadderMultiplier, CoronMultiplier)
-from pyecsca.ec.point import Point
+from pyecsca.ec.point import InfinityPoint
+from test.ec.curves import get_secp128r1, get_curve25519
class ScalarMultiplierTests(TestCase):
def setUp(self):
- self.p = 0xfffffffdffffffffffffffffffffffff
- self.coords = ShortWeierstrassModel().coordinates["projective"]
- self.base = Point(self.coords, X=Mod(0x161ff7528b899b2d0c28607ca52c5b86, self.p),
- Y=Mod(0xcf5ac8395bafeb13c02da292dded7a83, self.p),
- Z=Mod(1, self.p))
- self.secp128r1 = EllipticCurve(ShortWeierstrassModel(), self.coords,
- dict(a=0xfffffffdfffffffffffffffffffffffc,
- b=0xe87579c11079f43dd824993c2cee5ed3),
- Point(self.coords, X=Mod(0, self.p), Y=Mod(1, self.p),
- Z=Mod(0, self.p)))
+ self.secp128r1, self.base = get_secp128r1()
+ self.coords = self.secp128r1.coordinate_model
- self.coords25519 = MontgomeryModel().coordinates["xz"]
- self.p25519 = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed
- self.base25519 = Point(self.coords25519, X=Mod(9, self.p25519),
- Z=Mod(1, self.p25519))
- self.curve25519 = EllipticCurve(MontgomeryModel(), self.coords25519,
- dict(a=486662, b=1),
- Point(self.coords25519,
- X=Mod(0, self.p25519), Z=Mod(1, self.p25519)))
+ self.curve25519, self.base25519 = get_curve25519()
+ self.coords25519 = self.curve25519.coordinate_model
def test_rtl(self):
mult = RTLMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
@@ -38,6 +22,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base)
other = mult.multiply(2, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords), mult.multiply(0, self.base))
def test_ltr(self):
mult = LTRMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
@@ -46,6 +31,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base)
other = mult.multiply(2, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords), mult.multiply(0, self.base))
def test_coron(self):
mult = CoronMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
@@ -54,6 +40,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base)
other = mult.multiply(2, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords), mult.multiply(0, self.base))
def test_ladder(self):
mult = LadderMultiplier(self.curve25519, self.coords25519.formulas["ladd-1987-m"],
@@ -63,6 +50,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base25519)
other = mult.multiply(3, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords25519), mult.multiply(0, self.base25519))
def test_simple_ladder(self):
mult = SimpleLadderMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
@@ -72,6 +60,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base)
other = mult.multiply(2, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords), mult.multiply(0, self.base))
def test_ladder_differential(self):
ladder = LadderMultiplier(self.curve25519, self.coords25519.formulas["ladd-1987-m"],
@@ -84,6 +73,7 @@ class ScalarMultiplierTests(TestCase):
res_ladder = ladder.multiply(15, self.base25519)
res_differential = differential.multiply(15, self.base25519)
self.assertEqual(res_ladder, res_differential)
+ self.assertEqual(InfinityPoint(self.coords25519), differential.multiply(0, self.base25519))
def test_binary_naf(self):
mult = BinaryNAFMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
@@ -93,6 +83,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base)
other = mult.multiply(2, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords), mult.multiply(0, self.base))
def test_window_naf(self):
mult = WindowNAFMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
@@ -102,6 +93,7 @@ class ScalarMultiplierTests(TestCase):
other = mult.multiply(5, self.base)
other = mult.multiply(2, other)
self.assertEqual(res, other)
+ self.assertEqual(InfinityPoint(self.coords), mult.multiply(0, self.base))
mult = WindowNAFMultiplier(self.secp128r1, self.coords.formulas["add-1998-cmo"],
self.coords.formulas["dbl-1998-cmo"],