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) {
94 VERIFY_CHECK(overflow <= 1);
95 uint64_t t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
96 r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
97 t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
98 r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
99 t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
100 r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
101 t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
102 r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
103 t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
104 r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
105 t += (uint64_t)r->d[5];
106 r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
107 t += (uint64_t)r->d[6];
108 r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
109 t += (uint64_t)r->d[7];
110 r->d[7] = t & 0xFFFFFFFFUL;
114 static int secp256k1_scalar_add(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
115 uint64_t t = (uint64_t)a->d[0] + b->d[0];
116 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
117 t += (uint64_t)a->d[1] + b->d[1];
118 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
119 t += (uint64_t)a->d[2] + b->d[2];
120 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
121 t += (uint64_t)a->d[3] + b->d[3];
122 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
123 t += (uint64_t)a->d[4] + b->d[4];
124 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
125 t += (uint64_t)a->d[5] + b->d[5];
126 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
127 t += (uint64_t)a->d[6] + b->d[6];
128 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
129 t += (uint64_t)a->d[7] + b->d[7];
130 r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
131 int overflow = t + secp256k1_scalar_check_overflow(r);
132 VERIFY_CHECK(overflow == 0 || overflow == 1);
133 secp256k1_scalar_reduce(r, overflow);
137 static void secp256k1_scalar_add_bit(secp256k1_scalar_t *r, unsigned int bit) {
138 VERIFY_CHECK(bit < 256);
139 uint64_t t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
140 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
141 t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
142 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
143 t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
144 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
145 t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
146 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
147 t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
148 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
149 t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
150 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
151 t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
152 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
153 t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
154 r->d[7] = t & 0xFFFFFFFFULL;
156 VERIFY_CHECK((t >> 32) == 0);
157 VERIFY_CHECK(secp256k1_scalar_check_overflow(r) == 0);
161 static void secp256k1_scalar_set_b32(secp256k1_scalar_t *r, const unsigned char *b32, int *overflow) {
162 r->d[0] = (uint32_t)b32[31] | (uint32_t)b32[30] << 8 | (uint32_t)b32[29] << 16 | (uint32_t)b32[28] << 24;
163 r->d[1] = (uint32_t)b32[27] | (uint32_t)b32[26] << 8 | (uint32_t)b32[25] << 16 | (uint32_t)b32[24] << 24;
164 r->d[2] = (uint32_t)b32[23] | (uint32_t)b32[22] << 8 | (uint32_t)b32[21] << 16 | (uint32_t)b32[20] << 24;
165 r->d[3] = (uint32_t)b32[19] | (uint32_t)b32[18] << 8 | (uint32_t)b32[17] << 16 | (uint32_t)b32[16] << 24;
166 r->d[4] = (uint32_t)b32[15] | (uint32_t)b32[14] << 8 | (uint32_t)b32[13] << 16 | (uint32_t)b32[12] << 24;
167 r->d[5] = (uint32_t)b32[11] | (uint32_t)b32[10] << 8 | (uint32_t)b32[9] << 16 | (uint32_t)b32[8] << 24;
168 r->d[6] = (uint32_t)b32[7] | (uint32_t)b32[6] << 8 | (uint32_t)b32[5] << 16 | (uint32_t)b32[4] << 24;
169 r->d[7] = (uint32_t)b32[3] | (uint32_t)b32[2] << 8 | (uint32_t)b32[1] << 16 | (uint32_t)b32[0] << 24;
170 int over = secp256k1_scalar_reduce(r, secp256k1_scalar_check_overflow(r));
176 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar_t* a) {
177 bin[0] = a->d[7] >> 24; bin[1] = a->d[7] >> 16; bin[2] = a->d[7] >> 8; bin[3] = a->d[7];
178 bin[4] = a->d[6] >> 24; bin[5] = a->d[6] >> 16; bin[6] = a->d[6] >> 8; bin[7] = a->d[6];
179 bin[8] = a->d[5] >> 24; bin[9] = a->d[5] >> 16; bin[10] = a->d[5] >> 8; bin[11] = a->d[5];
180 bin[12] = a->d[4] >> 24; bin[13] = a->d[4] >> 16; bin[14] = a->d[4] >> 8; bin[15] = a->d[4];
181 bin[16] = a->d[3] >> 24; bin[17] = a->d[3] >> 16; bin[18] = a->d[3] >> 8; bin[19] = a->d[3];
182 bin[20] = a->d[2] >> 24; bin[21] = a->d[2] >> 16; bin[22] = a->d[2] >> 8; bin[23] = a->d[2];
183 bin[24] = a->d[1] >> 24; bin[25] = a->d[1] >> 16; bin[26] = a->d[1] >> 8; bin[27] = a->d[1];
184 bin[28] = a->d[0] >> 24; bin[29] = a->d[0] >> 16; bin[30] = a->d[0] >> 8; bin[31] = a->d[0];
187 SECP256K1_INLINE static int secp256k1_scalar_is_zero(const secp256k1_scalar_t *a) {
188 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;
191 static void secp256k1_scalar_negate(secp256k1_scalar_t *r, const secp256k1_scalar_t *a) {
192 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
193 uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
194 r->d[0] = t & nonzero; t >>= 32;
195 t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
196 r->d[1] = t & nonzero; t >>= 32;
197 t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
198 r->d[2] = t & nonzero; t >>= 32;
199 t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
200 r->d[3] = t & nonzero; t >>= 32;
201 t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
202 r->d[4] = t & nonzero; t >>= 32;
203 t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
204 r->d[5] = t & nonzero; t >>= 32;
205 t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
206 r->d[6] = t & nonzero; t >>= 32;
207 t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
208 r->d[7] = t & nonzero;
211 SECP256K1_INLINE static int secp256k1_scalar_is_one(const secp256k1_scalar_t *a) {
212 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;
215 static int secp256k1_scalar_is_high(const secp256k1_scalar_t *a) {
218 no |= (a->d[7] < SECP256K1_N_H_7);
219 yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
220 no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
221 no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
222 no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
223 no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
224 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
225 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
226 yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
227 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
228 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
229 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
233 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
235 /** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
236 #define muladd(a,b) { \
239 uint64_t t = (uint64_t)a * b; \
240 th = t >> 32; /* at most 0xFFFFFFFE */ \
243 c0 += tl; /* overflow is handled on the next line */ \
244 th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
245 c1 += th; /* overflow is handled on the next line */ \
246 c2 += (c1 < th) ? 1 : 0; /* never overflows by contract (verified in the next line) */ \
247 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
250 /** Add a*b to the number defined by (c0,c1). c1 must never overflow. */
251 #define muladd_fast(a,b) { \
254 uint64_t t = (uint64_t)a * b; \
255 th = t >> 32; /* at most 0xFFFFFFFE */ \
258 c0 += tl; /* overflow is handled on the next line */ \
259 th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
260 c1 += th; /* never overflows by contract (verified in the next line) */ \
261 VERIFY_CHECK(c1 >= th); \
264 /** Add 2*a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
265 #define muladd2(a,b) { \
268 uint64_t t = (uint64_t)a * b; \
269 th = t >> 32; /* at most 0xFFFFFFFE */ \
272 uint32_t th2 = th + th; /* at most 0xFFFFFFFE (in case th was 0x7FFFFFFF) */ \
273 c2 += (th2 < th) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
274 VERIFY_CHECK((th2 >= th) || (c2 != 0)); \
275 uint32_t tl2 = tl + tl; /* at most 0xFFFFFFFE (in case the lowest 63 bits of tl were 0x7FFFFFFF) */ \
276 th2 += (tl2 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
277 c0 += tl2; /* overflow is handled on the next line */ \
278 th2 += (c0 < tl2) ? 1 : 0; /* second overflow is handled on the next line */ \
279 c2 += (c0 < tl2) & (th2 == 0); /* never overflows by contract (verified the next line) */ \
280 VERIFY_CHECK((c0 >= tl2) || (th2 != 0) || (c2 != 0)); \
281 c1 += th2; /* overflow is handled on the next line */ \
282 c2 += (c1 < th2) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
283 VERIFY_CHECK((c1 >= th2) || (c2 != 0)); \
286 /** Add a to the number defined by (c0,c1,c2). c2 must never overflow. */
287 #define sumadd(a) { \
288 c0 += (a); /* overflow is handled on the next line */ \
289 unsigned int over = (c0 < (a)) ? 1 : 0; \
290 c1 += over; /* overflow is handled on the next line */ \
291 c2 += (c1 < over) ? 1 : 0; /* never overflows by contract */ \
294 /** Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero. */
295 #define sumadd_fast(a) { \
296 c0 += (a); /* overflow is handled on the next line */ \
297 c1 += (c0 < (a)) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
298 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
299 VERIFY_CHECK(c2 == 0); \
302 /** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. */
303 #define extract(n) { \
310 /** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. c2 is required to be zero. */
311 #define extract_fast(n) { \
315 VERIFY_CHECK(c2 == 0); \
318 static void secp256k1_scalar_reduce_512(secp256k1_scalar_t *r, const uint32_t *l) {
319 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];
321 /* 96 bit accumulator. */
324 /* Reduce 512 bits into 385. */
325 /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
326 c0 = l[0]; c1 = 0; c2 = 0;
327 muladd_fast(n0, SECP256K1_N_C_0);
328 uint32_t m0; extract_fast(m0);
330 muladd(n1, SECP256K1_N_C_0);
331 muladd(n0, SECP256K1_N_C_1);
332 uint32_t m1; extract(m1);
334 muladd(n2, SECP256K1_N_C_0);
335 muladd(n1, SECP256K1_N_C_1);
336 muladd(n0, SECP256K1_N_C_2);
337 uint32_t m2; extract(m2);
339 muladd(n3, SECP256K1_N_C_0);
340 muladd(n2, SECP256K1_N_C_1);
341 muladd(n1, SECP256K1_N_C_2);
342 muladd(n0, SECP256K1_N_C_3);
343 uint32_t m3; extract(m3);
345 muladd(n4, SECP256K1_N_C_0);
346 muladd(n3, SECP256K1_N_C_1);
347 muladd(n2, SECP256K1_N_C_2);
348 muladd(n1, SECP256K1_N_C_3);
350 uint32_t m4; extract(m4);
352 muladd(n5, SECP256K1_N_C_0);
353 muladd(n4, SECP256K1_N_C_1);
354 muladd(n3, SECP256K1_N_C_2);
355 muladd(n2, SECP256K1_N_C_3);
357 uint32_t m5; extract(m5);
359 muladd(n6, SECP256K1_N_C_0);
360 muladd(n5, SECP256K1_N_C_1);
361 muladd(n4, SECP256K1_N_C_2);
362 muladd(n3, SECP256K1_N_C_3);
364 uint32_t m6; extract(m6);
366 muladd(n7, SECP256K1_N_C_0);
367 muladd(n6, SECP256K1_N_C_1);
368 muladd(n5, SECP256K1_N_C_2);
369 muladd(n4, SECP256K1_N_C_3);
371 uint32_t m7; extract(m7);
372 muladd(n7, SECP256K1_N_C_1);
373 muladd(n6, SECP256K1_N_C_2);
374 muladd(n5, SECP256K1_N_C_3);
376 uint32_t m8; extract(m8);
377 muladd(n7, SECP256K1_N_C_2);
378 muladd(n6, SECP256K1_N_C_3);
380 uint32_t m9; extract(m9);
381 muladd(n7, SECP256K1_N_C_3);
383 uint32_t m10; extract(m10);
385 uint32_t m11; extract_fast(m11);
386 VERIFY_CHECK(c0 <= 1);
389 /* Reduce 385 bits into 258. */
390 /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
391 c0 = m0; c1 = 0; c2 = 0;
392 muladd_fast(m8, SECP256K1_N_C_0);
393 uint32_t p0; extract_fast(p0);
395 muladd(m9, SECP256K1_N_C_0);
396 muladd(m8, SECP256K1_N_C_1);
397 uint32_t p1; extract(p1);
399 muladd(m10, SECP256K1_N_C_0);
400 muladd(m9, SECP256K1_N_C_1);
401 muladd(m8, SECP256K1_N_C_2);
402 uint32_t p2; extract(p2);
404 muladd(m11, SECP256K1_N_C_0);
405 muladd(m10, SECP256K1_N_C_1);
406 muladd(m9, SECP256K1_N_C_2);
407 muladd(m8, SECP256K1_N_C_3);
408 uint32_t p3; extract(p3);
410 muladd(m12, SECP256K1_N_C_0);
411 muladd(m11, SECP256K1_N_C_1);
412 muladd(m10, SECP256K1_N_C_2);
413 muladd(m9, SECP256K1_N_C_3);
415 uint32_t p4; extract(p4);
417 muladd(m12, SECP256K1_N_C_1);
418 muladd(m11, SECP256K1_N_C_2);
419 muladd(m10, SECP256K1_N_C_3);
421 uint32_t p5; extract(p5);
423 muladd(m12, SECP256K1_N_C_2);
424 muladd(m11, SECP256K1_N_C_3);
426 uint32_t p6; extract(p6);
428 muladd_fast(m12, SECP256K1_N_C_3);
430 uint32_t p7; extract_fast(p7);
431 uint32_t p8 = c0 + m12;
432 VERIFY_CHECK(p8 <= 2);
434 /* Reduce 258 bits into 256. */
435 /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
436 uint64_t c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
437 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
438 c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
439 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
440 c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
441 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
442 c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
443 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
444 c += p4 + (uint64_t)p8;
445 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
447 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
449 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
451 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
453 /* Final reduction of r. */
454 secp256k1_scalar_reduce(r, c + secp256k1_scalar_check_overflow(r));
457 static void secp256k1_scalar_mul_512(uint32_t l[16], const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
458 /* 96 bit accumulator. */
459 uint32_t c0 = 0, c1 = 0, c2 = 0;
461 /* l[0..15] = a[0..7] * b[0..7]. */
462 muladd_fast(a->d[0], b->d[0]);
464 muladd(a->d[0], b->d[1]);
465 muladd(a->d[1], b->d[0]);
467 muladd(a->d[0], b->d[2]);
468 muladd(a->d[1], b->d[1]);
469 muladd(a->d[2], b->d[0]);
471 muladd(a->d[0], b->d[3]);
472 muladd(a->d[1], b->d[2]);
473 muladd(a->d[2], b->d[1]);
474 muladd(a->d[3], b->d[0]);
476 muladd(a->d[0], b->d[4]);
477 muladd(a->d[1], b->d[3]);
478 muladd(a->d[2], b->d[2]);
479 muladd(a->d[3], b->d[1]);
480 muladd(a->d[4], b->d[0]);
482 muladd(a->d[0], b->d[5]);
483 muladd(a->d[1], b->d[4]);
484 muladd(a->d[2], b->d[3]);
485 muladd(a->d[3], b->d[2]);
486 muladd(a->d[4], b->d[1]);
487 muladd(a->d[5], b->d[0]);
489 muladd(a->d[0], b->d[6]);
490 muladd(a->d[1], b->d[5]);
491 muladd(a->d[2], b->d[4]);
492 muladd(a->d[3], b->d[3]);
493 muladd(a->d[4], b->d[2]);
494 muladd(a->d[5], b->d[1]);
495 muladd(a->d[6], b->d[0]);
497 muladd(a->d[0], b->d[7]);
498 muladd(a->d[1], b->d[6]);
499 muladd(a->d[2], b->d[5]);
500 muladd(a->d[3], b->d[4]);
501 muladd(a->d[4], b->d[3]);
502 muladd(a->d[5], b->d[2]);
503 muladd(a->d[6], b->d[1]);
504 muladd(a->d[7], b->d[0]);
506 muladd(a->d[1], b->d[7]);
507 muladd(a->d[2], b->d[6]);
508 muladd(a->d[3], b->d[5]);
509 muladd(a->d[4], b->d[4]);
510 muladd(a->d[5], b->d[3]);
511 muladd(a->d[6], b->d[2]);
512 muladd(a->d[7], b->d[1]);
514 muladd(a->d[2], b->d[7]);
515 muladd(a->d[3], b->d[6]);
516 muladd(a->d[4], b->d[5]);
517 muladd(a->d[5], b->d[4]);
518 muladd(a->d[6], b->d[3]);
519 muladd(a->d[7], b->d[2]);
521 muladd(a->d[3], b->d[7]);
522 muladd(a->d[4], b->d[6]);
523 muladd(a->d[5], b->d[5]);
524 muladd(a->d[6], b->d[4]);
525 muladd(a->d[7], b->d[3]);
527 muladd(a->d[4], b->d[7]);
528 muladd(a->d[5], b->d[6]);
529 muladd(a->d[6], b->d[5]);
530 muladd(a->d[7], b->d[4]);
532 muladd(a->d[5], b->d[7]);
533 muladd(a->d[6], b->d[6]);
534 muladd(a->d[7], b->d[5]);
536 muladd(a->d[6], b->d[7]);
537 muladd(a->d[7], b->d[6]);
539 muladd_fast(a->d[7], b->d[7]);
541 VERIFY_CHECK(c1 == 0);
545 static void secp256k1_scalar_sqr_512(uint32_t l[16], const secp256k1_scalar_t *a) {
546 /* 96 bit accumulator. */
547 uint32_t c0 = 0, c1 = 0, c2 = 0;
549 /* l[0..15] = a[0..7]^2. */
550 muladd_fast(a->d[0], a->d[0]);
552 muladd2(a->d[0], a->d[1]);
554 muladd2(a->d[0], a->d[2]);
555 muladd(a->d[1], a->d[1]);
557 muladd2(a->d[0], a->d[3]);
558 muladd2(a->d[1], a->d[2]);
560 muladd2(a->d[0], a->d[4]);
561 muladd2(a->d[1], a->d[3]);
562 muladd(a->d[2], a->d[2]);
564 muladd2(a->d[0], a->d[5]);
565 muladd2(a->d[1], a->d[4]);
566 muladd2(a->d[2], a->d[3]);
568 muladd2(a->d[0], a->d[6]);
569 muladd2(a->d[1], a->d[5]);
570 muladd2(a->d[2], a->d[4]);
571 muladd(a->d[3], a->d[3]);
573 muladd2(a->d[0], a->d[7]);
574 muladd2(a->d[1], a->d[6]);
575 muladd2(a->d[2], a->d[5]);
576 muladd2(a->d[3], a->d[4]);
578 muladd2(a->d[1], a->d[7]);
579 muladd2(a->d[2], a->d[6]);
580 muladd2(a->d[3], a->d[5]);
581 muladd(a->d[4], a->d[4]);
583 muladd2(a->d[2], a->d[7]);
584 muladd2(a->d[3], a->d[6]);
585 muladd2(a->d[4], a->d[5]);
587 muladd2(a->d[3], a->d[7]);
588 muladd2(a->d[4], a->d[6]);
589 muladd(a->d[5], a->d[5]);
591 muladd2(a->d[4], a->d[7]);
592 muladd2(a->d[5], a->d[6]);
594 muladd2(a->d[5], a->d[7]);
595 muladd(a->d[6], a->d[6]);
597 muladd2(a->d[6], a->d[7]);
599 muladd_fast(a->d[7], a->d[7]);
601 VERIFY_CHECK(c1 == 0);
613 static void secp256k1_scalar_mul(secp256k1_scalar_t *r, const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
615 secp256k1_scalar_mul_512(l, a, b);
616 secp256k1_scalar_reduce_512(r, l);
619 static void secp256k1_scalar_sqr(secp256k1_scalar_t *r, const secp256k1_scalar_t *a) {
621 secp256k1_scalar_sqr_512(l, a);
622 secp256k1_scalar_reduce_512(r, l);
625 static void secp256k1_scalar_split_128(secp256k1_scalar_t *r1, secp256k1_scalar_t *r2, const secp256k1_scalar_t *a) {
644 SECP256K1_INLINE static int secp256k1_scalar_eq(const secp256k1_scalar_t *a, const secp256k1_scalar_t *b) {
645 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;
648 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) {
649 VERIFY_CHECK(shift >= 256);
651 secp256k1_scalar_mul_512(l, a, b);
652 unsigned int shiftlimbs = shift >> 5;
653 unsigned int shiftlow = shift & 0x1F;
654 unsigned int shifthigh = 32 - shiftlow;
655 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
656 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
657 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
658 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
659 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
660 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
661 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
662 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
663 if ((l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1) {
664 secp256k1_scalar_add_bit(r, 0);