7#ifndef SECP256K1_FIELD_REPR_IMPL_H
8#define SECP256K1_FIELD_REPR_IMPL_H
17 int m =
a->normalized ? 1 : 2 *
a->magnitude, r = 1;
18 r &= (d[0] <= 0x3FFFFFFUL * m);
19 r &= (d[1] <= 0x3FFFFFFUL * m);
20 r &= (d[2] <= 0x3FFFFFFUL * m);
21 r &= (d[3] <= 0x3FFFFFFUL * m);
22 r &= (d[4] <= 0x3FFFFFFUL * m);
23 r &= (d[5] <= 0x3FFFFFFUL * m);
24 r &= (d[6] <= 0x3FFFFFFUL * m);
25 r &= (d[7] <= 0x3FFFFFFUL * m);
26 r &= (d[8] <= 0x3FFFFFFUL * m);
27 r &= (d[9] <= 0x03FFFFFUL * 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] + 0x40UL + ((d[0] + 0x3D1UL) >> 26)) <= 0x3FFFFFFUL);
52 t0 += x * 0x3D1UL;
t1 += (x << 6);
53 t1 += (
t0 >> 26);
t0 &= 0x3FFFFFFUL;
54 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFFUL;
55 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFFUL; m =
t2;
56 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFFUL; m &=
t3;
57 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFFUL; m &=
t4;
58 t6 += (
t5 >> 26);
t5 &= 0x3FFFFFFUL; m &=
t5;
59 t7 += (
t6 >> 26);
t6 &= 0x3FFFFFFUL; m &=
t6;
60 t8 += (
t7 >> 26);
t7 &= 0x3FFFFFFUL; m &=
t7;
61 t9 += (
t8 >> 26);
t8 &= 0x3FFFFFFUL; m &=
t8;
67 x = (
t9 >> 22) | ((
t9 == 0x03FFFFFUL) & (m == 0x3FFFFFFUL)
68 & ((
t1 + 0x40UL + ((
t0 + 0x3D1UL) >> 26)) > 0x3FFFFFFUL));
71 t0 += x * 0x3D1UL;
t1 += (x << 6);
72 t1 += (
t0 >> 26);
t0 &= 0x3FFFFFFUL;
73 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFFUL;
74 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFFUL;
75 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFFUL;
76 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFFUL;
77 t6 += (
t5 >> 26);
t5 &= 0x3FFFFFFUL;
78 t7 += (
t6 >> 26);
t6 &= 0x3FFFFFFUL;
79 t8 += (
t7 >> 26);
t7 &= 0x3FFFFFFUL;
80 t9 += (
t8 >> 26);
t8 &= 0x3FFFFFFUL;
106 t0 += x * 0x3D1UL;
t1 += (x << 6);
107 t1 += (
t0 >> 26);
t0 &= 0x3FFFFFFUL;
108 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFFUL;
109 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFFUL;
110 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFFUL;
111 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFFUL;
112 t6 += (
t5 >> 26);
t5 &= 0x3FFFFFFUL;
113 t7 += (
t6 >> 26);
t6 &= 0x3FFFFFFUL;
114 t8 += (
t7 >> 26);
t7 &= 0x3FFFFFFUL;
115 t9 += (
t8 >> 26);
t8 &= 0x3FFFFFFUL;
138 t0 += x * 0x3D1UL;
t1 += (x << 6);
139 t1 += (
t0 >> 26);
t0 &= 0x3FFFFFFUL;
140 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFFUL;
141 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFFUL; m =
t2;
142 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFFUL; m &=
t3;
143 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFFUL; m &=
t4;
144 t6 += (
t5 >> 26);
t5 &= 0x3FFFFFFUL; m &=
t5;
145 t7 += (
t6 >> 26);
t6 &= 0x3FFFFFFUL; m &=
t6;
146 t8 += (
t7 >> 26);
t7 &= 0x3FFFFFFUL; m &=
t7;
147 t9 += (
t8 >> 26);
t8 &= 0x3FFFFFFUL; m &=
t8;
153 x = (
t9 >> 22) | ((
t9 == 0x03FFFFFUL) & (m == 0x3FFFFFFUL)
154 & ((
t1 + 0x40UL + ((
t0 + 0x3D1UL) >> 26)) > 0x3FFFFFFUL));
157 t0 += 0x3D1UL;
t1 += (x << 6);
158 t1 += (
t0 >> 26);
t0 &= 0x3FFFFFFUL;
159 t2 += (
t1 >> 26);
t1 &= 0x3FFFFFFUL;
160 t3 += (
t2 >> 26);
t2 &= 0x3FFFFFFUL;
161 t4 += (
t3 >> 26);
t3 &= 0x3FFFFFFUL;
162 t5 += (
t4 >> 26);
t4 &= 0x3FFFFFFUL;
163 t6 += (
t5 >> 26);
t5 &= 0x3FFFFFFUL;
164 t7 += (
t6 >> 26);
t6 &= 0x3FFFFFFUL;
165 t8 += (
t7 >> 26);
t7 &= 0x3FFFFFFUL;
166 t9 += (
t8 >> 26);
t8 &= 0x3FFFFFFUL;
196 t0 += x * 0x3D1UL;
t1 += (x << 6);
211 return (
z0 == 0) | (
z1 == 0x3FFFFFFUL);
229 z0 =
t0 & 0x3FFFFFFUL;
233 if ((
z0 != 0
UL) & (
z1 != 0x3FFFFFFUL)) {
263 return (
z0 == 0) | (
z1 == 0x3FFFFFFUL);
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;
282 return (
t[0] |
t[1] |
t[2] |
t[3] |
t[4] |
t[5] |
t[6] |
t[7] |
t[8] |
t[9]) == 0;
299 for (i=0; i<10; i++) {
312 for (i = 9; i >= 0; i--) {
313 if (
a->n[i] >
b->n[i]) {
316 if (
a->n[i] <
b->n[i]) {
336 ret = !((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] + 0x3D1UL) >> 26)) > 0x3FFFFFFUL));
355 r[0] = (
a->n[9] >> 14) & 0xff;
356 r[1] = (
a->n[9] >> 6) & 0xff;
357 r[2] = ((
a->n[9] & 0x3F) << 2) | ((
a->n[8] >> 24) & 0x3);
358 r[3] = (
a->n[8] >> 16) & 0xff;
359 r[4] = (
a->n[8] >> 8) & 0xff;
360 r[5] =
a->n[8] & 0xff;
361 r[6] = (
a->n[7] >> 18) & 0xff;
362 r[7] = (
a->n[7] >> 10) & 0xff;
363 r[8] = (
a->n[7] >> 2) & 0xff;
364 r[9] = ((
a->n[7] & 0x3) << 6) | ((
a->n[6] >> 20) & 0x3f);
365 r[10] = (
a->n[6] >> 12) & 0xff;
366 r[11] = (
a->n[6] >> 4) & 0xff;
367 r[12] = ((
a->n[6] & 0xf) << 4) | ((
a->n[5] >> 22) & 0xf);
368 r[13] = (
a->n[5] >> 14) & 0xff;
369 r[14] = (
a->n[5] >> 6) & 0xff;
370 r[15] = ((
a->n[5] & 0x3f) << 2) | ((
a->n[4] >> 24) & 0x3);
371 r[16] = (
a->n[4] >> 16) & 0xff;
372 r[17] = (
a->n[4] >> 8) & 0xff;
373 r[18] =
a->n[4] & 0xff;
374 r[19] = (
a->n[3] >> 18) & 0xff;
375 r[20] = (
a->n[3] >> 10) & 0xff;
376 r[21] = (
a->n[3] >> 2) & 0xff;
377 r[22] = ((
a->n[3] & 0x3) << 6) | ((
a->n[2] >> 20) & 0x3f);
378 r[23] = (
a->n[2] >> 12) & 0xff;
379 r[24] = (
a->n[2] >> 4) & 0xff;
380 r[25] = ((
a->n[2] & 0xf) << 4) | ((
a->n[1] >> 22) & 0xf);
381 r[26] = (
a->n[1] >> 14) & 0xff;
382 r[27] = (
a->n[1] >> 6) & 0xff;
383 r[28] = ((
a->n[1] & 0x3f) << 2) | ((
a->n[0] >> 24) & 0x3);
384 r[29] = (
a->n[0] >> 16) & 0xff;
385 r[30] = (
a->n[0] >> 8) & 0xff;
386 r[31] =
a->n[0] & 0xff;
394 r->
n[0] = 0x3FFFC2FUL * 2 * (m + 1) -
a->n[0];
395 r->
n[1] = 0x3FFFFBFUL * 2 * (m + 1) -
a->n[1];
396 r->
n[2] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[2];
397 r->
n[3] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[3];
398 r->
n[4] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[4];
399 r->
n[5] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[5];
400 r->
n[6] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[6];
401 r->
n[7] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[7];
402 r->
n[8] = 0x3FFFFFFUL * 2 * (m + 1) -
a->n[8];
403 r->
n[9] = 0x03FFFFFUL * 2 * (m + 1) -
a->n[9];
405 r->magnitude = m + 1;
444 r->magnitude +=
a->magnitude;
450#if defined(USE_EXTERNAL_ASM)
459#define VERIFY_BITS(x, n) VERIFY_CHECK(((x) >> (n)) == 0)
461#define VERIFY_BITS(x, n) do { } while(0)
509 t9 = d &
M; d >>= 26;
754 r[8] =
c &
M;
c >>= 26;
c +=
u8 *
R1;
762 r[9] =
c & (
M >> 4);
c >>= 22;
c += d * (
R1 << 4);
769 d =
c * (
R0 >> 4) +
t0;
772 r[0] = d &
M; d >>= 26;
776 d +=
c * (
R1 >> 4) +
t1;
781 r[1] = d &
M; d >>= 26;
823 t9 = d &
M; d >>= 26;
1005 u8 = d &
M; d >>= 26;
c +=
u8 *
R0;
1028 r[8] =
c &
M;
c >>= 26;
c +=
u8 *
R1;
1036 r[9] =
c & (
M >> 4);
c >>= 22;
c += d * (
R1 << 4);
1043 d =
c * (
R0 >> 4) +
t0;
1046 r[0] = d &
M; d >>= 26;
1050 d +=
c * (
R1 >> 4) +
t1;
1055 r[1] = d &
M; d >>= 26;
1116 r->magnitude =
a->magnitude;
1117 r->normalized =
a->normalized;
1141 r->
n[0] =
a->n[0] |
a->n[1] << 26;
1142 r->
n[1] =
a->n[1] >> 6 |
a->n[2] << 20;
1143 r->
n[2] =
a->n[2] >> 12 |
a->n[3] << 14;
1144 r->
n[3] =
a->n[3] >> 18 |
a->n[4] << 8;
1145 r->
n[4] =
a->n[4] >> 24 |
a->n[5] << 2 |
a->n[6] << 28;
1146 r->
n[5] =
a->n[6] >> 4 |
a->n[7] << 22;
1147 r->
n[6] =
a->n[7] >> 10 |
a->n[8] << 16;
1148 r->
n[7] =
a->n[8] >> 16 |
a->n[9] << 10;
1152 r->
n[0] =
a->n[0] & 0x3FFFFFFUL;
1153 r->
n[1] =
a->n[0] >> 26 | ((
a->n[1] << 6) & 0x3FFFFFFUL);
1154 r->
n[2] =
a->n[1] >> 20 | ((
a->n[2] << 12) & 0x3FFFFFFUL);
1155 r->
n[3] =
a->n[2] >> 14 | ((
a->n[3] << 18) & 0x3FFFFFFUL);
1156 r->
n[4] =
a->n[3] >> 8 | ((
a->n[4] << 24) & 0x3FFFFFFUL);
1157 r->
n[5] = (
a->n[4] >> 2) & 0x3FFFFFFUL;
1158 r->
n[6] =
a->n[4] >> 28 | ((
a->n[5] << 4) & 0x3FFFFFFUL);
1159 r->
n[7] =
a->n[5] >> 22 | ((
a->n[6] << 10) & 0x3FFFFFFUL);
1160 r->
n[8] =
a->n[6] >> 16 | ((
a->n[7] << 16) & 0x3FFFFFFUL);
1161 r->
n[9] =
a->n[7] >> 10;
1171 a5 =
a->v[5],
a6 =
a->v[6],
a7 =
a->v[7],
a8 =
a->v[8];
1187 r->
n[1] = (
a0 >> 26 |
a1 << 4) &
M26;
1188 r->
n[2] = (
a1 >> 22 |
a2 << 8) &
M26;
1189 r->
n[3] = (
a2 >> 18 |
a3 << 12) &
M26;
1190 r->
n[4] = (
a3 >> 14 |
a4 << 16) &
M26;
1191 r->
n[5] = (
a4 >> 10 |
a5 << 20) &
M26;
1192 r->
n[6] = (
a5 >> 6 |
a6 << 24) &
M26;
1193 r->
n[7] = (
a6 >> 2 ) &
M26;
1194 r->
n[8] = (
a6 >> 28 |
a7 << 2) &
M26;
1195 r->
n[9] = (
a7 >> 24 |
a8 << 6);
1214 r->
v[1] = (
a1 >> 4 |
a2 << 22) &
M30;
1215 r->
v[2] = (
a2 >> 8 |
a3 << 18) &
M30;
1216 r->
v[3] = (
a3 >> 12 |
a4 << 14) &
M30;
1217 r->
v[4] = (
a4 >> 16 |
a5 << 10) &
M30;
1218 r->
v[5] = (
a5 >> 20 |
a6 << 6) &
M30;
1219 r->
v[6] = (
a6 >> 24 |
a7 << 2
1221 r->
v[7] = (
a8 >> 2 |
a9 << 24) &
M30;
1226 {{-0x3D1, -4, 0, 0, 0, 0, 0, 0, 65536}},
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 void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *a)
static const secp256k1_modinv32_modinfo secp256k1_const_modinfo_fe
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 SECP256K1_INLINE void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t *SECP256K1_RESTRICT b)
static int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static void secp256k1_fe_from_signed30(secp256k1_fe *r, const secp256k1_modinv32_signed30 *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_normalize(secp256k1_fe *r)
#define VERIFY_BITS(x, n)
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 void secp256k1_fe_to_signed30(secp256k1_modinv32_signed30 *r, const secp256k1_fe *a)
static int secp256k1_fe_normalizes_to_zero(secp256k1_fe *r)
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_modinv32_var(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_modinfo *modinfo)
static void secp256k1_modinv32(secp256k1_modinv32_signed30 *x, const secp256k1_modinv32_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