sign: use fresh randomness as BIP340 auxiliary data #36409

Open
fametrano wants to merge 1 commit from sign-bip340-aux-rand into master AGit
fametrano commented 2026-10-02 07:04:35 +00:00 (Migrated from github.com)

Bitcoin Core signs Taproot inputs with all-zero BIP340 auxiliary data. BIP340 recommends fresh randomness there, as protection against fault injection and side-channel attacks. I made CreateSchnorrSig pass GetRandHash() instead, unless SignOptions::aux_rand sets a fixed value.

In the unit tests, only script_tests/bip341_keypath_test_vectors depended on the zero value: the BIP341 vectors were made with zero auxiliary data. It now builds the signature creator with aux_rand set to zero and compares its signature byte for byte with the vectors, then signs twice with default options and checks that the two signatures differ. If CreateSchnorrSig ignores aux_rand, the vector check fails for all 7 key path inputs. The signing-related functional tests pass unchanged. The script_sign fuzz target can now reach GetRandHash(), so it seeds the RNG like the other signing targets.

I chose GetRandHash() over GetStrongRandBytes(). Both draw from the same RNG, which is seeded with OS entropy at startup, so the output is unpredictable; GetRandHash() skips the OS call GetStrongRandBytes() makes on each draw. BIP340 says any non-repeating value increases protection against fault injection, and that no security property other than side-channel resistance depends on the quality of this randomness. In unit tests GetRandHash() follows the test seed, so the tests stay reproducible; GetStrongRandBytes() does not.

Default signing is no longer deterministic. SignOptions::aux_rand sets a fixed auxiliary value where one is needed, as in the vector test; Yudis-bit suggested it.

Fixes #31883.

Made with my usual tools: a computer, the Internet and an LLM. The mistakes, as usual, are all mine.

Bitcoin Core signs Taproot inputs with all-zero BIP340 auxiliary data. BIP340 recommends fresh randomness there, as protection against fault injection and side-channel attacks. I made `CreateSchnorrSig` pass `GetRandHash()` instead, unless `SignOptions::aux_rand` sets a fixed value. In the unit tests, only `script_tests/bip341_keypath_test_vectors` depended on the zero value: the BIP341 vectors were made with zero auxiliary data. It now builds the signature creator with `aux_rand` set to zero and compares its signature byte for byte with the vectors, then signs twice with default options and checks that the two signatures differ. If `CreateSchnorrSig` ignores `aux_rand`, the vector check fails for all 7 key path inputs. The signing-related functional tests pass unchanged. The `script_sign` fuzz target can now reach `GetRandHash()`, so it seeds the RNG like the other signing targets. I chose `GetRandHash()` over `GetStrongRandBytes()`. Both draw from the same RNG, which is seeded with OS entropy at startup, so the output is unpredictable; `GetRandHash()` skips the OS call `GetStrongRandBytes()` makes on each draw. BIP340 says any non-repeating value increases protection against fault injection, and that no security property other than side-channel resistance depends on the quality of this randomness. In unit tests `GetRandHash()` follows the test seed, so the tests stay reproducible; `GetStrongRandBytes()` does not. Default signing is no longer deterministic. `SignOptions::aux_rand` sets a fixed auxiliary value where one is needed, as in the vector test; Yudis-bit suggested it. Fixes #31883. *Made with my usual tools: a computer, the Internet and an LLM. The mistakes, as usual, are all mine.*
DrahtBot commented 2026-10-02 07:04:44 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK Yudis-bit

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36122 (BIP460: CISA for Taproot key path spends by fjahr)
  • #32857 (wallet: allow skipping script paths by Sjors)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36409 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36409.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36409#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [Yudis-bit](https://github.com/bitcoin/bitcoin/pull/36409#issuecomment-5974564439) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36122](https://github.com/bitcoin/bitcoin/pull/36122) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36122.svg"></sub> (BIP460: CISA for Taproot key path spends by fjahr) * [#32857](https://github.com/bitcoin/bitcoin/pull/32857) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/32857.svg"></sub> (wallet: allow skipping script paths by Sjors) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

🔵 Suggestion · Nondeterministic Taproot signing lacks a release note

Independent signing of the same unsigned Taproot input now produces different signatures, witnesses and wtxids, but the PR adds no release note explaining this user-visible change.

Location: src/script/sign.cpp:119 (head).

🔵 Suggestion · value_or(GetRandHash()) draws randomness even with fixed auxiliary data

An engaged aux_rand still causes a discarded RNG draw, adding unnecessary work and advancing the shared RNG state in the BIP341 vector test.

Location: src/script/sign.cpp:119 (head).

Review coverage was incomplete.


Reviewed: 1f274eae23f4 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:242447a4cf2eea8cf36c662182c044a4a03c53913b7033d9c612118a365b2d2c:1e657457416e20a18d8d2d136919c48e28e1bfed7a76c26939ab1ca10a232c6a --> <!-- ralph-base:ab7e668cc6af5d716ad40166d1f88235a2f6d25d --> <!-- ralph-head:1f274eae23f415ab29e22d25a846844629c672ad --> <!-- ralph-case:9f6043500f12af0f815d31cabc127c7ae5f30594a486428dc5f99b8dfaf3da42 --> **🔵 Suggestion · Nondeterministic Taproot signing lacks a release note** Independent signing of the same unsigned Taproot input now produces different signatures, witnesses and wtxids, but the PR adds no release note explaining this user-visible change. Location: [`src/script/sign.cpp:119`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/1f274eae23f415ab29e22d25a846844629c672ad/src/script/sign.cpp#L119>) (head). **🔵 Suggestion · `value_or(GetRandHash())` draws randomness even with fixed auxiliary data** An engaged `aux_rand` still causes a discarded RNG draw, adding unnecessary work and advancing the shared RNG state in the BIP341 vector test. Location: [`src/script/sign.cpp:119`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/1f274eae23f415ab29e22d25a846844629c672ad/src/script/sign.cpp#L119>) (head). Review coverage was incomplete. <!-- Review tier: sensitive --> --- Reviewed: [`1f274eae23f4`](<https://git.fish.foo/bitcoin/bitcoin/commit/1f274eae23f415ab29e22d25a846844629c672ad>) · [Full review report](<https://review.fish.foo/traces/36409-1f274eae23f415ab29e22d25a846844629c672ad-1904-3.html>) · [Previous reports](<https://review.fish.foo/traces/36409/>) [permalink](<https://review.fish.foo/traces/36409-1f274eae23f415ab29e22d25a846844629c672ad-1904-3.html>)
Yudis-bit commented 2026-10-03 16:33:20 +00:00 (Migrated from github.com)

Tested on be2bc19726.

I think GetRandHash() is definitely the right choice here. For BIP340, nonce generation already binds the secret key with the message via tagged hashes, so auxiliary randomness is purely for fault injection and side-channel mitigation rather than baseline entropy. Skipping the GetStrongRandBytes() OS syscall keeps transaction signing fast, and crucially keeps fuzzing and unit tests deterministic under SeedRandomStateForTest(SeedRand::ZEROS).

Regarding your question about SignOptions: I'd definitely lean towards adding std::optional<uint256> aux_rand there.

Notice what happens in src/test/script_tests.cpp: because MutableTransactionSignatureCreator doesn't accept an aux value, the test has to bypass the creator entirely and call key.SignSchnorr(...) directly. But the whole point of that test case in script_tests.cpp was to test MutableTransactionSignatureCreator's end-to-end handling of the BIP-341 witness generation against the official vectors.

If SignOptions has:

struct SignOptions {
    int sighash_type{SIGHASH_DEFAULT};
    std::optional<uint256> aux_rand{std::nullopt};
};

and in MutableTransactionSignatureCreator::CreateSchnorrSig:

const uint256 aux_rand = m_options.aux_rand.value_or(GetRandHash());
if (!key.SignSchnorr(*hash, sig, merkle_root, aux_rand)) return false;

Then default transaction signing keeps fresh randomness, but script_tests can still test the creator against the exact BIP-341 vector:

MutableTransactionSignatureCreator creator_zero_aux(tx, txinpos, utxos[txinpos].nValue, &txdata, {.sighash_type = hashtype, .aux_rand = uint256{}});
std::vector<unsigned char> zero_aux_sig;
BOOST_CHECK(creator_zero_aux.CreateSchnorrSig(provider, zero_aux_sig, pubkey, nullptr, &merkle_root, SigVersion::TAPROOT));
BOOST_CHECK_EQUAL(HexStr(zero_aux_sig), expected);

Verified this locally with bip341_wallet_vectors.json. All 7 keypath test vector inputs reproduce the expected witness signature directly through the creator, alongside the two differing non-deterministic signatures under default options.

Tested on be2bc19726. I think `GetRandHash()` is definitely the right choice here. For BIP340, nonce generation already binds the secret key with the message via tagged hashes, so auxiliary randomness is purely for fault injection and side-channel mitigation rather than baseline entropy. Skipping the `GetStrongRandBytes()` OS syscall keeps transaction signing fast, and crucially keeps fuzzing and unit tests deterministic under `SeedRandomStateForTest(SeedRand::ZEROS)`. Regarding your question about `SignOptions`: I'd definitely lean towards adding `std::optional<uint256> aux_rand` there. Notice what happens in `src/test/script_tests.cpp`: because `MutableTransactionSignatureCreator` doesn't accept an aux value, the test has to bypass the creator entirely and call `key.SignSchnorr(...)` directly. But the whole point of that test case in `script_tests.cpp` was to test `MutableTransactionSignatureCreator`'s end-to-end handling of the BIP-341 witness generation against the official vectors. If `SignOptions` has: ```cpp struct SignOptions { int sighash_type{SIGHASH_DEFAULT}; std::optional<uint256> aux_rand{std::nullopt}; }; ``` and in `MutableTransactionSignatureCreator::CreateSchnorrSig`: ```cpp const uint256 aux_rand = m_options.aux_rand.value_or(GetRandHash()); if (!key.SignSchnorr(*hash, sig, merkle_root, aux_rand)) return false; ``` Then default transaction signing keeps fresh randomness, but `script_tests` can still test the creator against the exact BIP-341 vector: ```cpp MutableTransactionSignatureCreator creator_zero_aux(tx, txinpos, utxos[txinpos].nValue, &txdata, {.sighash_type = hashtype, .aux_rand = uint256{}}); std::vector<unsigned char> zero_aux_sig; BOOST_CHECK(creator_zero_aux.CreateSchnorrSig(provider, zero_aux_sig, pubkey, nullptr, &merkle_root, SigVersion::TAPROOT)); BOOST_CHECK_EQUAL(HexStr(zero_aux_sig), expected); ``` Verified this locally with `bip341_wallet_vectors.json`. All 7 keypath test vector inputs reproduce the expected witness signature directly through the creator, alongside the two differing non-deterministic signatures under default options.
fametrano commented 2026-10-03 18:31:08 +00:00 (Migrated from github.com)

Done in 1f274eae23, as you suggested: SignOptions::aux_rand, defaulting to GetRandHash(). The vector test goes through the creator again with aux_rand = uint256{}, and two default-options signatures are checked to differ. With aux_rand ignored, the vector check fails for all 7 inputs. PR description updated. Thanks.

Done in 1f274eae23, as you suggested: `SignOptions::aux_rand`, defaulting to `GetRandHash()`. The vector test goes through the creator again with `aux_rand = uint256{}`, and two default-options signatures are checked to differ. With `aux_rand` ignored, the vector check fails for all 7 inputs. PR description updated. Thanks.
Yudis-bit commented 2026-10-03 23:23:30 +00:00 (Migrated from github.com)

ACK 1f274eae23

Re-reviewed the commit. Cleanly addresses the test vector coupling by keeping SignOptions::aux_rand optional while defaulting to GetRandHash(). Verified that bip341_keypath_test_vectors reproduces the BIP-341 witness with zero aux and confirms divergence under default options, with script_sign fuzzing seeded deterministically.

ACK 1f274eae23 Re-reviewed the commit. Cleanly addresses the test vector coupling by keeping `SignOptions::aux_rand` optional while defaulting to `GetRandHash()`. Verified that `bip341_keypath_test_vectors` reproduces the BIP-341 witness with zero aux and confirms divergence under default options, with `script_sign` fuzzing seeded deterministically.
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36409
No description provided.