Bitcoin Core  24.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 
13 /* Limbs of the secp256k1 order. */
14 #define SECP256K1_N_0 ((uint32_t)0xD0364141UL)
15 #define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
16 #define SECP256K1_N_2 ((uint32_t)0xAF48A03BUL)
17 #define SECP256K1_N_3 ((uint32_t)0xBAAEDCE6UL)
18 #define SECP256K1_N_4 ((uint32_t)0xFFFFFFFEUL)
19 #define SECP256K1_N_5 ((uint32_t)0xFFFFFFFFUL)
20 #define SECP256K1_N_6 ((uint32_t)0xFFFFFFFFUL)
21 #define SECP256K1_N_7 ((uint32_t)0xFFFFFFFFUL)
22 
23 /* Limbs of 2^256 minus the secp256k1 order. */
24 #define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
25 #define SECP256K1_N_C_1 (~SECP256K1_N_1)
26 #define SECP256K1_N_C_2 (~SECP256K1_N_2)
27 #define SECP256K1_N_C_3 (~SECP256K1_N_3)
28 #define SECP256K1_N_C_4 (1)
29 
30 /* Limbs of half the secp256k1 order. */
31 #define SECP256K1_N_H_0 ((uint32_t)0x681B20A0UL)
32 #define SECP256K1_N_H_1 ((uint32_t)0xDFE92F46UL)
33 #define SECP256K1_N_H_2 ((uint32_t)0x57A4501DUL)
34 #define SECP256K1_N_H_3 ((uint32_t)0x5D576E73UL)
35 #define SECP256K1_N_H_4 ((uint32_t)0xFFFFFFFFUL)
36 #define SECP256K1_N_H_5 ((uint32_t)0xFFFFFFFFUL)
37 #define SECP256K1_N_H_6 ((uint32_t)0xFFFFFFFFUL)
38 #define SECP256K1_N_H_7 ((uint32_t)0x7FFFFFFFUL)
39 
41  r->d[0] = 0;
42  r->d[1] = 0;
43  r->d[2] = 0;
44  r->d[3] = 0;
45  r->d[4] = 0;
46  r->d[5] = 0;
47  r->d[6] = 0;
48  r->d[7] = 0;
49 }
50 
52  r->d[0] = v;
53  r->d[1] = 0;
54  r->d[2] = 0;
55  r->d[3] = 0;
56  r->d[4] = 0;
57  r->d[5] = 0;
58  r->d[6] = 0;
59  r->d[7] = 0;
60 }
61 
62 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
63  VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
64  return (a->d[offset >> 5] >> (offset & 0x1F)) & ((1 << count) - 1);
65 }
66 
67 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
68  VERIFY_CHECK(count < 32);
69  VERIFY_CHECK(offset + count <= 256);
70  if ((offset + count - 1) >> 5 == offset >> 5) {
71  return secp256k1_scalar_get_bits(a, offset, count);
72  } else {
73  VERIFY_CHECK((offset >> 5) + 1 < 8);
74  return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & ((((uint32_t)1) << count) - 1);
75  }
76 }
77 
79  int yes = 0;
80  int no = 0;
81  no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */
82  no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */
83  no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */
84  no |= (a->d[4] < SECP256K1_N_4);
85  yes |= (a->d[4] > SECP256K1_N_4) & ~no;
86  no |= (a->d[3] < SECP256K1_N_3) & ~yes;
87  yes |= (a->d[3] > SECP256K1_N_3) & ~no;
88  no |= (a->d[2] < SECP256K1_N_2) & ~yes;
89  yes |= (a->d[2] > SECP256K1_N_2) & ~no;
90  no |= (a->d[1] < SECP256K1_N_1) & ~yes;
91  yes |= (a->d[1] > SECP256K1_N_1) & ~no;
92  yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
93  return yes;
94 }
95 
96 SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, uint32_t overflow) {
97  uint64_t t;
98  VERIFY_CHECK(overflow <= 1);
99  t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
100  r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
101  t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
102  r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
103  t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
104  r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
105  t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
106  r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
107  t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
108  r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
109  t += (uint64_t)r->d[5];
110  r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
111  t += (uint64_t)r->d[6];
112  r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
113  t += (uint64_t)r->d[7];
114  r->d[7] = t & 0xFFFFFFFFUL;
115  return overflow;
116 }
117 
119  int overflow;
120  uint64_t t = (uint64_t)a->d[0] + b->d[0];
121  r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
122  t += (uint64_t)a->d[1] + b->d[1];
123  r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
124  t += (uint64_t)a->d[2] + b->d[2];
125  r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
126  t += (uint64_t)a->d[3] + b->d[3];
127  r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
128  t += (uint64_t)a->d[4] + b->d[4];
129  r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
130  t += (uint64_t)a->d[5] + b->d[5];
131  r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
132  t += (uint64_t)a->d[6] + b->d[6];
133  r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
134  t += (uint64_t)a->d[7] + b->d[7];
135  r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
136  overflow = t + secp256k1_scalar_check_overflow(r);
137  VERIFY_CHECK(overflow == 0 || overflow == 1);
138  secp256k1_scalar_reduce(r, overflow);
139  return overflow;
140 }
141 
142 static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
143  uint64_t t;
144  VERIFY_CHECK(bit < 256);
145  bit += ((uint32_t) flag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
146  t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
147  r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
148  t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
149  r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
150  t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
151  r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
152  t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
153  r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
154  t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
155  r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
156  t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
157  r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
158  t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
159  r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
160  t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
161  r->d[7] = t & 0xFFFFFFFFULL;
162 #ifdef VERIFY
163  VERIFY_CHECK((t >> 32) == 0);
165 #endif
166 }
167 
168 static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
169  int over;
170  r->d[0] = (uint32_t)b32[31] | (uint32_t)b32[30] << 8 | (uint32_t)b32[29] << 16 | (uint32_t)b32[28] << 24;
171  r->d[1] = (uint32_t)b32[27] | (uint32_t)b32[26] << 8 | (uint32_t)b32[25] << 16 | (uint32_t)b32[24] << 24;
172  r->d[2] = (uint32_t)b32[23] | (uint32_t)b32[22] << 8 | (uint32_t)b32[21] << 16 | (uint32_t)b32[20] << 24;
173  r->d[3] = (uint32_t)b32[19] | (uint32_t)b32[18] << 8 | (uint32_t)b32[17] << 16 | (uint32_t)b32[16] << 24;
174  r->d[4] = (uint32_t)b32[15] | (uint32_t)b32[14] << 8 | (uint32_t)b32[13] << 16 | (uint32_t)b32[12] << 24;
175  r->d[5] = (uint32_t)b32[11] | (uint32_t)b32[10] << 8 | (uint32_t)b32[9] << 16 | (uint32_t)b32[8] << 24;
176  r->d[6] = (uint32_t)b32[7] | (uint32_t)b32[6] << 8 | (uint32_t)b32[5] << 16 | (uint32_t)b32[4] << 24;
177  r->d[7] = (uint32_t)b32[3] | (uint32_t)b32[2] << 8 | (uint32_t)b32[1] << 16 | (uint32_t)b32[0] << 24;
179  if (overflow) {
180  *overflow = over;
181  }
182 }
183 
184 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
185  bin[0] = a->d[7] >> 24; bin[1] = a->d[7] >> 16; bin[2] = a->d[7] >> 8; bin[3] = a->d[7];
186  bin[4] = a->d[6] >> 24; bin[5] = a->d[6] >> 16; bin[6] = a->d[6] >> 8; bin[7] = a->d[6];
187  bin[8] = a->d[5] >> 24; bin[9] = a->d[5] >> 16; bin[10] = a->d[5] >> 8; bin[11] = a->d[5];
188  bin[12] = a->d[4] >> 24; bin[13] = a->d[4] >> 16; bin[14] = a->d[4] >> 8; bin[15] = a->d[4];
189  bin[16] = a->d[3] >> 24; bin[17] = a->d[3] >> 16; bin[18] = a->d[3] >> 8; bin[19] = a->d[3];
190  bin[20] = a->d[2] >> 24; bin[21] = a->d[2] >> 16; bin[22] = a->d[2] >> 8; bin[23] = a->d[2];
191  bin[24] = a->d[1] >> 24; bin[25] = a->d[1] >> 16; bin[26] = a->d[1] >> 8; bin[27] = a->d[1];
192  bin[28] = a->d[0] >> 24; bin[29] = a->d[0] >> 16; bin[30] = a->d[0] >> 8; bin[31] = a->d[0];
193 }
194 
196  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;
197 }
198 
200  uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
201  uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
202  r->d[0] = t & nonzero; t >>= 32;
203  t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
204  r->d[1] = t & nonzero; t >>= 32;
205  t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
206  r->d[2] = t & nonzero; t >>= 32;
207  t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
208  r->d[3] = t & nonzero; t >>= 32;
209  t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
210  r->d[4] = t & nonzero; t >>= 32;
211  t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
212  r->d[5] = t & nonzero; t >>= 32;
213  t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
214  r->d[6] = t & nonzero; t >>= 32;
215  t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
216  r->d[7] = t & nonzero;
217 }
218 
220  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;
221 }
222 
224  int yes = 0;
225  int no = 0;
226  no |= (a->d[7] < SECP256K1_N_H_7);
227  yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
228  no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
229  no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
230  no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
231  no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
232  yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
233  no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
234  yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
235  no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
236  yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
237  yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
238  return yes;
239 }
240 
242  /* If we are flag = 0, mask = 00...00 and this is a no-op;
243  * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
244  uint32_t mask = !flag - 1;
245  uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
246  uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
247  r->d[0] = t & nonzero; t >>= 32;
248  t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask);
249  r->d[1] = t & nonzero; t >>= 32;
250  t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask);
251  r->d[2] = t & nonzero; t >>= 32;
252  t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask);
253  r->d[3] = t & nonzero; t >>= 32;
254  t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask);
255  r->d[4] = t & nonzero; t >>= 32;
256  t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask);
257  r->d[5] = t & nonzero; t >>= 32;
258  t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask);
259  r->d[6] = t & nonzero; t >>= 32;
260  t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask);
261  r->d[7] = t & nonzero;
262  return 2 * (mask == 0) - 1;
263 }
264 
265 
266 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
267 
269 #define muladd(a,b) { \
270  uint32_t tl, th; \
271  { \
272  uint64_t t = (uint64_t)a * b; \
273  th = t >> 32; /* at most 0xFFFFFFFE */ \
274  tl = t; \
275  } \
276  c0 += tl; /* overflow is handled on the next line */ \
277  th += (c0 < tl); /* at most 0xFFFFFFFF */ \
278  c1 += th; /* overflow is handled on the next line */ \
279  c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
280  VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
281 }
282 
284 #define muladd_fast(a,b) { \
285  uint32_t tl, th; \
286  { \
287  uint64_t t = (uint64_t)a * b; \
288  th = t >> 32; /* at most 0xFFFFFFFE */ \
289  tl = t; \
290  } \
291  c0 += tl; /* overflow is handled on the next line */ \
292  th += (c0 < tl); /* at most 0xFFFFFFFF */ \
293  c1 += th; /* never overflows by contract (verified in the next line) */ \
294  VERIFY_CHECK(c1 >= th); \
295 }
296 
298 #define sumadd(a) { \
299  unsigned int over; \
300  c0 += (a); /* overflow is handled on the next line */ \
301  over = (c0 < (a)); \
302  c1 += over; /* overflow is handled on the next line */ \
303  c2 += (c1 < over); /* never overflows by contract */ \
304 }
305 
307 #define sumadd_fast(a) { \
308  c0 += (a); /* overflow is handled on the next line */ \
309  c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
310  VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
311  VERIFY_CHECK(c2 == 0); \
312 }
313 
315 #define extract(n) { \
316  (n) = c0; \
317  c0 = c1; \
318  c1 = c2; \
319  c2 = 0; \
320 }
321 
323 #define extract_fast(n) { \
324  (n) = c0; \
325  c0 = c1; \
326  c1 = 0; \
327  VERIFY_CHECK(c2 == 0); \
328 }
329 
330 static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) {
331  uint64_t c;
332  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];
333  uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
334  uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
335 
336  /* 96 bit accumulator. */
337  uint32_t c0, c1, c2;
338 
339  /* Reduce 512 bits into 385. */
340  /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
341  c0 = l[0]; c1 = 0; c2 = 0;
343  extract_fast(m0);
344  sumadd_fast(l[1]);
345  muladd(n1, SECP256K1_N_C_0);
346  muladd(n0, SECP256K1_N_C_1);
347  extract(m1);
348  sumadd(l[2]);
349  muladd(n2, SECP256K1_N_C_0);
350  muladd(n1, SECP256K1_N_C_1);
351  muladd(n0, SECP256K1_N_C_2);
352  extract(m2);
353  sumadd(l[3]);
354  muladd(n3, SECP256K1_N_C_0);
355  muladd(n2, SECP256K1_N_C_1);
356  muladd(n1, SECP256K1_N_C_2);
357  muladd(n0, SECP256K1_N_C_3);
358  extract(m3);
359  sumadd(l[4]);
360  muladd(n4, SECP256K1_N_C_0);
361  muladd(n3, SECP256K1_N_C_1);
362  muladd(n2, SECP256K1_N_C_2);
363  muladd(n1, SECP256K1_N_C_3);
364  sumadd(n0);
365  extract(m4);
366  sumadd(l[5]);
367  muladd(n5, SECP256K1_N_C_0);
368  muladd(n4, SECP256K1_N_C_1);
369  muladd(n3, SECP256K1_N_C_2);
370  muladd(n2, SECP256K1_N_C_3);
371  sumadd(n1);
372  extract(m5);
373  sumadd(l[6]);
374  muladd(n6, SECP256K1_N_C_0);
375  muladd(n5, SECP256K1_N_C_1);
376  muladd(n4, SECP256K1_N_C_2);
377  muladd(n3, SECP256K1_N_C_3);
378  sumadd(n2);
379  extract(m6);
380  sumadd(l[7]);
381  muladd(n7, SECP256K1_N_C_0);
382  muladd(n6, SECP256K1_N_C_1);
383  muladd(n5, SECP256K1_N_C_2);
384  muladd(n4, SECP256K1_N_C_3);
385  sumadd(n3);
386  extract(m7);
387  muladd(n7, SECP256K1_N_C_1);
388  muladd(n6, SECP256K1_N_C_2);
389  muladd(n5, SECP256K1_N_C_3);
390  sumadd(n4);
391  extract(m8);
392  muladd(n7, SECP256K1_N_C_2);
393  muladd(n6, SECP256K1_N_C_3);
394  sumadd(n5);
395  extract(m9);
396  muladd(n7, SECP256K1_N_C_3);
397  sumadd(n6);
398  extract(m10);
399  sumadd_fast(n7);
400  extract_fast(m11);
401  VERIFY_CHECK(c0 <= 1);
402  m12 = c0;
403 
404  /* Reduce 385 bits into 258. */
405  /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
406  c0 = m0; c1 = 0; c2 = 0;
408  extract_fast(p0);
409  sumadd_fast(m1);
410  muladd(m9, SECP256K1_N_C_0);
411  muladd(m8, SECP256K1_N_C_1);
412  extract(p1);
413  sumadd(m2);
414  muladd(m10, SECP256K1_N_C_0);
415  muladd(m9, SECP256K1_N_C_1);
416  muladd(m8, SECP256K1_N_C_2);
417  extract(p2);
418  sumadd(m3);
419  muladd(m11, SECP256K1_N_C_0);
420  muladd(m10, SECP256K1_N_C_1);
421  muladd(m9, SECP256K1_N_C_2);
422  muladd(m8, SECP256K1_N_C_3);
423  extract(p3);
424  sumadd(m4);
425  muladd(m12, SECP256K1_N_C_0);
426  muladd(m11, SECP256K1_N_C_1);
427  muladd(m10, SECP256K1_N_C_2);
428  muladd(m9, SECP256K1_N_C_3);
429  sumadd(m8);
430  extract(p4);
431  sumadd(m5);
432  muladd(m12, SECP256K1_N_C_1);
433  muladd(m11, SECP256K1_N_C_2);
434  muladd(m10, SECP256K1_N_C_3);
435  sumadd(m9);
436  extract(p5);
437  sumadd(m6);
438  muladd(m12, SECP256K1_N_C_2);
439  muladd(m11, SECP256K1_N_C_3);
440  sumadd(m10);
441  extract(p6);
442  sumadd_fast(m7);
444  sumadd_fast(m11);
445  extract_fast(p7);
446  p8 = c0 + m12;
447  VERIFY_CHECK(p8 <= 2);
448 
449  /* Reduce 258 bits into 256. */
450  /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
451  c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
452  r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
453  c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
454  r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
455  c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
456  r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
457  c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
458  r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
459  c += p4 + (uint64_t)p8;
460  r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
461  c += p5;
462  r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
463  c += p6;
464  r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
465  c += p7;
466  r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
467 
468  /* Final reduction of r. */
470 }
471 
472 static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) {
473  /* 96 bit accumulator. */
474  uint32_t c0 = 0, c1 = 0, c2 = 0;
475 
476  /* l[0..15] = a[0..7] * b[0..7]. */
477  muladd_fast(a->d[0], b->d[0]);
478  extract_fast(l[0]);
479  muladd(a->d[0], b->d[1]);
480  muladd(a->d[1], b->d[0]);
481  extract(l[1]);
482  muladd(a->d[0], b->d[2]);
483  muladd(a->d[1], b->d[1]);
484  muladd(a->d[2], b->d[0]);
485  extract(l[2]);
486  muladd(a->d[0], b->d[3]);
487  muladd(a->d[1], b->d[2]);
488  muladd(a->d[2], b->d[1]);
489  muladd(a->d[3], b->d[0]);
490  extract(l[3]);
491  muladd(a->d[0], b->d[4]);
492  muladd(a->d[1], b->d[3]);
493  muladd(a->d[2], b->d[2]);
494  muladd(a->d[3], b->d[1]);
495  muladd(a->d[4], b->d[0]);
496  extract(l[4]);
497  muladd(a->d[0], b->d[5]);
498  muladd(a->d[1], b->d[4]);
499  muladd(a->d[2], b->d[3]);
500  muladd(a->d[3], b->d[2]);
501  muladd(a->d[4], b->d[1]);
502  muladd(a->d[5], b->d[0]);
503  extract(l[5]);
504  muladd(a->d[0], b->d[6]);
505  muladd(a->d[1], b->d[5]);
506  muladd(a->d[2], b->d[4]);
507  muladd(a->d[3], b->d[3]);
508  muladd(a->d[4], b->d[2]);
509  muladd(a->d[5], b->d[1]);
510  muladd(a->d[6], b->d[0]);
511  extract(l[6]);
512  muladd(a->d[0], b->d[7]);
513  muladd(a->d[1], b->d[6]);
514  muladd(a->d[2], b->d[5]);
515  muladd(a->d[3], b->d[4]);
516  muladd(a->d[4], b->d[3]);
517  muladd(a->d[5], b->d[2]);
518  muladd(a->d[6], b->d[1]);
519  muladd(a->d[7], b->d[0]);
520  extract(l[7]);
521  muladd(a->d[1], b->d[7]);
522  muladd(a->d[2], b->d[6]);
523  muladd(a->d[3], b->d[5]);
524  muladd(a->d[4], b->d[4]);
525  muladd(a->d[5], b->d[3]);
526  muladd(a->d[6], b->d[2]);
527  muladd(a->d[7], b->d[1]);
528  extract(l[8]);
529  muladd(a->d[2], b->d[7]);
530  muladd(a->d[3], b->d[6]);
531  muladd(a->d[4], b->d[5]);
532  muladd(a->d[5], b->d[4]);
533  muladd(a->d[6], b->d[3]);
534  muladd(a->d[7], b->d[2]);
535  extract(l[9]);
536  muladd(a->d[3], b->d[7]);
537  muladd(a->d[4], b->d[6]);
538  muladd(a->d[5], b->d[5]);
539  muladd(a->d[6], b->d[4]);
540  muladd(a->d[7], b->d[3]);
541  extract(l[10]);
542  muladd(a->d[4], b->d[7]);
543  muladd(a->d[5], b->d[6]);
544  muladd(a->d[6], b->d[5]);
545  muladd(a->d[7], b->d[4]);
546  extract(l[11]);
547  muladd(a->d[5], b->d[7]);
548  muladd(a->d[6], b->d[6]);
549  muladd(a->d[7], b->d[5]);
550  extract(l[12]);
551  muladd(a->d[6], b->d[7]);
552  muladd(a->d[7], b->d[6]);
553  extract(l[13]);
554  muladd_fast(a->d[7], b->d[7]);
555  extract_fast(l[14]);
556  VERIFY_CHECK(c1 == 0);
557  l[15] = c0;
558 }
559 
560 #undef sumadd
561 #undef sumadd_fast
562 #undef muladd
563 #undef muladd_fast
564 #undef extract
565 #undef extract_fast
566 
568  uint32_t l[16];
569  secp256k1_scalar_mul_512(l, a, b);
571 }
572 
574  int ret;
575  VERIFY_CHECK(n > 0);
576  VERIFY_CHECK(n < 16);
577  ret = r->d[0] & ((1 << n) - 1);
578  r->d[0] = (r->d[0] >> n) + (r->d[1] << (32 - n));
579  r->d[1] = (r->d[1] >> n) + (r->d[2] << (32 - n));
580  r->d[2] = (r->d[2] >> n) + (r->d[3] << (32 - n));
581  r->d[3] = (r->d[3] >> n) + (r->d[4] << (32 - n));
582  r->d[4] = (r->d[4] >> n) + (r->d[5] << (32 - n));
583  r->d[5] = (r->d[5] >> n) + (r->d[6] << (32 - n));
584  r->d[6] = (r->d[6] >> n) + (r->d[7] << (32 - n));
585  r->d[7] = (r->d[7] >> n);
586  return ret;
587 }
588 
590  r1->d[0] = k->d[0];
591  r1->d[1] = k->d[1];
592  r1->d[2] = k->d[2];
593  r1->d[3] = k->d[3];
594  r1->d[4] = 0;
595  r1->d[5] = 0;
596  r1->d[6] = 0;
597  r1->d[7] = 0;
598  r2->d[0] = k->d[4];
599  r2->d[1] = k->d[5];
600  r2->d[2] = k->d[6];
601  r2->d[3] = k->d[7];
602  r2->d[4] = 0;
603  r2->d[5] = 0;
604  r2->d[6] = 0;
605  r2->d[7] = 0;
606 }
607 
609  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;
610 }
611 
613  uint32_t l[16];
614  unsigned int shiftlimbs;
615  unsigned int shiftlow;
616  unsigned int shifthigh;
617  VERIFY_CHECK(shift >= 256);
618  secp256k1_scalar_mul_512(l, a, b);
619  shiftlimbs = shift >> 5;
620  shiftlow = shift & 0x1F;
621  shifthigh = 32 - shiftlow;
622  r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
623  r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
624  r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
625  r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
626  r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
627  r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
628  r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
629  r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
630  secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
631 }
632 
634  uint32_t mask0, mask1;
635  SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
636  mask0 = flag + ~((uint32_t)0);
637  mask1 = ~mask0;
638  r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
639  r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
640  r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
641  r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
642  r->d[4] = (r->d[4] & mask0) | (a->d[4] & mask1);
643  r->d[5] = (r->d[5] & mask0) | (a->d[5] & mask1);
644  r->d[6] = (r->d[6] & mask0) | (a->d[6] & mask1);
645  r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
646 }
647 
649  const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
650  a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];
651 
652  /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and
653  * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8).
654  */
655  VERIFY_CHECK(a0 >> 30 == 0);
656  VERIFY_CHECK(a1 >> 30 == 0);
657  VERIFY_CHECK(a2 >> 30 == 0);
658  VERIFY_CHECK(a3 >> 30 == 0);
659  VERIFY_CHECK(a4 >> 30 == 0);
660  VERIFY_CHECK(a5 >> 30 == 0);
661  VERIFY_CHECK(a6 >> 30 == 0);
662  VERIFY_CHECK(a7 >> 30 == 0);
663  VERIFY_CHECK(a8 >> 16 == 0);
664 
665  r->d[0] = a0 | a1 << 30;
666  r->d[1] = a1 >> 2 | a2 << 28;
667  r->d[2] = a2 >> 4 | a3 << 26;
668  r->d[3] = a3 >> 6 | a4 << 24;
669  r->d[4] = a4 >> 8 | a5 << 22;
670  r->d[5] = a5 >> 10 | a6 << 20;
671  r->d[6] = a6 >> 12 | a7 << 18;
672  r->d[7] = a7 >> 14 | a8 << 16;
673 
674 #ifdef VERIFY
676 #endif
677 }
678 
680  const uint32_t M30 = UINT32_MAX >> 2;
681  const uint32_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3],
682  a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7];
683 
684 #ifdef VERIFY
686 #endif
687 
688  r->v[0] = a0 & M30;
689  r->v[1] = (a0 >> 30 | a1 << 2) & M30;
690  r->v[2] = (a1 >> 28 | a2 << 4) & M30;
691  r->v[3] = (a2 >> 26 | a3 << 6) & M30;
692  r->v[4] = (a3 >> 24 | a4 << 8) & M30;
693  r->v[5] = (a4 >> 22 | a5 << 10) & M30;
694  r->v[6] = (a5 >> 20 | a6 << 12) & M30;
695  r->v[7] = (a6 >> 18 | a7 << 14) & M30;
696  r->v[8] = a7 >> 16;
697 }
698 
700  {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}},
701  0x2A774EC1L
702 };
703 
706 #ifdef VERIFY
707  int zero_in = secp256k1_scalar_is_zero(x);
708 #endif
712 
713 #ifdef VERIFY
714  VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in);
715 #endif
716 }
717 
720 #ifdef VERIFY
721  int zero_in = secp256k1_scalar_is_zero(x);
722 #endif
726 
727 #ifdef VERIFY
728  VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in);
729 #endif
730 }
731 
733  return !(a->d[0] & 1);
734 }
735 
736 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */
int ret
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:85
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)
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
#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 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 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)
static SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
#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 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)
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n)
#define SECP256K1_N_4
#define SECP256K1_N_7
static void secp256k1_scalar_to_signed30(secp256k1_modinv32_signed30 *r, const secp256k1_scalar *a)
#define VERIFY_CHECK(cond)
Definition: util.h:96
#define SECP256K1_INLINE
Definition: secp256k1.h:131
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