Skip to content

Commit d4c2387

Browse files
committed
use tuples for calculation of double
1 parent ab07a0c commit d4c2387

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/ecdsa/ellipticcurve.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,31 +227,31 @@ def from_affine(point, generator=False):
227227
# similarly, sometimes the `% p` is skipped if it makes the calculation
228228
# faster and the result of calculation is later reduced modulo `p`
229229

230-
def _double_with_z_1(self, X1, Y1):
230+
def _double_with_z_1(self, X1, Y1, p, a):
231231
"""Add a point to itself with z == 1."""
232232
# after:
233233
# http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-mdbl-2007-bl
234-
p, a = self.__curve.p(), self.__curve.a()
235234
XX, YY = X1 * X1 % p, Y1 * Y1 % p
236235
if not YY:
237-
return INFINITY
236+
return 0, 0, 1
238237
YYYY = YY * YY % p
239238
S = 2 * ((X1 + YY)**2 - XX - YYYY) % p
240239
M = 3 * XX + a
241240
T = (M * M - 2 * S) % p
242241
# X3 = T
243242
Y3 = (M * (S - T) - 8 * YYYY) % p
244243
Z3 = 2 * Y1 % p
245-
return PointJacobi(self.__curve, T, Y3, Z3, self.__order)
244+
return T, Y3, Z3
246245

247-
def _double(self, X1, Y1, Z1):
246+
def _double(self, X1, Y1, Z1, p, a):
248247
"""Add a point to itself, arbitrary z."""
248+
if Z1 == 1:
249+
return self._double_with_z_1(X1, Y1, p, a)
249250
# after:
250251
# http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#doubling-dbl-2007-bl
251-
p, a = self.__curve.p(), self.__curve.a()
252252
XX, YY = X1 * X1 % p, Y1 * Y1 % p
253253
if not YY:
254-
return INFINITY
254+
return 0, 0, 1
255255
YYYY = YY * YY % p
256256
ZZ = Z1 * Z1 % p
257257
S = 2 * ((X1 + YY)**2 - XX - YYYY) % p
@@ -261,17 +261,22 @@ def _double(self, X1, Y1, Z1):
261261
Y3 = (M * (S - T) - 8 * YYYY) % p
262262
Z3 = ((Y1 + Z1)**2 - YY - ZZ) % p
263263

264-
return PointJacobi(self.__curve, T, Y3, Z3, self.__order)
264+
return T, Y3, Z3
265265

266266
def double(self):
267267
"""Add a point to itself."""
268268
if not self.__y:
269269
return INFINITY
270270

271+
p, a = self.__curve.p(), self.__curve.a()
272+
271273
X1, Y1, Z1 = self.__x, self.__y, self.__z
272-
if Z1 == 1:
273-
return self._double_with_z_1(X1, Y1)
274-
return self._double(X1, Y1, Z1)
274+
275+
X3, Y3, Z3 = self._double(X1, Y1, Z1, p, a)
276+
277+
if not Y3:
278+
return INFINITY
279+
return PointJacobi(self.__curve, X3, Y3, Z3, self.__order)
275280

276281
def _add_with_z_1(self, X1, Y1, X2, Y2):
277282
"""add points when both Z1 and Z2 equal 1"""

0 commit comments

Comments
 (0)