ecc

Elliptic Curve Cryptograpy library from scratch in python.
Log | Files | Refs | README | LICENSE

test_curve.py (1826B)


      1 import pytest
      2 
      3 from ecc import Curve, Point
      4 
      5 curve = Curve(P=17, a=2, b=2, G=(5, 1), n=19, h=1, name="toy_curve")
      6 
      7 
      8 class TestCurve:
      9     """Tests the functionality of the Curve class."""
     10 
     11     def test_init(self):
     12         """Test that curve parameters are initialized correctly."""
     13         assert curve.P == 17
     14         assert curve.a.num == 2
     15         assert curve.b.num == 2
     16         assert curve.name == "toy_curve"
     17 
     18     def test_repr(self):
     19         """Test the __repr__ method for a clean representation."""
     20         assert repr(curve) == "Curve(toy_curve)"
     21 
     22         # Test representation for a curve without a name
     23         unnamed_curve = Curve(P=17, a=2, b=2, G=(5, 1), n=19)
     24         assert repr(unnamed_curve) == "Curve(y² = x³ + 2x + 2 mod 17)"
     25 
     26     def test_eq(self):
     27         """Test the __eq__ method for comparing curves."""
     28         curve1 = Curve(P=17, a=2, b=2, G=(5, 1), n=19)
     29         curve2 = Curve(P=17, a=2, b=2, G=(5, 1), n=19)
     30         assert curve1 == curve2
     31 
     32         curve3 = Curve(P=19, a=2, b=2, G=(5, 1), n=19)
     33         assert curve1 != curve3
     34 
     35     def test_generator_property(self):
     36         """Test that the G property returns a valid Point object."""
     37         G = curve.G
     38         assert isinstance(G, Point)
     39 
     40         # Assertion: Type safety
     41         assert G.x is not None and G.y is not None
     42 
     43         assert G.x.num == 5 and G.y.num == 1
     44         assert G.curve == curve
     45 
     46         G2 = curve.G
     47         assert id(G) == id(G2)
     48 
     49 
     50 class TestCurveErrors:
     51     """Tests the error handling of the Curve class."""
     52 
     53     def test_singular_curve(self):
     54         """Test that initializing a singular curve raises a ValueError."""
     55         # For y² = x³ - 3x + 2 over F_17, the discriminant is 0
     56         with pytest.raises(ValueError, match="This is a singular curve"):
     57             Curve(P=17, a=-3, b=2, G=(1, 0), n=19)