Skip to content

Commit faf3d50

Browse files
committed
ensure that the points are really the same before doubling
1 parent e559c4a commit faf3d50

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

src/ecdsa/ellipticcurve.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,14 +1185,15 @@ def __add__(self, other):
11851185
if self == INFINITY:
11861186
return other
11871187
assert self.__curve == other.__curve
1188-
if self.__x == other.__x:
1189-
if (self.__y + other.__y) % self.__curve.p() == 0:
1188+
1189+
p = self.__curve.p()
1190+
1191+
if (self.__x - other.__x) % p == 0:
1192+
if (self.__y + other.__y) % p == 0:
11901193
return INFINITY
11911194
else:
11921195
return self.double()
11931196

1194-
p = self.__curve.p()
1195-
11961197
l = (
11971198
(other.__y - self.__y)
11981199
* numbertheory.inverse_mod(other.__x - self.__x, p)

src/ecdsa/test_ellipticcurve.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ def setUpClass(cls):
146146
cls.c192 = CurveFp(p, -3, b)
147147
cls.p192 = Point(cls.c192, Gx, Gy, r)
148148

149+
def test_points_with_different_curves(self):
150+
with self.assertRaises(AssertionError):
151+
self.g_23 + self.p192
152+
153+
def test_add_point_to_negative(self):
154+
self.assertIs(self.g_23 + (-self.g_23), INFINITY)
155+
156+
def test_add_point_to_explicit_negative(self):
157+
self.assertIs(self.g_23 + Point(self.c_23, 13, -7), INFINITY)
158+
149159
def test_p192(self):
150160
# Checking against some sample computations presented
151161
# in X9.62:

0 commit comments

Comments
 (0)