@@ -633,7 +633,7 @@ def __eq__(self, other):
633
633
"""
634
634
x1 , y1 , z1 = self .__coords
635
635
if other is INFINITY :
636
- return not y1 or not z1
636
+ return ( not x1 and not y1 ) or not z1
637
637
if isinstance (other , Point ):
638
638
x2 , y2 , z2 = other .x (), other .y (), 1
639
639
elif isinstance (other , PointJacobi ):
@@ -728,6 +728,7 @@ def to_affine(self):
728
728
return INFINITY
729
729
self .scale ()
730
730
x , y , z = self .__coords
731
+ assert z == 1
731
732
return Point (self .__curve , x , y , self .__order )
732
733
733
734
@staticmethod
@@ -802,7 +803,7 @@ def double(self):
802
803
803
804
X3 , Y3 , Z3 = self ._double (X1 , Y1 , Z1 , p , a )
804
805
805
- if not Y3 or not Z3 :
806
+ if not Y3 and not X3 :
806
807
return INFINITY
807
808
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
808
809
@@ -886,10 +887,10 @@ def __radd__(self, other):
886
887
887
888
def _add (self , X1 , Y1 , Z1 , X2 , Y2 , Z2 , p ):
888
889
"""add two points, select fastest method."""
889
- if not Y1 or not Z1 :
890
- return X2 , Y2 , Z2
891
- if not Y2 or not Z2 :
892
- return X1 , Y1 , Z1
890
+ if ( not X1 and not Y1 ) or not Z1 :
891
+ return X2 % p , Y2 % p , Z2 % p
892
+ if ( not X2 and not Y2 ) or not Z2 :
893
+ return X1 % p , Y1 % p , Z1 % p
893
894
if Z1 == Z2 :
894
895
if Z1 == 1 :
895
896
return self ._add_with_z_1 (X1 , Y1 , X2 , Y2 , p )
@@ -917,7 +918,7 @@ def __add__(self, other):
917
918
918
919
X3 , Y3 , Z3 = self ._add (X1 , Y1 , Z1 , X2 , Y2 , Z2 , p )
919
920
920
- if not Y3 or not Z3 :
921
+ if ( not X3 and not Y3 ) or not Z3 :
921
922
return INFINITY
922
923
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
923
924
@@ -972,7 +973,7 @@ def __mul__(self, other):
972
973
elif i > 0 :
973
974
X3 , Y3 , Z3 = _add (X3 , Y3 , Z3 , X2 , Y2 , 1 , p )
974
975
975
- if not Y3 or not Z3 :
976
+ if ( not X3 and not Y3 ) or not Z3 :
976
977
return INFINITY
977
978
978
979
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
@@ -1070,7 +1071,7 @@ def mul_add(self, self_mul, other, other_mul):
1070
1071
assert B > 0
1071
1072
X3 , Y3 , Z3 = _add (X3 , Y3 , Z3 , pApB_X , pApB_Y , pApB_Z , p )
1072
1073
1073
- if not Y3 or not Z3 :
1074
+ if ( not X3 and not Y3 ) or not Z3 :
1074
1075
return INFINITY
1075
1076
1076
1077
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
@@ -1220,7 +1221,12 @@ def leftmost_bit(x):
1220
1221
# From X9.62 D.3.2:
1221
1222
1222
1223
e3 = 3 * e
1223
- negative_self = Point (self .__curve , self .__x , - self .__y , self .__order )
1224
+ negative_self = Point (
1225
+ self .__curve ,
1226
+ self .__x ,
1227
+ (- self .__y ) % self .__curve .p (),
1228
+ self .__order ,
1229
+ )
1224
1230
i = leftmost_bit (e3 ) // 2
1225
1231
result = self
1226
1232
# print("Multiplying %s by %d (e3 = %d):" % (self, other, e3))
@@ -1247,7 +1253,6 @@ def __str__(self):
1247
1253
1248
1254
def double (self ):
1249
1255
"""Return a new point that is twice the old."""
1250
-
1251
1256
if self == INFINITY :
1252
1257
return INFINITY
1253
1258
@@ -1261,6 +1266,9 @@ def double(self):
1261
1266
* numbertheory .inverse_mod (2 * self .__y , p )
1262
1267
) % p
1263
1268
1269
+ if not l :
1270
+ return INFINITY
1271
+
1264
1272
x3 = (l * l - 2 * self .__x ) % p
1265
1273
y3 = (l * (self .__x - x3 ) - self .__y ) % p
1266
1274
0 commit comments