Bitcoin Core  26.1.0
P2P Digital Currency
ecmult_const_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2015 Pieter Wuille, Andrew Poelstra *
3  * Distributed under the MIT software license, see the accompanying *
4  * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5  ***********************************************************************/
6 
7 #ifndef SECP256K1_ECMULT_CONST_IMPL_H
8 #define SECP256K1_ECMULT_CONST_IMPL_H
9 
10 #include "scalar.h"
11 #include "group.h"
12 #include "ecmult_const.h"
13 #include "ecmult_impl.h"
14 
23 
26 }
27 
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; \
35  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)); \
39  VERIFY_SETUP(secp256k1_fe_clear(&(r)->x)); \
40  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 \
42  * or will get replaced in the later iterations, this is needed to make sure `r` is initialized. */ \
43  (r)->x = (pre)[m].x; \
44  (r)->y = (pre)[m].y; \
45  for (m = 1; m < ECMULT_TABLE_SIZE(w); m++) { \
46  /* This loop is used to avoid secret data in array indices. See
47  * 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); \
50  } \
51  (r)->infinity = 0; \
52  secp256k1_fe_negate(&neg_y, &(r)->y, 1); \
53  secp256k1_fe_cmov(&(r)->y, &neg_y, (n) != abs_n); \
54 } while(0)
55 
69 static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size) {
70  int global_sign;
71  int skew;
72  int word = 0;
73 
74  /* 1 2 3 */
75  int u_last;
76  int u;
77 
78  int flip;
79  secp256k1_scalar s = *scalar;
80 
81  VERIFY_CHECK(w > 0);
82  VERIFY_CHECK(size > 0);
83 
84  /* Note that we cannot handle even numbers by negating them to be odd, as is
85  * done in other implementations, since if our scalars were specified to have
86  * width < 256 for performance reasons, their negations would have width 256
87  * and we'd lose any performance benefit. Instead, we use a variation of a
88  * technique from Section 4.2 of the Okeya/Tagaki paper, which is to add 1 to the
89  * number we are encoding when it is even, returning a skew value indicating
90  * this, and having the caller compensate after doing the multiplication.
91  *
92  * In fact, we _do_ want to negate numbers to minimize their bit-lengths (and in
93  * particular, to ensure that the outputs from the endomorphism-split fit into
94  * 128 bits). If we negate, the parity of our number flips, affecting whether
95  * we want to add to the scalar to ensure that it's odd. */
96  flip = secp256k1_scalar_is_high(&s);
97  skew = flip ^ secp256k1_scalar_is_even(&s);
98  secp256k1_scalar_cadd_bit(&s, 0, skew);
99  global_sign = secp256k1_scalar_cond_negate(&s, flip);
100 
101  /* 4 */
102  u_last = secp256k1_scalar_shr_int(&s, w);
103  do {
104  int even;
105 
106  /* 4.1 4.4 */
107  u = secp256k1_scalar_shr_int(&s, w);
108  /* 4.2 */
109  even = ((u & 1) == 0);
110  /* In contrast to the original algorithm, u_last is always > 0 and
111  * therefore we do not need to check its sign. In particular, it's easy
112  * to see that u_last is never < 0 because u is never < 0. Moreover,
113  * u_last is never = 0 because u is never even after a loop
114  * iteration. The same holds analogously for the initial value of
115  * u_last (in the first loop iteration). */
116  VERIFY_CHECK(u_last > 0);
117  VERIFY_CHECK((u_last & 1) == 1);
118  u += even;
119  u_last -= even * (1 << w);
120 
121  /* 4.3, adapted for global sign change */
122  wnaf[word++] = u_last * global_sign;
123 
124  u_last = u;
125  } while (word * w < size);
126  wnaf[word] = u * global_sign;
127 
129  VERIFY_CHECK(word == WNAF_SIZE_BITS(size, w));
130  return skew;
131 }
132 
133 static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar) {
135  secp256k1_ge tmpa;
136  secp256k1_fe Z;
137 
138  int skew_1;
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)];
144 
145  int i;
146 
147  if (secp256k1_ge_is_infinity(a)) {
149  return;
150  }
151 
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);
157 
158  /* Calculate odd multiples of a.
159  * 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
161  * that the Z coordinate was 1, use affine addition formulae, and correct
162  * the Z coordinate of the result once at the end.
163  */
164  VERIFY_CHECK(!a->infinity);
165  secp256k1_gej_set_ge(r, a);
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++) {
171  secp256k1_ge_mul_lambda(&pre_a_lam[i], &pre_a[i]);
172  }
173 
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;
188  int j;
189  for (j = 0; j < WINDOW_A - 1; ++j) {
190  secp256k1_gej_double(r, r);
191  }
192 
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);
214  }
215 
216  secp256k1_fe_mul(&r->z, &r->z, &Z);
217 }
218 
219 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) {
220 
221  /* This algorithm is a generalization of Peter Dettman's technique for
222  * avoiding the square root in a random-basepoint x-only multiplication
223  * on a Weierstrass curve:
224  * https://mailarchive.ietf.org/arch/msg/cfrg/7DyYY6gg32wDgHAhgSb6XxMDlJA/
225  *
226  *
227  * === Background: the effective affine technique ===
228  *
229  * Let phi_u be the isomorphism that maps (x, y) on secp256k1 curve y^2 = x^3 + 7 to
230  * x' = u^2*x, y' = u^3*y on curve y'^2 = x'^3 + u^6*7. This new curve has the same order as
231  * the original (it is isomorphic), but moreover, has the same addition/doubling formulas, as
232  * the curve b=7 coefficient does not appear in those formulas (or at least does not appear in
233  * the formulas implemented in this codebase, both affine and Jacobian). See also Example 9.5.2
234  * in https://www.math.auckland.ac.nz/~sgal018/crypto-book/ch9.pdf.
235  *
236  * This means any linear combination of secp256k1 points can be computed by applying phi_u
237  * (with non-zero u) on all input points (including the generator, if used), computing the
238  * linear combination on the isomorphic curve (using the same group laws), and then applying
239  * phi_u^{-1} to get back to secp256k1.
240  *
241  * Switching to Jacobian coordinates, note that phi_u applied to (X, Y, Z) is simply
242  * (X, Y, Z/u). Thus, if we want to compute (X1, Y1, Z) + (X2, Y2, Z), with identical Z
243  * coordinates, we can use phi_Z to transform it to (X1, Y1, 1) + (X2, Y2, 1) on an isomorphic
244  * curve where the affine addition formula can be used instead.
245  * If (X3, Y3, Z3) = (X1, Y1) + (X2, Y2) on that curve, then our answer on secp256k1 is
246  * (X3, Y3, Z3*Z).
247  *
248  * This is the effective affine technique: if we have a linear combination of group elements
249  * to compute, and all those group elements have the same Z coordinate, we can simply pretend
250  * that all those Z coordinates are 1, perform the computation that way, and then multiply the
251  * original Z coordinate back in.
252  *
253  * The technique works on any a=0 short Weierstrass curve. It is possible to generalize it to
254  * other curves too, but there the isomorphic curves will have different 'a' coefficients,
255  * which typically does affect the group laws.
256  *
257  *
258  * === Avoiding the square root for x-only point multiplication ===
259  *
260  * In this function, we want to compute the X coordinate of q*(n/d, y), for
261  * y = sqrt((n/d)^3 + 7). Its negation would also be a valid Y coordinate, but by convention
262  * we pick whatever sqrt returns (which we assume to be a deterministic function).
263  *
264  * Let g = y^2*d^3 = n^3 + 7*d^3. This also means y = sqrt(g/d^3).
265  * Further let v = sqrt(d*g), which must exist as d*g = y^2*d^4 = (y*d^2)^2.
266  *
267  * The input point (n/d, y) also has Jacobian coordinates:
268  *
269  * (n/d, y, 1)
270  * = (n/d * v^2, y * v^3, v)
271  * = (n/d * d*g, y * sqrt(d^3*g^3), v)
272  * = (n/d * d*g, sqrt(y^2 * d^3*g^3), v)
273  * = (n*g, sqrt(g/d^3 * d^3*g^3), v)
274  * = (n*g, sqrt(g^4), v)
275  * = (n*g, g^2, v)
276  *
277  * It is easy to verify that both (n*g, g^2, v) and its negation (n*g, -g^2, v) have affine X
278  * coordinate n/d, and this holds even when the square root function doesn't have a
279  * deterministic sign. We choose the (n*g, g^2, v) version.
280  *
281  * Now switch to the effective affine curve using phi_v, where the input point has coordinates
282  * (n*g, g^2). Compute (X, Y, Z) = q * (n*g, g^2) there.
283  *
284  * Back on secp256k1, that means q * (n*g, g^2, v) = (X, Y, v*Z). This last point has affine X
285  * coordinate X / (v^2*Z^2) = X / (d*g*Z^2). Determining the affine Y coordinate would involve
286  * a square root, but as long as we only care about the resulting X coordinate, no square root
287  * is needed anywhere in this computation.
288  */
289 
290  secp256k1_fe g, i;
291  secp256k1_ge p;
292  secp256k1_gej rj;
293 
294  /* Compute g = (n^3 + B*d^3). */
295  secp256k1_fe_sqr(&g, n);
296  secp256k1_fe_mul(&g, &g, n);
297  if (d) {
298  secp256k1_fe b;
299 #ifdef VERIFY
301 #endif
302  secp256k1_fe_sqr(&b, d);
303  VERIFY_CHECK(SECP256K1_B <= 8); /* magnitude of b will be <= 8 after the next call */
305  secp256k1_fe_mul(&b, &b, d);
306  secp256k1_fe_add(&g, &b);
307  if (!known_on_curve) {
308  /* We need to determine whether (n/d)^3 + 7 is square.
309  *
310  * is_square((n/d)^3 + 7)
311  * <=> is_square(((n/d)^3 + 7) * d^4)
312  * <=> is_square((n^3 + 7*d^3) * d)
313  * <=> is_square(g * d)
314  */
315  secp256k1_fe c;
316  secp256k1_fe_mul(&c, &g, d);
317  if (!secp256k1_fe_is_square_var(&c)) return 0;
318  }
319  } else {
321  if (!known_on_curve) {
322  /* g at this point equals x^3 + 7. Test if it is square. */
323  if (!secp256k1_fe_is_square_var(&g)) return 0;
324  }
325  }
326 
327  /* Compute base point P = (n*g, g^2), the effective affine version of (n*g, g^2, v), which has
328  * corresponding affine X coordinate n/d. */
329  secp256k1_fe_mul(&p.x, &g, n);
330  secp256k1_fe_sqr(&p.y, &g);
331  p.infinity = 0;
332 
333  /* Perform x-only EC multiplication of P with q. */
334 #ifdef VERIFY
336 #endif
337  secp256k1_ecmult_const(&rj, &p, q);
338 #ifdef VERIFY
340 #endif
341 
342  /* The resulting (X, Y, Z) point on the effective-affine isomorphic curve corresponds to
343  * (X, Y, Z*v) on the secp256k1 curve. The affine version of that has X coordinate
344  * (X / (Z^2*d*g)). */
345  secp256k1_fe_sqr(&i, &rj.z);
346  secp256k1_fe_mul(&i, &i, &g);
347  if (d) secp256k1_fe_mul(&i, &i, d);
348  secp256k1_fe_inv(&i, &i);
349  secp256k1_fe_mul(r, &rj.x, &i);
350 
351  return 1;
352 }
353 
354 #endif /* SECP256K1_ECMULT_CONST_IMPL_H */
static int secp256k1_ge_is_infinity(const secp256k1_ge *a)
Check whether a group element is the point at infinity.
#define VERIFY_CHECK(cond)
Definition: util.h:143
static int secp256k1_gej_is_infinity(const secp256k1_gej *a)
Check whether a group element is the point at infinity.
This field implementation represents the value as 10 uint32_t limbs in base 2^26. ...
Definition: field_10x26.h:14
#define secp256k1_fe_add_int
Definition: field.h:103
static int secp256k1_scalar_is_even(const secp256k1_scalar *a)
Check whether a scalar, considered as an nonnegative integer, is even.
#define ECMULT_TABLE_SIZE(w)
The number of entries a table with precomputed multiples needs to have.
Definition: ecmult.h:41
static void secp256k1_ge_neg(secp256k1_ge *r, const secp256k1_ge *a)
Set r equal to the inverse of a (i.e., mirrored around the X axis)
secp256k1_fe x
Definition: group.h:29
#define secp256k1_fe_mul
Definition: field.h:94
static void secp256k1_scalar_split_lambda(secp256k1_scalar *SECP256K1_RESTRICT r1, secp256k1_scalar *SECP256K1_RESTRICT r2, const secp256k1_scalar *SECP256K1_RESTRICT k)
Find r1 and r2 such that r1+r2*lambda = k, where r1 and r2 or their negations are maximum 128 bits lo...
#define secp256k1_fe_normalizes_to_zero
Definition: field.h:81
#define secp256k1_fe_mul_int(r, a)
Multiply a field element with a small integer.
Definition: field.h:237
#define secp256k1_fe_is_square_var
Definition: field.h:104
#define secp256k1_fe_sqr
Definition: field.h:95
#define secp256k1_fe_normalize_weak
Definition: field.h:79
static void secp256k1_ecmult_odd_multiples_table_globalz_windowa(secp256k1_ge *pre, secp256k1_fe *globalz, const secp256k1_gej *a)
Fill a table &#39;pre&#39; with precomputed odd multiples of a.
static int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
Check whether a scalar equals zero.
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n)
Shift a scalar right by some amount strictly between 0 and 16, returning the low bits that were shift...
#define secp256k1_fe_add
Definition: field.h:93
static int secp256k1_wnaf_const(int *wnaf, const secp256k1_scalar *scalar, int w, int size)
Convert a number to WNAF notation.
A group element of the secp256k1 curve, in jacobian coordinates.
Definition: group.h:28
#define SECP256K1_B
Definition: group_impl.h:71
static void secp256k1_gej_set_infinity(secp256k1_gej *r)
Set a group element (jacobian) equal to the point at infinity.
#define ECMULT_CONST_TABLE_GET_GE(r, pre, n, w)
#define WNAF_SIZE_BITS(bits, w)
Definition: ecmult_impl.h:45
static void secp256k1_gej_double(secp256k1_gej *r, const secp256k1_gej *a)
Set r equal to the double of a.
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
Check whether a scalar is higher than the group order divided by 2.
#define secp256k1_fe_inv
Definition: field.h:99
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag)
Conditionally add a power of two to a scalar.
A group element in affine coordinates on the secp256k1 curve, or occasionally on an isomorphic curve ...
Definition: group.h:16
secp256k1_fe x
Definition: group.h:17
static void secp256k1_ge_mul_lambda(secp256k1_ge *r, const secp256k1_ge *a)
Set r to be equal to lambda times a, where lambda is chosen in a way such that this is very fast...
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
int infinity
Definition: group.h:19
static int secp256k1_scalar_cond_negate(secp256k1_scalar *a, int flag)
Conditionally negate a number, in constant time.
#define WINDOW_A
Definition: ecmult_impl.h:32
static void secp256k1_gej_cmov(secp256k1_gej *r, const secp256k1_gej *a, int flag)
If flag is true, set *r equal to *a; otherwise leave it.
secp256k1_fe z
Definition: group.h:31
#define WNAF_SIZE(w)
Definition: ecmult_impl.h:46
static void secp256k1_gej_add_ge(secp256k1_gej *r, const secp256k1_gej *a, const secp256k1_ge *b)
Set r equal to the sum of a and b (with b given in affine coordinates, and not infinity).
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)
static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, const secp256k1_scalar *scalar)
static void secp256k1_gej_set_ge(secp256k1_gej *r, const secp256k1_ge *a)
Set a group element (jacobian) equal to another which is given in affine coordinates.
static void secp256k1_ecmult_odd_multiples_table(int n, secp256k1_ge *pre_a, secp256k1_fe *zr, secp256k1_fe *z, const secp256k1_gej *a)
Fill a table &#39;pre_a&#39; with precomputed odd multiples of a.
Definition: ecmult_impl.h:73
secp256k1_fe y
Definition: group.h:18
static void secp256k1_ge_table_set_globalz(size_t len, secp256k1_ge *a, const secp256k1_fe *zr)
Bring a batch of inputs to the same global z "denominator", based on ratios between (omitted) z coord...