ecc

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

commit aa52146f8d3ee7de3e9c856093de7a0bc3ced28d
parent 32ee643f47aed97facf9dc97486b7fc90818ff2c
Author: Shivakumar <[email protected]>
Date:   Sat,  2 Aug 2025 01:44:17 +0530

add tests for secp256k1 curve

Diffstat:
Mtests/test_secp256k1.py | 75++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------
1 file changed, 60 insertions(+), 15 deletions(-)

diff --git a/tests/test_secp256k1.py b/tests/test_secp256k1.py @@ -1,30 +1,75 @@ -from ecc import Point +from hypothesis import given, strategies as st from ecc import secp256k1 +from ecc import PrivateKey -def test_generator_point(): - Gx, Gy = secp256k1.G - G = Point(Gx, Gy, secp256k1) +class TestSecp256k1Integration: + """ + Integration tests for the secp256k1 curve instance. + These tests verify that the library works correctly with standard, + real-world curve parameters and known test vectors. + """ - assert G + def test_generator_is_on_curve(self): + """The secp256k1 generator point G must satisfy the curve equation.""" + G = secp256k1.G + assert G is not None, "Generator point should not be None" + assert G.y**2 == G.x**3 + secp256k1.a * G.x + secp256k1.b + def test_point_order_is_correct(self): + """The order 'n' of the curve should result in n * G = Infinity.""" + G = secp256k1.G + n = secp256k1.n + infinity_point = n * G -def test_scalar_multiplication(): - Gx, Gy = secp256k1.G - G = Point(Gx, Gy, secp256k1) + assert infinity_point.x is None, ( + "The x-coordinate should be None for the point at infinity" + ) + assert infinity_point.y is None, ( + "The y-coordinate should be None for the point at infinity" + ) + + def test_known_scalar_multiplication(self): + """ + Known Answer Test (KAT): Verifies scalar multiplication against a + standard, pre-computed value for 2*G on secp256k1. + """ + G = secp256k1.G + two_G = 2 * G + + two_G_x = 0xC6047F9441ED7D6D3045406E95C07CD85C778E4B8CEF3CA7ABAC09B95C709EE5 + two_G_y = 0x1AE168FEA63DC339A3C58419466CEAEEF7F632653266D0E1236431A950CFE52A + + assert two_G.x.num == two_G_x + assert two_G.y.num == two_G_y - k = 2 - P = k * G +# --- Property-Based Testing for Secp256k1 --- + +secp256k1_secrets = st.integers(min_value=1, max_value=secp256k1.n - 1) + + +@given(k=secp256k1_secrets) +def test_multiplicative_inverse_property(k): + """ + Property: For any scalar k, k * G * k⁻¹ should result back in G. + This is a strong check of the entire multiplication and field logic. + """ + G = secp256k1.G + P = k * G k_inv = pow(k, -1, secp256k1.n) G_check = k_inv * P assert G_check == G -def test_point_order(): - Gx, Gy = secp256k1.G - G = Point(Gx, Gy, secp256k1) - infinity = secp256k1.n * G +@given(secret=secp256k1_secrets) +def test_generated_public_key_is_valid(secret): + """ + Property: A public key generated from any valid secret must be a + valid point on the curve. + """ + private_key = PrivateKey(secret=secret, curve=secp256k1) + point = private_key.public_key.point - assert (infinity.x, infinity.y) == (None, None) + assert point.y**2 == point.x**3 + secp256k1.a * point.x + secp256k1.b