7#ifndef SECP256K1_FIELD_REPR_IMPL_H
8#define SECP256K1_FIELD_REPR_IMPL_H
10#if defined HAVE_CONFIG_H
18#if defined(USE_ASM_X86_64)
42 int m =
a->normalized ? 1 : 2 *
a->magnitude,
r = 1;
44 r &= (
d[0] <= 0xFFFFFFFFFFFFFULL * m);
45 r &= (
d[1] <= 0xFFFFFFFFFFFFFULL * m);
46 r &= (
d[2] <= 0xFFFFFFFFFFFFFULL * m);
47 r &= (
d[3] <= 0xFFFFFFFFFFFFFULL * m);
48 r &= (
d[4] <= 0x0FFFFFFFFFFFFULL * m);
49 r &= (
a->magnitude >= 0);
50 r &= (
a->magnitude <= 2048);
52 r &= (
a->magnitude <= 1);
53 if (
r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
54 r &= (
d[0] < 0xFFFFEFFFFFC2FULL);
64 r->n[0] = 0xFFFFFFFFFFFFFULL * 2 * m;
65 r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * m;
66 r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * m;
67 r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * m;
68 r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * m;
71 r->normalized = (m == 0);
72 secp256k1_fe_verify(
r);
77 uint64_t t0 =
r->n[0], t1 =
r->n[1], t2 =
r->n[2], t3 =
r->n[3], t4 =
r->n[4];
81 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
84 t0 += x * 0x1000003D1ULL;
85 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
86 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
87 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
88 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
94 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
95 & (t0 >= 0xFFFFEFFFFFC2FULL));
98 t0 += x * 0x1000003D1ULL;
99 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
100 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
101 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
102 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
108 t4 &= 0x0FFFFFFFFFFFFULL;
110 r->n[0] = t0;
r->n[1] = t1;
r->n[2] = t2;
r->n[3] = t3;
r->n[4] = t4;
115 secp256k1_fe_verify(
r);
120 uint64_t t0 =
r->n[0], t1 =
r->n[1], t2 =
r->n[2], t3 =
r->n[3], t4 =
r->n[4];
123 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
126 t0 += x * 0x1000003D1ULL;
127 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
128 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
129 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
130 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
135 r->n[0] = t0;
r->n[1] = t1;
r->n[2] = t2;
r->n[3] = t3;
r->n[4] = t4;
139 secp256k1_fe_verify(
r);
144 uint64_t t0 =
r->n[0], t1 =
r->n[1], t2 =
r->n[2], t3 =
r->n[3], t4 =
r->n[4];
148 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
151 t0 += x * 0x1000003D1ULL;
152 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
153 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
154 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
155 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
161 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
162 & (t0 >= 0xFFFFEFFFFFC2FULL));
165 t0 += 0x1000003D1ULL;
166 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
167 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
168 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
169 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
175 t4 &= 0x0FFFFFFFFFFFFULL;
178 r->n[0] = t0;
r->n[1] = t1;
r->n[2] = t2;
r->n[3] = t3;
r->n[4] = t4;
183 secp256k1_fe_verify(
r);
187static int secp256k1_fe_normalizes_to_zero(
const secp256k1_fe *
r) {
188 uint64_t t0 =
r->n[0], t1 =
r->n[1], t2 =
r->n[2], t3 =
r->n[3], t4 =
r->n[4];
194 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
197 t0 += x * 0x1000003D1ULL;
198 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
199 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
200 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
201 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
202 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
207 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
210static int secp256k1_fe_normalizes_to_zero_var(
const secp256k1_fe *
r) {
222 t0 += x * 0x1000003D1ULL;
225 z0 = t0 & 0xFFFFFFFFFFFFFULL;
226 z1 = z0 ^ 0x1000003D0ULL;
229 if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
237 t4 &= 0x0FFFFFFFFFFFFULL;
240 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
241 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
242 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
243 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
248 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
254 r->n[1] =
r->n[2] =
r->n[3] =
r->n[4] = 0;
256 r->magnitude = (
a != 0);
258 secp256k1_fe_verify(
r);
266 secp256k1_fe_verify(
a);
268 return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
274 secp256k1_fe_verify(
a);
285 for (i=0; i<5; i++) {
295 secp256k1_fe_verify(
a);
296 secp256k1_fe_verify(b);
298 for (i = 4; i >= 0; i--) {
299 if (
a->n[i] > b->
n[i]) {
302 if (
a->n[i] < b->
n[i]) {
309static int secp256k1_fe_set_b32(
secp256k1_fe *
r,
const unsigned char *
a) {
345 ret = !((
r->n[4] == 0x0FFFFFFFFFFFFULL) & ((
r->n[3] &
r->n[2] &
r->n[1]) == 0xFFFFFFFFFFFFFULL) & (
r->n[0] >= 0xFFFFEFFFFFC2FULL));
350 secp256k1_fe_verify(
r);
359static void secp256k1_fe_get_b32(
unsigned char *
r,
const secp256k1_fe *
a) {
362 secp256k1_fe_verify(
a);
364 r[0] = (
a->n[4] >> 40) & 0xFF;
365 r[1] = (
a->n[4] >> 32) & 0xFF;
366 r[2] = (
a->n[4] >> 24) & 0xFF;
367 r[3] = (
a->n[4] >> 16) & 0xFF;
368 r[4] = (
a->n[4] >> 8) & 0xFF;
369 r[5] =
a->n[4] & 0xFF;
370 r[6] = (
a->n[3] >> 44) & 0xFF;
371 r[7] = (
a->n[3] >> 36) & 0xFF;
372 r[8] = (
a->n[3] >> 28) & 0xFF;
373 r[9] = (
a->n[3] >> 20) & 0xFF;
374 r[10] = (
a->n[3] >> 12) & 0xFF;
375 r[11] = (
a->n[3] >> 4) & 0xFF;
376 r[12] = ((
a->n[2] >> 48) & 0xF) | ((
a->n[3] & 0xF) << 4);
377 r[13] = (
a->n[2] >> 40) & 0xFF;
378 r[14] = (
a->n[2] >> 32) & 0xFF;
379 r[15] = (
a->n[2] >> 24) & 0xFF;
380 r[16] = (
a->n[2] >> 16) & 0xFF;
381 r[17] = (
a->n[2] >> 8) & 0xFF;
382 r[18] =
a->n[2] & 0xFF;
383 r[19] = (
a->n[1] >> 44) & 0xFF;
384 r[20] = (
a->n[1] >> 36) & 0xFF;
385 r[21] = (
a->n[1] >> 28) & 0xFF;
386 r[22] = (
a->n[1] >> 20) & 0xFF;
387 r[23] = (
a->n[1] >> 12) & 0xFF;
388 r[24] = (
a->n[1] >> 4) & 0xFF;
389 r[25] = ((
a->n[0] >> 48) & 0xF) | ((
a->n[1] & 0xF) << 4);
390 r[26] = (
a->n[0] >> 40) & 0xFF;
391 r[27] = (
a->n[0] >> 32) & 0xFF;
392 r[28] = (
a->n[0] >> 24) & 0xFF;
393 r[29] = (
a->n[0] >> 16) & 0xFF;
394 r[30] = (
a->n[0] >> 8) & 0xFF;
395 r[31] =
a->n[0] & 0xFF;
401 secp256k1_fe_verify(
a);
402 VERIFY_CHECK(0xFFFFEFFFFFC2FULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
403 VERIFY_CHECK(0xFFFFFFFFFFFFFULL * 2 * (m + 1) >= 0xFFFFFFFFFFFFFULL * 2 * m);
404 VERIFY_CHECK(0x0FFFFFFFFFFFFULL * 2 * (m + 1) >= 0x0FFFFFFFFFFFFULL * 2 * m);
406 r->n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) -
a->n[0];
407 r->n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[1];
408 r->n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[2];
409 r->n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[3];
410 r->n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[4];
412 r->magnitude = m + 1;
414 secp256k1_fe_verify(
r);
427 secp256k1_fe_verify(
r);
433 secp256k1_fe_verify(
a);
441 r->magnitude +=
a->magnitude;
443 secp256k1_fe_verify(
r);
451 secp256k1_fe_verify(
a);
452 secp256k1_fe_verify(b);
456 secp256k1_fe_mul_inner(
r->n,
a->n, b->n);
460 secp256k1_fe_verify(
r);
467 secp256k1_fe_verify(
a);
469 secp256k1_fe_sqr_inner(
r->n,
a->n);
473 secp256k1_fe_verify(
r);
482 r->n[0] = (
r->n[0] & mask0) | (
a->n[0] & mask1);
483 r->n[1] = (
r->n[1] & mask0) | (
a->n[1] & mask1);
484 r->n[2] = (
r->n[2] & mask0) | (
a->n[2] & mask1);
485 r->n[3] = (
r->n[3] & mask0) | (
a->n[3] & mask1);
486 r->n[4] = (
r->n[4] & mask0) | (
a->n[4] & mask1);
489 r->magnitude =
a->magnitude;
490 r->normalized =
a->normalized;
496 uint64_t t0 =
r->n[0], t1 =
r->n[1], t2 =
r->n[2], t3 =
r->n[3], t4 =
r->n[4];
501 secp256k1_fe_verify(
r);
515 t0 += 0xFFFFEFFFFFC2FULL & mask;
530 r->n[0] = (t0 >> 1) + ((t1 &
one) << 51);
531 r->n[1] = (t1 >> 1) + ((t2 &
one) << 51);
532 r->n[2] = (t2 >> 1) + ((t3 &
one) << 51);
533 r->n[3] = (t3 >> 1) + ((t4 &
one) << 51);
554 r->magnitude = (
r->magnitude >> 1) + 1;
556 secp256k1_fe_verify(
r);
565 r->n[0] = (
r->n[0] & mask0) | (
a->n[0] & mask1);
566 r->n[1] = (
r->n[1] & mask0) | (
a->n[1] & mask1);
567 r->n[2] = (
r->n[2] & mask0) | (
a->n[2] & mask1);
568 r->n[3] = (
r->n[3] & mask0) | (
a->n[3] & mask1);
575 r->n[0] =
a->n[0] |
a->n[1] << 52;
576 r->n[1] =
a->n[1] >> 12 |
a->n[2] << 40;
577 r->n[2] =
a->n[2] >> 24 |
a->n[3] << 28;
578 r->n[3] =
a->n[3] >> 36 |
a->n[4] << 16;
582 r->n[0] =
a->n[0] & 0xFFFFFFFFFFFFFULL;
583 r->n[1] =
a->n[0] >> 52 | ((
a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
584 r->n[2] =
a->n[1] >> 40 | ((
a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
585 r->n[3] =
a->n[2] >> 28 | ((
a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
586 r->n[4] =
a->n[3] >> 16;
590 secp256k1_fe_verify(
r);
596 const uint64_t a0 =
a->v[0], a1 =
a->v[1], a2 =
a->v[2], a3 =
a->v[3], a4 =
a->v[4];
608 r->n[1] = (a0 >> 52 | a1 << 10) & M52;
609 r->n[2] = (a1 >> 42 | a2 << 20) & M52;
610 r->n[3] = (a2 >> 32 | a3 << 30) & M52;
611 r->n[4] = (a3 >> 22 | a4 << 40);
616 secp256k1_fe_verify(
r);
622 const uint64_t a0 =
a->n[0], a1 =
a->n[1], a2 =
a->n[2], a3 =
a->n[3], a4 =
a->n[4];
628 r->v[0] = (a0 | a1 << 52) & M62;
629 r->v[1] = (a1 >> 10 | a2 << 42) & M62;
630 r->v[2] = (a2 >> 20 | a3 << 32) & M62;
631 r->v[3] = (a3 >> 30 | a4 << 22) & M62;
636 {{-0x1000003D1LL, 0, 0, 0, 256}},
645 secp256k1_fe_normalize(&tmp);
646 secp256k1_fe_to_signed62(&
s, &tmp);
647 secp256k1_modinv64(&
s, &secp256k1_const_modinfo_fe);
648 secp256k1_fe_from_signed62(
r, &
s);
651 VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(
r) == secp256k1_fe_normalizes_to_zero(&tmp));
660 secp256k1_fe_normalize_var(&tmp);
661 secp256k1_fe_to_signed62(&
s, &tmp);
662 secp256k1_modinv64_var(&
s, &secp256k1_const_modinfo_fe);
663 secp256k1_fe_from_signed62(
r, &
s);
666 VERIFY_CHECK(secp256k1_fe_normalizes_to_zero(
r) == secp256k1_fe_normalizes_to_zero(&tmp));
#define VG_CHECK_VERIFY(x, y)
#define VERIFY_CHECK(cond)
#define SECP256K1_RESTRICT
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
unsigned __int64 uint64_t