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