Bitcoin ABC 0.33.6
P2P Digital Currency
scalar_4x64_impl.h
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2013, 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 "int128.h"
12#include "modinv64_impl.h"
13#include "util.h"
14
15/* Limbs of the secp256k1 order. */
16#define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
17#define SECP256K1_N_1 ((uint64_t)0xBAAEDCE6AF48A03BULL)
18#define SECP256K1_N_2 ((uint64_t)0xFFFFFFFFFFFFFFFEULL)
19#define SECP256K1_N_3 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
20
21/* Limbs of 2^256 minus the secp256k1 order. */
22#define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
23#define SECP256K1_N_C_1 (~SECP256K1_N_1)
24#define SECP256K1_N_C_2 (1)
25
26/* Limbs of half the secp256k1 order. */
27#define SECP256K1_N_H_0 ((uint64_t)0xDFE92F46681B20A0ULL)
28#define SECP256K1_N_H_1 ((uint64_t)0x5D576E7357A4501DULL)
29#define SECP256K1_N_H_2 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
30#define SECP256K1_N_H_3 ((uint64_t)0x7FFFFFFFFFFFFFFFULL)
31
33 r->d[0] = 0;
34 r->d[1] = 0;
35 r->d[2] = 0;
36 r->d[3] = 0;
37}
38
40 r->d[0] = v;
41 r->d[1] = 0;
42 r->d[2] = 0;
43 r->d[3] = 0;
44
46}
47
48SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
50 VERIFY_CHECK((offset + count - 1) >> 6 == offset >> 6);
51
52 return (a->d[offset >> 6] >> (offset & 0x3F)) & ((((uint64_t)1) << count) - 1);
53}
54
55SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
57 VERIFY_CHECK(count < 32);
58 VERIFY_CHECK(offset + count <= 256);
59
60 if ((offset + count - 1) >> 6 == offset >> 6) {
61 return secp256k1_scalar_get_bits(a, offset, count);
62 } else {
63 VERIFY_CHECK((offset >> 6) + 1 < 4);
64 return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & ((((uint64_t)1) << count) - 1);
65 }
66}
67
69 int yes = 0;
70 int no = 0;
71 no |= (a->d[3] < SECP256K1_N_3); /* No need for a > check. */
72 no |= (a->d[2] < SECP256K1_N_2);
73 yes |= (a->d[2] > SECP256K1_N_2) & ~no;
74 no |= (a->d[1] < SECP256K1_N_1);
75 yes |= (a->d[1] > SECP256K1_N_1) & ~no;
76 yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
77 return yes;
78}
79
80SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, unsigned int overflow) {
82 VERIFY_CHECK(overflow <= 1);
83
84 secp256k1_u128_from_u64(&t, r->d[0]);
87 secp256k1_u128_accum_u64(&t, r->d[1]);
90 secp256k1_u128_accum_u64(&t, r->d[2]);
93 secp256k1_u128_accum_u64(&t, r->d[3]);
94 r->d[3] = secp256k1_u128_to_u64(&t);
95
97 return overflow;
98}
99
101 int overflow;
105
106 secp256k1_u128_from_u64(&t, a->d[0]);
107 secp256k1_u128_accum_u64(&t, b->d[0]);
108 r->d[0] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
109 secp256k1_u128_accum_u64(&t, a->d[1]);
110 secp256k1_u128_accum_u64(&t, b->d[1]);
111 r->d[1] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
112 secp256k1_u128_accum_u64(&t, a->d[2]);
113 secp256k1_u128_accum_u64(&t, b->d[2]);
114 r->d[2] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
115 secp256k1_u128_accum_u64(&t, a->d[3]);
116 secp256k1_u128_accum_u64(&t, b->d[3]);
117 r->d[3] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
119 VERIFY_CHECK(overflow == 0 || overflow == 1);
120 secp256k1_scalar_reduce(r, overflow);
121
123 return overflow;
124}
125
126static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
128 volatile int vflag = flag;
130 VERIFY_CHECK(bit < 256);
131
132 bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 6) > 3 makes this a noop */
133 secp256k1_u128_from_u64(&t, r->d[0]);
134 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 0)) << (bit & 0x3F));
135 r->d[0] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
136 secp256k1_u128_accum_u64(&t, r->d[1]);
137 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 1)) << (bit & 0x3F));
138 r->d[1] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
139 secp256k1_u128_accum_u64(&t, r->d[2]);
140 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 2)) << (bit & 0x3F));
141 r->d[2] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
142 secp256k1_u128_accum_u64(&t, r->d[3]);
143 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 3)) << (bit & 0x3F));
144 r->d[3] = secp256k1_u128_to_u64(&t);
145
147#ifdef VERIFY
149#endif
150}
151
152static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
153 int over;
154 r->d[0] = secp256k1_read_be64(&b32[24]);
155 r->d[1] = secp256k1_read_be64(&b32[16]);
156 r->d[2] = secp256k1_read_be64(&b32[8]);
157 r->d[3] = secp256k1_read_be64(&b32[0]);
159 if (overflow) {
160 *overflow = over;
161 }
162
164}
165
166static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
168
169 secp256k1_write_be64(&bin[0], a->d[3]);
170 secp256k1_write_be64(&bin[8], a->d[2]);
171 secp256k1_write_be64(&bin[16], a->d[1]);
172 secp256k1_write_be64(&bin[24], a->d[0]);
173}
174
177
178 return (a->d[0] | a->d[1] | a->d[2] | a->d[3]) == 0;
179}
180
182 uint64_t nonzero = 0xFFFFFFFFFFFFFFFFULL * (secp256k1_scalar_is_zero(a) == 0);
185
186 secp256k1_u128_from_u64(&t, ~a->d[0]);
188 r->d[0] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
189 secp256k1_u128_accum_u64(&t, ~a->d[1]);
191 r->d[1] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
192 secp256k1_u128_accum_u64(&t, ~a->d[2]);
194 r->d[2] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
195 secp256k1_u128_accum_u64(&t, ~a->d[3]);
197 r->d[3] = secp256k1_u128_to_u64(&t) & nonzero;
198
200}
201
203 /* Writing `/` for field division and `//` for integer division, we compute
204 *
205 * a/2 = (a - (a&1))/2 + (a&1)/2
206 * = (a >> 1) + (a&1 ? 1/2 : 0)
207 * = (a >> 1) + (a&1 ? n//2+1 : 0),
208 *
209 * where n is the group order and in the last equality we have used 1/2 = n//2+1 (mod n).
210 * For n//2, we have the constants SECP256K1_N_H_0, ...
211 *
212 * This sum does not overflow. The most extreme case is a = -2, the largest odd scalar. Here:
213 * - the left summand is: a >> 1 = (a - a&1)/2 = (n-2-1)//2 = (n-3)//2
214 * - the right summand is: a&1 ? n//2+1 : 0 = n//2+1 = (n-1)//2 + 2//2 = (n+1)//2
215 * Together they sum to (n-3)//2 + (n+1)//2 = (2n-2)//2 = n - 1, which is less than n.
216 */
217 uint64_t mask = -(uint64_t)(a->d[0] & 1U);
220
221 secp256k1_u128_from_u64(&t, (a->d[0] >> 1) | (a->d[1] << 63));
222 secp256k1_u128_accum_u64(&t, (SECP256K1_N_H_0 + 1U) & mask);
223 r->d[0] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
224 secp256k1_u128_accum_u64(&t, (a->d[1] >> 1) | (a->d[2] << 63));
226 r->d[1] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
227 secp256k1_u128_accum_u64(&t, (a->d[2] >> 1) | (a->d[3] << 63));
229 r->d[2] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
230 r->d[3] = secp256k1_u128_to_u64(&t) + (a->d[3] >> 1) + (SECP256K1_N_H_3 & mask);
231#ifdef VERIFY
232 /* The line above only computed the bottom 64 bits of r->d[3]; redo the computation
233 * in full 128 bits to make sure the top 64 bits are indeed zero. */
234 secp256k1_u128_accum_u64(&t, a->d[3] >> 1);
236 secp256k1_u128_rshift(&t, 64);
238
240#endif
241}
242
245
246 return ((a->d[0] ^ 1) | a->d[1] | a->d[2] | a->d[3]) == 0;
247}
248
250 int yes = 0;
251 int no = 0;
253
254 no |= (a->d[3] < SECP256K1_N_H_3);
255 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
256 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes; /* No need for a > check. */
257 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
258 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
259 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
260 return yes;
261}
262
264 /* If we are flag = 0, mask = 00...00 and this is a no-op;
265 * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
266 volatile int vflag = flag;
267 uint64_t mask = -vflag;
268 uint64_t nonzero = (secp256k1_scalar_is_zero(r) != 0) - 1;
271
272 secp256k1_u128_from_u64(&t, r->d[0] ^ mask);
273 secp256k1_u128_accum_u64(&t, (SECP256K1_N_0 + 1) & mask);
274 r->d[0] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
275 secp256k1_u128_accum_u64(&t, r->d[1] ^ mask);
277 r->d[1] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
278 secp256k1_u128_accum_u64(&t, r->d[2] ^ mask);
280 r->d[2] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
281 secp256k1_u128_accum_u64(&t, r->d[3] ^ mask);
283 r->d[3] = secp256k1_u128_to_u64(&t) & nonzero;
284
286 return 2 * (mask == 0) - 1;
287}
288
289/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
290
292#define muladd(a,b) { \
293 uint64_t tl, th; \
294 { \
295 secp256k1_uint128 t; \
296 secp256k1_u128_mul(&t, a, b); \
297 th = secp256k1_u128_hi_u64(&t); /* at most 0xFFFFFFFFFFFFFFFE */ \
298 tl = secp256k1_u128_to_u64(&t); \
299 } \
300 c0 += tl; /* overflow is handled on the next line */ \
301 th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
302 c1 += th; /* overflow is handled on the next line */ \
303 c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
304 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
305}
306
308#define muladd_fast(a,b) { \
309 uint64_t tl, th; \
310 { \
311 secp256k1_uint128 t; \
312 secp256k1_u128_mul(&t, a, b); \
313 th = secp256k1_u128_hi_u64(&t); /* at most 0xFFFFFFFFFFFFFFFE */ \
314 tl = secp256k1_u128_to_u64(&t); \
315 } \
316 c0 += tl; /* overflow is handled on the next line */ \
317 th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
318 c1 += th; /* never overflows by contract (verified in the next line) */ \
319 VERIFY_CHECK(c1 >= th); \
320}
321
323#define sumadd(a) { \
324 unsigned int over; \
325 c0 += (a); /* overflow is handled on the next line */ \
326 over = (c0 < (a)); \
327 c1 += over; /* overflow is handled on the next line */ \
328 c2 += (c1 < over); /* never overflows by contract */ \
329}
330
332#define sumadd_fast(a) { \
333 c0 += (a); /* overflow is handled on the next line */ \
334 c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
335 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
336 VERIFY_CHECK(c2 == 0); \
337}
338
340#define extract(n) { \
341 (n) = c0; \
342 c0 = c1; \
343 c1 = c2; \
344 c2 = 0; \
345}
346
348#define extract_fast(n) { \
349 (n) = c0; \
350 c0 = c1; \
351 c1 = 0; \
352 VERIFY_CHECK(c2 == 0); \
353}
354
355static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l) {
356#ifdef USE_ASM_X86_64
357 /* Reduce 512 bits into 385. */
358 uint64_t m0, m1, m2, m3, m4, m5, m6;
359 uint64_t p0, p1, p2, p3, p4;
360 uint64_t c;
361
362 __asm__ __volatile__(
363 /* Preload. */
364 "movq 32(%%rsi), %%r11\n"
365 "movq 40(%%rsi), %%r12\n"
366 "movq 48(%%rsi), %%r13\n"
367 "movq 56(%%rsi), %%r14\n"
368 /* Initialize r8,r9,r10 */
369 "movq 0(%%rsi), %%r8\n"
370 "xorq %%r9, %%r9\n"
371 "xorq %%r10, %%r10\n"
372 /* (r8,r9) += n0 * c0 */
373 "movq %8, %%rax\n"
374 "mulq %%r11\n"
375 "addq %%rax, %%r8\n"
376 "adcq %%rdx, %%r9\n"
377 /* extract m0 */
378 "movq %%r8, %q0\n"
379 "xorq %%r8, %%r8\n"
380 /* (r9,r10) += l1 */
381 "addq 8(%%rsi), %%r9\n"
382 "adcq $0, %%r10\n"
383 /* (r9,r10,r8) += n1 * c0 */
384 "movq %8, %%rax\n"
385 "mulq %%r12\n"
386 "addq %%rax, %%r9\n"
387 "adcq %%rdx, %%r10\n"
388 "adcq $0, %%r8\n"
389 /* (r9,r10,r8) += n0 * c1 */
390 "movq %9, %%rax\n"
391 "mulq %%r11\n"
392 "addq %%rax, %%r9\n"
393 "adcq %%rdx, %%r10\n"
394 "adcq $0, %%r8\n"
395 /* extract m1 */
396 "movq %%r9, %q1\n"
397 "xorq %%r9, %%r9\n"
398 /* (r10,r8,r9) += l2 */
399 "addq 16(%%rsi), %%r10\n"
400 "adcq $0, %%r8\n"
401 "adcq $0, %%r9\n"
402 /* (r10,r8,r9) += n2 * c0 */
403 "movq %8, %%rax\n"
404 "mulq %%r13\n"
405 "addq %%rax, %%r10\n"
406 "adcq %%rdx, %%r8\n"
407 "adcq $0, %%r9\n"
408 /* (r10,r8,r9) += n1 * c1 */
409 "movq %9, %%rax\n"
410 "mulq %%r12\n"
411 "addq %%rax, %%r10\n"
412 "adcq %%rdx, %%r8\n"
413 "adcq $0, %%r9\n"
414 /* (r10,r8,r9) += n0 */
415 "addq %%r11, %%r10\n"
416 "adcq $0, %%r8\n"
417 "adcq $0, %%r9\n"
418 /* extract m2 */
419 "movq %%r10, %q2\n"
420 "xorq %%r10, %%r10\n"
421 /* (r8,r9,r10) += l3 */
422 "addq 24(%%rsi), %%r8\n"
423 "adcq $0, %%r9\n"
424 "adcq $0, %%r10\n"
425 /* (r8,r9,r10) += n3 * c0 */
426 "movq %8, %%rax\n"
427 "mulq %%r14\n"
428 "addq %%rax, %%r8\n"
429 "adcq %%rdx, %%r9\n"
430 "adcq $0, %%r10\n"
431 /* (r8,r9,r10) += n2 * c1 */
432 "movq %9, %%rax\n"
433 "mulq %%r13\n"
434 "addq %%rax, %%r8\n"
435 "adcq %%rdx, %%r9\n"
436 "adcq $0, %%r10\n"
437 /* (r8,r9,r10) += n1 */
438 "addq %%r12, %%r8\n"
439 "adcq $0, %%r9\n"
440 "adcq $0, %%r10\n"
441 /* extract m3 */
442 "movq %%r8, %q3\n"
443 "xorq %%r8, %%r8\n"
444 /* (r9,r10,r8) += n3 * c1 */
445 "movq %9, %%rax\n"
446 "mulq %%r14\n"
447 "addq %%rax, %%r9\n"
448 "adcq %%rdx, %%r10\n"
449 "adcq $0, %%r8\n"
450 /* (r9,r10,r8) += n2 */
451 "addq %%r13, %%r9\n"
452 "adcq $0, %%r10\n"
453 "adcq $0, %%r8\n"
454 /* extract m4 */
455 "movq %%r9, %q4\n"
456 /* (r10,r8) += n3 */
457 "addq %%r14, %%r10\n"
458 "adcq $0, %%r8\n"
459 /* extract m5 */
460 "movq %%r10, %q5\n"
461 /* extract m6 */
462 "movq %%r8, %q6\n"
463 : "=&g"(m0), "=&g"(m1), "=&g"(m2), "=g"(m3), "=g"(m4), "=g"(m5), "=g"(m6)
464 : "S"(l), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
465 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "cc");
466
467 /* Reduce 385 bits into 258. */
468 __asm__ __volatile__(
469 /* Preload */
470 "movq %q9, %%r11\n"
471 "movq %q10, %%r12\n"
472 "movq %q11, %%r13\n"
473 /* Initialize (r8,r9,r10) */
474 "movq %q5, %%r8\n"
475 "xorq %%r9, %%r9\n"
476 "xorq %%r10, %%r10\n"
477 /* (r8,r9) += m4 * c0 */
478 "movq %12, %%rax\n"
479 "mulq %%r11\n"
480 "addq %%rax, %%r8\n"
481 "adcq %%rdx, %%r9\n"
482 /* extract p0 */
483 "movq %%r8, %q0\n"
484 "xorq %%r8, %%r8\n"
485 /* (r9,r10) += m1 */
486 "addq %q6, %%r9\n"
487 "adcq $0, %%r10\n"
488 /* (r9,r10,r8) += m5 * c0 */
489 "movq %12, %%rax\n"
490 "mulq %%r12\n"
491 "addq %%rax, %%r9\n"
492 "adcq %%rdx, %%r10\n"
493 "adcq $0, %%r8\n"
494 /* (r9,r10,r8) += m4 * c1 */
495 "movq %13, %%rax\n"
496 "mulq %%r11\n"
497 "addq %%rax, %%r9\n"
498 "adcq %%rdx, %%r10\n"
499 "adcq $0, %%r8\n"
500 /* extract p1 */
501 "movq %%r9, %q1\n"
502 "xorq %%r9, %%r9\n"
503 /* (r10,r8,r9) += m2 */
504 "addq %q7, %%r10\n"
505 "adcq $0, %%r8\n"
506 "adcq $0, %%r9\n"
507 /* (r10,r8,r9) += m6 * c0 */
508 "movq %12, %%rax\n"
509 "mulq %%r13\n"
510 "addq %%rax, %%r10\n"
511 "adcq %%rdx, %%r8\n"
512 "adcq $0, %%r9\n"
513 /* (r10,r8,r9) += m5 * c1 */
514 "movq %13, %%rax\n"
515 "mulq %%r12\n"
516 "addq %%rax, %%r10\n"
517 "adcq %%rdx, %%r8\n"
518 "adcq $0, %%r9\n"
519 /* (r10,r8,r9) += m4 */
520 "addq %%r11, %%r10\n"
521 "adcq $0, %%r8\n"
522 "adcq $0, %%r9\n"
523 /* extract p2 */
524 "movq %%r10, %q2\n"
525 /* (r8,r9) += m3 */
526 "addq %q8, %%r8\n"
527 "adcq $0, %%r9\n"
528 /* (r8,r9) += m6 * c1 */
529 "movq %13, %%rax\n"
530 "mulq %%r13\n"
531 "addq %%rax, %%r8\n"
532 "adcq %%rdx, %%r9\n"
533 /* (r8,r9) += m5 */
534 "addq %%r12, %%r8\n"
535 "adcq $0, %%r9\n"
536 /* extract p3 */
537 "movq %%r8, %q3\n"
538 /* (r9) += m6 */
539 "addq %%r13, %%r9\n"
540 /* extract p4 */
541 "movq %%r9, %q4\n"
542 : "=&g"(p0), "=&g"(p1), "=&g"(p2), "=g"(p3), "=g"(p4)
543 : "g"(m0), "g"(m1), "g"(m2), "g"(m3), "g"(m4), "g"(m5), "g"(m6), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
544 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "cc");
545
546 /* Reduce 258 bits into 256. */
547 __asm__ __volatile__(
548 /* Preload */
549 "movq %q5, %%r10\n"
550 /* (rax,rdx) = p4 * c0 */
551 "movq %7, %%rax\n"
552 "mulq %%r10\n"
553 /* (rax,rdx) += p0 */
554 "addq %q1, %%rax\n"
555 "adcq $0, %%rdx\n"
556 /* extract r0 */
557 "movq %%rax, 0(%q6)\n"
558 /* Move to (r8,r9) */
559 "movq %%rdx, %%r8\n"
560 "xorq %%r9, %%r9\n"
561 /* (r8,r9) += p1 */
562 "addq %q2, %%r8\n"
563 "adcq $0, %%r9\n"
564 /* (r8,r9) += p4 * c1 */
565 "movq %8, %%rax\n"
566 "mulq %%r10\n"
567 "addq %%rax, %%r8\n"
568 "adcq %%rdx, %%r9\n"
569 /* Extract r1 */
570 "movq %%r8, 8(%q6)\n"
571 "xorq %%r8, %%r8\n"
572 /* (r9,r8) += p4 */
573 "addq %%r10, %%r9\n"
574 "adcq $0, %%r8\n"
575 /* (r9,r8) += p2 */
576 "addq %q3, %%r9\n"
577 "adcq $0, %%r8\n"
578 /* Extract r2 */
579 "movq %%r9, 16(%q6)\n"
580 "xorq %%r9, %%r9\n"
581 /* (r8,r9) += p3 */
582 "addq %q4, %%r8\n"
583 "adcq $0, %%r9\n"
584 /* Extract r3 */
585 "movq %%r8, 24(%q6)\n"
586 /* Extract c */
587 "movq %%r9, %q0\n"
588 : "=g"(c)
589 : "g"(p0), "g"(p1), "g"(p2), "g"(p3), "g"(p4), "D"(r), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
590 : "rax", "rdx", "r8", "r9", "r10", "cc", "memory");
591#else
593 uint64_t c, c0, c1, c2;
594 uint64_t n0 = l[4], n1 = l[5], n2 = l[6], n3 = l[7];
595 uint64_t m0, m1, m2, m3, m4, m5;
596 uint32_t m6;
597 uint64_t p0, p1, p2, p3;
598 uint32_t p4;
599
600 /* Reduce 512 bits into 385. */
601 /* m[0..6] = l[0..3] + n[0..3] * SECP256K1_N_C. */
602 c0 = l[0]; c1 = 0; c2 = 0;
604 extract_fast(m0);
605 sumadd_fast(l[1]);
608 extract(m1);
609 sumadd(l[2]);
612 sumadd(n0);
613 extract(m2);
614 sumadd(l[3]);
617 sumadd(n1);
618 extract(m3);
620 sumadd(n2);
621 extract(m4);
622 sumadd_fast(n3);
623 extract_fast(m5);
624 VERIFY_CHECK(c0 <= 1);
625 m6 = c0;
626
627 /* Reduce 385 bits into 258. */
628 /* p[0..4] = m[0..3] + m[4..6] * SECP256K1_N_C. */
629 c0 = m0; c1 = 0; c2 = 0;
631 extract_fast(p0);
632 sumadd_fast(m1);
635 extract(p1);
636 sumadd(m2);
639 sumadd(m4);
640 extract(p2);
641 sumadd_fast(m3);
643 sumadd_fast(m5);
644 extract_fast(p3);
645 p4 = c0 + m6;
646 VERIFY_CHECK(p4 <= 2);
647
648 /* Reduce 258 bits into 256. */
649 /* r[0..3] = p[0..3] + p[4] * SECP256K1_N_C. */
650 secp256k1_u128_from_u64(&c128, p0);
652 r->d[0] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
653 secp256k1_u128_accum_u64(&c128, p1);
655 r->d[1] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
656 secp256k1_u128_accum_u64(&c128, p2);
657 secp256k1_u128_accum_u64(&c128, p4);
658 r->d[2] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
659 secp256k1_u128_accum_u64(&c128, p3);
660 r->d[3] = secp256k1_u128_to_u64(&c128);
661 c = secp256k1_u128_hi_u64(&c128);
662#endif
663
664 /* Final reduction of r. */
666}
667
668static void secp256k1_scalar_mul_512(uint64_t l[8], const secp256k1_scalar *a, const secp256k1_scalar *b) {
669#ifdef USE_ASM_X86_64
670 const uint64_t *pb = b->d;
671 __asm__ __volatile__(
672 /* Preload */
673 "movq 0(%%rdi), %%r15\n"
674 "movq 8(%%rdi), %%rbx\n"
675 "movq 16(%%rdi), %%rcx\n"
676 "movq 0(%%rdx), %%r11\n"
677 "movq 8(%%rdx), %%r12\n"
678 "movq 16(%%rdx), %%r13\n"
679 "movq 24(%%rdx), %%r14\n"
680 /* (rax,rdx) = a0 * b0 */
681 "movq %%r15, %%rax\n"
682 "mulq %%r11\n"
683 /* Extract l0 */
684 "movq %%rax, 0(%%rsi)\n"
685 /* (r8,r9,r10) = (rdx) */
686 "movq %%rdx, %%r8\n"
687 "xorq %%r9, %%r9\n"
688 "xorq %%r10, %%r10\n"
689 /* (r8,r9,r10) += a0 * b1 */
690 "movq %%r15, %%rax\n"
691 "mulq %%r12\n"
692 "addq %%rax, %%r8\n"
693 "adcq %%rdx, %%r9\n"
694 "adcq $0, %%r10\n"
695 /* (r8,r9,r10) += a1 * b0 */
696 "movq %%rbx, %%rax\n"
697 "mulq %%r11\n"
698 "addq %%rax, %%r8\n"
699 "adcq %%rdx, %%r9\n"
700 "adcq $0, %%r10\n"
701 /* Extract l1 */
702 "movq %%r8, 8(%%rsi)\n"
703 "xorq %%r8, %%r8\n"
704 /* (r9,r10,r8) += a0 * b2 */
705 "movq %%r15, %%rax\n"
706 "mulq %%r13\n"
707 "addq %%rax, %%r9\n"
708 "adcq %%rdx, %%r10\n"
709 "adcq $0, %%r8\n"
710 /* (r9,r10,r8) += a1 * b1 */
711 "movq %%rbx, %%rax\n"
712 "mulq %%r12\n"
713 "addq %%rax, %%r9\n"
714 "adcq %%rdx, %%r10\n"
715 "adcq $0, %%r8\n"
716 /* (r9,r10,r8) += a2 * b0 */
717 "movq %%rcx, %%rax\n"
718 "mulq %%r11\n"
719 "addq %%rax, %%r9\n"
720 "adcq %%rdx, %%r10\n"
721 "adcq $0, %%r8\n"
722 /* Extract l2 */
723 "movq %%r9, 16(%%rsi)\n"
724 "xorq %%r9, %%r9\n"
725 /* (r10,r8,r9) += a0 * b3 */
726 "movq %%r15, %%rax\n"
727 "mulq %%r14\n"
728 "addq %%rax, %%r10\n"
729 "adcq %%rdx, %%r8\n"
730 "adcq $0, %%r9\n"
731 /* Preload a3 */
732 "movq 24(%%rdi), %%r15\n"
733 /* (r10,r8,r9) += a1 * b2 */
734 "movq %%rbx, %%rax\n"
735 "mulq %%r13\n"
736 "addq %%rax, %%r10\n"
737 "adcq %%rdx, %%r8\n"
738 "adcq $0, %%r9\n"
739 /* (r10,r8,r9) += a2 * b1 */
740 "movq %%rcx, %%rax\n"
741 "mulq %%r12\n"
742 "addq %%rax, %%r10\n"
743 "adcq %%rdx, %%r8\n"
744 "adcq $0, %%r9\n"
745 /* (r10,r8,r9) += a3 * b0 */
746 "movq %%r15, %%rax\n"
747 "mulq %%r11\n"
748 "addq %%rax, %%r10\n"
749 "adcq %%rdx, %%r8\n"
750 "adcq $0, %%r9\n"
751 /* Extract l3 */
752 "movq %%r10, 24(%%rsi)\n"
753 "xorq %%r10, %%r10\n"
754 /* (r8,r9,r10) += a1 * b3 */
755 "movq %%rbx, %%rax\n"
756 "mulq %%r14\n"
757 "addq %%rax, %%r8\n"
758 "adcq %%rdx, %%r9\n"
759 "adcq $0, %%r10\n"
760 /* (r8,r9,r10) += a2 * b2 */
761 "movq %%rcx, %%rax\n"
762 "mulq %%r13\n"
763 "addq %%rax, %%r8\n"
764 "adcq %%rdx, %%r9\n"
765 "adcq $0, %%r10\n"
766 /* (r8,r9,r10) += a3 * b1 */
767 "movq %%r15, %%rax\n"
768 "mulq %%r12\n"
769 "addq %%rax, %%r8\n"
770 "adcq %%rdx, %%r9\n"
771 "adcq $0, %%r10\n"
772 /* Extract l4 */
773 "movq %%r8, 32(%%rsi)\n"
774 "xorq %%r8, %%r8\n"
775 /* (r9,r10,r8) += a2 * b3 */
776 "movq %%rcx, %%rax\n"
777 "mulq %%r14\n"
778 "addq %%rax, %%r9\n"
779 "adcq %%rdx, %%r10\n"
780 "adcq $0, %%r8\n"
781 /* (r9,r10,r8) += a3 * b2 */
782 "movq %%r15, %%rax\n"
783 "mulq %%r13\n"
784 "addq %%rax, %%r9\n"
785 "adcq %%rdx, %%r10\n"
786 "adcq $0, %%r8\n"
787 /* Extract l5 */
788 "movq %%r9, 40(%%rsi)\n"
789 /* (r10,r8) += a3 * b3 */
790 "movq %%r15, %%rax\n"
791 "mulq %%r14\n"
792 "addq %%rax, %%r10\n"
793 "adcq %%rdx, %%r8\n"
794 /* Extract l6 */
795 "movq %%r10, 48(%%rsi)\n"
796 /* Extract l7 */
797 "movq %%r8, 56(%%rsi)\n"
798 : "+d"(pb)
799 : "S"(l), "D"(a->d)
800 : "rax", "rbx", "rcx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "cc", "memory");
801#else
802 /* 160 bit accumulator. */
803 uint64_t c0 = 0, c1 = 0;
804 uint32_t c2 = 0;
805
806 /* l[0..7] = a[0..3] * b[0..3]. */
807 muladd_fast(a->d[0], b->d[0]);
808 extract_fast(l[0]);
809 muladd(a->d[0], b->d[1]);
810 muladd(a->d[1], b->d[0]);
811 extract(l[1]);
812 muladd(a->d[0], b->d[2]);
813 muladd(a->d[1], b->d[1]);
814 muladd(a->d[2], b->d[0]);
815 extract(l[2]);
816 muladd(a->d[0], b->d[3]);
817 muladd(a->d[1], b->d[2]);
818 muladd(a->d[2], b->d[1]);
819 muladd(a->d[3], b->d[0]);
820 extract(l[3]);
821 muladd(a->d[1], b->d[3]);
822 muladd(a->d[2], b->d[2]);
823 muladd(a->d[3], b->d[1]);
824 extract(l[4]);
825 muladd(a->d[2], b->d[3]);
826 muladd(a->d[3], b->d[2]);
827 extract(l[5]);
828 muladd_fast(a->d[3], b->d[3]);
829 extract_fast(l[6]);
830 VERIFY_CHECK(c1 == 0);
831 l[7] = c0;
832#endif
833}
834
835#undef sumadd
836#undef sumadd_fast
837#undef muladd
838#undef muladd_fast
839#undef extract
840#undef extract_fast
841
843 uint64_t l[8];
846
849
851}
852
855
856 r1->d[0] = k->d[0];
857 r1->d[1] = k->d[1];
858 r1->d[2] = 0;
859 r1->d[3] = 0;
860 r2->d[0] = k->d[2];
861 r2->d[1] = k->d[3];
862 r2->d[2] = 0;
863 r2->d[3] = 0;
864
867}
868
872
873 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])) == 0;
874}
875
877 uint64_t l[8];
878 unsigned int shiftlimbs;
879 unsigned int shiftlow;
880 unsigned int shifthigh;
883 VERIFY_CHECK(shift >= 256);
884
886 shiftlimbs = shift >> 6;
887 shiftlow = shift & 0x3F;
888 shifthigh = 64 - shiftlow;
889 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
890 r->d[1] = shift < 448 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
891 r->d[2] = shift < 384 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
892 r->d[3] = shift < 320 ? (l[3 + shiftlimbs] >> shiftlow) : 0;
893 secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1);
894
896}
897
899 uint64_t mask0, mask1;
900 volatile int vflag = flag;
902 SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d));
903
904 mask0 = vflag + ~((uint64_t)0);
905 mask1 = ~mask0;
906 r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
907 r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
908 r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
909 r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
910
912}
913
915 const uint64_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4];
916
917 /* The output from secp256k1_modinv64{_var} should be normalized to range [0,modulus), and
918 * have limbs in [0,2^62). The modulus is < 2^256, so the top limb must be below 2^(256-62*4).
919 */
920 VERIFY_CHECK(a0 >> 62 == 0);
921 VERIFY_CHECK(a1 >> 62 == 0);
922 VERIFY_CHECK(a2 >> 62 == 0);
923 VERIFY_CHECK(a3 >> 62 == 0);
924 VERIFY_CHECK(a4 >> 8 == 0);
925
926 r->d[0] = a0 | a1 << 62;
927 r->d[1] = a1 >> 2 | a2 << 60;
928 r->d[2] = a2 >> 4 | a3 << 58;
929 r->d[3] = a3 >> 6 | a4 << 56;
930
932}
933
935 const uint64_t M62 = UINT64_MAX >> 2;
936 const uint64_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3];
938
939 r->v[0] = a0 & M62;
940 r->v[1] = (a0 >> 62 | a1 << 2) & M62;
941 r->v[2] = (a1 >> 60 | a2 << 4) & M62;
942 r->v[3] = (a2 >> 58 | a3 << 6) & M62;
943 r->v[4] = a3 >> 56;
944}
945
947 {{0x3FD25E8CD0364141LL, 0x2ABB739ABD2280EELL, -0x15LL, 0, 256}},
948 0x34F20099AA774EC1LL
949};
950
953#ifdef VERIFY
954 int zero_in = secp256k1_scalar_is_zero(x);
955#endif
957
961
963#ifdef VERIFY
965#endif
966}
967
970#ifdef VERIFY
971 int zero_in = secp256k1_scalar_is_zero(x);
972#endif
974
978
980#ifdef VERIFY
982#endif
983}
984
987
988 return !(a->d[0] & 1);
989}
990
991#endif /* SECP256K1_SCALAR_REPR_IMPL_H */
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
Definition: checkmem.h:85
static SECP256K1_INLINE uint64_t secp256k1_u128_hi_u64(const secp256k1_uint128 *a)
static SECP256K1_INLINE void secp256k1_u128_from_u64(secp256k1_uint128 *r, uint64_t a)
static SECP256K1_INLINE void secp256k1_u128_rshift(secp256k1_uint128 *r, unsigned int n)
static SECP256K1_INLINE void secp256k1_u128_accum_u64(secp256k1_uint128 *r, uint64_t a)
static SECP256K1_INLINE void secp256k1_u128_accum_mul(secp256k1_uint128 *r, uint64_t a, uint64_t b)
static SECP256K1_INLINE uint64_t secp256k1_u128_to_u64(const secp256k1_uint128 *a)
static void secp256k1_modinv64(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
static void secp256k1_modinv64_var(secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_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)
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 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits.
#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_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x)
static const secp256k1_modinv64_modinfo secp256k1_const_modinfo_scalar
#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
static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l)
#define SECP256K1_N_2
#define SECP256K1_N_H_2
static void secp256k1_scalar_from_signed62(secp256k1_scalar *r, const secp256k1_modinv64_signed62 *a)
static SECP256K1_INLINE void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static void secp256k1_scalar_mul_512(uint64_t l[8], const secp256k1_scalar *a, const secp256k1_scalar *b)
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 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits.
#define muladd(a, b)
Add a*b to the number defined by (c0,c1,c2).
static void secp256k1_scalar_to_signed62(secp256k1_modinv64_signed62 *r, const secp256k1_scalar *a)
#define SECP256K1_N_H_0
static SECP256K1_INLINE int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
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)
#define SECP256K1_N_H_1
static SECP256K1_INLINE int secp256k1_scalar_reduce(secp256k1_scalar *r, unsigned int overflow)
#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)
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
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_INLINE
Definition: util.h:48
static SECP256K1_INLINE void secp256k1_write_be64(unsigned char *p, uint64_t x)
Definition: util.h:374
#define VERIFY_CHECK(cond)
Definition: util.h:143
static SECP256K1_INLINE uint64_t secp256k1_read_be64(const unsigned char *p)
Definition: util.h:362
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