7#ifndef SECP256K1_FIELD_REPR_IMPL_H
8#define SECP256K1_FIELD_REPR_IMPL_H
10#if defined HAVE_CONFIG_H
11#include "libsecp256k1-config.h"
18#if defined(USE_ASM_X86_64)
35 int m =
a->normalized ? 1 : 2 *
a->magnitude, r = 1;
37 r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
38 r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
39 r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
40 r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
41 r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
42 r &= (
a->magnitude >= 0);
43 r &= (
a->magnitude <= 2048);
45 r &= (
a->magnitude <= 1);
46 if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
47 r &= (d[0] < 0xFFFFEFFFFFC2FULL);
62 t0 += x * 0x1000003D1ULL;
63 t1 += (
t0 >> 52);
t0 &= 0xFFFFFFFFFFFFFULL;
64 t2 += (
t1 >> 52);
t1 &= 0xFFFFFFFFFFFFFULL; m =
t1;
65 t3 += (
t2 >> 52);
t2 &= 0xFFFFFFFFFFFFFULL; m &=
t2;
66 t4 += (
t3 >> 52);
t3 &= 0xFFFFFFFFFFFFFULL; m &=
t3;
72 x = (
t4 >> 48) | ((
t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
73 & (
t0 >= 0xFFFFEFFFFFC2FULL));
76 t0 += x * 0x1000003D1ULL;
77 t1 += (
t0 >> 52);
t0 &= 0xFFFFFFFFFFFFFULL;
78 t2 += (
t1 >> 52);
t1 &= 0xFFFFFFFFFFFFFULL;
79 t3 += (
t2 >> 52);
t2 &= 0xFFFFFFFFFFFFFULL;
80 t4 += (
t3 >> 52);
t3 &= 0xFFFFFFFFFFFFFULL;
86 t4 &= 0x0FFFFFFFFFFFFULL;
104 t0 += x * 0x1000003D1ULL;
105 t1 += (
t0 >> 52);
t0 &= 0xFFFFFFFFFFFFFULL;
106 t2 += (
t1 >> 52);
t1 &= 0xFFFFFFFFFFFFFULL;
107 t3 += (
t2 >> 52);
t2 &= 0xFFFFFFFFFFFFFULL;
108 t4 += (
t3 >> 52);
t3 &= 0xFFFFFFFFFFFFFULL;
129 t0 += x * 0x1000003D1ULL;
130 t1 += (
t0 >> 52);
t0 &= 0xFFFFFFFFFFFFFULL;
131 t2 += (
t1 >> 52);
t1 &= 0xFFFFFFFFFFFFFULL; m =
t1;
132 t3 += (
t2 >> 52);
t2 &= 0xFFFFFFFFFFFFFULL; m &=
t2;
133 t4 += (
t3 >> 52);
t3 &= 0xFFFFFFFFFFFFFULL; m &=
t3;
139 x = (
t4 >> 48) | ((
t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
140 & (
t0 >= 0xFFFFEFFFFFC2FULL));
143 t0 += 0x1000003D1ULL;
144 t1 += (
t0 >> 52);
t0 &= 0xFFFFFFFFFFFFFULL;
145 t2 += (
t1 >> 52);
t1 &= 0xFFFFFFFFFFFFFULL;
146 t3 += (
t2 >> 52);
t2 &= 0xFFFFFFFFFFFFFULL;
147 t4 += (
t3 >> 52);
t3 &= 0xFFFFFFFFFFFFFULL;
153 t4 &= 0x0FFFFFFFFFFFFULL;
175 t0 += x * 0x1000003D1ULL;
176 t1 += (
t0 >> 52);
t0 &= 0xFFFFFFFFFFFFFULL;
z0 =
t0;
z1 =
t0 ^ 0x1000003D0ULL;
180 z0 |=
t4;
z1 &=
t4 ^ 0xF000000000000ULL;
185 return (
z0 == 0) | (
z1 == 0xFFFFFFFFFFFFFULL);
200 t0 += x * 0x1000003D1ULL;
203 z0 =
t0 & 0xFFFFFFFFFFFFFULL;
204 z1 =
z0 ^ 0x1000003D0ULL;
207 if ((
z0 != 0
ULL) & (
z1 != 0xFFFFFFFFFFFFFULL)) {
215 t4 &= 0x0FFFFFFFFFFFFULL;
221 z0 |=
t4;
z1 &=
t4 ^ 0xF000000000000ULL;
226 return (
z0 == 0) | (
z1 == 0xFFFFFFFFFFFFFULL);
231 r->
n[1] = r->
n[2] = r->
n[3] = r->
n[4] = 0;
245 return (
t[0] |
t[1] |
t[2] |
t[3] |
t[4]) == 0;
262 for (i=0; i<5; i++) {
275 for (i = 4; i >= 0; i--) {
276 if (
a->n[i] >
b->n[i]) {
279 if (
a->n[i] <
b->n[i]) {
322 ret = !((r->
n[4] == 0x0FFFFFFFFFFFFULL) & ((r->
n[3] & r->
n[2] & r->
n[1]) == 0xFFFFFFFFFFFFFULL) & (r->
n[0] >= 0xFFFFEFFFFFC2FULL));
341 r[0] = (
a->n[4] >> 40) & 0xFF;
342 r[1] = (
a->n[4] >> 32) & 0xFF;
343 r[2] = (
a->n[4] >> 24) & 0xFF;
344 r[3] = (
a->n[4] >> 16) & 0xFF;
345 r[4] = (
a->n[4] >> 8) & 0xFF;
346 r[5] =
a->n[4] & 0xFF;
347 r[6] = (
a->n[3] >> 44) & 0xFF;
348 r[7] = (
a->n[3] >> 36) & 0xFF;
349 r[8] = (
a->n[3] >> 28) & 0xFF;
350 r[9] = (
a->n[3] >> 20) & 0xFF;
351 r[10] = (
a->n[3] >> 12) & 0xFF;
352 r[11] = (
a->n[3] >> 4) & 0xFF;
353 r[12] = ((
a->n[2] >> 48) & 0xF) | ((
a->n[3] & 0xF) << 4);
354 r[13] = (
a->n[2] >> 40) & 0xFF;
355 r[14] = (
a->n[2] >> 32) & 0xFF;
356 r[15] = (
a->n[2] >> 24) & 0xFF;
357 r[16] = (
a->n[2] >> 16) & 0xFF;
358 r[17] = (
a->n[2] >> 8) & 0xFF;
359 r[18] =
a->n[2] & 0xFF;
360 r[19] = (
a->n[1] >> 44) & 0xFF;
361 r[20] = (
a->n[1] >> 36) & 0xFF;
362 r[21] = (
a->n[1] >> 28) & 0xFF;
363 r[22] = (
a->n[1] >> 20) & 0xFF;
364 r[23] = (
a->n[1] >> 12) & 0xFF;
365 r[24] = (
a->n[1] >> 4) & 0xFF;
366 r[25] = ((
a->n[0] >> 48) & 0xF) | ((
a->n[1] & 0xF) << 4);
367 r[26] = (
a->n[0] >> 40) & 0xFF;
368 r[27] = (
a->n[0] >> 32) & 0xFF;
369 r[28] = (
a->n[0] >> 24) & 0xFF;
370 r[29] = (
a->n[0] >> 16) & 0xFF;
371 r[30] = (
a->n[0] >> 8) & 0xFF;
372 r[31] =
a->n[0] & 0xFF;
380 r->
n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) -
a->n[0];
381 r->
n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[1];
382 r->
n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[2];
383 r->
n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[3];
384 r->
n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) -
a->n[4];
386 r->magnitude = m + 1;
415 r->magnitude +=
a->magnitude;
463 r->magnitude =
a->magnitude;
464 r->normalized =
a->normalized;
484 r->
n[0] =
a->n[0] |
a->n[1] << 52;
485 r->
n[1] =
a->n[1] >> 12 |
a->n[2] << 40;
486 r->
n[2] =
a->n[2] >> 24 |
a->n[3] << 28;
487 r->
n[3] =
a->n[3] >> 36 |
a->n[4] << 16;
491 r->
n[0] =
a->n[0] & 0xFFFFFFFFFFFFFULL;
492 r->
n[1] =
a->n[0] >> 52 | ((
a->n[1] << 12) & 0xFFFFFFFFFFFFFULL);
493 r->
n[2] =
a->n[1] >> 40 | ((
a->n[2] << 24) & 0xFFFFFFFFFFFFFULL);
494 r->
n[3] =
a->n[2] >> 28 | ((
a->n[3] << 36) & 0xFFFFFFFFFFFFFULL);
495 r->
n[4] =
a->n[3] >> 16;
516 r->
n[1] = (
a0 >> 52 |
a1 << 10) &
M52;
517 r->
n[2] = (
a1 >> 42 |
a2 << 20) &
M52;
518 r->
n[3] = (
a2 >> 32 |
a3 << 30) &
M52;
519 r->
n[4] = (
a3 >> 22 |
a4 << 40);
537 r->
v[1] = (
a1 >> 10 |
a2 << 42) &
M62;
538 r->
v[2] = (
a2 >> 20 |
a3 << 32) &
M62;
539 r->
v[3] = (
a3 >> 30 |
a4 << 22) &
M62;
544 {{-0x1000003D1LL, 0, 0, 0, 256}},
static SECP256K1_INLINE void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *a)
static SECP256K1_INLINE void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t *SECP256K1_RESTRICT b)
static SECP256K1_INLINE void secp256k1_fe_set_int(secp256k1_fe *r, int a)
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
static SECP256K1_INLINE int secp256k1_fe_is_zero(const secp256k1_fe *a)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_mul_int(secp256k1_fe *r, int a)
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static SECP256K1_INLINE void secp256k1_fe_storage_cmov(secp256k1_fe_storage *r, const secp256k1_fe_storage *a, int flag)
static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe *r)
static void secp256k1_fe_sqr(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
static SECP256K1_INLINE void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
static void secp256k1_fe_to_signed62(secp256k1_modinv64_signed62 *r, const secp256k1_fe *a)
static void secp256k1_fe_normalize(secp256k1_fe *r)
Implements arithmetic modulo FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE FFFFFC2F,...
static SECP256K1_INLINE int secp256k1_fe_is_odd(const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_clear(secp256k1_fe *a)
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
static void secp256k1_fe_get_b32(unsigned char *r, const secp256k1_fe *a)
Convert a field element to a 32-byte big endian value.
static SECP256K1_INLINE void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static int secp256k1_fe_normalizes_to_zero(secp256k1_fe *r)
static void secp256k1_fe_from_signed62(secp256k1_fe *r, const secp256k1_modinv64_signed62 *a)
static const secp256k1_modinv64_modinfo secp256k1_const_modinfo_fe
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b)
static void secp256k1_fe_inv(secp256k1_fe *r, const secp256k1_fe *x)
static void secp256k1_fe_inv_var(secp256k1_fe *r, const secp256k1_fe *x)
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)
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
#define VG_CHECK_VERIFY(x, y)
#define VERIFY_CHECK(cond)
#define SECP256K1_RESTRICT