Bitcoin ABC 0.32.10
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 "int128.h"
11#include "modinv64_impl.h"
12
13/* Limbs of the secp256k1 order. */
14#define SECP256K1_N_0 ((uint64_t)0xBFD25E8CD0364141ULL)
15#define SECP256K1_N_1 ((uint64_t)0xBAAEDCE6AF48A03BULL)
16#define SECP256K1_N_2 ((uint64_t)0xFFFFFFFFFFFFFFFEULL)
17#define SECP256K1_N_3 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
18
19/* Limbs of 2^256 minus the secp256k1 order. */
20#define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
21#define SECP256K1_N_C_1 (~SECP256K1_N_1)
22#define SECP256K1_N_C_2 (1)
23
24/* Limbs of half the secp256k1 order. */
25#define SECP256K1_N_H_0 ((uint64_t)0xDFE92F46681B20A0ULL)
26#define SECP256K1_N_H_1 ((uint64_t)0x5D576E7357A4501DULL)
27#define SECP256K1_N_H_2 ((uint64_t)0xFFFFFFFFFFFFFFFFULL)
28#define SECP256K1_N_H_3 ((uint64_t)0x7FFFFFFFFFFFFFFFULL)
29
31 r->d[0] = 0;
32 r->d[1] = 0;
33 r->d[2] = 0;
34 r->d[3] = 0;
35}
36
38 r->d[0] = v;
39 r->d[1] = 0;
40 r->d[2] = 0;
41 r->d[3] = 0;
42}
43
44SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
45 VERIFY_CHECK((offset + count - 1) >> 6 == offset >> 6);
46 return (a->d[offset >> 6] >> (offset & 0x3F)) & ((((uint64_t)1) << count) - 1);
47}
48
49SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
50 VERIFY_CHECK(count < 32);
51 VERIFY_CHECK(offset + count <= 256);
52 if ((offset + count - 1) >> 6 == offset >> 6) {
53 return secp256k1_scalar_get_bits(a, offset, count);
54 } else {
55 VERIFY_CHECK((offset >> 6) + 1 < 4);
56 return ((a->d[offset >> 6] >> (offset & 0x3F)) | (a->d[(offset >> 6) + 1] << (64 - (offset & 0x3F)))) & ((((uint64_t)1) << count) - 1);
57 }
58}
59
61 int yes = 0;
62 int no = 0;
63 no |= (a->d[3] < SECP256K1_N_3); /* No need for a > check. */
64 no |= (a->d[2] < SECP256K1_N_2);
65 yes |= (a->d[2] > SECP256K1_N_2) & ~no;
66 no |= (a->d[1] < SECP256K1_N_1);
67 yes |= (a->d[1] > SECP256K1_N_1) & ~no;
68 yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
69 return yes;
70}
71
72SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, unsigned int overflow) {
74 VERIFY_CHECK(overflow <= 1);
75 secp256k1_u128_from_u64(&t, r->d[0]);
78 secp256k1_u128_accum_u64(&t, r->d[1]);
81 secp256k1_u128_accum_u64(&t, r->d[2]);
84 secp256k1_u128_accum_u64(&t, r->d[3]);
85 r->d[3] = secp256k1_u128_to_u64(&t);
86 return overflow;
87}
88
90 int overflow;
92 secp256k1_u128_from_u64(&t, a->d[0]);
93 secp256k1_u128_accum_u64(&t, b->d[0]);
95 secp256k1_u128_accum_u64(&t, a->d[1]);
96 secp256k1_u128_accum_u64(&t, b->d[1]);
98 secp256k1_u128_accum_u64(&t, a->d[2]);
99 secp256k1_u128_accum_u64(&t, b->d[2]);
100 r->d[2] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
101 secp256k1_u128_accum_u64(&t, a->d[3]);
102 secp256k1_u128_accum_u64(&t, b->d[3]);
103 r->d[3] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
105 VERIFY_CHECK(overflow == 0 || overflow == 1);
106 secp256k1_scalar_reduce(r, overflow);
107 return overflow;
108}
109
110static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
112 volatile int vflag = flag;
113 VERIFY_CHECK(bit < 256);
114 bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 6) > 3 makes this a noop */
115 secp256k1_u128_from_u64(&t, r->d[0]);
116 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 0)) << (bit & 0x3F));
117 r->d[0] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
118 secp256k1_u128_accum_u64(&t, r->d[1]);
119 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 1)) << (bit & 0x3F));
120 r->d[1] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
121 secp256k1_u128_accum_u64(&t, r->d[2]);
122 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 2)) << (bit & 0x3F));
123 r->d[2] = secp256k1_u128_to_u64(&t); secp256k1_u128_rshift(&t, 64);
124 secp256k1_u128_accum_u64(&t, r->d[3]);
125 secp256k1_u128_accum_u64(&t, ((uint64_t)((bit >> 6) == 3)) << (bit & 0x3F));
126 r->d[3] = secp256k1_u128_to_u64(&t);
127#ifdef VERIFY
129#endif
130}
131
132static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
133 int over;
134 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;
135 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;
136 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;
137 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;
139 if (overflow) {
140 *overflow = over;
141 }
142}
143
144static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
145 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];
146 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];
147 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];
148 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];
149}
150
152 return (a->d[0] | a->d[1] | a->d[2] | a->d[3]) == 0;
153}
154
156 uint64_t nonzero = 0xFFFFFFFFFFFFFFFFULL * (secp256k1_scalar_is_zero(a) == 0);
158 secp256k1_u128_from_u64(&t, ~a->d[0]);
160 r->d[0] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
161 secp256k1_u128_accum_u64(&t, ~a->d[1]);
163 r->d[1] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
164 secp256k1_u128_accum_u64(&t, ~a->d[2]);
166 r->d[2] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
167 secp256k1_u128_accum_u64(&t, ~a->d[3]);
169 r->d[3] = secp256k1_u128_to_u64(&t) & nonzero;
170}
171
173 return ((a->d[0] ^ 1) | a->d[1] | a->d[2] | a->d[3]) == 0;
174}
175
177 int yes = 0;
178 int no = 0;
179 no |= (a->d[3] < SECP256K1_N_H_3);
180 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
181 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes; /* No need for a > check. */
182 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
183 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
184 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
185 return yes;
186}
187
189 /* If we are flag = 0, mask = 00...00 and this is a no-op;
190 * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
191 volatile int vflag = flag;
192 uint64_t mask = -vflag;
193 uint64_t nonzero = (secp256k1_scalar_is_zero(r) != 0) - 1;
195 secp256k1_u128_from_u64(&t, r->d[0] ^ mask);
196 secp256k1_u128_accum_u64(&t, (SECP256K1_N_0 + 1) & mask);
197 r->d[0] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
198 secp256k1_u128_accum_u64(&t, r->d[1] ^ mask);
200 r->d[1] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
201 secp256k1_u128_accum_u64(&t, r->d[2] ^ mask);
203 r->d[2] = secp256k1_u128_to_u64(&t) & nonzero; secp256k1_u128_rshift(&t, 64);
204 secp256k1_u128_accum_u64(&t, r->d[3] ^ mask);
206 r->d[3] = secp256k1_u128_to_u64(&t) & nonzero;
207 return 2 * (mask == 0) - 1;
208}
209
210/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
211
213#define muladd(a,b) { \
214 uint64_t tl, th; \
215 { \
216 secp256k1_uint128 t; \
217 secp256k1_u128_mul(&t, a, b); \
218 th = secp256k1_u128_hi_u64(&t); /* at most 0xFFFFFFFFFFFFFFFE */ \
219 tl = secp256k1_u128_to_u64(&t); \
220 } \
221 c0 += tl; /* overflow is handled on the next line */ \
222 th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
223 c1 += th; /* overflow is handled on the next line */ \
224 c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \
225 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
226}
227
229#define muladd_fast(a,b) { \
230 uint64_t tl, th; \
231 { \
232 secp256k1_uint128 t; \
233 secp256k1_u128_mul(&t, a, b); \
234 th = secp256k1_u128_hi_u64(&t); /* at most 0xFFFFFFFFFFFFFFFE */ \
235 tl = secp256k1_u128_to_u64(&t); \
236 } \
237 c0 += tl; /* overflow is handled on the next line */ \
238 th += (c0 < tl); /* at most 0xFFFFFFFFFFFFFFFF */ \
239 c1 += th; /* never overflows by contract (verified in the next line) */ \
240 VERIFY_CHECK(c1 >= th); \
241}
242
244#define sumadd(a) { \
245 unsigned int over; \
246 c0 += (a); /* overflow is handled on the next line */ \
247 over = (c0 < (a)); \
248 c1 += over; /* overflow is handled on the next line */ \
249 c2 += (c1 < over); /* never overflows by contract */ \
250}
251
253#define sumadd_fast(a) { \
254 c0 += (a); /* overflow is handled on the next line */ \
255 c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \
256 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
257 VERIFY_CHECK(c2 == 0); \
258}
259
261#define extract(n) { \
262 (n) = c0; \
263 c0 = c1; \
264 c1 = c2; \
265 c2 = 0; \
266}
267
269#define extract_fast(n) { \
270 (n) = c0; \
271 c0 = c1; \
272 c1 = 0; \
273 VERIFY_CHECK(c2 == 0); \
274}
275
276static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint64_t *l) {
277#ifdef USE_ASM_X86_64
278 /* Reduce 512 bits into 385. */
279 uint64_t m0, m1, m2, m3, m4, m5, m6;
280 uint64_t p0, p1, p2, p3, p4;
281 uint64_t c;
282
283 __asm__ __volatile__(
284 /* Preload. */
285 "movq 32(%%rsi), %%r11\n"
286 "movq 40(%%rsi), %%r12\n"
287 "movq 48(%%rsi), %%r13\n"
288 "movq 56(%%rsi), %%r14\n"
289 /* Initialize r8,r9,r10 */
290 "movq 0(%%rsi), %%r8\n"
291 "xorq %%r9, %%r9\n"
292 "xorq %%r10, %%r10\n"
293 /* (r8,r9) += n0 * c0 */
294 "movq %8, %%rax\n"
295 "mulq %%r11\n"
296 "addq %%rax, %%r8\n"
297 "adcq %%rdx, %%r9\n"
298 /* extract m0 */
299 "movq %%r8, %q0\n"
300 "xorq %%r8, %%r8\n"
301 /* (r9,r10) += l1 */
302 "addq 8(%%rsi), %%r9\n"
303 "adcq $0, %%r10\n"
304 /* (r9,r10,r8) += n1 * c0 */
305 "movq %8, %%rax\n"
306 "mulq %%r12\n"
307 "addq %%rax, %%r9\n"
308 "adcq %%rdx, %%r10\n"
309 "adcq $0, %%r8\n"
310 /* (r9,r10,r8) += n0 * c1 */
311 "movq %9, %%rax\n"
312 "mulq %%r11\n"
313 "addq %%rax, %%r9\n"
314 "adcq %%rdx, %%r10\n"
315 "adcq $0, %%r8\n"
316 /* extract m1 */
317 "movq %%r9, %q1\n"
318 "xorq %%r9, %%r9\n"
319 /* (r10,r8,r9) += l2 */
320 "addq 16(%%rsi), %%r10\n"
321 "adcq $0, %%r8\n"
322 "adcq $0, %%r9\n"
323 /* (r10,r8,r9) += n2 * c0 */
324 "movq %8, %%rax\n"
325 "mulq %%r13\n"
326 "addq %%rax, %%r10\n"
327 "adcq %%rdx, %%r8\n"
328 "adcq $0, %%r9\n"
329 /* (r10,r8,r9) += n1 * c1 */
330 "movq %9, %%rax\n"
331 "mulq %%r12\n"
332 "addq %%rax, %%r10\n"
333 "adcq %%rdx, %%r8\n"
334 "adcq $0, %%r9\n"
335 /* (r10,r8,r9) += n0 */
336 "addq %%r11, %%r10\n"
337 "adcq $0, %%r8\n"
338 "adcq $0, %%r9\n"
339 /* extract m2 */
340 "movq %%r10, %q2\n"
341 "xorq %%r10, %%r10\n"
342 /* (r8,r9,r10) += l3 */
343 "addq 24(%%rsi), %%r8\n"
344 "adcq $0, %%r9\n"
345 "adcq $0, %%r10\n"
346 /* (r8,r9,r10) += n3 * c0 */
347 "movq %8, %%rax\n"
348 "mulq %%r14\n"
349 "addq %%rax, %%r8\n"
350 "adcq %%rdx, %%r9\n"
351 "adcq $0, %%r10\n"
352 /* (r8,r9,r10) += n2 * c1 */
353 "movq %9, %%rax\n"
354 "mulq %%r13\n"
355 "addq %%rax, %%r8\n"
356 "adcq %%rdx, %%r9\n"
357 "adcq $0, %%r10\n"
358 /* (r8,r9,r10) += n1 */
359 "addq %%r12, %%r8\n"
360 "adcq $0, %%r9\n"
361 "adcq $0, %%r10\n"
362 /* extract m3 */
363 "movq %%r8, %q3\n"
364 "xorq %%r8, %%r8\n"
365 /* (r9,r10,r8) += n3 * c1 */
366 "movq %9, %%rax\n"
367 "mulq %%r14\n"
368 "addq %%rax, %%r9\n"
369 "adcq %%rdx, %%r10\n"
370 "adcq $0, %%r8\n"
371 /* (r9,r10,r8) += n2 */
372 "addq %%r13, %%r9\n"
373 "adcq $0, %%r10\n"
374 "adcq $0, %%r8\n"
375 /* extract m4 */
376 "movq %%r9, %q4\n"
377 /* (r10,r8) += n3 */
378 "addq %%r14, %%r10\n"
379 "adcq $0, %%r8\n"
380 /* extract m5 */
381 "movq %%r10, %q5\n"
382 /* extract m6 */
383 "movq %%r8, %q6\n"
384 : "=g"(m0), "=g"(m1), "=g"(m2), "=g"(m3), "=g"(m4), "=g"(m5), "=g"(m6)
385 : "S"(l), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
386 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "cc");
387
388 /* Reduce 385 bits into 258. */
389 __asm__ __volatile__(
390 /* Preload */
391 "movq %q9, %%r11\n"
392 "movq %q10, %%r12\n"
393 "movq %q11, %%r13\n"
394 /* Initialize (r8,r9,r10) */
395 "movq %q5, %%r8\n"
396 "xorq %%r9, %%r9\n"
397 "xorq %%r10, %%r10\n"
398 /* (r8,r9) += m4 * c0 */
399 "movq %12, %%rax\n"
400 "mulq %%r11\n"
401 "addq %%rax, %%r8\n"
402 "adcq %%rdx, %%r9\n"
403 /* extract p0 */
404 "movq %%r8, %q0\n"
405 "xorq %%r8, %%r8\n"
406 /* (r9,r10) += m1 */
407 "addq %q6, %%r9\n"
408 "adcq $0, %%r10\n"
409 /* (r9,r10,r8) += m5 * c0 */
410 "movq %12, %%rax\n"
411 "mulq %%r12\n"
412 "addq %%rax, %%r9\n"
413 "adcq %%rdx, %%r10\n"
414 "adcq $0, %%r8\n"
415 /* (r9,r10,r8) += m4 * c1 */
416 "movq %13, %%rax\n"
417 "mulq %%r11\n"
418 "addq %%rax, %%r9\n"
419 "adcq %%rdx, %%r10\n"
420 "adcq $0, %%r8\n"
421 /* extract p1 */
422 "movq %%r9, %q1\n"
423 "xorq %%r9, %%r9\n"
424 /* (r10,r8,r9) += m2 */
425 "addq %q7, %%r10\n"
426 "adcq $0, %%r8\n"
427 "adcq $0, %%r9\n"
428 /* (r10,r8,r9) += m6 * c0 */
429 "movq %12, %%rax\n"
430 "mulq %%r13\n"
431 "addq %%rax, %%r10\n"
432 "adcq %%rdx, %%r8\n"
433 "adcq $0, %%r9\n"
434 /* (r10,r8,r9) += m5 * c1 */
435 "movq %13, %%rax\n"
436 "mulq %%r12\n"
437 "addq %%rax, %%r10\n"
438 "adcq %%rdx, %%r8\n"
439 "adcq $0, %%r9\n"
440 /* (r10,r8,r9) += m4 */
441 "addq %%r11, %%r10\n"
442 "adcq $0, %%r8\n"
443 "adcq $0, %%r9\n"
444 /* extract p2 */
445 "movq %%r10, %q2\n"
446 /* (r8,r9) += m3 */
447 "addq %q8, %%r8\n"
448 "adcq $0, %%r9\n"
449 /* (r8,r9) += m6 * c1 */
450 "movq %13, %%rax\n"
451 "mulq %%r13\n"
452 "addq %%rax, %%r8\n"
453 "adcq %%rdx, %%r9\n"
454 /* (r8,r9) += m5 */
455 "addq %%r12, %%r8\n"
456 "adcq $0, %%r9\n"
457 /* extract p3 */
458 "movq %%r8, %q3\n"
459 /* (r9) += m6 */
460 "addq %%r13, %%r9\n"
461 /* extract p4 */
462 "movq %%r9, %q4\n"
463 : "=&g"(p0), "=&g"(p1), "=&g"(p2), "=g"(p3), "=g"(p4)
464 : "g"(m0), "g"(m1), "g"(m2), "g"(m3), "g"(m4), "g"(m5), "g"(m6), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
465 : "rax", "rdx", "r8", "r9", "r10", "r11", "r12", "r13", "cc");
466
467 /* Reduce 258 bits into 256. */
468 __asm__ __volatile__(
469 /* Preload */
470 "movq %q5, %%r10\n"
471 /* (rax,rdx) = p4 * c0 */
472 "movq %7, %%rax\n"
473 "mulq %%r10\n"
474 /* (rax,rdx) += p0 */
475 "addq %q1, %%rax\n"
476 "adcq $0, %%rdx\n"
477 /* extract r0 */
478 "movq %%rax, 0(%q6)\n"
479 /* Move to (r8,r9) */
480 "movq %%rdx, %%r8\n"
481 "xorq %%r9, %%r9\n"
482 /* (r8,r9) += p1 */
483 "addq %q2, %%r8\n"
484 "adcq $0, %%r9\n"
485 /* (r8,r9) += p4 * c1 */
486 "movq %8, %%rax\n"
487 "mulq %%r10\n"
488 "addq %%rax, %%r8\n"
489 "adcq %%rdx, %%r9\n"
490 /* Extract r1 */
491 "movq %%r8, 8(%q6)\n"
492 "xorq %%r8, %%r8\n"
493 /* (r9,r8) += p4 */
494 "addq %%r10, %%r9\n"
495 "adcq $0, %%r8\n"
496 /* (r9,r8) += p2 */
497 "addq %q3, %%r9\n"
498 "adcq $0, %%r8\n"
499 /* Extract r2 */
500 "movq %%r9, 16(%q6)\n"
501 "xorq %%r9, %%r9\n"
502 /* (r8,r9) += p3 */
503 "addq %q4, %%r8\n"
504 "adcq $0, %%r9\n"
505 /* Extract r3 */
506 "movq %%r8, 24(%q6)\n"
507 /* Extract c */
508 "movq %%r9, %q0\n"
509 : "=g"(c)
510 : "g"(p0), "g"(p1), "g"(p2), "g"(p3), "g"(p4), "D"(r), "i"(SECP256K1_N_C_0), "i"(SECP256K1_N_C_1)
511 : "rax", "rdx", "r8", "r9", "r10", "cc", "memory");
512#else
514 uint64_t c, c0, c1, c2;
515 uint64_t n0 = l[4], n1 = l[5], n2 = l[6], n3 = l[7];
516 uint64_t m0, m1, m2, m3, m4, m5;
517 uint32_t m6;
518 uint64_t p0, p1, p2, p3;
519 uint32_t p4;
520
521 /* Reduce 512 bits into 385. */
522 /* m[0..6] = l[0..3] + n[0..3] * SECP256K1_N_C. */
523 c0 = l[0]; c1 = 0; c2 = 0;
525 extract_fast(m0);
526 sumadd_fast(l[1]);
529 extract(m1);
530 sumadd(l[2]);
533 sumadd(n0);
534 extract(m2);
535 sumadd(l[3]);
538 sumadd(n1);
539 extract(m3);
541 sumadd(n2);
542 extract(m4);
543 sumadd_fast(n3);
544 extract_fast(m5);
545 VERIFY_CHECK(c0 <= 1);
546 m6 = c0;
547
548 /* Reduce 385 bits into 258. */
549 /* p[0..4] = m[0..3] + m[4..6] * SECP256K1_N_C. */
550 c0 = m0; c1 = 0; c2 = 0;
552 extract_fast(p0);
553 sumadd_fast(m1);
556 extract(p1);
557 sumadd(m2);
560 sumadd(m4);
561 extract(p2);
562 sumadd_fast(m3);
564 sumadd_fast(m5);
565 extract_fast(p3);
566 p4 = c0 + m6;
567 VERIFY_CHECK(p4 <= 2);
568
569 /* Reduce 258 bits into 256. */
570 /* r[0..3] = p[0..3] + p[4] * SECP256K1_N_C. */
571 secp256k1_u128_from_u64(&c128, p0);
573 r->d[0] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
574 secp256k1_u128_accum_u64(&c128, p1);
576 r->d[1] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
577 secp256k1_u128_accum_u64(&c128, p2);
578 secp256k1_u128_accum_u64(&c128, p4);
579 r->d[2] = secp256k1_u128_to_u64(&c128); secp256k1_u128_rshift(&c128, 64);
580 secp256k1_u128_accum_u64(&c128, p3);
581 r->d[3] = secp256k1_u128_to_u64(&c128);
582 c = secp256k1_u128_hi_u64(&c128);
583#endif
584
585 /* Final reduction of r. */
587}
588
589static void secp256k1_scalar_mul_512(uint64_t l[8], const secp256k1_scalar *a, const secp256k1_scalar *b) {
590#ifdef USE_ASM_X86_64
591 const uint64_t *pb = b->d;
592 __asm__ __volatile__(
593 /* Preload */
594 "movq 0(%%rdi), %%r15\n"
595 "movq 8(%%rdi), %%rbx\n"
596 "movq 16(%%rdi), %%rcx\n"
597 "movq 0(%%rdx), %%r11\n"
598 "movq 8(%%rdx), %%r12\n"
599 "movq 16(%%rdx), %%r13\n"
600 "movq 24(%%rdx), %%r14\n"
601 /* (rax,rdx) = a0 * b0 */
602 "movq %%r15, %%rax\n"
603 "mulq %%r11\n"
604 /* Extract l0 */
605 "movq %%rax, 0(%%rsi)\n"
606 /* (r8,r9,r10) = (rdx) */
607 "movq %%rdx, %%r8\n"
608 "xorq %%r9, %%r9\n"
609 "xorq %%r10, %%r10\n"
610 /* (r8,r9,r10) += a0 * b1 */
611 "movq %%r15, %%rax\n"
612 "mulq %%r12\n"
613 "addq %%rax, %%r8\n"
614 "adcq %%rdx, %%r9\n"
615 "adcq $0, %%r10\n"
616 /* (r8,r9,r10) += a1 * b0 */
617 "movq %%rbx, %%rax\n"
618 "mulq %%r11\n"
619 "addq %%rax, %%r8\n"
620 "adcq %%rdx, %%r9\n"
621 "adcq $0, %%r10\n"
622 /* Extract l1 */
623 "movq %%r8, 8(%%rsi)\n"
624 "xorq %%r8, %%r8\n"
625 /* (r9,r10,r8) += a0 * b2 */
626 "movq %%r15, %%rax\n"
627 "mulq %%r13\n"
628 "addq %%rax, %%r9\n"
629 "adcq %%rdx, %%r10\n"
630 "adcq $0, %%r8\n"
631 /* (r9,r10,r8) += a1 * b1 */
632 "movq %%rbx, %%rax\n"
633 "mulq %%r12\n"
634 "addq %%rax, %%r9\n"
635 "adcq %%rdx, %%r10\n"
636 "adcq $0, %%r8\n"
637 /* (r9,r10,r8) += a2 * b0 */
638 "movq %%rcx, %%rax\n"
639 "mulq %%r11\n"
640 "addq %%rax, %%r9\n"
641 "adcq %%rdx, %%r10\n"
642 "adcq $0, %%r8\n"
643 /* Extract l2 */
644 "movq %%r9, 16(%%rsi)\n"
645 "xorq %%r9, %%r9\n"
646 /* (r10,r8,r9) += a0 * b3 */
647 "movq %%r15, %%rax\n"
648 "mulq %%r14\n"
649 "addq %%rax, %%r10\n"
650 "adcq %%rdx, %%r8\n"
651 "adcq $0, %%r9\n"
652 /* Preload a3 */
653 "movq 24(%%rdi), %%r15\n"
654 /* (r10,r8,r9) += a1 * b2 */
655 "movq %%rbx, %%rax\n"
656 "mulq %%r13\n"
657 "addq %%rax, %%r10\n"
658 "adcq %%rdx, %%r8\n"
659 "adcq $0, %%r9\n"
660 /* (r10,r8,r9) += a2 * b1 */
661 "movq %%rcx, %%rax\n"
662 "mulq %%r12\n"
663 "addq %%rax, %%r10\n"
664 "adcq %%rdx, %%r8\n"
665 "adcq $0, %%r9\n"
666 /* (r10,r8,r9) += a3 * b0 */
667 "movq %%r15, %%rax\n"
668 "mulq %%r11\n"
669 "addq %%rax, %%r10\n"
670 "adcq %%rdx, %%r8\n"
671 "adcq $0, %%r9\n"
672 /* Extract l3 */
673 "movq %%r10, 24(%%rsi)\n"
674 "xorq %%r10, %%r10\n"
675 /* (r8,r9,r10) += a1 * b3 */
676 "movq %%rbx, %%rax\n"
677 "mulq %%r14\n"
678 "addq %%rax, %%r8\n"
679 "adcq %%rdx, %%r9\n"
680 "adcq $0, %%r10\n"
681 /* (r8,r9,r10) += a2 * b2 */
682 "movq %%rcx, %%rax\n"
683 "mulq %%r13\n"
684 "addq %%rax, %%r8\n"
685 "adcq %%rdx, %%r9\n"
686 "adcq $0, %%r10\n"
687 /* (r8,r9,r10) += a3 * b1 */
688 "movq %%r15, %%rax\n"
689 "mulq %%r12\n"
690 "addq %%rax, %%r8\n"
691 "adcq %%rdx, %%r9\n"
692 "adcq $0, %%r10\n"
693 /* Extract l4 */
694 "movq %%r8, 32(%%rsi)\n"
695 "xorq %%r8, %%r8\n"
696 /* (r9,r10,r8) += a2 * b3 */
697 "movq %%rcx, %%rax\n"
698 "mulq %%r14\n"
699 "addq %%rax, %%r9\n"
700 "adcq %%rdx, %%r10\n"
701 "adcq $0, %%r8\n"
702 /* (r9,r10,r8) += a3 * b2 */
703 "movq %%r15, %%rax\n"
704 "mulq %%r13\n"
705 "addq %%rax, %%r9\n"
706 "adcq %%rdx, %%r10\n"
707 "adcq $0, %%r8\n"
708 /* Extract l5 */
709 "movq %%r9, 40(%%rsi)\n"
710 /* (r10,r8) += a3 * b3 */
711 "movq %%r15, %%rax\n"
712 "mulq %%r14\n"
713 "addq %%rax, %%r10\n"
714 "adcq %%rdx, %%r8\n"
715 /* Extract l6 */
716 "movq %%r10, 48(%%rsi)\n"
717 /* Extract l7 */
718 "movq %%r8, 56(%%rsi)\n"
719 : "+d"(pb)
720 : "S"(l), "D"(a->d)
721 : "rax", "rbx", "rcx", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "cc", "memory");
722#else
723 /* 160 bit accumulator. */
724 uint64_t c0 = 0, c1 = 0;
725 uint32_t c2 = 0;
726
727 /* l[0..7] = a[0..3] * b[0..3]. */
728 muladd_fast(a->d[0], b->d[0]);
729 extract_fast(l[0]);
730 muladd(a->d[0], b->d[1]);
731 muladd(a->d[1], b->d[0]);
732 extract(l[1]);
733 muladd(a->d[0], b->d[2]);
734 muladd(a->d[1], b->d[1]);
735 muladd(a->d[2], b->d[0]);
736 extract(l[2]);
737 muladd(a->d[0], b->d[3]);
738 muladd(a->d[1], b->d[2]);
739 muladd(a->d[2], b->d[1]);
740 muladd(a->d[3], b->d[0]);
741 extract(l[3]);
742 muladd(a->d[1], b->d[3]);
743 muladd(a->d[2], b->d[2]);
744 muladd(a->d[3], b->d[1]);
745 extract(l[4]);
746 muladd(a->d[2], b->d[3]);
747 muladd(a->d[3], b->d[2]);
748 extract(l[5]);
749 muladd_fast(a->d[3], b->d[3]);
750 extract_fast(l[6]);
751 VERIFY_CHECK(c1 == 0);
752 l[7] = c0;
753#endif
754}
755
756#undef sumadd
757#undef sumadd_fast
758#undef muladd
759#undef muladd_fast
760#undef extract
761#undef extract_fast
762
764 uint64_t l[8];
767}
768
770 int ret;
771 VERIFY_CHECK(n > 0);
772 VERIFY_CHECK(n < 16);
773 ret = r->d[0] & ((1 << n) - 1);
774 r->d[0] = (r->d[0] >> n) + (r->d[1] << (64 - n));
775 r->d[1] = (r->d[1] >> n) + (r->d[2] << (64 - n));
776 r->d[2] = (r->d[2] >> n) + (r->d[3] << (64 - n));
777 r->d[3] = (r->d[3] >> n);
778 return ret;
779}
780
782 r1->d[0] = k->d[0];
783 r1->d[1] = k->d[1];
784 r1->d[2] = 0;
785 r1->d[3] = 0;
786 r2->d[0] = k->d[2];
787 r2->d[1] = k->d[3];
788 r2->d[2] = 0;
789 r2->d[3] = 0;
790}
791
793 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;
794}
795
797 uint64_t l[8];
798 unsigned int shiftlimbs;
799 unsigned int shiftlow;
800 unsigned int shifthigh;
801 VERIFY_CHECK(shift >= 256);
803 shiftlimbs = shift >> 6;
804 shiftlow = shift & 0x3F;
805 shifthigh = 64 - shiftlow;
806 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
807 r->d[1] = shift < 448 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
808 r->d[2] = shift < 384 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
809 r->d[3] = shift < 320 ? (l[3 + shiftlimbs] >> shiftlow) : 0;
810 secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 6] >> ((shift - 1) & 0x3f)) & 1);
811}
812
814 uint64_t mask0, mask1;
815 volatile int vflag = flag;
816 VG_CHECK_VERIFY(r->d, sizeof(r->d));
817 mask0 = vflag + ~((uint64_t)0);
818 mask1 = ~mask0;
819 r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1);
820 r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1);
821 r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1);
822 r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1);
823}
824
826 const uint64_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4];
827
828 /* The output from secp256k1_modinv64{_var} should be normalized to range [0,modulus), and
829 * have limbs in [0,2^62). The modulus is < 2^256, so the top limb must be below 2^(256-62*4).
830 */
831 VERIFY_CHECK(a0 >> 62 == 0);
832 VERIFY_CHECK(a1 >> 62 == 0);
833 VERIFY_CHECK(a2 >> 62 == 0);
834 VERIFY_CHECK(a3 >> 62 == 0);
835 VERIFY_CHECK(a4 >> 8 == 0);
836
837 r->d[0] = a0 | a1 << 62;
838 r->d[1] = a1 >> 2 | a2 << 60;
839 r->d[2] = a2 >> 4 | a3 << 58;
840 r->d[3] = a3 >> 6 | a4 << 56;
841
842#ifdef VERIFY
844#endif
845}
846
848 const uint64_t M62 = UINT64_MAX >> 2;
849 const uint64_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3];
850
851#ifdef VERIFY
853#endif
854
855 r->v[0] = a0 & M62;
856 r->v[1] = (a0 >> 62 | a1 << 2) & M62;
857 r->v[2] = (a1 >> 60 | a2 << 4) & M62;
858 r->v[3] = (a2 >> 58 | a3 << 6) & M62;
859 r->v[4] = a3 >> 56;
860}
861
863 {{0x3FD25E8CD0364141LL, 0x2ABB739ABD2280EELL, -0x15LL, 0, 256}},
864 0x34F20099AA774EC1LL
865};
866
869#ifdef VERIFY
870 int zero_in = secp256k1_scalar_is_zero(x);
871#endif
875
876#ifdef VERIFY
878#endif
879}
880
883#ifdef VERIFY
884 int zero_in = secp256k1_scalar_is_zero(x);
885#endif
889
890#ifdef VERIFY
892#endif
893}
894
896 return !(a->d[0] & 1);
897}
898
899#endif /* SECP256K1_SCALAR_REPR_IMPL_H */
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 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_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)
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n)
#define VG_CHECK_VERIFY(x, y)
Definition: util.h:116
#define VERIFY_CHECK(cond)
Definition: util.h:96
#define SECP256K1_INLINE
Definition: secp256k1.h:131
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