scalar_8x32_impl.h
1 /*********************************************************************** 2 * Copyright (c) 2014 Pieter Wuille * 3 * Distributed under the MIT software license, see the accompanying * 4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.* 5 ***********************************************************************/ 6 7 #ifndef SECP256K1_SCALAR_REPR_IMPL_H 8 #define SECP256K1_SCALAR_REPR_IMPL_H 9 10 #include "checkmem.h" 11 #include "modinv32_impl.h" 12 #include "util.h" 13 14 /* Limbs of the secp256k1 order. */ 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) 23 24 /* Limbs of 2^256 minus the secp256k1 order. */ 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) 30 31 /* Limbs of half the secp256k1 order. */ 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) 40 41 SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v) { 42 r->d[0] = v; 43 r->d[1] = 0; 44 r->d[2] = 0; 45 r->d[3] = 0; 46 r->d[4] = 0; 47 r->d[5] = 0; 48 r->d[6] = 0; 49 r->d[7] = 0; 50 51 SECP256K1_SCALAR_VERIFY(r); 52 } 53 54 SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_limb32(const secp256k1_scalar *a, unsigned int offset, unsigned int count) { 55 SECP256K1_SCALAR_VERIFY(a); 56 VERIFY_CHECK(count > 0 && count <= 32); 57 VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5); 58 59 return (a->d[offset >> 5] >> (offset & 0x1F)) & (0xFFFFFFFF >> (32 - count)); 60 } 61 62 SECP256K1_INLINE static uint32_t secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) { 63 SECP256K1_SCALAR_VERIFY(a); 64 VERIFY_CHECK(count > 0 && count <= 32); 65 VERIFY_CHECK(offset + count <= 256); 66 67 if ((offset + count - 1) >> 5 == offset >> 5) { 68 return secp256k1_scalar_get_bits_limb32(a, offset, count); 69 } else { 70 VERIFY_CHECK((offset >> 5) + 1 < 8); 71 return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & (0xFFFFFFFF >> (32 - count)); 72 } 73 } 74 75 SECP256K1_INLINE static int secp256k1_scalar_check_overflow(const secp256k1_scalar *a) { 76 int yes = 0; 77 int no = 0; 78 no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */ 79 no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */ 80 no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */ 81 no |= (a->d[4] < SECP256K1_N_4); 82 yes |= (a->d[4] > SECP256K1_N_4) & ~no; 83 no |= (a->d[3] < SECP256K1_N_3) & ~yes; 84 yes |= (a->d[3] > SECP256K1_N_3) & ~no; 85 no |= (a->d[2] < SECP256K1_N_2) & ~yes; 86 yes |= (a->d[2] > SECP256K1_N_2) & ~no; 87 no |= (a->d[1] < SECP256K1_N_1) & ~yes; 88 yes |= (a->d[1] > SECP256K1_N_1) & ~no; 89 yes |= (a->d[0] >= SECP256K1_N_0) & ~no; 90 return yes; 91 } 92 93 SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, uint32_t overflow) { 94 uint64_t t; 95 VERIFY_CHECK(overflow <= 1); 96 97 t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0; 98 r->d[0] = t & 0xFFFFFFFFUL; t >>= 32; 99 t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1; 100 r->d[1] = t & 0xFFFFFFFFUL; t >>= 32; 101 t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2; 102 r->d[2] = t & 0xFFFFFFFFUL; t >>= 32; 103 t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3; 104 r->d[3] = t & 0xFFFFFFFFUL; t >>= 32; 105 t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4; 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; 113 114 SECP256K1_SCALAR_VERIFY(r); 115 return overflow; 116 } 117 118 static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) { 119 int overflow; 120 uint64_t t = (uint64_t)a->d[0] + b->d[0]; 121 SECP256K1_SCALAR_VERIFY(a); 122 SECP256K1_SCALAR_VERIFY(b); 123 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; 139 overflow = t + secp256k1_scalar_check_overflow(r); 140 VERIFY_CHECK(overflow == 0 || overflow == 1); 141 secp256k1_scalar_reduce(r, overflow); 142 143 SECP256K1_SCALAR_VERIFY(r); 144 return overflow; 145 } 146 147 static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) { 148 uint64_t t; 149 volatile int vflag = flag; 150 VERIFY_CHECK(flag == 0 || flag == 1); 151 SECP256K1_SCALAR_VERIFY(r); 152 VERIFY_CHECK(bit < 256); 153 154 bit += ((uint32_t) vflag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */ 155 t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F)); 156 r->d[0] = t & 0xFFFFFFFFULL; t >>= 32; 157 t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F)); 158 r->d[1] = t & 0xFFFFFFFFULL; t >>= 32; 159 t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F)); 160 r->d[2] = t & 0xFFFFFFFFULL; t >>= 32; 161 t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F)); 162 r->d[3] = t & 0xFFFFFFFFULL; t >>= 32; 163 t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F)); 164 r->d[4] = t & 0xFFFFFFFFULL; t >>= 32; 165 t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F)); 166 r->d[5] = t & 0xFFFFFFFFULL; t >>= 32; 167 t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F)); 168 r->d[6] = t & 0xFFFFFFFFULL; t >>= 32; 169 t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F)); 170 r->d[7] = t & 0xFFFFFFFFULL; 171 172 SECP256K1_SCALAR_VERIFY(r); 173 VERIFY_CHECK((t >> 32) == 0); 174 } 175 176 static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) { 177 int over; 178 r->d[0] = secp256k1_read_be32(&b32[28]); 179 r->d[1] = secp256k1_read_be32(&b32[24]); 180 r->d[2] = secp256k1_read_be32(&b32[20]); 181 r->d[3] = secp256k1_read_be32(&b32[16]); 182 r->d[4] = secp256k1_read_be32(&b32[12]); 183 r->d[5] = secp256k1_read_be32(&b32[8]); 184 r->d[6] = secp256k1_read_be32(&b32[4]); 185 r->d[7] = secp256k1_read_be32(&b32[0]); 186 over = secp256k1_scalar_reduce(r, secp256k1_scalar_check_overflow(r)); 187 if (overflow) { 188 *overflow = over; 189 } 190 191 SECP256K1_SCALAR_VERIFY(r); 192 } 193 194 static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) { 195 SECP256K1_SCALAR_VERIFY(a); 196 197 secp256k1_write_be32(&bin[0], a->d[7]); 198 secp256k1_write_be32(&bin[4], a->d[6]); 199 secp256k1_write_be32(&bin[8], a->d[5]); 200 secp256k1_write_be32(&bin[12], a->d[4]); 201 secp256k1_write_be32(&bin[16], a->d[3]); 202 secp256k1_write_be32(&bin[20], a->d[2]); 203 secp256k1_write_be32(&bin[24], a->d[1]); 204 secp256k1_write_be32(&bin[28], a->d[0]); 205 } 206 207 SECP256K1_INLINE static int secp256k1_scalar_is_zero(const secp256k1_scalar *a) { 208 SECP256K1_SCALAR_VERIFY(a); 209 210 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; 211 } 212 213 static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a) { 214 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0); 215 uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1; 216 SECP256K1_SCALAR_VERIFY(a); 217 218 r->d[0] = t & nonzero; t >>= 32; 219 t += (uint64_t)(~a->d[1]) + SECP256K1_N_1; 220 r->d[1] = t & nonzero; t >>= 32; 221 t += (uint64_t)(~a->d[2]) + SECP256K1_N_2; 222 r->d[2] = t & nonzero; t >>= 32; 223 t += (uint64_t)(~a->d[3]) + SECP256K1_N_3; 224 r->d[3] = t & nonzero; t >>= 32; 225 t += (uint64_t)(~a->d[4]) + SECP256K1_N_4; 226 r->d[4] = t & nonzero; t >>= 32; 227 t += (uint64_t)(~a->d[5]) + SECP256K1_N_5; 228 r->d[5] = t & nonzero; t >>= 32; 229 t += (uint64_t)(~a->d[6]) + SECP256K1_N_6; 230 r->d[6] = t & nonzero; t >>= 32; 231 t += (uint64_t)(~a->d[7]) + SECP256K1_N_7; 232 r->d[7] = t & nonzero; 233 234 SECP256K1_SCALAR_VERIFY(r); 235 } 236 237 static void secp256k1_scalar_half(secp256k1_scalar *r, const secp256k1_scalar *a) { 238 /* Writing `/` for field division and `//` for integer division, we compute 239 * 240 * a/2 = (a - (a&1))/2 + (a&1)/2 241 * = (a >> 1) + (a&1 ? 1/2 : 0) 242 * = (a >> 1) + (a&1 ? n//2+1 : 0), 243 * 244 * where n is the group order and in the last equality we have used 1/2 = n//2+1 (mod n). 245 * For n//2, we have the constants SECP256K1_N_H_0, ... 246 * 247 * This sum does not overflow. The most extreme case is a = -2, the largest odd scalar. Here: 248 * - the left summand is: a >> 1 = (a - a&1)/2 = (n-2-1)//2 = (n-3)//2 249 * - the right summand is: a&1 ? n//2+1 : 0 = n//2+1 = (n-1)//2 + 2//2 = (n+1)//2 250 * Together they sum to (n-3)//2 + (n+1)//2 = (2n-2)//2 = n - 1, which is less than n. 251 */ 252 uint32_t mask = -(uint32_t)(a->d[0] & 1U); 253 uint64_t t = (uint32_t)((a->d[0] >> 1) | (a->d[1] << 31)); 254 SECP256K1_SCALAR_VERIFY(a); 255 256 t += (SECP256K1_N_H_0 + 1U) & mask; 257 r->d[0] = t; t >>= 32; 258 t += (uint32_t)((a->d[1] >> 1) | (a->d[2] << 31)); 259 t += SECP256K1_N_H_1 & mask; 260 r->d[1] = t; t >>= 32; 261 t += (uint32_t)((a->d[2] >> 1) | (a->d[3] << 31)); 262 t += SECP256K1_N_H_2 & mask; 263 r->d[2] = t; t >>= 32; 264 t += (uint32_t)((a->d[3] >> 1) | (a->d[4] << 31)); 265 t += SECP256K1_N_H_3 & mask; 266 r->d[3] = t; t >>= 32; 267 t += (uint32_t)((a->d[4] >> 1) | (a->d[5] << 31)); 268 t += SECP256K1_N_H_4 & mask; 269 r->d[4] = t; t >>= 32; 270 t += (uint32_t)((a->d[5] >> 1) | (a->d[6] << 31)); 271 t += SECP256K1_N_H_5 & mask; 272 r->d[5] = t; t >>= 32; 273 t += (uint32_t)((a->d[6] >> 1) | (a->d[7] << 31)); 274 t += SECP256K1_N_H_6 & mask; 275 r->d[6] = t; t >>= 32; 276 r->d[7] = (uint32_t)t + (uint32_t)(a->d[7] >> 1) + (SECP256K1_N_H_7 & mask); 277 278 /* The line above only computed the bottom 32 bits of r->d[7]. Redo the computation 279 * in full 64 bits to make sure the top 32 bits are indeed zero. */ 280 VERIFY_CHECK((t + (a->d[7] >> 1) + (SECP256K1_N_H_7 & mask)) >> 32 == 0); 281 282 SECP256K1_SCALAR_VERIFY(r); 283 } 284 285 SECP256K1_INLINE static int secp256k1_scalar_is_one(const secp256k1_scalar *a) { 286 SECP256K1_SCALAR_VERIFY(a); 287 288 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; 289 } 290 291 static int secp256k1_scalar_is_high(const secp256k1_scalar *a) { 292 int yes = 0; 293 int no = 0; 294 SECP256K1_SCALAR_VERIFY(a); 295 296 no |= (a->d[7] < SECP256K1_N_H_7); 297 yes |= (a->d[7] > SECP256K1_N_H_7) & ~no; 298 no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */ 299 no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */ 300 no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */ 301 no |= (a->d[3] < SECP256K1_N_H_3) & ~yes; 302 yes |= (a->d[3] > SECP256K1_N_H_3) & ~no; 303 no |= (a->d[2] < SECP256K1_N_H_2) & ~yes; 304 yes |= (a->d[2] > SECP256K1_N_H_2) & ~no; 305 no |= (a->d[1] < SECP256K1_N_H_1) & ~yes; 306 yes |= (a->d[1] > SECP256K1_N_H_1) & ~no; 307 yes |= (a->d[0] > SECP256K1_N_H_0) & ~no; 308 return yes; 309 } 310 311 static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) { 312 /* If we are flag = 0, mask = 00...00 and this is a no-op; 313 * if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */ 314 volatile int vflag = flag; 315 uint32_t mask = -vflag; 316 uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0); 317 uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask); 318 VERIFY_CHECK(flag == 0 || flag == 1); 319 SECP256K1_SCALAR_VERIFY(r); 320 321 r->d[0] = t & nonzero; t >>= 32; 322 t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask); 323 r->d[1] = t & nonzero; t >>= 32; 324 t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask); 325 r->d[2] = t & nonzero; t >>= 32; 326 t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask); 327 r->d[3] = t & nonzero; t >>= 32; 328 t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask); 329 r->d[4] = t & nonzero; t >>= 32; 330 t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask); 331 r->d[5] = t & nonzero; t >>= 32; 332 t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask); 333 r->d[6] = t & nonzero; t >>= 32; 334 t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask); 335 r->d[7] = t & nonzero; 336 337 SECP256K1_SCALAR_VERIFY(r); 338 return 2 * (mask == 0) - 1; 339 } 340 341 342 /* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */ 343 344 /** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */ 345 #define muladd(a,b) { \ 346 uint32_t tl, th; \ 347 { \ 348 uint64_t t = (uint64_t)a * b; \ 349 th = t >> 32; /* at most 0xFFFFFFFE */ \ 350 tl = t; \ 351 } \ 352 c0 += tl; /* overflow is handled on the next line */ \ 353 th += (c0 < tl); /* at most 0xFFFFFFFF */ \ 354 c1 += th; /* overflow is handled on the next line */ \ 355 c2 += (c1 < th); /* never overflows by contract (verified in the next line) */ \ 356 VERIFY_CHECK((c1 >= th) || (c2 != 0)); \ 357 } 358 359 /** Add a*b to the number defined by (c0,c1). c1 must never overflow. */ 360 #define muladd_fast(a,b) { \ 361 uint32_t tl, th; \ 362 { \ 363 uint64_t t = (uint64_t)a * b; \ 364 th = t >> 32; /* at most 0xFFFFFFFE */ \ 365 tl = t; \ 366 } \ 367 c0 += tl; /* overflow is handled on the next line */ \ 368 th += (c0 < tl); /* at most 0xFFFFFFFF */ \ 369 c1 += th; /* never overflows by contract (verified in the next line) */ \ 370 VERIFY_CHECK(c1 >= th); \ 371 } 372 373 /** Add a to the number defined by (c0,c1,c2). c2 must never overflow. */ 374 #define sumadd(a) { \ 375 unsigned int over; \ 376 c0 += (a); /* overflow is handled on the next line */ \ 377 over = (c0 < (a)); \ 378 c1 += over; /* overflow is handled on the next line */ \ 379 c2 += (c1 < over); /* never overflows by contract */ \ 380 } 381 382 /** Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero. */ 383 #define sumadd_fast(a) { \ 384 c0 += (a); /* overflow is handled on the next line */ \ 385 c1 += (c0 < (a)); /* never overflows by contract (verified the next line) */ \ 386 VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \ 387 VERIFY_CHECK(c2 == 0); \ 388 } 389 390 /** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. */ 391 #define extract(n) { \ 392 (n) = c0; \ 393 c0 = c1; \ 394 c1 = c2; \ 395 c2 = 0; \ 396 } 397 398 /** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. c2 is required to be zero. */ 399 #define extract_fast(n) { \ 400 (n) = c0; \ 401 c0 = c1; \ 402 c1 = 0; \ 403 VERIFY_CHECK(c2 == 0); \ 404 } 405 406 static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) { 407 uint64_t c; 408 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]; 409 uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12; 410 uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8; 411 412 /* 96 bit accumulator. */ 413 uint32_t c0, c1, c2; 414 415 /* Reduce 512 bits into 385. */ 416 /* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */ 417 c0 = l[0]; c1 = 0; c2 = 0; 418 muladd_fast(n0, SECP256K1_N_C_0); 419 extract_fast(m0); 420 sumadd_fast(l[1]); 421 muladd(n1, SECP256K1_N_C_0); 422 muladd(n0, SECP256K1_N_C_1); 423 extract(m1); 424 sumadd(l[2]); 425 muladd(n2, SECP256K1_N_C_0); 426 muladd(n1, SECP256K1_N_C_1); 427 muladd(n0, SECP256K1_N_C_2); 428 extract(m2); 429 sumadd(l[3]); 430 muladd(n3, SECP256K1_N_C_0); 431 muladd(n2, SECP256K1_N_C_1); 432 muladd(n1, SECP256K1_N_C_2); 433 muladd(n0, SECP256K1_N_C_3); 434 extract(m3); 435 sumadd(l[4]); 436 muladd(n4, SECP256K1_N_C_0); 437 muladd(n3, SECP256K1_N_C_1); 438 muladd(n2, SECP256K1_N_C_2); 439 muladd(n1, SECP256K1_N_C_3); 440 sumadd(n0); 441 extract(m4); 442 sumadd(l[5]); 443 muladd(n5, SECP256K1_N_C_0); 444 muladd(n4, SECP256K1_N_C_1); 445 muladd(n3, SECP256K1_N_C_2); 446 muladd(n2, SECP256K1_N_C_3); 447 sumadd(n1); 448 extract(m5); 449 sumadd(l[6]); 450 muladd(n6, SECP256K1_N_C_0); 451 muladd(n5, SECP256K1_N_C_1); 452 muladd(n4, SECP256K1_N_C_2); 453 muladd(n3, SECP256K1_N_C_3); 454 sumadd(n2); 455 extract(m6); 456 sumadd(l[7]); 457 muladd(n7, SECP256K1_N_C_0); 458 muladd(n6, SECP256K1_N_C_1); 459 muladd(n5, SECP256K1_N_C_2); 460 muladd(n4, SECP256K1_N_C_3); 461 sumadd(n3); 462 extract(m7); 463 muladd(n7, SECP256K1_N_C_1); 464 muladd(n6, SECP256K1_N_C_2); 465 muladd(n5, SECP256K1_N_C_3); 466 sumadd(n4); 467 extract(m8); 468 muladd(n7, SECP256K1_N_C_2); 469 muladd(n6, SECP256K1_N_C_3); 470 sumadd(n5); 471 extract(m9); 472 muladd(n7, SECP256K1_N_C_3); 473 sumadd(n6); 474 extract(m10); 475 sumadd_fast(n7); 476 extract_fast(m11); 477 VERIFY_CHECK(c0 <= 1); 478 m12 = c0; 479 480 /* Reduce 385 bits into 258. */ 481 /* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */ 482 c0 = m0; c1 = 0; c2 = 0; 483 muladd_fast(m8, SECP256K1_N_C_0); 484 extract_fast(p0); 485 sumadd_fast(m1); 486 muladd(m9, SECP256K1_N_C_0); 487 muladd(m8, SECP256K1_N_C_1); 488 extract(p1); 489 sumadd(m2); 490 muladd(m10, SECP256K1_N_C_0); 491 muladd(m9, SECP256K1_N_C_1); 492 muladd(m8, SECP256K1_N_C_2); 493 extract(p2); 494 sumadd(m3); 495 muladd(m11, SECP256K1_N_C_0); 496 muladd(m10, SECP256K1_N_C_1); 497 muladd(m9, SECP256K1_N_C_2); 498 muladd(m8, SECP256K1_N_C_3); 499 extract(p3); 500 sumadd(m4); 501 muladd(m12, SECP256K1_N_C_0); 502 muladd(m11, SECP256K1_N_C_1); 503 muladd(m10, SECP256K1_N_C_2); 504 muladd(m9, SECP256K1_N_C_3); 505 sumadd(m8); 506 extract(p4); 507 sumadd(m5); 508 muladd(m12, SECP256K1_N_C_1); 509 muladd(m11, SECP256K1_N_C_2); 510 muladd(m10, SECP256K1_N_C_3); 511 sumadd(m9); 512 extract(p5); 513 sumadd(m6); 514 muladd(m12, SECP256K1_N_C_2); 515 muladd(m11, SECP256K1_N_C_3); 516 sumadd(m10); 517 extract(p6); 518 sumadd_fast(m7); 519 muladd_fast(m12, SECP256K1_N_C_3); 520 sumadd_fast(m11); 521 extract_fast(p7); 522 p8 = c0 + m12; 523 VERIFY_CHECK(p8 <= 2); 524 525 /* Reduce 258 bits into 256. */ 526 /* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */ 527 c = p0 + (uint64_t)SECP256K1_N_C_0 * p8; 528 r->d[0] = c & 0xFFFFFFFFUL; c >>= 32; 529 c += p1 + (uint64_t)SECP256K1_N_C_1 * p8; 530 r->d[1] = c & 0xFFFFFFFFUL; c >>= 32; 531 c += p2 + (uint64_t)SECP256K1_N_C_2 * p8; 532 r->d[2] = c & 0xFFFFFFFFUL; c >>= 32; 533 c += p3 + (uint64_t)SECP256K1_N_C_3 * p8; 534 r->d[3] = c & 0xFFFFFFFFUL; c >>= 32; 535 c += p4 + (uint64_t)p8; 536 r->d[4] = c & 0xFFFFFFFFUL; c >>= 32; 537 c += p5; 538 r->d[5] = c & 0xFFFFFFFFUL; c >>= 32; 539 c += p6; 540 r->d[6] = c & 0xFFFFFFFFUL; c >>= 32; 541 c += p7; 542 r->d[7] = c & 0xFFFFFFFFUL; c >>= 32; 543 544 /* Final reduction of r. */ 545 secp256k1_scalar_reduce(r, c + secp256k1_scalar_check_overflow(r)); 546 } 547 548 static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) { 549 /* 96 bit accumulator. */ 550 uint32_t c0 = 0, c1 = 0, c2 = 0; 551 552 /* l[0..15] = a[0..7] * b[0..7]. */ 553 muladd_fast(a->d[0], b->d[0]); 554 extract_fast(l[0]); 555 muladd(a->d[0], b->d[1]); 556 muladd(a->d[1], b->d[0]); 557 extract(l[1]); 558 muladd(a->d[0], b->d[2]); 559 muladd(a->d[1], b->d[1]); 560 muladd(a->d[2], b->d[0]); 561 extract(l[2]); 562 muladd(a->d[0], b->d[3]); 563 muladd(a->d[1], b->d[2]); 564 muladd(a->d[2], b->d[1]); 565 muladd(a->d[3], b->d[0]); 566 extract(l[3]); 567 muladd(a->d[0], b->d[4]); 568 muladd(a->d[1], b->d[3]); 569 muladd(a->d[2], b->d[2]); 570 muladd(a->d[3], b->d[1]); 571 muladd(a->d[4], b->d[0]); 572 extract(l[4]); 573 muladd(a->d[0], b->d[5]); 574 muladd(a->d[1], b->d[4]); 575 muladd(a->d[2], b->d[3]); 576 muladd(a->d[3], b->d[2]); 577 muladd(a->d[4], b->d[1]); 578 muladd(a->d[5], b->d[0]); 579 extract(l[5]); 580 muladd(a->d[0], b->d[6]); 581 muladd(a->d[1], b->d[5]); 582 muladd(a->d[2], b->d[4]); 583 muladd(a->d[3], b->d[3]); 584 muladd(a->d[4], b->d[2]); 585 muladd(a->d[5], b->d[1]); 586 muladd(a->d[6], b->d[0]); 587 extract(l[6]); 588 muladd(a->d[0], b->d[7]); 589 muladd(a->d[1], b->d[6]); 590 muladd(a->d[2], b->d[5]); 591 muladd(a->d[3], b->d[4]); 592 muladd(a->d[4], b->d[3]); 593 muladd(a->d[5], b->d[2]); 594 muladd(a->d[6], b->d[1]); 595 muladd(a->d[7], b->d[0]); 596 extract(l[7]); 597 muladd(a->d[1], b->d[7]); 598 muladd(a->d[2], b->d[6]); 599 muladd(a->d[3], b->d[5]); 600 muladd(a->d[4], b->d[4]); 601 muladd(a->d[5], b->d[3]); 602 muladd(a->d[6], b->d[2]); 603 muladd(a->d[7], b->d[1]); 604 extract(l[8]); 605 muladd(a->d[2], b->d[7]); 606 muladd(a->d[3], b->d[6]); 607 muladd(a->d[4], b->d[5]); 608 muladd(a->d[5], b->d[4]); 609 muladd(a->d[6], b->d[3]); 610 muladd(a->d[7], b->d[2]); 611 extract(l[9]); 612 muladd(a->d[3], b->d[7]); 613 muladd(a->d[4], b->d[6]); 614 muladd(a->d[5], b->d[5]); 615 muladd(a->d[6], b->d[4]); 616 muladd(a->d[7], b->d[3]); 617 extract(l[10]); 618 muladd(a->d[4], b->d[7]); 619 muladd(a->d[5], b->d[6]); 620 muladd(a->d[6], b->d[5]); 621 muladd(a->d[7], b->d[4]); 622 extract(l[11]); 623 muladd(a->d[5], b->d[7]); 624 muladd(a->d[6], b->d[6]); 625 muladd(a->d[7], b->d[5]); 626 extract(l[12]); 627 muladd(a->d[6], b->d[7]); 628 muladd(a->d[7], b->d[6]); 629 extract(l[13]); 630 muladd_fast(a->d[7], b->d[7]); 631 extract_fast(l[14]); 632 VERIFY_CHECK(c1 == 0); 633 l[15] = c0; 634 } 635 636 #undef sumadd 637 #undef sumadd_fast 638 #undef muladd 639 #undef muladd_fast 640 #undef extract 641 #undef extract_fast 642 643 static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) { 644 uint32_t l[16]; 645 SECP256K1_SCALAR_VERIFY(a); 646 SECP256K1_SCALAR_VERIFY(b); 647 648 secp256k1_scalar_mul_512(l, a, b); 649 secp256k1_scalar_reduce_512(r, l); 650 651 SECP256K1_SCALAR_VERIFY(r); 652 } 653 654 static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *k) { 655 SECP256K1_SCALAR_VERIFY(k); 656 657 r1->d[0] = k->d[0]; 658 r1->d[1] = k->d[1]; 659 r1->d[2] = k->d[2]; 660 r1->d[3] = k->d[3]; 661 r1->d[4] = 0; 662 r1->d[5] = 0; 663 r1->d[6] = 0; 664 r1->d[7] = 0; 665 r2->d[0] = k->d[4]; 666 r2->d[1] = k->d[5]; 667 r2->d[2] = k->d[6]; 668 r2->d[3] = k->d[7]; 669 r2->d[4] = 0; 670 r2->d[5] = 0; 671 r2->d[6] = 0; 672 r2->d[7] = 0; 673 674 SECP256K1_SCALAR_VERIFY(r1); 675 SECP256K1_SCALAR_VERIFY(r2); 676 } 677 678 SECP256K1_INLINE static int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b) { 679 SECP256K1_SCALAR_VERIFY(a); 680 SECP256K1_SCALAR_VERIFY(b); 681 682 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; 683 } 684 685 SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b, unsigned int shift) { 686 uint32_t l[16]; 687 unsigned int shiftlimbs; 688 unsigned int shiftlow; 689 unsigned int shifthigh; 690 SECP256K1_SCALAR_VERIFY(a); 691 SECP256K1_SCALAR_VERIFY(b); 692 VERIFY_CHECK(shift >= 256); 693 694 secp256k1_scalar_mul_512(l, a, b); 695 shiftlimbs = shift >> 5; 696 shiftlow = shift & 0x1F; 697 shifthigh = 32 - shiftlow; 698 r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0; 699 r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0; 700 r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0; 701 r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0; 702 r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0; 703 r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0; 704 r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0; 705 r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0; 706 secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1); 707 708 SECP256K1_SCALAR_VERIFY(r); 709 } 710 711 static SECP256K1_INLINE void secp256k1_scalar_cmov(secp256k1_scalar *r, const secp256k1_scalar *a, int flag) { 712 uint32_t mask0, mask1; 713 volatile int vflag = flag; 714 VERIFY_CHECK(flag == 0 || flag == 1); 715 SECP256K1_SCALAR_VERIFY(a); 716 SECP256K1_CHECKMEM_CHECK_VERIFY(r->d, sizeof(r->d)); 717 718 mask0 = vflag + ~((uint32_t)0); 719 mask1 = ~mask0; 720 r->d[0] = (r->d[0] & mask0) | (a->d[0] & mask1); 721 r->d[1] = (r->d[1] & mask0) | (a->d[1] & mask1); 722 r->d[2] = (r->d[2] & mask0) | (a->d[2] & mask1); 723 r->d[3] = (r->d[3] & mask0) | (a->d[3] & mask1); 724 r->d[4] = (r->d[4] & mask0) | (a->d[4] & mask1); 725 r->d[5] = (r->d[5] & mask0) | (a->d[5] & mask1); 726 r->d[6] = (r->d[6] & mask0) | (a->d[6] & mask1); 727 r->d[7] = (r->d[7] & mask0) | (a->d[7] & mask1); 728 729 SECP256K1_SCALAR_VERIFY(r); 730 } 731 732 static void secp256k1_scalar_from_signed30(secp256k1_scalar *r, const secp256k1_modinv32_signed30 *a) { 733 const uint32_t a0 = a->v[0], a1 = a->v[1], a2 = a->v[2], a3 = a->v[3], a4 = a->v[4], 734 a5 = a->v[5], a6 = a->v[6], a7 = a->v[7], a8 = a->v[8]; 735 736 /* The output from secp256k1_modinv32{_var} should be normalized to range [0,modulus), and 737 * have limbs in [0,2^30). The modulus is < 2^256, so the top limb must be below 2^(256-30*8). 738 */ 739 VERIFY_CHECK(a0 >> 30 == 0); 740 VERIFY_CHECK(a1 >> 30 == 0); 741 VERIFY_CHECK(a2 >> 30 == 0); 742 VERIFY_CHECK(a3 >> 30 == 0); 743 VERIFY_CHECK(a4 >> 30 == 0); 744 VERIFY_CHECK(a5 >> 30 == 0); 745 VERIFY_CHECK(a6 >> 30 == 0); 746 VERIFY_CHECK(a7 >> 30 == 0); 747 VERIFY_CHECK(a8 >> 16 == 0); 748 749 r->d[0] = a0 | a1 << 30; 750 r->d[1] = a1 >> 2 | a2 << 28; 751 r->d[2] = a2 >> 4 | a3 << 26; 752 r->d[3] = a3 >> 6 | a4 << 24; 753 r->d[4] = a4 >> 8 | a5 << 22; 754 r->d[5] = a5 >> 10 | a6 << 20; 755 r->d[6] = a6 >> 12 | a7 << 18; 756 r->d[7] = a7 >> 14 | a8 << 16; 757 758 SECP256K1_SCALAR_VERIFY(r); 759 } 760 761 static void secp256k1_scalar_to_signed30(secp256k1_modinv32_signed30 *r, const secp256k1_scalar *a) { 762 const uint32_t M30 = UINT32_MAX >> 2; 763 const uint32_t a0 = a->d[0], a1 = a->d[1], a2 = a->d[2], a3 = a->d[3], 764 a4 = a->d[4], a5 = a->d[5], a6 = a->d[6], a7 = a->d[7]; 765 SECP256K1_SCALAR_VERIFY(a); 766 767 r->v[0] = a0 & M30; 768 r->v[1] = (a0 >> 30 | a1 << 2) & M30; 769 r->v[2] = (a1 >> 28 | a2 << 4) & M30; 770 r->v[3] = (a2 >> 26 | a3 << 6) & M30; 771 r->v[4] = (a3 >> 24 | a4 << 8) & M30; 772 r->v[5] = (a4 >> 22 | a5 << 10) & M30; 773 r->v[6] = (a5 >> 20 | a6 << 12) & M30; 774 r->v[7] = (a6 >> 18 | a7 << 14) & M30; 775 r->v[8] = a7 >> 16; 776 } 777 778 static const secp256k1_modinv32_modinfo secp256k1_const_modinfo_scalar = { 779 {{0x10364141L, 0x3F497A33L, 0x348A03BBL, 0x2BB739ABL, -0x146L, 0, 0, 0, 65536}}, 780 0x2A774EC1L 781 }; 782 783 static void secp256k1_scalar_inverse(secp256k1_scalar *r, const secp256k1_scalar *x) { 784 secp256k1_modinv32_signed30 s; 785 #ifdef VERIFY 786 int zero_in = secp256k1_scalar_is_zero(x); 787 #endif 788 SECP256K1_SCALAR_VERIFY(x); 789 790 secp256k1_scalar_to_signed30(&s, x); 791 secp256k1_modinv32(&s, &secp256k1_const_modinfo_scalar); 792 secp256k1_scalar_from_signed30(r, &s); 793 794 SECP256K1_SCALAR_VERIFY(r); 795 VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in); 796 } 797 798 static void secp256k1_scalar_inverse_var(secp256k1_scalar *r, const secp256k1_scalar *x) { 799 secp256k1_modinv32_signed30 s; 800 #ifdef VERIFY 801 int zero_in = secp256k1_scalar_is_zero(x); 802 #endif 803 SECP256K1_SCALAR_VERIFY(x); 804 805 secp256k1_scalar_to_signed30(&s, x); 806 secp256k1_modinv32_var(&s, &secp256k1_const_modinfo_scalar); 807 secp256k1_scalar_from_signed30(r, &s); 808 809 SECP256K1_SCALAR_VERIFY(r); 810 VERIFY_CHECK(secp256k1_scalar_is_zero(r) == zero_in); 811 } 812 813 SECP256K1_INLINE static int secp256k1_scalar_is_even(const secp256k1_scalar *a) { 814 SECP256K1_SCALAR_VERIFY(a); 815 816 return !(a->d[0] & 1); 817 } 818 819 #endif /* SECP256K1_SCALAR_REPR_IMPL_H */