|
1 | 1 | /***********************************************************************
|
2 |
| - * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra * |
| 2 | + * Copyright (c) 2015, 2022 Pieter Wuille, Andrew Poelstra * |
3 | 3 | * Distributed under the MIT software license, see the accompanying *
|
4 | 4 | * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
5 | 5 | ***********************************************************************/
|
|
12 | 12 | #include "ecmult_const.h"
|
13 | 13 | #include "ecmult_impl.h"
|
14 | 14 |
|
| 15 | +#if defined(EXHAUSTIVE_TEST_ORDER) |
| 16 | +/* We need 2^ECMULT_CONST_GROUP_SIZE - 1 to be less than EXHAUSTIVE_TEST_ORDER, because |
| 17 | + * the tables cannot have infinities in them (this breaks the effective-affine technique's |
| 18 | + * z-ratio tracking) */ |
| 19 | +# if EXHAUSTIVE_TEST_ORDER == 199 |
| 20 | +# define ECMULT_CONST_GROUP_SIZE 4 |
| 21 | +# elif EXHAUSTIVE_TEST_ORDER == 13 |
| 22 | +# define ECMULT_CONST_GROUP_SIZE 3 |
| 23 | +# elif EXHAUSTIVE_TEST_ORDER == 7 |
| 24 | +# define ECMULT_CONST_GROUP_SIZE 2 |
| 25 | +# else |
| 26 | +# error "Unknown EXHAUSTIVE_TEST_ORDER" |
| 27 | +# endif |
| 28 | +#else |
| 29 | +/* Group size 4 or 5 appears optimal. */ |
| 30 | +# define ECMULT_CONST_GROUP_SIZE 5 |
| 31 | +#endif |
| 32 | + |
| 33 | +#define ECMULT_CONST_TABLE_SIZE (1L << (ECMULT_CONST_GROUP_SIZE - 1)) |
| 34 | +#define ECMULT_CONST_GROUPS ((129 + ECMULT_CONST_GROUP_SIZE - 1) / ECMULT_CONST_GROUP_SIZE) |
| 35 | +#define ECMULT_CONST_BITS (ECMULT_CONST_GROUPS * ECMULT_CONST_GROUP_SIZE) |
| 36 | + |
15 | 37 | /** Fill a table 'pre' with precomputed odd multiples of a.
|
16 | 38 | *
|
17 | 39 | * The resulting point set is brought to a single constant Z denominator, stores the X and Y
|
18 |
| - * coordinates as ge_storage points in pre, and stores the global Z in globalz. |
19 |
| - * It only operates on tables sized for WINDOW_A wnaf multiples. |
| 40 | + * coordinates as ge points in pre, and stores the global Z in globalz. |
| 41 | + * |
| 42 | + * 'pre' must be an array of size ECMULT_CONST_TABLE_SIZE. |
20 | 43 | */
|
21 |
| -static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) { |
22 |
| - secp256k1_fe zr[ECMULT_TABLE_SIZE(WINDOW_A)]; |
| 44 | +static void secp256k1_ecmult_const_odd_multiples_table_globalz(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a) { |
| 45 | + secp256k1_fe zr[ECMULT_CONST_TABLE_SIZE]; |
23 | 46 |
|
24 |
| - secp256k1_ecmult_odd_multiples_table(ECMULT_TABLE_SIZE(WINDOW_A), pre, zr, globalz, a); |
25 |
| - secp256k1_ge_table_set_globalz(ECMULT_TABLE_SIZE(WINDOW_A), pre, zr); |
| 47 | + secp256k1_ecmult_odd_multiples_table(ECMULT_CONST_TABLE_SIZE, pre, zr, globalz, a); |
| 48 | + secp256k1_ge_table_set_globalz(ECMULT_CONST_TABLE_SIZE, pre, zr); |
26 | 49 | }
|
27 | 50 |
|
28 |
| -/* This is like `ECMULT_TABLE_GET_GE` but is constant time */ |
29 |
| -#define ECMULT_CONST_TABLE_GET_GE(r,pre,n,w) do { \ |
30 |
| - int m = 0; \ |
31 |
| - /* Extract the sign-bit for a constant time absolute-value. */ \ |
32 |
| - int volatile mask = (n) >> (sizeof(n) * CHAR_BIT - 1); \ |
33 |
| - int abs_n = ((n) + mask) ^ mask; \ |
34 |
| - int idx_n = abs_n >> 1; \ |
| 51 | +/* Given a table 'pre' with odd multiples of a point, put in r the signed-bit multiplication of n with that point. |
| 52 | + * |
| 53 | + * For example, if ECMULT_CONST_GROUP_SIZE is 4, then pre is expected to contain 8 entries: |
| 54 | + * [1*P, 3*P, 5*P, 7*P, 9*P, 11*P, 13*P, 15*P]. n is then expected to be a 4-bit integer (range 0-15), and its |
| 55 | + * bits are interpreted as signs of powers of two to look up. |
| 56 | + * |
| 57 | + * For example, if n=4, which is 0100 in binary, which is interpreted as [- + - -], so the looked up value is |
| 58 | + * [ -(2^3) + (2^2) - (2^1) - (2^0) ]*P = -7*P. Every valid n translates to an odd number in range [-15,15], |
| 59 | + * which means we just need to look up one of the precomputed values, and optionally negate it. |
| 60 | + */ |
| 61 | +#define ECMULT_CONST_TABLE_GET_GE(r,pre,n) do { \ |
| 62 | + unsigned int m = 0; \ |
| 63 | + /* If the top bit of n is 0, we want the negation. */ \ |
| 64 | + volatile unsigned int negative = ((n) >> (ECMULT_CONST_GROUP_SIZE - 1)) ^ 1; \ |
| 65 | + /* Let n[i] be the i-th bit of n, then the index is |
| 66 | + * sum(cnot(n[i]) * 2^i, i=0..l-2) |
| 67 | + * where cnot(b) = b if n[l-1] = 1 and 1 - b otherwise. |
| 68 | + * For example, if n = 4, in binary 0100, the index is 3, in binary 011. |
| 69 | + * |
| 70 | + * Proof: |
| 71 | + * Let |
| 72 | + * x = sum((2*n[i] - 1)*2^i, i=0..l-1) |
| 73 | + * = 2*sum(n[i] * 2^i, i=0..l-1) - 2^l + 1 |
| 74 | + * be the value represented by n. |
| 75 | + * The index is (x - 1)/2 if x > 0 and -(x + 1)/2 otherwise. |
| 76 | + * Case x > 0: |
| 77 | + * n[l-1] = 1 |
| 78 | + * index = sum(n[i] * 2^i, i=0..l-1) - 2^(l-1) |
| 79 | + * = sum(n[i] * 2^i, i=0..l-2) |
| 80 | + * Case x <= 0: |
| 81 | + * n[l-1] = 0 |
| 82 | + * index = -(2*sum(n[i] * 2^i, i=0..l-1) - 2^l + 2)/2 |
| 83 | + * = 2^(l-1) - 1 - sum(n[i] * 2^i, i=0..l-1) |
| 84 | + * = sum((1 - n[i]) * 2^i, i=0..l-2) |
| 85 | + */ \ |
| 86 | + unsigned int index = ((unsigned int)(-negative) ^ n) & ((1U << (ECMULT_CONST_GROUP_SIZE - 1)) - 1U); \ |
35 | 87 | secp256k1_fe neg_y; \
|
36 |
| - VERIFY_CHECK(((n) & 1) == 1); \ |
37 |
| - VERIFY_CHECK((n) >= -((1 << ((w)-1)) - 1)); \ |
38 |
| - VERIFY_CHECK((n) <= ((1 << ((w)-1)) - 1)); \ |
| 88 | + VERIFY_CHECK((n) < (1U << ECMULT_CONST_GROUP_SIZE)); \ |
| 89 | + VERIFY_CHECK(index < (1U << (ECMULT_CONST_GROUP_SIZE - 1))); \ |
39 | 90 | VERIFY_SETUP(secp256k1_fe_clear(&(r)->x)); \
|
40 | 91 | VERIFY_SETUP(secp256k1_fe_clear(&(r)->y)); \
|
41 |
| - /* Unconditionally set r->x = (pre)[m].x. r->y = (pre)[m].y. because it's either the correct one \ |
| 92 | + /* Unconditionally set r->x = (pre)[m].x. r->y = (pre)[m].y. because it's either the correct one |
42 | 93 | * or will get replaced in the later iterations, this is needed to make sure `r` is initialized. */ \
|
43 | 94 | (r)->x = (pre)[m].x; \
|
44 | 95 | (r)->y = (pre)[m].y; \
|
45 |
| - for (m = 1; m < ECMULT_TABLE_SIZE(w); m++) { \ |
| 96 | + for (m = 1; m < ECMULT_CONST_TABLE_SIZE; m++) { \ |
46 | 97 | /* This loop is used to avoid secret data in array indices. See
|
47 | 98 | * the comment in ecmult_gen_impl.h for rationale. */ \
|
48 |
| - secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == idx_n); \ |
49 |
| - secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == idx_n); \ |
| 99 | + secp256k1_fe_cmov(&(r)->x, &(pre)[m].x, m == index); \ |
| 100 | + secp256k1_fe_cmov(&(r)->y, &(pre)[m].y, m == index); \ |
50 | 101 | } \
|
51 | 102 | (r)->infinity = 0; \
|
52 | 103 | secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
|
53 |
| - secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \ |
| 104 | + secp256k1_fe_cmov(&(r)->y, &neg_y, negative); \ |
54 | 105 | } while(0)
|
55 | 106 |
|
56 | 107 | /** Convert a number to WNAF notation.
|
@@ -130,90 +181,169 @@ static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w
|
130 | 181 | return skew;
|
131 | 182 | }
|
132 | 183 |
|
133 |
| -static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar) { |
134 |
| - secp256k1_ge pre_a[ECMULT_TABLE_SIZE(WINDOW_A)]; |
135 |
| - secp256k1_ge tmpa; |
136 |
| - secp256k1_fe Z; |
137 |
| - |
138 |
| - int skew_1; |
139 |
| - secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)]; |
140 |
| - int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)]; |
141 |
| - int skew_lam; |
142 |
| - secp256k1_scalar q_1, q_lam; |
143 |
| - int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)]; |
| 184 | +/* For K as defined in the comment of secp256k1_ecmult_const, we have several precomputed |
| 185 | + * formulas/constants. |
| 186 | + * - in exhaustive test mode, we give an explicit expression to compute it at compile time: */ |
| 187 | +#ifdef EXHAUSTIVE_TEST_ORDER |
| 188 | +static const secp256k1_scalar secp256k1_ecmult_const_K = ((SECP256K1_SCALAR_CONST(0, 0, 0, (1U << (ECMULT_CONST_BITS - 128)) - 2U, 0, 0, 0, 0) + EXHAUSTIVE_TEST_ORDER - 1U) * (1U + EXHAUSTIVE_TEST_LAMBDA)) % EXHAUSTIVE_TEST_ORDER; |
| 189 | +/* - for the real secp256k1 group we have constants for various ECMULT_CONST_BITS values. */ |
| 190 | +#elif ECMULT_CONST_BITS == 129 |
| 191 | +/* For GROUP_SIZE = 1,3. */ |
| 192 | +static const secp256k1_scalar secp256k1_ecmult_const_K = SECP256K1_SCALAR_CONST(0xac9c52b3ul, 0x3fa3cf1ful, 0x5ad9e3fdul, 0x77ed9ba4ul, 0xa880b9fcul, 0x8ec739c2ul, 0xe0cfc810ul, 0xb51283ceul); |
| 193 | +#elif ECMULT_CONST_BITS == 130 |
| 194 | +/* For GROUP_SIZE = 2,5. */ |
| 195 | +static const secp256k1_scalar secp256k1_ecmult_const_K = SECP256K1_SCALAR_CONST(0xa4e88a7dul, 0xcb13034eul, 0xc2bdd6bful, 0x7c118d6bul, 0x589ae848ul, 0x26ba29e4ul, 0xb5c2c1dcul, 0xde9798d9ul); |
| 196 | +#elif ECMULT_CONST_BITS == 132 |
| 197 | +/* For GROUP_SIZE = 4,6 */ |
| 198 | +static const secp256k1_scalar secp256k1_ecmult_const_K = SECP256K1_SCALAR_CONST(0x76b1d93dul, 0x0fae3c6bul, 0x3215874bul, 0x94e93813ul, 0x7937fe0dul, 0xb66bcaaful, 0xb3749ca5ul, 0xd7b6171bul); |
| 199 | +#else |
| 200 | +# error "Unknown ECMULT_CONST_BITS" |
| 201 | +#endif |
144 | 202 |
|
145 |
| - int i; |
| 203 | +static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *q) { |
| 204 | + /* The approach below combines the signed-digit logic from Mike Hamburg's |
| 205 | + * "Fast and compact elliptic-curve cryptography" (https://eprint.iacr.org/2012/309) |
| 206 | + * Section 3.3, with the GLV endomorphism. |
| 207 | + * |
| 208 | + * The idea there is to interpret the bits of a scalar as signs (1 = +, 0 = -), and compute a |
| 209 | + * point multiplication in that fashion. Let v be an n-bit non-negative integer (0 <= v < 2^n), |
| 210 | + * and v[i] its i'th bit (so v = sum(v[i] * 2^i, i=0..n-1)). Then define: |
| 211 | + * |
| 212 | + * C_l(v, A) = sum((2*v[i] - 1) * 2^i*A, i=0..l-1) |
| 213 | + * |
| 214 | + * Then it holds that C_l(v, A) = sum((2*v[i] - 1) * 2^i*A, i=0..l-1) |
| 215 | + * = (2*sum(v[i] * 2^i, i=0..l-1) + 1 - 2^l) * A |
| 216 | + * = (2*v + 1 - 2^l) * A |
| 217 | + * |
| 218 | + * Thus, one can compute q*A as C_256((q + 2^256 - 1) / 2, A). This is the basis for the |
| 219 | + * paper's signed-digit multi-comb algorithm for multiplication using a precomputed table. |
| 220 | + * |
| 221 | + * It is appealing to try to combine this with the GLV optimization: the idea that a scalar |
| 222 | + * s can be written as s1 + lambda*s2, where lambda is a curve-specific constant such that |
| 223 | + * lambda*A is easy to compute, and where s1 and s2 are small. In particular we have the |
| 224 | + * secp256k1_scalar_split_lambda function which performs such a split with the resulting s1 |
| 225 | + * and s2 in range (-2^128, 2^128) mod n. This does work, but is uninteresting: |
| 226 | + * |
| 227 | + * To compute q*A: |
| 228 | + * - Let s1, s2 = split_lambda(q) |
| 229 | + * - Let R1 = C_256((s1 + 2^256 - 1) / 2, A) |
| 230 | + * - Let R2 = C_256((s2 + 2^256 - 1) / 2, lambda*A) |
| 231 | + * - Return R1 + R2 |
| 232 | + * |
| 233 | + * The issue is that while s1 and s2 are small-range numbers, (s1 + 2^256 - 1) / 2 (mod n) |
| 234 | + * and (s2 + 2^256 - 1) / 2 (mod n) are not, undoing the benefit of the splitting. |
| 235 | + * |
| 236 | + * To make it work, we want to modify the input scalar q first, before splitting, and then only |
| 237 | + * add a 2^128 offset of the split results (so that they end up in the single 129-bit range |
| 238 | + * [0,2^129]). A slightly smaller offset would work due to the bounds on the split, but we pick |
| 239 | + * 2^128 for simplicity. Let s be the scalar fed to split_lambda, and f(q) the function to |
| 240 | + * compute it from q: |
| 241 | + * |
| 242 | + * To compute q*A: |
| 243 | + * - Compute s = f(q) |
| 244 | + * - Let s1, s2 = split_lambda(s) |
| 245 | + * - Let v1 = s1 + 2^128 (mod n) |
| 246 | + * - Let v2 = s2 + 2^128 (mod n) |
| 247 | + * - Let R1 = C_l(v1, A) |
| 248 | + * - Let R2 = C_l(v2, lambda*A) |
| 249 | + * - Return R1 + R2 |
| 250 | + * |
| 251 | + * l will thus need to be at least 129, but we may overshoot by a few bits (see |
| 252 | + * further), so keep it as a variable. |
| 253 | + * |
| 254 | + * To solve for s, we reason: |
| 255 | + * q*A = R1 + R2 |
| 256 | + * <=> q*A = C_l(s1 + 2^128, A) + C_l(s2 + 2^128, lambda*A) |
| 257 | + * <=> q*A = (2*(s1 + 2^128) + 1 - 2^l) * A + (2*(s2 + 2^128) + 1 - 2^l) * lambda*A |
| 258 | + * <=> q*A = (2*(s1 + s2*lambda) + (2^129 + 1 - 2^l) * (1 + lambda)) * A |
| 259 | + * <=> q = 2*(s1 + s2*lambda) + (2^129 + 1 - 2^l) * (1 + lambda) (mod n) |
| 260 | + * <=> q = 2*s + (2^129 + 1 - 2^l) * (1 + lambda) (mod n) |
| 261 | + * <=> s = (q + (2^l - 2^129 - 1) * (1 + lambda)) / 2 (mod n) |
| 262 | + * <=> f(q) = (q + K) / 2 (mod n) |
| 263 | + * where K = (2^l - 2^129 - 1)*(1 + lambda) (mod n) |
| 264 | + * |
| 265 | + * We will process the computation of C_l(v1, A) and C_l(v2, lambda*A) in groups of |
| 266 | + * ECMULT_CONST_GROUP_SIZE, so we set l to the smallest multiple of ECMULT_CONST_GROUP_SIZE |
| 267 | + * that is not less than 129; this equals ECMULT_CONST_BITS. |
| 268 | + */ |
146 | 269 |
|
| 270 | + /* The offset to add to s1 and s2 to make them non-negative. Equal to 2^128. */ |
| 271 | + static const secp256k1_scalar S_OFFSET = SECP256K1_SCALAR_CONST(0, 0, 0, 1, 0, 0, 0, 0); |
| 272 | + secp256k1_scalar s, v1, v2; |
| 273 | + secp256k1_ge pre_a[ECMULT_CONST_TABLE_SIZE]; |
| 274 | + secp256k1_ge pre_a_lam[ECMULT_CONST_TABLE_SIZE]; |
| 275 | + secp256k1_fe global_z; |
| 276 | + int group, i; |
| 277 | + |
| 278 | + /* We're allowed to be non-constant time in the point, and the code below (in particular, |
| 279 | + * secp256k1_ecmult_const_odd_multiples_table_globalz) cannot deal with infinity in a |
| 280 | + * constant-time manner anyway. */ |
147 | 281 | if (secp256k1_ge_is_infinity(a)) {
|
148 | 282 | secp256k1_gej_set_infinity(r);
|
149 | 283 | return;
|
150 | 284 | }
|
151 | 285 |
|
152 |
| - /* build wnaf representation for q. */ |
153 |
| - /* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */ |
154 |
| - secp256k1_scalar_split_lambda(&q_1, &q_lam, scalar); |
155 |
| - skew_1 = secp256k1_wnaf_const(wnaf_1, &q_1, WINDOW_A - 1, 128); |
156 |
| - skew_lam = secp256k1_wnaf_const(wnaf_lam, &q_lam, WINDOW_A - 1, 128); |
| 286 | + /* Compute v1 and v2. */ |
| 287 | + secp256k1_scalar_add(&s, q, &secp256k1_ecmult_const_K); |
| 288 | + secp256k1_scalar_half(&s, &s); |
| 289 | + secp256k1_scalar_split_lambda(&v1, &v2, &s); |
| 290 | + secp256k1_scalar_add(&v1, &v1, &S_OFFSET); |
| 291 | + secp256k1_scalar_add(&v2, &v2, &S_OFFSET); |
157 | 292 |
|
158 |
| - /* Calculate odd multiples of a. |
| 293 | +#ifdef VERIFY |
| 294 | + /* Verify that v1 and v2 are in range [0, 2^129-1]. */ |
| 295 | + for (i = 129; i < 256; ++i) { |
| 296 | + VERIFY_CHECK(secp256k1_scalar_get_bits(&v1, i, 1) == 0); |
| 297 | + VERIFY_CHECK(secp256k1_scalar_get_bits(&v2, i, 1) == 0); |
| 298 | + } |
| 299 | +#endif |
| 300 | + |
| 301 | + /* Calculate odd multiples of A and A*lambda. |
159 | 302 | * All multiples are brought to the same Z 'denominator', which is stored
|
160 |
| - * in Z. Due to secp256k1' isomorphism we can do all operations pretending |
| 303 | + * in global_z. Due to secp256k1' isomorphism we can do all operations pretending |
161 | 304 | * that the Z coordinate was 1, use affine addition formulae, and correct
|
162 | 305 | * the Z coordinate of the result once at the end.
|
163 | 306 | */
|
164 |
| - VERIFY_CHECK(!a->infinity); |
165 | 307 | secp256k1_gej_set_ge(r, a);
|
166 |
| - secp256k1_ecmult_odd_multiples_table_globalz_windowa(pre_a, &Z, r); |
167 |
| - for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) { |
168 |
| - secp256k1_fe_normalize_weak(&pre_a[i].y); |
169 |
| - } |
170 |
| - for (i = 0; i < ECMULT_TABLE_SIZE(WINDOW_A); i++) { |
| 308 | + secp256k1_ecmult_const_odd_multiples_table_globalz(pre_a, &global_z, r); |
| 309 | + for (i = 0; i < ECMULT_CONST_TABLE_SIZE; i++) { |
171 | 310 | secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
|
172 | 311 | }
|
173 | 312 |
|
174 |
| - /* first loop iteration (separated out so we can directly set r, rather |
175 |
| - * than having it start at infinity, get doubled several times, then have |
176 |
| - * its new value added to it) */ |
177 |
| - i = wnaf_1[WNAF_SIZE_BITS(128, WINDOW_A - 1)]; |
178 |
| - VERIFY_CHECK(i != 0); |
179 |
| - ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A); |
180 |
| - secp256k1_gej_set_ge(r, &tmpa); |
181 |
| - i = wnaf_lam[WNAF_SIZE_BITS(128, WINDOW_A - 1)]; |
182 |
| - VERIFY_CHECK(i != 0); |
183 |
| - ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A); |
184 |
| - secp256k1_gej_add_ge(r, r, &tmpa); |
185 |
| - /* remaining loop iterations */ |
186 |
| - for (i = WNAF_SIZE_BITS(128, WINDOW_A - 1) - 1; i >= 0; i--) { |
187 |
| - int n; |
| 313 | + /* Next, we compute r = C_l(v1, A) + C_l(v2, lambda*A). |
| 314 | + * |
| 315 | + * We proceed in groups of ECMULT_CONST_GROUP_SIZE bits, operating on that many bits |
| 316 | + * at a time, from high in v1, v2 to low. Call these bits1 (from v1) and bits2 (from v2). |
| 317 | + * |
| 318 | + * Now note that ECMULT_CONST_TABLE_GET_GE(&t, pre_a, bits1) loads into t a point equal |
| 319 | + * to C_{ECMULT_CONST_GROUP_SIZE}(bits1, A), and analogously for pre_lam_a / bits2. |
| 320 | + * This means that all we need to do is add these looked up values together, multiplied |
| 321 | + * by 2^(ECMULT_GROUP_SIZE * group). |
| 322 | + */ |
| 323 | + for (group = ECMULT_CONST_GROUPS - 1; group >= 0; --group) { |
| 324 | + /* Using the _var get_bits function is ok here, since it's only variable in offset and count, not in the scalar. */ |
| 325 | + unsigned int bits1 = secp256k1_scalar_get_bits_var(&v1, group * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE); |
| 326 | + unsigned int bits2 = secp256k1_scalar_get_bits_var(&v2, group * ECMULT_CONST_GROUP_SIZE, ECMULT_CONST_GROUP_SIZE); |
| 327 | + secp256k1_ge t; |
188 | 328 | int j;
|
189 |
| - for (j = 0; j < WINDOW_A - 1; ++j) { |
190 |
| - secp256k1_gej_double(r, r); |
191 |
| - } |
192 | 329 |
|
193 |
| - n = wnaf_1[i]; |
194 |
| - ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A); |
195 |
| - VERIFY_CHECK(n != 0); |
196 |
| - secp256k1_gej_add_ge(r, r, &tmpa); |
197 |
| - n = wnaf_lam[i]; |
198 |
| - ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A); |
199 |
| - VERIFY_CHECK(n != 0); |
200 |
| - secp256k1_gej_add_ge(r, r, &tmpa); |
201 |
| - } |
202 |
| - |
203 |
| - { |
204 |
| - /* Correct for wNAF skew */ |
205 |
| - secp256k1_gej tmpj; |
206 |
| - |
207 |
| - secp256k1_ge_neg(&tmpa, &pre_a[0]); |
208 |
| - secp256k1_gej_add_ge(&tmpj, r, &tmpa); |
209 |
| - secp256k1_gej_cmov(r, &tmpj, skew_1); |
210 |
| - |
211 |
| - secp256k1_ge_neg(&tmpa, &pre_a_lam[0]); |
212 |
| - secp256k1_gej_add_ge(&tmpj, r, &tmpa); |
213 |
| - secp256k1_gej_cmov(r, &tmpj, skew_lam); |
| 330 | + ECMULT_CONST_TABLE_GET_GE(&t, pre_a, bits1); |
| 331 | + if (group == ECMULT_CONST_GROUPS - 1) { |
| 332 | + /* Directly set r in the first iteration. */ |
| 333 | + secp256k1_gej_set_ge(r, &t); |
| 334 | + } else { |
| 335 | + /* Shift the result so far up. */ |
| 336 | + for (j = 0; j < ECMULT_CONST_GROUP_SIZE; ++j) { |
| 337 | + secp256k1_gej_double(r, r); |
| 338 | + } |
| 339 | + secp256k1_gej_add_ge(r, r, &t); |
| 340 | + } |
| 341 | + ECMULT_CONST_TABLE_GET_GE(&t, pre_a_lam, bits2); |
| 342 | + secp256k1_gej_add_ge(r, r, &t); |
214 | 343 | }
|
215 | 344 |
|
216 |
| - secp256k1_fe_mul(&r->z, &r->z, &Z); |
| 345 | + /* Map the result back to the secp256k1 curve from the isomorphic curve. */ |
| 346 | + secp256k1_fe_mul(&r->z, &r->z, &global_z); |
217 | 347 | }
|
218 | 348 |
|
219 | 349 | static int secp256k1_ecmult_const_xonly(secp256k1_fe* r, const secp256k1_fe *n, const secp256k1_fe *d, const secp256k1_scalar *q, int known_on_curve) {
|
|
0 commit comments