Ejemplo n.º 1
0
    def test_init(self):
        with self.assertRaises(ValueError):
            EllipticCurve(
                MontgomeryModel(),
                self.secp128r1.curve.coordinate_model,
                1,
                InfinityPoint(self.secp128r1.curve.coordinate_model),
                parameters={},
            )

        with self.assertRaises(ValueError):
            EllipticCurve(
                self.secp128r1.curve.model,
                self.secp128r1.curve.coordinate_model,
                15,
                InfinityPoint(self.secp128r1.curve.coordinate_model),
                parameters={"c": 0},
            )

        with self.assertRaises(ValueError):
            EllipticCurve(
                self.secp128r1.curve.model,
                self.secp128r1.curve.coordinate_model,
                15,
                InfinityPoint(self.secp128r1.curve.coordinate_model),
                parameters={"a": Mod(1, 5), "b": Mod(2, 5)},
            )
Ejemplo n.º 2
0
    def test_equals(self):
        pt = Point(self.coords,
                   X=Mod(0x4, self.secp128r1.curve.prime),
                   Y=Mod(0x6, self.secp128r1.curve.prime),
                   Z=Mod(2, self.secp128r1.curve.prime))
        other = Point(self.coords,
                      X=Mod(0x2, self.secp128r1.curve.prime),
                      Y=Mod(0x3, self.secp128r1.curve.prime),
                      Z=Mod(1, self.secp128r1.curve.prime))
        self.assertTrue(pt.equals(other))
        self.assertNotEqual(pt, other)
        self.assertFalse(pt.equals(2))
        self.assertNotEqual(pt, 2)

        infty_one = InfinityPoint(self.coords)
        infty_other = InfinityPoint(self.coords)
        self.assertTrue(infty_one.equals(infty_other))
        self.assertEqual(infty_one, infty_other)

        mont = MontgomeryModel()
        different = Point(mont.coordinates["xz"],
                          X=Mod(0x64daccd2656420216545e5f65221eb,
                                0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),
                          Z=Mod(1, 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa))
        self.assertFalse(pt.equals(different))
        self.assertNotEqual(pt, different)
Ejemplo n.º 3
0
 def test_bytes(self):
     pt = Point(self.coords,
                X=Mod(0x4, self.secp128r1.curve.prime),
                Y=Mod(0x6, self.secp128r1.curve.prime),
                Z=Mod(2, self.secp128r1.curve.prime))
     self.assertEqual(
         bytes(pt),
         b"\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"
     )
     self.assertEqual(bytes(InfinityPoint(self.coords)), b"\x00")
Ejemplo n.º 4
0
    def test_to_model(self):
        affine = Point(self.affine,
                       x=Mod(0xabcd, self.secp128r1.curve.prime),
                       y=Mod(0xef, self.secp128r1.curve.prime))
        projective_model = self.coords
        other = affine.to_model(projective_model, self.secp128r1.curve)

        self.assertEqual(other.coordinate_model, projective_model)
        self.assertSetEqual(set(other.coords.keys()),
                            set(projective_model.variables))
        self.assertEqual(other.coords["X"], affine.coords["x"])
        self.assertEqual(other.coords["Y"], affine.coords["y"])
        self.assertEqual(other.coords["Z"], Mod(1, self.secp128r1.curve.prime))

        infty = InfinityPoint(AffineCoordinateModel(
            self.secp128r1.curve.model))
        other_infty = infty.to_model(self.coords, self.secp128r1.curve)
        self.assertIsInstance(other_infty, InfinityPoint)

        with self.assertRaises(ValueError):
            self.base.to_model(self.coords, self.secp128r1.curve)
Ejemplo n.º 5
0
 def do_basic_test(self, mult_class, params, base, add, dbl, scale, neg=None, **kwargs):
     mult = mult_class(*self.get_formulas(params.curve.coordinate_model, add, dbl, neg, scale),
                       **kwargs)
     mult.init(params, base)
     res = mult.multiply(314)
     other = mult.multiply(157)
     mult.init(params, other)
     other = mult.multiply(2)
     self.assertPointEquality(res, other, scale)
     mult.init(params, base)
     self.assertEqual(InfinityPoint(params.curve.coordinate_model), mult.multiply(0))
     return res
Ejemplo n.º 6
0
 def test_ladder_differential(self, name, num, complete):
     ladder = LadderMultiplier(self.coords25519.formulas["ladd-1987-m"],
                               self.coords25519.formulas["dbl-1987-m"],
                               self.coords25519.formulas["scale"], complete=complete)
     differential = DifferentialLadderMultiplier(self.coords25519.formulas["dadd-1987-m"],
                                                 self.coords25519.formulas["dbl-1987-m"],
                                                 self.coords25519.formulas["scale"],
                                                 complete=complete)
     ladder.init(self.curve25519, self.base25519)
     res_ladder = ladder.multiply(num)
     differential.init(self.curve25519, self.base25519)
     res_differential = differential.multiply(num)
     self.assertEqual(res_ladder, res_differential)
     self.assertEqual(InfinityPoint(self.coords25519), differential.multiply(0))
Ejemplo n.º 7
0
    def test_window_naf(self, name, add, dbl, neg, width, scale):
        formulas = self.get_formulas(self.coords, add, dbl, neg, scale)
        mult = WindowNAFMultiplier(*formulas[:3], width, *formulas[3:])
        mult.init(self.secp128r1, self.base)
        res = mult.multiply(157*789)
        other = mult.multiply(157)
        mult.init(self.secp128r1, other)
        other = mult.multiply(789)
        self.assertPointEquality(res, other, scale)
        mult.init(self.secp128r1, self.base)
        self.assertEqual(InfinityPoint(self.coords), mult.multiply(0))

        mult = WindowNAFMultiplier(*formulas[:3], width, *formulas[3:],
                                   precompute_negation=True)
        mult.init(self.secp128r1, self.base)
        res_precompute = mult.multiply(157*789)
        self.assertPointEquality(res_precompute, res, scale)
Ejemplo n.º 8
0
    def test_to_affine(self):
        pt = Point(self.coords,
                   X=Mod(0x161ff7528b899b2d0c28607ca52c5b86,
                         self.secp128r1.curve.prime),
                   Y=Mod(0xcf5ac8395bafeb13c02da292dded7a83,
                         self.secp128r1.curve.prime),
                   Z=Mod(1, self.secp128r1.curve.prime))
        affine = pt.to_affine()

        self.assertIsInstance(affine.coordinate_model, AffineCoordinateModel)
        self.assertSetEqual(set(affine.coords.keys()),
                            set(self.affine.variables))
        self.assertEqual(affine.coords["x"], pt.coords["X"])
        self.assertEqual(affine.coords["y"], pt.coords["Y"])
        self.assertEqual(affine.to_affine(), affine)

        affine = InfinityPoint(self.coords).to_affine()
        self.assertIsInstance(affine, InfinityPoint)
Ejemplo n.º 9
0
    def test_equals(self):
        pt = Point(
            self.coords,
            X=Mod(0x4, self.secp128r1.curve.prime),
            Y=Mod(0x6, self.secp128r1.curve.prime),
            Z=Mod(2, self.secp128r1.curve.prime),
        )
        other = Point(
            self.coords,
            X=Mod(0x2, self.secp128r1.curve.prime),
            Y=Mod(0x3, self.secp128r1.curve.prime),
            Z=Mod(1, self.secp128r1.curve.prime),
        )
        third = Point(
            self.coords,
            X=Mod(0x5, self.secp128r1.curve.prime),
            Y=Mod(0x3, self.secp128r1.curve.prime),
            Z=Mod(1, self.secp128r1.curve.prime),
        )
        self.assertTrue(pt.equals(other))
        self.assertNotEqual(pt, other)
        self.assertFalse(pt.equals(2))
        self.assertNotEqual(pt, 2)
        self.assertFalse(pt.equals(third))
        self.assertNotEqual(pt, third)
        self.assertTrue(pt.equals_scaled(other))
        self.assertTrue(pt.equals_affine(other))
        self.assertFalse(pt.equals_scaled(third))

        infty_one = InfinityPoint(self.coords)
        infty_other = InfinityPoint(self.coords)
        self.assertTrue(infty_one.equals(infty_other))
        self.assertTrue(infty_one.equals_affine(infty_other))
        self.assertTrue(infty_one.equals_scaled(infty_other))
        self.assertEqual(infty_one, infty_other)
        self.assertFalse(pt.equals(infty_one))
        self.assertFalse(pt.equals_affine(infty_one))
        self.assertFalse(pt.equals_scaled(infty_one))

        mont = MontgomeryModel()
        different = Point(
            mont.coordinates["xz"],
            X=Mod(
                0x64DACCD2656420216545E5F65221EB,
                0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,
            ),
            Z=Mod(1, 0xAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA),
        )
        self.assertFalse(pt.equals(different))
        self.assertNotEqual(pt, different)
Ejemplo n.º 10
0
    def test_to_affine(self):
        pt = Point(
            self.coords,
            X=Mod(0x161FF7528B899B2D0C28607CA52C5B86,
                  self.secp128r1.curve.prime),
            Y=Mod(0xCF5AC8395BAFEB13C02DA292DDED7A83,
                  self.secp128r1.curve.prime),
            Z=Mod(1, self.secp128r1.curve.prime),
        )
        affine = pt.to_affine()

        self.assertIsInstance(affine.coordinate_model, AffineCoordinateModel)
        self.assertSetEqual(set(affine.coords.keys()),
                            set(self.affine.variables))
        self.assertEqual(affine.coords["x"], pt.coords["X"])
        self.assertEqual(affine.coords["y"], pt.coords["Y"])
        self.assertEqual(affine.to_affine(), affine)

        affine = InfinityPoint(self.coords).to_affine()
        self.assertIsInstance(affine, InfinityPoint)
Ejemplo n.º 11
0
 def test_is_neutral(self):
     self.assertTrue(
         self.secp128r1.curve.is_neutral(
             InfinityPoint(self.secp128r1.curve.coordinate_model)))