7#ifndef SECP256K1_FIELD_REPR_IMPL_H
8#define SECP256K1_FIELD_REPR_IMPL_H
18static void secp256k1_fe_impl_verify(
const secp256k1_fe *a) {
19 const uint64_t *d = a->
n;
20 int m = a->normalized ? 1 : 2 * a->magnitude;
28 if ((d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
36 r->
n[0] = 0xFFFFFFFFFFFFFULL * 2 *
m;
37 r->
n[1] = 0xFFFFFFFFFFFFFULL * 2 *
m;
38 r->
n[2] = 0xFFFFFFFFFFFFFULL * 2 *
m;
39 r->
n[3] = 0xFFFFFFFFFFFFFULL * 2 *
m;
40 r->
n[4] = 0x0FFFFFFFFFFFFULL * 2 *
m;
44 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
48 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
51 t0 += x * 0x1000003D1ULL;
52 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
53 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
m = t1;
54 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
m &= t2;
55 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
m &= t3;
61 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (
m == 0xFFFFFFFFFFFFFULL)
62 & (t0 >= 0xFFFFEFFFFFC2FULL));
65 t0 += x * 0x1000003D1ULL;
66 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
67 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
68 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
69 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
75 t4 &= 0x0FFFFFFFFFFFFULL;
77 r->
n[0] = t0; r->
n[1] = t1; r->
n[2] = t2; r->
n[3] = t3; r->
n[4] = t4;
81 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
84 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
87 t0 += x * 0x1000003D1ULL;
88 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
89 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
90 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
91 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
96 r->
n[0] = t0; r->
n[1] = t1; r->
n[2] = t2; r->
n[3] = t3; r->
n[4] = t4;
100 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
104 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
107 t0 += x * 0x1000003D1ULL;
108 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
109 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
m = t1;
110 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
m &= t2;
111 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
m &= t3;
117 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (
m == 0xFFFFFFFFFFFFFULL)
118 & (t0 >= 0xFFFFEFFFFFC2FULL));
121 t0 += 0x1000003D1ULL;
122 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
123 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
124 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
125 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
131 t4 &= 0x0FFFFFFFFFFFFULL;
134 r->
n[0] = t0; r->
n[1] = t1; r->
n[2] = t2; r->
n[3] = t3; r->
n[4] = t4;
138 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
144 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
147 t0 += x * 0x1000003D1ULL;
148 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
149 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
150 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
151 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
152 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
157 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
161 uint64_t t0, t1, t2, t3, t4;
172 t0 += x * 0x1000003D1ULL;
175 z0 = t0 & 0xFFFFFFFFFFFFFULL;
176 z1 = z0 ^ 0x1000003D0ULL;
179 if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
187 t4 &= 0x0FFFFFFFFFFFFULL;
190 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
191 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
192 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
193 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
198 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
203 r->
n[1] = r->
n[2] = r->
n[3] = r->
n[4] = 0;
207 const uint64_t *
t = a->
n;
208 return (
t[0] |
t[1] |
t[2] |
t[3] |
t[4]) == 0;
217 for (i = 4; i >= 0; i--) {
218 if (a->
n[i] > b->
n[i]) {
221 if (a->
n[i] < b->
n[i]) {
229 r->
n[0] = (uint64_t)a[31]
230 | ((uint64_t)a[30] << 8)
231 | ((uint64_t)a[29] << 16)
232 | ((uint64_t)a[28] << 24)
233 | ((uint64_t)a[27] << 32)
234 | ((uint64_t)a[26] << 40)
235 | ((uint64_t)(a[25] & 0xF) << 48);
236 r->
n[1] = (uint64_t)((a[25] >> 4) & 0xF)
237 | ((uint64_t)a[24] << 4)
238 | ((uint64_t)a[23] << 12)
239 | ((uint64_t)a[22] << 20)
240 | ((uint64_t)a[21] << 28)
241 | ((uint64_t)a[20] << 36)
242 | ((uint64_t)a[19] << 44);
243 r->
n[2] = (uint64_t)a[18]
244 | ((uint64_t)a[17] << 8)
245 | ((uint64_t)a[16] << 16)
246 | ((uint64_t)a[15] << 24)
247 | ((uint64_t)a[14] << 32)
248 | ((uint64_t)a[13] << 40)
249 | ((uint64_t)(a[12] & 0xF) << 48);
250 r->
n[3] = (uint64_t)((a[12] >> 4) & 0xF)
251 | ((uint64_t)a[11] << 4)
252 | ((uint64_t)a[10] << 12)
253 | ((uint64_t)a[9] << 20)
254 | ((uint64_t)a[8] << 28)
255 | ((uint64_t)a[7] << 36)
256 | ((uint64_t)a[6] << 44);
257 r->
n[4] = (uint64_t)a[5]
258 | ((uint64_t)a[4] << 8)
259 | ((uint64_t)a[3] << 16)
260 | ((uint64_t)a[2] << 24)
261 | ((uint64_t)a[1] << 32)
262 | ((uint64_t)a[0] << 40);
267 return !((r->
n[4] == 0x0FFFFFFFFFFFFULL) & ((r->
n[3] & r->
n[2] & r->
n[1]) == 0xFFFFFFFFFFFFFULL) & (r->
n[0] >= 0xFFFFEFFFFFC2FULL));
272 r[0] = (a->
n[4] >> 40) & 0xFF;
273 r[1] = (a->
n[4] >> 32) & 0xFF;
274 r[2] = (a->
n[4] >> 24) & 0xFF;
275 r[3] = (a->
n[4] >> 16) & 0xFF;
276 r[4] = (a->
n[4] >> 8) & 0xFF;
277 r[5] = a->
n[4] & 0xFF;
278 r[6] = (a->
n[3] >> 44) & 0xFF;
279 r[7] = (a->
n[3] >> 36) & 0xFF;
280 r[8] = (a->
n[3] >> 28) & 0xFF;
281 r[9] = (a->
n[3] >> 20) & 0xFF;
282 r[10] = (a->
n[3] >> 12) & 0xFF;
283 r[11] = (a->
n[3] >> 4) & 0xFF;
284 r[12] = ((a->
n[2] >> 48) & 0xF) | ((a->
n[3] & 0xF) << 4);
285 r[13] = (a->
n[2] >> 40) & 0xFF;
286 r[14] = (a->
n[2] >> 32) & 0xFF;
287 r[15] = (a->
n[2] >> 24) & 0xFF;
288 r[16] = (a->
n[2] >> 16) & 0xFF;
289 r[17] = (a->
n[2] >> 8) & 0xFF;
290 r[18] = a->
n[2] & 0xFF;
291 r[19] = (a->
n[1] >> 44) & 0xFF;
292 r[20] = (a->
n[1] >> 36) & 0xFF;
293 r[21] = (a->
n[1] >> 28) & 0xFF;
294 r[22] = (a->
n[1] >> 20) & 0xFF;
295 r[23] = (a->
n[1] >> 12) & 0xFF;
296 r[24] = (a->
n[1] >> 4) & 0xFF;
297 r[25] = ((a->
n[0] >> 48) & 0xF) | ((a->
n[1] & 0xF) << 4);
298 r[26] = (a->
n[0] >> 40) & 0xFF;
299 r[27] = (a->
n[0] >> 32) & 0xFF;
300 r[28] = (a->
n[0] >> 24) & 0xFF;
301 r[29] = (a->
n[0] >> 16) & 0xFF;
302 r[30] = (a->
n[0] >> 8) & 0xFF;
303 r[31] = a->
n[0] & 0xFF;
308 VERIFY_CHECK(0xFFFFEFFFFFC2FULL * 2 * (
m + 1) >= 0xFFFFFFFFFFFFFULL * 2 *
m);
309 VERIFY_CHECK(0xFFFFFFFFFFFFFULL * 2 * (
m + 1) >= 0xFFFFFFFFFFFFFULL * 2 *
m);
310 VERIFY_CHECK(0x0FFFFFFFFFFFFULL * 2 * (
m + 1) >= 0x0FFFFFFFFFFFFULL * 2 *
m);
314 r->
n[0] = 0xFFFFEFFFFFC2FULL * 2 * (
m + 1) - a->
n[0];
315 r->
n[1] = 0xFFFFFFFFFFFFFULL * 2 * (
m + 1) - a->
n[1];
316 r->
n[2] = 0xFFFFFFFFFFFFFULL * 2 * (
m + 1) - a->
n[2];
317 r->
n[3] = 0xFFFFFFFFFFFFFULL * 2 * (
m + 1) - a->
n[3];
318 r->
n[4] = 0x0FFFFFFFFFFFFULL * 2 * (
m + 1) - a->
n[4];
350 uint64_t mask0, mask1;
351 volatile int vflag = flag;
353 mask0 = vflag + ~((uint64_t)0);
355 r->
n[0] = (r->
n[0] & mask0) | (a->
n[0] & mask1);
356 r->
n[1] = (r->
n[1] & mask0) | (a->
n[1] & mask1);
357 r->
n[2] = (r->
n[2] & mask0) | (a->
n[2] & mask1);
358 r->
n[3] = (r->
n[3] & mask0) | (a->
n[3] & mask1);
359 r->
n[4] = (r->
n[4] & mask0) | (a->
n[4] & mask1);
363 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
364 uint64_t one = (uint64_t)1;
365 uint64_t mask = -(t0 & one) >> 12;
377 t0 += 0xFFFFEFFFFFC2FULL & mask;
392 r->
n[0] = (t0 >> 1) + ((t1 & one) << 51);
393 r->
n[1] = (t1 >> 1) + ((t2 & one) << 51);
394 r->
n[2] = (t2 >> 1) + ((t3 & one) << 51);
395 r->
n[3] = (t3 >> 1) + ((t4 & one) << 51);
417 uint64_t mask0, mask1;
418 volatile int vflag = flag;
420 mask0 = vflag + ~((uint64_t)0);
422 r->
n[0] = (r->
n[0] & mask0) | (a->
n[0] & mask1);
423 r->
n[1] = (r->
n[1] & mask0) | (a->
n[1] & mask1);
424 r->
n[2] = (r->
n[2] & mask0) | (a->
n[2] & mask1);
425 r->
n[3] = (r->
n[3] & mask0) | (a->
n[3] & mask1);
429 r->
n[0] = a->
n[0] | a->
n[1] << 52;
430 r->
n[1] = a->
n[1] >> 12 | a->
n[2] << 40;
431 r->
n[2] = a->
n[2] >> 24 | a->
n[3] << 28;
432 r->
n[3] = a->
n[3] >> 36 | a->
n[4] << 16;
436 r->
n[0] = a->
n[0] & 0xFFFFFFFFFFFFFULL;
437 r->
n[1] = a->
n[0] >> 52 | ((a->
n[1] << 12) & 0xFFFFFFFFFFFFFULL);
438 r->
n[2] = a->
n[1] >> 40 | ((a->
n[2] << 24) & 0xFFFFFFFFFFFFFULL);
439 r->
n[3] = a->
n[2] >> 28 | ((a->
n[3] << 36) & 0xFFFFFFFFFFFFFULL);
440 r->
n[4] = a->
n[3] >> 16;
444 const uint64_t M52 = UINT64_MAX >> 12;
445 const uint64_t a0 = a->
v[0], a1 = a->
v[1], a2 = a->
v[2], a3 = a->
v[3], a4 = a->
v[4];
457 r->
n[1] = (a0 >> 52 | a1 << 10) & M52;
458 r->
n[2] = (a1 >> 42 | a2 << 20) & M52;
459 r->
n[3] = (a2 >> 32 | a3 << 30) & M52;
460 r->
n[4] = (a3 >> 22 | a4 << 40);
464 const uint64_t M62 = UINT64_MAX >> 2;
465 const uint64_t a0 = a->
n[0], a1 = a->
n[1], a2 = a->
n[2], a3 = a->
n[3], a4 = a->
n[4];
467 r->
v[0] = (a0 | a1 << 52) & M62;
468 r->
v[1] = (a1 >> 10 | a2 << 42) & M62;
469 r->
v[2] = (a2 >> 20 | a3 << 32) & M62;
470 r->
v[3] = (a3 >> 30 | a4 << 22) & M62;
475 {{-0x1000003D1LL, 0, 0, 0, 256}},
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
static int secp256k1_fe_sqrt(secp256k1_fe *SECP256K1_RESTRICT r, const secp256k1_fe *SECP256K1_RESTRICT a)
Compute a square root of a field element.
#define secp256k1_fe_normalize_var
#define secp256k1_fe_is_zero
#define secp256k1_fe_normalize
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_impl_half(secp256k1_fe *r)
static void secp256k1_fe_impl_set_b32_mod(secp256k1_fe *r, const unsigned char *a)
static void secp256k1_fe_impl_normalize_weak(secp256k1_fe *r)
static int secp256k1_fe_impl_is_square_var(const secp256k1_fe *x)
static void secp256k1_fe_impl_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_impl_add(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_impl_set_int(secp256k1_fe *r, int a)
static SECP256K1_INLINE int secp256k1_fe_impl_is_zero(const secp256k1_fe *a)
static void secp256k1_fe_impl_get_bounds(secp256k1_fe *r, int m)
static int secp256k1_fe_impl_set_b32_limit(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 SECP256K1_INLINE void secp256k1_fe_impl_negate_unchecked(secp256k1_fe *r, const secp256k1_fe *a, int m)
static SECP256K1_INLINE void secp256k1_fe_impl_mul_int_unchecked(secp256k1_fe *r, int a)
static int secp256k1_fe_impl_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b)
static void secp256k1_fe_to_signed62(secp256k1_modinv64_signed62 *r, const secp256k1_fe *a)
static int secp256k1_fe_impl_normalizes_to_zero(const secp256k1_fe *r)
static void secp256k1_fe_impl_inv_var(secp256k1_fe *r, const secp256k1_fe *x)
static SECP256K1_INLINE void secp256k1_fe_impl_sqr(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_impl_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
static void secp256k1_fe_impl_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_impl_add_int(secp256k1_fe *r, int a)
static int secp256k1_fe_impl_normalizes_to_zero_var(const secp256k1_fe *r)
static void secp256k1_fe_impl_normalize(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_impl_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
static void secp256k1_fe_impl_inv(secp256k1_fe *r, const secp256k1_fe *x)
static void secp256k1_fe_impl_normalize_var(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 SECP256K1_INLINE int secp256k1_fe_impl_is_odd(const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_impl_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
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)
static int secp256k1_jacobi64_maybe_var(const secp256k1_modinv64_signed62 *x, const secp256k1_modinv64_modinfo *modinfo)
#define VERIFY_CHECK(cond)
#define SECP256K1_RESTRICT
This field implementation represents the value as 10 uint32_t limbs in base 2^26.