test_point.py (3190B)
1 import pytest 2 from hypothesis import given 3 from hypothesis import strategies as st 4 5 from ecc import Curve, Point, secp256k1 6 7 # --- Test Setup --- 8 P = 17 9 A = 2 10 B = 2 11 N = 19 12 Gx, Gy = (5, 1) 13 14 # A global curve object for our tests 15 curve = Curve(P=P, a=A, b=B, G=(Gx, Gy), n=N) 16 G = Point(Gx, Gy, curve) 17 identity = Point(None, None, curve) 18 19 20 # --- Testing for Expected Errors --- 21 class TestPointErrors: 22 def test_init_point_not_on_curve(self): 23 """Points not satisfying the curve equation should raise a ValueError.""" 24 with pytest.raises(ValueError, match="is not on the curve"): 25 Point(x=3, y=7, curve=curve) 26 27 def test_add_points_on_different_curves(self): 28 """Adding points from different curves should raise a TypeError.""" 29 p1 = G 30 p2 = secp256k1.G 31 with pytest.raises(TypeError, match="Points are not on the same curve"): 32 p1 + p2 # pyright: ignore[reportUnusedExpression] 33 34 def test_rmul_by_non_integer(self): 35 """Scalar multiplication requires an integer scalar.""" 36 with pytest.raises(TypeError, match="Scalar must be an integer"): 37 2.5 * G # pyright: ignore[reportUnusedExpression, reportOperatorIssue] 38 39 40 # --- Testing Edge Cases and Core Operations --- 41 class TestPointOperations: 42 def test_addition(self): 43 """Test standard point addition P + Q = R.""" 44 p1 = G 45 p2 = Point(6, 3, curve) 46 assert p1 + p2 == Point(10, 6, curve) 47 48 def test_doubling(self): 49 """Test point doubling P + P = 2P.""" 50 assert G + G == Point(6, 3, curve) 51 52 def test_identity_element(self): 53 """Test that adding the point at infinity is an identity operation.""" 54 assert G + identity == G 55 assert identity + G == G 56 57 def test_additive_inverse(self): 58 """Test that adding a point to its inverse results in infinity.""" 59 60 # Assertion: Type safety 61 assert G.x is not None and G.y is not None 62 63 p_inv = Point(G.x.num, -G.y.num, curve) 64 assert G + p_inv == identity 65 66 67 # --- Property-Based Testing (with Hypothesis) --- 68 integers = st.integers(min_value=1, max_value=N - 1) 69 points = st.integers(min_value=1, max_value=N - 1).map(lambda k: k * G) 70 71 72 @given(P=points) 73 def test_identity_property(P): 74 """Property: P + O = P (where O is the identity/infinity point).""" 75 assert P + identity == P 76 assert identity + P == P 77 78 79 @given(P=points) 80 def test_inverse_property(P): 81 """Property: P + (-P) = O.""" 82 if P.y is not None: 83 P_inv = Point(P.x.num, -P.y.num, curve) 84 assert P + P_inv == identity 85 86 87 @given(k1=integers, k2=integers) 88 def test_scalar_distributivity(k1, k2): 89 """Property: (k1 + k2) * P = k1*P + k2*P.""" 90 p1 = ((k1 + k2) % N) * G 91 p2 = (k1 * G) + (k2 * G) 92 assert p1 == p2 93 94 95 @given(k1=integers, k2=integers) 96 def test_scalar_associativity(k1, k2): 97 """Property: k1 * (k2 * P) = (k1 * k2) * P.""" 98 p1 = k1 * (k2 * G) 99 p2 = ((k1 * k2) % N) * G 100 assert p1 == p2 101 102 103 @given(P=points, Q=points, R=points) 104 def test_associativity_of_addition(P, Q, R): 105 """Property: (P + Q) + R = P + (Q + R).""" 106 p1 = (P + Q) + R 107 p2 = P + (Q + R) 108 assert p1 == p2