Merge rust-bitcoin/rust-bitcoin#7022: taproot-primitives: Add consensus validation tests for LeafVersion
What changed, and why it matters
This commit only adds new unit tests for an existing Bitcoin Taproot leaf-version validation function. It does not change any production code, fix a bug, or alter behavior. There is no security issue in the commit itself.
No action needed; this is a test-only addition. Review the existing LeafVersion::from_consensus implementation separately if desired, but this commit does not introduce or fix a vulnerability.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change adds three test cases to taproot_primitives/src/lib.rs: (1) round-trip consensus encoding for valid future leaf versions (even non-tapscript bytes), (2) rejection of invalid leaf versions including the annex prefix 0x50 and odd bytes, and (3) serde deserialization round-trip and rejection tests. The implementation being tested already exists; the diff is test-only (+47 lines).
Changed components
taproot_primitives/src/lib.rsInspect captured patch +47 / −0
### taproot_primitives/src/lib.rs
@@ -551,4 +551,51 @@ mod test {
let v = LeafVersion::Future(FutureLeafVersion(1));
assert_eq!(alloc::format!("{:#}", v), "future_script_0x01");
}
+
+ #[test]
+ fn leaf_version_future_valid() {
+ for version in [0x00, 0x02, 0xc2, 0xc4, 0xfe] {
+ let lv = LeafVersion::from_consensus(version).unwrap();
+ assert_eq!(lv.to_consensus(), version);
+ match lv {
+ LeafVersion::Future(f) => assert_eq!(f.to_consensus(), version),
+ LeafVersion::TapScript => panic!("expected Future leaf version"),
+ }
+ }
+ }
+
+ #[test]
+ fn leaf_version_invalid_rejected() {
+ let err = LeafVersion::from_consensus(TAPROOT_ANNEX_PREFIX).unwrap_err();
+ assert_eq!(err.invalid_leaf_version(), 0x50);
+
+ for odd in [0x01, 0x03, 0x51, 0xc1, 0xc3, 0xff] {
+ let err = LeafVersion::from_consensus(odd).unwrap_err();
+ assert_eq!(err.invalid_leaf_version(), odd);
+ }
+ }
+
+ #[cfg(feature = "serde")]
+ #[test]
+ fn serde_leaf_version() {
+ use serde::de::value::Error as DeError;
+ use serde::de::IntoDeserializer as _;
+ use serde::Deserialize;
+
+ let de_u8 = |val: u8| -> Result<LeafVersion, DeError> {
+ LeafVersion::deserialize(val.into_deserializer())
+ };
+
+ let future = LeafVersion::from_consensus(0xc2).unwrap();
+ let deserialized = de_u8(0xc2).unwrap();
+ assert_eq!(deserialized, future);
+
+ assert!(de_u8(80).is_err());
+ assert!(de_u8(193).is_err());
+
+ let de_u16 = |val: u16| -> Result<LeafVersion, DeError> {
+ LeafVersion::deserialize(val.into_deserializer())
+ };
+ assert!(de_u16(256).is_err());
+ }
}Why this scored 15/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.