Skip to content

Commit a1102b1

Browse files
committed
Merge #1029: Simpler and faster ecdh skew fixup
e82144e Fixup skew before global Z fixup (Peter Dettman) 40b624c Add tests for _gej_cmov (Peter Dettman) 8c13a9b ECDH skews by 0 or 1 (Peter Dettman) 1515099 Simpler and faster ecdh skew fixup (Peter Dettman) Pull request description: This PR adds a `_gej_cmov` method, with accompanying tests, and uses it to simplify the skew fixup at the end of `_ecmult_const`. In the existing code, `_wnaf_const` chooses a skew of either 1 or 2, and `_ecmult_const` needs a call to `_ge_set_gej` (which does an expensive field inversion internally) and some overly-complicated conversions to/from `_ge_storage` so that `_ge_storage_cmov` can be used to select what value to add for the fixup. This PR uses a simpler scheme where `_wnaf_const` chooses a skew of 0 or 1 and no longer needs special handling for scalars with value negative one. A new `_gej_cmov` method is used at the end of `_ecmult_const` for const-time optional addition to adjust the final result for the skew. Finally, the skew fixup is moved to before the global-Z adjustment, and the precomputed table entries (for 1P, λ(1P)) are used for the skew fixup, saving a field multiply and ensuring the fixup is done on the same isomorphism as the ladder. The resulting `_wnaf_const` and `_ecmult_const` are shorter and simpler, and the ECDH benchmark is around 5% faster (64bit, i7). Edit: Updated description once the final scope was clear. ACKs for top commit: apoelstra: ACK e82144e sipa: ACK e82144e real-or-random: ACK e82144e Tree-SHA512: 10d6770f4ef4f8d0c78abbf58d643f25f5daef68896643af0a3f7f877414e23356724b6f20af2027316a4353a35b8cb0a7851e057a3f6483897df02bf033a8a2
2 parents 39a36db + e82144e commit a1102b1

File tree

4 files changed

+68
-56
lines changed

4 files changed

+68
-56
lines changed

src/ecmult_const_impl.h

+18-55
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *p
5656
secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
5757
} while(0)
5858

59-
6059
/** Convert a number to WNAF notation.
6160
* The number becomes represented by sum(2^{wi} * wnaf[i], i=0..WNAF_SIZE(w)+1) - return_val.
6261
* It has the following guarantees:
@@ -72,51 +71,35 @@ static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *p
7271
*/
7372
static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
7473
int global_sign;
75-
int skew = 0;
74+
int skew;
7675
int word = 0;
7776

7877
/* 1 2 3 */
7978
int u_last;
8079
int u;
8180

8281
int flip;
83-
int bit;
84-
secp256k1_scalar s;
85-
int not_neg_one;
82+
secp256k1_scalar s = *scalar;
8683

8784
VERIFY_CHECK(w > 0);
8885
VERIFY_CHECK(size > 0);
8986

9087
/* Note that we cannot handle even numbers by negating them to be odd, as is
9188
* done in other implementations, since if our scalars were specified to have
9289
* width < 256 for performance reasons, their negations would have width 256
93-
* and we'd lose any performance benefit. Instead, we use a technique from
94-
* Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even)
95-
* or 2 (for odd) to the number we are encoding, returning a skew value indicating
90+
* and we'd lose any performance benefit. Instead, we use a variation of a
91+
* technique from Section 4.2 of the Okeya/Tagaki paper, which is to add 1 to the
92+
* number we are encoding when it is even, returning a skew value indicating
9693
* this, and having the caller compensate after doing the multiplication.
9794
*
9895
* In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
9996
* particular, to ensure that the outputs from the endomorphism-split fit into
100-
* 128 bits). If we negate, the parity of our number flips, inverting which of
101-
* {1, 2} we want to add to the scalar when ensuring that it's odd. Further
102-
* complicating things, -1 interacts badly with `secp256k1_scalar_cadd_bit` and
103-
* we need to special-case it in this logic. */
104-
flip = secp256k1_scalar_is_high(scalar);
105-
/* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */
106-
bit = flip ^ !secp256k1_scalar_is_even(scalar);
107-
/* We check for negative one, since adding 2 to it will cause an overflow */
108-
secp256k1_scalar_negate(&s, scalar);
109-
not_neg_one = !secp256k1_scalar_is_one(&s);
110-
s = *scalar;
111-
secp256k1_scalar_cadd_bit(&s, bit, not_neg_one);
112-
/* If we had negative one, flip == 1, s.d[0] == 0, bit == 1, so caller expects
113-
* that we added two to it and flipped it. In fact for -1 these operations are
114-
* identical. We only flipped, but since skewing is required (in the sense that
115-
* the skew must be 1 or 2, never zero) and flipping is not, we need to change
116-
* our flags to claim that we only skewed. */
97+
* 128 bits). If we negate, the parity of our number flips, affecting whether
98+
* we want to add to the scalar to ensure that it's odd. */
99+
flip = secp256k1_scalar_is_high(&s);
100+
skew = flip ^ secp256k1_scalar_is_even(&s);
101+
secp256k1_scalar_cadd_bit(&s, 0, skew);
117102
global_sign = secp256k1_scalar_cond_negate(&s, flip);
118-
global_sign *= not_neg_one * 2 - 1;
119-
skew = 1 << bit;
120103

121104
/* 4 */
122105
u_last = secp256k1_scalar_shr_int(&s, w);
@@ -230,42 +213,22 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
230213
}
231214
}
232215

233-
secp256k1_fe_mul(&r->z, &r->z, &Z);
234-
235216
{
236217
/* Correct for wNAF skew */
237-
secp256k1_ge correction = *a;
238-
secp256k1_ge_storage correction_1_stor;
239-
secp256k1_ge_storage correction_lam_stor;
240-
secp256k1_ge_storage a2_stor;
241218
secp256k1_gej tmpj;
242-
secp256k1_gej_set_ge(&tmpj, &correction);
243-
secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
244-
secp256k1_ge_set_gej(&correction, &tmpj);
245-
secp256k1_ge_to_storage(&correction_1_stor, a);
246-
if (size > 128) {
247-
secp256k1_ge_to_storage(&correction_lam_stor, a);
248-
}
249-
secp256k1_ge_to_storage(&a2_stor, &correction);
250219

251-
/* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
252-
secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
253-
if (size > 128) {
254-
secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
255-
}
256-
257-
/* Apply the correction */
258-
secp256k1_ge_from_storage(&correction, &correction_1_stor);
259-
secp256k1_ge_neg(&correction, &correction);
260-
secp256k1_gej_add_ge(r, r, &correction);
220+
secp256k1_ge_neg(&tmpa, &pre_a[0]);
221+
secp256k1_gej_add_ge(&tmpj, r, &tmpa);
222+
secp256k1_gej_cmov(r, &tmpj, skew_1);
261223

262224
if (size > 128) {
263-
secp256k1_ge_from_storage(&correction, &correction_lam_stor);
264-
secp256k1_ge_neg(&correction, &correction);
265-
secp256k1_ge_mul_lambda(&correction, &correction);
266-
secp256k1_gej_add_ge(r, r, &correction);
225+
secp256k1_ge_neg(&tmpa, &pre_a_lam[0]);
226+
secp256k1_gej_add_ge(&tmpj, r, &tmpa);
227+
secp256k1_gej_cmov(r, &tmpj, skew_lam);
267228
}
268229
}
230+
231+
secp256k1_fe_mul(&r->z, &r->z, &Z);
269232
}
270233

271234
#endif /* SECP256K1_ECMULT_CONST_IMPL_H */

src/group.h

+3
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

+8
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);

src/tests.c

+39-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ void random_group_element_jacobian_test(secp256k1_gej *gej, const secp256k1_ge *
100100
gej->infinity = ge->infinity;
101101
}
102102

103+
void random_gej_test(secp256k1_gej *gej) {
104+
secp256k1_ge ge;
105+
random_group_element_test(&ge);
106+
random_group_element_jacobian_test(gej, &ge);
107+
}
108+
103109
void random_scalar_order_test(secp256k1_scalar *num) {
104110
do {
105111
unsigned char b32[32];
@@ -3341,6 +3347,37 @@ void run_ge(void) {
33413347
test_intialized_inf();
33423348
}
33433349

3350+
void test_gej_cmov(const secp256k1_gej *a, const secp256k1_gej *b) {
3351+
secp256k1_gej t = *a;
3352+
secp256k1_gej_cmov(&t, b, 0);
3353+
CHECK(gej_xyz_equals_gej(&t, a));
3354+
secp256k1_gej_cmov(&t, b, 1);
3355+
CHECK(gej_xyz_equals_gej(&t, b));
3356+
}
3357+
3358+
void run_gej(void) {
3359+
int i;
3360+
secp256k1_gej a, b;
3361+
3362+
/* Tests for secp256k1_gej_cmov */
3363+
for (i = 0; i < count; i++) {
3364+
secp256k1_gej_set_infinity(&a);
3365+
secp256k1_gej_set_infinity(&b);
3366+
test_gej_cmov(&a, &b);
3367+
3368+
random_gej_test(&a);
3369+
test_gej_cmov(&a, &b);
3370+
test_gej_cmov(&b, &a);
3371+
3372+
b = a;
3373+
test_gej_cmov(&a, &b);
3374+
3375+
random_gej_test(&b);
3376+
test_gej_cmov(&a, &b);
3377+
test_gej_cmov(&b, &a);
3378+
}
3379+
}
3380+
33443381
void test_ec_combine(void) {
33453382
secp256k1_scalar sum = SECP256K1_SCALAR_CONST(0, 0, 0, 0, 0, 0, 0, 0);
33463383
secp256k1_pubkey data[6];
@@ -4522,7 +4559,7 @@ void test_constant_wnaf(const secp256k1_scalar *number, int w) {
45224559
secp256k1_scalar_add(&x, &x, &t);
45234560
}
45244561
/* Skew num because when encoding numbers as odd we use an offset */
4525-
secp256k1_scalar_set_int(&scalar_skew, 1 << (skew == 2));
4562+
secp256k1_scalar_set_int(&scalar_skew, skew);
45264563
secp256k1_scalar_add(&num, &num, &scalar_skew);
45274564
CHECK(secp256k1_scalar_eq(&x, &num));
45284565
}
@@ -6808,6 +6845,7 @@ int main(int argc, char **argv) {
68086845

68096846
/* group tests */
68106847
run_ge();
6848+
run_gej();
68116849
run_group_decompress();
68126850

68136851
/* ecmult tests */

0 commit comments

Comments
 (0)