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
249 /* Writing `/` for field division and `//` for integer division, we compute
250 *
251 * a/2 = (a - (a&1))/2 + (a&1)/2
252 * = (a >> 1) + (a&1 ? 1/2 : 0)
253 * = (a >> 1) + (a&1 ? n//2+1 : 0),
254 *
255 * where n is the group order and in the last equality we have used 1/2 = n//2+1 (mod n).
256 * For n//2, we have the constants SECP256K1_N_H_0, ...
257 *
258 * This sum does not overflow. The most extreme case is a = -2, the largest odd scalar. Here:
259 * - the left summand is: a >> 1 = (a - a&1)/2 = (n-2-1)//2 = (n-3)//2
260 * - the right summand is: a&1 ? n//2+1 : 0 = n//2+1 = (n-1)//2 + 2//2 = (n+1)//2
261 * Together they sum to (n-3)//2 + (n+1)//2 = (2n-2)//2 = n - 1, which is less than n.
262 */
263 uint32_t mask = -(uint32_t)(a->d[0] & 1U);
264 uint64_t t = (uint32_t)((a->d[0] >> 1) | (a->d[1] << 31));
266
267 t += (SECP256K1_N_H_0 + 1U) & mask;
268 r->d[0] = t; t >>= 32;
269 t += (uint32_t)((a->d[1] >> 1) | (a->d[2] << 31));
270 t += SECP256K1_N_H_1 & mask;
271 r->d[1] = t; t >>= 32;
272 t += (uint32_t)((a->d[2] >> 1) | (a->d[3] << 31));
273 t += SECP256K1_N_H_2 & mask;
274 r->d[2] = t; t >>= 32;
275 t += (uint32_t)((a->d[3] >> 1) | (a->d[4] << 31));
276 t += SECP256K1_N_H_3 & mask;
277 r->d[3] = t; t >>= 32;
278 t += (uint32_t)((a->d[4] >> 1) | (a->d[5] << 31));
279 t += SECP256K1_N_H_4 & mask;
280 r->d[4] = t; t >>= 32;
281 t += (uint32_t)((a->d[5] >> 1) | (a->d[6] << 31));
282 t += SECP256K1_N_H_5 & mask;
283 r->d[5] = t; t >>= 32;
284 t += (uint32_t)((a->d[6] >> 1) | (a->d[7] << 31));
285 t += SECP256K1_N_H_6 & mask;
286 r->d[6] = t; t >>= 32;
287 r->d[7] = (uint32_t)t + (uint32_t)(a->d[7] >> 1) + (SECP256K1_N_H_7 & mask);
288#ifdef VERIFY
289 /* The line above only computed the bottom 32 bits of r->d[7]. Redo the computation
290 * in full 64 bits to make sure the top 32 bits are indeed zero. */
291 VERIFY_CHECK((t + (a->d[7] >> 1) + (SECP256K1_N_H_7 & mask)) >> 32 == 0);
292
294#endif
295}
296
299
300 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;
301}
302
304 int yes = 0;
305 int no = 0;
307
308 no |= (a->d[7] < SECP256K1_N_H_7);
309 yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
310 no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
311 no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
312 no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
313 no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
314 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
315 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
316 yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
317 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
318 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
319 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
320 return yes;
321}
322
324 /* If we are flag = 0, mask = 00...00 and this is a no-op;
325 * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
326 volatile int vflag = flag;
327 uint32_t mask = -vflag;
328 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
329 uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
331
332 r->d[0] = t & nonzero; t >>= 32;
333 t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask);
334 r->d[1] = t & nonzero; t >>= 32;
335 t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask);
336 r->d[2] = t & nonzero; t >>= 32;
337 t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask);
338 r->d[3] = t & nonzero; t >>= 32;
339 t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask);
340 r->d[4] = t & nonzero; t >>= 32;
341 t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask);
342 r->d[5] = t & nonzero; t >>= 32;
343 t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask);
344 r->d[6] = t & nonzero; t >>= 32;
345 t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask);
346 r->d[7] = t & nonzero;
347
349 return 2 * (mask == 0) - 1;
350}
351
352
353/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
354
356#define muladd(a,b) { \
357 uint32_t tl, th; \
358 { \
359 uint64_t t = (uint64_t)a * b; \
360 th = t >> 32; /* at most 0xFFFFFFFE */ \
361 tl = t; \
362 } \
363 c0 += tl; /* overflow is handled on the next line */ \
364 th += (c0 < tl); /* at most 0xFFFFFFFF */ \
365 c1 += th; /* overflow is handled on the next line */ \
366 c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
367 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
368}
369
371#define muladd_fast(a,b) { \
372 uint32_t tl, th; \
373 { \
374 uint64_t t = (uint64_t)a * b; \
375 th = t >> 32; /* at most 0xFFFFFFFE */ \
376 tl = t; \
377 } \
378 c0 += tl; /* overflow is handled on the next line */ \
379 th += (c0 < tl); /* at most 0xFFFFFFFF */ \
380 c1 += th; /* never overflows by contract (verified in the next line) */ \
381 VERIFY_CHECK(c1 >= th); \
382}
383
385#define sumadd(a) { \
386 unsigned int over; \
387 c0 += (a); /* overflow is handled on the next line */ \
388 over = (c0 < (a)); \
389 c1 += over; /* overflow is handled on the next line */ \
390 c2 += (c1 < over); /* never overflows by contract */ \
391}
392
394#define sumadd_fast(a) { \
395 c0 += (a); /* overflow is handled on the next line */ \
396 c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
397 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
398 VERIFY_CHECK(c2 == 0); \
399}
400
402#define extract(n) { \
403 (n) = c0; \
404 c0 = c1; \
405 c1 = c2; \
406 c2 = 0; \
407}
408
410#define extract_fast(n) { \
411 (n) = c0; \
412 c0 = c1; \
413 c1 = 0; \
414 VERIFY_CHECK(c2 == 0); \
415}
416
417static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) {
418 uint64_t c;
419 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];
420 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
421 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
422
423 /* 96 bit accumulator. */
424 uint32_t c0, c1, c2;
425
426 /* Reduce 512 bits into 385. */
427 /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
428 c0 = l[0]; c1 = 0; c2 = 0;
430 extract_fast(m0);
431 sumadd_fast(l[1]);
434 extract(m1);
435 sumadd(l[2]);
439 extract(m2);
440 sumadd(l[3]);
445 extract(m3);
446 sumadd(l[4]);
451 sumadd(n0);
452 extract(m4);
453 sumadd(l[5]);
458 sumadd(n1);
459 extract(m5);
460 sumadd(l[6]);
465 sumadd(n2);
466 extract(m6);
467 sumadd(l[7]);
472 sumadd(n3);
473 extract(m7);
477 sumadd(n4);
478 extract(m8);
481 sumadd(n5);
482 extract(m9);
484 sumadd(n6);
485 extract(m10);
486 sumadd_fast(n7);
487 extract_fast(m11);
488 VERIFY_CHECK(c0 <= 1);
489 m12 = c0;
490
491 /* Reduce 385 bits into 258. */
492 /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
493 c0 = m0; c1 = 0; c2 = 0;
495 extract_fast(p0);
496 sumadd_fast(m1);
499 extract(p1);
500 sumadd(m2);
504 extract(p2);
505 sumadd(m3);
510 extract(p3);
511 sumadd(m4);
516 sumadd(m8);
517 extract(p4);
518 sumadd(m5);
522 sumadd(m9);
523 extract(p5);
524 sumadd(m6);
527 sumadd(m10);
528 extract(p6);
529 sumadd_fast(m7);
531 sumadd_fast(m11);
532 extract_fast(p7);
533 p8 = c0 + m12;
534 VERIFY_CHECK(p8 <= 2);
535
536 /* Reduce 258 bits into 256. */
537 /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
538 c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
539 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
540 c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
541 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
542 c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
543 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
544 c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
545 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
546 c += p4 + (uint64_t)p8;
547 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
548 c += p5;
549 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
550 c += p6;
551 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
552 c += p7;
553 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
554
555 /* Final reduction of r. */
557}
558
559static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) {
560 /* 96 bit accumulator. */
561 uint32_t c0 = 0, c1 = 0, c2 = 0;
562
563 /* l[0..15] = a[0..7] * b[0..7]. */
564 muladd_fast(a->d[0], b->d[0]);
565 extract_fast(l[0]);
566 muladd(a->d[0], b->d[1]);
567 muladd(a->d[1], b->d[0]);
568 extract(l[1]);
569 muladd(a->d[0], b->d[2]);
570 muladd(a->d[1], b->d[1]);
571 muladd(a->d[2], b->d[0]);
572 extract(l[2]);
573 muladd(a->d[0], b->d[3]);
574 muladd(a->d[1], b->d[2]);
575 muladd(a->d[2], b->d[1]);
576 muladd(a->d[3], b->d[0]);
577 extract(l[3]);
578 muladd(a->d[0], b->d[4]);
579 muladd(a->d[1], b->d[3]);
580 muladd(a->d[2], b->d[2]);
581 muladd(a->d[3], b->d[1]);
582 muladd(a->d[4], b->d[0]);
583 extract(l[4]);
584 muladd(a->d[0], b->d[5]);
585 muladd(a->d[1], b->d[4]);
586 muladd(a->d[2], b->d[3]);
587 muladd(a->d[3], b->d[2]);
588 muladd(a->d[4], b->d[1]);
589 muladd(a->d[5], b->d[0]);
590 extract(l[5]);
591 muladd(a->d[0], b->d[6]);
592 muladd(a->d[1], b->d[5]);
593 muladd(a->d[2], b->d[4]);
594 muladd(a->d[3], b->d[3]);
595 muladd(a->d[4], b->d[2]);
596 muladd(a->d[5], b->d[1]);
597 muladd(a->d[6], b->d[0]);
598 extract(l[6]);
599 muladd(a->d[0], b->d[7]);
600 muladd(a->d[1], b->d[6]);
601 muladd(a->d[2], b->d[5]);
602 muladd(a->d[3], b->d[4]);
603 muladd(a->d[4], b->d[3]);
604 muladd(a->d[5], b->d[2]);
605 muladd(a->d[6], b->d[1]);
606 muladd(a->d[7], b->d[0]);
607 extract(l[7]);
608 muladd(a->d[1], b->d[7]);
609 muladd(a->d[2], b->d[6]);
610 muladd(a->d[3], b->d[5]);
611 muladd(a->d[4], b->d[4]);
612 muladd(a->d[5], b->d[3]);
613 muladd(a->d[6], b->d[2]);
614 muladd(a->d[7], b->d[1]);
615 extract(l[8]);
616 muladd(a->d[2], b->d[7]);
617 muladd(a->d[3], b->d[6]);
618 muladd(a->d[4], b->d[5]);
619 muladd(a->d[5], b->d[4]);
620 muladd(a->d[6], b->d[3]);
621 muladd(a->d[7], b->d[2]);
622 extract(l[9]);
623 muladd(a->d[3], b->d[7]);
624 muladd(a->d[4], b->d[6]);
625 muladd(a->d[5], b->d[5]);
626 muladd(a->d[6], b->d[4]);
627 muladd(a->d[7], b->d[3]);
628 extract(l[10]);
629 muladd(a->d[4], b->d[7]);
630 muladd(a->d[5], b->d[6]);
631 muladd(a->d[6], b->d[5]);
632 muladd(a->d[7], b->d[4]);
633 extract(l[11]);
634 muladd(a->d[5], b->d[7]);
635 muladd(a->d[6], b->d[6]);
636 muladd(a->d[7], b->d[5]);
637 extract(l[12]);
638 muladd(a->d[6], b->d[7]);
639 muladd(a->d[7], b->d[6]);
640 extract(l[13]);
641 muladd_fast(a->d[7], b->d[7]);
642 extract_fast(l[14]);
643 VERIFY_CHECK(c1 == 0);
644 l[15] = c0;
645}
646
647#undef sumadd
648#undef sumadd_fast
649#undef muladd
650#undef muladd_fast
651#undef extract
652#undef extract_fast
653
655 uint32_t l[16];
658
661
663}
664
667
668 r1->d[0] = k->d[0];
669 r1->d[1] = k->d[1];
670 r1->d[2] = k->d[2];
671 r1->d[3] = k->d[3];
672 r1->d[4] = 0;
673 r1->d[5] = 0;
674 r1->d[6] = 0;
675 r1->d[7] = 0;
676 r2->d[0] = k->d[4];
677 r2->d[1] = k->d[5];
678 r2->d[2] = k->d[6];
679 r2->d[3] = k->d[7];
680 r2->d[4] = 0;
681 r2->d[5] = 0;
682 r2->d[6] = 0;
683 r2->d[7] = 0;
684
687}
688
692
693 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;
694}
695
697 uint32_t l[16];
698 unsigned int shiftlimbs;
699 unsigned int shiftlow;
700 unsigned int shifthigh;
703 VERIFY_CHECK(shift >= 256);
704
706 shiftlimbs = shift >> 5;
707 shiftlow = shift & 0x1F;
708 shifthigh = 32 - shiftlow;
709 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
710 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
711 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
712 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
713 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
714 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
715 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
716 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
717 secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
718
720}
721
723 uint32_t mask0, mask1;
724 volatile int vflag = flag;
726 SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
727
728 mask0 = vflag + ~((uint32_t)0);
729 mask1 = ~mask0;
730 r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
731 r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
732 r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
733 r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
734 r->d[4] = (r->d[4] & mask0) | (a->d[4] & mask1);
735 r->d[5] = (r->d[5] & mask0) | (a->d[5] & mask1);
736 r->d[6] = (r->d[6] & mask0) | (a->d[6] & mask1);
737 r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1);
738
740}
741
743 const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4],
744 a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8];
745
746 /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and
747 * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8).
748 */
749 VERIFY_CHECK(a0 >> 30 == 0);
750 VERIFY_CHECK(a1 >> 30 == 0);
751 VERIFY_CHECK(a2 >> 30 == 0);
752 VERIFY_CHECK(a3 >> 30 == 0);
753 VERIFY_CHECK(a4 >> 30 == 0);
754 VERIFY_CHECK(a5 >> 30 == 0);
755 VERIFY_CHECK(a6 >> 30 == 0);
756 VERIFY_CHECK(a7 >> 30 == 0);
757 VERIFY_CHECK(a8 >> 16 == 0);
758
759 r->d[0] = a0 | a1 << 30;
760 r->d[1] = a1 >> 2 | a2 << 28;
761 r->d[2] = a2 >> 4 | a3 << 26;
762 r->d[3] = a3 >> 6 | a4 << 24;
763 r->d[4] = a4 >> 8 | a5 << 22;
764 r->d[5] = a5 >> 10 | a6 << 20;
765 r->d[6] = a6 >> 12 | a7 << 18;
766 r->d[7] = a7 >> 14 | a8 << 16;
767
769}
770
772 const uint32_t M30 = UINT32_MAX >> 2;
773 const uint32_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3],
774 a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7];
776
777 r->v[0] = a0 & M30;
778 r->v[1] = (a0 >> 30 | a1 << 2) & M30;
779 r->v[2] = (a1 >> 28 | a2 << 4) & M30;
780 r->v[3] = (a2 >> 26 | a3 << 6) & M30;
781 r->v[4] = (a3 >> 24 | a4 << 8) & M30;
782 r->v[5] = (a4 >> 22 | a5 << 10) & M30;
783 r->v[6] = (a5 >> 20 | a6 << 12) & M30;
784 r->v[7] = (a6 >> 18 | a7 << 14) & M30;
785 r->v[8] = a7 >> 16;
786}
787
789 {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}},
790 0x2A774EC1L
791};
792
795#ifdef VERIFY
796 int zero_in = secp256k1_scalar_is_zero(x);
797#endif
799
803
805#ifdef VERIFY
807#endif
808}
809
812#ifdef VERIFY
813 int zero_in = secp256k1_scalar_is_zero(x);
814#endif
816
820
822#ifdef VERIFY
824#endif
825}
826
829
830 return !(a->d[0] & 1);
831}
832
833#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
static void secp256k1_scalar_half(secp256k1_scalar *r, const secp256k1_scalar *a)
#define SECP256K1_N_3
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k)
static SECP256K1_INLINE unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static SECP256K1_INLINE void secp256k1_scalar_clear(secp256k1_scalar *r)
#define extract(n)
Extract the lowest 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)
#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