Skip to content

Commit c736dcd

Browse files
committed
tests for x=0 and y=0 for points on curve
1 parent 0f2e764 commit c736dcd

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

src/ecdsa/ellipticcurve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ def double(self):
803803

804804
X3, Y3, Z3 = self._double(X1, Y1, Z1, p, a)
805805

806-
if not Y3 and not X3:
806+
if (not X3 and not Y3) or not Z3:
807807
return INFINITY
808808
return PointJacobi(self.__curve, X3, Y3, Z3, self.__order)
809809

@@ -941,7 +941,7 @@ def _mul_precompute(self, other):
941941
else:
942942
other //= 2
943943

944-
if not Y3 or not Z3:
944+
if (not X3 and not Y3) or not Z3:
945945
return INFINITY
946946
return PointJacobi(self.__curve, X3, Y3, Z3, self.__order)
947947

src/ecdsa/test_jacobi.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,33 @@ def test_compare_non_zero_with_infinity(self):
195195

196196
self.assertNotEqual(pj, INFINITY)
197197

198+
def test_compare_non_zero_bad_scale_with_infinity(self):
199+
pj = PointJacobi(curve_256, 1, 1, 0)
200+
self.assertEqual(pj, INFINITY)
201+
202+
def test_eq_x_0_on_curve_with_infinity(self):
203+
c_23 = CurveFp(23, 1, 1)
204+
pj = PointJacobi(c_23, 0, 1, 1)
205+
206+
self.assertTrue(c_23.contains_point(0, 1))
207+
208+
self.assertNotEqual(pj, INFINITY)
209+
210+
def test_eq_y_0_on_curve_with_infinity(self):
211+
c_23 = CurveFp(23, 1, 1)
212+
pj = PointJacobi(c_23, 4, 0, 1)
213+
214+
self.assertTrue(c_23.contains_point(4, 0))
215+
216+
self.assertNotEqual(pj, INFINITY)
217+
218+
def test_eq_with_same_x_different_y(self):
219+
c_23 = CurveFp(23, 1, 1)
220+
p_a = PointJacobi(c_23, 0, 22, 1)
221+
p_b = PointJacobi(c_23, 0, 1, 1)
222+
223+
self.assertNotEqual(p_a, p_b)
224+
198225
def test_compare_zero_point_with_infinity(self):
199226
pj = PointJacobi(curve_256, 0, 0, 1)
200227

@@ -671,6 +698,41 @@ def test_add_to_infinity(self):
671698
self.assertEqual(p3, INFINITY)
672699
self.assertIs(p3, INFINITY)
673700

701+
def test_mul_to_x_0(self):
702+
c_23 = CurveFp(23, 1, 1)
703+
p = PointJacobi(c_23, 9, 7, 1)
704+
705+
p2 = p * 13
706+
self.assertEqual((p2.x(), p2.y()), (0, 22))
707+
708+
def test_mul_to_y_0(self):
709+
c_23 = CurveFp(23, 1, 1)
710+
p = PointJacobi(c_23, 9, 7, 1)
711+
712+
p2 = p * 14
713+
self.assertEqual((p2.x(), p2.y()), (4, 0))
714+
715+
def test_add_to_x_0(self):
716+
c_23 = CurveFp(23, 1, 1)
717+
p = PointJacobi(c_23, 9, 7, 1)
718+
719+
p2 = p * 12 + p
720+
self.assertEqual((p2.x(), p2.y()), (0, 22))
721+
722+
def test_add_to_y_0(self):
723+
c_23 = CurveFp(23, 1, 1)
724+
p = PointJacobi(c_23, 9, 7, 1)
725+
726+
p2 = p * 13 + p
727+
self.assertEqual((p2.x(), p2.y()), (4, 0))
728+
729+
def test_add_diff_z_to_infinity(self):
730+
c_23 = CurveFp(23, 1, 1)
731+
p = PointJacobi(c_23, 9, 7, 1)
732+
733+
c = p * 20 + p * 8
734+
self.assertIs(c, INFINITY)
735+
674736
def test_pickle(self):
675737
pj = PointJacobi(curve=CurveFp(23, 1, 1, 1), x=2, y=3, z=1, order=1)
676738
self.assertEqual(pickle.loads(pickle.dumps(pj)), pj)

0 commit comments

Comments
 (0)