Bitcoin Core  27.99.0
P2P Digital Currency
scalar_4x64_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 2013, 2014 Pieter Wuille *
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_SCALAR_REPR_IMPL_H
8 #define SECP256K1_SCALAR_REPR_IMPL_H
9 
10 #include "checkmem.h"
11 #include "int128.h"
12 #include "modinv64_impl.h"
13 #include "util.h"
14 
15 /* Limbs of the secp256k1 order. */
16 #define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
17 #define SECP256K1_N_1 ((uint64_t)0xBAAEDCE6AF48A03BULL)
18 #define SECP256K1_N_2 ((uint64_t)0xFFFFFFFFFFFFFFFEULL)
19 #define SECP256K1_N_3 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
20 
21 /* Limbs of 2^256 minus the secp256k1 order. */
22 #define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
23 #define SECP256K1_N_C_1 (~SECP256K1_N_1)
24 #define SECP256K1_N_C_2 (1)
25 
26 /* Limbs of half the secp256k1 order. */
27 #define SECP256K1_N_H_0 ((uint64_t)0xDFE92F46681B20A0ULL)
28 #define SECP256K1_N_H_1 ((uint64_t)0x5D576E7357A4501DULL)
29 #define SECP256K1_N_H_2 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
30 #define SECP256K1_N_H_3 ((uint64_t)0x7FFFFFFFFFFFFFFFULL)
31 
33  r->d[0] = 0;
34  r->d[1] = 0;
35  r->d[2] = 0;
36  r->d[3] = 0;
37 }
38 
40  r->d[0] = v;
41  r->d[1] = 0;
42  r->d[2] = 0;
43  r->d[3] = 0;
44 
46 }
47 
48 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
50  VERIFY_CHECK((offset + count - 1) >> 6 == offset >> 6);
51 
52  return (a->d[offset >> 6] >> (offset & 0x3F)) & ((((uint64_t)1) << count) - 1);
53 }
54 
55 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
57  VERIFY_CHECK(count < 32);
58  VERIFY_CHECK(offset + count <= 256);
59 
60  if ((offset + count - 1) >> 6 == offset >> 6) {
61  return secp256k1_scalar_get_bits(a, offset, count);
62  } else {
63  VERIFY_CHECK((offset >> 6) + 1 < 4);
64  return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & ((((uint64_t)1) << count) - 1);
65  }
66 }
67 
69  int yes = 0;
70  int no = 0;
71  no |= (a->d[3] < SECP256K1_N_3); /* No need for a > check. */
72  no |= (a->d[2] < SECP256K1_N_2);
73  yes |= (a->d[2] > SECP256K1_N_2) & ~no;
74  no |= (a->d[1] < SECP256K1_N_1);
75  yes |= (a->d[1] > SECP256K1_N_1) & ~no;
76  yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
77  return yes;
78 }
79 
80 SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, unsigned int overflow) {
82  VERIFY_CHECK(overflow <= 1);
83 
84  secp256k1_u128_from_u64(&t, r->d[0]);
87  secp256k1_u128_accum_u64(&t, r->d[1]);
90  secp256k1_u128_accum_u64(&t, r->d[2]);
93  secp256k1_u128_accum_u64(&t, r->d[3]);
94  r->d[3] = secp256k1_u128_to_u64(&t);
95 
97  return overflow;
98 }
99 
101  int overflow;
105 
106  secp256k1_u128_from_u64(&t, a->d[0]);
107  secp256k1_u128_accum_u64(&t, b->d[0]);
109  secp256k1_u128_accum_u64(&t, a->d[1]);
110  secp256k1_u128_accum_u64(&t, b->d[1]);
112  secp256k1_u128_accum_u64(&t, a->d[2]);
113  secp256k1_u128_accum_u64(&t, b->d[2]);
115  secp256k1_u128_accum_u64(&t, a->d[3]);
116  secp256k1_u128_accum_u64(&t, b->d[3]);
119  VERIFY_CHECK(overflow == 0 || overflow == 1);
120  secp256k1_scalar_reduce(r, overflow);
121 
123  return overflow;
124 }
125 
126 static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
128  volatile int vflag = flag;
130  VERIFY_CHECK(bit < 256);
131 
132  bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 6) > 3 makes this a noop */
133  secp256k1_u128_from_u64(&t, r->d[0]);
134  secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 0)) << (bit & 0x3F));
136  secp256k1_u128_accum_u64(&t, r->d[1]);
137  secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 1)) << (bit & 0x3F));
139  secp256k1_u128_accum_u64(&t, r->d[2]);
140  secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 2)) << (bit & 0x3F));
142  secp256k1_u128_accum_u64(&t, r->d[3]);
143  secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 3)) << (bit & 0x3F));
144  r->d[3] = secp256k1_u128_to_u64(&t);
145 
148 }
149 
150 static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
151  int over;
152  r->d[0] = secp256k1_read_be64(&b32[24]);
153  r->d[1] = secp256k1_read_be64(&b32[16]);
154  r->d[2] = secp256k1_read_be64(&b32[8]);
155  r->d[3] = secp256k1_read_be64(&b32[0]);
157  if (overflow) {
158  *overflow = over;
159  }
160 
162 }
163 
164 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
166 
167  secp256k1_write_be64(&bin[0], a->d[3]);
168  secp256k1_write_be64(&bin[8], a->d[2]);
169  secp256k1_write_be64(&bin[16], a->d[1]);
170  secp256k1_write_be64(&bin[24], a->d[0]);
171 }
172 
175 
176  return (a->d[0] | a->d[1] | a->d[2] | a->d[3]) == 0;
177 }
178 
180  uint64_t nonzero = 0xFFFFFFFFFFFFFFFFULL * (secp256k1_scalar_is_zero(a) == 0);
183 
184  secp256k1_u128_from_u64(&t, ~a->d[0]);
186  r->d[0] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
187  secp256k1_u128_accum_u64(&t, ~a->d[1]);
189  r->d[1] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
190  secp256k1_u128_accum_u64(&t, ~a->d[2]);
192  r->d[2] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
193  secp256k1_u128_accum_u64(&t, ~a->d[3]);
195  r->d[3] = secp256k1_u128_to_u64(&t) & nonzero;
196 
198 }
199 
201  /* Writing `/` for field division and `//` for integer division, we compute
202  *
203  * a/2 = (a - (a&1))/2 + (a&1)/2
204  * = (a >> 1) + (a&1 ? 1/2 : 0)
205  * = (a >> 1) + (a&1 ? n//2+1 : 0),
206  *
207  * where n is the group order and in the last equality we have used 1/2 = n//2+1 (mod n).
208  * For n//2, we have the constants SECP256K1_N_H_0, ...
209  *
210  * This sum does not overflow. The most extreme case is a = -2, the largest odd scalar. Here:
211  * - the left summand is: a >> 1 = (a - a&1)/2 = (n-2-1)//2 = (n-3)//2
212  * - the right summand is: a&1 ? n//2+1 : 0 = n//2+1 = (n-1)//2 + 2//2 = (n+1)//2
213  * Together they sum to (n-3)//2 + (n+1)//2 = (2n-2)//2 = n - 1, which is less than n.
214  */
215  uint64_t mask = -(uint64_t)(a->d[0] & 1U);
218 
219  secp256k1_u128_from_u64(&t, (a->d[0] >> 1) | (a->d[1] << 63));
220  secp256k1_u128_accum_u64(&t, (SECP256K1_N_H_0 + 1U) & mask);
222  secp256k1_u128_accum_u64(&t, (a->d[1] >> 1) | (a->d[2] << 63));
225  secp256k1_u128_accum_u64(&t, (a->d[2] >> 1) | (a->d[3] << 63));
228  r->d[3] = secp256k1_u128_to_u64(&t) + (a->d[3] >> 1) + (SECP256K1_N_H_3 & mask);
229 #ifdef VERIFY
230  /* The line above only computed the bottom 64 bits of r->d[3]; redo the computation
231  * in full 128 bits to make sure the top 64 bits are indeed zero. */
232  secp256k1_u128_accum_u64(&t, a->d[3] >> 1);
234  secp256k1_u128_rshift(&t, 64);
236 
238 #endif
239 }
240 
243 
244  return ((a->d[0] ^ 1) | a->d[1] | a->d[2] | a->d[3]) == 0;
245 }
246 
248  int yes = 0;
249  int no = 0;
251 
252  no |= (a->d[3] < SECP256K1_N_H_3);
253  yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
254  no |= (a->d[2] < SECP256K1_N_H_2) & ~yes; /* No need for a > check. */
255  no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
256  yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
257  yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
258  return yes;
259 }
260 
262  /* If we are flag = 0, mask = 00...00 and this is a no-op;
263  * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
264  volatile int vflag = flag;
265  uint64_t mask = -vflag;
266  uint64_t nonzero = (secp256k1_scalar_is_zero(r) != 0) - 1;
269 
270  secp256k1_u128_from_u64(&t, r->d[0] ^ mask);
271  secp256k1_u128_accum_u64(&t, (SECP256K1_N_0 + 1) & mask);
272  r->d[0] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
273  secp256k1_u128_accum_u64(&t, r->d[1] ^ mask);
275  r->d[1] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
276  secp256k1_u128_accum_u64(&t, r->d[2] ^ mask);
278  r->d[2] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
279  secp256k1_u128_accum_u64(&t, r->d[3] ^ mask);
281  r->d[3] = secp256k1_u128_to_u64(&t) & nonzero;
282 
284  return 2 * (mask == 0) - 1;
285 }
286 
287 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
288 
290 #define muladd(a,b) { \
291  uint64_t tl, th; \
292  { \
293  secp256k1_uint128 t; \
294  secp256k1_u128_mul(&t, a, b); \
295  th = secp256k1_u128_hi_u64(&t); /* at most 0xFFFFFFFFFFFFFFFE */ \
296  tl = secp256k1_u128_to_u64(&t); \
297  } \
298  c0 += tl; /* overflow is handled on the next line */ \
299  th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
300  c1 += th; /* overflow is handled on the next line */ \
301  c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
302  VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
303 }
304 
306 #define muladd_fast(a,b) { \
307  uint64_t tl, th; \
308  { \
309  secp256k1_uint128 t; \
310  secp256k1_u128_mul(&t, a, b); \
311  th = secp256k1_u128_hi_u64(&t); /* at most 0xFFFFFFFFFFFFFFFE */ \
312  tl = secp256k1_u128_to_u64(&t); \
313  } \
314  c0 += tl; /* overflow is handled on the next line */ \
315  th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
316  c1 += th; /* never overflows by contract (verified in the next line) */ \
317  VERIFY_CHECK(c1 >= th); \
318 }
319 
321 #define sumadd(a) { \
322  unsigned int over; \
323  c0 += (a); /* overflow is handled on the next line */ \
324  over = (c0 < (a)); \
325  c1 += over; /* overflow is handled on the next line */ \
326  c2 += (c1 < over); /* never overflows by contract */ \
327 }
328 
330 #define sumadd_fast(a) { \
331  c0 += (a); /* overflow is handled on the next line */ \
332  c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
333  VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
334  VERIFY_CHECK(c2 == 0); \
335 }
336 
338 #define extract(n) { \
339  (n) = c0; \
340  c0 = c1; \
341  c1 = c2; \
342  c2 = 0; \
343 }
344 
346 #define extract_fast(n) { \
347  (n) = c0; \
348  c0 = c1; \
349  c1 = 0; \
350  VERIFY_CHECK(c2 == 0); \
351 }
352 
353 static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l) {
354 #ifdef USE_ASM_X86_64
355  /* Reduce 512 bits into 385. */
356  uint64_t m0, m1, m2, m3, m4, m5, m6;
357  uint64_t p0, p1, p2, p3, p4;
358  uint64_t c;
359 
360  __asm__ __volatile__(
361  /* Preload. */
362  "movq 32(%%rsi), %%r11\n"
363  "movq 40(%%rsi), %%r12\n"
364  "movq 48(%%rsi), %%r13\n"
365  "movq 56(%%rsi), %%r14\n"
366  /* Initialize r8,r9,r10 */
367  "movq 0(%%rsi), %%r8\n"
368  "xorq %%r9, %%r9\n"
369  "xorq %%r10, %%r10\n"
370  /* (r8,r9) += n0 * c0 */
371  "movq %8, %%rax\n"
372  "mulq %%r11\n"
373  "addq %%rax, %%r8\n"
374  "adcq %%rdx, %%r9\n"
375  /* extract m0 */
376  "movq %%r8, %q0\n"
377  "xorq %%r8, %%r8\n"
378  /* (r9,r10) += l1 */
379  "addq 8(%%rsi), %%r9\n"
380  "adcq $0, %%r10\n"
381  /* (r9,r10,r8) += n1 * c0 */
382  "movq %8, %%rax\n"
383  "mulq %%r12\n"
384  "addq %%rax, %%r9\n"
385  "adcq %%rdx, %%r10\n"
386  "adcq $0, %%r8\n"
387  /* (r9,r10,r8) += n0 * c1 */
388  "movq %9, %%rax\n"
389  "mulq %%r11\n"
390  "addq %%rax, %%r9\n"
391  "adcq %%rdx, %%r10\n"
392  "adcq $0, %%r8\n"
393  /* extract m1 */
394  "movq %%r9, %q1\n"
395  "xorq %%r9, %%r9\n"
396  /* (r10,r8,r9) += l2 */
397  "addq 16(%%rsi), %%r10\n"
398  "adcq $0, %%r8\n"
399  "adcq $0, %%r9\n"
400  /* (r10,r8,r9) += n2 * c0 */
401  "movq %8, %%rax\n"
402  "mulq %%r13\n"
403  "addq %%rax, %%r10\n"
404  "adcq %%rdx, %%r8\n"
405  "adcq $0, %%r9\n"
406  /* (r10,r8,r9) += n1 * c1 */
407  "movq %9, %%rax\n"
408  "mulq %%r12\n"
409  "addq %%rax, %%r10\n"
410  "adcq %%rdx, %%r8\n"
411  "adcq $0, %%r9\n"
412  /* (r10,r8,r9) += n0 */
413  "addq %%r11, %%r10\n"
414  "adcq $0, %%r8\n"
415  "adcq $0, %%r9\n"
416  /* extract m2 */
417  "movq %%r10, %q2\n"
418  "xorq %%r10, %%r10\n"
419  /* (r8,r9,r10) += l3 */
420  "addq 24(%%rsi), %%r8\n"
421  "adcq $0, %%r9\n"
422  "adcq $0, %%r10\n"
423  /* (r8,r9,r10) += n3 * c0 */
424  "movq %8, %%rax\n"
425  "mulq %%r14\n"
426  "addq %%rax, %%r8\n"
427  "adcq %%rdx, %%r9\n"
428  "adcq $0, %%r10\n"
429  /* (r8,r9,r10) += n2 * c1 */
430  "movq %9, %%rax\n"
431  "mulq %%r13\n"
432  "addq %%rax, %%r8\n"
433  "adcq %%rdx, %%r9\n"
434  "adcq $0, %%r10\n"
435  /* (r8,r9,r10) += n1 */
436  "addq %%r12, %%r8\n"
437  "adcq $0, %%r9\n"
438  "adcq $0, %%r10\n"
439  /* extract m3 */
440  "movq %%r8, %q3\n"
441  "xorq %%r8, %%r8\n"
442  /* (r9,r10,r8) += n3 * c1 */
443  "movq %9, %%rax\n"
444  "mulq %%r14\n"
445  "addq %%rax, %%r9\n"
446  "adcq %%rdx, %%r10\n"
447  "adcq $0, %%r8\n"
448  /* (r9,r10,r8) += n2 */
449  "addq %%r13, %%r9\n"
450  "adcq $0, %%r10\n"
451  "adcq $0, %%r8\n"
452  /* extract m4 */
453  "movq %%r9, %q4\n"
454  /* (r10,r8) += n3 */
455  "addq %%r14, %%r10\n"
456  "adcq $0, %%r8\n"
457  /* extract m5 */
458  "movq %%r10, %q5\n"
459  /* extract m6 */
460  "movq %%r8, %q6\n"
461  : "=&g"(m0), "=&g"(m1), "=&g"(m2), "=g"(m3), "=g"(m4), "=g"(m5), "=g"(m6)
462  : "S"(l), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
463  : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "cc");
464 
465  SECP256K1_CHECKMEM_MSAN_DEFINE(&m0, sizeof(m0));
466  SECP256K1_CHECKMEM_MSAN_DEFINE(&m1, sizeof(m1));
467  SECP256K1_CHECKMEM_MSAN_DEFINE(&m2, sizeof(m2));
468  SECP256K1_CHECKMEM_MSAN_DEFINE(&m3, sizeof(m3));
469  SECP256K1_CHECKMEM_MSAN_DEFINE(&m4, sizeof(m4));
470  SECP256K1_CHECKMEM_MSAN_DEFINE(&m5, sizeof(m5));
471  SECP256K1_CHECKMEM_MSAN_DEFINE(&m6, sizeof(m6));
472 
473  /* Reduce 385 bits into 258. */
474  __asm__ __volatile__(
475  /* Preload */
476  "movq %q9, %%r11\n"
477  "movq %q10, %%r12\n"
478  "movq %q11, %%r13\n"
479  /* Initialize (r8,r9,r10) */
480  "movq %q5, %%r8\n"
481  "xorq %%r9, %%r9\n"
482  "xorq %%r10, %%r10\n"
483  /* (r8,r9) += m4 * c0 */
484  "movq %12, %%rax\n"
485  "mulq %%r11\n"
486  "addq %%rax, %%r8\n"
487  "adcq %%rdx, %%r9\n"
488  /* extract p0 */
489  "movq %%r8, %q0\n"
490  "xorq %%r8, %%r8\n"
491  /* (r9,r10) += m1 */
492  "addq %q6, %%r9\n"
493  "adcq $0, %%r10\n"
494  /* (r9,r10,r8) += m5 * c0 */
495  "movq %12, %%rax\n"
496  "mulq %%r12\n"
497  "addq %%rax, %%r9\n"
498  "adcq %%rdx, %%r10\n"
499  "adcq $0, %%r8\n"
500  /* (r9,r10,r8) += m4 * c1 */
501  "movq %13, %%rax\n"
502  "mulq %%r11\n"
503  "addq %%rax, %%r9\n"
504  "adcq %%rdx, %%r10\n"
505  "adcq $0, %%r8\n"
506  /* extract p1 */
507  "movq %%r9, %q1\n"
508  "xorq %%r9, %%r9\n"
509  /* (r10,r8,r9) += m2 */
510  "addq %q7, %%r10\n"
511  "adcq $0, %%r8\n"
512  "adcq $0, %%r9\n"
513  /* (r10,r8,r9) += m6 * c0 */
514  "movq %12, %%rax\n"
515  "mulq %%r13\n"
516  "addq %%rax, %%r10\n"
517  "adcq %%rdx, %%r8\n"
518  "adcq $0, %%r9\n"
519  /* (r10,r8,r9) += m5 * c1 */
520  "movq %13, %%rax\n"
521  "mulq %%r12\n"
522  "addq %%rax, %%r10\n"
523  "adcq %%rdx, %%r8\n"
524  "adcq $0, %%r9\n"
525  /* (r10,r8,r9) += m4 */
526  "addq %%r11, %%r10\n"
527  "adcq $0, %%r8\n"
528  "adcq $0, %%r9\n"
529  /* extract p2 */
530  "movq %%r10, %q2\n"
531  /* (r8,r9) += m3 */
532  "addq %q8, %%r8\n"
533  "adcq $0, %%r9\n"
534  /* (r8,r9) += m6 * c1 */
535  "movq %13, %%rax\n"
536  "mulq %%r13\n"
537  "addq %%rax, %%r8\n"
538  "adcq %%rdx, %%r9\n"
539  /* (r8,r9) += m5 */
540  "addq %%r12, %%r8\n"
541  "adcq $0, %%r9\n"
542  /* extract p3 */
543  "movq %%r8, %q3\n"
544  /* (r9) += m6 */
545  "addq %%r13, %%r9\n"
546  /* extract p4 */
547  "movq %%r9, %q4\n"
548  : "=&g"(p0), "=&g"(p1), "=&g"(p2), "=g"(p3), "=g"(p4)
549  : "g"(m0), "g"(m1), "g"(m2), "g"(m3), "g"(m4), "g"(m5), "g"(m6), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
550  : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "cc");
551 
552  SECP256K1_CHECKMEM_MSAN_DEFINE(&p0, sizeof(p0));
553  SECP256K1_CHECKMEM_MSAN_DEFINE(&p1, sizeof(p1));
554  SECP256K1_CHECKMEM_MSAN_DEFINE(&p2, sizeof(p2));
555  SECP256K1_CHECKMEM_MSAN_DEFINE(&p3, sizeof(p3));
556  SECP256K1_CHECKMEM_MSAN_DEFINE(&p4, sizeof(p4));
557 
558  /* Reduce 258 bits into 256. */
559  __asm__ __volatile__(
560  /* Preload */
561  "movq %q5, %%r10\n"
562  /* (rax,rdx) = p4 * c0 */
563  "movq %7, %%rax\n"
564  "mulq %%r10\n"
565  /* (rax,rdx) += p0 */
566  "addq %q1, %%rax\n"
567  "adcq $0, %%rdx\n"
568  /* extract r0 */
569  "movq %%rax, 0(%q6)\n"
570  /* Move to (r8,r9) */
571  "movq %%rdx, %%r8\n"
572  "xorq %%r9, %%r9\n"
573  /* (r8,r9) += p1 */
574  "addq %q2, %%r8\n"
575  "adcq $0, %%r9\n"
576  /* (r8,r9) += p4 * c1 */
577  "movq %8, %%rax\n"
578  "mulq %%r10\n"
579  "addq %%rax, %%r8\n"
580  "adcq %%rdx, %%r9\n"
581  /* Extract r1 */
582  "movq %%r8, 8(%q6)\n"
583  "xorq %%r8, %%r8\n"
584  /* (r9,r8) += p4 */
585  "addq %%r10, %%r9\n"
586  "adcq $0, %%r8\n"
587  /* (r9,r8) += p2 */
588  "addq %q3, %%r9\n"
589  "adcq $0, %%r8\n"
590  /* Extract r2 */
591  "movq %%r9, 16(%q6)\n"
592  "xorq %%r9, %%r9\n"
593  /* (r8,r9) += p3 */
594  "addq %q4, %%r8\n"
595  "adcq $0, %%r9\n"
596  /* Extract r3 */
597  "movq %%r8, 24(%q6)\n"
598  /* Extract c */
599  "movq %%r9, %q0\n"
600  : "=g"(c)
601  : "g"(p0), "g"(p1), "g"(p2), "g"(p3), "g"(p4), "D"(r), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
602  : "rax", "rdx", "r8", "r9", "r10", "cc", "memory");
603 
604  SECP256K1_CHECKMEM_MSAN_DEFINE(r, sizeof(*r));
605  SECP256K1_CHECKMEM_MSAN_DEFINE(&c, sizeof(c));
606 
607 #else
608  secp256k1_uint128 c128;
609  uint64_t c, c0, c1, c2;
610  uint64_t n0 = l[4], n1 = l[5], n2 = l[6], n3 = l[7];
611  uint64_t m0, m1, m2, m3, m4, m5;
612  uint32_t m6;
613  uint64_t p0, p1, p2, p3;
614  uint32_t p4;
615 
616  /* Reduce 512 bits into 385. */
617  /* m[0..6] = l[0..3] + n[0..3] * SECP256K1_N_C. */
618  c0 = l[0]; c1 = 0; c2 = 0;
620  extract_fast(m0);
621  sumadd_fast(l[1]);
622  muladd(n1, SECP256K1_N_C_0);
623  muladd(n0, SECP256K1_N_C_1);
624  extract(m1);
625  sumadd(l[2]);
626  muladd(n2, SECP256K1_N_C_0);
627  muladd(n1, SECP256K1_N_C_1);
628  sumadd(n0);
629  extract(m2);
630  sumadd(l[3]);
631  muladd(n3, SECP256K1_N_C_0);
632  muladd(n2, SECP256K1_N_C_1);
633  sumadd(n1);
634  extract(m3);
635  muladd(n3, SECP256K1_N_C_1);
636  sumadd(n2);
637  extract(m4);
638  sumadd_fast(n3);
639  extract_fast(m5);
640  VERIFY_CHECK(c0 <= 1);
641  m6 = c0;
642 
643  /* Reduce 385 bits into 258. */
644  /* p[0..4] = m[0..3] + m[4..6] * SECP256K1_N_C. */
645  c0 = m0; c1 = 0; c2 = 0;
647  extract_fast(p0);
648  sumadd_fast(m1);
649  muladd(m5, SECP256K1_N_C_0);
650  muladd(m4, SECP256K1_N_C_1);
651  extract(p1);
652  sumadd(m2);
653  muladd(m6, SECP256K1_N_C_0);
654  muladd(m5, SECP256K1_N_C_1);
655  sumadd(m4);
656  extract(p2);
657  sumadd_fast(m3);
659  sumadd_fast(m5);
660  extract_fast(p3);
661  p4 = c0 + m6;
662  VERIFY_CHECK(p4 <= 2);
663 
664  /* Reduce 258 bits into 256. */
665  /* r[0..3] = p[0..3] + p[4] * SECP256K1_N_C. */
666  secp256k1_u128_from_u64(&c128, p0);
668  r->d[0] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
669  secp256k1_u128_accum_u64(&c128, p1);
671  r->d[1] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
672  secp256k1_u128_accum_u64(&c128, p2);
673  secp256k1_u128_accum_u64(&c128, p4);
674  r->d[2] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
675  secp256k1_u128_accum_u64(&c128, p3);
676  r->d[3] = secp256k1_u128_to_u64(&c128);
677  c = secp256k1_u128_hi_u64(&c128);
678 #endif
679 
680  /* Final reduction of r. */
682 }
683 
684 static void secp256k1_scalar_mul_512(uint64_t *l8, const secp256k1_scalar *a, const secp256k1_scalar *b) {
685 #ifdef USE_ASM_X86_64
686  const uint64_t *pb = b->d;
687  __asm__ __volatile__(
688  /* Preload */
689  "movq 0(%%rdi), %%r15\n"
690  "movq 8(%%rdi), %%rbx\n"
691  "movq 16(%%rdi), %%rcx\n"
692  "movq 0(%%rdx), %%r11\n"
693  "movq 8(%%rdx), %%r12\n"
694  "movq 16(%%rdx), %%r13\n"
695  "movq 24(%%rdx), %%r14\n"
696  /* (rax,rdx) = a0 * b0 */
697  "movq %%r15, %%rax\n"
698  "mulq %%r11\n"
699  /* Extract l8[0] */
700  "movq %%rax, 0(%%rsi)\n"
701  /* (r8,r9,r10) = (rdx) */
702  "movq %%rdx, %%r8\n"
703  "xorq %%r9, %%r9\n"
704  "xorq %%r10, %%r10\n"
705  /* (r8,r9,r10) += a0 * b1 */
706  "movq %%r15, %%rax\n"
707  "mulq %%r12\n"
708  "addq %%rax, %%r8\n"
709  "adcq %%rdx, %%r9\n"
710  "adcq $0, %%r10\n"
711  /* (r8,r9,r10) += a1 * b0 */
712  "movq %%rbx, %%rax\n"
713  "mulq %%r11\n"
714  "addq %%rax, %%r8\n"
715  "adcq %%rdx, %%r9\n"
716  "adcq $0, %%r10\n"
717  /* Extract l8[1] */
718  "movq %%r8, 8(%%rsi)\n"
719  "xorq %%r8, %%r8\n"
720  /* (r9,r10,r8) += a0 * b2 */
721  "movq %%r15, %%rax\n"
722  "mulq %%r13\n"
723  "addq %%rax, %%r9\n"
724  "adcq %%rdx, %%r10\n"
725  "adcq $0, %%r8\n"
726  /* (r9,r10,r8) += a1 * b1 */
727  "movq %%rbx, %%rax\n"
728  "mulq %%r12\n"
729  "addq %%rax, %%r9\n"
730  "adcq %%rdx, %%r10\n"
731  "adcq $0, %%r8\n"
732  /* (r9,r10,r8) += a2 * b0 */
733  "movq %%rcx, %%rax\n"
734  "mulq %%r11\n"
735  "addq %%rax, %%r9\n"
736  "adcq %%rdx, %%r10\n"
737  "adcq $0, %%r8\n"
738  /* Extract l8[2] */
739  "movq %%r9, 16(%%rsi)\n"
740  "xorq %%r9, %%r9\n"
741  /* (r10,r8,r9) += a0 * b3 */
742  "movq %%r15, %%rax\n"
743  "mulq %%r14\n"
744  "addq %%rax, %%r10\n"
745  "adcq %%rdx, %%r8\n"
746  "adcq $0, %%r9\n"
747  /* Preload a3 */
748  "movq 24(%%rdi), %%r15\n"
749  /* (r10,r8,r9) += a1 * b2 */
750  "movq %%rbx, %%rax\n"
751  "mulq %%r13\n"
752  "addq %%rax, %%r10\n"
753  "adcq %%rdx, %%r8\n"
754  "adcq $0, %%r9\n"
755  /* (r10,r8,r9) += a2 * b1 */
756  "movq %%rcx, %%rax\n"
757  "mulq %%r12\n"
758  "addq %%rax, %%r10\n"
759  "adcq %%rdx, %%r8\n"
760  "adcq $0, %%r9\n"
761  /* (r10,r8,r9) += a3 * b0 */
762  "movq %%r15, %%rax\n"
763  "mulq %%r11\n"
764  "addq %%rax, %%r10\n"
765  "adcq %%rdx, %%r8\n"
766  "adcq $0, %%r9\n"
767  /* Extract l8[3] */
768  "movq %%r10, 24(%%rsi)\n"
769  "xorq %%r10, %%r10\n"
770  /* (r8,r9,r10) += a1 * b3 */
771  "movq %%rbx, %%rax\n"
772  "mulq %%r14\n"
773  "addq %%rax, %%r8\n"
774  "adcq %%rdx, %%r9\n"
775  "adcq $0, %%r10\n"
776  /* (r8,r9,r10) += a2 * b2 */
777  "movq %%rcx, %%rax\n"
778  "mulq %%r13\n"
779  "addq %%rax, %%r8\n"
780  "adcq %%rdx, %%r9\n"
781  "adcq $0, %%r10\n"
782  /* (r8,r9,r10) += a3 * b1 */
783  "movq %%r15, %%rax\n"
784  "mulq %%r12\n"
785  "addq %%rax, %%r8\n"
786  "adcq %%rdx, %%r9\n"
787  "adcq $0, %%r10\n"
788  /* Extract l8[4] */
789  "movq %%r8, 32(%%rsi)\n"
790  "xorq %%r8, %%r8\n"
791  /* (r9,r10,r8) += a2 * b3 */
792  "movq %%rcx, %%rax\n"
793  "mulq %%r14\n"
794  "addq %%rax, %%r9\n"
795  "adcq %%rdx, %%r10\n"
796  "adcq $0, %%r8\n"
797  /* (r9,r10,r8) += a3 * b2 */
798  "movq %%r15, %%rax\n"
799  "mulq %%r13\n"
800  "addq %%rax, %%r9\n"
801  "adcq %%rdx, %%r10\n"
802  "adcq $0, %%r8\n"
803  /* Extract l8[5] */
804  "movq %%r9, 40(%%rsi)\n"
805  /* (r10,r8) += a3 * b3 */
806  "movq %%r15, %%rax\n"
807  "mulq %%r14\n"
808  "addq %%rax, %%r10\n"
809  "adcq %%rdx, %%r8\n"
810  /* Extract l8[6] */
811  "movq %%r10, 48(%%rsi)\n"
812  /* Extract l8[7] */
813  "movq %%r8, 56(%%rsi)\n"
814  : "+d"(pb)
815  : "S"(l8), "D"(a->d)
816  : "rax", "rbx", "rcx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "cc", "memory");
817 
818  SECP256K1_CHECKMEM_MSAN_DEFINE(l8, sizeof(*l8) * 8);
819 
820 #else
821  /* 160 bit accumulator. */
822  uint64_t c0 = 0, c1 = 0;
823  uint32_t c2 = 0;
824 
825  /* l8[0..7] = a[0..3] * b[0..3]. */
826  muladd_fast(a->d[0], b->d[0]);
827  extract_fast(l8[0]);
828  muladd(a->d[0], b->d[1]);
829  muladd(a->d[1], b->d[0]);
830  extract(l8[1]);
831  muladd(a->d[0], b->d[2]);
832  muladd(a->d[1], b->d[1]);
833  muladd(a->d[2], b->d[0]);
834  extract(l8[2]);
835  muladd(a->d[0], b->d[3]);
836  muladd(a->d[1], b->d[2]);
837  muladd(a->d[2], b->d[1]);
838  muladd(a->d[3], b->d[0]);
839  extract(l8[3]);
840  muladd(a->d[1], b->d[3]);
841  muladd(a->d[2], b->d[2]);
842  muladd(a->d[3], b->d[1]);
843  extract(l8[4]);
844  muladd(a->d[2], b->d[3]);
845  muladd(a->d[3], b->d[2]);
846  extract(l8[5]);
847  muladd_fast(a->d[3], b->d[3]);
848  extract_fast(l8[6]);
849  VERIFY_CHECK(c1 == 0);
850  l8[7] = c0;
851 #endif
852 }
853 
854 #undef sumadd
855 #undef sumadd_fast
856 #undef muladd
857 #undef muladd_fast
858 #undef extract
859 #undef extract_fast
860 
862  uint64_t l[8];
865 
866  secp256k1_scalar_mul_512(l, a, b);
868 
870 }
871 
874 
875  r1->d[0] = k->d[0];
876  r1->d[1] = k->d[1];
877  r1->d[2] = 0;
878  r1->d[3] = 0;
879  r2->d[0] = k->d[2];
880  r2->d[1] = k->d[3];
881  r2->d[2] = 0;
882  r2->d[3] = 0;
883 
886 }
887 
891 
892  return ((a->d[0] ^ b->d[0]) | (a->d[1] ^ b->d[1]) | (a->d[2] ^ b->d[2]) | (a->d[3] ^ b->d[3])) == 0;
893 }
894 
896  uint64_t l[8];
897  unsigned int shiftlimbs;
898  unsigned int shiftlow;
899  unsigned int shifthigh;
902  VERIFY_CHECK(shift >= 256);
903 
904  secp256k1_scalar_mul_512(l, a, b);
905  shiftlimbs = shift >> 6;
906  shiftlow = shift & 0x3F;
907  shifthigh = 64 - shiftlow;
908  r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
909  r->d[1] = shift < 448 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
910  r->d[2] = shift < 384 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
911  r->d[3] = shift < 320 ? (l[3 + shiftlimbs] >> shiftlow) : 0;
912  secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1);
913 
915 }
916 
918  uint64_t mask0, mask1;
919  volatile int vflag = flag;
921  SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
922 
923  mask0 = vflag + ~((uint64_t)0);
924  mask1 = ~mask0;
925  r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
926  r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
927  r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
928  r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
929 
931 }
932 
934  const uint64_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4];
935 
936  /* The output from secp256k1_modinv64{_var} should be normalized to range [0,modulus), and
937  * have limbs in [0,2^62). The modulus is < 2^256, so the top limb must be below 2^(256-62*4).
938  */
939  VERIFY_CHECK(a0 >> 62 == 0);
940  VERIFY_CHECK(a1 >> 62 == 0);
941  VERIFY_CHECK(a2 >> 62 == 0);
942  VERIFY_CHECK(a3 >> 62 == 0);
943  VERIFY_CHECK(a4 >> 8 == 0);
944 
945  r->d[0] = a0 | a1 << 62;
946  r->d[1] = a1 >> 2 | a2 << 60;
947  r->d[2] = a2 >> 4 | a3 << 58;
948  r->d[3] = a3 >> 6 | a4 << 56;
949 
951 }
952 
954  const uint64_t M62 = UINT64_MAX >> 2;
955  const uint64_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3];
957 
958  r->v[0] = a0 & M62;
959  r->v[1] = (a0 >> 62 | a1 << 2) & M62;
960  r->v[2] = (a1 >> 60 | a2 << 4) & M62;
961  r->v[3] = (a2 >> 58 | a3 << 6) & M62;
962  r->v[4] = a3 >> 56;
963 }
964 
966  {{0x3FD25E8CD0364141LL, 0x2ABB739ABD2280EELL, -0x15LL, 0, 256}},
967  0x34F20099AA774EC1LL
968 };
969 
972 #ifdef VERIFY
973  int zero_in = secp256k1_scalar_is_zero(x);
974 #endif
976 
980 
982  VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in);
983 }
984 
987 #ifdef VERIFY
988  int zero_in = secp256k1_scalar_is_zero(x);
989 #endif
991 
995 
997  VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in);
998 }
999 
1002 
1003  return !(a->d[0] & 1);
1004 }
1005 
1006 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_MSAN_DEFINE(p, len)
Definition: checkmem.h:60
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:99
static SECP256K1_INLINE uint64_t secp256k1_u128_hi_u64(const secp256k1_uint128 *a)
static SECP256K1_INLINE void secp256k1_u128_from_u64(secp256k1_uint128 *r, uint64_t a)
static SECP256K1_INLINE void secp256k1_u128_rshift(secp256k1_uint128 *r, unsigned int n)
static SECP256K1_INLINE void secp256k1_u128_accum_u64(secp256k1_uint128 *r, uint64_t a)
static SECP256K1_INLINE void secp256k1_u128_accum_mul(secp256k1_uint128 *r, uint64_t a, uint64_t b)
static SECP256K1_INLINE uint64_t secp256k1_u128_to_u64(const secp256k1_uint128 *a)
static void secp256k1_modinv64(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static void secp256k1_modinv64_var(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
#define SECP256K1_SCALAR_VERIFY(r)
Definition: scalar.h:103
static SECP256K1_INLINE int secp256k1_scalar_is_even(const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_check_overflow(const secp256k1_scalar *a)
static SECP256K1_INLINE void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b, unsigned int shift)
static void secp256k1_scalar_half(secp256k1_scalar *r, const secp256k1_scalar *a)
#define SECP256K1_N_3
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k)
static SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static SECP256K1_INLINE void secp256k1_scalar_clear(secp256k1_scalar *r)
#define extract(n)
Extract the lowest 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits.
#define SECP256K1_N_C_2
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow)
#define SECP256K1_N_C_1
static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x)
static const secp256k1_modinv64_modinfo secp256k1_const_modinfo_scalar
#define sumadd_fast(a)
Add a to the number defined by (c0,c1).
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
#define SECP256K1_N_1
static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
#define SECP256K1_N_2
#define SECP256K1_N_H_2
static void secp256k1_scalar_from_signed62(secp256k1_scalar *r, const secp256k1_modinv64_signed62 *a)
static SECP256K1_INLINE void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x)
#define SECP256K1_N_C_0
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
#define extract_fast(n)
Extract the lowest 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits.
#define muladd(a, b)
Add a*b to the number defined by (c0,c1,c2).
static void secp256k1_scalar_to_signed62(secp256k1_modinv64_signed62 *r, const secp256k1_scalar *a)
#define SECP256K1_N_H_0
static SECP256K1_INLINE int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
#define sumadd(a)
Add a to the number defined by (c0,c1,c2).
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag)
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
#define SECP256K1_N_H_1
static SECP256K1_INLINE int secp256k1_scalar_reduce(secp256k1_scalar *r, unsigned int overflow)
#define SECP256K1_N_0
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
static SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_mul_512(uint64_t *l8, const secp256k1_scalar *a, const secp256k1_scalar *b)
#define SECP256K1_N_H_3
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag)
#define muladd_fast(a, b)
Add a*b to the number defined by (c0,c1).
static SECP256K1_INLINE int secp256k1_scalar_is_one(const secp256k1_scalar *a)
#define SECP256K1_INLINE
Definition: util.h:48
static SECP256K1_INLINE void secp256k1_write_be64(unsigned char *p, uint64_t x)
Definition: util.h:383
#define VERIFY_CHECK(cond)
Definition: util.h:153
static SECP256K1_INLINE uint64_t secp256k1_read_be64(const unsigned char *p)
Definition: util.h:371
A scalar modulo the group order of the secp256k1 curve.
Definition: scalar_4x64.h:13
uint64_t d[4]
Definition: scalar_4x64.h:14
static int count