7 #ifndef SECP256K1_SCALAR_REPR_IMPL_H
8 #define SECP256K1_SCALAR_REPR_IMPL_H
13 #define SECP256K1_N_0 ((uint32_t)0xD0364141UL)
14 #define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
15 #define SECP256K1_N_2 ((uint32_t)0xAF48A03BUL)
16 #define SECP256K1_N_3 ((uint32_t)0xBAAEDCE6UL)
17 #define SECP256K1_N_4 ((uint32_t)0xFFFFFFFEUL)
18 #define SECP256K1_N_5 ((uint32_t)0xFFFFFFFFUL)
19 #define SECP256K1_N_6 ((uint32_t)0xFFFFFFFFUL)
20 #define SECP256K1_N_7 ((uint32_t)0xFFFFFFFFUL)
23 #define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
24 #define SECP256K1_N_C_1 (~SECP256K1_N_1)
25 #define SECP256K1_N_C_2 (~SECP256K1_N_2)
26 #define SECP256K1_N_C_3 (~SECP256K1_N_3)
27 #define SECP256K1_N_C_4 (1)
30 #define SECP256K1_N_H_0 ((uint32_t)0x681B20A0UL)
31 #define SECP256K1_N_H_1 ((uint32_t)0xDFE92F46UL)
32 #define SECP256K1_N_H_2 ((uint32_t)0x57A4501DUL)
33 #define SECP256K1_N_H_3 ((uint32_t)0x5D576E73UL)
34 #define SECP256K1_N_H_4 ((uint32_t)0xFFFFFFFFUL)
35 #define SECP256K1_N_H_5 ((uint32_t)0xFFFFFFFFUL)
36 #define SECP256K1_N_H_6 ((uint32_t)0xFFFFFFFFUL)
37 #define SECP256K1_N_H_7 ((uint32_t)0x7FFFFFFFUL)
74 return (a->
d[offset >> 5] >> (offset & 0x1F)) & ((1 << count) - 1);
80 if ((offset + count - 1) >> 5 == offset >> 5) {
81 return secp256k1_scalar_get_bits(a, offset, count);
84 return ((a->
d[offset >> 5] >> (offset & 0x1F)) | (a->
d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & ((((uint32_t)1) << count) - 1);
110 r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
112 r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
114 r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
116 r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
118 r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
119 t += (uint64_t)
r->d[5];
120 r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
121 t += (uint64_t)
r->d[6];
122 r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
123 t += (uint64_t)
r->d[7];
124 r->d[7] = t & 0xFFFFFFFFUL;
130 uint64_t t = (uint64_t)a->
d[0] +
b->d[0];
131 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
132 t += (uint64_t)a->
d[1] +
b->d[1];
133 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
134 t += (uint64_t)a->
d[2] +
b->d[2];
135 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
136 t += (uint64_t)a->
d[3] +
b->d[3];
137 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
138 t += (uint64_t)a->
d[4] +
b->d[4];
139 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
140 t += (uint64_t)a->
d[5] +
b->d[5];
141 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
142 t += (uint64_t)a->
d[6] +
b->d[6];
143 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
144 t += (uint64_t)a->
d[7] +
b->d[7];
145 r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
146 overflow = t + secp256k1_scalar_check_overflow(
r);
148 secp256k1_scalar_reduce(
r, overflow);
152 static void secp256k1_scalar_cadd_bit(
secp256k1_scalar *
r,
unsigned int bit,
int flag) {
155 bit += ((uint32_t) flag - 1) & 0x100;
156 t = (uint64_t)
r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
157 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
158 t += (uint64_t)
r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
159 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
160 t += (uint64_t)
r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
161 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
162 t += (uint64_t)
r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
163 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
164 t += (uint64_t)
r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
165 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
166 t += (uint64_t)
r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
167 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
168 t += (uint64_t)
r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
169 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
170 t += (uint64_t)
r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
171 r->d[7] = t & 0xFFFFFFFFULL;
178 static void secp256k1_scalar_set_b32(
secp256k1_scalar *
r,
const unsigned char *b32,
int *overflow) {
180 r->d[0] = (uint32_t)b32[31] | (uint32_t)b32[30] << 8 | (uint32_t)b32[29] << 16 | (uint32_t)b32[28] << 24;
181 r->d[1] = (uint32_t)b32[27] | (uint32_t)b32[26] << 8 | (uint32_t)b32[25] << 16 | (uint32_t)b32[24] << 24;
182 r->d[2] = (uint32_t)b32[23] | (uint32_t)b32[22] << 8 | (uint32_t)b32[21] << 16 | (uint32_t)b32[20] << 24;
183 r->d[3] = (uint32_t)b32[19] | (uint32_t)b32[18] << 8 | (uint32_t)b32[17] << 16 | (uint32_t)b32[16] << 24;
184 r->d[4] = (uint32_t)b32[15] | (uint32_t)b32[14] << 8 | (uint32_t)b32[13] << 16 | (uint32_t)b32[12] << 24;
185 r->d[5] = (uint32_t)b32[11] | (uint32_t)b32[10] << 8 | (uint32_t)b32[9] << 16 | (uint32_t)b32[8] << 24;
186 r->d[6] = (uint32_t)b32[7] | (uint32_t)b32[6] << 8 | (uint32_t)b32[5] << 16 | (uint32_t)b32[4] << 24;
187 r->d[7] = (uint32_t)b32[3] | (uint32_t)b32[2] << 8 | (uint32_t)b32[1] << 16 | (uint32_t)b32[0] << 24;
188 over = secp256k1_scalar_reduce(
r, secp256k1_scalar_check_overflow(
r));
194 static void secp256k1_scalar_get_b32(
unsigned char *bin,
const secp256k1_scalar* a) {
195 bin[0] = a->
d[7] >> 24; bin[1] = a->
d[7] >> 16; bin[2] = a->
d[7] >> 8; bin[3] = a->
d[7];
196 bin[4] = a->
d[6] >> 24; bin[5] = a->
d[6] >> 16; bin[6] = a->
d[6] >> 8; bin[7] = a->
d[6];
197 bin[8] = a->
d[5] >> 24; bin[9] = a->
d[5] >> 16; bin[10] = a->
d[5] >> 8; bin[11] = a->
d[5];
198 bin[12] = a->
d[4] >> 24; bin[13] = a->
d[4] >> 16; bin[14] = a->
d[4] >> 8; bin[15] = a->
d[4];
199 bin[16] = a->
d[3] >> 24; bin[17] = a->
d[3] >> 16; bin[18] = a->
d[3] >> 8; bin[19] = a->
d[3];
200 bin[20] = a->
d[2] >> 24; bin[21] = a->
d[2] >> 16; bin[22] = a->
d[2] >> 8; bin[23] = a->
d[2];
201 bin[24] = a->
d[1] >> 24; bin[25] = a->
d[1] >> 16; bin[26] = a->
d[1] >> 8; bin[27] = a->
d[1];
202 bin[28] = a->
d[0] >> 24; bin[29] = a->
d[0] >> 16; bin[30] = a->
d[0] >> 8; bin[31] = a->
d[0];
206 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;
210 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
212 r->d[0] = t & nonzero; t >>= 32;
214 r->d[1] = t & nonzero; t >>= 32;
216 r->d[2] = t & nonzero; t >>= 32;
218 r->d[3] = t & nonzero; t >>= 32;
220 r->d[4] = t & nonzero; t >>= 32;
222 r->d[5] = t & nonzero; t >>= 32;
224 r->d[6] = t & nonzero; t >>= 32;
226 r->d[7] = t & nonzero;
230 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;
254 uint32_t mask = !flag - 1;
255 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(
r) == 0);
256 uint64_t t = (uint64_t)(
r->d[0] ^ mask) + ((
SECP256K1_N_0 + 1) & mask);
257 r->d[0] = t & nonzero; t >>= 32;
259 r->d[1] = t & nonzero; t >>= 32;
261 r->d[2] = t & nonzero; t >>= 32;
263 r->d[3] = t & nonzero; t >>= 32;
265 r->d[4] = t & nonzero; t >>= 32;
267 r->d[5] = t & nonzero; t >>= 32;
269 r->d[6] = t & nonzero; t >>= 32;
271 r->d[7] = t & nonzero;
272 return 2 * (mask == 0) - 1;
279 #define muladd(a,b) { \
282 uint64_t t = (uint64_t)a * b; \
287 th += (c0 < tl) ? 1 : 0; \
289 c2 += (c1 < th) ? 1 : 0; \
290 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
294 #define muladd_fast(a,b) { \
297 uint64_t t = (uint64_t)a * b; \
302 th += (c0 < tl) ? 1 : 0; \
304 VERIFY_CHECK(c1 >= th); \
308 #define muladd2(a,b) { \
309 uint32_t tl, th, th2, tl2; \
311 uint64_t t = (uint64_t)a * b; \
316 c2 += (th2 < th) ? 1 : 0; \
317 VERIFY_CHECK((th2 >= th) || (c2 != 0)); \
319 th2 += (tl2 < tl) ? 1 : 0; \
321 th2 += (c0 < tl2) ? 1 : 0; \
322 c2 += (c0 < tl2) & (th2 == 0); \
323 VERIFY_CHECK((c0 >= tl2) || (th2 != 0) || (c2 != 0)); \
325 c2 += (c1 < th2) ? 1 : 0; \
326 VERIFY_CHECK((c1 >= th2) || (c2 != 0)); \
330 #define sumadd(a) { \
333 over = (c0 < (a)) ? 1 : 0; \
335 c2 += (c1 < over) ? 1 : 0; \
339 #define sumadd_fast(a) { \
341 c1 += (c0 < (a)) ? 1 : 0; \
342 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
343 VERIFY_CHECK(c2 == 0); \
347 #define extract(n) { \
355 #define extract_fast(n) { \
359 VERIFY_CHECK(c2 == 0); \
362 static void secp256k1_scalar_reduce_512(
secp256k1_scalar *
r,
const uint32_t *l) {
364 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];
365 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
366 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
373 c0 = l[0]; c1 = 0; c2 = 0;
438 c0 = m0; c1 = 0; c2 = 0;
484 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
486 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
488 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
490 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
491 c += p4 + (uint64_t)p8;
492 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
494 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
496 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
498 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
501 secp256k1_scalar_reduce(
r, c + secp256k1_scalar_check_overflow(
r));
506 uint32_t c0 = 0, c1 = 0, c2 = 0;
592 static void secp256k1_scalar_sqr_512(uint32_t *l,
const secp256k1_scalar *a) {
594 uint32_t c0 = 0, c1 = 0, c2 = 0;
662 secp256k1_scalar_mul_512(l, a,
b);
663 secp256k1_scalar_reduce_512(
r, l);
670 ret =
r->d[0] & ((1 << n) - 1);
671 r->d[0] = (
r->d[0] >> n) + (
r->d[1] << (32 - n));
672 r->d[1] = (
r->d[1] >> n) + (
r->d[2] << (32 - n));
673 r->d[2] = (
r->d[2] >> n) + (
r->d[3] << (32 - n));
674 r->d[3] = (
r->d[3] >> n) + (
r->d[4] << (32 - n));
675 r->d[4] = (
r->d[4] >> n) + (
r->d[5] << (32 - n));
676 r->d[5] = (
r->d[5] >> n) + (
r->d[6] << (32 - n));
677 r->d[6] = (
r->d[6] >> n) + (
r->d[7] << (32 - n));
678 r->d[7] = (
r->d[7] >> n);
684 secp256k1_scalar_sqr_512(l, a);
685 secp256k1_scalar_reduce_512(
r, l);
688 #ifdef USE_ENDOMORPHISM
710 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;
715 unsigned int shiftlimbs;
716 unsigned int shiftlow;
717 unsigned int shifthigh;
719 secp256k1_scalar_mul_512(l, a,
b);
720 shiftlimbs = shift >> 5;
721 shiftlow = shift & 0x1F;
722 shifthigh = 32 - shiftlow;
723 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
724 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
725 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
726 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
727 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
728 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
729 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
730 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
731 secp256k1_scalar_cadd_bit(
r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
734 #define ROTL32(x,n) ((x) << (n) | (x) >> (32-(n)))
735 #define QUARTERROUND(a,b,c,d) \
736 a += b; d = ROTL32(d ^ a, 16); \
737 c += d; b = ROTL32(b ^ c, 12); \
738 a += b; d = ROTL32(d ^ a, 8); \
739 c += d; b = ROTL32(b ^ c, 7);
741 #ifdef WORDS_BIGENDIAN
742 #define LE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
745 #define BE32(p) ((((p) & 0xFF) << 24) | (((p) & 0xFF00) << 8) | (((p) & 0xFF0000) >> 8) | (((p) & 0xFF000000) >> 24))
751 size_t over_count = 0;
753 uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
756 memcpy((
void *) seed32, (
const void *) seed, 32);
762 x4 =
LE32(seed32[0]);
763 x5 =
LE32(seed32[1]);
764 x6 =
LE32(seed32[2]);
765 x7 =
LE32(seed32[3]);
766 x8 =
LE32(seed32[4]);
767 x9 =
LE32(seed32[5]);
768 x10 =
LE32(seed32[6]);
769 x11 =
LE32(seed32[7]);
791 x4 +=
LE32(seed32[0]);
792 x5 +=
LE32(seed32[1]);
793 x6 +=
LE32(seed32[2]);
794 x7 +=
LE32(seed32[3]);
795 x8 +=
LE32(seed32[4]);
796 x9 +=
LE32(seed32[5]);
797 x10 +=
LE32(seed32[6]);
798 x11 +=
LE32(seed32[7]);
814 r2->
d[5] =
BE32(x10);
815 r2->
d[4] =
BE32(x11);
816 r2->
d[3] =
BE32(x12);
817 r2->
d[2] =
BE32(x13);
818 r2->
d[1] =
BE32(x14);
819 r2->
d[0] =
BE32(x15);
821 over1 = secp256k1_scalar_check_overflow(r1);
822 over2 = secp256k1_scalar_check_overflow(r2);
824 }
while (over1 | over2);