7#ifndef SECP256K1_SCALAR_REPR_IMPL_H
8#define SECP256K1_SCALAR_REPR_IMPL_H
15#define SECP256K1_N_0 ((uint32_t)0xD0364141UL)
16#define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
17#define SECP256K1_N_2 ((uint32_t)0xAF48A03BUL)
18#define SECP256K1_N_3 ((uint32_t)0xBAAEDCE6UL)
19#define SECP256K1_N_4 ((uint32_t)0xFFFFFFFEUL)
20#define SECP256K1_N_5 ((uint32_t)0xFFFFFFFFUL)
21#define SECP256K1_N_6 ((uint32_t)0xFFFFFFFFUL)
22#define SECP256K1_N_7 ((uint32_t)0xFFFFFFFFUL)
25#define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
26#define SECP256K1_N_C_1 (~SECP256K1_N_1)
27#define SECP256K1_N_C_2 (~SECP256K1_N_2)
28#define SECP256K1_N_C_3 (~SECP256K1_N_3)
29#define SECP256K1_N_C_4 (1)
32#define SECP256K1_N_H_0 ((uint32_t)0x681B20A0UL)
33#define SECP256K1_N_H_1 ((uint32_t)0xDFE92F46UL)
34#define SECP256K1_N_H_2 ((uint32_t)0x57A4501DUL)
35#define SECP256K1_N_H_3 ((uint32_t)0x5D576E73UL)
36#define SECP256K1_N_H_4 ((uint32_t)0xFFFFFFFFUL)
37#define SECP256K1_N_H_5 ((uint32_t)0xFFFFFFFFUL)
38#define SECP256K1_N_H_6 ((uint32_t)0xFFFFFFFFUL)
39#define SECP256K1_N_H_7 ((uint32_t)0x7FFFFFFFUL)
59 return (a->
d[offset >> 5] >> (offset & 0x1F)) & (0xFFFFFFFF >> (32 -
count));
67 if ((offset +
count - 1) >> 5 == offset >> 5) {
71 return ((a->
d[offset >> 5] >> (offset & 0x1F)) | (a->
d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & (0xFFFFFFFF >> (32 -
count));
98 r->
d[0] =
t & 0xFFFFFFFFUL;
t >>= 32;
100 r->
d[1] =
t & 0xFFFFFFFFUL;
t >>= 32;
102 r->
d[2] =
t & 0xFFFFFFFFUL;
t >>= 32;
104 r->
d[3] =
t & 0xFFFFFFFFUL;
t >>= 32;
106 r->
d[4] =
t & 0xFFFFFFFFUL;
t >>= 32;
107 t += (uint64_t)r->
d[5];
108 r->
d[5] =
t & 0xFFFFFFFFUL;
t >>= 32;
109 t += (uint64_t)r->
d[6];
110 r->
d[6] =
t & 0xFFFFFFFFUL;
t >>= 32;
111 t += (uint64_t)r->
d[7];
112 r->
d[7] =
t & 0xFFFFFFFFUL;
120 uint64_t
t = (uint64_t)a->
d[0] + b->
d[0];
124 r->
d[0] =
t & 0xFFFFFFFFULL;
t >>= 32;
125 t += (uint64_t)a->
d[1] + b->
d[1];
126 r->
d[1] =
t & 0xFFFFFFFFULL;
t >>= 32;
127 t += (uint64_t)a->
d[2] + b->
d[2];
128 r->
d[2] =
t & 0xFFFFFFFFULL;
t >>= 32;
129 t += (uint64_t)a->
d[3] + b->
d[3];
130 r->
d[3] =
t & 0xFFFFFFFFULL;
t >>= 32;
131 t += (uint64_t)a->
d[4] + b->
d[4];
132 r->
d[4] =
t & 0xFFFFFFFFULL;
t >>= 32;
133 t += (uint64_t)a->
d[5] + b->
d[5];
134 r->
d[5] =
t & 0xFFFFFFFFULL;
t >>= 32;
135 t += (uint64_t)a->
d[6] + b->
d[6];
136 r->
d[6] =
t & 0xFFFFFFFFULL;
t >>= 32;
137 t += (uint64_t)a->
d[7] + b->
d[7];
138 r->
d[7] =
t & 0xFFFFFFFFULL;
t >>= 32;
149 volatile int vflag = flag;
153 bit += ((uint32_t) vflag - 1) & 0x100;
154 t = (uint64_t)r->
d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
155 r->
d[0] =
t & 0xFFFFFFFFULL;
t >>= 32;
156 t += (uint64_t)r->
d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
157 r->
d[1] =
t & 0xFFFFFFFFULL;
t >>= 32;
158 t += (uint64_t)r->
d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
159 r->
d[2] =
t & 0xFFFFFFFFULL;
t >>= 32;
160 t += (uint64_t)r->
d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
161 r->
d[3] =
t & 0xFFFFFFFFULL;
t >>= 32;
162 t += (uint64_t)r->
d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
163 r->
d[4] =
t & 0xFFFFFFFFULL;
t >>= 32;
164 t += (uint64_t)r->
d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
165 r->
d[5] =
t & 0xFFFFFFFFULL;
t >>= 32;
166 t += (uint64_t)r->
d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
167 r->
d[6] =
t & 0xFFFFFFFFULL;
t >>= 32;
168 t += (uint64_t)r->
d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
169 r->
d[7] =
t & 0xFFFFFFFFULL;
209 return (a->
d[0] | a->
d[1] | a->
d[2] | a->
d[3] | a->
d[4] | a->
d[5] | a->
d[6] | a->
d[7]) == 0;
217 r->
d[0] =
t & nonzero;
t >>= 32;
219 r->
d[1] =
t & nonzero;
t >>= 32;
221 r->
d[2] =
t & nonzero;
t >>= 32;
223 r->
d[3] =
t & nonzero;
t >>= 32;
225 r->
d[4] =
t & nonzero;
t >>= 32;
227 r->
d[5] =
t & nonzero;
t >>= 32;
229 r->
d[6] =
t & nonzero;
t >>= 32;
231 r->
d[7] =
t & nonzero;
251 uint32_t mask = -(uint32_t)(a->
d[0] & 1U);
252 uint64_t
t = (uint32_t)((a->
d[0] >> 1) | (a->
d[1] << 31));
256 r->
d[0] =
t;
t >>= 32;
257 t += (uint32_t)((a->
d[1] >> 1) | (a->
d[2] << 31));
259 r->
d[1] =
t;
t >>= 32;
260 t += (uint32_t)((a->
d[2] >> 1) | (a->
d[3] << 31));
262 r->
d[2] =
t;
t >>= 32;
263 t += (uint32_t)((a->
d[3] >> 1) | (a->
d[4] << 31));
265 r->
d[3] =
t;
t >>= 32;
266 t += (uint32_t)((a->
d[4] >> 1) | (a->
d[5] << 31));
268 r->
d[4] =
t;
t >>= 32;
269 t += (uint32_t)((a->
d[5] >> 1) | (a->
d[6] << 31));
271 r->
d[5] =
t;
t >>= 32;
272 t += (uint32_t)((a->
d[6] >> 1) | (a->
d[7] << 31));
274 r->
d[6] =
t;
t >>= 32;
287 return ((a->
d[0] ^ 1) | a->
d[1] | a->
d[2] | a->
d[3] | a->
d[4] | a->
d[5] | a->
d[6] | a->
d[7]) == 0;
313 volatile int vflag = flag;
314 uint32_t mask = -vflag;
316 uint64_t
t = (uint64_t)(r->
d[0] ^ mask) + ((
SECP256K1_N_0 + 1) & mask);
319 r->
d[0] =
t & nonzero;
t >>= 32;
321 r->
d[1] =
t & nonzero;
t >>= 32;
323 r->
d[2] =
t & nonzero;
t >>= 32;
325 r->
d[3] =
t & nonzero;
t >>= 32;
327 r->
d[4] =
t & nonzero;
t >>= 32;
329 r->
d[5] =
t & nonzero;
t >>= 32;
331 r->
d[6] =
t & nonzero;
t >>= 32;
333 r->
d[7] =
t & nonzero;
336 return 2 * (mask == 0) - 1;
343#define muladd(a,b) { \
346 uint64_t t = (uint64_t)a * b; \
354 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
358#define muladd_fast(a,b) { \
361 uint64_t t = (uint64_t)a * b; \
368 VERIFY_CHECK(c1 >= th); \
381#define sumadd_fast(a) { \
384 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
385 VERIFY_CHECK(c2 == 0); \
389#define extract(n) { \
397#define extract_fast(n) { \
401 VERIFY_CHECK(c2 == 0); \
406 uint32_t n0 = l[8], n1 = l[9], n2 = l[10], n3 = l[11], n4 = l[12], n5 = l[13], n6 = l[14], n7 = l[15];
407 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
408 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
415 c0 = l[0]; c1 = 0; c2 = 0;
480 c0 = m0; c1 = 0; c2 = 0;
526 r->
d[0] = c & 0xFFFFFFFFUL; c >>= 32;
528 r->
d[1] = c & 0xFFFFFFFFUL; c >>= 32;
530 r->
d[2] = c & 0xFFFFFFFFUL; c >>= 32;
532 r->
d[3] = c & 0xFFFFFFFFUL; c >>= 32;
533 c += p4 + (uint64_t)p8;
534 r->
d[4] = c & 0xFFFFFFFFUL; c >>= 32;
536 r->
d[5] = c & 0xFFFFFFFFUL; c >>= 32;
538 r->
d[6] = c & 0xFFFFFFFFUL; c >>= 32;
540 r->
d[7] = c & 0xFFFFFFFFUL; c >>= 32;
548 uint32_t c0 = 0, c1 = 0, c2 = 0;
680 return ((a->
d[0] ^ b->
d[0]) | (a->
d[1] ^ b->
d[1]) | (a->
d[2] ^ b->
d[2]) | (a->
d[3] ^ b->
d[3]) | (a->
d[4] ^ b->
d[4]) | (a->
d[5] ^ b->
d[5]) | (a->
d[6] ^ b->
d[6]) | (a->
d[7] ^ b->
d[7])) == 0;
685 unsigned int shiftlimbs;
686 unsigned int shiftlow;
687 unsigned int shifthigh;
693 shiftlimbs = shift >> 5;
694 shiftlow = shift & 0x1F;
695 shifthigh = 32 - shiftlow;
696 r->
d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
697 r->
d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
698 r->
d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
699 r->
d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
700 r->
d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
701 r->
d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
702 r->
d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
703 r->
d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
710 uint32_t mask0, mask1;
711 volatile int vflag = flag;
715 mask0 = vflag + ~((uint32_t)0);
717 r->
d[0] = (r->
d[0] & mask0) | (a->
d[0] & mask1);
718 r->
d[1] = (r->
d[1] & mask0) | (a->
d[1] & mask1);
719 r->
d[2] = (r->
d[2] & mask0) | (a->
d[2] & mask1);
720 r->
d[3] = (r->
d[3] & mask0) | (a->
d[3] & mask1);
721 r->
d[4] = (r->
d[4] & mask0) | (a->
d[4] & mask1);
722 r->
d[5] = (r->
d[5] & mask0) | (a->
d[5] & mask1);
723 r->
d[6] = (r->
d[6] & mask0) | (a->
d[6] & mask1);
724 r->
d[7] = (r->
d[7] & mask0) | (a->
d[7] & mask1);
730 const uint32_t a0 = a->
v[0], a1 = a->
v[1], a2 = a->
v[2], a3 = a->
v[3], a4 = a->
v[4],
731 a5 = a->
v[5], a6 = a->
v[6], a7 = a->
v[7], a8 = a->
v[8];
746 r->
d[0] = a0 | a1 << 30;
747 r->
d[1] = a1 >> 2 | a2 << 28;
748 r->
d[2] = a2 >> 4 | a3 << 26;
749 r->
d[3] = a3 >> 6 | a4 << 24;
750 r->
d[4] = a4 >> 8 | a5 << 22;
751 r->
d[5] = a5 >> 10 | a6 << 20;
752 r->
d[6] = a6 >> 12 | a7 << 18;
753 r->
d[7] = a7 >> 14 | a8 << 16;
759 const uint32_t M30 = UINT32_MAX >> 2;
760 const uint32_t a0 = a->
d[0], a1 = a->
d[1], a2 = a->
d[2], a3 = a->
d[3],
761 a4 = a->
d[4], a5 = a->
d[5], a6 = a->
d[6], a7 = a->
d[7];
765 r->
v[1] = (a0 >> 30 | a1 << 2) & M30;
766 r->
v[2] = (a1 >> 28 | a2 << 4) & M30;
767 r->
v[3] = (a2 >> 26 | a3 << 6) & M30;
768 r->
v[4] = (a3 >> 24 | a4 << 8) & M30;
769 r->
v[5] = (a4 >> 22 | a5 << 10) & M30;
770 r->
v[6] = (a5 >> 20 | a6 << 12) & M30;
771 r->
v[7] = (a6 >> 18 | a7 << 14) & M30;
776 {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}},
813 return !(a->
d[0] & 1);
#define SECP256K1_CHECKMEM_CHECK_VERIFY(p, len)
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)
#define SECP256K1_SCALAR_VERIFY(r)
static SECP256K1_INLINE int secp256k1_scalar_is_even(const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_check_overflow(const secp256k1_scalar *a)
static SECP256K1_INLINE void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b, unsigned int shift)
static void secp256k1_scalar_half(secp256k1_scalar *r, const secp256k1_scalar *a)
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k)
#define extract(n)
Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits.
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow)
static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b)
static SECP256K1_INLINE uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x)
#define sumadd_fast(a)
Add a to the number defined by (c0,c1).
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar *a)
static SECP256K1_INLINE void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v)
static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x)
static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag)
#define extract_fast(n)
Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits.
#define muladd(a, b)
Add a*b to the number defined by (c0,c1,c2).
static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l)
static SECP256K1_INLINE int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b)
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
#define sumadd(a)
Add a to the number defined by (c0,c1,c2).
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag)
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b)
static const secp256k1_modinv32_modinfo secp256k1_const_modinfo_scalar
static SECP256K1_INLINE int secp256k1_scalar_reduce(secp256k1_scalar *r, uint32_t overflow)
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a)
static SECP256K1_INLINE int secp256k1_scalar_is_zero(const secp256k1_scalar *a)
static int secp256k1_scalar_is_high(const secp256k1_scalar *a)
static void secp256k1_scalar_from_signed30(secp256k1_scalar *r, const secp256k1_modinv32_signed30 *a)
static SECP256K1_INLINE uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count)
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag)
#define muladd_fast(a, b)
Add a*b to the number defined by (c0,c1).
static SECP256K1_INLINE int secp256k1_scalar_is_one(const secp256k1_scalar *a)
static void secp256k1_scalar_to_signed30(secp256k1_modinv32_signed30 *r, const secp256k1_scalar *a)
static SECP256K1_INLINE uint32_t secp256k1_read_be32(const unsigned char *p)
static SECP256K1_INLINE void secp256k1_write_be32(unsigned char *p, uint32_t x)
#define VERIFY_CHECK(cond)
A scalar modulo the group order of the secp256k1 curve.