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" 17 #if defined(USE_ASM_X86_64) 33 const uint64_t *d = a->
n;
34 int m = a->normalized ? 1 : 2 * a->magnitude, r = 1;
36 r &= (d[0] <= 0xFFFFFFFFFFFFFULL * m);
37 r &= (d[1] <= 0xFFFFFFFFFFFFFULL * m);
38 r &= (d[2] <= 0xFFFFFFFFFFFFFULL * m);
39 r &= (d[3] <= 0xFFFFFFFFFFFFFULL * m);
40 r &= (d[4] <= 0x0FFFFFFFFFFFFULL * m);
41 r &= (a->magnitude >= 0);
42 r &= (a->magnitude <= 2048);
44 r &= (a->magnitude <= 1);
45 if (r && (d[4] == 0x0FFFFFFFFFFFFULL) && ((d[3] & d[2] & d[1]) == 0xFFFFFFFFFFFFFULL)) {
46 r &= (d[0] < 0xFFFFEFFFFFC2FULL);
54 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
58 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
61 t0 += x * 0x1000003D1ULL;
62 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
63 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
64 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
65 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
71 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
72 & (t0 >= 0xFFFFEFFFFFC2FULL));
75 t0 += x * 0x1000003D1ULL;
76 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
77 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
78 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
79 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
85 t4 &= 0x0FFFFFFFFFFFFULL;
87 r->
n[0] = t0; r->
n[1] = t1; r->
n[2] = t2; r->
n[3] = t3; r->
n[4] = t4;
92 secp256k1_fe_verify(r);
97 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
100 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
103 t0 += x * 0x1000003D1ULL;
104 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
105 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
106 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
107 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
112 r->
n[0] = t0; r->
n[1] = t1; r->
n[2] = t2; r->
n[3] = t3; r->
n[4] = t4;
116 secp256k1_fe_verify(r);
121 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
125 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
128 t0 += x * 0x1000003D1ULL;
129 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
130 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; m = t1;
131 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; m &= t2;
132 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; m &= t3;
138 x = (t4 >> 48) | ((t4 == 0x0FFFFFFFFFFFFULL) & (m == 0xFFFFFFFFFFFFFULL)
139 & (t0 >= 0xFFFFEFFFFFC2FULL));
142 t0 += 0x1000003D1ULL;
143 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL;
144 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL;
145 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL;
146 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL;
152 t4 &= 0x0FFFFFFFFFFFFULL;
155 r->
n[0] = t0; r->
n[1] = t1; r->
n[2] = t2; r->
n[3] = t3; r->
n[4] = t4;
160 secp256k1_fe_verify(r);
165 uint64_t t0 = r->
n[0], t1 = r->
n[1], t2 = r->
n[2], t3 = r->
n[3], t4 = r->
n[4];
171 uint64_t x = t4 >> 48; t4 &= 0x0FFFFFFFFFFFFULL;
174 t0 += x * 0x1000003D1ULL;
175 t1 += (t0 >> 52); t0 &= 0xFFFFFFFFFFFFFULL; z0 = t0; z1 = t0 ^ 0x1000003D0ULL;
176 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
177 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
178 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
179 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
184 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
188 uint64_t t0, t1, t2, t3, t4;
199 t0 += x * 0x1000003D1ULL;
202 z0 = t0 & 0xFFFFFFFFFFFFFULL;
203 z1 = z0 ^ 0x1000003D0ULL;
206 if ((z0 != 0ULL) & (z1 != 0xFFFFFFFFFFFFFULL)) {
214 t4 &= 0x0FFFFFFFFFFFFULL;
217 t2 += (t1 >> 52); t1 &= 0xFFFFFFFFFFFFFULL; z0 |= t1; z1 &= t1;
218 t3 += (t2 >> 52); t2 &= 0xFFFFFFFFFFFFFULL; z0 |= t2; z1 &= t2;
219 t4 += (t3 >> 52); t3 &= 0xFFFFFFFFFFFFFULL; z0 |= t3; z1 &= t3;
220 z0 |= t4; z1 &= t4 ^ 0xF000000000000ULL;
225 return (z0 == 0) | (z1 == 0xFFFFFFFFFFFFFULL);
230 r->
n[1] = r->
n[2] = r->
n[3] = r->
n[4] = 0;
234 secp256k1_fe_verify(r);
239 const uint64_t *t = a->
n;
242 secp256k1_fe_verify(a);
244 return (t[0] | t[1] | t[2] | t[3] | t[4]) == 0;
250 secp256k1_fe_verify(a);
261 for (i=0; i<5; i++) {
271 secp256k1_fe_verify(a);
272 secp256k1_fe_verify(b);
274 for (i = 4; i >= 0; i--) {
275 if (a->
n[i] > b->
n[i]) {
278 if (a->
n[i] < b->
n[i]) {
286 r->
n[0] = (uint64_t)a[31]
287 | ((uint64_t)a[30] << 8)
288 | ((uint64_t)a[29] << 16)
289 | ((uint64_t)a[28] << 24)
290 | ((uint64_t)a[27] << 32)
291 | ((uint64_t)a[26] << 40)
292 | ((uint64_t)(a[25] & 0xF) << 48);
293 r->
n[1] = (uint64_t)((a[25] >> 4) & 0xF)
294 | ((uint64_t)a[24] << 4)
295 | ((uint64_t)a[23] << 12)
296 | ((uint64_t)a[22] << 20)
297 | ((uint64_t)a[21] << 28)
298 | ((uint64_t)a[20] << 36)
299 | ((uint64_t)a[19] << 44);
300 r->
n[2] = (uint64_t)a[18]
301 | ((uint64_t)a[17] << 8)
302 | ((uint64_t)a[16] << 16)
303 | ((uint64_t)a[15] << 24)
304 | ((uint64_t)a[14] << 32)
305 | ((uint64_t)a[13] << 40)
306 | ((uint64_t)(a[12] & 0xF) << 48);
307 r->
n[3] = (uint64_t)((a[12] >> 4) & 0xF)
308 | ((uint64_t)a[11] << 4)
309 | ((uint64_t)a[10] << 12)
310 | ((uint64_t)a[9] << 20)
311 | ((uint64_t)a[8] << 28)
312 | ((uint64_t)a[7] << 36)
313 | ((uint64_t)a[6] << 44);
314 r->
n[4] = (uint64_t)a[5]
315 | ((uint64_t)a[4] << 8)
316 | ((uint64_t)a[3] << 16)
317 | ((uint64_t)a[2] << 24)
318 | ((uint64_t)a[1] << 32)
319 | ((uint64_t)a[0] << 40);
320 if (r->
n[4] == 0x0FFFFFFFFFFFFULL && (r->
n[3] & r->
n[2] & r->
n[1]) == 0xFFFFFFFFFFFFFULL && r->
n[0] >= 0xFFFFEFFFFFC2FULL) {
326 secp256k1_fe_verify(r);
335 secp256k1_fe_verify(a);
337 r[0] = (a->
n[4] >> 40) & 0xFF;
338 r[1] = (a->
n[4] >> 32) & 0xFF;
339 r[2] = (a->
n[4] >> 24) & 0xFF;
340 r[3] = (a->
n[4] >> 16) & 0xFF;
341 r[4] = (a->
n[4] >> 8) & 0xFF;
342 r[5] = a->
n[4] & 0xFF;
343 r[6] = (a->
n[3] >> 44) & 0xFF;
344 r[7] = (a->
n[3] >> 36) & 0xFF;
345 r[8] = (a->
n[3] >> 28) & 0xFF;
346 r[9] = (a->
n[3] >> 20) & 0xFF;
347 r[10] = (a->
n[3] >> 12) & 0xFF;
348 r[11] = (a->
n[3] >> 4) & 0xFF;
349 r[12] = ((a->
n[2] >> 48) & 0xF) | ((a->
n[3] & 0xF) << 4);
350 r[13] = (a->
n[2] >> 40) & 0xFF;
351 r[14] = (a->
n[2] >> 32) & 0xFF;
352 r[15] = (a->
n[2] >> 24) & 0xFF;
353 r[16] = (a->
n[2] >> 16) & 0xFF;
354 r[17] = (a->
n[2] >> 8) & 0xFF;
355 r[18] = a->
n[2] & 0xFF;
356 r[19] = (a->
n[1] >> 44) & 0xFF;
357 r[20] = (a->
n[1] >> 36) & 0xFF;
358 r[21] = (a->
n[1] >> 28) & 0xFF;
359 r[22] = (a->
n[1] >> 20) & 0xFF;
360 r[23] = (a->
n[1] >> 12) & 0xFF;
361 r[24] = (a->
n[1] >> 4) & 0xFF;
362 r[25] = ((a->
n[0] >> 48) & 0xF) | ((a->
n[1] & 0xF) << 4);
363 r[26] = (a->
n[0] >> 40) & 0xFF;
364 r[27] = (a->
n[0] >> 32) & 0xFF;
365 r[28] = (a->
n[0] >> 24) & 0xFF;
366 r[29] = (a->
n[0] >> 16) & 0xFF;
367 r[30] = (a->
n[0] >> 8) & 0xFF;
368 r[31] = a->
n[0] & 0xFF;
374 secp256k1_fe_verify(a);
376 r->
n[0] = 0xFFFFEFFFFFC2FULL * 2 * (m + 1) - a->
n[0];
377 r->
n[1] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->
n[1];
378 r->
n[2] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->
n[2];
379 r->
n[3] = 0xFFFFFFFFFFFFFULL * 2 * (m + 1) - a->
n[3];
380 r->
n[4] = 0x0FFFFFFFFFFFFULL * 2 * (m + 1) - a->
n[4];
382 r->magnitude = m + 1;
384 secp256k1_fe_verify(r);
397 secp256k1_fe_verify(r);
403 secp256k1_fe_verify(a);
411 r->magnitude += a->magnitude;
413 secp256k1_fe_verify(r);
421 secp256k1_fe_verify(a);
422 secp256k1_fe_verify(b);
430 secp256k1_fe_verify(r);
437 secp256k1_fe_verify(a);
443 secp256k1_fe_verify(r);
448 uint64_t mask0, mask1;
449 mask0 = flag + ~((uint64_t)0);
451 r->
n[0] = (r->
n[0] & mask0) | (a->
n[0] & mask1);
452 r->
n[1] = (r->
n[1] & mask0) | (a->
n[1] & mask1);
453 r->
n[2] = (r->
n[2] & mask0) | (a->
n[2] & mask1);
454 r->
n[3] = (r->
n[3] & mask0) | (a->
n[3] & mask1);
455 r->
n[4] = (r->
n[4] & mask0) | (a->
n[4] & mask1);
457 if (a->magnitude > r->magnitude) {
458 r->magnitude = a->magnitude;
460 r->normalized &= a->normalized;
465 uint64_t mask0, mask1;
466 mask0 = flag + ~((uint64_t)0);
468 r->
n[0] = (r->
n[0] & mask0) | (a->
n[0] & mask1);
469 r->
n[1] = (r->
n[1] & mask0) | (a->
n[1] & mask1);
470 r->
n[2] = (r->
n[2] & mask0) | (a->
n[2] & mask1);
471 r->
n[3] = (r->
n[3] & mask0) | (a->
n[3] & mask1);
478 r->
n[0] = a->
n[0] | a->
n[1] << 52;
479 r->
n[1] = a->
n[1] >> 12 | a->
n[2] << 40;
480 r->
n[2] = a->
n[2] >> 24 | a->
n[3] << 28;
481 r->
n[3] = a->
n[3] >> 36 | a->
n[4] << 16;
485 r->
n[0] = a->
n[0] & 0xFFFFFFFFFFFFFULL;
486 r->
n[1] = a->
n[0] >> 52 | ((a->
n[1] << 12) & 0xFFFFFFFFFFFFFULL);
487 r->
n[2] = a->
n[1] >> 40 | ((a->
n[2] << 24) & 0xFFFFFFFFFFFFFULL);
488 r->
n[3] = a->
n[2] >> 28 | ((a->
n[3] << 36) & 0xFFFFFFFFFFFFFULL);
489 r->
n[4] = a->
n[3] >> 16;
#define VERIFY_CHECK(cond)
static SECP256K1_INLINE void secp256k1_fe_sqr_inner(uint32_t *r, const uint32_t *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 int secp256k1_fe_set_b32(secp256k1_fe *r, const unsigned char *a)
static SECP256K1_INLINE void secp256k1_fe_cmov(secp256k1_fe *r, const secp256k1_fe *a, int flag)
static void secp256k1_fe_sqr(secp256k1_fe *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 void secp256k1_fe_mul_inner(uint32_t *r, const uint32_t *a, const uint32_t *SECP256K1_RESTRICT b)
static int secp256k1_fe_normalizes_to_zero(secp256k1_fe *r)
#define SECP256K1_RESTRICT
static void secp256k1_fe_to_storage(secp256k1_fe_storage *r, const secp256k1_fe *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_mul_int(secp256k1_fe *r, int a)
static int secp256k1_fe_normalizes_to_zero_var(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_negate(secp256k1_fe *r, const secp256k1_fe *a, int m)
static void secp256k1_fe_normalize_weak(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_clear(secp256k1_fe *a)
static SECP256K1_INLINE int secp256k1_fe_is_zero(const secp256k1_fe *a)
static int secp256k1_fe_cmp_var(const secp256k1_fe *a, const secp256k1_fe *b)
static void secp256k1_fe_mul(secp256k1_fe *r, const secp256k1_fe *a, const secp256k1_fe *SECP256K1_RESTRICT b)
static void secp256k1_fe_normalize_var(secp256k1_fe *r)
static SECP256K1_INLINE void secp256k1_fe_add(secp256k1_fe *r, const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_from_storage(secp256k1_fe *r, const secp256k1_fe_storage *a)
static SECP256K1_INLINE int secp256k1_fe_is_odd(const secp256k1_fe *a)
static SECP256K1_INLINE void secp256k1_fe_set_int(secp256k1_fe *r, int a)