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