1 /**********************************************************************
2 * Copyright (c) 2013, 2014 Pieter Wuille *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or http://www.opensource.org/licenses/mit-license.php.*
5 **********************************************************************/
7 #ifndef _SECP256K1_SCALAR_REPR_IMPL_H_
8 #define _SECP256K1_SCALAR_REPR_IMPL_H_
10 /* Limbs of the secp256k1 order. */
11 #define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
12 #define SECP256K1_N_1 ((uint64_t)0xBAAEDCE6AF48A03BULL)
13 #define SECP256K1_N_2 ((uint64_t)0xFFFFFFFFFFFFFFFEULL)
14 #define SECP256K1_N_3 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
16 /* Limbs of 2^256 minus the secp256k1 order. */
17 #define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
18 #define SECP256K1_N_C_1 (~SECP256K1_N_1)
19 #define SECP256K1_N_C_2 (1)
21 /* Limbs of half the secp256k1 order. */
22 #define SECP256K1_N_H_0 ((uint64_t)0xDFE92F46681B20A0ULL)
23 #define SECP256K1_N_H_1 ((uint64_t)0x5D576E7357A4501DULL)
24 #define SECP256K1_N_H_2 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
25 #define SECP256K1_N_H_3 ((uint64_t)0x7FFFFFFFFFFFFFFFULL)
27 SECP256K1_INLINE static void secp256k1_scalar_clear(secp256k1_scalar_t *r) {
34 SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar_t *r, unsigned int v) {
41 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar_t *a, unsigned int offset, unsigned int count) {
42 VERIFY_CHECK((offset + count - 1) >> 6 == offset >> 6);
43 return (a->d[offset >> 6] >> (offset & 0x3F)) & ((((uint64_t)1) << count) - 1);
46 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar_t *a, unsigned int offset, unsigned int count) {
47 VERIFY_CHECK(count < 32);
48 VERIFY_CHECK(offset + count <= 256);
49 if ((offset + count - 1) >> 6 == offset >> 6) {
50 return secp256k1_scalar_get_bits(a, offset, count);
52 VERIFY_CHECK((offset >> 6) + 1 < 4);
53 return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & ((((uint64_t)1) << count) - 1);
57 SECP256K1_INLINE static int secp256k1_scalar_check_overflow(const secp256k1_scalar_t *a) {
60 no |= (a->d[3] < SECP256K1_N_3); /* No need for a > check. */
61 no |= (a->d[2] < SECP256K1_N_2);
62 yes |= (a->d[2] > SECP256K1_N_2) & ~no;
63 no |= (a->d[1] < SECP256K1_N_1);
64 yes |= (a->d[1] > SECP256K1_N_1) & ~no;
65 yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
69 SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar_t *r, unsigned int overflow) {
71 VERIFY_CHECK(overflow <= 1);
72 t = (uint128_t)r->d[0] + overflow * SECP256K1_N_C_0;
73 r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
74 t += (uint128_t)r->d[1] + overflow * SECP256K1_N_C_1;
75 r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
76 t += (uint128_t)r->d[2] + overflow * SECP256K1_N_C_2;
77 r->d[2] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
78 t += (uint64_t)r->d[3];
79 r->d[3] = t & 0xFFFFFFFFFFFFFFFFULL;
83 static int secp256k1_scalar_add(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
85 uint128_t t = (uint128_t)a->d[0] + b->d[0];
86 r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
87 t += (uint128_t)a->d[1] + b->d[1];
88 r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
89 t += (uint128_t)a->d[2] + b->d[2];
90 r->d[2] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
91 t += (uint128_t)a->d[3] + b->d[3];
92 r->d[3] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
93 overflow = t + secp256k1_scalar_check_overflow(r);
94 VERIFY_CHECK(overflow == 0 || overflow == 1);
95 secp256k1_scalar_reduce(r, overflow);
99 static void secp256k1_scalar_add_bit(secp256k1_scalar_t *r, unsigned int bit) {
101 VERIFY_CHECK(bit < 256);
102 t = (uint128_t)r->d[0] + (((uint64_t)((bit >> 6) == 0)) << (bit & 0x3F));
103 r->d[0] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
104 t += (uint128_t)r->d[1] + (((uint64_t)((bit >> 6) == 1)) << (bit & 0x3F));
105 r->d[1] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
106 t += (uint128_t)r->d[2] + (((uint64_t)((bit >> 6) == 2)) << (bit & 0x3F));
107 r->d[2] = t & 0xFFFFFFFFFFFFFFFFULL; t >>= 64;
108 t += (uint128_t)r->d[3] + (((uint64_t)((bit >> 6) == 3)) << (bit & 0x3F));
109 r->d[3] = t & 0xFFFFFFFFFFFFFFFFULL;
111 VERIFY_CHECK((t >> 64) == 0);
112 VERIFY_CHECK(secp256k1_scalar_check_overflow(r) == 0);
116 static void secp256k1_scalar_set_b32(secp256k1_scalar_t *r, const unsigned char *b32, int *overflow) {
118 r->d[0] = (uint64_t)b32[31] | (uint64_t)b32[30] << 8 | (uint64_t)b32[29] << 16 | (uint64_t)b32[28] << 24 | (uint64_t)b32[27] << 32 | (uint64_t)b32[26] << 40 | (uint64_t)b32[25] << 48 | (uint64_t)b32[24] << 56;
119 r->d[1] = (uint64_t)b32[23] | (uint64_t)b32[22] << 8 | (uint64_t)b32[21] << 16 | (uint64_t)b32[20] << 24 | (uint64_t)b32[19] << 32 | (uint64_t)b32[18] << 40 | (uint64_t)b32[17] << 48 | (uint64_t)b32[16] << 56;
120 r->d[2] = (uint64_t)b32[15] | (uint64_t)b32[14] << 8 | (uint64_t)b32[13] << 16 | (uint64_t)b32[12] << 24 | (uint64_t)b32[11] << 32 | (uint64_t)b32[10] << 40 | (uint64_t)b32[9] << 48 | (uint64_t)b32[8] << 56;
121 r->d[3] = (uint64_t)b32[7] | (uint64_t)b32[6] << 8 | (uint64_t)b32[5] << 16 | (uint64_t)b32[4] << 24 | (uint64_t)b32[3] << 32 | (uint64_t)b32[2] << 40 | (uint64_t)b32[1] << 48 | (uint64_t)b32[0] << 56;
122 over = secp256k1_scalar_reduce(r, secp256k1_scalar_check_overflow(r));
128 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar_t* a) {
129 bin[0] = a->d[3] >> 56; bin[1] = a->d[3] >> 48; bin[2] = a->d[3] >> 40; bin[3] = a->d[3] >> 32; bin[4] = a->d[3] >> 24; bin[5] = a->d[3] >> 16; bin[6] = a->d[3] >> 8; bin[7] = a->d[3];
130 bin[8] = a->d[2] >> 56; bin[9] = a->d[2] >> 48; bin[10] = a->d[2] >> 40; bin[11] = a->d[2] >> 32; bin[12] = a->d[2] >> 24; bin[13] = a->d[2] >> 16; bin[14] = a->d[2] >> 8; bin[15] = a->d[2];
131 bin[16] = a->d[1] >> 56; bin[17] = a->d[1] >> 48; bin[18] = a->d[1] >> 40; bin[19] = a->d[1] >> 32; bin[20] = a->d[1] >> 24; bin[21] = a->d[1] >> 16; bin[22] = a->d[1] >> 8; bin[23] = a->d[1];
132 bin[24] = a->d[0] >> 56; bin[25] = a->d[0] >> 48; bin[26] = a->d[0] >> 40; bin[27] = a->d[0] >> 32; bin[28] = a->d[0] >> 24; bin[29] = a->d[0] >> 16; bin[30] = a->d[0] >> 8; bin[31] = a->d[0];
135 SECP256K1_INLINE static int secp256k1_scalar_is_zero(const secp256k1_scalar_t *a) {
136 return (a->d[0] | a->d[1] | a->d[2] | a->d[3]) == 0;
139 static void secp256k1_scalar_negate(secp256k1_scalar_t *r, const secp256k1_scalar_t *a) {
140 uint64_t nonzero = 0xFFFFFFFFFFFFFFFFULL * (secp256k1_scalar_is_zero(a) == 0);
141 uint128_t t = (uint128_t)(~a->d[0]) + SECP256K1_N_0 + 1;
142 r->d[0] = t & nonzero; t >>= 64;
143 t += (uint128_t)(~a->d[1]) + SECP256K1_N_1;
144 r->d[1] = t & nonzero; t >>= 64;
145 t += (uint128_t)(~a->d[2]) + SECP256K1_N_2;
146 r->d[2] = t & nonzero; t >>= 64;
147 t += (uint128_t)(~a->d[3]) + SECP256K1_N_3;
148 r->d[3] = t & nonzero;
151 SECP256K1_INLINE static int secp256k1_scalar_is_one(const secp256k1_scalar_t *a) {
152 return ((a->d[0] ^ 1) | a->d[1] | a->d[2] | a->d[3]) == 0;
155 static int secp256k1_scalar_is_high(const secp256k1_scalar_t *a) {
158 no |= (a->d[3] < SECP256K1_N_H_3);
159 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
160 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes; /* No need for a > check. */
161 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
162 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
163 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
167 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
169 /** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
170 #define muladd(a,b) { \
173 uint128_t t = (uint128_t)a * b; \
174 th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
177 c0 += tl; /* overflow is handled on the next line */ \
178 th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFFFFFFFFFF */ \
179 c1 += th; /* overflow is handled on the next line */ \
180 c2 += (c1 < th) ? 1 : 0; /* never overflows by contract (verified in the next line) */ \
181 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
184 /** Add a*b to the number defined by (c0,c1). c1 must never overflow. */
185 #define muladd_fast(a,b) { \
188 uint128_t t = (uint128_t)a * b; \
189 th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
192 c0 += tl; /* overflow is handled on the next line */ \
193 th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFFFFFFFFFF */ \
194 c1 += th; /* never overflows by contract (verified in the next line) */ \
195 VERIFY_CHECK(c1 >= th); \
198 /** Add 2*a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
199 #define muladd2(a,b) { \
200 uint64_t tl, th, th2, tl2; \
202 uint128_t t = (uint128_t)a * b; \
203 th = t >> 64; /* at most 0xFFFFFFFFFFFFFFFE */ \
206 th2 = th + th; /* at most 0xFFFFFFFFFFFFFFFE (in case th was 0x7FFFFFFFFFFFFFFF) */ \
207 c2 += (th2 < th) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
208 VERIFY_CHECK((th2 >= th) || (c2 != 0)); \
209 tl2 = tl + tl; /* at most 0xFFFFFFFFFFFFFFFE (in case the lowest 63 bits of tl were 0x7FFFFFFFFFFFFFFF) */ \
210 th2 += (tl2 < tl) ? 1 : 0; /* at most 0xFFFFFFFFFFFFFFFF */ \
211 c0 += tl2; /* overflow is handled on the next line */ \
212 th2 += (c0 < tl2) ? 1 : 0; /* second overflow is handled on the next line */ \
213 c2 += (c0 < tl2) & (th2 == 0); /* never overflows by contract (verified the next line) */ \
214 VERIFY_CHECK((c0 >= tl2) || (th2 != 0) || (c2 != 0)); \
215 c1 += th2; /* overflow is handled on the next line */ \
216 c2 += (c1 < th2) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
217 VERIFY_CHECK((c1 >= th2) || (c2 != 0)); \
220 /** Add a to the number defined by (c0,c1,c2). c2 must never overflow. */
221 #define sumadd(a) { \
223 c0 += (a); /* overflow is handled on the next line */ \
224 over = (c0 < (a)) ? 1 : 0; \
225 c1 += over; /* overflow is handled on the next line */ \
226 c2 += (c1 < over) ? 1 : 0; /* never overflows by contract */ \
229 /** Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero. */
230 #define sumadd_fast(a) { \
231 c0 += (a); /* overflow is handled on the next line */ \
232 c1 += (c0 < (a)) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
233 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
234 VERIFY_CHECK(c2 == 0); \
237 /** Extract the lowest 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits. */
238 #define extract(n) { \
245 /** Extract the lowest 64 bits of (c0,c1,c2) into n, and left shift the number 64 bits. c2 is required to be zero. */
246 #define extract_fast(n) { \
250 VERIFY_CHECK(c2 == 0); \
253 static void secp256k1_scalar_reduce_512(secp256k1_scalar_t *r, const uint64_t *l) {
254 #ifdef USE_ASM_X86_64
255 /* Reduce 512 bits into 385. */
256 uint64_t m0, m1, m2, m3, m4, m5, m6;
257 uint64_t p0, p1, p2, p3, p4;
260 __asm__ __volatile__(
262 "movq 32(%%rsi), %%r11\n"
263 "movq 40(%%rsi), %%r12\n"
264 "movq 48(%%rsi), %%r13\n"
265 "movq 56(%%rsi), %%r14\n"
266 /* Initialize r8,r9,r10 */
267 "movq 0(%%rsi), %%r8\n"
270 /* (r8,r9) += n0 * c0 */
279 "addq 8(%%rsi), %%r9\n"
281 /* (r9,r10,r8) += n1 * c0 */
285 "adcq %%rdx, %%r10\n"
287 /* (r9,r10,r8) += n0 * c1 */
291 "adcq %%rdx, %%r10\n"
296 /* (r10,r8,r9) += l2 */
297 "addq 16(%%rsi), %%r10\n"
300 /* (r10,r8,r9) += n2 * c0 */
303 "addq %%rax, %%r10\n"
306 /* (r10,r8,r9) += n1 * c1 */
309 "addq %%rax, %%r10\n"
312 /* (r10,r8,r9) += n0 */
313 "addq %%r11, %%r10\n"
319 /* (r8,r9,r10) += l3 */
320 "addq 24(%%rsi), %%r8\n"
323 /* (r8,r9,r10) += n3 * c0 */
329 /* (r8,r9,r10) += n2 * c1 */
335 /* (r8,r9,r10) += n1 */
342 /* (r9,r10,r8) += n3 * c1 */
346 "adcq %%rdx, %%r10\n"
348 /* (r9,r10,r8) += n2 */
355 "addq %%r14, %%r10\n"
361 : "=g"(m0), "=g"(m1), "=g"(m2), "=g"(m3), "=g"(m4), "=g"(m5), "=g"(m6)
362 : "S"(l), "n"(SECP256K1_N_C_0), "n"(SECP256K1_N_C_1)
363 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "cc");
365 /* Reduce 385 bits into 258. */
366 __asm__ __volatile__(
371 /* Initialize (r8,r9,r10) */
375 /* (r8,r9) += m4 * c0 */
386 /* (r9,r10,r8) += m5 * c0 */
390 "adcq %%rdx, %%r10\n"
392 /* (r9,r10,r8) += m4 * c1 */
396 "adcq %%rdx, %%r10\n"
401 /* (r10,r8,r9) += m2 */
405 /* (r10,r8,r9) += m6 * c0 */
408 "addq %%rax, %%r10\n"
411 /* (r10,r8,r9) += m5 * c1 */
414 "addq %%rax, %%r10\n"
417 /* (r10,r8,r9) += m4 */
418 "addq %%r11, %%r10\n"
426 /* (r8,r9) += m6 * c1 */
440 : "=&g"(p0), "=&g"(p1), "=&g"(p2), "=g"(p3), "=g"(p4)
441 : "g"(m0), "g"(m1), "g"(m2), "g"(m3), "g"(m4), "g"(m5), "g"(m6), "n"(SECP256K1_N_C_0), "n"(SECP256K1_N_C_1)
442 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "cc");
444 /* Reduce 258 bits into 256. */
445 __asm__ __volatile__(
448 /* (rax,rdx) = p4 * c0 */
451 /* (rax,rdx) += p0 */
455 "movq %%rax, 0(%q6)\n"
456 /* Move to (r8,r9) */
462 /* (r8,r9) += p4 * c1 */
468 "movq %%r8, 8(%q6)\n"
477 "movq %%r9, 16(%q6)\n"
483 "movq %%r8, 24(%q6)\n"
487 : "g"(p0), "g"(p1), "g"(p2), "g"(p3), "g"(p4), "D"(r), "n"(SECP256K1_N_C_0), "n"(SECP256K1_N_C_1)
488 : "rax", "rdx", "r8", "r9", "r10", "cc", "memory");
492 uint64_t n0 = l[4], n1 = l[5], n2 = l[6], n3 = l[7];
493 uint64_t m0, m1, m2, m3, m4, m5;
495 uint64_t p0, p1, p2, p3;
498 /* Reduce 512 bits into 385. */
499 /* m[0..6] = l[0..3] + n[0..3] * SECP256K1_N_C. */
500 c0 = l[0]; c1 = 0; c2 = 0;
501 muladd_fast(n0, SECP256K1_N_C_0);
504 muladd(n1, SECP256K1_N_C_0);
505 muladd(n0, SECP256K1_N_C_1);
508 muladd(n2, SECP256K1_N_C_0);
509 muladd(n1, SECP256K1_N_C_1);
513 muladd(n3, SECP256K1_N_C_0);
514 muladd(n2, SECP256K1_N_C_1);
517 muladd(n3, SECP256K1_N_C_1);
522 VERIFY_CHECK(c0 <= 1);
525 /* Reduce 385 bits into 258. */
526 /* p[0..4] = m[0..3] + m[4..6] * SECP256K1_N_C. */
527 c0 = m0; c1 = 0; c2 = 0;
528 muladd_fast(m4, SECP256K1_N_C_0);
531 muladd(m5, SECP256K1_N_C_0);
532 muladd(m4, SECP256K1_N_C_1);
535 muladd(m6, SECP256K1_N_C_0);
536 muladd(m5, SECP256K1_N_C_1);
540 muladd_fast(m6, SECP256K1_N_C_1);
544 VERIFY_CHECK(p4 <= 2);
546 /* Reduce 258 bits into 256. */
547 /* r[0..3] = p[0..3] + p[4] * SECP256K1_N_C. */
548 c = p0 + (uint128_t)SECP256K1_N_C_0 * p4;
549 r->d[0] = c & 0xFFFFFFFFFFFFFFFFULL; c >>= 64;
550 c += p1 + (uint128_t)SECP256K1_N_C_1 * p4;
551 r->d[1] = c & 0xFFFFFFFFFFFFFFFFULL; c >>= 64;
552 c += p2 + (uint128_t)p4;
553 r->d[2] = c & 0xFFFFFFFFFFFFFFFFULL; c >>= 64;
555 r->d[3] = c & 0xFFFFFFFFFFFFFFFFULL; c >>= 64;
558 /* Final reduction of r. */
559 secp256k1_scalar_reduce(r, c + secp256k1_scalar_check_overflow(r));
562 static void secp256k1_scalar_mul_512(uint64_t l[8], const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
563 #ifdef USE_ASM_X86_64
564 const uint64_t *pb = b->d;
565 __asm__ __volatile__(
567 "movq 0(%%rdi), %%r15\n"
568 "movq 8(%%rdi), %%rbx\n"
569 "movq 16(%%rdi), %%rcx\n"
570 "movq 0(%%rdx), %%r11\n"
571 "movq 8(%%rdx), %%r12\n"
572 "movq 16(%%rdx), %%r13\n"
573 "movq 24(%%rdx), %%r14\n"
574 /* (rax,rdx) = a0 * b0 */
575 "movq %%r15, %%rax\n"
578 "movq %%rax, 0(%%rsi)\n"
579 /* (r8,r9,r10) = (rdx) */
582 "xorq %%r10, %%r10\n"
583 /* (r8,r9,r10) += a0 * b1 */
584 "movq %%r15, %%rax\n"
589 /* (r8,r9,r10) += a1 * b0 */
590 "movq %%rbx, %%rax\n"
596 "movq %%r8, 8(%%rsi)\n"
598 /* (r9,r10,r8) += a0 * b2 */
599 "movq %%r15, %%rax\n"
602 "adcq %%rdx, %%r10\n"
604 /* (r9,r10,r8) += a1 * b1 */
605 "movq %%rbx, %%rax\n"
608 "adcq %%rdx, %%r10\n"
610 /* (r9,r10,r8) += a2 * b0 */
611 "movq %%rcx, %%rax\n"
614 "adcq %%rdx, %%r10\n"
617 "movq %%r9, 16(%%rsi)\n"
619 /* (r10,r8,r9) += a0 * b3 */
620 "movq %%r15, %%rax\n"
622 "addq %%rax, %%r10\n"
626 "movq 24(%%rdi), %%r15\n"
627 /* (r10,r8,r9) += a1 * b2 */
628 "movq %%rbx, %%rax\n"
630 "addq %%rax, %%r10\n"
633 /* (r10,r8,r9) += a2 * b1 */
634 "movq %%rcx, %%rax\n"
636 "addq %%rax, %%r10\n"
639 /* (r10,r8,r9) += a3 * b0 */
640 "movq %%r15, %%rax\n"
642 "addq %%rax, %%r10\n"
646 "movq %%r10, 24(%%rsi)\n"
647 "xorq %%r10, %%r10\n"
648 /* (r8,r9,r10) += a1 * b3 */
649 "movq %%rbx, %%rax\n"
654 /* (r8,r9,r10) += a2 * b2 */
655 "movq %%rcx, %%rax\n"
660 /* (r8,r9,r10) += a3 * b1 */
661 "movq %%r15, %%rax\n"
667 "movq %%r8, 32(%%rsi)\n"
669 /* (r9,r10,r8) += a2 * b3 */
670 "movq %%rcx, %%rax\n"
673 "adcq %%rdx, %%r10\n"
675 /* (r9,r10,r8) += a3 * b2 */
676 "movq %%r15, %%rax\n"
679 "adcq %%rdx, %%r10\n"
682 "movq %%r9, 40(%%rsi)\n"
683 /* (r10,r8) += a3 * b3 */
684 "movq %%r15, %%rax\n"
686 "addq %%rax, %%r10\n"
689 "movq %%r10, 48(%%rsi)\n"
691 "movq %%r8, 56(%%rsi)\n"
694 : "rax", "rbx", "rcx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "cc", "memory");
696 /* 160 bit accumulator. */
697 uint64_t c0 = 0, c1 = 0;
700 /* l[0..7] = a[0..3] * b[0..3]. */
701 muladd_fast(a->d[0], b->d[0]);
703 muladd(a->d[0], b->d[1]);
704 muladd(a->d[1], b->d[0]);
706 muladd(a->d[0], b->d[2]);
707 muladd(a->d[1], b->d[1]);
708 muladd(a->d[2], b->d[0]);
710 muladd(a->d[0], b->d[3]);
711 muladd(a->d[1], b->d[2]);
712 muladd(a->d[2], b->d[1]);
713 muladd(a->d[3], b->d[0]);
715 muladd(a->d[1], b->d[3]);
716 muladd(a->d[2], b->d[2]);
717 muladd(a->d[3], b->d[1]);
719 muladd(a->d[2], b->d[3]);
720 muladd(a->d[3], b->d[2]);
722 muladd_fast(a->d[3], b->d[3]);
724 VERIFY_CHECK(c1 <= 0);
729 static void secp256k1_scalar_sqr_512(uint64_t l[8], const secp256k1_scalar_t *a) {
730 #ifdef USE_ASM_X86_64
731 __asm__ __volatile__(
733 "movq 0(%%rdi), %%r11\n"
734 "movq 8(%%rdi), %%r12\n"
735 "movq 16(%%rdi), %%r13\n"
736 "movq 24(%%rdi), %%r14\n"
737 /* (rax,rdx) = a0 * a0 */
738 "movq %%r11, %%rax\n"
741 "movq %%rax, 0(%%rsi)\n"
742 /* (r8,r9,r10) = (rdx,0) */
745 "xorq %%r10, %%r10\n"
746 /* (r8,r9,r10) += 2 * a0 * a1 */
747 "movq %%r11, %%rax\n"
756 "movq %%r8, 8(%%rsi)\n"
758 /* (r9,r10,r8) += 2 * a0 * a2 */
759 "movq %%r11, %%rax\n"
762 "adcq %%rdx, %%r10\n"
765 "adcq %%rdx, %%r10\n"
767 /* (r9,r10,r8) += a1 * a1 */
768 "movq %%r12, %%rax\n"
771 "adcq %%rdx, %%r10\n"
774 "movq %%r9, 16(%%rsi)\n"
776 /* (r10,r8,r9) += 2 * a0 * a3 */
777 "movq %%r11, %%rax\n"
779 "addq %%rax, %%r10\n"
782 "addq %%rax, %%r10\n"
785 /* (r10,r8,r9) += 2 * a1 * a2 */
786 "movq %%r12, %%rax\n"
788 "addq %%rax, %%r10\n"
791 "addq %%rax, %%r10\n"
795 "movq %%r10, 24(%%rsi)\n"
796 "xorq %%r10, %%r10\n"
797 /* (r8,r9,r10) += 2 * a1 * a3 */
798 "movq %%r12, %%rax\n"
806 /* (r8,r9,r10) += a2 * a2 */
807 "movq %%r13, %%rax\n"
813 "movq %%r8, 32(%%rsi)\n"
815 /* (r9,r10,r8) += 2 * a2 * a3 */
816 "movq %%r13, %%rax\n"
819 "adcq %%rdx, %%r10\n"
822 "adcq %%rdx, %%r10\n"
825 "movq %%r9, 40(%%rsi)\n"
826 /* (r10,r8) += a3 * a3 */
827 "movq %%r14, %%rax\n"
829 "addq %%rax, %%r10\n"
832 "movq %%r10, 48(%%rsi)\n"
834 "movq %%r8, 56(%%rsi)\n"
837 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "cc", "memory");
839 /* 160 bit accumulator. */
840 uint64_t c0 = 0, c1 = 0;
843 /* l[0..7] = a[0..3] * b[0..3]. */
844 muladd_fast(a->d[0], a->d[0]);
846 muladd2(a->d[0], a->d[1]);
848 muladd2(a->d[0], a->d[2]);
849 muladd(a->d[1], a->d[1]);
851 muladd2(a->d[0], a->d[3]);
852 muladd2(a->d[1], a->d[2]);
854 muladd2(a->d[1], a->d[3]);
855 muladd(a->d[2], a->d[2]);
857 muladd2(a->d[2], a->d[3]);
859 muladd_fast(a->d[3], a->d[3]);
861 VERIFY_CHECK(c1 == 0);
874 static void secp256k1_scalar_mul(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
876 secp256k1_scalar_mul_512(l, a, b);
877 secp256k1_scalar_reduce_512(r, l);
880 static void secp256k1_scalar_sqr(secp256k1_scalar_t *r, const secp256k1_scalar_t *a) {
882 secp256k1_scalar_sqr_512(l, a);
883 secp256k1_scalar_reduce_512(r, l);
886 static void secp256k1_scalar_split_128(secp256k1_scalar_t *r1, secp256k1_scalar_t *r2, const secp256k1_scalar_t *a) {
897 SECP256K1_INLINE static int secp256k1_scalar_eq(const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
898 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;
901 SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b, unsigned int shift) {
903 unsigned int shiftlimbs;
904 unsigned int shiftlow;
905 unsigned int shifthigh;
906 VERIFY_CHECK(shift >= 256);
907 secp256k1_scalar_mul_512(l, a, b);
908 shiftlimbs = shift >> 6;
909 shiftlow = shift & 0x3F;
910 shifthigh = 64 - shiftlow;
911 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
912 r->d[1] = shift < 448 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
913 r->d[2] = shift < 384 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
914 r->d[3] = shift < 320 ? (l[3 + shiftlimbs] >> shiftlow) : 0;
915 if ((l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1) {
916 secp256k1_scalar_add_bit(r, 0);