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