Bitcoin Core 31.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] = v;
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
52}
53
54SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
56 VERIFY_CHECK(count > 0 && count <= 32);
57 VERIFY_CHECK(offset <= 256 - count);
58 VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
59
60 return (a->d[offset >> 5] >> (offset & 0x1F)) & (0xFFFFFFFF >> (32 - count));
61}
62
63SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
65 VERIFY_CHECK(count > 0 && count <= 32);
66 VERIFY_CHECK(offset <= 256 - count);
67
68 if ((offset + count - 1) >> 5 == offset >> 5) {
69 return secp256k1_scalar_get_bits_limb32(a, offset, count);
70 } else {
71 VERIFY_CHECK((offset >> 5) + 1 < 8);
72 return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & (0xFFFFFFFF >> (32 - count));
73 }
74}
75
77 int yes = 0;
78 int no = 0;
79 no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */
80 no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */
81 no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */
82 no |= (a->d[4] < SECP256K1_N_4);
83 yes |= (a->d[4] > SECP256K1_N_4) & ~no;
84 no |= (a->d[3] < SECP256K1_N_3) & ~yes;
85 yes |= (a->d[3] > SECP256K1_N_3) & ~no;
86 no |= (a->d[2] < SECP256K1_N_2) & ~yes;
87 yes |= (a->d[2] > SECP256K1_N_2) & ~no;
88 no |= (a->d[1] < SECP256K1_N_1) & ~yes;
89 yes |= (a->d[1] > SECP256K1_N_1) & ~no;
90 yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
91 return yes;
92}
93
95 uint64_t t;
96 VERIFY_CHECK(overflow <= 1);
97
98 t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
99 r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
100 t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
101 r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
102 t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
103 r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
104 t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
105 r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
106 t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
107 r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
108 t += (uint64_t)r->d[5];
109 r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
110 t += (uint64_t)r->d[6];
111 r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
112 t += (uint64_t)r->d[7];
113 r->d[7] = t & 0xFFFFFFFFUL;
114
116 return overflow;
117}
118
120 int overflow;
121 uint64_t t = (uint64_t)a->d[0] + b->d[0];
124
125 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
126 t += (uint64_t)a->d[1] + b->d[1];
127 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
128 t += (uint64_t)a->d[2] + b->d[2];
129 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
130 t += (uint64_t)a->d[3] + b->d[3];
131 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
132 t += (uint64_t)a->d[4] + b->d[4];
133 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
134 t += (uint64_t)a->d[5] + b->d[5];
135 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
136 t += (uint64_t)a->d[6] + b->d[6];
137 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
138 t += (uint64_t)a->d[7] + b->d[7];
139 r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
140 overflow = t + secp256k1_scalar_check_overflow(r);
141 VERIFY_CHECK(overflow == 0 || overflow == 1);
142 secp256k1_scalar_reduce(r, overflow);
143
145 return overflow;
146}
147
148static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
149 uint64_t t;
150 volatile int vflag = flag;
151 VERIFY_CHECK(flag == 0 || flag == 1);
153 VERIFY_CHECK(bit < 256);
154
155 bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
156 t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
157 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
158 t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
159 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
160 t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
161 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
162 t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
163 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
164 t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
165 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
166 t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
167 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
168 t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
169 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
170 t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
171 r->d[7] = t & 0xFFFFFFFFULL;
172
174 VERIFY_CHECK((t >> 32) == 0);
175}
176
177static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
178 int over;
179 r->d[0] = secp256k1_read_be32(&b32[28]);
180 r->d[1] = secp256k1_read_be32(&b32[24]);
181 r->d[2] = secp256k1_read_be32(&b32[20]);
182 r->d[3] = secp256k1_read_be32(&b32[16]);
183 r->d[4] = secp256k1_read_be32(&b32[12]);
184 r->d[5] = secp256k1_read_be32(&b32[8]);
185 r->d[6] = secp256k1_read_be32(&b32[4]);
186 r->d[7] = secp256k1_read_be32(&b32[0]);
188 if (overflow) {
189 *overflow = over;
190 }
191
193}
194
195static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
197
198 secp256k1_write_be32(&bin[0], a->d[7]);
199 secp256k1_write_be32(&bin[4], a->d[6]);
200 secp256k1_write_be32(&bin[8], a->d[5]);
201 secp256k1_write_be32(&bin[12], a->d[4]);
202 secp256k1_write_be32(&bin[16], a->d[3]);
203 secp256k1_write_be32(&bin[20], a->d[2]);
204 secp256k1_write_be32(&bin[24], a->d[1]);
205 secp256k1_write_be32(&bin[28], a->d[0]);
206}
207
210
211 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;
212}
213
215 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
216 uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
218
219 r->d[0] = t & nonzero; t >>= 32;
220 t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
221 r->d[1] = t & nonzero; t >>= 32;
222 t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
223 r->d[2] = t & nonzero; t >>= 32;
224 t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
225 r->d[3] = t & nonzero; t >>= 32;
226 t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
227 r->d[4] = t & nonzero; t >>= 32;
228 t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
229 r->d[5] = t & nonzero; t >>= 32;
230 t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
231 r->d[6] = t & nonzero; t >>= 32;
232 t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
233 r->d[7] = t & nonzero;
234
236}
237
239 /* Writing `/` for field division and `//` for integer division, we compute
240 *
241 * a/2 = (a - (a&1))/2 + (a&1)/2
242 * = (a >> 1) + (a&1 ? 1/2 : 0)
243 * = (a >> 1) + (a&1 ? n//2+1 : 0),
244 *
245 * where n is the group order and in the last equality we have used 1/2 = n//2+1 (mod n).
246 * For n//2, we have the constants SECP256K1_N_H_0, ...
247 *
248 * This sum does not overflow. The most extreme case is a = -2, the largest odd scalar. Here:
249 * - the left summand is: a >> 1 = (a - a&1)/2 = (n-2-1)//2 = (n-3)//2
250 * - the right summand is: a&1 ? n//2+1 : 0 = n//2+1 = (n-1)//2 + 2//2 = (n+1)//2
251 * Together they sum to (n-3)//2 + (n+1)//2 = (2n-2)//2 = n - 1, which is less than n.
252 */
253 uint32_t mask = -(uint32_t)(a->d[0] & 1U);
254 uint64_t t = (uint32_t)((a->d[0] >> 1) | (a->d[1] << 31));
256
257 t += (SECP256K1_N_H_0 + 1U) & mask;
258 r->d[0] = t; t >>= 32;
259 t += (uint32_t)((a->d[1] >> 1) | (a->d[2] << 31));
260 t += SECP256K1_N_H_1 & mask;
261 r->d[1] = t; t >>= 32;
262 t += (uint32_t)((a->d[2] >> 1) | (a->d[3] << 31));
263 t += SECP256K1_N_H_2 & mask;
264 r->d[2] = t; t >>= 32;
265 t += (uint32_t)((a->d[3] >> 1) | (a->d[4] << 31));
266 t += SECP256K1_N_H_3 & mask;
267 r->d[3] = t; t >>= 32;
268 t += (uint32_t)((a->d[4] >> 1) | (a->d[5] << 31));
269 t += SECP256K1_N_H_4 & mask;
270 r->d[4] = t; t >>= 32;
271 t += (uint32_t)((a->d[5] >> 1) | (a->d[6] << 31));
272 t += SECP256K1_N_H_5 & mask;
273 r->d[5] = t; t >>= 32;
274 t += (uint32_t)((a->d[6] >> 1) | (a->d[7] << 31));
275 t += SECP256K1_N_H_6 & mask;
276 r->d[6] = t; t >>= 32;
277 r->d[7] = (uint32_t)t + (uint32_t)(a->d[7] >> 1) + (SECP256K1_N_H_7 & mask);
278
279 /* The line above only computed the bottom 32 bits of r->d[7]. Redo the computation
280 * in full 64 bits to make sure the top 32 bits are indeed zero. */
281 VERIFY_CHECK((t + (a->d[7] >> 1) + (SECP256K1_N_H_7 & mask)) >> 32 == 0);
282
284}
285
288
289 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;
290}
291
293 int yes = 0;
294 int no = 0;
296
297 no |= (a->d[7] < SECP256K1_N_H_7);
298 yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
299 no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
300 no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
301 no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
302 no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
303 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
304 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
305 yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
306 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
307 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
308 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
309 return yes;
310}
311
313 /* If we are flag = 0, mask = 00...00 and this is a no-op;
314 * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
315 volatile int vflag = flag;
316 uint32_t mask = -vflag;
317 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
318 uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
319 VERIFY_CHECK(flag == 0 || flag == 1);
321
322 r->d[0] = t & nonzero; t >>= 32;
323 t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask);
324 r->d[1] = t & nonzero; t >>= 32;
325 t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask);
326 r->d[2] = t & nonzero; t >>= 32;
327 t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask);
328 r->d[3] = t & nonzero; t >>= 32;
329 t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask);
330 r->d[4] = t & nonzero; t >>= 32;
331 t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask);
332 r->d[5] = t & nonzero; t >>= 32;
333 t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask);
334 r->d[6] = t & nonzero; t >>= 32;
335 t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask);
336 r->d[7] = t & nonzero;
337
339 return 2 * (mask == 0) - 1;
340}
341
342
343/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
344
346#define muladd(a,b) { \
347 uint32_t tl, th; \
348 { \
349 uint64_t t = (uint64_t)a * b; \
350 th = t >> 32; /* at most 0xFFFFFFFE */ \
351 tl = t; \
352 } \
353 c0 += tl; /* overflow is handled on the next line */ \
354 th += (c0 < tl); /* at most 0xFFFFFFFF */ \
355 c1 += th; /* overflow is handled on the next line */ \
356 c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
357 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
358}
359
361#define muladd_fast(a,b) { \
362 uint32_t tl, th; \
363 { \
364 uint64_t t = (uint64_t)a * b; \
365 th = t >> 32; /* at most 0xFFFFFFFE */ \
366 tl = t; \
367 } \
368 c0 += tl; /* overflow is handled on the next line */ \
369 th += (c0 < tl); /* at most 0xFFFFFFFF */ \
370 c1 += th; /* never overflows by contract (verified in the next line) */ \
371 VERIFY_CHECK(c1 >= th); \
372}
373
375#define sumadd(a) { \
376 unsigned int over; \
377 c0 += (a); /* overflow is handled on the next line */ \
378 over = (c0 < (a)); \
379 c1 += over; /* overflow is handled on the next line */ \
380 c2 += (c1 < over); /* never overflows by contract */ \
381}
382
384#define sumadd_fast(a) { \
385 c0 += (a); /* overflow is handled on the next line */ \
386 c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
387 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
388 VERIFY_CHECK(c2 == 0); \
389}
390
392#define extract(n) { \
393 (n) = c0; \
394 c0 = c1; \
395 c1 = c2; \
396 c2 = 0; \
397}
398
400#define extract_fast(n) { \
401 (n) = c0; \
402 c0 = c1; \
403 c1 = 0; \
404 VERIFY_CHECK(c2 == 0); \
405}
406
407static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) {
408 uint64_t c;
409 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];
410 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
411 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
412
413 /* 96 bit accumulator. */
414 uint32_t c0, c1, c2;
415
416 /* Reduce 512 bits into 385. */
417 /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
418 c0 = l[0]; c1 = 0; c2 = 0;
420 extract_fast(m0);
421 sumadd_fast(l[1]);
424 extract(m1);
425 sumadd(l[2]);
429 extract(m2);
430 sumadd(l[3]);
435 extract(m3);
436 sumadd(l[4]);
441 sumadd(n0);
442 extract(m4);
443 sumadd(l[5]);
448 sumadd(n1);
449 extract(m5);
450 sumadd(l[6]);
455 sumadd(n2);
456 extract(m6);
457 sumadd(l[7]);
462 sumadd(n3);
463 extract(m7);
467 sumadd(n4);
468 extract(m8);
471 sumadd(n5);
472 extract(m9);
474 sumadd(n6);
475 extract(m10);
476 sumadd_fast(n7);
477 extract_fast(m11);
478 VERIFY_CHECK(c0 <= 1);
479 m12 = c0;
480
481 /* Reduce 385 bits into 258. */
482 /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
483 c0 = m0; c1 = 0; c2 = 0;
485 extract_fast(p0);
486 sumadd_fast(m1);
489 extract(p1);
490 sumadd(m2);
494 extract(p2);
495 sumadd(m3);
500 extract(p3);
501 sumadd(m4);
506 sumadd(m8);
507 extract(p4);
508 sumadd(m5);
512 sumadd(m9);
513 extract(p5);
514 sumadd(m6);
517 sumadd(m10);
518 extract(p6);
519 sumadd_fast(m7);
521 sumadd_fast(m11);
522 extract_fast(p7);
523 p8 = c0 + m12;
524 VERIFY_CHECK(p8 <= 2);
525
526 /* Reduce 258 bits into 256. */
527 /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
528 c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
529 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
530 c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
531 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
532 c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
533 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
534 c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
535 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
536 c += p4 + (uint64_t)p8;
537 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
538 c += p5;
539 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
540 c += p6;
541 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
542 c += p7;
543 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
544
545 /* Final reduction of r. */
547}
548
549static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) {
550 /* 96 bit accumulator. */
551 uint32_t c0 = 0, c1 = 0, c2 = 0;
552
553 /* l[0..15] = a[0..7] * b[0..7]. */
554 muladd_fast(a->d[0], b->d[0]);
555 extract_fast(l[0]);
556 muladd(a->d[0], b->d[1]);
557 muladd(a->d[1], b->d[0]);
558 extract(l[1]);
559 muladd(a->d[0], b->d[2]);
560 muladd(a->d[1], b->d[1]);
561 muladd(a->d[2], b->d[0]);
562 extract(l[2]);
563 muladd(a->d[0], b->d[3]);
564 muladd(a->d[1], b->d[2]);
565 muladd(a->d[2], b->d[1]);
566 muladd(a->d[3], b->d[0]);
567 extract(l[3]);
568 muladd(a->d[0], b->d[4]);
569 muladd(a->d[1], b->d[3]);
570 muladd(a->d[2], b->d[2]);
571 muladd(a->d[3], b->d[1]);
572 muladd(a->d[4], b->d[0]);
573 extract(l[4]);
574 muladd(a->d[0], b->d[5]);
575 muladd(a->d[1], b->d[4]);
576 muladd(a->d[2], b->d[3]);
577 muladd(a->d[3], b->d[2]);
578 muladd(a->d[4], b->d[1]);
579 muladd(a->d[5], b->d[0]);
580 extract(l[5]);
581 muladd(a->d[0], b->d[6]);
582 muladd(a->d[1], b->d[5]);
583 muladd(a->d[2], b->d[4]);
584 muladd(a->d[3], b->d[3]);
585 muladd(a->d[4], b->d[2]);
586 muladd(a->d[5], b->d[1]);
587 muladd(a->d[6], b->d[0]);
588 extract(l[6]);
589 muladd(a->d[0], b->d[7]);
590 muladd(a->d[1], b->d[6]);
591 muladd(a->d[2], b->d[5]);
592 muladd(a->d[3], b->d[4]);
593 muladd(a->d[4], b->d[3]);
594 muladd(a->d[5], b->d[2]);
595 muladd(a->d[6], b->d[1]);
596 muladd(a->d[7], b->d[0]);
597 extract(l[7]);
598 muladd(a->d[1], b->d[7]);
599 muladd(a->d[2], b->d[6]);
600 muladd(a->d[3], b->d[5]);
601 muladd(a->d[4], b->d[4]);
602 muladd(a->d[5], b->d[3]);
603 muladd(a->d[6], b->d[2]);
604 muladd(a->d[7], b->d[1]);
605 extract(l[8]);
606 muladd(a->d[2], b->d[7]);
607 muladd(a->d[3], b->d[6]);
608 muladd(a->d[4], b->d[5]);
609 muladd(a->d[5], b->d[4]);
610 muladd(a->d[6], b->d[3]);
611 muladd(a->d[7], b->d[2]);
612 extract(l[9]);
613 muladd(a->d[3], b->d[7]);
614 muladd(a->d[4], b->d[6]);
615 muladd(a->d[5], b->d[5]);
616 muladd(a->d[6], b->d[4]);
617 muladd(a->d[7], b->d[3]);
618 extract(l[10]);
619 muladd(a->d[4], b->d[7]);
620 muladd(a->d[5], b->d[6]);
621 muladd(a->d[6], b->d[5]);
622 muladd(a->d[7], b->d[4]);
623 extract(l[11]);
624 muladd(a->d[5], b->d[7]);
625 muladd(a->d[6], b->d[6]);
626 muladd(a->d[7], b->d[5]);
627 extract(l[12]);
628 muladd(a->d[6], b->d[7]);
629 muladd(a->d[7], b->d[6]);
630 extract(l[13]);
631 muladd_fast(a->d[7], b->d[7]);
632 extract_fast(l[14]);
633 VERIFY_CHECK(c1 == 0);
634 l[15] = c0;
635}
636
637#undef sumadd
638#undef sumadd_fast
639#undef muladd
640#undef muladd_fast
641#undef extract
642#undef extract_fast
643
645 uint32_t l[16];
648
651
653}
654
657
658 r1->d[0] = k->d[0];
659 r1->d[1] = k->d[1];
660 r1->d[2] = k->d[2];
661 r1->d[3] = k->d[3];
662 r1->d[4] = 0;
663 r1->d[5] = 0;
664 r1->d[6] = 0;
665 r1->d[7] = 0;
666 r2->d[0] = k->d[4];
667 r2->d[1] = k->d[5];
668 r2->d[2] = k->d[6];
669 r2->d[3] = k->d[7];
670 r2->d[4] = 0;
671 r2->d[5] = 0;
672 r2->d[6] = 0;
673 r2->d[7] = 0;
674
677}
678
682
683 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;
684}
685
687 uint32_t l[16];
688 unsigned int shiftlimbs;
689 unsigned int shiftlow;
690 unsigned int shifthigh;
693 VERIFY_CHECK(shift >= 256);
694
696 shiftlimbs = shift >> 5;
697 shiftlow = shift & 0x1F;
698 shifthigh = 32 - shiftlow;
699 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
700 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
701 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
702 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
703 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
704 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
705 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
706 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
707 secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
708
710}
711
713 uint32_t mask0, mask1;
714 volatile int vflag = flag;
715 VERIFY_CHECK(flag == 0 || flag == 1);
717 SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
718
719 mask0 = vflag + ~((uint32_t)0);
720 mask1 = ~mask0;
721 r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
722 r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
723 r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
724 r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
725 r->d[4] = (r->d[4] & mask0) | (a->d[4] & mask1);
726 r->d[5] = (r->d[5] & mask0) | (a->d[5] & mask1);
727 r->d[6] = (r->d[6] & mask0) | (a->d[6] & mask1);
728 r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
729
731}
732
734 const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
735 a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];
736
737 /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and
738 * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8).
739 */
740 VERIFY_CHECK(a0 >> 30 == 0);
741 VERIFY_CHECK(a1 >> 30 == 0);
742 VERIFY_CHECK(a2 >> 30 == 0);
743 VERIFY_CHECK(a3 >> 30 == 0);
744 VERIFY_CHECK(a4 >> 30 == 0);
745 VERIFY_CHECK(a5 >> 30 == 0);
746 VERIFY_CHECK(a6 >> 30 == 0);
747 VERIFY_CHECK(a7 >> 30 == 0);
748 VERIFY_CHECK(a8 >> 16 == 0);
749
750 r->d[0] = a0 | a1 << 30;
751 r->d[1] = a1 >> 2 | a2 << 28;
752 r->d[2] = a2 >> 4 | a3 << 26;
753 r->d[3] = a3 >> 6 | a4 << 24;
754 r->d[4] = a4 >> 8 | a5 << 22;
755 r->d[5] = a5 >> 10 | a6 << 20;
756 r->d[6] = a6 >> 12 | a7 << 18;
757 r->d[7] = a7 >> 14 | a8 << 16;
758
760}
761
763 const uint32_t M30 = UINT32_MAX >> 2;
764 const uint32_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3],
765 a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7];
767
768 r->v[0] = a0 & M30;
769 r->v[1] = (a0 >> 30 | a1 << 2) & M30;
770 r->v[2] = (a1 >> 28 | a2 << 4) & M30;
771 r->v[3] = (a2 >> 26 | a3 << 6) & M30;
772 r->v[4] = (a3 >> 24 | a4 << 8) & M30;
773 r->v[5] = (a4 >> 22 | a5 << 10) & M30;
774 r->v[6] = (a5 >> 20 | a6 << 12) & M30;
775 r->v[7] = (a6 >> 18 | a7 << 14) & M30;
776 r->v[8] = a7 >> 16;
777}
778
780 {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}},
781 0x2A774EC1L
782};
783
786#ifdef VERIFY
787 int zero_in = secp256k1_scalar_is_zero(x);
788#endif
790
794
797}
798
801#ifdef VERIFY
802 int zero_in = secp256k1_scalar_is_zero(x);
803#endif
805
809
812}
813
816
817 return !(a->d[0] & 1);
818}
819
820#endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:114
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)
#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:429
#define SECP256K1_INLINE
Definition: util.h:54
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition: util.h:437
#define VERIFY_CHECK(cond)
Definition: util.h:170
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