Skip to content

Commit 2478c67

Browse files
committed
Make signing follow BIP340 exactly w.r.t. aux randomness
libsecp256k1's secp256k1_schnorrsig_sign only follows BIP340 exactly if an aux_rand32 argument is passed. When no randomness is used (as is the case in the current codebase here), there is no impact on security between not providing aux_rand32 at all, or providing an empty one. Yet, for repeatability/testability it is simpler to always use an all-zero one.
1 parent c9dd5c8 commit 2478c67

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

src/key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
275275
return true;
276276
}
277277

278-
bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256* aux) const
278+
bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256& aux) const
279279
{
280280
assert(sig.size() == 64);
281281
secp256k1_keypair keypair;
@@ -288,7 +288,7 @@ bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint2
288288
uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root);
289289
if (!secp256k1_keypair_xonly_tweak_add(GetVerifyContext(), &keypair, tweak.data())) return false;
290290
}
291-
bool ret = secp256k1_schnorrsig_sign(secp256k1_context_sign, sig.data(), hash.data(), &keypair, aux ? (unsigned char*)aux->data() : nullptr);
291+
bool ret = secp256k1_schnorrsig_sign(secp256k1_context_sign, sig.data(), hash.data(), &keypair, (unsigned char*)aux.data());
292292
if (ret) {
293293
// Additional verification step to prevent using a potentially corrupted signature
294294
secp256k1_xonly_pubkey pubkey_verify;

src/key.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class CKey
130130

131131
/**
132132
* Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this,
133-
* optionally tweaked by *merkle_root. Additional nonce entropy can be provided through
133+
* optionally tweaked by *merkle_root. Additional nonce entropy is provided through
134134
* aux.
135135
*
136136
* merkle_root is used to optionally perform tweaking of the private key, as specified
@@ -143,7 +143,7 @@ class CKey
143143
* (this is used for key path spending, with specific
144144
* Merkle root of the script tree).
145145
*/
146-
bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root = nullptr, const uint256* aux = nullptr) const;
146+
bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256& aux) const;
147147

148148
//! Derive BIP32 child key.
149149
bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;

src/script/sign.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ bool MutableTransactionSignatureCreator::CreateSchnorrSig(const SigningProvider&
8181
uint256 hash;
8282
if (!SignatureHashSchnorr(hash, execdata, *txTo, nIn, nHashType, sigversion, *m_txdata, MissingDataBehavior::FAIL)) return false;
8383
sig.resize(64);
84-
if (!key.SignSchnorr(hash, sig, merkle_root, nullptr)) return false;
84+
// Use uint256{} as aux_rnd for now.
85+
if (!key.SignSchnorr(hash, sig, merkle_root, {})) return false;
8586
if (nHashType) sig.push_back(nHashType);
8687
return true;
8788
}

src/test/key_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors)
321321
key.Set(sec.begin(), sec.end(), true);
322322
XOnlyPubKey pubkey(key.GetPubKey());
323323
BOOST_CHECK(std::equal(pubkey.begin(), pubkey.end(), pub.begin(), pub.end()));
324-
bool ok = key.SignSchnorr(msg256, sig64, nullptr, &aux256);
324+
bool ok = key.SignSchnorr(msg256, sig64, nullptr, aux256);
325325
BOOST_CHECK(ok);
326326
BOOST_CHECK(std::vector<unsigned char>(sig64, sig64 + 64) == sig);
327327
// Verify those signatures for good measure.
@@ -337,7 +337,7 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors)
337337
BOOST_CHECK(tweaked);
338338
XOnlyPubKey tweaked_key = tweaked->first;
339339
aux256 = InsecureRand256();
340-
bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, &aux256);
340+
bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, aux256);
341341
BOOST_CHECK(ok);
342342
BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64));
343343
}

0 commit comments

Comments
 (0)