@@ -303,7 +303,7 @@ static int secp256k1_ge_is_valid_var(const secp256k1_ge *a) {
303303 return secp256k1_fe_equal_var (& y2 , & x3 );
304304}
305305
306- static void secp256k1_gej_double_var (secp256k1_gej * r , const secp256k1_gej * a , secp256k1_fe * rzr ) {
306+ static SECP256K1_INLINE void secp256k1_gej_double_nonzero (secp256k1_gej * r , const secp256k1_gej * a ) {
307307 /* Operations: 3 mul, 4 sqr, 0 normalize, 12 mul_int/add/negate.
308308 *
309309 * Note that there is an implementation described at
@@ -312,29 +312,9 @@ static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, s
312312 * mainly because it requires more normalizations.
313313 */
314314 secp256k1_fe t1 ,t2 ,t3 ,t4 ;
315- /** For secp256k1, 2Q is infinity if and only if Q is infinity. This is because if 2Q = infinity,
316- * Q must equal -Q, or that Q.y == -(Q.y), or Q.y is 0. For a point on y^2 = x^3 + 7 to have
317- * y=0, x^3 must be -7 mod p. However, -7 has no cube root mod p.
318- *
319- * Having said this, if this function receives a point on a sextic twist, e.g. by
320- * a fault attack, it is possible for y to be 0. This happens for y^2 = x^3 + 6,
321- * since -6 does have a cube root mod p. For this point, this function will not set
322- * the infinity flag even though the point doubles to infinity, and the result
323- * point will be gibberish (z = 0 but infinity = 0).
324- */
325- r -> infinity = a -> infinity ;
326- if (r -> infinity ) {
327- if (rzr != NULL ) {
328- secp256k1_fe_set_int (rzr , 1 );
329- }
330- return ;
331- }
332315
333- if (rzr != NULL ) {
334- * rzr = a -> y ;
335- secp256k1_fe_normalize_weak (rzr );
336- secp256k1_fe_mul_int (rzr , 2 );
337- }
316+ VERIFY_CHECK (!secp256k1_gej_is_infinity (a ));
317+ r -> infinity = 0 ;
338318
339319 secp256k1_fe_mul (& r -> z , & a -> z , & a -> y );
340320 secp256k1_fe_mul_int (& r -> z , 2 ); /* Z' = 2*Y*Z (2) */
@@ -358,9 +338,32 @@ static void secp256k1_gej_double_var(secp256k1_gej *r, const secp256k1_gej *a, s
358338 secp256k1_fe_add (& r -> y , & t2 ); /* Y' = 36*X^3*Y^2 - 27*X^6 - 8*Y^4 (4) */
359339}
360340
361- static SECP256K1_INLINE void secp256k1_gej_double_nonzero (secp256k1_gej * r , const secp256k1_gej * a , secp256k1_fe * rzr ) {
362- VERIFY_CHECK (!secp256k1_gej_is_infinity (a ));
363- secp256k1_gej_double_var (r , a , rzr );
341+ static void secp256k1_gej_double_var (secp256k1_gej * r , const secp256k1_gej * a , secp256k1_fe * rzr ) {
342+ /** For secp256k1, 2Q is infinity if and only if Q is infinity. This is because if 2Q = infinity,
343+ * Q must equal -Q, or that Q.y == -(Q.y), or Q.y is 0. For a point on y^2 = x^3 + 7 to have
344+ * y=0, x^3 must be -7 mod p. However, -7 has no cube root mod p.
345+ *
346+ * Having said this, if this function receives a point on a sextic twist, e.g. by
347+ * a fault attack, it is possible for y to be 0. This happens for y^2 = x^3 + 6,
348+ * since -6 does have a cube root mod p. For this point, this function will not set
349+ * the infinity flag even though the point doubles to infinity, and the result
350+ * point will be gibberish (z = 0 but infinity = 0).
351+ */
352+ if (a -> infinity ) {
353+ r -> infinity = 1 ;
354+ if (rzr != NULL ) {
355+ secp256k1_fe_set_int (rzr , 1 );
356+ }
357+ return ;
358+ }
359+
360+ if (rzr != NULL ) {
361+ * rzr = a -> y ;
362+ secp256k1_fe_normalize_weak (rzr );
363+ secp256k1_fe_mul_int (rzr , 2 );
364+ }
365+
366+ secp256k1_gej_double_nonzero (r , a );
364367}
365368
366369static void secp256k1_gej_add_var (secp256k1_gej * r , const secp256k1_gej * a , const secp256k1_gej * b , secp256k1_fe * rzr ) {
0 commit comments