From 28e2cf2b1c2e2354317a7f7e9567cf5818c4339c Mon Sep 17 00:00:00 2001 From: Evan Pardon <158092262+epard0@users.noreply.github.com> Date: Sat, 21 Nov 2026 15:20:28 -0500 Subject: [PATCH] Correct exceptional short Weierstrass point arithmetic --- src/ecdsa/ellipticcurve.py | 23 +++++++++---- src/ecdsa/test_exceptional_points.py | 50 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 src/ecdsa/test_exceptional_points.py diff --git a/src/ecdsa/ellipticcurve.py b/src/ecdsa/ellipticcurve.py index f601f263..0b44dd2d 100644 --- a/src/ecdsa/ellipticcurve.py +++ b/src/ecdsa/ellipticcurve.py @@ -812,11 +812,11 @@ def _add_with_z_1(self, X1, Y1, X2, Y2, p): """add points when both Z1 and Z2 equal 1""" # after: # http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-mmadd-2007-bl - H = X2 - X1 + H = (X2 - X1) % p HH = H * H I = 4 * HH % p J = H * I - r = 2 * (Y2 - Y1) + r = 2 * (Y2 - Y1) % p if not H and not r: return self._double_with_z_1(X1, Y1, p, self.__curve.a()) V = X1 * I @@ -948,8 +948,13 @@ def _mul_precompute(self, other): def __mul__(self, other): """Multiply point by an integer.""" - if not self.__coords[1] or not other: + if not self.__coords[2] or not other: return INFINITY + if self.__coords[1] % self.__curve.p() == 0: + if not self.__curve.contains_point(self.x(), self.y()): + return INFINITY + # Finite points with zero Y have order two. + return self if other % 2 else INFINITY if other == 1: return self if self.__order: @@ -1171,7 +1176,11 @@ def __ne__(self, other): return not self == other def __neg__(self): - return Point(self.__curve, self.__x, self.__curve.p() - self.__y) + return Point( + self.__curve, + self.__x % self.__curve.p(), + (-self.__y) % self.__curve.p(), + ) def __add__(self, other): """Add one point to another point.""" @@ -1246,14 +1255,14 @@ def double(self): p = self.__curve.p() a = self.__curve.a() + if self.__y % p == 0: + return INFINITY + l = ( (3 * self.__x * self.__x + a) * numbertheory.inverse_mod(2 * self.__y, p) ) % p - if not l: - return INFINITY - x3 = (l * l - 2 * self.__x) % p y3 = (l * (self.__x - x3) - self.__y) % p diff --git a/src/ecdsa/test_exceptional_points.py b/src/ecdsa/test_exceptional_points.py new file mode 100644 index 00000000..6f9bfd5a --- /dev/null +++ b/src/ecdsa/test_exceptional_points.py @@ -0,0 +1,50 @@ +"""Regressions for exceptional points on short Weierstrass curves.""" + +import pytest + +from .ellipticcurve import CurveFp, INFINITY, Point, PointJacobi + + +def test_horizontal_and_vertical_tangents(): + curve = CurveFp(17, 0, 1) + assert Point(curve, 0, 1).double() == Point(curve, 0, 16) + assert Point(curve, 16, 0).double() == INFINITY + + +def test_negation_of_order_two_point_is_canonical(): + point = Point(CurveFp(17, 0, 1), 16, 0) + assert (-point).x() == 16 + assert (-point).y() == 0 + + +@pytest.mark.parametrize("scalar", range(-4, 5)) +@pytest.mark.parametrize("scale", [1, 2, 16]) +def test_finite_order_two_multiplication(scalar, scale): + curve = CurveFp(17, 0, 1) + point = PointJacobi(curve, 16 * scale**2 % 17, 0, scale) + expected = Point(curve, 16, 0) if scalar % 2 else INFINITY + assert point * scalar == expected + assert scalar * point == expected + + +@pytest.mark.parametrize("x,y", [(3, -3), (8, 2), (8, -3)]) +def test_equal_projective_points_use_doubling(x, y): + curve = CurveFp(5, 0, 2) + point = PointJacobi(curve, 3, 2, 1) + assert point + PointJacobi(curve, x, y, 1) == point.double() + + +@pytest.mark.parametrize("representation", [Point, PointJacobi]) +def test_order_three_subgroup(representation): + curve = CurveFp(17, 0, 1) + members = [] + for x in range(17): + for y in range(17): + if curve.contains_point(x, y): + point = Point(curve, x, y) + if representation is PointJacobi: + point = PointJacobi.from_affine(point) + if point * 3 == INFINITY: + members.append((x, y)) + assert members == [(0, 1), (0, 16)] + assert INFINITY * 3 == INFINITY