mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-11 17:52:48 +01:00
Squashed 'src/secp256k1/' changes from 6c527ec..8225239
8225239Merge #433: Make the libcrypto detection fail the newer API.12de863Make the libcrypto detection fail the newer API.2928420Merge #427: Remove Schnorr from travis as well8eecc4aRemove Schnorr from travis as wella8abae7Merge #310: Add exhaustive test for group functions on a low-order subgroupb4ceedfAdd exhaustive test for verification83836a9Add exhaustive tests for group arithmetic, signing, and ecmult on a small group20b8877Add exhaustive test for group functions on a low-order subgroup80773a6Merge #425: Remove Schnorr experimente06e878Remove Schnorr experiment04c8ef3Merge #407: Modify parameter order of internal functions to match API parameter order6e06696Merge #411: Remove guarantees about memcmp-ability40c8d7eMerge #421: Update scalar_4x64_impl.ha922365Merge #422: Restructure nonce clearing3769783Restructure nonce clearing0f9e69dRestructure nonce clearing9d67afaUpdate scalar_4x64_impl.h7d15cd7Merge #413: fix auto-enabled static precompuatation00c5d2efix auto-enabled static precompuatation91219a1Remove guarantees about memcmp-ability7a49cacMerge #410: Add string.h include to ecmult_impl0bbd5d4Add string.h include to ecmult_impl353c1bfFix secp256k1_ge_set_table_gej_var parameter order541b783Fix secp256k1_ge_set_all_gej_var parameter order7d893f4Fix secp256k1_fe_inv_all_var parameter orderc5b32e1Merge #405: Make secp256k1_fe_sqrt constant time926836aMake secp256k1_fe_sqrt constant timee2a8e92Merge #404: Replace 3M + 4S doubling formula with 2M + 5S one8ec49d8Add note about 2M + 5S doubling formula5a91bd7Merge #400: A couple minor cleanupsac01378build: add -DSECP256K1_BUILD to benchmark_internal build flagsa6c6f99Remove a bunch of unused stdlib #includes65285a6Merge #403: configure: add flag to disable OpenSSL testsa9b2a5dconfigure: add flag to disable OpenSSL testsb340123Merge #402: Add support for testing quadratic residuese6e9805Add function for testing quadratic residue field/group elements.efd953aAdd Jacobi symbol test via GMPfa36a0dMerge #401: ecmult_const: unify endomorphism and non-endomorphism skew casesc6191fdecmult_const: unify endomorphism and non-endomorphism skew cases0b3e618Merge #378: .gitignore build-aux cleanup6042217Merge #384: JNI: align shared files copyright/comments to bitcoinj's24ad20fMerge #399: build: verify that the native compiler works for static precompb3be852Merge #398: Test whether ECDH and Schnorr are enabled for JNIaa0b1fdbuild: verify that the native compiler works for static precompeee808dTest whether ECDH and Schnorr are enabled for JNI7b0fb18Merge #366: ARM assembly implementation of field_10x26 inner (rebase of #173)001f176ARM assembly implementation of field_10x26 inner0172be9Merge #397: Small fixes for sha2563f8b78eFix undefs in hash_impl.h2ab4695Fix state size in sha256 struct6875b01Merge #386: Add some missing `VERIFY_CHECK(ctx != NULL)`2c52b5dMerge #389: Cast pointers through uintptr_t under JNI43097a4Merge #390: Update bitcoin-core GitHub links31c9c12Merge #391: JNI: Only call ecdsa_verify if its inputs parsed correctly1cb2302Merge #392: Add testcase which hits additional branch in secp256k1_scalar_sqrd2ee340Merge #388: bench_ecdh: fix call to secp256k1_context_create093a497Add testcase which hits additional branch in secp256k1_scalar_sqra40c701JNI: Only call ecdsa_verify if its inputs parsed correctlyfaa2a11Update bitcoin-core GitHub links47b9e78Cast pointers through uintptr_t under JNIf36f9c6bench_ecdh: fix call to secp256k1_context_createbcc4881Add some missing `VERIFY_CHECK(ctx != NULL)` for functions that use `ARG_CHECK`6ceea2calign shared files copyright/comments to bitcoinj's70141a8Update .gitignore7b549b1Merge #373: build: fix x86_64 asm detection for some compilersbc7c93cMerge #374: Add note about y=0 being possible on one of the sextic twistse457018Merge #364: JNI rebased86e2d07JNI library: cleanup, removed unimplemented code3093576aJNI librarybd2895fMerge pull request #371e72e93aAdd note about y=0 being possible on one of the sextic twists3f8fdfbbuild: fix x86_64 asm detection for some compilerse5a9047[Trivial] Remove double semicolonsc18b869Merge pull request #3603026daaMerge pull request #30203d4611Add sage verification script for the group lawsa965937Merge pull request #36183221ecAdd experimental features to configure5d4c5a3Prevent damage_array in the signature test from going out of bounds.419bf7fMerge pull request #35603d84a4Benchmark against OpenSSL verification git-subtree-dir: src/secp256k1 git-subtree-split:8225239f49
This commit is contained in:
@@ -58,25 +58,27 @@ static int secp256k1_wnaf_const(int *wnaf, secp256k1_scalar s, int w) {
|
||||
int global_sign;
|
||||
int skew = 0;
|
||||
int word = 0;
|
||||
|
||||
/* 1 2 3 */
|
||||
int u_last;
|
||||
int u;
|
||||
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
int flip;
|
||||
int bit;
|
||||
secp256k1_scalar neg_s;
|
||||
int not_neg_one;
|
||||
/* If we are using the endomorphism, we cannot handle even numbers by negating
|
||||
* them, since we are working with 128-bit numbers whose negations would be 256
|
||||
* bits, eliminating the performance advantage. Instead we use a technique from
|
||||
/* Note that we cannot handle even numbers by negating them to be odd, as is
|
||||
* done in other implementations, since if our scalars were specified to have
|
||||
* width < 256 for performance reasons, their negations would have width 256
|
||||
* and we'd lose any performance benefit. Instead, we use a technique from
|
||||
* Section 4.2 of the Okeya/Tagaki paper, which is to add either 1 (for even)
|
||||
* or 2 (for odd) to the number we are encoding, then compensating after the
|
||||
* multiplication. */
|
||||
/* Negative 128-bit numbers will be negated, since otherwise they are 256-bit */
|
||||
* or 2 (for odd) to the number we are encoding, returning a skew value indicating
|
||||
* this, and having the caller compensate after doing the multiplication. */
|
||||
|
||||
/* Negative numbers will be negated to keep their bit representation below the maximum width */
|
||||
flip = secp256k1_scalar_is_high(&s);
|
||||
/* We add 1 to even numbers, 2 to odd ones, noting that negation flips parity */
|
||||
bit = flip ^ (s.d[0] & 1);
|
||||
bit = flip ^ !secp256k1_scalar_is_even(&s);
|
||||
/* We check for negative one, since adding 2 to it will cause an overflow */
|
||||
secp256k1_scalar_negate(&neg_s, &s);
|
||||
not_neg_one = !secp256k1_scalar_is_one(&neg_s);
|
||||
@@ -89,11 +91,6 @@ static int secp256k1_wnaf_const(int *wnaf, secp256k1_scalar s, int w) {
|
||||
global_sign = secp256k1_scalar_cond_negate(&s, flip);
|
||||
global_sign *= not_neg_one * 2 - 1;
|
||||
skew = 1 << bit;
|
||||
#else
|
||||
/* Otherwise, we just negate to force oddness */
|
||||
int is_even = secp256k1_scalar_is_even(&s);
|
||||
global_sign = secp256k1_scalar_cond_negate(&s, is_even);
|
||||
#endif
|
||||
|
||||
/* 4 */
|
||||
u_last = secp256k1_scalar_shr_int(&s, w);
|
||||
@@ -127,15 +124,13 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
|
||||
secp256k1_ge tmpa;
|
||||
secp256k1_fe Z;
|
||||
|
||||
int skew_1;
|
||||
int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
secp256k1_ge pre_a_lam[ECMULT_TABLE_SIZE(WINDOW_A)];
|
||||
int wnaf_1[1 + WNAF_SIZE(WINDOW_A - 1)];
|
||||
int wnaf_lam[1 + WNAF_SIZE(WINDOW_A - 1)];
|
||||
int skew_1;
|
||||
int skew_lam;
|
||||
secp256k1_scalar q_1, q_lam;
|
||||
#else
|
||||
int wnaf[1 + WNAF_SIZE(WINDOW_A - 1)];
|
||||
#endif
|
||||
|
||||
int i;
|
||||
@@ -145,18 +140,10 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
/* split q into q_1 and q_lam (where q = q_1 + q_lam*lambda, and q_1 and q_lam are ~128 bit) */
|
||||
secp256k1_scalar_split_lambda(&q_1, &q_lam, &sc);
|
||||
/* no need for zero correction when using endomorphism since even
|
||||
* numbers have one added to them anyway */
|
||||
skew_1 = secp256k1_wnaf_const(wnaf_1, q_1, WINDOW_A - 1);
|
||||
skew_lam = secp256k1_wnaf_const(wnaf_lam, q_lam, WINDOW_A - 1);
|
||||
#else
|
||||
int is_zero = secp256k1_scalar_is_zero(scalar);
|
||||
/* the wNAF ladder cannot handle zero, so bump this to one .. we will
|
||||
* correct the result after the fact */
|
||||
sc.d[0] += is_zero;
|
||||
VERIFY_CHECK(!secp256k1_scalar_is_zero(&sc));
|
||||
|
||||
secp256k1_wnaf_const(wnaf, sc, WINDOW_A - 1);
|
||||
skew_1 = secp256k1_wnaf_const(wnaf_1, sc, WINDOW_A - 1);
|
||||
#endif
|
||||
|
||||
/* Calculate odd multiples of a.
|
||||
@@ -179,21 +166,15 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
|
||||
/* first loop iteration (separated out so we can directly set r, rather
|
||||
* than having it start at infinity, get doubled several times, then have
|
||||
* its new value added to it) */
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
i = wnaf_1[WNAF_SIZE(WINDOW_A - 1)];
|
||||
VERIFY_CHECK(i != 0);
|
||||
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
|
||||
secp256k1_gej_set_ge(r, &tmpa);
|
||||
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
i = wnaf_lam[WNAF_SIZE(WINDOW_A - 1)];
|
||||
VERIFY_CHECK(i != 0);
|
||||
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, i, WINDOW_A);
|
||||
secp256k1_gej_add_ge(r, r, &tmpa);
|
||||
#else
|
||||
i = wnaf[WNAF_SIZE(WINDOW_A - 1)];
|
||||
VERIFY_CHECK(i != 0);
|
||||
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, i, WINDOW_A);
|
||||
secp256k1_gej_set_ge(r, &tmpa);
|
||||
#endif
|
||||
/* remaining loop iterations */
|
||||
for (i = WNAF_SIZE(WINDOW_A - 1) - 1; i >= 0; i--) {
|
||||
@@ -202,59 +183,57 @@ static void secp256k1_ecmult_const(secp256k1_gej *r, const secp256k1_ge *a, cons
|
||||
for (j = 0; j < WINDOW_A - 1; ++j) {
|
||||
secp256k1_gej_double_nonzero(r, r, NULL);
|
||||
}
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
|
||||
n = wnaf_1[i];
|
||||
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
|
||||
VERIFY_CHECK(n != 0);
|
||||
secp256k1_gej_add_ge(r, r, &tmpa);
|
||||
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
n = wnaf_lam[i];
|
||||
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a_lam, n, WINDOW_A);
|
||||
VERIFY_CHECK(n != 0);
|
||||
secp256k1_gej_add_ge(r, r, &tmpa);
|
||||
#else
|
||||
n = wnaf[i];
|
||||
VERIFY_CHECK(n != 0);
|
||||
ECMULT_CONST_TABLE_GET_GE(&tmpa, pre_a, n, WINDOW_A);
|
||||
secp256k1_gej_add_ge(r, r, &tmpa);
|
||||
#endif
|
||||
}
|
||||
|
||||
secp256k1_fe_mul(&r->z, &r->z, &Z);
|
||||
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
{
|
||||
/* Correct for wNAF skew */
|
||||
secp256k1_ge correction = *a;
|
||||
secp256k1_ge_storage correction_1_stor;
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
secp256k1_ge_storage correction_lam_stor;
|
||||
#endif
|
||||
secp256k1_ge_storage a2_stor;
|
||||
secp256k1_gej tmpj;
|
||||
secp256k1_gej_set_ge(&tmpj, &correction);
|
||||
secp256k1_gej_double_var(&tmpj, &tmpj, NULL);
|
||||
secp256k1_ge_set_gej(&correction, &tmpj);
|
||||
secp256k1_ge_to_storage(&correction_1_stor, a);
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
secp256k1_ge_to_storage(&correction_lam_stor, a);
|
||||
#endif
|
||||
secp256k1_ge_to_storage(&a2_stor, &correction);
|
||||
|
||||
/* For odd numbers this is 2a (so replace it), for even ones a (so no-op) */
|
||||
secp256k1_ge_storage_cmov(&correction_1_stor, &a2_stor, skew_1 == 2);
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
secp256k1_ge_storage_cmov(&correction_lam_stor, &a2_stor, skew_lam == 2);
|
||||
#endif
|
||||
|
||||
/* Apply the correction */
|
||||
secp256k1_ge_from_storage(&correction, &correction_1_stor);
|
||||
secp256k1_ge_neg(&correction, &correction);
|
||||
secp256k1_gej_add_ge(r, r, &correction);
|
||||
|
||||
#ifdef USE_ENDOMORPHISM
|
||||
secp256k1_ge_from_storage(&correction, &correction_lam_stor);
|
||||
secp256k1_ge_neg(&correction, &correction);
|
||||
secp256k1_ge_mul_lambda(&correction, &correction);
|
||||
secp256k1_gej_add_ge(r, r, &correction);
|
||||
}
|
||||
#else
|
||||
/* correct for zero */
|
||||
r->infinity |= is_zero;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user