Merge rust-bitcoin/rust-bitcoin#7067: Add annex and codeseparator position to taproot sighash methods
What changed, and why it matters
This commit fixes a bug in how Bitcoin transaction signatures are calculated for Taproot-style transactions. Previously, two helper methods could not accept an optional extra data chunk called an 'annex' and always assumed no special code-separator instruction had run. That meant any transaction using those features would compute an incorrect signature hash, causing a valid signature to be rejected. The change adds the missing parameters so the helpers match the more complete API. It is a correctness fix rather than a memory-safety or remote-exploitable vulnerability, but incorrect sighashes can break wallets, tests, or services that rely on these methods.
Review downstream callers of `taproot_key_spend_signature_hash` and `taproot_script_spend_signature_hash` to ensure they now pass the correct annex and code-separator position; update any code that relied on the old hardcoded behavior. Consider this a bug-fix release for the sighash module.
Security signals we found
API correctness fix for BIP-341 Taproot sighash computation
Previously hardcoded annex and code-separator values produced incorrect signature hashes
Incorrect sighash can cause signature verification failures for valid transactions
New test vectors from Bitcoin Core cover code-separator positions
No memory-unsafe code, panic paths, or input-validation bypass observed
Evidence from the diff
The patch updates SighashCache::taproot_key_spend_signature_hash and taproot_script_spend_signature_hash in bitcoin/src/crypto/sighash.rs to accept annex: Option<Annex> and, for script spends, code_separator_pos: Option<u32>. Previously both methods hardcoded None for the annex and 0xFFFFFFFF for the code-separator position, so they produced wrong BIP-341 sighashes whenever an annex was present or an OP_CODESEPARATOR had executed. The new signatures delegate correctly to the lower-level taproot_encode_signing_data_to. Tests and the sign-tx-taproot example are updated to pass the new arguments. New test vectors from Bitcoin Core exercise non-default code-separator positions.
Changed components
bitcoin/src/crypto/sighash.rsbitcoin/examples/sign-tx-taproot.rsInspect captured patch +86 / −16
### bitcoin/examples/sign-tx-taproot.rs
@@ -61,7 +61,7 @@ fn main() {
let mut sighasher = SighashCache::new(&mut unsigned_tx);
let sighash = sighasher
- .taproot_key_spend_signature_hash(input_index, &prevouts, sighash_type)
+ .taproot_key_spend_signature_hash(input_index, &prevouts, None, sighash_type)
.expect("failed to construct sighash");
// Sign the sighash and update the witness stack.
### bitcoin/src/crypto/sighash.rs
@@ -520,14 +520,15 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
&mut self,
input_index: usize,
prevouts: &Prevouts<T>,
+ annex: Option<Annex>,
sighash_type: TapSighashType,
) -> Result<TapSighash, TaprootError> {
let mut enc = sha256t::Hash::<TapSighashTag>::engine();
self.taproot_encode_signing_data_to(
&mut enc,
input_index,
prevouts,
- None,
+ annex,
None,
sighash_type,
)
@@ -538,22 +539,24 @@ impl<R: Borrow<Transaction>> SighashCache<R> {
/// Computes the BIP-0341 sighash for a script spend.
///
- /// Assumes the default `OP_CODESEPARATOR` position of `0xFFFFFFFF`. Custom values can be
- /// provided through the more fine-grained API of [`SighashCache::taproot_encode_signing_data_to`].
+ /// `code_separator_pos` is the opcode position of the last executed `OP_CODESEPARATOR`, or
+ /// `None` if no `OP_CODESEPARATOR` was executed. `None` and `Some(0xFFFFFFFF)` are equivalent.
pub fn taproot_script_spend_signature_hash<S: Into<TapLeafHash>, T: Borrow<TxOut>>(
&mut self,
input_index: usize,
prevouts: &Prevouts<T>,
+ annex: Option<Annex>,
leaf_hash: S,
+ code_separator_pos: Option<u32>,
sighash_type: TapSighashType,
) -> Result<TapSighash, TaprootError> {
let mut enc = sha256t::Hash::<TapSighashTag>::engine();
self.taproot_encode_signing_data_to(
&mut enc,
input_index,
prevouts,
- None,
- Some((leaf_hash.into(), 0xFFFFFFFF)),
+ annex,
+ Some((leaf_hash.into(), code_separator_pos.unwrap_or(0xFFFFFFFF))),
sighash_type,
)
.map_err(SigningDataError::unwrap_sighash)?;
@@ -1498,55 +1501,55 @@ mod tests {
"01365724000000000023542156b39dab4f8f3508e0432cfb41fab110170acaa2d4c42539cb90a4dc7c093bc500",
0,
"33ca0ebfb4a945eeee9569fc0f5040221275f88690b7f8592ada88ce3bdf6703",
- TapSighashType::Default, None, None, None
+ TapSighashType::Default, None, None, None, None
);
test_taproot_sighash(
"0200000002fff49be59befe7566050737910f6ccdc5e749c7f8860ddc140386463d88c5ad0f3000000002cf68eb4a3d67f9d4c079249f7e4f27b8854815cb1ed13842d4fbf395f9e217fd605ee24090100000065235d9203f458520000000000160014b6d48333bb13b4c644e57c43a9a26df3a44b785e58020000000000001976a914eea9461a9e1e3f765d3af3e726162e0229fe3eb688ac58020000000000001976a9143a8869c9f2b5ea1d4ff3aeeb6a8fb2fffb1ad5fe88ac0ad7125c",
"02591f220000000000225120f25ad35583ea31998d968871d7de1abd2a52f6fe4178b54ea158274806ff4ece48fb310000000000225120f25ad35583ea31998d968871d7de1abd2a52f6fe4178b54ea158274806ff4ece",
1,
"626ab955d58c9a8a600a0c580549d06dc7da4e802eb2a531f62a588e430967a8",
- TapSighashType::All, None, None, None
+ TapSighashType::All, None, None, None, None
);
test_taproot_sighash(
"0200000001350005f65aa830ced2079df348e2d8c2bdb4f10e2dde6a161d8a07b40d1ad87dae000000001611d0d603d9dc0e000000000017a914459b6d7d6bbb4d8837b4bf7e9a4556f952da2f5c8758020000000000001976a9141dd70e1299ffc2d5b51f6f87de9dfe9398c33cbb88ac58020000000000001976a9141dd70e1299ffc2d5b51f6f87de9dfe9398c33cbb88aca71c1f4f",
"01c4811000000000002251201bf9297d0a2968ae6693aadd0fa514717afefd218087a239afb7418e2d22e65c",
0,
"dfa9437f9c9a1d1f9af271f79f2f5482f287cdb0d2e03fa92c8a9b216cc6061c",
- TapSighashType::AllPlusAnyoneCanPay, None, None, None
+ TapSighashType::AllPlusAnyoneCanPay, None, None, None, None
);
test_taproot_sighash(
"020000000185bed1a6da2bffbd60ec681a1bfb71c5111d6395b99b3f8b2bf90167111bcb18f5010000007c83ace802ded24a00000000001600142c4698f9f7a773866879755aa78c516fb332af8e5802000000000000160014d38639dfbac4259323b98a472405db0c461b31fa61073747",
"0144c84d0000000000225120e3f2107989c88e67296ab2faca930efa2e3a5bd3ff0904835a11c9e807458621",
0,
"3129de36a5d05fff97ffca31eb75fcccbbbc27b3147a7a36a9e4b45d8b625067",
- TapSighashType::None, None, None, None
+ TapSighashType::None, None, None, None, None
);
test_taproot_sighash(
"eb93dbb901028c8515589dac980b6e7f8e4088b77ed866ca0d6d210a7218b6fd0f6b22dd6d7300000000eb4740a9047efc0e0000000000160014913da2128d8fcf292b3691db0e187414aa1783825802000000000000160014913da2128d8fcf292b3691db0e187414aa178382580200000000000017a9143dd27f01c6f7ef9bb9159937b17f17065ed01a0c875802000000000000160014d7630e19df70ada9905ede1722b800c0005f246641000000",
"013fed110000000000225120eb536ae8c33580290630fc495046e998086a64f8f33b93b07967d9029b265c55",
0,
"2441e8b0e063a2083ee790f14f2045022f07258ddde5ee01de543c9e789d80ae",
- TapSighashType::NonePlusAnyoneCanPay, None, None, None
+ TapSighashType::NonePlusAnyoneCanPay, None, None, None, None
);
test_taproot_sighash(
"02000000017836b409a5fed32211407e44b971591f2032053f14701fb5b3a30c0ff382f2cc9c0100000061ac55f60288fb5600000000001976a9144ea02f6f182b082fb6ce47e36bbde390b6a41b5088ac58020000000000001976a9144ea02f6f182b082fb6ce47e36bbde390b6a41b5088ace4000000",
"01efa558000000000022512007071ea3dc7e331b0687d0193d1e6d6ed10e645ef36f10ef8831d5e522ac9e80",
0,
"30239345177cadd0e3ea413d49803580abb6cb27971b481b7788a78d35117a88",
- TapSighashType::Single, None, None, None
+ TapSighashType::Single, None, None, None, None
);
test_taproot_sighash(
"0100000001aa6deae89d5e0aaca58714fc76ef6f3c8284224888089232d4e663843ed3ab3eae010000008b6657a60450cb4c0000000000160014a3d42b5413ef0c0701c4702f3cd7d4df222c147058020000000000001976a91430b4ed8723a4ee8992aa2c8814cfe5c3ad0ab9d988ac5802000000000000160014365b1166a6ed0a5e8e9dff17a6d00bbb43454bc758020000000000001976a914bc98c51a84fe7fad5dc380eb8b39586eff47241688ac4f313247",
"0107af4e00000000002251202c36d243dfc06cb56a248e62df27ecba7417307511a81ae61aa41c597a929c69",
0,
"bf9c83f26c6dd16449e4921f813f551c4218e86f2ec906ca8611175b41b566df",
- TapSighashType::SinglePlusAnyoneCanPay, None, None, None
+ TapSighashType::SinglePlusAnyoneCanPay, None, None, None, None
);
}
@@ -1561,6 +1564,7 @@ mod tests {
Some("507b979802e62d397acb29f56743a791894b99372872fc5af06a4f6e8d242d0615cda53062bb20e6ec79756fe39183f0c128adfe85559a8fa042b042c018aa8010143799e44f0893c40e1e"),
None,
None,
+ None,
);
}
@@ -1575,6 +1579,7 @@ mod tests {
None,
Some("20cc4e1107aea1d170c5ff5b6817e1303010049724fb3caa7941792ea9d29b3e2bacab"),
None,
+ None,
);
}
@@ -1589,6 +1594,7 @@ mod tests {
None,
None,
Some("15a2530514e399f8b5cf0b3d3112cf5b289eaa3e308ba2071b58392fdc6da68a"),
+ None,
);
}
@@ -1603,6 +1609,52 @@ mod tests {
Some("50a6272b470e1460e3332ade7bb14b81671c564fb6245761bd5bd531394b28860e0b3808ab229fb51791fb6ae6fa82d915b2efb8f6df83ae1f5ab3db13e30928875e2a22b749d89358de481f19286cd4caa792ce27f9559082d227a731c5486882cc707f83da361c51b7aadd9a0cf68fe7480c410fa137b454482d9a1ebf0f96d760b4d61426fc109c6e8e99a508372c45caa7b000a41f8251305da3f206c1849985ba03f3d9592832b4053afbd23ab25d0465df0bc25a36c223aacf8e04ec736a418c72dc319e4da3e972e349713ca600965e7c665f2090d5a70e241ac164115a1f5639f28b1773327715ca307ace64a2de7f0e3df70a2ffee3857689f909c0dad46d8a20fa373a4cc6eed6d4c9806bf146f0d76baae1"),
Some("7520ab9160dd8299dc1367659be3e8f66781fe440d52940c7f8d314a89b9f2698d406ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6ead6eadac"),
None,
+ None,
+ );
+ }
+
+ #[test]
+ fn sighashes_with_code_separator() {
+ // Script path spends from Bitcoin Core's `script_assets_test.json`. The signature of each
+ // spend verifies against the expected sighash.
+
+ // sighash/codesep_pk
+ test_taproot_sighash(
+ "02000000034f7934adcb0534e6228761a7c93463ffc82acc81e3b5e4dad99cf5777b5f7d522b01000000040ade8134661f3e57f35597df94ee61c0e7d29ae053bf3cc04b3161f087c7fca0ee30b7c6000000009b0cbba134661f3e57f35597df94ee61c0e7d29ae053bf3cc04b3161f087c7fca0ee30b7b200000000700a9fa60447ae6c00000000001976a91454198cf320e2e48fb333572ef9e05ad68c33566d88ac58020000000000001976a91448525f330a27540194162951c1678cc6b9e79b8b88ac58020000000000001976a91448525f330a27540194162951c1678cc6b9e79b8b88ac58020000000000001976a91454198cf320e2e48fb333572ef9e05ad68c33566d88acb5010000",
+ "0317144c0000000000225120ab4fb9e5a16a68191e8bc6fb181beef306f6a89bfa39b735793fbdb4ad516efcd6a91200000000002251206ea719654c1bfee09219cbee8e1895f04c37f5b83a45014aa3000531a6f380568cb51000000000002251206ea719654c1bfee09219cbee8e1895f04c37f5b83a45014aa3000531a6f38056",
+ 2,
+ "b6edee6ec9193029b9801085dd59da0dd278b4198c7fc3a87e6bceea65020fcf",
+ TapSighashType::NonePlusAnyoneCanPay,
+ Some("5076635a14d4edb6ee19fd651eb43281a8840fc162fd8906f5c0a0ae190a0c3e9a23befb8b181b26c6244577d0f542d2ad19d49635e32bb5d2037aa85d23e81bc9789c75907d4e327c0bc91df7b2cb351627476b19cee7f5c7b80baf9d996f17a5dc66a54cc71fec77e0da9e7d7eb1d97aaa5394db046d41179e95cb7c905f1adf7fc7b0fb8d749d52ee0f50dda4ec6f3cd89abaca114bdfbd93659762c7b803fd05a360f255de495f6da41603c1f1b2c3904e2045bd81827c60f3ba60de4b4c596b69138ef196ef1c2c7f5d4a69e3fde71b6995e2cd788e4c98d6b3"),
+ Some("ab20f6acd1ae90f996222528e266cbd9570963200b0e1c8bd4234322718e23a53747ac"),
+ None,
+ Some(0),
+ );
+
+ // sighash/branched_codesep/left
+ test_taproot_sighash(
+ "8978a00f01c8fcf6c075055bed05468c08217fcd9834a0cea986e17a5461ad52f2c96314c51302000000505f90cb018ea22401000000001600146f246454d9cda265b3944397c63270c3c6b160b658010000",
+ "01cab2b101000000002251203cf3576a5e98c16948a4f626ce0fb7ac42b499875f18ea92ad913ae63522d523",
+ 0,
+ "9b0809681aa44cb159b62c397e6dc1b14b6d30c4511be1e0915a7d46239b94bb",
+ TapSighashType::Default,
+ None,
+ Some("01897563ab20703009f4e84cd691802b7a9075eabd8d0df500d719827f22b1301a2e2874038967ab20ef1c38d2738c8d14e5e75007cb88258f2c0b5f9bc8b7e9aa9748fdd2f1a5b37468ac"),
+ None,
+ Some(3),
+ );
+
+ // sighash/branched_codesep/right
+ test_taproot_sighash(
+ "01000000015f82c1147b9f62a9e29134b5db46f02604f1889a358b91002826bfa07148b4235b01000000fce93b0201cac9160100000000160014430c9945e707a7d3c561538a216689ea73bb3befc31e114b",
+ "01974841010000000022512071e870a57b9ef88e09a8fb18ca536507c7435016508923c52c373a179c8b4699",
+ 0,
+ "3deb71509aa8874803ff889a134c6410e5de4221a3408fa13dadfb3aebea251d",
+ TapSighashType::None,
+ None,
+ Some("03edf8e07563ab20b2dadd4cc7d9d89162c72664855491dbe6dbf3ff5d7dc532643ab99582fafbba67ab2081341db0ebeef53a7d269962467fdc49e9b72ea1ea58e7bf3d0af939f77af3b968ac"),
+ None,
+ Some(6),
);
}
@@ -1683,6 +1735,7 @@ mod tests {
annex_hex: Option<&str>,
script_hex: Option<&str>,
script_leaf_hash: Option<&str>,
+ code_separator_pos: Option<u32>,
) {
let tx_bytes = hex::decode_to_vec(tx_hex).unwrap();
let tx: Transaction = decode_from_slice(&tx_bytes).unwrap();
@@ -1707,8 +1760,7 @@ mod tests {
(_, Some(script_leaf_hash)) => Some(script_leaf_hash.parse::<TapLeafHash>().unwrap()),
_ => None,
};
- // All our tests use the default `0xFFFFFFFF` codeseparator value
- let leaf_hash = leaf_hash.map(|lh| (lh, 0xFFFFFFFF));
+ let leaf_hash = leaf_hash.map(|lh| (lh, code_separator_pos.unwrap_or(0xFFFFFFFF)));
let prevouts = if sighash_type.split_anyonecanpay_flag().1 && tx_bytes[0] % 2 == 0 {
// for anyonecanpay the `Prevouts::All` variant is good anyway, but sometimes we want to
@@ -1721,10 +1773,28 @@ mod tests {
let mut sighash_cache = SighashCache::new(&tx);
let hash = sighash_cache
- .taproot_signature_hash(input_index, &prevouts, annex, leaf_hash, sighash_type)
+ .taproot_signature_hash(input_index, &prevouts, annex.clone(), leaf_hash, sighash_type)
.unwrap();
let expected = hex::decode_to_vec(expected_hash).unwrap();
assert_eq!(expected, hash.to_byte_array());
+
+ let spend_hash = match leaf_hash {
+ Some((leaf_hash, _)) => sighash_cache.taproot_script_spend_signature_hash(
+ input_index,
+ &prevouts,
+ annex,
+ leaf_hash,
+ code_separator_pos,
+ sighash_type,
+ ),
+ None => sighash_cache.taproot_key_spend_signature_hash(
+ input_index,
+ &prevouts,
+ annex,
+ sighash_type,
+ ),
+ };
+ assert_eq!(spend_hash.unwrap(), hash);
}
#[test]Why this scored 60/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.