84973d3 Merge #454: Remove residual parts from the schnorr expirement. 5e95bf2 Remove residual parts from the schnorr expirement. cbc20b8 Merge #452: Minor optimizations to _scalar_inverse to save 4M 4cc8f52 Merge #437: Unroll secp256k1_fe_(get|set)_b32 to make them much faster. 465159c Further shorten the addition chain for scalar inversion. a2b6b19 Fix benchmark print_number infinite loop. 8b7680a Unroll secp256k1_fe_(get|set)_b32 for 10x26. aa84990 Unroll secp256k1_fe_(get|set)_b32 for 5x52. cf12fa1 Minor optimizations to _scalar_inverse to save 4M 1199492 Merge #408: Add `secp256k1_ec_pubkey_negate` and `secp256k1_ec_privkey_negate` 6af0871 Merge #441: secp256k1_context_randomize: document. ab31a52 Merge #444: test: Use checked_alloc eda5c1a Merge #449: Remove executable bit from secp256k1.c 51b77ae Remove executable bit from secp256k1.c 5eb030c test: Use checked_alloc 72d952c FIXUP: Missing "is" 70ff29b secp256k1_context_randomize: document. 9d560f9 Merge #428: Exhaustive recovery 8e48aa6 Add `secp256k1_ec_pubkey_negate` and `secp256k1_ec_privkey_negate` 2cee5fd exhaustive tests: add recovery module 8225239 Merge #433: Make the libcrypto detection fail the newer API. 12de863 Make the libcrypto detection fail the newer API. 678b0e5 exhaustive tests: remove erroneous comment from ecdsa_sig_sign 2928420 Merge #427: Remove Schnorr from travis as well 03ff8c2 group_impl.h: remove unused `secp256k1_ge_set_infinity` function a724d72 configure: add --enable-coverage to set options for coverage analysis b595163 recovery: add tests to cover API misusage 8eecc4a Remove Schnorr from travis as well 6f8ae2f ecdh: test NULL-checking of arguments 25e3cfb ecdsa_impl: replace scalar if-checks with VERIFY_CHECKs in ecdsa_sig_sign a8abae7 Merge #310: Add exhaustive test for group functions on a low-order subgroup b4ceedf Add exhaustive test for verification 83836a9 Add exhaustive tests for group arithmetic, signing, and ecmult on a small group 20b8877 Add exhaustive test for group functions on a low-order subgroup 80773a6 Merge #425: Remove Schnorr experiment e06e878 Remove Schnorr experiment 04c8ef3 Merge #407: Modify parameter order of internal functions to match API parameter order 6e06696 Merge #411: Remove guarantees about memcmp-ability 40c8d7e Merge #421: Update scalar_4x64_impl.h a922365 Merge #422: Restructure nonce clearing 3769783 Restructure nonce clearing 0f9e69d Restructure nonce clearing 9d67afa Update scalar_4x64_impl.h 7d15cd7 Merge #413: fix auto-enabled static precompuatation 00c5d2e fix auto-enabled static precompuatation 91219a1 Remove guarantees about memcmp-ability 7a49cac Merge #410: Add string.h include to ecmult_impl 0bbd5d4 Add string.h include to ecmult_impl 353c1bf Fix secp256k1_ge_set_table_gej_var parameter order 541b783 Fix secp256k1_ge_set_all_gej_var parameter order 7d893f4 Fix secp256k1_fe_inv_all_var parameter order c5b32e1 Merge #405: Make secp256k1_fe_sqrt constant time 926836a Make secp256k1_fe_sqrt constant time e2a8e92 Merge #404: Replace 3M + 4S doubling formula with 2M + 5S one 8ec49d8 Add note about 2M + 5S doubling formula 5a91bd7 Merge #400: A couple minor cleanups ac01378 build: add -DSECP256K1_BUILD to benchmark_internal build flags a6c6f99 Remove a bunch of unused stdlib #includes 65285a6 Merge #403: configure: add flag to disable OpenSSL tests a9b2a5d configure: add flag to disable OpenSSL tests b340123 Merge #402: Add support for testing quadratic residues e6e9805 Add function for testing quadratic residue field/group elements. efd953a Add Jacobi symbol test via GMP fa36a0d Merge #401: ecmult_const: unify endomorphism and non-endomorphism skew cases c6191fd ecmult_const: unify endomorphism and non-endomorphism skew cases 0b3e618 Merge #378: .gitignore build-aux cleanup 6042217 Merge #384: JNI: align shared files copyright/comments to bitcoinj's 24ad20f Merge #399: build: verify that the native compiler works for static precomp b3be852 Merge #398: Test whether ECDH and Schnorr are enabled for JNI aa0b1fd build: verify that the native compiler works for static precomp eee808d Test whether ECDH and Schnorr are enabled for JNI 7b0fb18 Merge #366: ARM assembly implementation of field_10x26 inner (rebase of #173) 001f176 ARM assembly implementation of field_10x26 inner 0172be9 Merge #397: Small fixes for sha256 3f8b78e Fix undefs in hash_impl.h 2ab4695 Fix state size in sha256 struct 6875b01 Merge #386: Add some missing `VERIFY_CHECK(ctx != NULL)` 2c52b5d Merge #389: Cast pointers through uintptr_t under JNI 43097a4 Merge #390: Update bitcoin-core GitHub links 31c9c12 Merge #391: JNI: Only call ecdsa_verify if its inputs parsed correctly 1cb2302 Merge #392: Add testcase which hits additional branch in secp256k1_scalar_sqr d2ee340 Merge #388: bench_ecdh: fix call to secp256k1_context_create 093a497 Add testcase which hits additional branch in secp256k1_scalar_sqr a40c701 JNI: Only call ecdsa_verify if its inputs parsed correctly faa2a11 Update bitcoin-core GitHub links 47b9e78 Cast pointers through uintptr_t under JNI f36f9c6 bench_ecdh: fix call to secp256k1_context_create bcc4881 Add some missing `VERIFY_CHECK(ctx != NULL)` for functions that use `ARG_CHECK` 6ceea2c align shared files copyright/comments to bitcoinj's 70141a8 Update .gitignore 7b549b1 Merge #373: build: fix x86_64 asm detection for some compilers bc7c93c Merge #374: Add note about y=0 being possible on one of the sextic twists e457018 Merge #364: JNI rebased 86e2d07 JNI library: cleanup, removed unimplemented code 3093576a JNI library bd2895f Merge pull request #371 e72e93a Add note about y=0 being possible on one of the sextic twists 3f8fdfb build: fix x86_64 asm detection for some compilers e5a9047 [Trivial] Remove double semicolons c18b869 Merge pull request #360 3026daa Merge pull request #302 03d4611 Add sage verification script for the group laws a965937 Merge pull request #361 83221ec Add experimental features to configure 5d4c5a3 Prevent damage_array in the signature test from going out of bounds. 419bf7f Merge pull request #356 6c527ec Merge pull request #357 445f7f1 Fix for Windows compile issue 03d84a4 Benchmark against OpenSSL verification 2bfb82b Merge pull request #351 06aeea5 Turn secp256k1_ec_pubkey_serialize outlen to in/out 970164d Merge pull request #348 6466625 Improvements for coordinate decompression e2100ad Merge pull request #347 8e48787 Change secp256k1_ec_pubkey_combine's count argument to size_t. c69dea0 Clear output in more cases for pubkey_combine, adds tests. 269d422 Comment copyediting. b4d17da Merge pull request #344 4709265 Merge pull request #345 26abce7 Adds 32 static test vectors for scalar mul, sqr, inv. 5b71a3f Better error case handling for pubkey_create & pubkey_serialize, more tests. 3b7bc69 Merge pull request #343 eed87af Change contrib/laxder from headers-only to files compilable as standalone C d7eb1ae Merge pull request #342 7914a6e Make lax_der_privatekey_parsing.h not depend on internal code 73f64ff Merge pull request #339 9234391 Overhaul flags handling 1a36898 Make flags more explicit, add runtime checks. 1a3e03a Merge pull request #340 96be204 Add additional tests for eckey and arg-checks. bb5aa4d Make the tweak function zeroize-output-on-fail behavior consistent. 4a243da Move secp256k1_ec_privkey_import/export to contrib. 1b3efc1 Move secp256k1_ecdsa_sig_recover into the recovery module. e3cd679 Eliminate all side-effects from VERIFY_CHECK() usage. b30fc85 Avoid nonce_function_rfc6979 algo16 argument emulation. 70d4640 Make secp256k1_ec_pubkey_create skip processing invalid secret keys. 6c476a8 Minor comment improvements. 131afe5 Merge pull request #334 0c6ab2f Introduce explicit lower-S normalization fea19e7 Add contrib/lax_der_parsing.h 3bb9c44 Rewrite ECDSA signature parsing code fa57f1b Use secp256k1_rand_int and secp256k1_rand_bits more 49b3749 Add new tests for the extra testrand functions f684d7d Faster secp256k1_rand_int implementation 251b1a6 Improve testrand: add extra random functions 31994c8 Merge pull request #338 f79aa88 Bugfix: swap arguments to noncefp c98df26 Merge pull request #319 67f7da4 Extensive interface and operations tests for secp256k1_ec_pubkey_parse. ee2cb40 Add ARG_CHECKs to secp256k1_ec_pubkey_parse/secp256k1_ec_pubkey_serialize 7450ef1 Merge pull request #328 68a3c76 Merge pull request #329 98135ee Merge pull request #332 37100d7 improve ECDH header-doc b13d749 Fix couple of typos in API comments 7c823e3 travis: fixup module configs cc3141a Merge pull request #325 ee58fae Merge pull request #326 213aa67 Do not force benchmarks to be statically linked. 338fc8b Add API exports to secp256k1_nonce_function_default and secp256k1_nonce_function_rfc6979. 52fd03f Merge pull request #320 9f6993f Remove some dead code. 357f8cd Merge pull request #314 118cd82 Use explicit symbol visibility. 4e64608 Include public module headers when compiling modules. 1f41437 Merge pull request #316 fe0d463 Merge pull request #317 cfe0ed9 Fix miscellaneous style nits that irritate overactive static analysis. 2b199de Use the explicit NULL macro for pointer comparisons. 9e90516 Merge pull request #294 dd891e0 Get rid of _t as it is POSIX reserved 201819b Merge pull request #313 912f203 Eliminate a few unbraced statements that crept into the code. eeab823 Merge pull request #299 486b9bb Use a flags bitfield for compressed option to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export 05732c5 Callback data: Accept pointers to either const or non-const data 1973c73 Bugfix: Reinitialise buffer lengths that have been used as outputs 788038d Use size_t for lengths (at least in external API) c9d7c2a secp256k1_context_set_{error,illegal}_callback: Restore default handler by passing NULL as function argument 9aac008 secp256k1_context_destroy: Allow NULL argument as a no-op 64b730b secp256k1_context_create: Use unsigned type for flags bitfield cb04ab5 Merge pull request #309 a551669 Merge pull request #295 81e45ff Update group_impl.h 85e3a2c Merge pull request #112 b2eb63b Merge pull request #293 dc0ce9f [API BREAK] Change argument order to out/outin/in 6d947ca Merge pull request #298 c822693 Merge pull request #301 6d04350 Merge pull request #303 7ab311c Merge pull request #304 5fb3229 Fixes a bug where bench_sign would fail due to passing in too small a buffer. 263dcbc remove unused assignment b183b41 bugfix: "ARG_CHECK(ctx != NULL)" makes no sense 6da1446 build: fix parallel build 5eb4356 Merge pull request #291 c996d53 Print success 9f443be Move pubkey recovery code to separate module d49abbd Separate ECDSA recovery tests 439d34a Separate recoverable and normal signatures a7b046e Merge pull request #289 f66907f Improve/reformat API documentation secp256k1.h 2f77487 Add context building benchmarks cc623d5 Merge pull request #287 de7e398 small typo fix 9d96e36 Merge pull request #280 432e1ce Merge pull request #283 14727fd Use correct name in gitignore 356b0e9 Actually test static precomputation in Travis ff3a5df Merge pull request #284 2587208 Merge pull request #212 a5a66c7 Add support for custom EC-Schnorr-SHA256 signatures d84a378 Merge pull request #252 72ae443 Improve perf. of cmov-based table lookup 92e53fc Implement endomorphism optimization for secp256k1_ecmult_const ed35d43 Make `secp256k1_scalar_add_bit` conditional; make `secp256k1_scalar_split_lambda_var` constant time 91c0ce9 Add benchmarks for ECDH and const-time multiplication 0739bbb Add ECDH module which works by hashing the output of ecmult_const 4401500 Add constant-time multiply `secp256k1_ecmult_const` for ECDH e4ce393 build: fix hard-coded usage of "gen_context" b8e39ac build: don't use BUILT_SOURCES for the static context header baa75da tests: add a couple tests ae4f0c6 Merge pull request #278 995c548 Introduce callback functions for dealing with errors. c333074 Merge pull request #282 18c329c Remove the internal secp256k1_ecdsa_sig_t type 74a2acd Add a secp256k1_ecdsa_signature_t type 23cfa91 Introduce secp256k1_pubkey_t type 4c63780 Merge pull request #269 3e6f1e2 Change rfc6979 implementation to be a generic PRNG ed5334a Update configure.ac to make it build on OpenBSD 1b68366 Merge pull request #274 a83bb48 Make ecmult static precomputation default 166b32f Merge pull request #276 c37812f Add gen_context src/ecmult_static_context.h to CLEANFILES to fix distclean. 125c15d Merge pull request #275 76f6769 Fix build with static ecmult altroot and make dist. 5133f78 Merge pull request #254 b0a60e6 Merge pull request #258 733c1e6 Add travis build to test the static context. fbecc38 Add ability to use a statically generated ecmult context. 4fb174d Merge pull request #263 4ab8990 Merge pull request #270 bdf0e0c Merge pull request #271 31d0c1f Merge pull request #273 eb2c8ff Add missing casts to SECP256K1_FE_CONST_INNER 55399c2 Further performance improvements to _ecmult_wnaf 99fd963 Add secp256k1_ec_pubkey_compress(), with test similar to the related decompress() function. 145cc6e Improve performance of _ecmult_wnaf 36b305a Verify the result of GMP modular inverse using non-GMP code 0cbc860 Merge pull request #266 06ff7fe Merge pull request #267 5a43124 Save 1 _fe_negate since s1 == -s2 a5d796e Update code comments 3f3964e Add specific VERIFY tests for _fe_cmov 7d054cd Refactor to save a _fe_negate b28d02a Refactor to remove a local var 55e7fc3 Perf. improvement in _gej_add_ge a0601cd Fix VERIFY calculations in _fe_cmov methods 17f7148 Merge pull request #261 7657420 Add tests for adding P+Q with P.x!=Q.x and P.y=-Q.y 8c5d5f7 tests: Add failing unit test for #257 (bad addition formula) 5de4c5d gej_add_ge: fix degenerate case when computing P + (-lambda)P bcf2fcf gej_add_ge: rearrange algebra e2a07c7 Fix compilation with C++ 873a453 Merge pull request #250 91eb0da Merge pull request #247 210ffed Use separate in and out pointers in `secp256k1_ec_pubkey_decompress` a1d5ae1 Tiny optimization 729badf Merge pull request #210 2d5a186 Apply effective-affine trick to precomp 4f9791a Effective affine addition in EC multiplication 2b4cf41 Use pkg-config always when possible, with failover to manual checks for libcrypto git-subtree-dir: src/secp256k1 git-subtree-split: 84973d393ac240a90b2e1a6538c5368202bc2224
722 lines
26 KiB
C
722 lines
26 KiB
C
/**********************************************************************
|
|
* Copyright (c) 2014 Pieter Wuille *
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
* file COPYING or http://www.opensource.org/licenses/mit-license.php.*
|
|
**********************************************************************/
|
|
|
|
#ifndef _SECP256K1_SCALAR_REPR_IMPL_H_
|
|
#define _SECP256K1_SCALAR_REPR_IMPL_H_
|
|
|
|
/* Limbs of the secp256k1 order. */
|
|
#define SECP256K1_N_0 ((uint32_t)0xD0364141UL)
|
|
#define SECP256K1_N_1 ((uint32_t)0xBFD25E8CUL)
|
|
#define SECP256K1_N_2 ((uint32_t)0xAF48A03BUL)
|
|
#define SECP256K1_N_3 ((uint32_t)0xBAAEDCE6UL)
|
|
#define SECP256K1_N_4 ((uint32_t)0xFFFFFFFEUL)
|
|
#define SECP256K1_N_5 ((uint32_t)0xFFFFFFFFUL)
|
|
#define SECP256K1_N_6 ((uint32_t)0xFFFFFFFFUL)
|
|
#define SECP256K1_N_7 ((uint32_t)0xFFFFFFFFUL)
|
|
|
|
/* Limbs of 2^256 minus the secp256k1 order. */
|
|
#define SECP256K1_N_C_0 (~SECP256K1_N_0 + 1)
|
|
#define SECP256K1_N_C_1 (~SECP256K1_N_1)
|
|
#define SECP256K1_N_C_2 (~SECP256K1_N_2)
|
|
#define SECP256K1_N_C_3 (~SECP256K1_N_3)
|
|
#define SECP256K1_N_C_4 (1)
|
|
|
|
/* Limbs of half the secp256k1 order. */
|
|
#define SECP256K1_N_H_0 ((uint32_t)0x681B20A0UL)
|
|
#define SECP256K1_N_H_1 ((uint32_t)0xDFE92F46UL)
|
|
#define SECP256K1_N_H_2 ((uint32_t)0x57A4501DUL)
|
|
#define SECP256K1_N_H_3 ((uint32_t)0x5D576E73UL)
|
|
#define SECP256K1_N_H_4 ((uint32_t)0xFFFFFFFFUL)
|
|
#define SECP256K1_N_H_5 ((uint32_t)0xFFFFFFFFUL)
|
|
#define SECP256K1_N_H_6 ((uint32_t)0xFFFFFFFFUL)
|
|
#define SECP256K1_N_H_7 ((uint32_t)0x7FFFFFFFUL)
|
|
|
|
SECP256K1_INLINE static void secp256k1_scalar_clear(secp256k1_scalar *r) {
|
|
r->d[0] = 0;
|
|
r->d[1] = 0;
|
|
r->d[2] = 0;
|
|
r->d[3] = 0;
|
|
r->d[4] = 0;
|
|
r->d[5] = 0;
|
|
r->d[6] = 0;
|
|
r->d[7] = 0;
|
|
}
|
|
|
|
SECP256K1_INLINE static void secp256k1_scalar_set_int(secp256k1_scalar *r, unsigned int v) {
|
|
r->d[0] = v;
|
|
r->d[1] = 0;
|
|
r->d[2] = 0;
|
|
r->d[3] = 0;
|
|
r->d[4] = 0;
|
|
r->d[5] = 0;
|
|
r->d[6] = 0;
|
|
r->d[7] = 0;
|
|
}
|
|
|
|
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
|
|
VERIFY_CHECK((offset + count - 1) >> 5 == offset >> 5);
|
|
return (a->d[offset >> 5] >> (offset & 0x1F)) & ((1 << count) - 1);
|
|
}
|
|
|
|
SECP256K1_INLINE static unsigned int secp256k1_scalar_get_bits_var(const secp256k1_scalar *a, unsigned int offset, unsigned int count) {
|
|
VERIFY_CHECK(count < 32);
|
|
VERIFY_CHECK(offset + count <= 256);
|
|
if ((offset + count - 1) >> 5 == offset >> 5) {
|
|
return secp256k1_scalar_get_bits(a, offset, count);
|
|
} else {
|
|
VERIFY_CHECK((offset >> 5) + 1 < 8);
|
|
return ((a->d[offset >> 5] >> (offset & 0x1F)) | (a->d[(offset >> 5) + 1] << (32 - (offset & 0x1F)))) & ((((uint32_t)1) << count) - 1);
|
|
}
|
|
}
|
|
|
|
SECP256K1_INLINE static int secp256k1_scalar_check_overflow(const secp256k1_scalar *a) {
|
|
int yes = 0;
|
|
int no = 0;
|
|
no |= (a->d[7] < SECP256K1_N_7); /* No need for a > check. */
|
|
no |= (a->d[6] < SECP256K1_N_6); /* No need for a > check. */
|
|
no |= (a->d[5] < SECP256K1_N_5); /* No need for a > check. */
|
|
no |= (a->d[4] < SECP256K1_N_4);
|
|
yes |= (a->d[4] > SECP256K1_N_4) & ~no;
|
|
no |= (a->d[3] < SECP256K1_N_3) & ~yes;
|
|
yes |= (a->d[3] > SECP256K1_N_3) & ~no;
|
|
no |= (a->d[2] < SECP256K1_N_2) & ~yes;
|
|
yes |= (a->d[2] > SECP256K1_N_2) & ~no;
|
|
no |= (a->d[1] < SECP256K1_N_1) & ~yes;
|
|
yes |= (a->d[1] > SECP256K1_N_1) & ~no;
|
|
yes |= (a->d[0] >= SECP256K1_N_0) & ~no;
|
|
return yes;
|
|
}
|
|
|
|
SECP256K1_INLINE static int secp256k1_scalar_reduce(secp256k1_scalar *r, uint32_t overflow) {
|
|
uint64_t t;
|
|
VERIFY_CHECK(overflow <= 1);
|
|
t = (uint64_t)r->d[0] + overflow * SECP256K1_N_C_0;
|
|
r->d[0] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[1] + overflow * SECP256K1_N_C_1;
|
|
r->d[1] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[2] + overflow * SECP256K1_N_C_2;
|
|
r->d[2] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[3] + overflow * SECP256K1_N_C_3;
|
|
r->d[3] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[4] + overflow * SECP256K1_N_C_4;
|
|
r->d[4] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[5];
|
|
r->d[5] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[6];
|
|
r->d[6] = t & 0xFFFFFFFFUL; t >>= 32;
|
|
t += (uint64_t)r->d[7];
|
|
r->d[7] = t & 0xFFFFFFFFUL;
|
|
return overflow;
|
|
}
|
|
|
|
static int secp256k1_scalar_add(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
|
|
int overflow;
|
|
uint64_t t = (uint64_t)a->d[0] + b->d[0];
|
|
r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[1] + b->d[1];
|
|
r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[2] + b->d[2];
|
|
r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[3] + b->d[3];
|
|
r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[4] + b->d[4];
|
|
r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[5] + b->d[5];
|
|
r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[6] + b->d[6];
|
|
r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)a->d[7] + b->d[7];
|
|
r->d[7] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
overflow = t + secp256k1_scalar_check_overflow(r);
|
|
VERIFY_CHECK(overflow == 0 || overflow == 1);
|
|
secp256k1_scalar_reduce(r, overflow);
|
|
return overflow;
|
|
}
|
|
|
|
static void secp256k1_scalar_cadd_bit(secp256k1_scalar *r, unsigned int bit, int flag) {
|
|
uint64_t t;
|
|
VERIFY_CHECK(bit < 256);
|
|
bit += ((uint32_t) flag - 1) & 0x100; /* forcing (bit >> 5) > 7 makes this a noop */
|
|
t = (uint64_t)r->d[0] + (((uint32_t)((bit >> 5) == 0)) << (bit & 0x1F));
|
|
r->d[0] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[1] + (((uint32_t)((bit >> 5) == 1)) << (bit & 0x1F));
|
|
r->d[1] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[2] + (((uint32_t)((bit >> 5) == 2)) << (bit & 0x1F));
|
|
r->d[2] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[3] + (((uint32_t)((bit >> 5) == 3)) << (bit & 0x1F));
|
|
r->d[3] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[4] + (((uint32_t)((bit >> 5) == 4)) << (bit & 0x1F));
|
|
r->d[4] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[5] + (((uint32_t)((bit >> 5) == 5)) << (bit & 0x1F));
|
|
r->d[5] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[6] + (((uint32_t)((bit >> 5) == 6)) << (bit & 0x1F));
|
|
r->d[6] = t & 0xFFFFFFFFULL; t >>= 32;
|
|
t += (uint64_t)r->d[7] + (((uint32_t)((bit >> 5) == 7)) << (bit & 0x1F));
|
|
r->d[7] = t & 0xFFFFFFFFULL;
|
|
#ifdef VERIFY
|
|
VERIFY_CHECK((t >> 32) == 0);
|
|
VERIFY_CHECK(secp256k1_scalar_check_overflow(r) == 0);
|
|
#endif
|
|
}
|
|
|
|
static void secp256k1_scalar_set_b32(secp256k1_scalar *r, const unsigned char *b32, int *overflow) {
|
|
int over;
|
|
r->d[0] = (uint32_t)b32[31] | (uint32_t)b32[30] << 8 | (uint32_t)b32[29] << 16 | (uint32_t)b32[28] << 24;
|
|
r->d[1] = (uint32_t)b32[27] | (uint32_t)b32[26] << 8 | (uint32_t)b32[25] << 16 | (uint32_t)b32[24] << 24;
|
|
r->d[2] = (uint32_t)b32[23] | (uint32_t)b32[22] << 8 | (uint32_t)b32[21] << 16 | (uint32_t)b32[20] << 24;
|
|
r->d[3] = (uint32_t)b32[19] | (uint32_t)b32[18] << 8 | (uint32_t)b32[17] << 16 | (uint32_t)b32[16] << 24;
|
|
r->d[4] = (uint32_t)b32[15] | (uint32_t)b32[14] << 8 | (uint32_t)b32[13] << 16 | (uint32_t)b32[12] << 24;
|
|
r->d[5] = (uint32_t)b32[11] | (uint32_t)b32[10] << 8 | (uint32_t)b32[9] << 16 | (uint32_t)b32[8] << 24;
|
|
r->d[6] = (uint32_t)b32[7] | (uint32_t)b32[6] << 8 | (uint32_t)b32[5] << 16 | (uint32_t)b32[4] << 24;
|
|
r->d[7] = (uint32_t)b32[3] | (uint32_t)b32[2] << 8 | (uint32_t)b32[1] << 16 | (uint32_t)b32[0] << 24;
|
|
over = secp256k1_scalar_reduce(r, secp256k1_scalar_check_overflow(r));
|
|
if (overflow) {
|
|
*overflow = over;
|
|
}
|
|
}
|
|
|
|
static void secp256k1_scalar_get_b32(unsigned char *bin, const secp256k1_scalar* a) {
|
|
bin[0] = a->d[7] >> 24; bin[1] = a->d[7] >> 16; bin[2] = a->d[7] >> 8; bin[3] = a->d[7];
|
|
bin[4] = a->d[6] >> 24; bin[5] = a->d[6] >> 16; bin[6] = a->d[6] >> 8; bin[7] = a->d[6];
|
|
bin[8] = a->d[5] >> 24; bin[9] = a->d[5] >> 16; bin[10] = a->d[5] >> 8; bin[11] = a->d[5];
|
|
bin[12] = a->d[4] >> 24; bin[13] = a->d[4] >> 16; bin[14] = a->d[4] >> 8; bin[15] = a->d[4];
|
|
bin[16] = a->d[3] >> 24; bin[17] = a->d[3] >> 16; bin[18] = a->d[3] >> 8; bin[19] = a->d[3];
|
|
bin[20] = a->d[2] >> 24; bin[21] = a->d[2] >> 16; bin[22] = a->d[2] >> 8; bin[23] = a->d[2];
|
|
bin[24] = a->d[1] >> 24; bin[25] = a->d[1] >> 16; bin[26] = a->d[1] >> 8; bin[27] = a->d[1];
|
|
bin[28] = a->d[0] >> 24; bin[29] = a->d[0] >> 16; bin[30] = a->d[0] >> 8; bin[31] = a->d[0];
|
|
}
|
|
|
|
SECP256K1_INLINE static int secp256k1_scalar_is_zero(const secp256k1_scalar *a) {
|
|
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;
|
|
}
|
|
|
|
static void secp256k1_scalar_negate(secp256k1_scalar *r, const secp256k1_scalar *a) {
|
|
uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(a) == 0);
|
|
uint64_t t = (uint64_t)(~a->d[0]) + SECP256K1_N_0 + 1;
|
|
r->d[0] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[1]) + SECP256K1_N_1;
|
|
r->d[1] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[2]) + SECP256K1_N_2;
|
|
r->d[2] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[3]) + SECP256K1_N_3;
|
|
r->d[3] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[4]) + SECP256K1_N_4;
|
|
r->d[4] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[5]) + SECP256K1_N_5;
|
|
r->d[5] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[6]) + SECP256K1_N_6;
|
|
r->d[6] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(~a->d[7]) + SECP256K1_N_7;
|
|
r->d[7] = t & nonzero;
|
|
}
|
|
|
|
SECP256K1_INLINE static int secp256k1_scalar_is_one(const secp256k1_scalar *a) {
|
|
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;
|
|
}
|
|
|
|
static int secp256k1_scalar_is_high(const secp256k1_scalar *a) {
|
|
int yes = 0;
|
|
int no = 0;
|
|
no |= (a->d[7] < SECP256K1_N_H_7);
|
|
yes |= (a->d[7] > SECP256K1_N_H_7) & ~no;
|
|
no |= (a->d[6] < SECP256K1_N_H_6) & ~yes; /* No need for a > check. */
|
|
no |= (a->d[5] < SECP256K1_N_H_5) & ~yes; /* No need for a > check. */
|
|
no |= (a->d[4] < SECP256K1_N_H_4) & ~yes; /* No need for a > check. */
|
|
no |= (a->d[3] < SECP256K1_N_H_3) & ~yes;
|
|
yes |= (a->d[3] > SECP256K1_N_H_3) & ~no;
|
|
no |= (a->d[2] < SECP256K1_N_H_2) & ~yes;
|
|
yes |= (a->d[2] > SECP256K1_N_H_2) & ~no;
|
|
no |= (a->d[1] < SECP256K1_N_H_1) & ~yes;
|
|
yes |= (a->d[1] > SECP256K1_N_H_1) & ~no;
|
|
yes |= (a->d[0] > SECP256K1_N_H_0) & ~no;
|
|
return yes;
|
|
}
|
|
|
|
static int secp256k1_scalar_cond_negate(secp256k1_scalar *r, int flag) {
|
|
/* If we are flag = 0, mask = 00...00 and this is a no-op;
|
|
* if we are flag = 1, mask = 11...11 and this is identical to secp256k1_scalar_negate */
|
|
uint32_t mask = !flag - 1;
|
|
uint32_t nonzero = 0xFFFFFFFFUL * (secp256k1_scalar_is_zero(r) == 0);
|
|
uint64_t t = (uint64_t)(r->d[0] ^ mask) + ((SECP256K1_N_0 + 1) & mask);
|
|
r->d[0] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[1] ^ mask) + (SECP256K1_N_1 & mask);
|
|
r->d[1] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[2] ^ mask) + (SECP256K1_N_2 & mask);
|
|
r->d[2] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[3] ^ mask) + (SECP256K1_N_3 & mask);
|
|
r->d[3] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[4] ^ mask) + (SECP256K1_N_4 & mask);
|
|
r->d[4] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[5] ^ mask) + (SECP256K1_N_5 & mask);
|
|
r->d[5] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[6] ^ mask) + (SECP256K1_N_6 & mask);
|
|
r->d[6] = t & nonzero; t >>= 32;
|
|
t += (uint64_t)(r->d[7] ^ mask) + (SECP256K1_N_7 & mask);
|
|
r->d[7] = t & nonzero;
|
|
return 2 * (mask == 0) - 1;
|
|
}
|
|
|
|
|
|
/* Inspired by the macros in OpenSSL's crypto/bn/asm/x86_64-gcc.c. */
|
|
|
|
/** Add a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
|
|
#define muladd(a,b) { \
|
|
uint32_t tl, th; \
|
|
{ \
|
|
uint64_t t = (uint64_t)a * b; \
|
|
th = t >> 32; /* at most 0xFFFFFFFE */ \
|
|
tl = t; \
|
|
} \
|
|
c0 += tl; /* overflow is handled on the next line */ \
|
|
th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
|
|
c1 += th; /* overflow is handled on the next line */ \
|
|
c2 += (c1 < th) ? 1 : 0; /* never overflows by contract (verified in the next line) */ \
|
|
VERIFY_CHECK((c1 >= th) || (c2 != 0)); \
|
|
}
|
|
|
|
/** Add a*b to the number defined by (c0,c1). c1 must never overflow. */
|
|
#define muladd_fast(a,b) { \
|
|
uint32_t tl, th; \
|
|
{ \
|
|
uint64_t t = (uint64_t)a * b; \
|
|
th = t >> 32; /* at most 0xFFFFFFFE */ \
|
|
tl = t; \
|
|
} \
|
|
c0 += tl; /* overflow is handled on the next line */ \
|
|
th += (c0 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
|
|
c1 += th; /* never overflows by contract (verified in the next line) */ \
|
|
VERIFY_CHECK(c1 >= th); \
|
|
}
|
|
|
|
/** Add 2*a*b to the number defined by (c0,c1,c2). c2 must never overflow. */
|
|
#define muladd2(a,b) { \
|
|
uint32_t tl, th, th2, tl2; \
|
|
{ \
|
|
uint64_t t = (uint64_t)a * b; \
|
|
th = t >> 32; /* at most 0xFFFFFFFE */ \
|
|
tl = t; \
|
|
} \
|
|
th2 = th + th; /* at most 0xFFFFFFFE (in case th was 0x7FFFFFFF) */ \
|
|
c2 += (th2 < th) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
|
|
VERIFY_CHECK((th2 >= th) || (c2 != 0)); \
|
|
tl2 = tl + tl; /* at most 0xFFFFFFFE (in case the lowest 63 bits of tl were 0x7FFFFFFF) */ \
|
|
th2 += (tl2 < tl) ? 1 : 0; /* at most 0xFFFFFFFF */ \
|
|
c0 += tl2; /* overflow is handled on the next line */ \
|
|
th2 += (c0 < tl2) ? 1 : 0; /* second overflow is handled on the next line */ \
|
|
c2 += (c0 < tl2) & (th2 == 0); /* never overflows by contract (verified the next line) */ \
|
|
VERIFY_CHECK((c0 >= tl2) || (th2 != 0) || (c2 != 0)); \
|
|
c1 += th2; /* overflow is handled on the next line */ \
|
|
c2 += (c1 < th2) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
|
|
VERIFY_CHECK((c1 >= th2) || (c2 != 0)); \
|
|
}
|
|
|
|
/** Add a to the number defined by (c0,c1,c2). c2 must never overflow. */
|
|
#define sumadd(a) { \
|
|
unsigned int over; \
|
|
c0 += (a); /* overflow is handled on the next line */ \
|
|
over = (c0 < (a)) ? 1 : 0; \
|
|
c1 += over; /* overflow is handled on the next line */ \
|
|
c2 += (c1 < over) ? 1 : 0; /* never overflows by contract */ \
|
|
}
|
|
|
|
/** Add a to the number defined by (c0,c1). c1 must never overflow, c2 must be zero. */
|
|
#define sumadd_fast(a) { \
|
|
c0 += (a); /* overflow is handled on the next line */ \
|
|
c1 += (c0 < (a)) ? 1 : 0; /* never overflows by contract (verified the next line) */ \
|
|
VERIFY_CHECK((c1 != 0) | (c0 >= (a))); \
|
|
VERIFY_CHECK(c2 == 0); \
|
|
}
|
|
|
|
/** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. */
|
|
#define extract(n) { \
|
|
(n) = c0; \
|
|
c0 = c1; \
|
|
c1 = c2; \
|
|
c2 = 0; \
|
|
}
|
|
|
|
/** Extract the lowest 32 bits of (c0,c1,c2) into n, and left shift the number 32 bits. c2 is required to be zero. */
|
|
#define extract_fast(n) { \
|
|
(n) = c0; \
|
|
c0 = c1; \
|
|
c1 = 0; \
|
|
VERIFY_CHECK(c2 == 0); \
|
|
}
|
|
|
|
static void secp256k1_scalar_reduce_512(secp256k1_scalar *r, const uint32_t *l) {
|
|
uint64_t c;
|
|
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];
|
|
uint32_t m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12;
|
|
uint32_t p0, p1, p2, p3, p4, p5, p6, p7, p8;
|
|
|
|
/* 96 bit accumulator. */
|
|
uint32_t c0, c1, c2;
|
|
|
|
/* Reduce 512 bits into 385. */
|
|
/* m[0..12] = l[0..7] + n[0..7] * SECP256K1_N_C. */
|
|
c0 = l[0]; c1 = 0; c2 = 0;
|
|
muladd_fast(n0, SECP256K1_N_C_0);
|
|
extract_fast(m0);
|
|
sumadd_fast(l[1]);
|
|
muladd(n1, SECP256K1_N_C_0);
|
|
muladd(n0, SECP256K1_N_C_1);
|
|
extract(m1);
|
|
sumadd(l[2]);
|
|
muladd(n2, SECP256K1_N_C_0);
|
|
muladd(n1, SECP256K1_N_C_1);
|
|
muladd(n0, SECP256K1_N_C_2);
|
|
extract(m2);
|
|
sumadd(l[3]);
|
|
muladd(n3, SECP256K1_N_C_0);
|
|
muladd(n2, SECP256K1_N_C_1);
|
|
muladd(n1, SECP256K1_N_C_2);
|
|
muladd(n0, SECP256K1_N_C_3);
|
|
extract(m3);
|
|
sumadd(l[4]);
|
|
muladd(n4, SECP256K1_N_C_0);
|
|
muladd(n3, SECP256K1_N_C_1);
|
|
muladd(n2, SECP256K1_N_C_2);
|
|
muladd(n1, SECP256K1_N_C_3);
|
|
sumadd(n0);
|
|
extract(m4);
|
|
sumadd(l[5]);
|
|
muladd(n5, SECP256K1_N_C_0);
|
|
muladd(n4, SECP256K1_N_C_1);
|
|
muladd(n3, SECP256K1_N_C_2);
|
|
muladd(n2, SECP256K1_N_C_3);
|
|
sumadd(n1);
|
|
extract(m5);
|
|
sumadd(l[6]);
|
|
muladd(n6, SECP256K1_N_C_0);
|
|
muladd(n5, SECP256K1_N_C_1);
|
|
muladd(n4, SECP256K1_N_C_2);
|
|
muladd(n3, SECP256K1_N_C_3);
|
|
sumadd(n2);
|
|
extract(m6);
|
|
sumadd(l[7]);
|
|
muladd(n7, SECP256K1_N_C_0);
|
|
muladd(n6, SECP256K1_N_C_1);
|
|
muladd(n5, SECP256K1_N_C_2);
|
|
muladd(n4, SECP256K1_N_C_3);
|
|
sumadd(n3);
|
|
extract(m7);
|
|
muladd(n7, SECP256K1_N_C_1);
|
|
muladd(n6, SECP256K1_N_C_2);
|
|
muladd(n5, SECP256K1_N_C_3);
|
|
sumadd(n4);
|
|
extract(m8);
|
|
muladd(n7, SECP256K1_N_C_2);
|
|
muladd(n6, SECP256K1_N_C_3);
|
|
sumadd(n5);
|
|
extract(m9);
|
|
muladd(n7, SECP256K1_N_C_3);
|
|
sumadd(n6);
|
|
extract(m10);
|
|
sumadd_fast(n7);
|
|
extract_fast(m11);
|
|
VERIFY_CHECK(c0 <= 1);
|
|
m12 = c0;
|
|
|
|
/* Reduce 385 bits into 258. */
|
|
/* p[0..8] = m[0..7] + m[8..12] * SECP256K1_N_C. */
|
|
c0 = m0; c1 = 0; c2 = 0;
|
|
muladd_fast(m8, SECP256K1_N_C_0);
|
|
extract_fast(p0);
|
|
sumadd_fast(m1);
|
|
muladd(m9, SECP256K1_N_C_0);
|
|
muladd(m8, SECP256K1_N_C_1);
|
|
extract(p1);
|
|
sumadd(m2);
|
|
muladd(m10, SECP256K1_N_C_0);
|
|
muladd(m9, SECP256K1_N_C_1);
|
|
muladd(m8, SECP256K1_N_C_2);
|
|
extract(p2);
|
|
sumadd(m3);
|
|
muladd(m11, SECP256K1_N_C_0);
|
|
muladd(m10, SECP256K1_N_C_1);
|
|
muladd(m9, SECP256K1_N_C_2);
|
|
muladd(m8, SECP256K1_N_C_3);
|
|
extract(p3);
|
|
sumadd(m4);
|
|
muladd(m12, SECP256K1_N_C_0);
|
|
muladd(m11, SECP256K1_N_C_1);
|
|
muladd(m10, SECP256K1_N_C_2);
|
|
muladd(m9, SECP256K1_N_C_3);
|
|
sumadd(m8);
|
|
extract(p4);
|
|
sumadd(m5);
|
|
muladd(m12, SECP256K1_N_C_1);
|
|
muladd(m11, SECP256K1_N_C_2);
|
|
muladd(m10, SECP256K1_N_C_3);
|
|
sumadd(m9);
|
|
extract(p5);
|
|
sumadd(m6);
|
|
muladd(m12, SECP256K1_N_C_2);
|
|
muladd(m11, SECP256K1_N_C_3);
|
|
sumadd(m10);
|
|
extract(p6);
|
|
sumadd_fast(m7);
|
|
muladd_fast(m12, SECP256K1_N_C_3);
|
|
sumadd_fast(m11);
|
|
extract_fast(p7);
|
|
p8 = c0 + m12;
|
|
VERIFY_CHECK(p8 <= 2);
|
|
|
|
/* Reduce 258 bits into 256. */
|
|
/* r[0..7] = p[0..7] + p[8] * SECP256K1_N_C. */
|
|
c = p0 + (uint64_t)SECP256K1_N_C_0 * p8;
|
|
r->d[0] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p1 + (uint64_t)SECP256K1_N_C_1 * p8;
|
|
r->d[1] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p2 + (uint64_t)SECP256K1_N_C_2 * p8;
|
|
r->d[2] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p3 + (uint64_t)SECP256K1_N_C_3 * p8;
|
|
r->d[3] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p4 + (uint64_t)p8;
|
|
r->d[4] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p5;
|
|
r->d[5] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p6;
|
|
r->d[6] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
c += p7;
|
|
r->d[7] = c & 0xFFFFFFFFUL; c >>= 32;
|
|
|
|
/* Final reduction of r. */
|
|
secp256k1_scalar_reduce(r, c + secp256k1_scalar_check_overflow(r));
|
|
}
|
|
|
|
static void secp256k1_scalar_mul_512(uint32_t *l, const secp256k1_scalar *a, const secp256k1_scalar *b) {
|
|
/* 96 bit accumulator. */
|
|
uint32_t c0 = 0, c1 = 0, c2 = 0;
|
|
|
|
/* l[0..15] = a[0..7] * b[0..7]. */
|
|
muladd_fast(a->d[0], b->d[0]);
|
|
extract_fast(l[0]);
|
|
muladd(a->d[0], b->d[1]);
|
|
muladd(a->d[1], b->d[0]);
|
|
extract(l[1]);
|
|
muladd(a->d[0], b->d[2]);
|
|
muladd(a->d[1], b->d[1]);
|
|
muladd(a->d[2], b->d[0]);
|
|
extract(l[2]);
|
|
muladd(a->d[0], b->d[3]);
|
|
muladd(a->d[1], b->d[2]);
|
|
muladd(a->d[2], b->d[1]);
|
|
muladd(a->d[3], b->d[0]);
|
|
extract(l[3]);
|
|
muladd(a->d[0], b->d[4]);
|
|
muladd(a->d[1], b->d[3]);
|
|
muladd(a->d[2], b->d[2]);
|
|
muladd(a->d[3], b->d[1]);
|
|
muladd(a->d[4], b->d[0]);
|
|
extract(l[4]);
|
|
muladd(a->d[0], b->d[5]);
|
|
muladd(a->d[1], b->d[4]);
|
|
muladd(a->d[2], b->d[3]);
|
|
muladd(a->d[3], b->d[2]);
|
|
muladd(a->d[4], b->d[1]);
|
|
muladd(a->d[5], b->d[0]);
|
|
extract(l[5]);
|
|
muladd(a->d[0], b->d[6]);
|
|
muladd(a->d[1], b->d[5]);
|
|
muladd(a->d[2], b->d[4]);
|
|
muladd(a->d[3], b->d[3]);
|
|
muladd(a->d[4], b->d[2]);
|
|
muladd(a->d[5], b->d[1]);
|
|
muladd(a->d[6], b->d[0]);
|
|
extract(l[6]);
|
|
muladd(a->d[0], b->d[7]);
|
|
muladd(a->d[1], b->d[6]);
|
|
muladd(a->d[2], b->d[5]);
|
|
muladd(a->d[3], b->d[4]);
|
|
muladd(a->d[4], b->d[3]);
|
|
muladd(a->d[5], b->d[2]);
|
|
muladd(a->d[6], b->d[1]);
|
|
muladd(a->d[7], b->d[0]);
|
|
extract(l[7]);
|
|
muladd(a->d[1], b->d[7]);
|
|
muladd(a->d[2], b->d[6]);
|
|
muladd(a->d[3], b->d[5]);
|
|
muladd(a->d[4], b->d[4]);
|
|
muladd(a->d[5], b->d[3]);
|
|
muladd(a->d[6], b->d[2]);
|
|
muladd(a->d[7], b->d[1]);
|
|
extract(l[8]);
|
|
muladd(a->d[2], b->d[7]);
|
|
muladd(a->d[3], b->d[6]);
|
|
muladd(a->d[4], b->d[5]);
|
|
muladd(a->d[5], b->d[4]);
|
|
muladd(a->d[6], b->d[3]);
|
|
muladd(a->d[7], b->d[2]);
|
|
extract(l[9]);
|
|
muladd(a->d[3], b->d[7]);
|
|
muladd(a->d[4], b->d[6]);
|
|
muladd(a->d[5], b->d[5]);
|
|
muladd(a->d[6], b->d[4]);
|
|
muladd(a->d[7], b->d[3]);
|
|
extract(l[10]);
|
|
muladd(a->d[4], b->d[7]);
|
|
muladd(a->d[5], b->d[6]);
|
|
muladd(a->d[6], b->d[5]);
|
|
muladd(a->d[7], b->d[4]);
|
|
extract(l[11]);
|
|
muladd(a->d[5], b->d[7]);
|
|
muladd(a->d[6], b->d[6]);
|
|
muladd(a->d[7], b->d[5]);
|
|
extract(l[12]);
|
|
muladd(a->d[6], b->d[7]);
|
|
muladd(a->d[7], b->d[6]);
|
|
extract(l[13]);
|
|
muladd_fast(a->d[7], b->d[7]);
|
|
extract_fast(l[14]);
|
|
VERIFY_CHECK(c1 == 0);
|
|
l[15] = c0;
|
|
}
|
|
|
|
static void secp256k1_scalar_sqr_512(uint32_t *l, const secp256k1_scalar *a) {
|
|
/* 96 bit accumulator. */
|
|
uint32_t c0 = 0, c1 = 0, c2 = 0;
|
|
|
|
/* l[0..15] = a[0..7]^2. */
|
|
muladd_fast(a->d[0], a->d[0]);
|
|
extract_fast(l[0]);
|
|
muladd2(a->d[0], a->d[1]);
|
|
extract(l[1]);
|
|
muladd2(a->d[0], a->d[2]);
|
|
muladd(a->d[1], a->d[1]);
|
|
extract(l[2]);
|
|
muladd2(a->d[0], a->d[3]);
|
|
muladd2(a->d[1], a->d[2]);
|
|
extract(l[3]);
|
|
muladd2(a->d[0], a->d[4]);
|
|
muladd2(a->d[1], a->d[3]);
|
|
muladd(a->d[2], a->d[2]);
|
|
extract(l[4]);
|
|
muladd2(a->d[0], a->d[5]);
|
|
muladd2(a->d[1], a->d[4]);
|
|
muladd2(a->d[2], a->d[3]);
|
|
extract(l[5]);
|
|
muladd2(a->d[0], a->d[6]);
|
|
muladd2(a->d[1], a->d[5]);
|
|
muladd2(a->d[2], a->d[4]);
|
|
muladd(a->d[3], a->d[3]);
|
|
extract(l[6]);
|
|
muladd2(a->d[0], a->d[7]);
|
|
muladd2(a->d[1], a->d[6]);
|
|
muladd2(a->d[2], a->d[5]);
|
|
muladd2(a->d[3], a->d[4]);
|
|
extract(l[7]);
|
|
muladd2(a->d[1], a->d[7]);
|
|
muladd2(a->d[2], a->d[6]);
|
|
muladd2(a->d[3], a->d[5]);
|
|
muladd(a->d[4], a->d[4]);
|
|
extract(l[8]);
|
|
muladd2(a->d[2], a->d[7]);
|
|
muladd2(a->d[3], a->d[6]);
|
|
muladd2(a->d[4], a->d[5]);
|
|
extract(l[9]);
|
|
muladd2(a->d[3], a->d[7]);
|
|
muladd2(a->d[4], a->d[6]);
|
|
muladd(a->d[5], a->d[5]);
|
|
extract(l[10]);
|
|
muladd2(a->d[4], a->d[7]);
|
|
muladd2(a->d[5], a->d[6]);
|
|
extract(l[11]);
|
|
muladd2(a->d[5], a->d[7]);
|
|
muladd(a->d[6], a->d[6]);
|
|
extract(l[12]);
|
|
muladd2(a->d[6], a->d[7]);
|
|
extract(l[13]);
|
|
muladd_fast(a->d[7], a->d[7]);
|
|
extract_fast(l[14]);
|
|
VERIFY_CHECK(c1 == 0);
|
|
l[15] = c0;
|
|
}
|
|
|
|
#undef sumadd
|
|
#undef sumadd_fast
|
|
#undef muladd
|
|
#undef muladd_fast
|
|
#undef muladd2
|
|
#undef extract
|
|
#undef extract_fast
|
|
|
|
static void secp256k1_scalar_mul(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b) {
|
|
uint32_t l[16];
|
|
secp256k1_scalar_mul_512(l, a, b);
|
|
secp256k1_scalar_reduce_512(r, l);
|
|
}
|
|
|
|
static int secp256k1_scalar_shr_int(secp256k1_scalar *r, int n) {
|
|
int ret;
|
|
VERIFY_CHECK(n > 0);
|
|
VERIFY_CHECK(n < 16);
|
|
ret = r->d[0] & ((1 << n) - 1);
|
|
r->d[0] = (r->d[0] >> n) + (r->d[1] << (32 - n));
|
|
r->d[1] = (r->d[1] >> n) + (r->d[2] << (32 - n));
|
|
r->d[2] = (r->d[2] >> n) + (r->d[3] << (32 - n));
|
|
r->d[3] = (r->d[3] >> n) + (r->d[4] << (32 - n));
|
|
r->d[4] = (r->d[4] >> n) + (r->d[5] << (32 - n));
|
|
r->d[5] = (r->d[5] >> n) + (r->d[6] << (32 - n));
|
|
r->d[6] = (r->d[6] >> n) + (r->d[7] << (32 - n));
|
|
r->d[7] = (r->d[7] >> n);
|
|
return ret;
|
|
}
|
|
|
|
static void secp256k1_scalar_sqr(secp256k1_scalar *r, const secp256k1_scalar *a) {
|
|
uint32_t l[16];
|
|
secp256k1_scalar_sqr_512(l, a);
|
|
secp256k1_scalar_reduce_512(r, l);
|
|
}
|
|
|
|
#ifdef USE_ENDOMORPHISM
|
|
static void secp256k1_scalar_split_128(secp256k1_scalar *r1, secp256k1_scalar *r2, const secp256k1_scalar *a) {
|
|
r1->d[0] = a->d[0];
|
|
r1->d[1] = a->d[1];
|
|
r1->d[2] = a->d[2];
|
|
r1->d[3] = a->d[3];
|
|
r1->d[4] = 0;
|
|
r1->d[5] = 0;
|
|
r1->d[6] = 0;
|
|
r1->d[7] = 0;
|
|
r2->d[0] = a->d[4];
|
|
r2->d[1] = a->d[5];
|
|
r2->d[2] = a->d[6];
|
|
r2->d[3] = a->d[7];
|
|
r2->d[4] = 0;
|
|
r2->d[5] = 0;
|
|
r2->d[6] = 0;
|
|
r2->d[7] = 0;
|
|
}
|
|
#endif
|
|
|
|
SECP256K1_INLINE static int secp256k1_scalar_eq(const secp256k1_scalar *a, const secp256k1_scalar *b) {
|
|
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;
|
|
}
|
|
|
|
SECP256K1_INLINE static void secp256k1_scalar_mul_shift_var(secp256k1_scalar *r, const secp256k1_scalar *a, const secp256k1_scalar *b, unsigned int shift) {
|
|
uint32_t l[16];
|
|
unsigned int shiftlimbs;
|
|
unsigned int shiftlow;
|
|
unsigned int shifthigh;
|
|
VERIFY_CHECK(shift >= 256);
|
|
secp256k1_scalar_mul_512(l, a, b);
|
|
shiftlimbs = shift >> 5;
|
|
shiftlow = shift & 0x1F;
|
|
shifthigh = 32 - shiftlow;
|
|
r->d[0] = shift < 512 ? (l[0 + shiftlimbs] >> shiftlow | (shift < 480 && shiftlow ? (l[1 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[1] = shift < 480 ? (l[1 + shiftlimbs] >> shiftlow | (shift < 448 && shiftlow ? (l[2 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[2] = shift < 448 ? (l[2 + shiftlimbs] >> shiftlow | (shift < 416 && shiftlow ? (l[3 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[3] = shift < 416 ? (l[3 + shiftlimbs] >> shiftlow | (shift < 384 && shiftlow ? (l[4 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[4] = shift < 384 ? (l[4 + shiftlimbs] >> shiftlow | (shift < 352 && shiftlow ? (l[5 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[5] = shift < 352 ? (l[5 + shiftlimbs] >> shiftlow | (shift < 320 && shiftlow ? (l[6 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[6] = shift < 320 ? (l[6 + shiftlimbs] >> shiftlow | (shift < 288 && shiftlow ? (l[7 + shiftlimbs] << shifthigh) : 0)) : 0;
|
|
r->d[7] = shift < 288 ? (l[7 + shiftlimbs] >> shiftlow) : 0;
|
|
secp256k1_scalar_cadd_bit(r, 0, (l[(shift - 1) >> 5] >> ((shift - 1) & 0x1f)) & 1);
|
|
}
|
|
|
|
#endif
|