|
19 | 19 | #endif
|
20 | 20 | #define WNAF_SIZE(w) ((WNAF_BITS + (w) - 1) / (w))
|
21 | 21 |
|
| 22 | +/* This is like `ECMULT_TABLE_GET_GE` but is constant time */ |
| 23 | +#define ECDH_TABLE_GET_GE(r,pre,n,w) do { \ |
| 24 | + int m; \ |
| 25 | + int abs_n = (n) * (((n) > 0) * 2 - 1); \ |
| 26 | + secp256k1_fe_t neg_y; \ |
| 27 | + VERIFY_CHECK(((n) & 1) == 1); \ |
| 28 | + VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \ |
| 29 | + VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \ |
| 30 | + for (m = 1; m < (1 << ((w) - 1)); m += 2) { \ |
| 31 | + /* This loop is used to avoid secret data in array indices. See |
| 32 | + * the comment in ecmult_gen_impl.h for rationale. */ \ |
| 33 | + secp256k1_fe_cmov(&(r)->x, &(pre)[(m - 1) / 2].x, m == abs_n); \ |
| 34 | + secp256k1_fe_cmov(&(r)->y, &(pre)[(m - 1) / 2].y, m == abs_n); \ |
| 35 | + } \ |
| 36 | + (r)->infinity = 0; \ |
| 37 | + secp256k1_fe_normalize_weak(&(r)->x); \ |
| 38 | + secp256k1_fe_normalize_weak(&(r)->y); \ |
| 39 | + secp256k1_fe_negate(&neg_y, &(r)->y, 1); \ |
| 40 | + secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \ |
| 41 | +} while(0) |
| 42 | + |
22 | 43 | /** Convert a number to WNAF notation. The number becomes represented by sum(2^{wi} * wnaf[i], i=0..return_val)
|
23 | 44 | * with the following guarantees:
|
24 | 45 | * - each wnaf[i] an odd integer between -(1 << w) and (1 << w)
|
@@ -161,18 +182,18 @@ static void secp256k1_point_multiply(secp256k1_gej_t *r, const secp256k1_ge_t *a
|
161 | 182 | }
|
162 | 183 | #ifdef USE_ENDOMORPHISM
|
163 | 184 | n = wnaf_1[i];
|
164 |
| - ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A); |
| 185 | + ECDH_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A); |
165 | 186 | VERIFY_CHECK(n != 0);
|
166 | 187 | secp256k1_gej_add_ge(r, r, &tmpa);
|
167 | 188 |
|
168 | 189 | n = wnaf_lam[i];
|
169 |
| - ECMULT_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A); |
| 190 | + ECDH_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A); |
170 | 191 | VERIFY_CHECK(n != 0);
|
171 | 192 | secp256k1_gej_add_ge(r, r, &tmpa);
|
172 | 193 | #else
|
173 | 194 | n = wnaf[i];
|
174 | 195 | VERIFY_CHECK(n != 0);
|
175 |
| - ECMULT_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A); |
| 196 | + ECDH_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A); |
176 | 197 | secp256k1_gej_add_ge(r, r, &tmpa);
|
177 | 198 | #endif
|
178 | 199 | }
|
|
0 commit comments