7 #ifndef SECP256K1_FIELD_REPR_IMPL_H 8 #define SECP256K1_FIELD_REPR_IMPL_H 16 const uint32_t *
d = a->
n;
17 int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
18 r &= (d[0] <= 0x3FFFFFF
UL * m);
19 r &= (d[1] <= 0x3FFFFFF
UL * m);
20 r &= (d[2] <= 0x3FFFFFF
UL * m);
21 r &= (d[3] <= 0x3FFFFFF
UL * m);
22 r &= (d[4] <= 0x3FFFFFF
UL * m);
23 r &= (d[5] <= 0x3FFFFFF
UL * m);
24 r &= (d[6] <= 0x3FFFFFF
UL * m);
25 r &= (d[7] <= 0x3FFFFFF
UL * m);
26 r &= (d[8] <= 0x3FFFFFF
UL * m);
27 r &= (d[9] <= 0x03FFFFF
UL * m);
28 r &= (a->magnitude >= 0);
29 r &= (a->magnitude <= 32);
31 r &= (a->magnitude <= 1);
32 if (r && (d[9] == 0x03FFFFFUL)) {
33 uint32_t mid = d[8] & d[7] & d[6] & d[5] & d[4] & d[3] & d[2];
34 if (mid == 0x3FFFFFFUL) {
35 r &= ((d[1] + 0x40
UL + ((d[0] + 0x3D1
UL) >> 26)) <= 0x3FFFFFFUL);
44 uint32_t t0 = r->
n[0],
t1 = r->
n[1],
t2 = r->
n[2],
t3 = r->
n[3],
t4 = r->
n[4],
45 t5 = r->
n[5], t6 = r->
n[6], t7 = r->
n[7], t8 = r->
n[8], t9 = r->
n[9];
49 uint32_t
x = t9 >> 22; t9 &= 0x03FFFFF
UL;
52 t0 += x * 0x3D1
UL;
t1 += (x << 6);
53 t1 += (t0 >> 26); t0 &= 0x3FFFFFF
UL;
57 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFF
UL; m &=
t4;
58 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL; m &= t5;
59 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL; m &= t6;
60 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL; m &= t7;
61 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL; m &= t8;
67 x = (t9 >> 22) | ((t9 == 0x03FFFFFUL) & (m == 0x3FFFFFF
UL)
68 & ((
t1 + 0x40UL + ((t0 + 0x3D1UL) >> 26)) > 0x3FFFFFF
UL));
71 t0 += x * 0x3D1
UL;
t1 += (x << 6);
72 t1 += (t0 >> 26); t0 &= 0x3FFFFFF
UL;
76 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFF
UL;
77 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL;
78 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL;
79 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL;
80 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL;
88 r->
n[0] = t0; r->
n[1] =
t1; r->
n[2] =
t2; r->
n[3] =
t3; r->
n[4] =
t4;
89 r->
n[5] = t5; r->
n[6] = t6; r->
n[7] = t7; r->
n[8] = t8; r->
n[9] = t9;
94 secp256k1_fe_verify(r);
98 static void secp256k1_fe_normalize_weak(
secp256k1_fe *r) {
99 uint32_t t0 = r->
n[0],
t1 = r->
n[1],
t2 = r->
n[2],
t3 = r->
n[3],
t4 = r->
n[4],
100 t5 = r->
n[5], t6 = r->
n[6], t7 = r->
n[7], t8 = r->
n[8], t9 = r->
n[9];
103 uint32_t x = t9 >> 22; t9 &= 0x03FFFFF
UL;
106 t0 += x * 0x3D1
UL;
t1 += (x << 6);
107 t1 += (t0 >> 26); t0 &= 0x3FFFFFF
UL;
108 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFF
UL;
109 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFF
UL;
110 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFF
UL;
111 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFF
UL;
112 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL;
113 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL;
114 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL;
115 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL;
120 r->
n[0] = t0; r->
n[1] =
t1; r->
n[2] =
t2; r->
n[3] =
t3; r->
n[4] =
t4;
121 r->
n[5] = t5; r->
n[6] = t6; r->
n[7] = t7; r->
n[8] = t8; r->
n[9] = t9;
125 secp256k1_fe_verify(r);
129 static void secp256k1_fe_normalize_var(
secp256k1_fe *r) {
130 uint32_t t0 = r->
n[0],
t1 = r->
n[1],
t2 = r->
n[2],
t3 = r->
n[3],
t4 = r->
n[4],
131 t5 = r->
n[5], t6 = r->
n[6], t7 = r->
n[7], t8 = r->
n[8], t9 = r->
n[9];
135 uint32_t x = t9 >> 22; t9 &= 0x03FFFFF
UL;
138 t0 += x * 0x3D1
UL;
t1 += (x << 6);
139 t1 += (t0 >> 26); t0 &= 0x3FFFFFF
UL;
140 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFF
UL;
143 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFF
UL; m &=
t4;
144 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL; m &= t5;
145 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL; m &= t6;
146 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL; m &= t7;
147 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL; m &= t8;
153 x = (t9 >> 22) | ((t9 == 0x03FFFFFUL) & (m == 0x3FFFFFF
UL)
154 & ((
t1 + 0x40UL + ((t0 + 0x3D1UL) >> 26)) > 0x3FFFFFF
UL));
157 t0 += 0x3D1
UL;
t1 += (x << 6);
158 t1 += (t0 >> 26); t0 &= 0x3FFFFFF
UL;
159 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFF
UL;
160 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFF
UL;
161 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFF
UL;
162 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFF
UL;
163 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL;
164 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL;
165 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL;
166 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL;
175 r->
n[0] = t0; r->
n[1] =
t1; r->
n[2] =
t2; r->
n[3] =
t3; r->
n[4] =
t4;
176 r->
n[5] = t5; r->
n[6] = t6; r->
n[7] = t7; r->
n[8] = t8; r->
n[9] = t9;
181 secp256k1_fe_verify(r);
185 static int secp256k1_fe_normalizes_to_zero(
secp256k1_fe *r) {
186 uint32_t t0 = r->
n[0],
t1 = r->
n[1],
t2 = r->
n[2],
t3 = r->
n[3],
t4 = r->
n[4],
187 t5 = r->
n[5], t6 = r->
n[6], t7 = r->
n[7], t8 = r->
n[8], t9 = r->
n[9];
193 uint32_t x = t9 >> 22; t9 &= 0x03FFFFF
UL;
196 t0 += x * 0x3D1
UL;
t1 += (x << 6);
197 t1 += (t0 >> 26); t0 &= 0x3FFFFFF
UL; z0 = t0; z1 = t0 ^ 0x3D0
UL;
199 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFF
UL; z0 |=
t2; z1 &=
t2;
200 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFF
UL; z0 |=
t3; z1 &=
t3;
201 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFF
UL; z0 |=
t4; z1 &=
t4;
202 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL; z0 |= t5; z1 &= t5;
203 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL; z0 |= t6; z1 &= t6;
204 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL; z0 |= t7; z1 &= t7;
205 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL; z0 |= t8; z1 &= t8;
206 z0 |= t9; z1 &= t9 ^ 0x3C00000
UL;
211 return (z0 == 0) | (z1 == 0x3FFFFFF
UL);
214 static int secp256k1_fe_normalizes_to_zero_var(
secp256k1_fe *r) {
215 uint32_t t0,
t1,
t2,
t3,
t4, t5, t6, t7, t8, t9;
229 z0 = t0 & 0x3FFFFFF
UL;
233 if ((z0 != 0UL) & (z1 != 0x3FFFFFFUL)) {
250 t2 += (t1 >> 26); t1 &= 0x3FFFFFF
UL; z0 |=
t1; z1 &= t1 ^ 0x40
UL;
251 t3 += (t2 >> 26); t2 &= 0x3FFFFFF
UL; z0 |=
t2; z1 &=
t2;
252 t4 += (t3 >> 26); t3 &= 0x3FFFFFF
UL; z0 |=
t3; z1 &=
t3;
253 t5 += (t4 >> 26); t4 &= 0x3FFFFFF
UL; z0 |=
t4; z1 &=
t4;
254 t6 += (t5 >> 26); t5 &= 0x3FFFFFF
UL; z0 |= t5; z1 &= t5;
255 t7 += (t6 >> 26); t6 &= 0x3FFFFFF
UL; z0 |= t6; z1 &= t6;
256 t8 += (t7 >> 26); t7 &= 0x3FFFFFF
UL; z0 |= t7; z1 &= t7;
257 t9 += (t8 >> 26); t8 &= 0x3FFFFFF
UL; z0 |= t8; z1 &= t8;
258 z0 |= t9; z1 &= t9 ^ 0x3C00000
UL;
263 return (z0 == 0) | (z1 == 0x3FFFFFF
UL);
268 r->
n[1] = r->
n[2] = r->
n[3] = r->
n[4] = r->
n[5] = r->
n[6] = r->
n[7] = r->
n[8] = r->
n[9] = 0;
272 secp256k1_fe_verify(r);
277 const uint32_t *t = a->
n;
280 secp256k1_fe_verify(a);
282 return (t[0] | t[1] | t[2] | t[3] | t[4] | t[5] | t[6] | t[7] | t[8] | t[9]) == 0;
288 secp256k1_fe_verify(a);
299 for (i=0; i<10; i++) {
309 secp256k1_fe_verify(a);
310 secp256k1_fe_verify(b);
312 for (i = 9; i >= 0; i--) {
313 if (a->
n[i] > b->
n[i]) {
316 if (a->
n[i] < b->
n[i]) {
323 static int secp256k1_fe_set_b32(
secp256k1_fe *r,
const unsigned char *a) {
324 r->
n[0] = (uint32_t)a[31] | ((uint32_t)a[30] << 8) | ((uint32_t)a[29] << 16) | ((uint32_t)(a[28] & 0x3) << 24);
325 r->
n[1] = (uint32_t)((a[28] >> 2) & 0x3f) | ((uint32_t)a[27] << 6) | ((uint32_t)a[26] << 14) | ((uint32_t)(a[25] & 0xf) << 22);
326 r->
n[2] = (uint32_t)((a[25] >> 4) & 0xf) | ((uint32_t)a[24] << 4) | ((uint32_t)a[23] << 12) | ((uint32_t)(a[22] & 0x3f) << 20);
327 r->
n[3] = (uint32_t)((a[22] >> 6) & 0x3) | ((uint32_t)a[21] << 2) | ((uint32_t)a[20] << 10) | ((uint32_t)a[19] << 18);
328 r->
n[4] = (uint32_t)a[18] | ((uint32_t)a[17] << 8) | ((uint32_t)a[16] << 16) | ((uint32_t)(a[15] & 0x3) << 24);
329 r->
n[5] = (uint32_t)((a[15] >> 2) & 0x3f) | ((uint32_t)a[14] << 6) | ((uint32_t)a[13] << 14) | ((uint32_t)(a[12] & 0xf) << 22);
330 r->
n[6] = (uint32_t)((a[12] >> 4) & 0xf) | ((uint32_t)a[11] << 4) | ((uint32_t)a[10] << 12) | ((uint32_t)(a[9] & 0x3f) << 20);
331 r->
n[7] = (uint32_t)((a[9] >> 6) & 0x3) | ((uint32_t)a[8] << 2) | ((uint32_t)a[7] << 10) | ((uint32_t)a[6] << 18);
332 r->
n[8] = (uint32_t)a[5] | ((uint32_t)a[4] << 8) | ((uint32_t)a[3] << 16) | ((uint32_t)(a[2] & 0x3) << 24);
333 r->
n[9] = (uint32_t)((a[2] >> 2) & 0x3f) | ((uint32_t)a[1] << 6) | ((uint32_t)a[0] << 14);
335 if (r->
n[9] == 0x3FFFFFUL && (r->
n[8] & r->
n[7] & r->
n[6] & r->
n[5] & r->
n[4] & r->
n[3] & r->
n[2]) == 0x3FFFFFFUL && (r->
n[1] + 0x40UL + ((r->
n[0] + 0x3D1
UL) >> 26)) > 0x3FFFFFFUL) {
341 secp256k1_fe_verify(r);
347 static void secp256k1_fe_get_b32(
unsigned char *r,
const secp256k1_fe *a) {
350 secp256k1_fe_verify(a);
352 r[0] = (a->
n[9] >> 14) & 0xff;
353 r[1] = (a->
n[9] >> 6) & 0xff;
354 r[2] = ((a->
n[9] & 0x3F) << 2) | ((a->
n[8] >> 24) & 0x3);
355 r[3] = (a->
n[8] >> 16) & 0xff;
356 r[4] = (a->
n[8] >> 8) & 0xff;
357 r[5] = a->
n[8] & 0xff;
358 r[6] = (a->
n[7] >> 18) & 0xff;
359 r[7] = (a->
n[7] >> 10) & 0xff;
360 r[8] = (a->
n[7] >> 2) & 0xff;
361 r[9] = ((a->
n[7] & 0x3) << 6) | ((a->
n[6] >> 20) & 0x3f);
362 r[10] = (a->
n[6] >> 12) & 0xff;
363 r[11] = (a->
n[6] >> 4) & 0xff;
364 r[12] = ((a->
n[6] & 0xf) << 4) | ((a->
n[5] >> 22) & 0xf);
365 r[13] = (a->
n[5] >> 14) & 0xff;
366 r[14] = (a->
n[5] >> 6) & 0xff;
367 r[15] = ((a->
n[5] & 0x3f) << 2) | ((a->
n[4] >> 24) & 0x3);
368 r[16] = (a->
n[4] >> 16) & 0xff;
369 r[17] = (a->
n[4] >> 8) & 0xff;
370 r[18] = a->
n[4] & 0xff;
371 r[19] = (a->
n[3] >> 18) & 0xff;
372 r[20] = (a->
n[3] >> 10) & 0xff;
373 r[21] = (a->
n[3] >> 2) & 0xff;
374 r[22] = ((a->
n[3] & 0x3) << 6) | ((a->
n[2] >> 20) & 0x3f);
375 r[23] = (a->
n[2] >> 12) & 0xff;
376 r[24] = (a->
n[2] >> 4) & 0xff;
377 r[25] = ((a->
n[2] & 0xf) << 4) | ((a->
n[1] >> 22) & 0xf);
378 r[26] = (a->
n[1] >> 14) & 0xff;
379 r[27] = (a->
n[1] >> 6) & 0xff;
380 r[28] = ((a->
n[1] & 0x3f) << 2) | ((a->
n[0] >> 24) & 0x3);
381 r[29] = (a->
n[0] >> 16) & 0xff;
382 r[30] = (a->
n[0] >> 8) & 0xff;
383 r[31] = a->
n[0] & 0xff;
389 secp256k1_fe_verify(a);
391 r->
n[0] = 0x3FFFC2FUL * 2 * (m + 1) - a->
n[0];
392 r->
n[1] = 0x3FFFFBFUL * 2 * (m + 1) - a->
n[1];
393 r->
n[2] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[2];
394 r->
n[3] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[3];
395 r->
n[4] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[4];
396 r->
n[5] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[5];
397 r->
n[6] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[6];
398 r->
n[7] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[7];
399 r->
n[8] = 0x3FFFFFFUL * 2 * (m + 1) - a->
n[8];
400 r->
n[9] = 0x03FFFFFUL * 2 * (m + 1) - a->
n[9];
402 r->magnitude = m + 1;
404 secp256k1_fe_verify(r);
422 secp256k1_fe_verify(r);
428 secp256k1_fe_verify(a);
441 r->magnitude += a->magnitude;
443 secp256k1_fe_verify(r);
447 #if defined(USE_EXTERNAL_ASM) 450 void secp256k1_fe_mul_inner(uint32_t *r,
const uint32_t *a,
const uint32_t *
SECP256K1_RESTRICT b);
451 void secp256k1_fe_sqr_inner(uint32_t *r,
const uint32_t *a);
456 #define VERIFY_BITS(x, n) VERIFY_CHECK(((x) >> (n)) == 0) 458 #define VERIFY_BITS(x, n) do { } while(0) 463 uint64_t u0, u1, u2, u3, u4, u5, u6, u7,
u8;
464 uint32_t t9,
t1, t0,
t2,
t3,
t4, t5, t6, t7;
465 const uint32_t M = 0x3FFFFFF
UL,
R0 = 0x3D10
UL,
R1 = 0x400
UL;
493 d = (uint64_t)a[0] * b[9]
494 + (uint64_t)a[1] * b[8]
495 + (uint64_t)a[2] * b[7]
496 + (uint64_t)a[3] * b[6]
497 + (uint64_t)a[4] * b[5]
498 + (uint64_t)a[5] * b[4]
499 + (uint64_t)a[6] * b[3]
500 + (uint64_t)a[7] * b[2]
501 + (uint64_t)a[8] * b[1]
502 + (uint64_t)a[9] * b[0];
505 t9 = d & M; d >>= 26;
510 c = (uint64_t)a[0] * b[0];
513 d += (uint64_t)a[1] * b[9]
514 + (uint64_t)a[2] * b[8]
515 + (uint64_t)a[3] * b[7]
516 + (uint64_t)a[4] * b[6]
517 + (uint64_t)a[5] * b[5]
518 + (uint64_t)a[6] * b[4]
519 + (uint64_t)a[7] * b[3]
520 + (uint64_t)a[8] * b[2]
521 + (uint64_t)a[9] * b[1];
524 u0 = d & M; d >>= 26; c += u0 *
R0;
529 t0 = c & M; c >>= 26; c += u0 *
R1;
535 c += (uint64_t)a[0] * b[1]
536 + (uint64_t)a[1] * b[0];
539 d += (uint64_t)a[2] * b[9]
540 + (uint64_t)a[3] * b[8]
541 + (uint64_t)a[4] * b[7]
542 + (uint64_t)a[5] * b[6]
543 + (uint64_t)a[6] * b[5]
544 + (uint64_t)a[7] * b[4]
545 + (uint64_t)a[8] * b[3]
546 + (uint64_t)a[9] * b[2];
549 u1 = d & M; d >>= 26; c += u1 *
R0;
554 t1 = c & M; c >>= 26; c += u1 *
R1;
560 c += (uint64_t)a[0] * b[2]
561 + (uint64_t)a[1] * b[1]
562 + (uint64_t)a[2] * b[0];
565 d += (uint64_t)a[3] * b[9]
566 + (uint64_t)a[4] * b[8]
567 + (uint64_t)a[5] * b[7]
568 + (uint64_t)a[6] * b[6]
569 + (uint64_t)a[7] * b[5]
570 + (uint64_t)a[8] * b[4]
571 + (uint64_t)a[9] * b[3];
574 u2 = d & M; d >>= 26; c += u2 *
R0;
579 t2 = c & M; c >>= 26; c += u2 *
R1;
585 c += (uint64_t)a[0] * b[3]
586 + (uint64_t)a[1] * b[2]
587 + (uint64_t)a[2] * b[1]
588 + (uint64_t)a[3] * b[0];
591 d += (uint64_t)a[4] * b[9]
592 + (uint64_t)a[5] * b[8]
593 + (uint64_t)a[6] * b[7]
594 + (uint64_t)a[7] * b[6]
595 + (uint64_t)a[8] * b[5]
596 + (uint64_t)a[9] * b[4];
599 u3 = d & M; d >>= 26; c += u3 *
R0;
604 t3 = c & M; c >>= 26; c += u3 *
R1;
610 c += (uint64_t)a[0] * b[4]
611 + (uint64_t)a[1] * b[3]
612 + (uint64_t)a[2] * b[2]
613 + (uint64_t)a[3] * b[1]
614 + (uint64_t)a[4] * b[0];
617 d += (uint64_t)a[5] * b[9]
618 + (uint64_t)a[6] * b[8]
619 + (uint64_t)a[7] * b[7]
620 + (uint64_t)a[8] * b[6]
621 + (uint64_t)a[9] * b[5];
624 u4 = d & M; d >>= 26; c += u4 *
R0;
629 t4 = c & M; c >>= 26; c += u4 *
R1;
635 c += (uint64_t)a[0] * b[5]
636 + (uint64_t)a[1] * b[4]
637 + (uint64_t)a[2] * b[3]
638 + (uint64_t)a[3] * b[2]
639 + (uint64_t)a[4] * b[1]
640 + (uint64_t)a[5] * b[0];
643 d += (uint64_t)a[6] * b[9]
644 + (uint64_t)a[7] * b[8]
645 + (uint64_t)a[8] * b[7]
646 + (uint64_t)a[9] * b[6];
649 u5 = d & M; d >>= 26; c += u5 *
R0;
654 t5 = c & M; c >>= 26; c += u5 *
R1;
660 c += (uint64_t)a[0] * b[6]
661 + (uint64_t)a[1] * b[5]
662 + (uint64_t)a[2] * b[4]
663 + (uint64_t)a[3] * b[3]
664 + (uint64_t)a[4] * b[2]
665 + (uint64_t)a[5] * b[1]
666 + (uint64_t)a[6] * b[0];
669 d += (uint64_t)a[7] * b[9]
670 + (uint64_t)a[8] * b[8]
671 + (uint64_t)a[9] * b[7];
674 u6 = d & M; d >>= 26; c += u6 *
R0;
679 t6 = c & M; c >>= 26; c += u6 *
R1;
685 c += (uint64_t)a[0] * b[7]
686 + (uint64_t)a[1] * b[6]
687 + (uint64_t)a[2] * b[5]
688 + (uint64_t)a[3] * b[4]
689 + (uint64_t)a[4] * b[3]
690 + (uint64_t)a[5] * b[2]
691 + (uint64_t)a[6] * b[1]
692 + (uint64_t)a[7] * b[0];
696 d += (uint64_t)a[8] * b[9]
697 + (uint64_t)a[9] * b[8];
700 u7 = d & M; d >>= 26; c += u7 *
R0;
706 t7 = c & M; c >>= 26; c += u7 *
R1;
712 c += (uint64_t)a[0] * b[8]
713 + (uint64_t)a[1] * b[7]
714 + (uint64_t)a[2] * b[6]
715 + (uint64_t)a[3] * b[5]
716 + (uint64_t)a[4] * b[4]
717 + (uint64_t)a[5] * b[3]
718 + (uint64_t)a[6] * b[2]
719 + (uint64_t)a[7] * b[1]
720 + (uint64_t)a[8] * b[0];
724 d += (uint64_t)a[9] * b[9];
727 u8 = d & M; d >>= 26; c += u8 *
R0;
750 r[8] = c & M; c >>= 26; c += u8 *
R1;
758 r[9] = c & (M >> 4); c >>= 22; c += d * (R1 << 4);
765 d = c * (R0 >> 4) + t0;
768 r[0] = d & M; d >>= 26;
772 d += c * (R1 >> 4) + t1;
777 r[1] = d & M; d >>= 26;
790 SECP256K1_INLINE static void secp256k1_fe_sqr_inner(uint32_t *r,
const uint32_t *a) {
792 uint64_t u0, u1, u2, u3, u4, u5, u6, u7,
u8;
793 uint32_t t9, t0,
t1,
t2,
t3,
t4, t5, t6, t7;
794 const uint32_t M = 0x3FFFFFF
UL,
R0 = 0x3D10
UL,
R1 = 0x400
UL;
812 d = (uint64_t)(a[0]*2) * a[9]
813 + (uint64_t)(a[1]*2) * a[8]
814 + (uint64_t)(a[2]*2) * a[7]
815 + (uint64_t)(a[3]*2) * a[6]
816 + (uint64_t)(a[4]*2) * a[5];
819 t9 = d & M; d >>= 26;
824 c = (uint64_t)a[0] * a[0];
827 d += (uint64_t)(a[1]*2) * a[9]
828 + (uint64_t)(a[2]*2) * a[8]
829 + (uint64_t)(a[3]*2) * a[7]
830 + (uint64_t)(a[4]*2) * a[6]
831 + (uint64_t)a[5] * a[5];
834 u0 = d & M; d >>= 26; c += u0 *
R0;
839 t0 = c & M; c >>= 26; c += u0 *
R1;
845 c += (uint64_t)(a[0]*2) * a[1];
848 d += (uint64_t)(a[2]*2) * a[9]
849 + (uint64_t)(a[3]*2) * a[8]
850 + (uint64_t)(a[4]*2) * a[7]
851 + (uint64_t)(a[5]*2) * a[6];
854 u1 = d & M; d >>= 26; c += u1 *
R0;
859 t1 = c & M; c >>= 26; c += u1 *
R1;
865 c += (uint64_t)(a[0]*2) * a[2]
866 + (uint64_t)a[1] * a[1];
869 d += (uint64_t)(a[3]*2) * a[9]
870 + (uint64_t)(a[4]*2) * a[8]
871 + (uint64_t)(a[5]*2) * a[7]
872 + (uint64_t)a[6] * a[6];
875 u2 = d & M; d >>= 26; c += u2 *
R0;
880 t2 = c & M; c >>= 26; c += u2 *
R1;
886 c += (uint64_t)(a[0]*2) * a[3]
887 + (uint64_t)(a[1]*2) * a[2];
890 d += (uint64_t)(a[4]*2) * a[9]
891 + (uint64_t)(a[5]*2) * a[8]
892 + (uint64_t)(a[6]*2) * a[7];
895 u3 = d & M; d >>= 26; c += u3 *
R0;
900 t3 = c & M; c >>= 26; c += u3 *
R1;
906 c += (uint64_t)(a[0]*2) * a[4]
907 + (uint64_t)(a[1]*2) * a[3]
908 + (uint64_t)a[2] * a[2];
911 d += (uint64_t)(a[5]*2) * a[9]
912 + (uint64_t)(a[6]*2) * a[8]
913 + (uint64_t)a[7] * a[7];
916 u4 = d & M; d >>= 26; c += u4 *
R0;
921 t4 = c & M; c >>= 26; c += u4 *
R1;
927 c += (uint64_t)(a[0]*2) * a[5]
928 + (uint64_t)(a[1]*2) * a[4]
929 + (uint64_t)(a[2]*2) * a[3];
932 d += (uint64_t)(a[6]*2) * a[9]
933 + (uint64_t)(a[7]*2) * a[8];
936 u5 = d & M; d >>= 26; c += u5 *
R0;
941 t5 = c & M; c >>= 26; c += u5 *
R1;
947 c += (uint64_t)(a[0]*2) * a[6]
948 + (uint64_t)(a[1]*2) * a[5]
949 + (uint64_t)(a[2]*2) * a[4]
950 + (uint64_t)a[3] * a[3];
953 d += (uint64_t)(a[7]*2) * a[9]
954 + (uint64_t)a[8] * a[8];
957 u6 = d & M; d >>= 26; c += u6 *
R0;
962 t6 = c & M; c >>= 26; c += u6 *
R1;
968 c += (uint64_t)(a[0]*2) * a[7]
969 + (uint64_t)(a[1]*2) * a[6]
970 + (uint64_t)(a[2]*2) * a[5]
971 + (uint64_t)(a[3]*2) * a[4];
975 d += (uint64_t)(a[8]*2) * a[9];
978 u7 = d & M; d >>= 26; c += u7 *
R0;
984 t7 = c & M; c >>= 26; c += u7 *
R1;
990 c += (uint64_t)(a[0]*2) * a[8]
991 + (uint64_t)(a[1]*2) * a[7]
992 + (uint64_t)(a[2]*2) * a[6]
993 + (uint64_t)(a[3]*2) * a[5]
994 + (uint64_t)a[4] * a[4];
998 d += (uint64_t)a[9] * a[9];
1001 u8 = d & M; d >>= 26; c += u8 *
R0;
1024 r[8] = c & M; c >>= 26; c += u8 *
R1;
1032 r[9] = c & (M >> 4); c >>= 22; c += d * (R1 << 4);
1039 d = c * (R0 >> 4) + t0;
1042 r[0] = d & M; d >>= 26;
1046 d += c * (R1 >> 4) + t1;
1051 r[1] = d & M; d >>= 26;
1069 secp256k1_fe_verify(a);
1070 secp256k1_fe_verify(b);
1073 secp256k1_fe_mul_inner(r->
n, a->
n, b->n);
1077 secp256k1_fe_verify(r);
1084 secp256k1_fe_verify(a);
1086 secp256k1_fe_sqr_inner(r->
n, a->
n);
1090 secp256k1_fe_verify(r);
1095 uint32_t mask0, mask1;
1096 mask0 = flag + ~((uint32_t)0);
1098 r->
n[0] = (r->
n[0] & mask0) | (a->
n[0] & mask1);
1099 r->
n[1] = (r->
n[1] & mask0) | (a->
n[1] & mask1);
1100 r->
n[2] = (r->
n[2] & mask0) | (a->
n[2] & mask1);
1101 r->
n[3] = (r->
n[3] & mask0) | (a->
n[3] & mask1);
1102 r->
n[4] = (r->
n[4] & mask0) | (a->
n[4] & mask1);
1103 r->
n[5] = (r->
n[5] & mask0) | (a->
n[5] & mask1);
1104 r->
n[6] = (r->
n[6] & mask0) | (a->
n[6] & mask1);
1105 r->
n[7] = (r->
n[7] & mask0) | (a->
n[7] & mask1);
1106 r->
n[8] = (r->
n[8] & mask0) | (a->
n[8] & mask1);
1107 r->
n[9] = (r->
n[9] & mask0) | (a->
n[9] & mask1);
1109 if (a->magnitude > r->magnitude) {
1110 r->magnitude = a->magnitude;
1112 r->normalized &= a->normalized;
1117 uint32_t mask0, mask1;
1118 mask0 = flag + ~((uint32_t)0);
1120 r->
n[0] = (r->
n[0] & mask0) | (a->
n[0] & mask1);
1121 r->
n[1] = (r->
n[1] & mask0) | (a->
n[1] & mask1);
1122 r->
n[2] = (r->
n[2] & mask0) | (a->
n[2] & mask1);
1123 r->
n[3] = (r->
n[3] & mask0) | (a->
n[3] & mask1);
1124 r->
n[4] = (r->
n[4] & mask0) | (a->
n[4] & mask1);
1125 r->
n[5] = (r->
n[5] & mask0) | (a->
n[5] & mask1);
1126 r->
n[6] = (r->
n[6] & mask0) | (a->
n[6] & mask1);
1127 r->
n[7] = (r->
n[7] & mask0) | (a->
n[7] & mask1);
1134 r->
n[0] = a->
n[0] | a->
n[1] << 26;
1135 r->
n[1] = a->
n[1] >> 6 | a->
n[2] << 20;
1136 r->
n[2] = a->
n[2] >> 12 | a->
n[3] << 14;
1137 r->
n[3] = a->
n[3] >> 18 | a->
n[4] << 8;
1138 r->
n[4] = a->
n[4] >> 24 | a->
n[5] << 2 | a->
n[6] << 28;
1139 r->
n[5] = a->
n[6] >> 4 | a->
n[7] << 22;
1140 r->
n[6] = a->
n[7] >> 10 | a->
n[8] << 16;
1141 r->
n[7] = a->
n[8] >> 16 | a->
n[9] << 10;
1145 r->
n[0] = a->
n[0] & 0x3FFFFFF
UL;
1146 r->
n[1] = a->
n[0] >> 26 | ((a->
n[1] << 6) & 0x3FFFFFFUL);
1147 r->
n[2] = a->
n[1] >> 20 | ((a->
n[2] << 12) & 0x3FFFFFFUL);
1148 r->
n[3] = a->
n[2] >> 14 | ((a->
n[3] << 18) & 0x3FFFFFFUL);
1149 r->
n[4] = a->
n[3] >> 8 | ((a->
n[4] << 24) & 0x3FFFFFFUL);
1150 r->
n[5] = (a->
n[4] >> 2) & 0x3FFFFFFUL;
1151 r->
n[6] = a->
n[4] >> 28 | ((a->
n[5] << 4) & 0x3FFFFFFUL);
1152 r->
n[7] = a->
n[5] >> 22 | ((a->
n[6] << 10) & 0x3FFFFFFUL);
1153 r->
n[8] = a->
n[6] >> 16 | ((a->
n[7] << 16) & 0x3FFFFFFUL);
1154 r->
n[9] = a->
n[7] >> 10;
#define VERIFY_CHECK(cond)
#define SECP256K1_RESTRICT
#define VERIFY_BITS(x, n)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 8, 8, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u8