@@ -532,11 +532,11 @@ static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const
532
532
/* Operations: 7 mul, 5 sqr, 24 add/cmov/half/mul_int/negate/normalize_weak/normalizes_to_zero */
533
533
secp256k1_fe zz , u1 , u2 , s1 , s2 , t , tt , m , n , q , rr ;
534
534
secp256k1_fe m_alt , rr_alt ;
535
- int infinity , degenerate ;
535
+ int degenerate ;
536
536
VERIFY_CHECK (!b -> infinity );
537
537
VERIFY_CHECK (a -> infinity == 0 || a -> infinity == 1 );
538
538
539
- /** In:
539
+ /* In:
540
540
* Eric Brier and Marc Joye, Weierstrass Elliptic Curves and Side-Channel Attacks.
541
541
* In D. Naccache and P. Paillier, Eds., Public Key Cryptography, vol. 2274 of Lecture Notes in Computer Science, pages 335-345. Springer-Verlag, 2002.
542
542
* we find as solution for a unified addition/doubling formula:
@@ -598,10 +598,9 @@ static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const
598
598
secp256k1_fe_negate (& m_alt , & u2 , 1 ); /* Malt = -X2*Z1^2 */
599
599
secp256k1_fe_mul (& tt , & u1 , & m_alt ); /* tt = -U1*U2 (2) */
600
600
secp256k1_fe_add (& rr , & tt ); /* rr = R = T^2-U1*U2 (3) */
601
- /** If lambda = R/M = 0/0 we have a problem (except in the "trivial"
602
- * case that Z = z1z2 = 0, and this is special-cased later on). */
603
- degenerate = secp256k1_fe_normalizes_to_zero (& m ) &
604
- secp256k1_fe_normalizes_to_zero (& rr );
601
+ /* If lambda = R/M = R/0 we have a problem (except in the "trivial"
602
+ * case that Z = z1z2 = 0, and this is special-cased later on). */
603
+ degenerate = secp256k1_fe_normalizes_to_zero (& m );
605
604
/* This only occurs when y1 == -y2 and x1^3 == x2^3, but x1 != x2.
606
605
* This means either x1 == beta*x2 or beta*x1 == x2, where beta is
607
606
* a nontrivial cube root of one. In either case, an alternate
@@ -613,7 +612,7 @@ static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const
613
612
614
613
secp256k1_fe_cmov (& rr_alt , & rr , !degenerate );
615
614
secp256k1_fe_cmov (& m_alt , & m , !degenerate );
616
- /* Now Ralt / Malt = lambda and is guaranteed not to be 0/ 0.
615
+ /* Now Ralt / Malt = lambda and is guaranteed not to be Ralt / 0.
617
616
* From here on out Ralt and Malt represent the numerator
618
617
* and denominator of lambda; R and M represent the explicit
619
618
* expressions x1^2 + x2^2 + x1x2 and y1 + y2. */
@@ -628,7 +627,6 @@ static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const
628
627
secp256k1_fe_cmov (& n , & m , degenerate ); /* n = M^3 * Malt (2) */
629
628
secp256k1_fe_sqr (& t , & rr_alt ); /* t = Ralt^2 (1) */
630
629
secp256k1_fe_mul (& r -> z , & a -> z , & m_alt ); /* r->z = Z3 = Malt*Z (1) */
631
- infinity = secp256k1_fe_normalizes_to_zero (& r -> z ) & ~a -> infinity ;
632
630
secp256k1_fe_add (& t , & q ); /* t = Ralt^2 + Q (2) */
633
631
r -> x = t ; /* r->x = X3 = Ralt^2 + Q (2) */
634
632
secp256k1_fe_mul_int (& t , 2 ); /* t = 2*X3 (4) */
@@ -638,11 +636,28 @@ static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const
638
636
secp256k1_fe_negate (& r -> y , & t , 3 ); /* r->y = -(Ralt*(2*X3 + Q) + M^3*Malt) (4) */
639
637
secp256k1_fe_half (& r -> y ); /* r->y = Y3 = -(Ralt*(2*X3 + Q) + M^3*Malt)/2 (3) */
640
638
641
- /** In case a->infinity == 1, replace r with (b->x, b->y, 1). */
639
+ /* In case a->infinity == 1, replace r with (b->x, b->y, 1). */
642
640
secp256k1_fe_cmov (& r -> x , & b -> x , a -> infinity );
643
641
secp256k1_fe_cmov (& r -> y , & b -> y , a -> infinity );
644
642
secp256k1_fe_cmov (& r -> z , & secp256k1_fe_one , a -> infinity );
645
- r -> infinity = infinity ;
643
+
644
+ /* Set r->infinity if r->z is 0.
645
+ *
646
+ * If a->infinity is set, then r->infinity = (r->z == 0) = (1 == 0) = false,
647
+ * which is correct because the function assumes that b is not infinity.
648
+ *
649
+ * Now assume !a->infinity. This implies Z = Z1 != 0.
650
+ *
651
+ * Case y1 = -y2:
652
+ * In this case we could have a = -b, namely if x1 = x2.
653
+ * We have degenerate = true, r->z = (x1 - x2) * Z.
654
+ * Then r->infinity = ((x1 - x2)Z == 0) = (x1 == x2) = (a == -b).
655
+ *
656
+ * Case y1 != -y2:
657
+ * In this case, we can't have a = -b.
658
+ * We have degenerate = false, r->z = (y1 + y2) * Z.
659
+ * Then r->infinity = ((y1 + y2)Z == 0) = (y1 == -y2) = false. */
660
+ r -> infinity = secp256k1_fe_normalizes_to_zero (& r -> z );
646
661
}
647
662
648
663
static void secp256k1_gej_rescale (secp256k1_gej * r , const secp256k1_fe * s ) {
0 commit comments