Skip to content

Commit

Permalink
Switch BIP340 implementation to even tiebreaker
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa committed Aug 12, 2020
1 parent 9493ac0 commit 822311c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/modules/schnorrsig/main_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, unsigned char *sig64
/* We declassify r to allow using it as a branch point. This is fine
* because r is not a secret. */
secp256k1_declassify(ctx, &r, sizeof(r));
if (!secp256k1_fe_is_quad_var(&r.y)) {
secp256k1_fe_normalize_var(&r.y);
if (secp256k1_fe_is_odd(&r.y)) {
secp256k1_scalar_negate(&k, &k);
}
secp256k1_fe_normalize_var(&r.x);
Expand Down Expand Up @@ -187,6 +188,7 @@ int secp256k1_schnorrsig_verify(const secp256k1_context* ctx, const unsigned cha
secp256k1_ge pk;
secp256k1_gej pkj;
secp256k1_fe rx;
secp256k1_ge r;
secp256k1_sha256 sha;
unsigned char buf[32];
int overflow;
Expand Down Expand Up @@ -223,8 +225,11 @@ int secp256k1_schnorrsig_verify(const secp256k1_context* ctx, const unsigned cha
secp256k1_gej_set_ge(&pkj, &pk);
secp256k1_ecmult(&ctx->ecmult_ctx, &rj, &pkj, &e, &s);

return secp256k1_gej_has_quad_y_var(&rj) /* fails if rj is infinity */
&& secp256k1_gej_eq_x_var(&rx, &rj);
secp256k1_ge_set_gej_var(&r, &rj);
secp256k1_fe_normalize_var(&r.y);
return !secp256k1_ge_is_infinity(&r) &&
!secp256k1_fe_is_odd(&r.y) &&
secp256k1_fe_equal_var(&rx, &r.x);
}

#endif
2 changes: 2 additions & 0 deletions src/modules/schnorrsig/tests_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ void test_schnorrsig_bip_vectors_check_verify(const unsigned char *pk_serialized
/* Test vectors according to BIP-340 ("Schnorr Signatures for secp256k1"). See
* https://github.com/bitcoin/bips/blob/master/bip-0340/test-vectors.csv. */
void test_schnorrsig_bip_vectors(void) {
#if 0 /* test vectors have not been adapted yet */
{
/* Test vector 0 */
const unsigned char sk[32] = {
Expand Down Expand Up @@ -616,6 +617,7 @@ void test_schnorrsig_bip_vectors(void) {
/* No need to check the signature of the test vector as parsing the pubkey already fails */
CHECK(!secp256k1_xonly_pubkey_parse(ctx, &pk_parsed, pk));
}
#endif
}

/* Nonce function that returns constant 0 */
Expand Down

0 comments on commit 822311c

Please sign in to comment.