Skip to content

Commit ab07a0c

Browse files
committed
move numeric code out of __add__
don't treat the universal code for point addition specially
1 parent 58eef88 commit ab07a0c

File tree

1 file changed

+28
-25
lines changed

1 file changed

+28
-25
lines changed

src/ecdsa/ellipticcurve.py

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,33 @@ def _add_with_z2_1(self, X1, Y1, Z1, X2, Y2):
333333
return INFINITY
334334
return PointJacobi(self.__curve, X3, Y3, Z3, self.__order)
335335

336+
def _add_with_z_ne(self, X1, Y1, Z1, X2, Y2, Z2):
337+
"""add points with arbitrary z"""
338+
# after:
339+
# http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-add-2007-bl
340+
p = self.__curve.p()
341+
Z1Z1 = Z1 * Z1 % p
342+
Z2Z2 = Z2 * Z2 % p
343+
U1 = X1 * Z2Z2 % p
344+
U2 = X2 * Z1Z1 % p
345+
S1 = Y1 * Z2 * Z2Z2 % p
346+
S2 = Y2 * Z1 * Z1Z1 % p
347+
H = U2 - U1
348+
I = 4 * H * H % p
349+
J = H * I % p
350+
r = 2 * (S2 - S1) % p
351+
if not H and not r:
352+
return self.double()
353+
V = U1 * I
354+
X3 = (r * r - J - 2 * V) % p
355+
Y3 = (r * (V - X3) - 2 * S1 * J) % p
356+
Z3 = ((Z1 + Z2)**2 - Z1Z1 - Z2Z2) * H % p
357+
358+
if not Y3 or not Z3:
359+
return INFINITY
360+
361+
return PointJacobi(self.__curve, X3, Y3, Z3, self.__order)
362+
336363
def __radd__(self, other):
337364
"""Add other to self."""
338365
return self + other
@@ -348,7 +375,6 @@ def __add__(self, other):
348375
if self.__curve != other.__curve:
349376
raise ValueError("The other point is on different curve")
350377

351-
p = self.__curve.p()
352378
X1, Y1, Z1 = self.__x, self.__y, self.__z
353379
X2, Y2, Z2 = other.__x, other.__y, other.__z
354380
if Z1 == Z2:
@@ -359,30 +385,7 @@ def __add__(self, other):
359385
return self._add_with_z2_1(X2, Y2, Z2, X1, Y1)
360386
if Z2 == 1:
361387
return self._add_with_z2_1(X1, Y1, Z1, X2, Y2)
362-
363-
# after:
364-
# http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-add-2007-bl
365-
Z1Z1 = Z1 * Z1 % p
366-
Z2Z2 = Z2 * Z2 % p
367-
U1 = X1 * Z2Z2 % p
368-
U2 = X2 * Z1Z1 % p
369-
S1 = Y1 * Z2 * Z2Z2 % p
370-
S2 = Y2 * Z1 * Z1Z1 % p
371-
H = U2 - U1
372-
I = 4 * H * H % p
373-
J = H * I % p
374-
r = 2 * (S2 - S1) % p
375-
if not H and not r:
376-
return self.double()
377-
V = U1 * I
378-
X3 = (r * r - J - 2 * V) % p
379-
Y3 = (r * (V - X3) - 2 * S1 * J) % p
380-
Z3 = ((Z1 + Z2)**2 - Z1Z1 - Z2Z2) * H % p
381-
382-
if not Y3 or not Z3:
383-
return INFINITY
384-
385-
return PointJacobi(self.__curve, X3, Y3, Z3, self.__order)
388+
return self._add_with_z_ne(X1, Y1, Z1, X2, Y2, Z2)
386389

387390
def __rmul__(self, other):
388391
"""Multiply point by an integer."""

0 commit comments

Comments
 (0)