Bitcoin ABC 0.33.6
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
65SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
67 VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
68
69 return (a->d[offset >> 5] >> (offset & 0x1F)) & ((1 << count) - 1);
70}
71
72SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
74 VERIFY_CHECK(count < 32);
75 VERIFY_CHECK(offset + count <= 256);
76
77 if ((offset + count - 1) >> 5 == offset >> 5) {
78 return secp256k1_scalar_get_bits(a, offset, count);
79 } else {
80 VERIFY_CHECK((offset >> 5) + 1 < 8);
81 return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & ((((uint32_t)1) << count) - 1);
82 }
83}
84
86 int yes = 0;
87 int no = 0;
88 no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */
89 no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */
90 no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */
91 no |= (a->d[4] < SECP256K1_N_4);
92 yes |= (a->d[4] > SECP256K1_N_4) & ~no;
93 no |= (a->d[3] < SECP256K1_N_3) & ~yes;
94 yes |= (a->d[3] > SECP256K1_N_3) & ~no;
95 no |= (a->d[2] < SECP256K1_N_2) & ~yes;
96 yes |= (a->d[2] > SECP256K1_N_2) & ~no;
97 no |= (a->d[1] < SECP256K1_N_1) & ~yes;
98 yes |= (a->d[1] > SECP256K1_N_1) & ~no;
99 yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
100 return yes;
101}
102
104 uint64_t t;
105 VERIFY_CHECK(overflow <= 1);
106
107 t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
108 r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
109 t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
110 r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
111 t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
112 r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
113 t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
114 r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
115 t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
116 r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
117 t += (uint64_t)r->d[5];
118 r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
119 t += (uint64_t)r->d[6];
120 r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
121 t += (uint64_t)r->d[7];
122 r->d[7] = t & 0xFFFFFFFFUL;
123
125 return overflow;
126}
127
129 int overflow;
130 uint64_t t = (uint64_t)a->d[0] + b->d[0];
133
134 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
135 t += (uint64_t)a->d[1] + b->d[1];
136 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
137 t += (uint64_t)a->d[2] + b->d[2];
138 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
139 t += (uint64_t)a->d[3] + b->d[3];
140 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
141 t += (uint64_t)a->d[4] + b->d[4];
142 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
143 t += (uint64_t)a->d[5] + b->d[5];
144 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
145 t += (uint64_t)a->d[6] + b->d[6];
146 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
147 t += (uint64_t)a->d[7] + b->d[7];
148 r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
149 overflow = t + secp256k1_scalar_check_overflow(r);
150 VERIFY_CHECK(overflow == 0 || overflow == 1);
151 secp256k1_scalar_reduce(r, overflow);
152
154 return overflow;
155}
156
157static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
158 uint64_t t;
159 volatile int vflag = flag;
161 VERIFY_CHECK(bit < 256);
162
163 bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
164 t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
165 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
166 t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
167 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
168 t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
169 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
170 t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
171 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
172 t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
173 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
174 t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
175 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
176 t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
177 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
178 t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
179 r->d[7] = t & 0xFFFFFFFFULL;
180
182#ifdef VERIFY
183 VERIFY_CHECK((t >> 32) == 0);
184#endif
185}
186
187static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
188 int over;
189 r->d[0] = secp256k1_read_be32(&b32[28]);
190 r->d[1] = secp256k1_read_be32(&b32[24]);
191 r->d[2] = secp256k1_read_be32(&b32[20]);
192 r->d[3] = secp256k1_read_be32(&b32[16]);
193 r->d[4] = secp256k1_read_be32(&b32[12]);
194 r->d[5] = secp256k1_read_be32(&b32[8]);
195 r->d[6] = secp256k1_read_be32(&b32[4]);
196 r->d[7] = secp256k1_read_be32(&b32[0]);
198 if (overflow) {
199 *overflow = over;
200 }
201
203}
204
205static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
207
208 secp256k1_write_be32(&bin[0], a->d[7]);
209 secp256k1_write_be32(&bin[4], a->d[6]);
210 secp256k1_write_be32(&bin[8], a->d[5]);
211 secp256k1_write_be32(&bin[12], a->d[4]);
212 secp256k1_write_be32(&bin[16], a->d[3]);
213 secp256k1_write_be32(&bin[20], a->d[2]);
214 secp256k1_write_be32(&bin[24], a->d[1]);
215 secp256k1_write_be32(&bin[28], a->d[0]);
216}
217
220
221 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;
222}
223
225 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
226 uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
228
229 r->d[0] = t & nonzero; t >>= 32;
230 t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
231 r->d[1] = t & nonzero; t >>= 32;
232 t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
233 r->d[2] = t & nonzero; t >>= 32;
234 t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
235 r->d[3] = t & nonzero; t >>= 32;
236 t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
237 r->d[4] = t & nonzero; t >>= 32;
238 t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
239 r->d[5] = t & nonzero; t >>= 32;
240 t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
241 r->d[6] = t & nonzero; t >>= 32;
242 t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
243 r->d[7] = t & nonzero;
244
246}
247
250
251 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;
252}
253
255 int yes = 0;
256 int no = 0;
258
259 no |= (a->d[7] < SECP256K1_N_H_7);
260 yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
261 no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
262 no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
263 no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
264 no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
265 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
266 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
267 yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
268 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
269 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
270 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
271 return yes;
272}
273
275 /* If we are flag = 0, mask = 00...00 and this is a no-op;
276 * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
277 volatile int vflag = flag;
278 uint32_t mask = -vflag;
279 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
280 uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
282
283 r->d[0] = t & nonzero; t >>= 32;
284 t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask);
285 r->d[1] = t & nonzero; t >>= 32;
286 t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask);
287 r->d[2] = t & nonzero; t >>= 32;
288 t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask);
289 r->d[3] = t & nonzero; t >>= 32;
290 t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask);
291 r->d[4] = t & nonzero; t >>= 32;
292 t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask);
293 r->d[5] = t & nonzero; t >>= 32;
294 t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask);
295 r->d[6] = t & nonzero; t >>= 32;
296 t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask);
297 r->d[7] = t & nonzero;
298
300 return 2 * (mask == 0) - 1;
301}
302
303
304/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
305
307#define muladd(a,b) { \
308 uint32_t tl, th; \
309 { \
310 uint64_t t = (uint64_t)a * b; \
311 th = t >> 32; /* at most 0xFFFFFFFE */ \
312 tl = t; \
313 } \
314 c0 += tl; /* overflow is handled on the next line */ \
315 th += (c0 < tl); /* at most 0xFFFFFFFF */ \
316 c1 += th; /* overflow is handled on the next line */ \
317 c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
318 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
319}
320
322#define muladd_fast(a,b) { \
323 uint32_t tl, th; \
324 { \
325 uint64_t t = (uint64_t)a * b; \
326 th = t >> 32; /* at most 0xFFFFFFFE */ \
327 tl = t; \
328 } \
329 c0 += tl; /* overflow is handled on the next line */ \
330 th += (c0 < tl); /* at most 0xFFFFFFFF */ \
331 c1 += th; /* never overflows by contract (verified in the next line) */ \
332 VERIFY_CHECK(c1 >= th); \
333}
334
336#define sumadd(a) { \
337 unsigned int over; \
338 c0 += (a); /* overflow is handled on the next line */ \
339 over = (c0 < (a)); \
340 c1 += over; /* overflow is handled on the next line */ \
341 c2 += (c1 < over); /* never overflows by contract */ \
342}
343
345#define sumadd_fast(a) { \
346 c0 += (a); /* overflow is handled on the next line */ \
347 c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
348 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
349 VERIFY_CHECK(c2 == 0); \
350}
351
353#define extract(n) { \
354 (n) = c0; \
355 c0 = c1; \
356 c1 = c2; \
357 c2 = 0; \
358}
359
361#define extract_fast(n) { \
362 (n) = c0; \
363 c0 = c1; \
364 c1 = 0; \
365 VERIFY_CHECK(c2 == 0); \
366}
367
368static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) {
369 uint64_t c;
370 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];
371 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
372 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
373
374 /* 96 bit accumulator. */
375 uint32_t c0, c1, c2;
376
377 /* Reduce 512 bits into 385. */
378 /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
379 c0 = l[0]; c1 = 0; c2 = 0;
381 extract_fast(m0);
382 sumadd_fast(l[1]);
385 extract(m1);
386 sumadd(l[2]);
390 extract(m2);
391 sumadd(l[3]);
396 extract(m3);
397 sumadd(l[4]);
402 sumadd(n0);
403 extract(m4);
404 sumadd(l[5]);
409 sumadd(n1);
410 extract(m5);
411 sumadd(l[6]);
416 sumadd(n2);
417 extract(m6);
418 sumadd(l[7]);
423 sumadd(n3);
424 extract(m7);
428 sumadd(n4);
429 extract(m8);
432 sumadd(n5);
433 extract(m9);
435 sumadd(n6);
436 extract(m10);
437 sumadd_fast(n7);
438 extract_fast(m11);
439 VERIFY_CHECK(c0 <= 1);
440 m12 = c0;
441
442 /* Reduce 385 bits into 258. */
443 /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
444 c0 = m0; c1 = 0; c2 = 0;
446 extract_fast(p0);
447 sumadd_fast(m1);
450 extract(p1);
451 sumadd(m2);
455 extract(p2);
456 sumadd(m3);
461 extract(p3);
462 sumadd(m4);
467 sumadd(m8);
468 extract(p4);
469 sumadd(m5);
473 sumadd(m9);
474 extract(p5);
475 sumadd(m6);
478 sumadd(m10);
479 extract(p6);
480 sumadd_fast(m7);
482 sumadd_fast(m11);
483 extract_fast(p7);
484 p8 = c0 + m12;
485 VERIFY_CHECK(p8 <= 2);
486
487 /* Reduce 258 bits into 256. */
488 /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
489 c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
490 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
491 c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
492 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
493 c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
494 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
495 c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
496 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
497 c += p4 + (uint64_t)p8;
498 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
499 c += p5;
500 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
501 c += p6;
502 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
503 c += p7;
504 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
505
506 /* Final reduction of r. */
508}
509
510static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) {
511 /* 96 bit accumulator. */
512 uint32_t c0 = 0, c1 = 0, c2 = 0;
513
514 /* l[0..15] = a[0..7] * b[0..7]. */
515 muladd_fast(a->d[0], b->d[0]);
516 extract_fast(l[0]);
517 muladd(a->d[0], b->d[1]);
518 muladd(a->d[1], b->d[0]);
519 extract(l[1]);
520 muladd(a->d[0], b->d[2]);
521 muladd(a->d[1], b->d[1]);
522 muladd(a->d[2], b->d[0]);
523 extract(l[2]);
524 muladd(a->d[0], b->d[3]);
525 muladd(a->d[1], b->d[2]);
526 muladd(a->d[2], b->d[1]);
527 muladd(a->d[3], b->d[0]);
528 extract(l[3]);
529 muladd(a->d[0], b->d[4]);
530 muladd(a->d[1], b->d[3]);
531 muladd(a->d[2], b->d[2]);
532 muladd(a->d[3], b->d[1]);
533 muladd(a->d[4], b->d[0]);
534 extract(l[4]);
535 muladd(a->d[0], b->d[5]);
536 muladd(a->d[1], b->d[4]);
537 muladd(a->d[2], b->d[3]);
538 muladd(a->d[3], b->d[2]);
539 muladd(a->d[4], b->d[1]);
540 muladd(a->d[5], b->d[0]);
541 extract(l[5]);
542 muladd(a->d[0], b->d[6]);
543 muladd(a->d[1], b->d[5]);
544 muladd(a->d[2], b->d[4]);
545 muladd(a->d[3], b->d[3]);
546 muladd(a->d[4], b->d[2]);
547 muladd(a->d[5], b->d[1]);
548 muladd(a->d[6], b->d[0]);
549 extract(l[6]);
550 muladd(a->d[0], b->d[7]);
551 muladd(a->d[1], b->d[6]);
552 muladd(a->d[2], b->d[5]);
553 muladd(a->d[3], b->d[4]);
554 muladd(a->d[4], b->d[3]);
555 muladd(a->d[5], b->d[2]);
556 muladd(a->d[6], b->d[1]);
557 muladd(a->d[7], b->d[0]);
558 extract(l[7]);
559 muladd(a->d[1], b->d[7]);
560 muladd(a->d[2], b->d[6]);
561 muladd(a->d[3], b->d[5]);
562 muladd(a->d[4], b->d[4]);
563 muladd(a->d[5], b->d[3]);
564 muladd(a->d[6], b->d[2]);
565 muladd(a->d[7], b->d[1]);
566 extract(l[8]);
567 muladd(a->d[2], b->d[7]);
568 muladd(a->d[3], b->d[6]);
569 muladd(a->d[4], b->d[5]);
570 muladd(a->d[5], b->d[4]);
571 muladd(a->d[6], b->d[3]);
572 muladd(a->d[7], b->d[2]);
573 extract(l[9]);
574 muladd(a->d[3], b->d[7]);
575 muladd(a->d[4], b->d[6]);
576 muladd(a->d[5], b->d[5]);
577 muladd(a->d[6], b->d[4]);
578 muladd(a->d[7], b->d[3]);
579 extract(l[10]);
580 muladd(a->d[4], b->d[7]);
581 muladd(a->d[5], b->d[6]);
582 muladd(a->d[6], b->d[5]);
583 muladd(a->d[7], b->d[4]);
584 extract(l[11]);
585 muladd(a->d[5], b->d[7]);
586 muladd(a->d[6], b->d[6]);
587 muladd(a->d[7], b->d[5]);
588 extract(l[12]);
589 muladd(a->d[6], b->d[7]);
590 muladd(a->d[7], b->d[6]);
591 extract(l[13]);
592 muladd_fast(a->d[7], b->d[7]);
593 extract_fast(l[14]);
594 VERIFY_CHECK(c1 == 0);
595 l[15] = c0;
596}
597
598#undef sumadd
599#undef sumadd_fast
600#undef muladd
601#undef muladd_fast
602#undef extract
603#undef extract_fast
604
606 uint32_t l[16];
609
612
614}
615
617 int ret;
619 VERIFY_CHECK(n > 0);
620 VERIFY_CHECK(n < 16);
621
622 ret = r->d[0] & ((1 << n) - 1);
623 r->d[0] = (r->d[0] >> n) + (r->d[1] << (32 - n));
624 r->d[1] = (r->d[1] >> n) + (r->d[2] << (32 - n));
625 r->d[2] = (r->d[2] >> n) + (r->d[3] << (32 - n));
626 r->d[3] = (r->d[3] >> n) + (r->d[4] << (32 - n));
627 r->d[4] = (r->d[4] >> n) + (r->d[5] << (32 - n));
628 r->d[5] = (r->d[5] >> n) + (r->d[6] << (32 - n));
629 r->d[6] = (r->d[6] >> n) + (r->d[7] << (32 - n));
630 r->d[7] = (r->d[7] >> n);
631
633 return ret;
634}
635
638
639 r1->d[0] = k->d[0];
640 r1->d[1] = k->d[1];
641 r1->d[2] = k->d[2];
642 r1->d[3] = k->d[3];
643 r1->d[4] = 0;
644 r1->d[5] = 0;
645 r1->d[6] = 0;
646 r1->d[7] = 0;
647 r2->d[0] = k->d[4];
648 r2->d[1] = k->d[5];
649 r2->d[2] = k->d[6];
650 r2->d[3] = k->d[7];
651 r2->d[4] = 0;
652 r2->d[5] = 0;
653 r2->d[6] = 0;
654 r2->d[7] = 0;
655
658}
659
663
664 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;
665}
666
668 uint32_t l[16];
669 unsigned int shiftlimbs;
670 unsigned int shiftlow;
671 unsigned int shifthigh;
674 VERIFY_CHECK(shift >= 256);
675
677 shiftlimbs = shift >> 5;
678 shiftlow = shift & 0x1F;
679 shifthigh = 32 - shiftlow;
680 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
681 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
682 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
683 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
684 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
685 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
686 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
687 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
688 secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
689
691}
692
694 uint32_t mask0, mask1;
695 volatile int vflag = flag;
697 SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
698
699 mask0 = vflag + ~((uint32_t)0);
700 mask1 = ~mask0;
701 r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
702 r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
703 r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
704 r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
705 r->d[4] = (r->d[4] & mask0) | (a->d[4] & mask1);
706 r->d[5] = (r->d[5] & mask0) | (a->d[5] & mask1);
707 r->d[6] = (r->d[6] & mask0) | (a->d[6] & mask1);
708 r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
709
711}
712
714 const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
715 a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];
716
717 /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and
718 * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8).
719 */
720 VERIFY_CHECK(a0 >> 30 == 0);
721 VERIFY_CHECK(a1 >> 30 == 0);
722 VERIFY_CHECK(a2 >> 30 == 0);
723 VERIFY_CHECK(a3 >> 30 == 0);
724 VERIFY_CHECK(a4 >> 30 == 0);
725 VERIFY_CHECK(a5 >> 30 == 0);
726 VERIFY_CHECK(a6 >> 30 == 0);
727 VERIFY_CHECK(a7 >> 30 == 0);
728 VERIFY_CHECK(a8 >> 16 == 0);
729
730 r->d[0] = a0 | a1 << 30;
731 r->d[1] = a1 >> 2 | a2 << 28;
732 r->d[2] = a2 >> 4 | a3 << 26;
733 r->d[3] = a3 >> 6 | a4 << 24;
734 r->d[4] = a4 >> 8 | a5 << 22;
735 r->d[5] = a5 >> 10 | a6 << 20;
736 r->d[6] = a6 >> 12 | a7 << 18;
737 r->d[7] = a7 >> 14 | a8 << 16;
738
740}
741
743 const uint32_t M30 = UINT32_MAX >> 2;
744 const uint32_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3],
745 a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7];
747
748 r->v[0] = a0 & M30;
749 r->v[1] = (a0 >> 30 | a1 << 2) & M30;
750 r->v[2] = (a1 >> 28 | a2 << 4) & M30;
751 r->v[3] = (a2 >> 26 | a3 << 6) & M30;
752 r->v[4] = (a3 >> 24 | a4 << 8) & M30;
753 r->v[5] = (a4 >> 22 | a5 << 10) & M30;
754 r->v[6] = (a5 >> 20 | a6 << 12) & M30;
755 r->v[7] = (a6 >> 18 | a7 << 14) & M30;
756 r->v[8] = a7 >> 16;
757}
758
760 {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}},
761 0x2A774EC1L
762};
763
766#ifdef VERIFY
767 int zero_in = secp256k1_scalar_is_zero(x);
768#endif
770
774
776#ifdef VERIFY
778#endif
779}
780
783#ifdef VERIFY
784 int zero_in = secp256k1_scalar_is_zero(x);
785#endif
787
791
793#ifdef VERIFY
795#endif
796}
797
800
801 return !(a->d[0] & 1);
802}
803
804#endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#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 void secp256k1_scalar_verify(const secp256k1_scalar *r)
Check invariants on a scalar (no-op unless VERIFY is enabled).
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)
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
Definition: util.h:346
#define SECP256K1_INLINE
Definition: util.h:48
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
Definition: util.h:354
#define VERIFY_CHECK(cond)
Definition: util.h:143
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