예제 #1
0
    def test_encode(self):
        """Unit test native Python EC point encoding"""

        G, _ = get_ec_curve_params(b'nistp256')

        with self.subTest('Encode infinity'):
            self.assertEqual(encode_ec_point(None, None, None), b'\x00')

        with self.subTest('Decode infinity'):
            point = PrimePoint.decode(G.curve, b'\x00')
            self.assertEqual((point.curve, point.x, point.y),
                             (None, None, None))

        with self.subTest('Encode and decode'):
            self.assertEqual(PrimePoint.decode(G.curve, G.encode()), G)

        with self.subTest('Bad point type'):
            with self.assertRaises(ValueError):
                decode_ec_point(0, b'\x05')

        with self.subTest('Bad point length'):
            with self.assertRaises(ValueError):
                decode_ec_point(G.curve.keylen, G.encode()[:-1])
예제 #2
0
    def test_math(self):
        """Unit test native Python EC point math"""

        G, n = get_ec_curve_params(b'nistp256')
        G2, _ = get_ec_curve_params(b'nistp521')
        Inf = PrimePoint.construct(G.curve, None, None)

        with self.subTest('Add to infinity'):
            self.assertEqual(G + Inf, G)

        with self.subTest('Negate'):
            negG = -G
            self.assertEqual(-negG, G)

        with self.subTest('Negate infinity'):
            self.assertEqual(-Inf, Inf)

        with self.subTest('Multiply returning infinity'):
            self.assertEqual(n * G, Inf)

        with self.subTest('Add from different curves'):
            with self.assertRaises(ValueError):
                _ = G + G2