Bitcoin Core  27.99.0
P2P Digital Currency
scalar_8x32_impl.h
Go to the documentation of this file.
1 /***********************************************************************
2  * Copyright (c) 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 "modinv32_impl.h"
12 #include "util.h"
13 
14 /* Limbs of the secp256k1 order. */
15 #define SECP256K1_N_0 ((uint32_t)0xD0364141UL)
16 #define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
17 #define SECP256K1_N_2 ((uint32_t)0xAF48A03BUL)
18 #define SECP256K1_N_3 ((uint32_t)0xBAAEDCE6UL)
19 #define SECP256K1_N_4 ((uint32_t)0xFFFFFFFEUL)
20 #define SECP256K1_N_5 ((uint32_t)0xFFFFFFFFUL)
21 #define SECP256K1_N_6 ((uint32_t)0xFFFFFFFFUL)
22 #define SECP256K1_N_7 ((uint32_t)0xFFFFFFFFUL)
23 
24 /* Limbs of 2^256 minus the secp256k1 order. */
25 #define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
26 #define SECP256K1_N_C_1 (~SECP256K1_N_1)
27 #define SECP256K1_N_C_2 (~SECP256K1_N_2)
28 #define SECP256K1_N_C_3 (~SECP256K1_N_3)
29 #define SECP256K1_N_C_4 (1)
30 
31 /* Limbs of half the secp256k1 order. */
32 #define SECP256K1_N_H_0 ((uint32_t)0x681B20A0UL)
33 #define SECP256K1_N_H_1 ((uint32_t)0xDFE92F46UL)
34 #define SECP256K1_N_H_2 ((uint32_t)0x57A4501DUL)
35 #define SECP256K1_N_H_3 ((uint32_t)0x5D576E73UL)
36 #define SECP256K1_N_H_4 ((uint32_t)0xFFFFFFFFUL)
37 #define SECP256K1_N_H_5 ((uint32_t)0xFFFFFFFFUL)
38 #define SECP256K1_N_H_6 ((uint32_t)0xFFFFFFFFUL)
39 #define SECP256K1_N_H_7 ((uint32_t)0x7FFFFFFFUL)
40 
42  r->d[0] = 0;
43  r->d[1] = 0;
44  r->d[2] = 0;
45  r->d[3] = 0;
46  r->d[4] = 0;
47  r->d[5] = 0;
48  r->d[6] = 0;
49  r->d[7] = 0;
50 }
51 
53  r->d[0] = v;
54  r->d[1] = 0;
55  r->d[2] = 0;
56  r->d[3] = 0;
57  r->d[4] = 0;
58  r->d[5] = 0;
59  r->d[6] = 0;
60  r->d[7] = 0;
61 
63 }
64 
65 SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
67  VERIFY_CHECK(count > 0 && count <= 32);
68  VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
69 
70  return (a->d[offset >> 5] >> (offset & 0x1F)) & (0xFFFFFFFF >> (32 - count));
71 }
72 
73 SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
75  VERIFY_CHECK(count > 0 && count <= 32);
76  VERIFY_CHECK(offset + count <= 256);
77 
78  if ((offset + count - 1) >> 5 == offset >> 5) {
79  return secp256k1_scalar_get_bits_limb32(a, offset, count);
80  } else {
81  VERIFY_CHECK((offset >> 5) + 1 < 8);
82  return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & (0xFFFFFFFF >> (32 - count));
83  }
84 }
85 
87  int yes = 0;
88  int no = 0;
89  no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */
90  no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */
91  no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */
92  no |= (a->d[4] < SECP256K1_N_4);
93  yes |= (a->d[4] > SECP256K1_N_4) & ~no;
94  no |= (a->d[3] < SECP256K1_N_3) & ~yes;
95  yes |= (a->d[3] > SECP256K1_N_3) & ~no;
96  no |= (a->d[2] < SECP256K1_N_2) & ~yes;
97  yes |= (a->d[2] > SECP256K1_N_2) & ~no;
98  no |= (a->d[1] < SECP256K1_N_1) & ~yes;
99  yes |= (a->d[1] > SECP256K1_N_1) & ~no;
100  yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
101  return yes;
102 }
103 
104 SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, uint32_t overflow) {
105  uint64_t t;
106  VERIFY_CHECK(overflow <= 1);
107 
108  t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
109  r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
110  t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
111  r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
112  t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
113  r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
114  t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
115  r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
116  t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
117  r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
118  t += (uint64_t)r->d[5];
119  r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
120  t += (uint64_t)r->d[6];
121  r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
122  t += (uint64_t)r->d[7];
123  r->d[7] = t & 0xFFFFFFFFUL;
124 
126  return overflow;
127 }
128 
130  int overflow;
131  uint64_t t = (uint64_t)a->d[0] + b->d[0];
134 
135  r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
136  t += (uint64_t)a->d[1] + b->d[1];
137  r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
138  t += (uint64_t)a->d[2] + b->d[2];
139  r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
140  t += (uint64_t)a->d[3] + b->d[3];
141  r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
142  t += (uint64_t)a->d[4] + b->d[4];
143  r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
144  t += (uint64_t)a->d[5] + b->d[5];
145  r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
146  t += (uint64_t)a->d[6] + b->d[6];
147  r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
148  t += (uint64_t)a->d[7] + b->d[7];
149  r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
150  overflow = t + secp256k1_scalar_check_overflow(r);
151  VERIFY_CHECK(overflow == 0 || overflow == 1);
152  secp256k1_scalar_reduce(r, overflow);
153 
155  return overflow;
156 }
157 
158 static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
159  uint64_t t;
160  volatile int vflag = flag;
162  VERIFY_CHECK(bit < 256);
163 
164  bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
165  t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
166  r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
167  t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
168  r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
169  t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
170  r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
171  t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
172  r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
173  t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
174  r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
175  t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
176  r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
177  t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
178  r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
179  t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
180  r->d[7] = t & 0xFFFFFFFFULL;
181 
183  VERIFY_CHECK((t >> 32) == 0);
184 }
185 
186 static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
187  int over;
188  r->d[0] = secp256k1_read_be32(&b32[28]);
189  r->d[1] = secp256k1_read_be32(&b32[24]);
190  r->d[2] = secp256k1_read_be32(&b32[20]);
191  r->d[3] = secp256k1_read_be32(&b32[16]);
192  r->d[4] = secp256k1_read_be32(&b32[12]);
193  r->d[5] = secp256k1_read_be32(&b32[8]);
194  r->d[6] = secp256k1_read_be32(&b32[4]);
195  r->d[7] = secp256k1_read_be32(&b32[0]);
197  if (overflow) {
198  *overflow = over;
199  }
200 
202 }
203 
204 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
206 
207  secp256k1_write_be32(&bin[0], a->d[7]);
208  secp256k1_write_be32(&bin[4], a->d[6]);
209  secp256k1_write_be32(&bin[8], a->d[5]);
210  secp256k1_write_be32(&bin[12], a->d[4]);
211  secp256k1_write_be32(&bin[16], a->d[3]);
212  secp256k1_write_be32(&bin[20], a->d[2]);
213  secp256k1_write_be32(&bin[24], a->d[1]);
214  secp256k1_write_be32(&bin[28], a->d[0]);
215 }
216 
219 
220  return (a->d[0] | a->d[1] | a->d[2] | a->d[3] | a->d[4] | a->d[5] | a->d[6] | a->d[7]) == 0;
221 }
222 
224  uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
225  uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
227 
228  r->d[0] = t & nonzero; t >>= 32;
229  t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
230  r->d[1] = t & nonzero; t >>= 32;
231  t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
232  r->d[2] = t & nonzero; t >>= 32;
233  t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
234  r->d[3] = t & nonzero; t >>= 32;
235  t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
236  r->d[4] = t & nonzero; t >>= 32;
237  t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
238  r->d[5] = t & nonzero; t >>= 32;
239  t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
240  r->d[6] = t & nonzero; t >>= 32;
241  t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
242  r->d[7] = t & nonzero;
243 
245 }
246 
248  /* Writing `/` for field division and `//` for integer division, we compute
249  *
250  * a/2 = (a - (a&1))/2 + (a&1)/2
251  * = (a >> 1) + (a&1 ? 1/2 : 0)
252  * = (a >> 1) + (a&1 ? n//2+1 : 0),
253  *
254  * where n is the group order and in the last equality we have used 1/2 = n//2+1 (mod n).
255  * For n//2, we have the constants SECP256K1_N_H_0, ...
256  *
257  * This sum does not overflow. The most extreme case is a = -2, the largest odd scalar. Here:
258  * - the left summand is: a >> 1 = (a - a&1)/2 = (n-2-1)//2 = (n-3)//2
259  * - the right summand is: a&1 ? n//2+1 : 0 = n//2+1 = (n-1)//2 + 2//2 = (n+1)//2
260  * Together they sum to (n-3)//2 + (n+1)//2 = (2n-2)//2 = n - 1, which is less than n.
261  */
262  uint32_t mask = -(uint32_t)(a->d[0] & 1U);
263  uint64_t t = (uint32_t)((a->d[0] >> 1) | (a->d[1] << 31));
265 
266  t += (SECP256K1_N_H_0 + 1U) & mask;
267  r->d[0] = t; t >>= 32;
268  t += (uint32_t)((a->d[1] >> 1) | (a->d[2] << 31));
269  t += SECP256K1_N_H_1 & mask;
270  r->d[1] = t; t >>= 32;
271  t += (uint32_t)((a->d[2] >> 1) | (a->d[3] << 31));
272  t += SECP256K1_N_H_2 & mask;
273  r->d[2] = t; t >>= 32;
274  t += (uint32_t)((a->d[3] >> 1) | (a->d[4] << 31));
275  t += SECP256K1_N_H_3 & mask;
276  r->d[3] = t; t >>= 32;
277  t += (uint32_t)((a->d[4] >> 1) | (a->d[5] << 31));
278  t += SECP256K1_N_H_4 & mask;
279  r->d[4] = t; t >>= 32;
280  t += (uint32_t)((a->d[5] >> 1) | (a->d[6] << 31));
281  t += SECP256K1_N_H_5 & mask;
282  r->d[5] = t; t >>= 32;
283  t += (uint32_t)((a->d[6] >> 1) | (a->d[7] << 31));
284  t += SECP256K1_N_H_6 & mask;
285  r->d[6] = t; t >>= 32;
286  r->d[7] = (uint32_t)t + (uint32_t)(a->d[7] >> 1) + (SECP256K1_N_H_7 & mask);
287 
288  /* The line above only computed the bottom 32 bits of r->d[7]. Redo the computation
289  * in full 64 bits to make sure the top 32 bits are indeed zero. */
290  VERIFY_CHECK((t + (a->d[7] >> 1) + (SECP256K1_N_H_7 & mask)) >> 32 == 0);
291 
293 }
294 
297 
298  return ((a->d[0] ^ 1) | a->d[1] | a->d[2] | a->d[3] | a->d[4] | a->d[5] | a->d[6] | a->d[7]) == 0;
299 }
300 
302  int yes = 0;
303  int no = 0;
305 
306  no |= (a->d[7] < SECP256K1_N_H_7);
307  yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
308  no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
309  no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
310  no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
311  no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
312  yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
313  no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
314  yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
315  no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
316  yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
317  yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
318  return yes;
319 }
320 
322  /* If we are flag = 0, mask = 00...00 and this is a no-op;
323  * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
324  volatile int vflag = flag;
325  uint32_t mask = -vflag;
326  uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
327  uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
329 
330  r->d[0] = t & nonzero; t >>= 32;
331  t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask);
332  r->d[1] = t & nonzero; t >>= 32;
333  t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask);
334  r->d[2] = t & nonzero; t >>= 32;
335  t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask);
336  r->d[3] = t & nonzero; t >>= 32;
337  t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask);
338  r->d[4] = t & nonzero; t >>= 32;
339  t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask);
340  r->d[5] = t & nonzero; t >>= 32;
341  t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask);
342  r->d[6] = t & nonzero; t >>= 32;
343  t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask);
344  r->d[7] = t & nonzero;
345 
347  return 2 * (mask == 0) - 1;
348 }
349 
350 
351 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
352 
354 #define muladd(a,b) { \
355  uint32_t tl, th; \
356  { \
357  uint64_t t = (uint64_t)a * b; \
358  th = t >> 32; /* at most 0xFFFFFFFE */ \
359  tl = t; \
360  } \
361  c0 += tl; /* overflow is handled on the next line */ \
362  th += (c0 < tl); /* at most 0xFFFFFFFF */ \
363  c1 += th; /* overflow is handled on the next line */ \
364  c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
365  VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
366 }
367 
369 #define muladd_fast(a,b) { \
370  uint32_t tl, th; \
371  { \
372  uint64_t t = (uint64_t)a * b; \
373  th = t >> 32; /* at most 0xFFFFFFFE */ \
374  tl = t; \
375  } \
376  c0 += tl; /* overflow is handled on the next line */ \
377  th += (c0 < tl); /* at most 0xFFFFFFFF */ \
378  c1 += th; /* never overflows by contract (verified in the next line) */ \
379  VERIFY_CHECK(c1 >= th); \
380 }
381 
383 #define sumadd(a) { \
384  unsigned int over; \
385  c0 += (a); /* overflow is handled on the next line */ \
386  over = (c0 < (a)); \
387  c1 += over; /* overflow is handled on the next line */ \
388  c2 += (c1 < over); /* never overflows by contract */ \
389 }
390 
392 #define sumadd_fast(a) { \
393  c0 += (a); /* overflow is handled on the next line */ \
394  c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
395  VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
396  VERIFY_CHECK(c2 == 0); \
397 }
398 
400 #define extract(n) { \
401  (n) = c0; \
402  c0 = c1; \
403  c1 = c2; \
404  c2 = 0; \
405 }
406 
408 #define extract_fast(n) { \
409  (n) = c0; \
410  c0 = c1; \
411  c1 = 0; \
412  VERIFY_CHECK(c2 == 0); \
413 }
414 
415 static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) {
416  uint64_t c;
417  uint32_t n0 = l[8], n1 = l[9], n2 = l[10], n3 = l[11], n4 = l[12], n5 = l[13], n6 = l[14], n7 = l[15];
418  uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
419  uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
420 
421  /* 96 bit accumulator. */
422  uint32_t c0, c1, c2;
423 
424  /* Reduce 512 bits into 385. */
425  /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
426  c0 = l[0]; c1 = 0; c2 = 0;
428  extract_fast(m0);
429  sumadd_fast(l[1]);
430  muladd(n1, SECP256K1_N_C_0);
431  muladd(n0, SECP256K1_N_C_1);
432  extract(m1);
433  sumadd(l[2]);
434  muladd(n2, SECP256K1_N_C_0);
435  muladd(n1, SECP256K1_N_C_1);
436  muladd(n0, SECP256K1_N_C_2);
437  extract(m2);
438  sumadd(l[3]);
439  muladd(n3, SECP256K1_N_C_0);
440  muladd(n2, SECP256K1_N_C_1);
441  muladd(n1, SECP256K1_N_C_2);
442  muladd(n0, SECP256K1_N_C_3);
443  extract(m3);
444  sumadd(l[4]);
445  muladd(n4, SECP256K1_N_C_0);
446  muladd(n3, SECP256K1_N_C_1);
447  muladd(n2, SECP256K1_N_C_2);
448  muladd(n1, SECP256K1_N_C_3);
449  sumadd(n0);
450  extract(m4);
451  sumadd(l[5]);
452  muladd(n5, SECP256K1_N_C_0);
453  muladd(n4, SECP256K1_N_C_1);
454  muladd(n3, SECP256K1_N_C_2);
455  muladd(n2, SECP256K1_N_C_3);
456  sumadd(n1);
457  extract(m5);
458  sumadd(l[6]);
459  muladd(n6, SECP256K1_N_C_0);
460  muladd(n5, SECP256K1_N_C_1);
461  muladd(n4, SECP256K1_N_C_2);
462  muladd(n3, SECP256K1_N_C_3);
463  sumadd(n2);
464  extract(m6);
465  sumadd(l[7]);
466  muladd(n7, SECP256K1_N_C_0);
467  muladd(n6, SECP256K1_N_C_1);
468  muladd(n5, SECP256K1_N_C_2);
469  muladd(n4, SECP256K1_N_C_3);
470  sumadd(n3);
471  extract(m7);
472  muladd(n7, SECP256K1_N_C_1);
473  muladd(n6, SECP256K1_N_C_2);
474  muladd(n5, SECP256K1_N_C_3);
475  sumadd(n4);
476  extract(m8);
477  muladd(n7, SECP256K1_N_C_2);
478  muladd(n6, SECP256K1_N_C_3);
479  sumadd(n5);
480  extract(m9);
481  muladd(n7, SECP256K1_N_C_3);
482  sumadd(n6);
483  extract(m10);
484  sumadd_fast(n7);
485  extract_fast(m11);
486  VERIFY_CHECK(c0 <= 1);
487  m12 = c0;
488 
489  /* Reduce 385 bits into 258. */
490  /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
491  c0 = m0; c1 = 0; c2 = 0;
493  extract_fast(p0);
494  sumadd_fast(m1);
495  muladd(m9, SECP256K1_N_C_0);
496  muladd(m8, SECP256K1_N_C_1);
497  extract(p1);
498  sumadd(m2);
499  muladd(m10, SECP256K1_N_C_0);
500  muladd(m9, SECP256K1_N_C_1);
501  muladd(m8, SECP256K1_N_C_2);
502  extract(p2);
503  sumadd(m3);
504  muladd(m11, SECP256K1_N_C_0);
505  muladd(m10, SECP256K1_N_C_1);
506  muladd(m9, SECP256K1_N_C_2);
507  muladd(m8, SECP256K1_N_C_3);
508  extract(p3);
509  sumadd(m4);
510  muladd(m12, SECP256K1_N_C_0);
511  muladd(m11, SECP256K1_N_C_1);
512  muladd(m10, SECP256K1_N_C_2);
513  muladd(m9, SECP256K1_N_C_3);
514  sumadd(m8);
515  extract(p4);
516  sumadd(m5);
517  muladd(m12, SECP256K1_N_C_1);
518  muladd(m11, SECP256K1_N_C_2);
519  muladd(m10, SECP256K1_N_C_3);
520  sumadd(m9);
521  extract(p5);
522  sumadd(m6);
523  muladd(m12, SECP256K1_N_C_2);
524  muladd(m11, SECP256K1_N_C_3);
525  sumadd(m10);
526  extract(p6);
527  sumadd_fast(m7);
529  sumadd_fast(m11);
530  extract_fast(p7);
531  p8 = c0 + m12;
532  VERIFY_CHECK(p8 <= 2);
533 
534  /* Reduce 258 bits into 256. */
535  /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
536  c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
537  r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
538  c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
539  r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
540  c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
541  r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
542  c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
543  r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
544  c += p4 + (uint64_t)p8;
545  r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
546  c += p5;
547  r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
548  c += p6;
549  r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
550  c += p7;
551  r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
552 
553  /* Final reduction of r. */
555 }
556 
557 static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) {
558  /* 96 bit accumulator. */
559  uint32_t c0 = 0, c1 = 0, c2 = 0;
560 
561  /* l[0..15] = a[0..7] * b[0..7]. */
562  muladd_fast(a->d[0], b->d[0]);
563  extract_fast(l[0]);
564  muladd(a->d[0], b->d[1]);
565  muladd(a->d[1], b->d[0]);
566  extract(l[1]);
567  muladd(a->d[0], b->d[2]);
568  muladd(a->d[1], b->d[1]);
569  muladd(a->d[2], b->d[0]);
570  extract(l[2]);
571  muladd(a->d[0], b->d[3]);
572  muladd(a->d[1], b->d[2]);
573  muladd(a->d[2], b->d[1]);
574  muladd(a->d[3], b->d[0]);
575  extract(l[3]);
576  muladd(a->d[0], b->d[4]);
577  muladd(a->d[1], b->d[3]);
578  muladd(a->d[2], b->d[2]);
579  muladd(a->d[3], b->d[1]);
580  muladd(a->d[4], b->d[0]);
581  extract(l[4]);
582  muladd(a->d[0], b->d[5]);
583  muladd(a->d[1], b->d[4]);
584  muladd(a->d[2], b->d[3]);
585  muladd(a->d[3], b->d[2]);
586  muladd(a->d[4], b->d[1]);
587  muladd(a->d[5], b->d[0]);
588  extract(l[5]);
589  muladd(a->d[0], b->d[6]);
590  muladd(a->d[1], b->d[5]);
591  muladd(a->d[2], b->d[4]);
592  muladd(a->d[3], b->d[3]);
593  muladd(a->d[4], b->d[2]);
594  muladd(a->d[5], b->d[1]);
595  muladd(a->d[6], b->d[0]);
596  extract(l[6]);
597  muladd(a->d[0], b->d[7]);
598  muladd(a->d[1], b->d[6]);
599  muladd(a->d[2], b->d[5]);
600  muladd(a->d[3], b->d[4]);
601  muladd(a->d[4], b->d[3]);
602  muladd(a->d[5], b->d[2]);
603  muladd(a->d[6], b->d[1]);
604  muladd(a->d[7], b->d[0]);
605  extract(l[7]);
606  muladd(a->d[1], b->d[7]);
607  muladd(a->d[2], b->d[6]);
608  muladd(a->d[3], b->d[5]);
609  muladd(a->d[4], b->d[4]);
610  muladd(a->d[5], b->d[3]);
611  muladd(a->d[6], b->d[2]);
612  muladd(a->d[7], b->d[1]);
613  extract(l[8]);
614  muladd(a->d[2], b->d[7]);
615  muladd(a->d[3], b->d[6]);
616  muladd(a->d[4], b->d[5]);
617  muladd(a->d[5], b->d[4]);
618  muladd(a->d[6], b->d[3]);
619  muladd(a->d[7], b->d[2]);
620  extract(l[9]);
621  muladd(a->d[3], b->d[7]);
622  muladd(a->d[4], b->d[6]);
623  muladd(a->d[5], b->d[5]);
624  muladd(a->d[6], b->d[4]);
625  muladd(a->d[7], b->d[3]);
626  extract(l[10]);
627  muladd(a->d[4], b->d[7]);
628  muladd(a->d[5], b->d[6]);
629  muladd(a->d[6], b->d[5]);
630  muladd(a->d[7], b->d[4]);
631  extract(l[11]);
632  muladd(a->d[5], b->d[7]);
633  muladd(a->d[6], b->d[6]);
634  muladd(a->d[7], b->d[5]);
635  extract(l[12]);
636  muladd(a->d[6], b->d[7]);
637  muladd(a->d[7], b->d[6]);
638  extract(l[13]);
639  muladd_fast(a->d[7], b->d[7]);
640  extract_fast(l[14]);
641  VERIFY_CHECK(c1 == 0);
642  l[15] = c0;
643 }
644 
645 #undef sumadd
646 #undef sumadd_fast
647 #undef muladd
648 #undef muladd_fast
649 #undef extract
650 #undef extract_fast
651 
653  uint32_t l[16];
656 
657  secp256k1_scalar_mul_512(l, a, b);
659 
661 }
662 
665 
666  r1->d[0] = k->d[0];
667  r1->d[1] = k->d[1];
668  r1->d[2] = k->d[2];
669  r1->d[3] = k->d[3];
670  r1->d[4] = 0;
671  r1->d[5] = 0;
672  r1->d[6] = 0;
673  r1->d[7] = 0;
674  r2->d[0] = k->d[4];
675  r2->d[1] = k->d[5];
676  r2->d[2] = k->d[6];
677  r2->d[3] = k->d[7];
678  r2->d[4] = 0;
679  r2->d[5] = 0;
680  r2->d[6] = 0;
681  r2->d[7] = 0;
682 
685 }
686 
690 
691  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]) | (a->d[4] ^ b->d[4]) | (a->d[5] ^ b->d[5]) | (a->d[6] ^ b->d[6]) | (a->d[7] ^ b->d[7])) == 0;
692 }
693 
695  uint32_t l[16];
696  unsigned int shiftlimbs;
697  unsigned int shiftlow;
698  unsigned int shifthigh;
701  VERIFY_CHECK(shift >= 256);
702 
703  secp256k1_scalar_mul_512(l, a, b);
704  shiftlimbs = shift >> 5;
705  shiftlow = shift & 0x1F;
706  shifthigh = 32 - shiftlow;
707  r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
708  r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
709  r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
710  r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
711  r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
712  r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
713  r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
714  r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
715  secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
716 
718 }
719 
721  uint32_t mask0, mask1;
722  volatile int vflag = flag;
724  SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
725 
726  mask0 = vflag + ~((uint32_t)0);
727  mask1 = ~mask0;
728  r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
729  r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
730  r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
731  r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
732  r->d[4] = (r->d[4] & mask0) | (a->d[4] & mask1);
733  r->d[5] = (r->d[5] & mask0) | (a->d[5] & mask1);
734  r->d[6] = (r->d[6] & mask0) | (a->d[6] & mask1);
735  r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
736 
738 }
739 
741  const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
742  a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];
743 
744  /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and
745  * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8).
746  */
747  VERIFY_CHECK(a0 >> 30 == 0);
748  VERIFY_CHECK(a1 >> 30 == 0);
749  VERIFY_CHECK(a2 >> 30 == 0);
750  VERIFY_CHECK(a3 >> 30 == 0);
751  VERIFY_CHECK(a4 >> 30 == 0);
752  VERIFY_CHECK(a5 >> 30 == 0);
753  VERIFY_CHECK(a6 >> 30 == 0);
754  VERIFY_CHECK(a7 >> 30 == 0);
755  VERIFY_CHECK(a8 >> 16 == 0);
756 
757  r->d[0] = a0 | a1 << 30;
758  r->d[1] = a1 >> 2 | a2 << 28;
759  r->d[2] = a2 >> 4 | a3 << 26;
760  r->d[3] = a3 >> 6 | a4 << 24;
761  r->d[4] = a4 >> 8 | a5 << 22;
762  r->d[5] = a5 >> 10 | a6 << 20;
763  r->d[6] = a6 >> 12 | a7 << 18;
764  r->d[7] = a7 >> 14 | a8 << 16;
765 
767 }
768 
770  const uint32_t M30 = UINT32_MAX >> 2;
771  const uint32_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3],
772  a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7];
774 
775  r->v[0] = a0 & M30;
776  r->v[1] = (a0 >> 30 | a1 << 2) & M30;
777  r->v[2] = (a1 >> 28 | a2 << 4) & M30;
778  r->v[3] = (a2 >> 26 | a3 << 6) & M30;
779  r->v[4] = (a3 >> 24 | a4 << 8) & M30;
780  r->v[5] = (a4 >> 22 | a5 << 10) & M30;
781  r->v[6] = (a5 >> 20 | a6 << 12) & M30;
782  r->v[7] = (a6 >> 18 | a7 << 14) & M30;
783  r->v[8] = a7 >> 16;
784 }
785 
787  {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}},
788  0x2A774EC1L
789 };
790 
793 #ifdef VERIFY
794  int zero_in = secp256k1_scalar_is_zero(x);
795 #endif
797 
801 
803  VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in);
804 }
805 
808 #ifdef VERIFY
809  int zero_in = secp256k1_scalar_is_zero(x);
810 #endif
812 
816 
818  VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in);
819 }
820 
823 
824  return !(a->d[0] & 1);
825 }
826 
827 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:99
static void secp256k1_modinv32_var(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static void secp256k1_modinv32(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_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)
#define SECP256K1_N_5
#define SECP256K1_N_C_4
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 void secp256k1_scalar_clear(secp256k1_scalar *r)
#define extract(n)
Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits.
#define SECP256K1_N_6
#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_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b)
static SECP256K1_INLINE uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x)
#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
#define SECP256K1_N_2
#define SECP256K1_N_H_2
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 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits.
#define muladd(a, b)
Add a*b to the number defined by (c0,c1,c2).
#define SECP256K1_N_H_5
static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l)
#define SECP256K1_N_H_0
static SECP256K1_INLINE int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
#define SECP256K1_N_C_3
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)
static const secp256k1_modinv32_modinfo secp256k1_const_modinfo_scalar
static SECP256K1_INLINE int secp256k1_scalar_reduce(secp256k1_scalar *r, uint32_t overflow)
#define SECP256K1_N_H_1
#define SECP256K1_N_H_6
#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)
#define SECP256K1_N_H_7
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
#define SECP256K1_N_H_3
#define SECP256K1_N_H_4
static void secp256k1_scalar_from_signed30(secp256k1_scalar *r, const secp256k1_modinv32_signed30 *a)
static SECP256K1_INLINE uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
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_N_4
#define SECP256K1_N_7
static void secp256k1_scalar_to_signed30(secp256k1_modinv32_signed30 *r, const secp256k1_scalar *a)
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
Definition: util.h:358
#define SECP256K1_INLINE
Definition: util.h:48
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition: util.h:366
#define VERIFY_CHECK(cond)
Definition: util.h:153
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