Skip to content

Commit da099be

Browse files
committed
Simpler and faster ecdh skew fixup
1 parent 09971a3 commit da099be

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

src/ecmult_const_impl.h

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -218,36 +218,21 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
218218

219219
{
220220
/* Correct for wNAF skew */
221-
secp256k1_ge correction = *a;
222-
secp256k1_ge_storage correction_1_stor;
223-
secp256k1_ge_storage correction_lam_stor;
224-
secp256k1_ge_storage a2_stor;
225-
secp256k1_gej tmpj;
226-
secp256k1_gej_set_ge(&tmpj, &correction);
227-
secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
228-
secp256k1_ge_set_gej(&correction, &tmpj);
229-
secp256k1_ge_to_storage(&correction_1_stor, a);
230-
if (size > 128) {
231-
secp256k1_ge_to_storage(&correction_lam_stor, a);
232-
}
233-
secp256k1_ge_to_storage(&a2_stor, &correction);
234-
235-
/* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
236-
secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
237-
if (size > 128) {
238-
secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
239-
}
221+
secp256k1_gej tmp;
222+
secp256k1_ge a_1;
240223

241-
/* Apply the correction */
242-
secp256k1_ge_from_storage(&correction, &correction_1_stor);
243-
secp256k1_ge_neg(&correction, &correction);
244-
secp256k1_gej_add_ge(r, r, &correction);
224+
secp256k1_ge_neg(&a_1, a);
225+
secp256k1_gej_add_ge(r, r, &a_1);
226+
secp256k1_gej_add_ge(&tmp, r, &a_1);
227+
secp256k1_gej_cmov(r, &tmp, skew_1 == 2);
245228

246229
if (size > 128) {
247-
secp256k1_ge_from_storage(&correction, &correction_lam_stor);
248-
secp256k1_ge_neg(&correction, &correction);
249-
secp256k1_ge_mul_lambda(&correction, &correction);
250-
secp256k1_gej_add_ge(r, r, &correction);
230+
secp256k1_ge a_lam;
231+
secp256k1_ge_mul_lambda(&a_lam, &a_1);
232+
233+
secp256k1_gej_add_ge(r, r, &a_lam);
234+
secp256k1_gej_add_ge(&tmp, r, &a_lam);
235+
secp256k1_gej_cmov(r, &tmp, skew_lam == 2);
251236
}
252237
}
253238
}

src/group.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ static void secp256k1_ge_to_storage(secp256k1_ge_storage *r, const secp256k1_ge
124124
/** Convert a group element back from the storage type. */
125125
static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storage *a);
126126

127+
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
128+
static void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag);
129+
127130
/** If flag is true, set *r equal to *a; otherwise leave it. Constant-time. Both *r and *a must be initialized.*/
128131
static void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag);
129132

src/group_impl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,14 @@ static void secp256k1_ge_from_storage(secp256k1_ge *r, const secp256k1_ge_storag
642642
r->infinity = 0;
643643
}
644644

645+
static SECP256K1_INLINE void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag) {
646+
secp256k1_fe_cmov(&r->x, &a->x, flag);
647+
secp256k1_fe_cmov(&r->y, &a->y, flag);
648+
secp256k1_fe_cmov(&r->z, &a->z, flag);
649+
650+
r->infinity ^= (r->infinity ^ a->infinity) & flag;
651+
}
652+
645653
static SECP256K1_INLINE void secp256k1_ge_storage_cmov(secp256k1_ge_storage *r, const secp256k1_ge_storage *a, int flag) {
646654
secp256k1_fe_storage_cmov(&r->x, &a->x, flag);
647655
secp256k1_fe_storage_cmov(&r->y, &a->y, flag);

0 commit comments

Comments
 (0)