@@ -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 x1 and not y1 ) or not z1
636
+ return not z1
637
637
if isinstance (other , Point ):
638
638
x2 , y2 , z2 = other .x (), other .y (), 1
639
639
elif isinstance (other , PointJacobi ):
@@ -723,8 +723,9 @@ def scale(self):
723
723
724
724
def to_affine (self ):
725
725
"""Return point in affine form."""
726
- _ , y , z = self .__coords
727
- if not y or not z :
726
+ _ , _ , z = self .__coords
727
+ p = self .__curve .p ()
728
+ if not (z % p ):
728
729
return INFINITY
729
730
self .scale ()
730
731
x , y , z = self .__coords
@@ -760,7 +761,7 @@ def _double_with_z_1(self, X1, Y1, p, a):
760
761
# http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-mdbl-2007-bl
761
762
XX , YY = X1 * X1 % p , Y1 * Y1 % p
762
763
if not YY :
763
- return 0 , 0 , 1
764
+ return 0 , 0 , 0
764
765
YYYY = YY * YY % p
765
766
S = 2 * ((X1 + YY ) ** 2 - XX - YYYY ) % p
766
767
M = 3 * XX + a
@@ -774,13 +775,13 @@ def _double(self, X1, Y1, Z1, p, a):
774
775
"""Add a point to itself, arbitrary z."""
775
776
if Z1 == 1 :
776
777
return self ._double_with_z_1 (X1 , Y1 , p , a )
777
- if not Y1 or not Z1 :
778
- return 0 , 0 , 1
778
+ if not Z1 :
779
+ return 0 , 0 , 0
779
780
# after:
780
781
# http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl
781
782
XX , YY = X1 * X1 % p , Y1 * Y1 % p
782
783
if not YY :
783
- return 0 , 0 , 1
784
+ return 0 , 0 , 0
784
785
YYYY = YY * YY % p
785
786
ZZ = Z1 * Z1 % p
786
787
S = 2 * ((X1 + YY ) ** 2 - XX - YYYY ) % p
@@ -796,14 +797,14 @@ def double(self):
796
797
"""Add a point to itself."""
797
798
X1 , Y1 , Z1 = self .__coords
798
799
799
- if not Y1 :
800
+ if not Z1 :
800
801
return INFINITY
801
802
802
803
p , a = self .__curve .p (), self .__curve .a ()
803
804
804
805
X3 , Y3 , Z3 = self ._double (X1 , Y1 , Z1 , p , a )
805
806
806
- if not Y3 and not X3 :
807
+ if not Z3 :
807
808
return INFINITY
808
809
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
809
810
@@ -887,9 +888,9 @@ def __radd__(self, other):
887
888
888
889
def _add (self , X1 , Y1 , Z1 , X2 , Y2 , Z2 , p ):
889
890
"""add two points, select fastest method."""
890
- if ( not X1 and not Y1 ) or not Z1 :
891
+ if not Z1 :
891
892
return X2 % p , Y2 % p , Z2 % p
892
- if ( not X2 and not Y2 ) or not Z2 :
893
+ if not Z2 :
893
894
return X1 % p , Y1 % p , Z1 % p
894
895
if Z1 == Z2 :
895
896
if Z1 == 1 :
@@ -918,7 +919,7 @@ def __add__(self, other):
918
919
919
920
X3 , Y3 , Z3 = self ._add (X1 , Y1 , Z1 , X2 , Y2 , Z2 , p )
920
921
921
- if ( not X3 and not Y3 ) or not Z3 :
922
+ if not Z3 :
922
923
return INFINITY
923
924
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
924
925
@@ -928,7 +929,7 @@ def __rmul__(self, other):
928
929
929
930
def _mul_precompute (self , other ):
930
931
"""Multiply point by integer with precomputation table."""
931
- X3 , Y3 , Z3 , p = 0 , 0 , 1 , self .__curve .p ()
932
+ X3 , Y3 , Z3 , p = 0 , 0 , 0 , self .__curve .p ()
932
933
_add = self ._add
933
934
for X2 , Y2 in self .__precompute :
934
935
if other % 2 :
@@ -941,7 +942,7 @@ def _mul_precompute(self, other):
941
942
else :
942
943
other //= 2
943
944
944
- if not Y3 or not Z3 :
945
+ if not Z3 :
945
946
return INFINITY
946
947
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
947
948
@@ -960,7 +961,7 @@ def __mul__(self, other):
960
961
961
962
self = self .scale ()
962
963
X2 , Y2 , _ = self .__coords
963
- X3 , Y3 , Z3 = 0 , 0 , 1
964
+ X3 , Y3 , Z3 = 0 , 0 , 0
964
965
p , a = self .__curve .p (), self .__curve .a ()
965
966
_double = self ._double
966
967
_add = self ._add
@@ -973,7 +974,7 @@ def __mul__(self, other):
973
974
elif i > 0 :
974
975
X3 , Y3 , Z3 = _add (X3 , Y3 , Z3 , X2 , Y2 , 1 , p )
975
976
976
- if ( not X3 and not Y3 ) or not Z3 :
977
+ if not Z3 :
977
978
return INFINITY
978
979
979
980
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
@@ -1002,7 +1003,7 @@ def mul_add(self, self_mul, other, other_mul):
1002
1003
other_mul = other_mul % self .__order
1003
1004
1004
1005
# (X3, Y3, Z3) is the accumulator
1005
- X3 , Y3 , Z3 = 0 , 0 , 1
1006
+ X3 , Y3 , Z3 = 0 , 0 , 0
1006
1007
p , a = self .__curve .p (), self .__curve .a ()
1007
1008
1008
1009
# as we have 6 unique points to work with, we can't scale all of them,
@@ -1026,7 +1027,7 @@ def mul_add(self, self_mul, other, other_mul):
1026
1027
# when the self and other sum to infinity, we need to add them
1027
1028
# one by one to get correct result but as that's very unlikely to
1028
1029
# happen in regular operation, we don't need to optimise this case
1029
- if not pApB_Y or not pApB_Z :
1030
+ if not pApB_Z :
1030
1031
return self * self_mul + other * other_mul
1031
1032
1032
1033
# gmp object creation has cumulatively higher overhead than the
@@ -1071,7 +1072,7 @@ def mul_add(self, self_mul, other, other_mul):
1071
1072
assert B > 0
1072
1073
X3 , Y3 , Z3 = _add (X3 , Y3 , Z3 , pApB_X , pApB_Y , pApB_Z , p )
1073
1074
1074
- if ( not X3 and not Y3 ) or not Z3 :
1075
+ if not Z3 :
1075
1076
return INFINITY
1076
1077
1077
1078
return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
@@ -1155,6 +1156,8 @@ def __eq__(self, other):
1155
1156
1156
1157
Note: only points that lay on the same curve can be equal.
1157
1158
"""
1159
+ if other is INFINITY :
1160
+ return self .__x is None or self .__y is None
1158
1161
if isinstance (other , Point ):
1159
1162
return (
1160
1163
self .__curve == other .__curve
0 commit comments