ecc

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

commit 825f18656186b7d93d659a060042281a29666632
parent 14ec900382ab0a82db71ec4ce35b5a1f34d560a4
Author: Shivakumar <[email protected]>
Date:   Sat, 26 Jul 2025 19:29:35 +0530

better tests for point and field

Diffstat:
Mtests/test_field.py | 95++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
Mtests/test_point.py | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
2 files changed, 160 insertions(+), 45 deletions(-)

diff --git a/tests/test_field.py b/tests/test_field.py @@ -1,36 +1,85 @@ +import pytest +from hypothesis import given, strategies as st + from ecc import FieldElement -def test_addition(): - a = FieldElement(7, 13) - b = FieldElement(12, 13) - assert a + b == FieldElement(6, 13) +# --- Testing for Expected Errors --- +class TestFieldElementErrors: + def test_init_invalid_prime(self): + with pytest.raises(ValueError, match="Prime must be greater than 1."): + FieldElement(3, 1) + + def test_init_invalid_type(self): + with pytest.raises(TypeError, match="Number and prime must be integers."): + FieldElement(3.5, 13) + + def test_add_mismatched_fields(self): + a = FieldElement(7, 13) + b = FieldElement(5, 17) + with pytest.raises( + TypeError, match="Cannot operate on FieldElements from different fields." + ): + c = a + b + + def test_division_by_zero(self): + a = FieldElement(7, 13) + b = FieldElement(0, 13) + with pytest.raises((ValueError, ZeroDivisionError)): + c = a / b + + +# --- Testing Edge Cases and Reflected Operations --- +class TestFieldElementOperations: + def test_addition(self): + a = FieldElement(7, 13) + b = FieldElement(12, 13) + assert a + b == FieldElement(6, 13) + + def test_reflected_addition(self): + a = FieldElement(7, 13) + assert 5 + a == FieldElement(12, 13) + + def test_subtraction(self): + a = FieldElement(7, 13) + b = FieldElement(12, 13) + assert b - a == FieldElement(5, 13) + + def test_negation(self): + a = FieldElement(7, 13) + assert -a == FieldElement(6, 13) + assert a + (-a) == FieldElement(0, 13) + + def test_multiplication_by_zero(self): + a = FieldElement(7, 13) + zero = FieldElement(0, 13) + assert a * zero == zero + def test_exponentiation_to_zero(self): + a = FieldElement(7, 13) + identity = FieldElement(1, 13) + assert a**0 == identity -def test_exponentiation(): - a = FieldElement(3, 13) - assert a**12 == FieldElement(1, 13) +# --- Property-Based Testing (Advanced) --- +field_elements = st.builds(FieldElement, num=st.integers(0, 12), prime=st.just(13)) -def test_multiplication(): - a = FieldElement(7, 13) - b = FieldElement(12, 13) - assert a * b == FieldElement(6, 13) +@given(a=field_elements, b=field_elements) +def test_commutative_property_of_addition(a, b): + assert a + b == b + a -def test_subtraction(): - a = FieldElement(7, 13) - b = FieldElement(12, 13) - assert b - a == FieldElement(5, 13) +@given(a=field_elements, b=field_elements) +def test_commutative_property_of_multiplication(a, b): + assert a * b == b * a -def test_scalar_multiplication(): - a = FieldElement(7, 13) - k = 2 - assert k * a == FieldElement(1, 13) +@given(a=field_elements) +def test_multiplicative_inverse(a): + if a.num == 0: + return -def test_division(): - a = FieldElement(7, 13) - b = FieldElement(3, 13) - assert a / b == FieldElement(11, 13) + identity = FieldElement(1, a.prime) + inverse = 1 / a + assert a * inverse == identity diff --git a/tests/test_point.py b/tests/test_point.py @@ -1,38 +1,104 @@ -from ecc import Point -from ecc import Curve +import pytest +from hypothesis import given, strategies as st +from ecc import Point, Curve +from ecc import secp256k1 -G = (5, 1) +# --- Test Setup --- P = 17 -n = 19 +A = 2 +B = 2 +N = 19 +Gx, Gy = (5, 1) -curve = Curve(2, 2, P, G, n) +# A global curve object for our tests +curve = Curve(P=P, a=A, b=B, G=(Gx, Gy), n=N) +G = Point(Gx, Gy, curve) +I = Point(None, None, curve) -def test_point_on_curve(): - p = Point(3, 1, curve) - assert p.y**2 == p.x**3 + curve.a * p.x + curve.b +# --- Testing for Expected Errors --- +class TestPointErrors: + def test_init_point_not_on_curve(self): + """Points not satisfying the curve equation should raise a ValueError.""" + with pytest.raises(ValueError, match="is not on the curve"): + Point(x=3, y=7, curve=curve) + def test_add_points_on_different_curves(self): + """Adding points from different curves should raise a TypeError.""" + p1 = G + p2 = Point(secp256k1.G[0], secp256k1.G[1], secp256k1) + with pytest.raises(TypeError, match="Points are not on the same curve"): + p1 + p2 -def test_addition(): - p1 = Point(5, 1, curve) - p2 = Point(6, 3, curve) + def test_rmul_by_non_integer(self): + """Scalar multiplication requires an integer scalar.""" + with pytest.raises(TypeError, match="Scalar must be an integer"): + p = 2.5 * G - assert Point(10, 6, curve) == p1 + p2 +# --- Testing Edge Cases and Core Operations --- +class TestPointOperations: + def test_addition(self): + """Test standard point addition P + Q = R.""" + p1 = G + p2 = Point(6, 3, curve) + assert p1 + p2 == Point(10, 6, curve) -def test_point_doubling(): - p = Point(5, 1, curve) - assert Point(6, 3, curve) == p + p + def test_doubling(self): + """Test point doubling P + P = 2P.""" + assert G + G == Point(6, 3, curve) + def test_identity_element(self): + """Test that adding the point at infinity is an identity operation.""" + assert G + I == G + assert I + G == G -def test_scalar_multiplication(): - p = Point(5, 1, curve) - k = 16 + def test_additive_inverse(self): + """Test that adding a point to its inverse results in infinity.""" + p_inv = Point(G.x.num, -G.y.num, curve) + assert G + p_inv == I - assert Point(10, 11, curve) == k * p +# --- Property-Based Testing (with Hypothesis) --- +integers = st.integers(min_value=1, max_value=N - 1) +points = st.integers(min_value=1, max_value=N - 1).map(lambda k: k * G) -def test_infinity(): - p = Point(5, 1, curve) - assert Point(None, None, curve) == n * p + +@given(P=points) +def test_identity_property(P): + """Property: P + O = P (where O is the identity/infinity point).""" + assert P + I == P + assert I + P == P + + +@given(P=points) +def test_inverse_property(P): + """Property: P + (-P) = O.""" + if P.y is not None: + P_inv = Point(P.x.num, -P.y.num, curve) + assert P + P_inv == I + + +@given(k=integers, l=integers) +def test_scalar_distributivity(k, l): + """Property: (k + l) * P = k*P + l*P.""" + p1 = (k + l) * G + p2 = (k * G) + (l * G) + assert p1 == p2 + + +@given(k=integers, l=integers) +def test_scalar_associativity(k, l): + """Property: k * (l * P) = (k * l) * P.""" + p1 = k * (l * G) + p2 = ((k * l) % N) * G # modulo N + assert p1 == p2 + + +@given(P=points, Q=points, R=points) +def test_associativity_of_addition(P, Q, R): + """Property: (P + Q) + R = P + (Q + R).""" + p1 = (P + Q) + R + p2 = P + (Q + R) + assert p1 == p2