Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/ecdsa/ellipticcurve.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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

Expand Down
50 changes: 50 additions & 0 deletions src/ecdsa/test_exceptional_points.py
Original file line number Diff line number Diff line change
@@ -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