Skip to content

Commit 55e7fc3

Browse files
committed
Perf. improvement in _gej_add_ge
- Avoid one weak normalization - Change one full normalization to weak - Avoid unnecessary fe assignment - Update magnitude annotations
1 parent 17f7148 commit 55e7fc3

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/group_impl.h

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static void secp256k1_gej_add_zinv_var(secp256k1_gej_t *r, const secp256k1_gej_t
461461

462462

463463
static void secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, const secp256k1_ge_t *b) {
464-
/* Operations: 7 mul, 5 sqr, 5 normalize, 17 mul_int/add/negate/cmov */
464+
/* Operations: 7 mul, 5 sqr, 4 normalize, 17 mul_int/add/negate/cmov */
465465
static const secp256k1_fe_t fe_1 = SECP256K1_FE_CONST(0, 0, 0, 0, 0, 0, 0, 1);
466466
secp256k1_fe_t zz, u1, u2, s1, s2, z, t, tt, m, n, q, rr;
467467
secp256k1_fe_t m_alt, rr_alt;
@@ -557,23 +557,21 @@ static void secp256k1_gej_add_ge(secp256k1_gej_t *r, const secp256k1_gej_t *a, c
557557
* so M^3 * Malt is either Malt^4 (which is computed by squaring), or
558558
* zero (which is "computed" by cmov). So the cost is one squaring
559559
* versus two multiplications. */
560-
secp256k1_fe_sqr(&n, &n); /* n = M^3 * Malt (1) */
561-
secp256k1_fe_cmov(&n, &m, degenerate);
562-
secp256k1_fe_normalize_weak(&n);
560+
secp256k1_fe_sqr(&n, &n);
561+
secp256k1_fe_cmov(&n, &m, degenerate); /* n = M^3 * Malt (2) */
563562
secp256k1_fe_sqr(&t, &rr_alt); /* t = Ralt^2 (1) */
564563
secp256k1_fe_mul(&r->z, &m_alt, &z); /* r->z = Malt*Z (1) */
565564
infinity = secp256k1_fe_normalizes_to_zero(&r->z) * (1 - a->infinity);
566565
secp256k1_fe_mul_int(&r->z, 2); /* r->z = Z3 = 2*Malt*Z (2) */
567-
r->x = t; /* r->x = Ralt^2 (1) */
568566
secp256k1_fe_negate(&q, &q, 1); /* q = -Q (2) */
569-
secp256k1_fe_add(&r->x, &q); /* r->x = Ralt^2-Q (3) */
570-
secp256k1_fe_normalize(&r->x);
571-
t = r->x;
567+
secp256k1_fe_add(&t, &q); /* t = Ralt^2-Q (3) */
568+
secp256k1_fe_normalize_weak(&t);
569+
r->x = t; /* r->x = Ralt^2-Q (1) */
572570
secp256k1_fe_mul_int(&t, 2); /* t = 2*x3 (2) */
573-
secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (8) */
571+
secp256k1_fe_add(&t, &q); /* t = 2*x3 - Q: (4) */
574572
secp256k1_fe_mul(&t, &t, &rr_alt); /* t = Ralt*(2*x3 - Q) (1) */
575-
secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (2) */
576-
secp256k1_fe_negate(&r->y, &t, 2); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (3) */
573+
secp256k1_fe_add(&t, &n); /* t = Ralt*(2*x3 - Q) + M^3*Malt (3) */
574+
secp256k1_fe_negate(&r->y, &t, 3); /* r->y = Ralt*(Q - 2x3) - M^3*Malt (4) */
577575
secp256k1_fe_normalize_weak(&r->y);
578576
secp256k1_fe_mul_int(&r->x, 4); /* r->x = X3 = 4*(Ralt^2-Q) */
579577
secp256k1_fe_mul_int(&r->y, 4); /* r->y = Y3 = 4*Ralt*(Q - 2x3) - 4*M^3*Malt (4) */

0 commit comments

Comments
 (0)