ecc

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

test_keys.py (4745B)


      1 import pytest
      2 from hypothesis import given
      3 from hypothesis import strategies as st
      4 
      5 from ecc import Curve, Point, PrivateKey, PublicKey, generate_keypair, secp256k1
      6 
      7 # --- Test Setup ---
      8 curve = Curve(P=17, a=2, b=2, G=(5, 1), n=19)
      9 
     10 
     11 # --- Test Classes for Organization ---
     12 class TestPrivateKey:
     13     """Tests functionalities of the PrivateKey class."""
     14 
     15     def test_init_with_secret(self):
     16         """Test creating a private key with a specific secret."""
     17         pk = PrivateKey(secret=10, curve=curve)
     18         assert pk.secret == 10
     19         assert pk.curve == curve
     20 
     21     def test_init_without_secret(self):
     22         """Test that a new key is generated within the correct range."""
     23         pk = PrivateKey(curve=curve)
     24         assert 1 <= pk.secret < curve.n
     25 
     26     def test_public_key_property(self):
     27         """Test that the public_key property computes the correct point."""
     28         pk = PrivateKey(secret=2, curve=curve)
     29         expected_point = Point(6, 3, curve)
     30         assert pk.public_key.point == expected_point
     31 
     32     def test_public_key_is_cached(self):
     33         """Test that the public key is computed only once and cached."""
     34         pk = PrivateKey(curve=curve)
     35         pub_key1 = pk.public_key
     36         pub_key2 = pk.public_key
     37         assert pub_key1 is pub_key2
     38 
     39     def test_equality(self):
     40         """Test the __eq__ method for private keys."""
     41         pk1 = PrivateKey(secret=5, curve=curve)
     42         pk2 = PrivateKey(secret=5, curve=curve)
     43         pk3 = PrivateKey(secret=6, curve=curve)
     44         assert pk1 == pk2
     45         assert pk1 != pk3
     46 
     47 
     48 class TestPublicKey:
     49     """Tests functionalities of the PublicKey class."""
     50 
     51     def test_init(self):
     52         point = Point(5, 1, curve)
     53         pub_key = PublicKey(point)
     54         assert pub_key.point == point
     55 
     56     def test_curve_property(self):
     57         """Test that the curve property correctly points to the curve."""
     58         point = Point(5, 1, curve)
     59         pub_key = PublicKey(point)
     60         assert pub_key.curve == curve
     61 
     62     def test_equality(self):
     63         """Test the __eq__ method for public keys."""
     64         p1 = Point(5, 1, curve)
     65         p2 = Point(6, 3, curve)
     66 
     67         pub1 = PublicKey(p1)
     68         pub2 = PublicKey(p1)
     69         pub3 = PublicKey(p2)
     70 
     71         assert pub1 == pub2
     72         assert pub1 != pub3
     73 
     74 
     75 class TestKeyGeneration:
     76     """Tests the key generation process and error handling."""
     77 
     78     def test_generate_keypair_function(self):
     79         """Test the top-level generate_keypair helper function."""
     80         private_key, public_key = generate_keypair(curve)
     81 
     82         assert isinstance(private_key, PrivateKey)
     83         assert isinstance(public_key, PublicKey)
     84         assert private_key.public_key == public_key
     85 
     86     def test_error_on_out_of_range_secret(self):
     87         """A secret must be in the range [1, n-1]."""
     88         with pytest.raises(ValueError):
     89             PrivateKey(secret=0, curve=curve)
     90 
     91         with pytest.raises(ValueError):
     92             PrivateKey(secret=curve.n, curve=curve)
     93 
     94     def test_error_on_missing_curve(self):
     95         """The curve is a required keyword-only argument."""
     96         with pytest.raises(TypeError):
     97             PrivateKey(secret=5)  # pyright: ignore[reportCallIssue]
     98 
     99 
    100 # --- Property-Based Testing ---
    101 
    102 curves = st.just(secp256k1)
    103 
    104 
    105 @st.composite
    106 def private_keys(draw, curve):
    107     """A Hypothesis strategy to generate a PrivateKey for a given curve."""
    108     secret = draw(st.integers(min_value=1, max_value=curve.n - 1))
    109     return PrivateKey(secret=secret, curve=curve)
    110 
    111 
    112 # --- Tests ---
    113 
    114 
    115 @given(curve=curves)
    116 def test_generate_keypair_is_valid(curve):
    117     """
    118     Property: The keypair generated by the helper function must be valid.
    119     The public key must correspond to the private key's secret.
    120     """
    121     private_key, public_key = generate_keypair(curve)
    122     assert isinstance(private_key, PrivateKey)
    123     assert isinstance(public_key, PublicKey)
    124 
    125     assert private_key.public_key == public_key
    126     assert public_key.point == private_key.secret * curve.G
    127 
    128 
    129 @given(private_key=private_keys(secp256k1))
    130 def test_public_key_is_always_on_curve(private_key):
    131     """
    132     Property: The public key must always be a valid point on its curve.
    133     This is a crucial security guarantee.
    134     """
    135     public_key = private_key.public_key
    136     point = public_key.point
    137     curve = public_key.curve
    138 
    139     assert point.y**2 == point.x**3 + curve.a * point.x + curve.b
    140 
    141 
    142 @given(private_key=private_keys(secp256k1))
    143 def test_key_relationship_property(private_key):
    144     """
    145     Property: The public key must always equal secret * G.
    146     This is the fundamental definition of an elliptic curve key pair.
    147     """
    148     public_key = private_key.public_key
    149     G = private_key.curve.G
    150     assert public_key.point == private_key.secret * G