ecc

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

test_ecdsa.py (3038B)


      1 from hypothesis import given
      2 from hypothesis import strategies as st
      3 
      4 from ecc import PrivateKey, Signature, generate_keypair, secp256k1
      5 
      6 # --- Official RFC 6979 Test Vector for secp256k1 ---
      7 # Source: RFC 6979, Appendix A.2.5
      8 KAT_D = 0xC9AFA9D845BA75166B5C215767B1D6934E50C3DB36E89B127B8A622B120F6721
      9 KAT_MSG = b"sample"
     10 KAT_R = 0x432310E32CB80EB6503A26CE83CC165C783B870845FB8AAD6D970889FCD7A6C8
     11 KAT_S = 0x530128B6B81C548874A6305D93ED071CA6E05074D85863D4056CE89B02BFAB69
     12 
     13 
     14 class TestECDSACorrectness:
     15     """Verifies the ECDSA implementation against the official RFC 6979 standard."""
     16 
     17     def test_known_answer_vector_rfc6979(self):
     18         """
     19         Tests the implementation against the official test vector from
     20         RFC 6979, Appendix A.2.5. This is the gold standard for correctness.
     21         """
     22         private_key = PrivateKey(secret=KAT_D, curve=secp256k1)
     23         public_key = private_key.public_key
     24 
     25         # 1. Test Signature Generation
     26         signature = private_key.sign(KAT_MSG)
     27         assert signature.r == KAT_R, (
     28             "The 'r' component does not match the official RFC 6979 vector."
     29         )
     30         assert signature.s == KAT_S, (
     31             "The 's' component does not match the official RFC 6979 vector."
     32         )
     33 
     34         # 2. Test Signature Verification
     35         assert public_key.verify(KAT_MSG, signature) is True, (
     36             "A valid known signature failed to verify."
     37         )
     38 
     39 
     40 class TestECDSASecurity:
     41     """Tests for fundamental security properties."""
     42 
     43     def test_sign_produces_low_s_signature(self):
     44         """The sign function must enforce a low-s value to prevent malleability."""
     45         private_key = PrivateKey(curve=secp256k1)
     46         signature = private_key.sign(b"test for low-s")
     47         assert signature.s <= secp256k1.n // 2
     48 
     49     def test_verify_accepts_high_s_signature(self):
     50         """The verify function must accept a mathematically valid high-s signature."""
     51         priv, pub = generate_keypair(secp256k1)
     52         message = b"test for malleability"
     53         low_s_signature = priv.sign(message)
     54 
     55         high_s = secp256k1.n - low_s_signature.s
     56         malleated_signature = Signature(r=low_s_signature.r, s=high_s)
     57 
     58         assert pub.verify(message, malleated_signature) is True
     59 
     60 
     61 # --- Property-Based Testing ---
     62 
     63 private_keys = st.builds(PrivateKey, curve=st.just(secp256k1))
     64 messages = st.binary(min_size=1, max_size=256)
     65 
     66 
     67 @given(private_key=private_keys, message=messages)
     68 def test_sign_verify_property(private_key, message):
     69     """Property: Any message signed by a key must be verifiable by its public key."""
     70     public_key = private_key.public_key
     71     signature = private_key.sign(message)
     72     assert public_key.verify(message, signature) is True
     73 
     74 
     75 @given(private_key=private_keys, message=messages)
     76 def test_deterministic_signature_property(private_key, message):
     77     """Property: Signing the same message twice must produce the exact same signature."""
     78     sig1 = private_key.sign(message)
     79     sig2 = private_key.sign(message)
     80     assert sig1 == sig2