@@ -278,49 +278,42 @@ def double(self):
278278 return INFINITY
279279 return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
280280
281- def _add_with_z_1 (self , X1 , Y1 , X2 , Y2 ):
281+ def _add_with_z_1 (self , X1 , Y1 , X2 , Y2 , p ):
282282 """add points when both Z1 and Z2 equal 1"""
283283 # after:
284284 # http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-mmadd-2007-bl
285- p = self .__curve .p ()
286285 H = X2 - X1
287286 HH = H * H
288287 I = 4 * HH % p
289288 J = H * I
290289 r = 2 * (Y2 - Y1 )
291290 if not H and not r :
292- return self .double ( )
291+ return self ._double_with_z_1 ( X1 , Y1 , p , self . __curve . a () )
293292 V = X1 * I
294293 X3 = (r ** 2 - J - 2 * V ) % p
295294 Y3 = (r * (V - X3 ) - 2 * Y1 * J ) % p
296295 Z3 = 2 * H % p
297- if not Y3 or not Z3 :
298- return INFINITY
299- return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
296+ return X3 , Y3 , Z3
300297
301- def _add_with_z_eq (self , X1 , Y1 , Z1 , X2 , Y2 ):
298+ def _add_with_z_eq (self , X1 , Y1 , Z1 , X2 , Y2 , p ):
302299 """add points when Z1 == Z2"""
303300 # after:
304301 # http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-zadd-2007-m
305- p = self .__curve .p ()
306302 A = (X2 - X1 )** 2 % p
307303 B = X1 * A % p
308304 C = X2 * A
309305 D = (Y2 - Y1 )** 2 % p
310306 if not A and not D :
311- return self .double ( )
307+ return self ._double ( X1 , Y1 , Z1 , p , self . __curve . a () )
312308 X3 = (D - B - C ) % p
313309 Y3 = ((Y2 - Y1 ) * (B - X3 ) - Y1 * (C - B )) % p
314310 Z3 = Z1 * (X2 - X1 ) % p
315- if not Y3 or not Z3 :
316- return INFINITY
317- return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
311+ return X3 , Y3 , Z3
318312
319- def _add_with_z2_1 (self , X1 , Y1 , Z1 , X2 , Y2 ):
313+ def _add_with_z2_1 (self , X1 , Y1 , Z1 , X2 , Y2 , p ):
320314 """add points when Z2 == 1"""
321315 # after:
322316 # http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-madd-2007-bl
323- p = self .__curve .p ()
324317 Z1Z1 = Z1 * Z1 % p
325318 U2 , S2 = X2 * Z1Z1 % p , Y2 * Z1 * Z1Z1 % p
326319 H = (U2 - X1 ) % p
@@ -329,20 +322,17 @@ def _add_with_z2_1(self, X1, Y1, Z1, X2, Y2):
329322 J = H * I
330323 r = 2 * (S2 - Y1 ) % p
331324 if not r and not H :
332- return self .double ( )
325+ return self ._double_with_z_1 ( X2 , Y2 , p , self . __curve . a () )
333326 V = X1 * I
334327 X3 = (r * r - J - 2 * V ) % p
335328 Y3 = (r * (V - X3 ) - 2 * Y1 * J ) % p
336329 Z3 = ((Z1 + H )** 2 - Z1Z1 - HH ) % p
337- if not Y3 or not Z3 :
338- return INFINITY
339- return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
330+ return X3 , Y3 , Z3
340331
341- def _add_with_z_ne (self , X1 , Y1 , Z1 , X2 , Y2 , Z2 ):
332+ def _add_with_z_ne (self , X1 , Y1 , Z1 , X2 , Y2 , Z2 , p ):
342333 """add points with arbitrary z"""
343334 # after:
344335 # http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian.html#addition-add-2007-bl
345- p = self .__curve .p ()
346336 Z1Z1 = Z1 * Z1 % p
347337 Z2Z2 = Z2 * Z2 % p
348338 U1 = X1 * Z2Z2 % p
@@ -354,21 +344,30 @@ def _add_with_z_ne(self, X1, Y1, Z1, X2, Y2, Z2):
354344 J = H * I % p
355345 r = 2 * (S2 - S1 ) % p
356346 if not H and not r :
357- return self .double ( )
347+ return self ._double ( X1 , Y1 , Z1 , p , self . __curve . a () )
358348 V = U1 * I
359349 X3 = (r * r - J - 2 * V ) % p
360350 Y3 = (r * (V - X3 ) - 2 * S1 * J ) % p
361351 Z3 = ((Z1 + Z2 )** 2 - Z1Z1 - Z2Z2 ) * H % p
362352
363- if not Y3 or not Z3 :
364- return INFINITY
365-
366- return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
353+ return X3 , Y3 , Z3
367354
368355 def __radd__ (self , other ):
369356 """Add other to self."""
370357 return self + other
371358
359+ def _add (self , X1 , Y1 , Z1 , X2 , Y2 , Z2 , p ):
360+ """add two points, select fastest method."""
361+ if Z1 == Z2 :
362+ if Z1 == 1 :
363+ return self ._add_with_z_1 (X1 , Y1 , X2 , Y2 , p )
364+ return self ._add_with_z_eq (X1 , Y1 , Z1 , X2 , Y2 , p )
365+ if Z1 == 1 :
366+ return self ._add_with_z2_1 (X2 , Y2 , Z2 , X1 , Y1 , p )
367+ if Z2 == 1 :
368+ return self ._add_with_z2_1 (X1 , Y1 , Z1 , X2 , Y2 , p )
369+ return self ._add_with_z_ne (X1 , Y1 , Z1 , X2 , Y2 , Z2 , p )
370+
372371 def __add__ (self , other ):
373372 """Add two points on elliptic curve."""
374373 if self == INFINITY :
@@ -380,17 +379,14 @@ def __add__(self, other):
380379 if self .__curve != other .__curve :
381380 raise ValueError ("The other point is on different curve" )
382381
382+ p = self .__curve .p ()
383383 X1 , Y1 , Z1 = self .__x , self .__y , self .__z
384384 X2 , Y2 , Z2 = other .__x , other .__y , other .__z
385- if Z1 == Z2 :
386- if Z1 == 1 :
387- return self ._add_with_z_1 (X1 , Y1 , X2 , Y2 )
388- return self ._add_with_z_eq (X1 , Y1 , Z1 , X2 , Y2 )
389- if Z1 == 1 :
390- return self ._add_with_z2_1 (X2 , Y2 , Z2 , X1 , Y1 )
391- if Z2 == 1 :
392- return self ._add_with_z2_1 (X1 , Y1 , Z1 , X2 , Y2 )
393- return self ._add_with_z_ne (X1 , Y1 , Z1 , X2 , Y2 , Z2 )
385+ X3 , Y3 , Z3 = self ._add (X1 , Y1 , Z1 , X2 , Y2 , Z2 , p )
386+
387+ if not Y3 or not Z3 :
388+ return INFINITY
389+ return PointJacobi (self .__curve , X3 , Y3 , Z3 , self .__order )
394390
395391 def __rmul__ (self , other ):
396392 """Multiply point by an integer."""
0 commit comments