1 /**********************************************************************
2 * Copyright (c) 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 ((uint32_t)0xD0364141UL)
12 #define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
13 #define SECP256K1_N_2 ((uint32_t)0xAF48A03BUL)
14 #define SECP256K1_N_3 ((uint32_t)0xBAAEDCE6UL)
15 #define SECP256K1_N_4 ((uint32_t)0xFFFFFFFEUL)
16 #define SECP256K1_N_5 ((uint32_t)0xFFFFFFFFUL)
17 #define SECP256K1_N_6 ((uint32_t)0xFFFFFFFFUL)
18 #define SECP256K1_N_7 ((uint32_t)0xFFFFFFFFUL)
20 /* Limbs of 2^256 minus the secp256k1 order. */
21 #define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
22 #define SECP256K1_N_C_1 (~SECP256K1_N_1)
23 #define SECP256K1_N_C_2 (~SECP256K1_N_2)
24 #define SECP256K1_N_C_3 (~SECP256K1_N_3)
25 #define SECP256K1_N_C_4 (1)
27 /* Limbs of half the secp256k1 order. */
28 #define SECP256K1_N_H_0 ((uint32_t)0x681B20A0UL)
29 #define SECP256K1_N_H_1 ((uint32_t)0xDFE92F46UL)
30 #define SECP256K1_N_H_2 ((uint32_t)0x57A4501DUL)
31 #define SECP256K1_N_H_3 ((uint32_t)0x5D576E73UL)
32 #define SECP256K1_N_H_4 ((uint32_t)0xFFFFFFFFUL)
33 #define SECP256K1_N_H_5 ((uint32_t)0xFFFFFFFFUL)
34 #define SECP256K1_N_H_6 ((uint32_t)0xFFFFFFFFUL)
35 #define SECP256K1_N_H_7 ((uint32_t)0x7FFFFFFFUL)
37 SECP256K1_INLINE static void secp256k1_scalar_clear(secp256k1_scalar_t *r) {
48 SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar_t *r, unsigned int v) {
59 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar_t *a, unsigned int offset, unsigned int count) {
60 VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
61 return (a->d[offset >> 5] >> (offset & 0x1F)) & ((1 << count) - 1);
64 SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar_t *a, unsigned int offset, unsigned int count) {
65 VERIFY_CHECK(count < 32);
66 VERIFY_CHECK(offset + count <= 256);
67 if ((offset + count - 1) >> 5 == offset >> 5) {
68 return secp256k1_scalar_get_bits(a, offset, count);
70 VERIFY_CHECK((offset >> 5) + 1 < 8);
71 return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & ((((uint32_t)1) << count) - 1);
75 SECP256K1_INLINE static int secp256k1_scalar_check_overflow(const secp256k1_scalar_t *a) {
78 no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */
79 no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */
80 no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */
81 no |= (a->d[4] < SECP256K1_N_4);
82 yes |= (a->d[4] > SECP256K1_N_4) & ~no;
83 no |= (a->d[3] < SECP256K1_N_3) & ~yes;
84 yes |= (a->d[3] > SECP256K1_N_3) & ~no;
85 no |= (a->d[2] < SECP256K1_N_2) & ~yes;
86 yes |= (a->d[2] > SECP256K1_N_2) & ~no;
87 no |= (a->d[1] < SECP256K1_N_1) & ~yes;
88 yes |= (a->d[1] > SECP256K1_N_1) & ~no;
89 yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
93 SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar_t *r, uint32_t overflow) {
95 VERIFY_CHECK(overflow <= 1);
96 t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
97 r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
98 t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
99 r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
100 t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
101 r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
102 t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
103 r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
104 t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
105 r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
106 t += (uint64_t)r->d[5];
107 r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
108 t += (uint64_t)r->d[6];
109 r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
110 t += (uint64_t)r->d[7];
111 r->d[7] = t & 0xFFFFFFFFUL;
115 static int secp256k1_scalar_add(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
117 uint64_t t = (uint64_t)a->d[0] + b->d[0];
118 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
119 t += (uint64_t)a->d[1] + b->d[1];
120 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
121 t += (uint64_t)a->d[2] + b->d[2];
122 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
123 t += (uint64_t)a->d[3] + b->d[3];
124 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
125 t += (uint64_t)a->d[4] + b->d[4];
126 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
127 t += (uint64_t)a->d[5] + b->d[5];
128 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
129 t += (uint64_t)a->d[6] + b->d[6];
130 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
131 t += (uint64_t)a->d[7] + b->d[7];
132 r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
133 overflow = t + secp256k1_scalar_check_overflow(r);
134 VERIFY_CHECK(overflow == 0 || overflow == 1);
135 secp256k1_scalar_reduce(r, overflow);
139 static void secp256k1_scalar_add_bit(secp256k1_scalar_t *r, unsigned int bit) {
141 VERIFY_CHECK(bit < 256);
142 t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
143 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
144 t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
145 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
146 t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
147 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
148 t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
149 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
150 t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
151 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
152 t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
153 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
154 t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
155 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
156 t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
157 r->d[7] = t & 0xFFFFFFFFULL;
159 VERIFY_CHECK((t >> 32) == 0);
160 VERIFY_CHECK(secp256k1_scalar_check_overflow(r) == 0);
164 static void secp256k1_scalar_set_b32(secp256k1_scalar_t *r, const unsigned char *b32, int *overflow) {
166 r->d[0] = (uint32_t)b32[31] | (uint32_t)b32[30] << 8 | (uint32_t)b32[29] << 16 | (uint32_t)b32[28] << 24;
167 r->d[1] = (uint32_t)b32[27] | (uint32_t)b32[26] << 8 | (uint32_t)b32[25] << 16 | (uint32_t)b32[24] << 24;
168 r->d[2] = (uint32_t)b32[23] | (uint32_t)b32[22] << 8 | (uint32_t)b32[21] << 16 | (uint32_t)b32[20] << 24;
169 r->d[3] = (uint32_t)b32[19] | (uint32_t)b32[18] << 8 | (uint32_t)b32[17] << 16 | (uint32_t)b32[16] << 24;
170 r->d[4] = (uint32_t)b32[15] | (uint32_t)b32[14] << 8 | (uint32_t)b32[13] << 16 | (uint32_t)b32[12] << 24;
171 r->d[5] = (uint32_t)b32[11] | (uint32_t)b32[10] << 8 | (uint32_t)b32[9] << 16 | (uint32_t)b32[8] << 24;
172 r->d[6] = (uint32_t)b32[7] | (uint32_t)b32[6] << 8 | (uint32_t)b32[5] << 16 | (uint32_t)b32[4] << 24;
173 r->d[7] = (uint32_t)b32[3] | (uint32_t)b32[2] << 8 | (uint32_t)b32[1] << 16 | (uint32_t)b32[0] << 24;
174 over = secp256k1_scalar_reduce(r, secp256k1_scalar_check_overflow(r));
180 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar_t* a) {
181 bin[0] = a->d[7] >> 24; bin[1] = a->d[7] >> 16; bin[2] = a->d[7] >> 8; bin[3] = a->d[7];
182 bin[4] = a->d[6] >> 24; bin[5] = a->d[6] >> 16; bin[6] = a->d[6] >> 8; bin[7] = a->d[6];
183 bin[8] = a->d[5] >> 24; bin[9] = a->d[5] >> 16; bin[10] = a->d[5] >> 8; bin[11] = a->d[5];
184 bin[12] = a->d[4] >> 24; bin[13] = a->d[4] >> 16; bin[14] = a->d[4] >> 8; bin[15] = a->d[4];
185 bin[16] = a->d[3] >> 24; bin[17] = a->d[3] >> 16; bin[18] = a->d[3] >> 8; bin[19] = a->d[3];
186 bin[20] = a->d[2] >> 24; bin[21] = a->d[2] >> 16; bin[22] = a->d[2] >> 8; bin[23] = a->d[2];
187 bin[24] = a->d[1] >> 24; bin[25] = a->d[1] >> 16; bin[26] = a->d[1] >> 8; bin[27] = a->d[1];
188 bin[28] = a->d[0] >> 24; bin[29] = a->d[0] >> 16; bin[30] = a->d[0] >> 8; bin[31] = a->d[0];
191 SECP256K1_INLINE static int secp256k1_scalar_is_zero(const secp256k1_scalar_t *a) {
192 return (a->d[0] | a->d[1] | a->d[2] | a->d[3] | a->d[4] | a->d[5] | a->d[6] | a->d[7]) == 0;
195 static void secp256k1_scalar_negate(secp256k1_scalar_t *r, const secp256k1_scalar_t *a) {
196 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
197 uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
198 r->d[0] = t & nonzero; t >>= 32;
199 t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
200 r->d[1] = t & nonzero; t >>= 32;
201 t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
202 r->d[2] = t & nonzero; t >>= 32;
203 t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
204 r->d[3] = t & nonzero; t >>= 32;
205 t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
206 r->d[4] = t & nonzero; t >>= 32;
207 t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
208 r->d[5] = t & nonzero; t >>= 32;
209 t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
210 r->d[6] = t & nonzero; t >>= 32;
211 t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
212 r->d[7] = t & nonzero;
215 SECP256K1_INLINE static int secp256k1_scalar_is_one(const secp256k1_scalar_t *a) {
216 return ((a->d[0] ^ 1) | a->d[1] | a->d[2] | a->d[3] | a->d[4] | a->d[5] | a->d[6] | a->d[7]) == 0;
219 static int secp256k1_scalar_is_high(const secp256k1_scalar_t *a) {
222 no |= (a->d[7] < SECP256K1_N_H_7);
223 yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
224 no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
225 no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
226 no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
227 no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
228 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
229 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
230 yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
231 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
232 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
233 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
237 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
239 /** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
240 #define muladd(a,b) { \
243 uint64_t t = (uint64_t)a * b; \
244 th = t >> 32; /* at most 0xFFFFFFFE */ \
247 c0 += tl; /* overflow is handled on the next line */ \
248 th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
249 c1 += th; /* overflow is handled on the next line */ \
250 c2 += (c1 < th) ? 1 : 0; /* never overflows by contract (verified in the next line) */ \
251 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
254 /** Add a*b to the number defined by (c0,c1). c1 must never overflow. */
255 #define muladd_fast(a,b) { \
258 uint64_t t = (uint64_t)a * b; \
259 th = t >> 32; /* at most 0xFFFFFFFE */ \
262 c0 += tl; /* overflow is handled on the next line */ \
263 th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
264 c1 += th; /* never overflows by contract (verified in the next line) */ \
265 VERIFY_CHECK(c1 >= th); \
268 /** Add 2*a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
269 #define muladd2(a,b) { \
270 uint32_t tl, th, th2, tl2; \
272 uint64_t t = (uint64_t)a * b; \
273 th = t >> 32; /* at most 0xFFFFFFFE */ \
276 th2 = th + th; /* at most 0xFFFFFFFE (in case th was 0x7FFFFFFF) */ \
277 c2 += (th2 < th) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
278 VERIFY_CHECK((th2 >= th) || (c2 != 0)); \
279 tl2 = tl + tl; /* at most 0xFFFFFFFE (in case the lowest 63 bits of tl were 0x7FFFFFFF) */ \
280 th2 += (tl2 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
281 c0 += tl2; /* overflow is handled on the next line */ \
282 th2 += (c0 < tl2) ? 1 : 0; /* second overflow is handled on the next line */ \
283 c2 += (c0 < tl2) & (th2 == 0); /* never overflows by contract (verified the next line) */ \
284 VERIFY_CHECK((c0 >= tl2) || (th2 != 0) || (c2 != 0)); \
285 c1 += th2; /* overflow is handled on the next line */ \
286 c2 += (c1 < th2) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
287 VERIFY_CHECK((c1 >= th2) || (c2 != 0)); \
290 /** Add a to the number defined by (c0,c1,c2). c2 must never overflow. */
291 #define sumadd(a) { \
293 c0 += (a); /* overflow is handled on the next line */ \
294 over = (c0 < (a)) ? 1 : 0; \
295 c1 += over; /* overflow is handled on the next line */ \
296 c2 += (c1 < over) ? 1 : 0; /* never overflows by contract */ \
299 /** Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero. */
300 #define sumadd_fast(a) { \
301 c0 += (a); /* overflow is handled on the next line */ \
302 c1 += (c0 < (a)) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
303 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
304 VERIFY_CHECK(c2 == 0); \
307 /** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. */
308 #define extract(n) { \
315 /** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. c2 is required to be zero. */
316 #define extract_fast(n) { \
320 VERIFY_CHECK(c2 == 0); \
323 static void secp256k1_scalar_reduce_512(secp256k1_scalar_t *r, const uint32_t *l) {
325 uint32_t n0 = l[8], n1 = l[9], n2 = l[10], n3 = l[11], n4 = l[12], n5 = l[13], n6 = l[14], n7 = l[15];
326 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
327 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
329 /* 96 bit accumulator. */
332 /* Reduce 512 bits into 385. */
333 /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
334 c0 = l[0]; c1 = 0; c2 = 0;
335 muladd_fast(n0, SECP256K1_N_C_0);
338 muladd(n1, SECP256K1_N_C_0);
339 muladd(n0, SECP256K1_N_C_1);
342 muladd(n2, SECP256K1_N_C_0);
343 muladd(n1, SECP256K1_N_C_1);
344 muladd(n0, SECP256K1_N_C_2);
347 muladd(n3, SECP256K1_N_C_0);
348 muladd(n2, SECP256K1_N_C_1);
349 muladd(n1, SECP256K1_N_C_2);
350 muladd(n0, SECP256K1_N_C_3);
353 muladd(n4, SECP256K1_N_C_0);
354 muladd(n3, SECP256K1_N_C_1);
355 muladd(n2, SECP256K1_N_C_2);
356 muladd(n1, SECP256K1_N_C_3);
360 muladd(n5, SECP256K1_N_C_0);
361 muladd(n4, SECP256K1_N_C_1);
362 muladd(n3, SECP256K1_N_C_2);
363 muladd(n2, SECP256K1_N_C_3);
367 muladd(n6, SECP256K1_N_C_0);
368 muladd(n5, SECP256K1_N_C_1);
369 muladd(n4, SECP256K1_N_C_2);
370 muladd(n3, SECP256K1_N_C_3);
374 muladd(n7, SECP256K1_N_C_0);
375 muladd(n6, SECP256K1_N_C_1);
376 muladd(n5, SECP256K1_N_C_2);
377 muladd(n4, SECP256K1_N_C_3);
380 muladd(n7, SECP256K1_N_C_1);
381 muladd(n6, SECP256K1_N_C_2);
382 muladd(n5, SECP256K1_N_C_3);
385 muladd(n7, SECP256K1_N_C_2);
386 muladd(n6, SECP256K1_N_C_3);
389 muladd(n7, SECP256K1_N_C_3);
394 VERIFY_CHECK(c0 <= 1);
397 /* Reduce 385 bits into 258. */
398 /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
399 c0 = m0; c1 = 0; c2 = 0;
400 muladd_fast(m8, SECP256K1_N_C_0);
403 muladd(m9, SECP256K1_N_C_0);
404 muladd(m8, SECP256K1_N_C_1);
407 muladd(m10, SECP256K1_N_C_0);
408 muladd(m9, SECP256K1_N_C_1);
409 muladd(m8, SECP256K1_N_C_2);
412 muladd(m11, SECP256K1_N_C_0);
413 muladd(m10, SECP256K1_N_C_1);
414 muladd(m9, SECP256K1_N_C_2);
415 muladd(m8, SECP256K1_N_C_3);
418 muladd(m12, SECP256K1_N_C_0);
419 muladd(m11, SECP256K1_N_C_1);
420 muladd(m10, SECP256K1_N_C_2);
421 muladd(m9, SECP256K1_N_C_3);
425 muladd(m12, SECP256K1_N_C_1);
426 muladd(m11, SECP256K1_N_C_2);
427 muladd(m10, SECP256K1_N_C_3);
431 muladd(m12, SECP256K1_N_C_2);
432 muladd(m11, SECP256K1_N_C_3);
436 muladd_fast(m12, SECP256K1_N_C_3);
440 VERIFY_CHECK(p8 <= 2);
442 /* Reduce 258 bits into 256. */
443 /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
444 c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
445 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
446 c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
447 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
448 c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
449 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
450 c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
451 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
452 c += p4 + (uint64_t)p8;
453 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
455 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
457 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
459 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
461 /* Final reduction of r. */
462 secp256k1_scalar_reduce(r, c + secp256k1_scalar_check_overflow(r));
465 static void secp256k1_scalar_mul_512(uint32_t l[16], const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
466 /* 96 bit accumulator. */
467 uint32_t c0 = 0, c1 = 0, c2 = 0;
469 /* l[0..15] = a[0..7] * b[0..7]. */
470 muladd_fast(a->d[0], b->d[0]);
472 muladd(a->d[0], b->d[1]);
473 muladd(a->d[1], b->d[0]);
475 muladd(a->d[0], b->d[2]);
476 muladd(a->d[1], b->d[1]);
477 muladd(a->d[2], b->d[0]);
479 muladd(a->d[0], b->d[3]);
480 muladd(a->d[1], b->d[2]);
481 muladd(a->d[2], b->d[1]);
482 muladd(a->d[3], b->d[0]);
484 muladd(a->d[0], b->d[4]);
485 muladd(a->d[1], b->d[3]);
486 muladd(a->d[2], b->d[2]);
487 muladd(a->d[3], b->d[1]);
488 muladd(a->d[4], b->d[0]);
490 muladd(a->d[0], b->d[5]);
491 muladd(a->d[1], b->d[4]);
492 muladd(a->d[2], b->d[3]);
493 muladd(a->d[3], b->d[2]);
494 muladd(a->d[4], b->d[1]);
495 muladd(a->d[5], b->d[0]);
497 muladd(a->d[0], b->d[6]);
498 muladd(a->d[1], b->d[5]);
499 muladd(a->d[2], b->d[4]);
500 muladd(a->d[3], b->d[3]);
501 muladd(a->d[4], b->d[2]);
502 muladd(a->d[5], b->d[1]);
503 muladd(a->d[6], b->d[0]);
505 muladd(a->d[0], b->d[7]);
506 muladd(a->d[1], b->d[6]);
507 muladd(a->d[2], b->d[5]);
508 muladd(a->d[3], b->d[4]);
509 muladd(a->d[4], b->d[3]);
510 muladd(a->d[5], b->d[2]);
511 muladd(a->d[6], b->d[1]);
512 muladd(a->d[7], b->d[0]);
514 muladd(a->d[1], b->d[7]);
515 muladd(a->d[2], b->d[6]);
516 muladd(a->d[3], b->d[5]);
517 muladd(a->d[4], b->d[4]);
518 muladd(a->d[5], b->d[3]);
519 muladd(a->d[6], b->d[2]);
520 muladd(a->d[7], b->d[1]);
522 muladd(a->d[2], b->d[7]);
523 muladd(a->d[3], b->d[6]);
524 muladd(a->d[4], b->d[5]);
525 muladd(a->d[5], b->d[4]);
526 muladd(a->d[6], b->d[3]);
527 muladd(a->d[7], b->d[2]);
529 muladd(a->d[3], b->d[7]);
530 muladd(a->d[4], b->d[6]);
531 muladd(a->d[5], b->d[5]);
532 muladd(a->d[6], b->d[4]);
533 muladd(a->d[7], b->d[3]);
535 muladd(a->d[4], b->d[7]);
536 muladd(a->d[5], b->d[6]);
537 muladd(a->d[6], b->d[5]);
538 muladd(a->d[7], b->d[4]);
540 muladd(a->d[5], b->d[7]);
541 muladd(a->d[6], b->d[6]);
542 muladd(a->d[7], b->d[5]);
544 muladd(a->d[6], b->d[7]);
545 muladd(a->d[7], b->d[6]);
547 muladd_fast(a->d[7], b->d[7]);
549 VERIFY_CHECK(c1 == 0);
553 static void secp256k1_scalar_sqr_512(uint32_t l[16], const secp256k1_scalar_t *a) {
554 /* 96 bit accumulator. */
555 uint32_t c0 = 0, c1 = 0, c2 = 0;
557 /* l[0..15] = a[0..7]^2. */
558 muladd_fast(a->d[0], a->d[0]);
560 muladd2(a->d[0], a->d[1]);
562 muladd2(a->d[0], a->d[2]);
563 muladd(a->d[1], a->d[1]);
565 muladd2(a->d[0], a->d[3]);
566 muladd2(a->d[1], a->d[2]);
568 muladd2(a->d[0], a->d[4]);
569 muladd2(a->d[1], a->d[3]);
570 muladd(a->d[2], a->d[2]);
572 muladd2(a->d[0], a->d[5]);
573 muladd2(a->d[1], a->d[4]);
574 muladd2(a->d[2], a->d[3]);
576 muladd2(a->d[0], a->d[6]);
577 muladd2(a->d[1], a->d[5]);
578 muladd2(a->d[2], a->d[4]);
579 muladd(a->d[3], a->d[3]);
581 muladd2(a->d[0], a->d[7]);
582 muladd2(a->d[1], a->d[6]);
583 muladd2(a->d[2], a->d[5]);
584 muladd2(a->d[3], a->d[4]);
586 muladd2(a->d[1], a->d[7]);
587 muladd2(a->d[2], a->d[6]);
588 muladd2(a->d[3], a->d[5]);
589 muladd(a->d[4], a->d[4]);
591 muladd2(a->d[2], a->d[7]);
592 muladd2(a->d[3], a->d[6]);
593 muladd2(a->d[4], a->d[5]);
595 muladd2(a->d[3], a->d[7]);
596 muladd2(a->d[4], a->d[6]);
597 muladd(a->d[5], a->d[5]);
599 muladd2(a->d[4], a->d[7]);
600 muladd2(a->d[5], a->d[6]);
602 muladd2(a->d[5], a->d[7]);
603 muladd(a->d[6], a->d[6]);
605 muladd2(a->d[6], a->d[7]);
607 muladd_fast(a->d[7], a->d[7]);
609 VERIFY_CHECK(c1 == 0);
621 static void secp256k1_scalar_mul(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
623 secp256k1_scalar_mul_512(l, a, b);
624 secp256k1_scalar_reduce_512(r, l);
627 static void secp256k1_scalar_sqr(secp256k1_scalar_t *r, const secp256k1_scalar_t *a) {
629 secp256k1_scalar_sqr_512(l, a);
630 secp256k1_scalar_reduce_512(r, l);
633 static void secp256k1_scalar_split_128(secp256k1_scalar_t *r1, secp256k1_scalar_t *r2, const secp256k1_scalar_t *a) {
652 SECP256K1_INLINE static int secp256k1_scalar_eq(const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
653 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]) | (a->d[4] ^ b->d[4]) | (a->d[5] ^ b->d[5]) | (a->d[6] ^ b->d[6]) | (a->d[7] ^ b->d[7])) == 0;
656 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) {
658 unsigned int shiftlimbs;
659 unsigned int shiftlow;
660 unsigned int shifthigh;
661 VERIFY_CHECK(shift >= 256);
662 secp256k1_scalar_mul_512(l, a, b);
663 shiftlimbs = shift >> 5;
664 shiftlow = shift & 0x1F;
665 shifthigh = 32 - shiftlow;
666 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
667 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
668 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
669 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
670 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
671 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
672 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
673 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
674 if ((l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1) {
675 secp256k1_scalar_add_bit(r, 0);