From 3430d554e4e6097406a500fb879baa13cdf54214 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Wed, 29 Apr 2026 03:20:30 -0400 Subject: [PATCH 1/6] descriptor: enforce BIP key-expression rule dpk_to_pk now returns Result<_, Error::InvalidKeyExpression>: Single literal pubkeys and bare xpubs (no derivation, no wildcard) are rejected. descr_to_dpks and dpks_to_derivation_keys_paths silently filter rejected expressions, falling through to DescriptorHasNoKeys only when nothing valid remains. Test vectors gain expected: Option so disallowed cases pin "must error". --- src/descriptor.rs | 157 +++++++++++++++++++++++++---------- src/lib.rs | 7 +- test_vectors/keys_types.json | 40 ++++++--- 3 files changed, 145 insertions(+), 59 deletions(-) diff --git a/src/descriptor.rs b/src/descriptor.rs index dcf36f4..426edad 100644 --- a/src/descriptor.rs +++ b/src/descriptor.rs @@ -9,22 +9,39 @@ use alloc::{collections::BTreeSet, str::FromStr, vec::Vec}; use miniscript::{ bitcoin::{self, bip32::DerivationPath, secp256k1}, + descriptor::{DerivPaths, Wildcard}, Descriptor, DescriptorPublicKey, ForEachKey, }; use crate::Error; -pub fn dpk_to_pk(key: &DescriptorPublicKey) -> bitcoin::secp256k1::PublicKey { - match key { - DescriptorPublicKey::Single(key) => match key.key { - miniscript::descriptor::SinglePubKey::FullKey(pk) => pk.inner, - miniscript::descriptor::SinglePubKey::XOnly(pk) => { - pk.public_key(bitcoin::key::Parity::Even) - } - }, - DescriptorPublicKey::XPub(key) => key.xkey.public_key, - DescriptorPublicKey::MultiXPub(key) => key.xkey.public_key, +pub fn dpk_to_pk(key: &DescriptorPublicKey) -> Result { + let (key, path, wildcard) = match key { + DescriptorPublicKey::Single(_) => return Err(Error::InvalidKeyExpression), + DescriptorPublicKey::XPub(key) => ( + key.xkey.public_key, + DerivPaths::new(vec![key.derivation_path.clone()]).expect("path not empty"), + key.wildcard, + ), + DescriptorPublicKey::MultiXPub(key) => ( + key.xkey.public_key, + key.derivation_paths.clone(), + key.wildcard, + ), + }; + let path = path.into_paths(); + let mut deriv = true; + if path.is_empty() { + deriv = false; + } + for p in path { + if p.is_empty() { + deriv = false; + } } + (deriv || wildcard != Wildcard::None) + .then_some(key) + .ok_or(Error::InvalidKeyExpression) } fn dpk_to_deriv_path(key: &DescriptorPublicKey) -> Option { @@ -54,9 +71,11 @@ pub fn descr_to_dpks( let nums_xonly = bip341_nums().x_only_public_key().0; let mut keys = BTreeSet::new(); descriptor.for_each_key(|k| { - let pk = dpk_to_pk(k); - if pk.x_only_public_key().0 != nums_xonly { - keys.insert(k.clone()); + // invalid key expressions are sorted out + if let Ok(pk) = dpk_to_pk(k) { + if pk.x_only_public_key().0 != nums_xonly { + keys.insert(k.clone()); + } } true }); @@ -75,9 +94,12 @@ pub fn dpks_to_derivation_keys_paths( let mut derivation_paths = BTreeSet::new(); let mut keys = BTreeSet::new(); for k in dpks { - keys.insert(dpk_to_pk(k)); - if let Some(path) = dpk_to_deriv_path(k) { - derivation_paths.insert(path); + // invalid key expressions are sorted out + if let Ok(key) = dpk_to_pk(k) { + keys.insert(key); + if let Some(path) = dpk_to_deriv_path(k) { + derivation_paths.insert(path); + } } } let deriv = derivation_paths.into_iter().collect(); @@ -129,20 +151,20 @@ pub mod tests { #[test] fn test_dpk_to_pk() { + // Valid key expressions (xpub with /<0;1>/*) extract the xpub root + // pubkey and are accepted by dpk_to_pk. let expected = pk(); - let pk = dpk_to_pk(&dpk_1()); - assert_eq!(pk, expected); - let pk = dpk_to_pk(&dpk_2()); - assert_eq!(pk, expected); + let p = dpk_to_pk(&dpk_1()).unwrap(); + assert_eq!(p, expected); + let p = dpk_to_pk(&dpk_2()).unwrap(); + assert_eq!(p, expected); - // Single + // Single FullKey; disallowed by the spec's key-expression rule. let single_str = "0250929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"; let dpk = DescriptorPublicKey::from_str(single_str).unwrap(); - let pk = dpk_to_pk(&dpk); - let expected = bitcoin::secp256k1::PublicKey::from_str(single_str).unwrap(); - assert_eq!(expected, pk); + assert_eq!(dpk_to_pk(&dpk), Err(Error::InvalidKeyExpression)); - // Single Xonly + // Single XOnly; disallowed. let xonly = bitcoin::PublicKey::from_str(single_str) .unwrap() .to_x_only_pubkey(); @@ -150,10 +172,9 @@ pub mod tests { origin: None, key: descriptor::SinglePubKey::XOnly(xonly), }); - let pk = dpk_to_pk(&dpk); - assert_eq!(expected, pk); + assert_eq!(dpk_to_pk(&dpk), Err(Error::InvalidKeyExpression)); - // Xpub + // Xpub with no derivation and no wildcard; disallowed. let xpub = bip32::Xpub { network: bitcoin::NetworkKind::Test, depth: 1, @@ -162,25 +183,42 @@ pub mod tests { public_key: bitcoin::secp256k1::PublicKey::from_str(single_str).unwrap(), chain_code: ChainCode::from(&[1u8; 32]), }; - let dpk = DescriptorPublicKey::XPub(DescriptorXKey { + let bare_xpub = DescriptorPublicKey::XPub(DescriptorXKey { origin: None, xkey: xpub, derivation_path: DerivationPath::default(), wildcard: Wildcard::None, }); - let pk = dpk_to_pk(&dpk); - assert_eq!(expected, pk); + assert_eq!(dpk_to_pk(&bare_xpub), Err(Error::InvalidKeyExpression)); - // MultiXpub - let dpk = DescriptorPublicKey::MultiXPub(DescriptorMultiXKey { + // Xpub with non-empty derivation, no wildcard; allowed. + let xpub_fixed = DescriptorPublicKey::XPub(DescriptorXKey { + origin: None, + xkey: xpub, + derivation_path: DerivationPath::from_str("0/5").unwrap(), + wildcard: Wildcard::None, + }); + let expected_xpub_pk = bitcoin::secp256k1::PublicKey::from_str(single_str).unwrap(); + assert_eq!(dpk_to_pk(&xpub_fixed).unwrap(), expected_xpub_pk); + + // Xpub with empty derivation and wildcard; allowed. + let xpub_wild = DescriptorPublicKey::XPub(DescriptorXKey { + origin: None, + xkey: xpub, + derivation_path: DerivationPath::default(), + wildcard: Wildcard::Unhardened, + }); + assert_eq!(dpk_to_pk(&xpub_wild).unwrap(), expected_xpub_pk); + + // MultiXpub with non-empty path, no wildcard; allowed (deriv only). + let multi_fixed = DescriptorPublicKey::MultiXPub(DescriptorMultiXKey { origin: None, xkey: xpub, derivation_paths: DerivPaths::new(vec![DerivationPath::from_str("0").unwrap()]) .unwrap(), wildcard: Wildcard::None, }); - let pk = dpk_to_pk(&dpk); - assert_eq!(expected, pk); + assert_eq!(dpk_to_pk(&multi_fixed).unwrap(), expected_xpub_pk); } #[test] @@ -200,7 +238,7 @@ pub mod tests { let dpk = DescriptorPublicKey::from_str(single_str).unwrap(); let none = dpk_to_deriv_path(&dpk); assert!(none.is_none()); - let single_pk = SinglePubKey::FullKey(dpk_to_pk(&dpk).into()); + let single_pk = SinglePubKey::FullKey(bitcoin::PublicKey::from_str(single_str).unwrap()); let dpk = DescriptorPublicKey::Single(SinglePub { origin: origin.clone(), key: single_pk, @@ -270,13 +308,19 @@ pub mod tests { let keys = descr_to_dpks(&descriptor).unwrap(); let nums_xonly = bip341_nums().x_only_public_key().0; for key in keys { - let pk = dpk_to_pk(&key); + let pk = dpk_to_pk(&key).unwrap(); assert_ne!(pk.x_only_public_key().0, nums_xonly); } - // but the descriptor contains unspendable + // but the descriptor contains unspendable. The descriptor here uses + // only xpub key expressions, so reading `xkey.public_key` directly + // is sufficient and avoids exposing an unvalidated extractor. let contains_unspendable = descriptor.for_any_key(|k| { - let pk = dpk_to_pk(k); - pk.x_only_public_key().0 == nums_xonly + let xpub_key = match k { + DescriptorPublicKey::XPub(x) => x.xkey.public_key, + DescriptorPublicKey::MultiXPub(x) => x.xkey.public_key, + DescriptorPublicKey::Single(_) => return false, + }; + xpub_key.x_only_public_key().0 == nums_xonly }); assert!(contains_unspendable); } @@ -303,7 +347,9 @@ mod keys_types { struct TestVector { description: String, key: String, - expected: String, + // Some(hex) → expression is allowed; expected x-only normalization. + // None → expression is disallowed; dpk_to_pk must return Err. + expected: Option, } #[test] @@ -312,9 +358,27 @@ mod keys_types { for v in vectors { let dpk = DescriptorPublicKey::from_str(&v.key).expect(&v.description); - let pk = dpk_to_pk(&dpk); - let res = hex::encode(pk.x_only_public_key().0.serialize()); - assert_eq!(v.expected, res, "{}", v.description); + match (&v.expected, dpk_to_pk(&dpk)) { + (Some(hex_expected), Ok(pk)) => { + let res = hex::encode(pk.x_only_public_key().0.serialize()); + assert_eq!(*hex_expected, res, "{}", v.description); + } + (None, Err(_)) => { + // Disallowed expression correctly rejected by dpk_to_pk. + } + (Some(_), Err(e)) => { + panic!( + "{}: expected allowed but dpk_to_pk failed with {e:?}", + v.description + ); + } + (None, Ok(_)) => { + panic!( + "{}: expected disallowed but dpk_to_pk succeeded", + v.description + ); + } + } } } @@ -324,8 +388,9 @@ mod keys_types { let mut vectors: Vec = serde_json::from_str(TEST_VECTORS_JSON).unwrap(); for v in vectors.iter_mut() { let dpk = DescriptorPublicKey::from_str(&v.key).expect(&v.description); - let pk = dpk_to_pk(&dpk); - v.expected = hex::encode(pk.x_only_public_key().0.serialize()); + v.expected = dpk_to_pk(&dpk) + .ok() + .map(|pk| hex::encode(pk.x_only_public_key().0.serialize())); } let out = serde_json::to_string_pretty(&vectors).unwrap(); std::fs::write("test_vectors/keys_types.json", out).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 0da7d96..2bd6d06 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -391,6 +391,7 @@ pub enum Error { WrongKey, DescriptorHasNoKeys, Base64, + InvalidKeyExpression, String(Box), } @@ -430,7 +431,7 @@ mod tests { assert!(!backp.payload.is_none()); assert!(backp.get_keys().is_empty()); - let pk1 = dpk_to_pk(&descriptor::tests::dpk_1()); + let pk1 = dpk_to_pk(&descriptor::tests::dpk_1()).unwrap(); backp = backp.set_keys(vec![pk1]); let pks = backp.get_keys(); assert_eq!(pks.len(), 1); @@ -500,7 +501,7 @@ mod tests { } pub fn dummy_encrypted_payload() -> Vec { - let key = dpk_to_pk(&descriptor::tests::dpk_1()); + let key = dpk_to_pk(&descriptor::tests::dpk_1()).unwrap(); EncryptedBackup::new() .set_payload(&vec![0x00]) .unwrap() @@ -533,7 +534,7 @@ mod tests { #[test] fn test_decrypt_wrong_payload() { - let key = dpk_to_pk(&descriptor::tests::dpk_1()); + let key = dpk_to_pk(&descriptor::tests::dpk_1()).unwrap(); // No payload let fail = EncryptedBackup::new() .set_keys(vec![key]) diff --git a/test_vectors/keys_types.json b/test_vectors/keys_types.json index ce91762..a2346e0 100644 --- a/test_vectors/keys_types.json +++ b/test_vectors/keys_types.json @@ -1,27 +1,47 @@ [ { - "description": "Xpub with origin and multipath", + "description": "Xpub with origin and multipath wildcard", "key": "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*", "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" }, { - "description": "Xpub with origin and w/o multipath", + "description": "Xpub with single-path wildcard", + "key": "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/*", + "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "Xpub with bare wildcard (no preceding path)", + "key": "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/*", + "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "Xpub with fixed deep derivation, no wildcard", + "key": "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/5", + "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "Xpub with multipath, no wildcard", + "key": "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>", + "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + }, + { + "description": "Bare xpub (no derivation, no wildcard); DISALLOWED", "key": "[d4ab66f1/48'/1'/1'/2']tpubDFTxBKyUCgkwp5enwZh3t2FJ5AMJqmCWoh1NRT13qNYQb1iKTUrAG6u5gpsDYhG8cZGXouYWuQtzcuSVjPStTc4dwU6JqPMFtgaLGvSQXhi", - "expected": "8e886919a6b72579a28bd292505d2afd41c1b5012414c5e24d7b59f4abdfc0ce" + "expected": null }, { - "description": "Compressed public key", + "description": "Compressed literal pubkey; DISALLOWED", "key": "02ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708", - "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + "expected": null }, { - "description": "X only public key", + "description": "X-only literal pubkey; DISALLOWED", "key": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708", - "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + "expected": null }, { - "description": "Uncompressed public key", + "description": "Uncompressed literal pubkey; DISALLOWED", "key": "04ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b517089e956909c4c07e8529f45f3ff8904d28df5a181619e21bdf748a896322530039", - "expected": "ebd252ca0877aae09b9d058219682775aa3cbcd049c12f07832f2cf6a3b51708" + "expected": null } -] \ No newline at end of file +] From bfa184caa5685a1885a08087d217eceb851a9d40 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Wed, 29 Apr 2026 03:21:36 -0400 Subject: [PATCH 2/6] lib: surface filtered keys via Warning + Encrypted return types Add Warning enum (DisallowedKeyExpression, NumsKey) and switch encrypt() to return Encrypted { bytes, warnings }. Encrypted carries a to_base64() helper (gated on the base64 feature) so callers can get bitcoin-core-compatible base64 output without losing access to the warnings. EncryptedBackup gains a warnings field populated through ToPayload::warnings; the Descriptor impl forwards to a new descriptor::descr_warnings that walks the descriptor and reports each excluded key expression. NUMS detection wins over the disallow rule so a NUMS literal in tr() is reported with the more specific Warning::NumsKey. The CLI bin is migrated to the new return type to keep the cli feature build green; warning printing follows in the next commit. --- src/bin/main.rs | 6 +- src/descriptor.rs | 37 ++++- src/lib.rs | 391 ++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 400 insertions(+), 34 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 458546d..55c59b6 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -134,7 +134,7 @@ async fn main() -> Result<(), CliError> { .map_err(CliError::CantConvertToDescriptor)?; // encrypt the descriptor - let bytes = EncryptedBackup::new() + let encrypted = EncryptedBackup::new() .set_payload(&descriptor) .map_err(CliError::FailedToEncrypt)? .encrypt() @@ -142,7 +142,9 @@ async fn main() -> Result<(), CliError> { // pass the byte vector to a file let mut output = File::create(&output_path).map_err(CliError::CreateError)?; - output.write(&bytes).map_err(CliError::WriteError)?; + output + .write(&encrypted.bytes) + .map_err(CliError::WriteError)?; println!("descriptor written to {output_path:?}"); } Commands::Decrypt { file, key, output } => { diff --git a/src/descriptor.rs b/src/descriptor.rs index 426edad..a7e40ed 100644 --- a/src/descriptor.rs +++ b/src/descriptor.rs @@ -9,11 +9,25 @@ use alloc::{collections::BTreeSet, str::FromStr, vec::Vec}; use miniscript::{ bitcoin::{self, bip32::DerivationPath, secp256k1}, - descriptor::{DerivPaths, Wildcard}, + descriptor::{DerivPaths, SinglePubKey, Wildcard}, Descriptor, DescriptorPublicKey, ForEachKey, }; -use crate::Error; +/// Internal-only x-only normalization used by NUMS detection. Bypasses the +/// allow/disallow check intentionally so a NUMS literal in tr() is reported +/// as Warning::NumsKey rather than DisallowedKeyExpression. +fn xonly_of(key: &DescriptorPublicKey) -> [u8; 32] { + match key { + DescriptorPublicKey::Single(k) => match k.key { + SinglePubKey::FullKey(pk) => pk.inner.x_only_public_key().0.serialize(), + SinglePubKey::XOnly(xo) => xo.serialize(), + }, + DescriptorPublicKey::XPub(k) => k.xkey.public_key.x_only_public_key().0.serialize(), + DescriptorPublicKey::MultiXPub(k) => k.xkey.public_key.x_only_public_key().0.serialize(), + } +} + +use crate::{Error, Warning}; pub fn dpk_to_pk(key: &DescriptorPublicKey) -> Result { let (key, path, wildcard) = match key { @@ -88,6 +102,25 @@ pub fn descr_to_dpks( } } +/// Walk the descriptor and emit a warning for every key expression that +/// `descr_to_dpks` sorts out of the encryption-key set: disallowed +/// expressions (literal pubkey, bare xpub) and the BIP341 NUMS key. +/// NUMS detection wins over the disallow rule so a NUMS literal in tr() +/// is reported with the more specific reason. +pub fn descr_warnings(descriptor: &Descriptor) -> Result, Error> { + let nums_xonly = bip341_nums().x_only_public_key().0.serialize(); + let mut warnings = Vec::new(); + descriptor.for_each_key(|k| { + if xonly_of(k) == nums_xonly { + warnings.push(Warning::NumsKey(k.clone())); + } else if dpk_to_pk(k).is_err() { + warnings.push(Warning::DisallowedKeyExpression(k.clone())); + } + true + }); + Ok(warnings) +} + pub fn dpks_to_derivation_keys_paths( dpks: &Vec, ) -> (Vec, Vec) { diff --git a/src/lib.rs b/src/lib.rs index 2bd6d06..a6a231f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,11 +30,46 @@ pub mod ll; #[cfg(feature = "devices")] pub mod signing_devices; +/// Non-fatal signal raised while extracting keys from a descriptor: a key +/// expression was sorted out of the encryption-key set. The cosigner +/// holding that key will be unable to decrypt the backup with their key. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum Warning { + /// The expression is not allowed by the BIP (e.g. literal pubkey, or + /// bare xpub with no trailing derivation and no wildcard). + DisallowedKeyExpression(DescriptorPublicKey), + /// The expression resolves to the BIP341 NUMS point. + NumsKey(DescriptorPublicKey), +} + +/// Output of [`EncryptedBackup::encrypt`]: the encoded backup plus any +/// warnings raised while building the encryption-key set. +#[derive(Debug, Clone)] +pub struct Encrypted { + pub bytes: Vec, + pub warnings: Vec, +} + +impl Encrypted { + /// Standard RFC 4648 base64 of the encoded backup (the format + /// produced by bitcoin-core's wallet tool). Warnings remain + /// accessible via `self.warnings`. + #[cfg(feature = "base64")] + pub fn to_base64(&self) -> String { + use base64::Engine as _; + base64::engine::general_purpose::STANDARD.encode(&self.bytes) + } +} + pub trait ToPayload { fn to_payload(&self) -> Result, Error>; fn content_type(&self) -> Content; fn derivation_paths(&self) -> Result, Error>; fn keys(&self) -> Result, Error>; + /// Warnings about filtered key expressions. Default empty. + fn warnings(&self) -> Result, Error> { + Ok(vec![]) + } } impl ToPayload for Vec { @@ -72,6 +107,10 @@ impl ToPayload for Descriptor { let (k, _) = descriptor::dpks_to_derivation_keys_paths(&dpks); Ok(k) } + + fn warnings(&self) -> Result, Error> { + descriptor::descr_warnings(self) + } } #[derive(Debug, Clone, PartialEq, Eq)] @@ -110,6 +149,7 @@ pub struct EncryptedBackup { derivation_paths: Vec, keys: Vec, payload: Payload, + warnings: Vec, } impl Default for EncryptedBackup { @@ -121,6 +161,7 @@ impl Default for EncryptedBackup { derivation_paths: vec![], keys: vec![], payload: Payload::None, + warnings: vec![], } } } @@ -174,9 +215,16 @@ impl EncryptedBackup { self.derivation_paths .append(&mut payload.derivation_paths()?); self.keys.append(&mut payload.keys()?); + self.warnings.append(&mut payload.warnings()?); Ok(self) } - pub fn encrypt(self, #[cfg(not(feature = "rand"))] nonce: [u8; 12]) -> Result, Error> { + pub fn get_warnings(&self) -> &[Warning] { + &self.warnings + } + pub fn encrypt( + self, + #[cfg(not(feature = "rand"))] nonce: [u8; 12], + ) -> Result { if self.content == Content::Unknown { return Err(Error::UnknownContent); } @@ -192,30 +240,22 @@ impl EncryptedBackup { return Err(Error::WrongPayload); }; + let warnings = self.warnings.clone(); match (self.encryption, self.version) { - (Encryption::ChaCha20Poly1305, Version::V1) => Ok(ll::encrypt_chacha20_poly1305_v1( - self.derivation_paths, - self.content.clone(), - self.keys, - &bytes, - #[cfg(not(feature = "rand"))] - nonce, - )?), + (Encryption::ChaCha20Poly1305, Version::V1) => { + let bytes = ll::encrypt_chacha20_poly1305_v1( + self.derivation_paths, + self.content.clone(), + self.keys, + &bytes, + #[cfg(not(feature = "rand"))] + nonce, + )?; + Ok(Encrypted { bytes, warnings }) + } _ => Err(Error::NotImplemented), } } - #[cfg(feature = "base64")] - pub fn encrypt_base64( - self, - #[cfg(not(feature = "rand"))] nonce: [u8; 12], - ) -> Result { - use base64::Engine as _; - let bytes = self.encrypt( - #[cfg(not(feature = "rand"))] - nonce, - )?; - Ok(base64::engine::general_purpose::STANDARD.encode(bytes)) - } pub fn set_encrypted_payload(self, bytes: &[u8]) -> Result { // Auto-detect: the binary BIPXXX blob always starts with the // 6-byte ASCII magic "BIPXXX". If the input does not start with @@ -414,7 +454,7 @@ mod tests { let descriptor = descriptor::tests::descr_1(); let backp = EncryptedBackup::new().set_payload(&descriptor).unwrap(); let keys = backp.get_keys(); - let bytes = backp.encrypt().unwrap(); + let bytes = backp.encrypt().unwrap().bytes; let restored = EncryptedBackup::new() .set_encrypted_payload(&bytes) .unwrap() @@ -464,7 +504,7 @@ mod tests { backp = backp.set_version(Version::V1); assert_eq!(backp.get_version(), Version::V1); - let bytes = backp.encrypt().unwrap(); + let bytes = backp.encrypt().unwrap().bytes; let fail = EncryptedBackup::new() .set_encrypted_payload(&bytes) @@ -489,7 +529,7 @@ mod tests { assert_eq!(fail, Error::WrongKey); // Plaintext `[0x00, 0x00, 0x00]` is not a valid descriptor string, so - // extracting BIP380 content surfaces Error::Descriptor — proving the + // extracting BIP380 content surfaces Error::Descriptor; proving the // round-trip decrypt succeeded before extract failed. let fail = EncryptedBackup::new() .set_encrypted_payload(&bytes) @@ -509,6 +549,7 @@ mod tests { .set_content_type(Content::Bip380) .encrypt() .unwrap() + .bytes } #[test] @@ -590,11 +631,12 @@ mod tests { .set_keys(vec![pk1, pk2, pk3]) .set_content_type(Content::Bip380) .encrypt() - .unwrap(); + .unwrap() + .bytes; for key in [pk1, pk2, pk3] { // Plaintext isn't a real descriptor, so Bip380 extract fails - // with Error::Descriptor — that failure proves the chacha + // with Error::Descriptor; that failure proves the chacha // decrypt step succeeded first (same signal used by // test_encrypt_bytes). The WrongKey case below is the // negative control. @@ -632,8 +674,9 @@ mod tests { .unwrap() .set_keys(vec![pk]) .set_content_type(Content::Bip380) - .encrypt_base64() - .unwrap(); + .encrypt() + .unwrap() + .to_base64(); // Decrypt via auto-detected base64 input (bytes of UTF-8 string). let err = EncryptedBackup::new() @@ -642,7 +685,7 @@ mod tests { .set_keys(vec![pk]) .decrypt() .unwrap_err(); - // Plaintext isn't a valid descriptor — extract failing proves + // Plaintext isn't a valid descriptor; extract failing proves // the chacha decrypt step succeeded first. assert_eq!(err, Error::Descriptor); @@ -675,7 +718,8 @@ mod tests { .set_keys(vec![pk]) .set_content_type(Content::Bip380) .encrypt() - .unwrap(); + .unwrap() + .bytes; let err = EncryptedBackup::new() .set_encrypted_payload(&bytes) .unwrap() @@ -696,6 +740,293 @@ mod tests { assert_eq!(err, Error::Base64); } + // The following tests address review feedback on the BIP that suggested + // single-signature policies would yield c_i = 0x0...0 (i.e. a backup that + // leaks the encryption secret in plaintext). They demonstrate the opposite: + // because the decryption secret `s` and the per-key term `s_i` are derived + // with *different* tagged hashes (`BIPXXX_DECRYPTION_SECRET` vs + // `BIPXXX_INDIVIDUAL_SECRET`), `s != s_i` and so `c_1 = s ^ s_1 != 0` even + // when n = 1. + + #[test] + fn test_single_sig_individual_secret_is_non_zero() { + use miniscript::bitcoin::hashes::Hash; + // Direct math check: with a single key, s and s_1 use different tags, + // so their XOR cannot be all-zero in any practical sense. + let xonly = dpk_to_pk(&descriptor::tests::dpk_1()) + .unwrap() + .x_only_public_key() + .0 + .serialize(); + + let s = ll::decryption_secret(&[xonly]); + let s1 = ll::tagged_hash("BIPXXX_INDIVIDUAL_SECRET".as_bytes(), &xonly); + let c1 = ll::individual_secret(&s, &xonly); + + assert_ne!( + s.to_byte_array(), + s1.to_byte_array(), + "decryption secret must differ from individual term for single-sig" + ); + assert_ne!( + c1, [0u8; 32], + "c_1 = s XOR s_1 must not be all-zero for single-sig (would leak the secret)" + ); + } + + #[test] + fn test_single_sig_wpkh_roundtrip() { + // End-to-end round trip with a single-key wpkh() descriptor; the + // canonical single-sig policy. Confirms that single-sig is fully + // supported by the scheme: encrypt yields a valid blob and the same + // single key decrypts it back to the original descriptor. + let descr_str = "wpkh([58b7f8dc/84'/1'/0']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let backp = EncryptedBackup::new().set_payload(&descriptor).unwrap(); + let keys = backp.get_keys(); + assert_eq!(keys.len(), 1, "wpkh is single-sig"); + + let bytes = backp.encrypt().unwrap().bytes; + let restored = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(keys) + .decrypt() + .unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + } + + #[test] + fn test_single_sig_tr_roundtrip() { + // Same end-to-end check for a single-key tr() (taproot) descriptor. + let descr_str = "tr([58b7f8dc/86'/1'/0']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let backp = EncryptedBackup::new().set_payload(&descriptor).unwrap(); + let keys = backp.get_keys(); + assert_eq!(keys.len(), 1, "tr() with no script tree is single-sig"); + + let bytes = backp.encrypt().unwrap().bytes; + let restored = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(keys) + .decrypt() + .unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + } + + #[test] + fn test_single_sig_backup_blob_does_not_contain_zero_secret() { + // Sanity check on the wire format: the 32-byte INDIVIDUAL_SECRET + // embedded in a single-sig backup must not be all-zero. If it were, + // anyone parsing the blob would recover the encryption secret + // unconditionally. + let descr_str = "wpkh([58b7f8dc/84'/1'/0']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let bytes = EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt() + .unwrap() + .bytes; + + let (_paths, secrets, _enc, _nonce, _ct) = ll::decode_v1(&bytes).unwrap(); + assert_eq!(secrets.len(), 1); + assert_ne!( + secrets[0], [0u8; 32], + "single-sig blob must not store a zeroed individual secret" + ); + } + + // The next group of tests pins down the "Descriptor key requirements" rule + // from the BIP: each xpub key expression must have a non-empty trailing + // derivation OR a wildcard, and `Single` literal pubkeys are never valid. + // The Rust impl enforces this in `descriptor::dpk_to_pk` and filters + // invalid expressions in `descr_to_dpks` (each filtered expression + // surfaces as a Warning), returning `Error::DescriptorHasNoKeys` only + // when nothing valid remains. + + #[test] + fn test_reject_bare_xpub_descriptor() { + // wpkh(); no derivation, no wildcard. The encryption seed + // would equal the on-chain pubkey, so this expression is invalid and + // there is no other key to fall back to. + let descr_str = "wpkh(tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let err = EncryptedBackup::new().set_payload(&descriptor).unwrap_err(); + assert_eq!(err, Error::DescriptorHasNoKeys); + } + + #[test] + fn test_reject_single_literal_only_descriptor() { + // pk(<33-byte hex>); literal Single pubkey, used on-chain verbatim. + let descr_str = "pk(0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let err = EncryptedBackup::new().set_payload(&descriptor).unwrap_err(); + assert_eq!(err, Error::DescriptorHasNoKeys); + } + + #[test] + fn test_accept_xpub_fixed_deriv_no_wildcard() { + // wpkh(xpub.../0/5); non-empty trailing derivation, no wildcard. The + // on-chain key is xpub/0/5, distinct from the xpub root used as the + // encryption seed, so this expression is valid. + let descr_str = "wpkh([58b7f8dc/84'/1'/0']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/0/5)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let backp = EncryptedBackup::new().set_payload(&descriptor).unwrap(); + let keys = backp.get_keys(); + assert_eq!(keys.len(), 1); + + let bytes = backp.encrypt().unwrap().bytes; + let restored = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(keys) + .decrypt() + .unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + } + + #[test] + fn test_accept_xpub_wildcard_no_extra_deriv() { + // wpkh(xpub.../*); empty trailing derivation but a wildcard. The + // wildcard forces a child derivation, so the on-chain key differs + // from the xpub root. + let descr_str = "wpkh([58b7f8dc/84'/1'/0']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/*)"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let backp = EncryptedBackup::new().set_payload(&descriptor).unwrap(); + let keys = backp.get_keys(); + assert_eq!(keys.len(), 1); + + let bytes = backp.encrypt().unwrap().bytes; + let restored = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(keys) + .decrypt() + .unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + } + + #[test] + fn test_filter_partial_invalid_multikey() { + // Mixed descriptor: one valid xpub (with /<0;1>/*) and one bare xpub. + // The bare xpub is filtered (surfacing as a Warning); encryption + // proceeds with the single remaining valid key. The cosigner holding + // the filtered key cannot decrypt; only the valid key works. + let valid_xpub = "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*"; + let bare_xpub = "tpubDC5FSnBiZDMmkoat4aZFfbJdEthnPqJ1jXZcKWJNKC4yJanLA55dRW5qKJRRvAo1SwaXeUx2ayUQyVJ6eCbABbBB8Wn3T7dAuVJRnZgntVC"; + let descr_str = format!("wsh(or_d(pk({}),pk({})))", valid_xpub, bare_xpub); + let descriptor = Descriptor::::from_str(&descr_str).unwrap(); + + let backp = EncryptedBackup::new().set_payload(&descriptor).unwrap(); + let keys = backp.get_keys(); + assert_eq!( + keys.len(), + 1, + "bare xpub must be filtered, leaving only the valid one" + ); + + let bytes = backp.encrypt().unwrap().bytes; + + // The valid key decrypts. + let restored = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(keys) + .decrypt() + .unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + + // The filtered (bare) key does NOT decrypt; its pubkey was excluded + // from the encryption-key set. + let bare_pk = miniscript::bitcoin::bip32::Xpub::from_str(bare_xpub) + .unwrap() + .public_key; + let err = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(vec![bare_pk]) + .decrypt() + .unwrap_err(); + assert_eq!(err, Error::WrongKey); + } + + #[test] + fn test_warning_disallowed_key_expression() { + // Multikey descriptor with one bare xpub: the bare expression must + // surface as Warning::DisallowedKeyExpression in the Encrypted output. + let valid_xpub = "[58b7f8dc/48'/1'/0'/2']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*"; + let bare_xpub = "tpubDC5FSnBiZDMmkoat4aZFfbJdEthnPqJ1jXZcKWJNKC4yJanLA55dRW5qKJRRvAo1SwaXeUx2ayUQyVJ6eCbABbBB8Wn3T7dAuVJRnZgntVC"; + let descr_str = format!("wsh(or_d(pk({valid_xpub}),pk({bare_xpub})))"); + let descriptor = Descriptor::::from_str(&descr_str).unwrap(); + + let encrypted = EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt() + .unwrap(); + + assert_eq!(encrypted.warnings.len(), 1, "exactly one excluded key"); + match &encrypted.warnings[0] { + Warning::DisallowedKeyExpression(k) => { + assert!( + k.to_string().contains(bare_xpub), + "warning carries the bare xpub" + ); + } + other => panic!("expected DisallowedKeyExpression, got {other:?}"), + } + } + + #[test] + fn test_warning_nums_key() { + // tr() with NUMS as the internal key: NUMS is filtered out, so the + // descriptor's other key still encrypts and the NUMS exclusion + // surfaces as Warning::NumsKey. + let descr_str = "tr(50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0,pk([58b7f8dc/86'/1'/0']tpubDEPBvXvhta3pjVaKokqC3eeMQnszj9ehFaA2zD5nSdkaccwGAizu8jVB2NeSpvmP2P52MBoZvNCixqXRJnTyXx51FQzARR63tjxQSyP3Btw/<0;1>/*))"; + let descriptor = Descriptor::::from_str(descr_str).unwrap(); + + let encrypted = EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt() + .unwrap(); + + assert!( + encrypted + .warnings + .iter() + .any(|w| matches!(w, Warning::NumsKey(_))), + "NUMS exclusion must surface as Warning::NumsKey, got {:?}", + encrypted.warnings + ); + } + + #[test] + fn test_no_warnings_on_clean_descriptor() { + // descr_1 is a well-formed multipath multisig with no NUMS and no + // disallowed expressions: warnings must be empty. + let descriptor = descriptor::tests::descr_1(); + let encrypted = EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt() + .unwrap(); + assert!( + encrypted.warnings.is_empty(), + "no warnings expected, got {:?}", + encrypted.warnings + ); + } + #[test] fn test_encryption_to_u8() { let mut u: u8 = Encryption::ChaCha20Poly1305.into(); From 7118cdb0270dc2479a9e3b55524bf3f1b0d9cf28 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Wed, 29 Apr 2026 03:22:04 -0400 Subject: [PATCH 3/6] cli: print warnings about excluded keys on encrypt Iterate Encrypted::warnings and write a one-line message to stderr per excluded key expression so users see when a cosigner's key was sorted out of the encryption-key set. --- src/bin/main.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/bin/main.rs b/src/bin/main.rs index 55c59b6..8e41bba 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -140,6 +140,19 @@ async fn main() -> Result<(), CliError> { .encrypt() .map_err(CliError::FailedToEncrypt)?; + for w in &encrypted.warnings { + match w { + bitcoin_encrypted_backup::Warning::DisallowedKeyExpression(k) => { + eprintln!( + "warning: disallowed key expression excluded from encryption-key set: {k}; the cosigner holding this key cannot decrypt the backup with their key" + ); + } + bitcoin_encrypted_backup::Warning::NumsKey(k) => { + eprintln!("warning: BIP341 NUMS key excluded from encryption-key set: {k}"); + } + } + } + // pass the byte vector to a file let mut output = File::create(&output_path).map_err(CliError::CreateError)?; output From 8f18412d17443d5edde3db79ce9dda3adbd884b5 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Wed, 29 Apr 2026 03:22:45 -0400 Subject: [PATCH 4/6] ll: assert c_i != 0 + use non-zero test vectors Add a sanity-harness assertion in individual_secret: distinct domain-separation tags for s and s_i make c_i = s ^ s_i = 0 statistically impossible, so seeing it means the secret derivation is misconfigured. Rewrite the two zero-secret fixtures (encode-decode and no-duplicates tests, plus individual_secrets.json) to use non-zero patterns that match the spec's expected shape; spell out the BTreeSet ordering in the encode-decode test. --- src/ll.rs | 27 ++++++++++++++++++--------- test_vectors/individual_secrets.json | 28 ++++++++++++++-------------- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/ll.rs b/src/ll.rs index 2dc0be7..5aca24f 100644 --- a/src/ll.rs +++ b/src/ll.rs @@ -220,7 +220,15 @@ pub fn decryption_secret(keys: &[[u8; XONLY_KEY_SIZE]]) -> sha256::Hash { pub fn individual_secret(secret: &sha256::Hash, key: &[u8; XONLY_KEY_SIZE]) -> [u8; 32] { let si = tagged_hash(INDIVIDUAL_SECRET.as_bytes(), key); - xor(secret.as_byte_array(), si.as_byte_array()) + let ci = xor(secret.as_byte_array(), si.as_byte_array()); + // Sanity harness: distinct domain-separation tags make c_i = 0 + // statistically impossible, so seeing it means secret derivation is + // misconfigured (e.g. same tag for s and s_i). + assert_ne!( + ci, [0u8; 32], + "c_i collapsed to zero, secret derivation is broken" + ); + ci } pub fn individual_secrets(secret: &sha256::Hash, keys: &[[u8; XONLY_KEY_SIZE]]) -> Vec<[u8; 32]> { @@ -1128,25 +1136,26 @@ mod tests { #[test] fn test_encode_decode_individual_secrets() { - let secrets = vec![[0; 32], [1; 32]]; + let secrets = vec![[2; 32], [1; 32]]; let bytes = encode_individual_secrets(&secrets).unwrap(); let expected = vec![ - 2u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, + 2u8, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, + 2, 2, 2, 2, 2, 2, 2, 2, ]; assert_eq!(expected, bytes); let (_, decoded) = parse_individual_secrets(&bytes).unwrap(); - assert_eq!(secrets, decoded); + // BTreeSet sorts by value, so the encoded order is [1; 32], [2; 32]. + assert_eq!(vec![[1; 32], [2; 32]], decoded); } #[test] fn test_encode_individual_secrets_no_duplicates() { - let secrets = vec![[0; 32], [0; 32]]; + let secrets = vec![[7; 32], [7; 32]]; let bytes = encode_individual_secrets(&secrets).unwrap(); let expected = vec![ - 1u8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, + 1u8, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, + 7, 7, 7, 7, 7, ]; assert_eq!(expected, bytes); } diff --git a/test_vectors/individual_secrets.json b/test_vectors/individual_secrets.json index 8e4f1a4..cfe0ee7 100644 --- a/test_vectors/individual_secrets.json +++ b/test_vectors/individual_secrets.json @@ -2,26 +2,26 @@ { "description": "Single secret", "secrets": [ - "0000000000000000000000000000000000000000000000000000000000000000" + "0101010101010101010101010101010101010101010101010101010101010101" ], - "expected": "010000000000000000000000000000000000000000000000000000000000000000" + "expected": "010101010101010101010101010101010101010101010101010101010101010101" }, { "description": "Two different secrets", "secrets": [ - "0000000000000000000000000000000000000000000000000000000000000000", - "0101010101010101010101010101010101010101010101010101010101010101" + "0101010101010101010101010101010101010101010101010101010101010101", + "0202020202020202020202020202020202020202020202020202020202020202" ], - "expected": "0200000000000000000000000000000000000000000000000000000000000000000101010101010101010101010101010101010101010101010101010101010101" + "expected": "0201010101010101010101010101010101010101010101010101010101010101010202020202020202020202020202020202020202020202020202020202020202" }, { "description": "Three secrets with different patterns", "secrets": [ "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", - "0000000000000000000000000000000000000000000000000000000000000000", + "5555555555555555555555555555555555555555555555555555555555555555", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" ], - "expected": "030000000000000000000000000000000000000000000000000000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" + "expected": "035555555555555555555555555555555555555555555555555555555555555555aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" }, { "description": "Empty secrets should fail", @@ -29,9 +29,8 @@ "expected": null }, { - "description": "256 secrets should fail (exceeds u8::MAX)", + "description": "256 distinct non-zero secrets should fail (exceeds u8::MAX)", "secrets": [ - "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000002", "0000000000000000000000000000000000000000000000000000000000000003", @@ -286,14 +285,14 @@ "00000000000000000000000000000000000000000000000000000000000000fc", "00000000000000000000000000000000000000000000000000000000000000fd", "00000000000000000000000000000000000000000000000000000000000000fe", - "00000000000000000000000000000000000000000000000000000000000000ff" + "00000000000000000000000000000000000000000000000000000000000000ff", + "0000000000000000000000000000000000000000000000000000000000000100" ], "expected": null }, { - "description": "256 secrets should pass)", + "description": "255 distinct non-zero secrets should pass", "secrets": [ - "0000000000000000000000000000000000000000000000000000000000000000", "0000000000000000000000000000000000000000000000000000000000000001", "0000000000000000000000000000000000000000000000000000000000000002", "0000000000000000000000000000000000000000000000000000000000000003", @@ -547,8 +546,9 @@ "00000000000000000000000000000000000000000000000000000000000000fb", "00000000000000000000000000000000000000000000000000000000000000fc", "00000000000000000000000000000000000000000000000000000000000000fd", - "00000000000000000000000000000000000000000000000000000000000000fe" + "00000000000000000000000000000000000000000000000000000000000000fe", + "00000000000000000000000000000000000000000000000000000000000000ff" ], - "expected": "ff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001700000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000019000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000001d000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000023000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000250000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000002700000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000029000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000002b000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000002f0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003100000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000033000000000000000000000000000000000000000000000000000000000000003400000000000000000000000000000000000000000000000000000000000000350000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000003700000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000039000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000003b000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000003d000000000000000000000000000000000000000000000000000000000000003e000000000000000000000000000000000000000000000000000000000000003f0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004100000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000043000000000000000000000000000000000000000000000000000000000000004400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000000000000000000000004700000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000049000000000000000000000000000000000000000000000000000000000000004a000000000000000000000000000000000000000000000000000000000000004b000000000000000000000000000000000000000000000000000000000000004c000000000000000000000000000000000000000000000000000000000000004d000000000000000000000000000000000000000000000000000000000000004e000000000000000000000000000000000000000000000000000000000000004f0000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000005100000000000000000000000000000000000000000000000000000000000000520000000000000000000000000000000000000000000000000000000000000053000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000550000000000000000000000000000000000000000000000000000000000000056000000000000000000000000000000000000000000000000000000000000005700000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000059000000000000000000000000000000000000000000000000000000000000005a000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000005c000000000000000000000000000000000000000000000000000000000000005d000000000000000000000000000000000000000000000000000000000000005e000000000000000000000000000000000000000000000000000000000000005f0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000006100000000000000000000000000000000000000000000000000000000000000620000000000000000000000000000000000000000000000000000000000000063000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000006700000000000000000000000000000000000000000000000000000000000000680000000000000000000000000000000000000000000000000000000000000069000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000006b000000000000000000000000000000000000000000000000000000000000006c000000000000000000000000000000000000000000000000000000000000006d000000000000000000000000000000000000000000000000000000000000006e000000000000000000000000000000000000000000000000000000000000006f0000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000007100000000000000000000000000000000000000000000000000000000000000720000000000000000000000000000000000000000000000000000000000000073000000000000000000000000000000000000000000000000000000000000007400000000000000000000000000000000000000000000000000000000000000750000000000000000000000000000000000000000000000000000000000000076000000000000000000000000000000000000000000000000000000000000007700000000000000000000000000000000000000000000000000000000000000780000000000000000000000000000000000000000000000000000000000000079000000000000000000000000000000000000000000000000000000000000007a000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000007c000000000000000000000000000000000000000000000000000000000000007d000000000000000000000000000000000000000000000000000000000000007e000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000008100000000000000000000000000000000000000000000000000000000000000820000000000000000000000000000000000000000000000000000000000000083000000000000000000000000000000000000000000000000000000000000008400000000000000000000000000000000000000000000000000000000000000850000000000000000000000000000000000000000000000000000000000000086000000000000000000000000000000000000000000000000000000000000008700000000000000000000000000000000000000000000000000000000000000880000000000000000000000000000000000000000000000000000000000000089000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000008b000000000000000000000000000000000000000000000000000000000000008c000000000000000000000000000000000000000000000000000000000000008d000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000008f0000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009100000000000000000000000000000000000000000000000000000000000000920000000000000000000000000000000000000000000000000000000000000093000000000000000000000000000000000000000000000000000000000000009400000000000000000000000000000000000000000000000000000000000000950000000000000000000000000000000000000000000000000000000000000096000000000000000000000000000000000000000000000000000000000000009700000000000000000000000000000000000000000000000000000000000000980000000000000000000000000000000000000000000000000000000000000099000000000000000000000000000000000000000000000000000000000000009a000000000000000000000000000000000000000000000000000000000000009b000000000000000000000000000000000000000000000000000000000000009c000000000000000000000000000000000000000000000000000000000000009d000000000000000000000000000000000000000000000000000000000000009e000000000000000000000000000000000000000000000000000000000000009f00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a100000000000000000000000000000000000000000000000000000000000000a200000000000000000000000000000000000000000000000000000000000000a300000000000000000000000000000000000000000000000000000000000000a400000000000000000000000000000000000000000000000000000000000000a500000000000000000000000000000000000000000000000000000000000000a600000000000000000000000000000000000000000000000000000000000000a700000000000000000000000000000000000000000000000000000000000000a800000000000000000000000000000000000000000000000000000000000000a900000000000000000000000000000000000000000000000000000000000000aa00000000000000000000000000000000000000000000000000000000000000ab00000000000000000000000000000000000000000000000000000000000000ac00000000000000000000000000000000000000000000000000000000000000ad00000000000000000000000000000000000000000000000000000000000000ae00000000000000000000000000000000000000000000000000000000000000af00000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000b100000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000b300000000000000000000000000000000000000000000000000000000000000b400000000000000000000000000000000000000000000000000000000000000b500000000000000000000000000000000000000000000000000000000000000b600000000000000000000000000000000000000000000000000000000000000b700000000000000000000000000000000000000000000000000000000000000b800000000000000000000000000000000000000000000000000000000000000b900000000000000000000000000000000000000000000000000000000000000ba00000000000000000000000000000000000000000000000000000000000000bb00000000000000000000000000000000000000000000000000000000000000bc00000000000000000000000000000000000000000000000000000000000000bd00000000000000000000000000000000000000000000000000000000000000be00000000000000000000000000000000000000000000000000000000000000bf00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c100000000000000000000000000000000000000000000000000000000000000c200000000000000000000000000000000000000000000000000000000000000c300000000000000000000000000000000000000000000000000000000000000c400000000000000000000000000000000000000000000000000000000000000c500000000000000000000000000000000000000000000000000000000000000c600000000000000000000000000000000000000000000000000000000000000c700000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000000ca00000000000000000000000000000000000000000000000000000000000000cb00000000000000000000000000000000000000000000000000000000000000cc00000000000000000000000000000000000000000000000000000000000000cd00000000000000000000000000000000000000000000000000000000000000ce00000000000000000000000000000000000000000000000000000000000000cf00000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000d100000000000000000000000000000000000000000000000000000000000000d200000000000000000000000000000000000000000000000000000000000000d300000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000d500000000000000000000000000000000000000000000000000000000000000d600000000000000000000000000000000000000000000000000000000000000d700000000000000000000000000000000000000000000000000000000000000d800000000000000000000000000000000000000000000000000000000000000d900000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000db00000000000000000000000000000000000000000000000000000000000000dc00000000000000000000000000000000000000000000000000000000000000dd00000000000000000000000000000000000000000000000000000000000000de00000000000000000000000000000000000000000000000000000000000000df00000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000e100000000000000000000000000000000000000000000000000000000000000e200000000000000000000000000000000000000000000000000000000000000e300000000000000000000000000000000000000000000000000000000000000e400000000000000000000000000000000000000000000000000000000000000e500000000000000000000000000000000000000000000000000000000000000e600000000000000000000000000000000000000000000000000000000000000e700000000000000000000000000000000000000000000000000000000000000e800000000000000000000000000000000000000000000000000000000000000e900000000000000000000000000000000000000000000000000000000000000ea00000000000000000000000000000000000000000000000000000000000000eb00000000000000000000000000000000000000000000000000000000000000ec00000000000000000000000000000000000000000000000000000000000000ed00000000000000000000000000000000000000000000000000000000000000ee00000000000000000000000000000000000000000000000000000000000000ef00000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000000f100000000000000000000000000000000000000000000000000000000000000f200000000000000000000000000000000000000000000000000000000000000f300000000000000000000000000000000000000000000000000000000000000f400000000000000000000000000000000000000000000000000000000000000f500000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000f700000000000000000000000000000000000000000000000000000000000000f800000000000000000000000000000000000000000000000000000000000000f900000000000000000000000000000000000000000000000000000000000000fa00000000000000000000000000000000000000000000000000000000000000fb00000000000000000000000000000000000000000000000000000000000000fc00000000000000000000000000000000000000000000000000000000000000fd00000000000000000000000000000000000000000000000000000000000000fe" + "expected": "ff000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000013000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000150000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001700000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000019000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001c000000000000000000000000000000000000000000000000000000000000001d000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002100000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000000000000000023000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000250000000000000000000000000000000000000000000000000000000000000026000000000000000000000000000000000000000000000000000000000000002700000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000029000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000002b000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000002f0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003100000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000033000000000000000000000000000000000000000000000000000000000000003400000000000000000000000000000000000000000000000000000000000000350000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000003700000000000000000000000000000000000000000000000000000000000000380000000000000000000000000000000000000000000000000000000000000039000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000003b000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000003d000000000000000000000000000000000000000000000000000000000000003e000000000000000000000000000000000000000000000000000000000000003f0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004100000000000000000000000000000000000000000000000000000000000000420000000000000000000000000000000000000000000000000000000000000043000000000000000000000000000000000000000000000000000000000000004400000000000000000000000000000000000000000000000000000000000000450000000000000000000000000000000000000000000000000000000000000046000000000000000000000000000000000000000000000000000000000000004700000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000049000000000000000000000000000000000000000000000000000000000000004a000000000000000000000000000000000000000000000000000000000000004b000000000000000000000000000000000000000000000000000000000000004c000000000000000000000000000000000000000000000000000000000000004d000000000000000000000000000000000000000000000000000000000000004e000000000000000000000000000000000000000000000000000000000000004f0000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000005100000000000000000000000000000000000000000000000000000000000000520000000000000000000000000000000000000000000000000000000000000053000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000000000000550000000000000000000000000000000000000000000000000000000000000056000000000000000000000000000000000000000000000000000000000000005700000000000000000000000000000000000000000000000000000000000000580000000000000000000000000000000000000000000000000000000000000059000000000000000000000000000000000000000000000000000000000000005a000000000000000000000000000000000000000000000000000000000000005b000000000000000000000000000000000000000000000000000000000000005c000000000000000000000000000000000000000000000000000000000000005d000000000000000000000000000000000000000000000000000000000000005e000000000000000000000000000000000000000000000000000000000000005f0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000006100000000000000000000000000000000000000000000000000000000000000620000000000000000000000000000000000000000000000000000000000000063000000000000000000000000000000000000000000000000000000000000006400000000000000000000000000000000000000000000000000000000000000650000000000000000000000000000000000000000000000000000000000000066000000000000000000000000000000000000000000000000000000000000006700000000000000000000000000000000000000000000000000000000000000680000000000000000000000000000000000000000000000000000000000000069000000000000000000000000000000000000000000000000000000000000006a000000000000000000000000000000000000000000000000000000000000006b000000000000000000000000000000000000000000000000000000000000006c000000000000000000000000000000000000000000000000000000000000006d000000000000000000000000000000000000000000000000000000000000006e000000000000000000000000000000000000000000000000000000000000006f0000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000007100000000000000000000000000000000000000000000000000000000000000720000000000000000000000000000000000000000000000000000000000000073000000000000000000000000000000000000000000000000000000000000007400000000000000000000000000000000000000000000000000000000000000750000000000000000000000000000000000000000000000000000000000000076000000000000000000000000000000000000000000000000000000000000007700000000000000000000000000000000000000000000000000000000000000780000000000000000000000000000000000000000000000000000000000000079000000000000000000000000000000000000000000000000000000000000007a000000000000000000000000000000000000000000000000000000000000007b000000000000000000000000000000000000000000000000000000000000007c000000000000000000000000000000000000000000000000000000000000007d000000000000000000000000000000000000000000000000000000000000007e000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000008100000000000000000000000000000000000000000000000000000000000000820000000000000000000000000000000000000000000000000000000000000083000000000000000000000000000000000000000000000000000000000000008400000000000000000000000000000000000000000000000000000000000000850000000000000000000000000000000000000000000000000000000000000086000000000000000000000000000000000000000000000000000000000000008700000000000000000000000000000000000000000000000000000000000000880000000000000000000000000000000000000000000000000000000000000089000000000000000000000000000000000000000000000000000000000000008a000000000000000000000000000000000000000000000000000000000000008b000000000000000000000000000000000000000000000000000000000000008c000000000000000000000000000000000000000000000000000000000000008d000000000000000000000000000000000000000000000000000000000000008e000000000000000000000000000000000000000000000000000000000000008f0000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009100000000000000000000000000000000000000000000000000000000000000920000000000000000000000000000000000000000000000000000000000000093000000000000000000000000000000000000000000000000000000000000009400000000000000000000000000000000000000000000000000000000000000950000000000000000000000000000000000000000000000000000000000000096000000000000000000000000000000000000000000000000000000000000009700000000000000000000000000000000000000000000000000000000000000980000000000000000000000000000000000000000000000000000000000000099000000000000000000000000000000000000000000000000000000000000009a000000000000000000000000000000000000000000000000000000000000009b000000000000000000000000000000000000000000000000000000000000009c000000000000000000000000000000000000000000000000000000000000009d000000000000000000000000000000000000000000000000000000000000009e000000000000000000000000000000000000000000000000000000000000009f00000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000a100000000000000000000000000000000000000000000000000000000000000a200000000000000000000000000000000000000000000000000000000000000a300000000000000000000000000000000000000000000000000000000000000a400000000000000000000000000000000000000000000000000000000000000a500000000000000000000000000000000000000000000000000000000000000a600000000000000000000000000000000000000000000000000000000000000a700000000000000000000000000000000000000000000000000000000000000a800000000000000000000000000000000000000000000000000000000000000a900000000000000000000000000000000000000000000000000000000000000aa00000000000000000000000000000000000000000000000000000000000000ab00000000000000000000000000000000000000000000000000000000000000ac00000000000000000000000000000000000000000000000000000000000000ad00000000000000000000000000000000000000000000000000000000000000ae00000000000000000000000000000000000000000000000000000000000000af00000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000b100000000000000000000000000000000000000000000000000000000000000b200000000000000000000000000000000000000000000000000000000000000b300000000000000000000000000000000000000000000000000000000000000b400000000000000000000000000000000000000000000000000000000000000b500000000000000000000000000000000000000000000000000000000000000b600000000000000000000000000000000000000000000000000000000000000b700000000000000000000000000000000000000000000000000000000000000b800000000000000000000000000000000000000000000000000000000000000b900000000000000000000000000000000000000000000000000000000000000ba00000000000000000000000000000000000000000000000000000000000000bb00000000000000000000000000000000000000000000000000000000000000bc00000000000000000000000000000000000000000000000000000000000000bd00000000000000000000000000000000000000000000000000000000000000be00000000000000000000000000000000000000000000000000000000000000bf00000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c100000000000000000000000000000000000000000000000000000000000000c200000000000000000000000000000000000000000000000000000000000000c300000000000000000000000000000000000000000000000000000000000000c400000000000000000000000000000000000000000000000000000000000000c500000000000000000000000000000000000000000000000000000000000000c600000000000000000000000000000000000000000000000000000000000000c700000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000c900000000000000000000000000000000000000000000000000000000000000ca00000000000000000000000000000000000000000000000000000000000000cb00000000000000000000000000000000000000000000000000000000000000cc00000000000000000000000000000000000000000000000000000000000000cd00000000000000000000000000000000000000000000000000000000000000ce00000000000000000000000000000000000000000000000000000000000000cf00000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000d100000000000000000000000000000000000000000000000000000000000000d200000000000000000000000000000000000000000000000000000000000000d300000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000d500000000000000000000000000000000000000000000000000000000000000d600000000000000000000000000000000000000000000000000000000000000d700000000000000000000000000000000000000000000000000000000000000d800000000000000000000000000000000000000000000000000000000000000d900000000000000000000000000000000000000000000000000000000000000da00000000000000000000000000000000000000000000000000000000000000db00000000000000000000000000000000000000000000000000000000000000dc00000000000000000000000000000000000000000000000000000000000000dd00000000000000000000000000000000000000000000000000000000000000de00000000000000000000000000000000000000000000000000000000000000df00000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000e100000000000000000000000000000000000000000000000000000000000000e200000000000000000000000000000000000000000000000000000000000000e300000000000000000000000000000000000000000000000000000000000000e400000000000000000000000000000000000000000000000000000000000000e500000000000000000000000000000000000000000000000000000000000000e600000000000000000000000000000000000000000000000000000000000000e700000000000000000000000000000000000000000000000000000000000000e800000000000000000000000000000000000000000000000000000000000000e900000000000000000000000000000000000000000000000000000000000000ea00000000000000000000000000000000000000000000000000000000000000eb00000000000000000000000000000000000000000000000000000000000000ec00000000000000000000000000000000000000000000000000000000000000ed00000000000000000000000000000000000000000000000000000000000000ee00000000000000000000000000000000000000000000000000000000000000ef00000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000000f100000000000000000000000000000000000000000000000000000000000000f200000000000000000000000000000000000000000000000000000000000000f300000000000000000000000000000000000000000000000000000000000000f400000000000000000000000000000000000000000000000000000000000000f500000000000000000000000000000000000000000000000000000000000000f600000000000000000000000000000000000000000000000000000000000000f700000000000000000000000000000000000000000000000000000000000000f800000000000000000000000000000000000000000000000000000000000000f900000000000000000000000000000000000000000000000000000000000000fa00000000000000000000000000000000000000000000000000000000000000fb00000000000000000000000000000000000000000000000000000000000000fc00000000000000000000000000000000000000000000000000000000000000fd00000000000000000000000000000000000000000000000000000000000000fe00000000000000000000000000000000000000000000000000000000000000ff" } ] From 4db448cd56c859f4ce35bff2b505dad67cf7ba97 Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Wed, 29 Apr 2026 03:51:37 -0400 Subject: [PATCH 5/6] lib: fall back to bitcoin-encrypted-backup 0.0.2 for legacy blobs Add a transparent v0 decrypt path so backups produced by the 0.0.2 release stay readable after upgrade. Magic-byte dispatch in set_encrypted_payload routes BIPXXX to the current parser and BEB to a new Payload::DecryptV0 variant; decrypt() detects the variant and delegates to the v0 crate via a private try_v0_decrypt helper that re-maps Decrypted/Error variants. Both crates pin the same miniscript version with identical features, so the Descriptor type crosses the boundary without re-parsing. The v0 dep is gated behind a `v0` cargo feature (on by default); miniscript_12_3_5 / miniscript_12_0 selections propagate to the v0 dep via `?/` so disabling `v0` keeps the dep tree minimal. This is a decrypt-only path - encrypt() never emits BEB, pinned by test_encrypt_never_emits_v0. --- Cargo.toml | 11 ++- src/lib.rs | 248 ++++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 226 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2d0dec3..abf16b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,10 @@ chacha20poly1305 = {version = "0.10.1", default-features = false, features = ["a rand = {version = "0.9.2", optional = true} base64 = {version = "0.22", default-features = false, features = ["alloc"], optional = true} +# Old format (BEB magic, AES-256-GCM): used solely on the decrypt path +# as a transparent fallback for backups produced by 0.0.2. +bitcoin-encrypted-backup-v0 = { version = "0.0.2", package = "bitcoin-encrypted-backup", default-features = false, optional = true } + #miniscript mscript_12_3_5 = { version = "12.3.5", default-features = false, features = ["serde", "no-std"], package = "miniscript" , optional = true} mscript_12_0 = { version = "12.0", default-features = false, features = ["serde", "no-std"], package = "miniscript" , optional = true} @@ -36,12 +40,13 @@ path = "src/bin/main.rs" required-features = ["cli"] [features] -default = ["miniscript_latest", "rand", "base64"] +default = ["miniscript_latest", "rand", "base64", "v0"] rand = ["dep:rand"] base64 = ["dep:base64"] +v0 = ["dep:bitcoin-encrypted-backup-v0"] miniscript_latest = ["miniscript_12_3_5"] -miniscript_12_3_5 = ["mscript_12_3_5"] -miniscript_12_0 = ["mscript_12_0"] +miniscript_12_3_5 = ["mscript_12_3_5", "bitcoin-encrypted-backup-v0?/miniscript_12_3_5"] +miniscript_12_0 = ["mscript_12_0", "bitcoin-encrypted-backup-v0?/miniscript_12_0"] tokio = ["dep:tokio"] devices = ["dep:async-hwi", "tokio"] cli = ["dep:clap", "tokio", "miniscript_latest", "rand"] diff --git a/src/lib.rs b/src/lib.rs index a6a231f..ec8b731 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,6 +46,7 @@ pub enum Warning { /// warnings raised while building the encryption-key set. #[derive(Debug, Clone)] pub struct Encrypted { + pub version: Version, pub bytes: Vec, pub warnings: Vec, } @@ -133,6 +134,13 @@ pub enum Payload { individual_secrets: Vec<[u8; 32]>, nonce: [u8; 12], }, + /// Raw bytes of a backup produced by bitcoin-encrypted-backup 0.0.2 + /// (BEB magic, AES-256-GCM). Decryption is delegated to the v0 crate; + /// this crate never emits this variant from `encrypt()`. + #[cfg(feature = "v0")] + DecryptV0 { + raw: Vec, + }, } impl Payload { @@ -155,7 +163,7 @@ pub struct EncryptedBackup { impl Default for EncryptedBackup { fn default() -> Self { Self { - version: Version::V1, + version: Version::Unknown, content: Content::Unknown, encryption: Encryption::ChaCha20Poly1305, derivation_paths: vec![], @@ -189,10 +197,6 @@ impl EncryptedBackup { self.keys = keys; self } - pub fn set_version(mut self, version: Version) -> Self { - self.version = version; - self - } pub fn set_content_type(mut self, content_type: Content) -> Self { self.content = content_type; self @@ -222,9 +226,11 @@ impl EncryptedBackup { &self.warnings } pub fn encrypt( - self, + mut self, #[cfg(not(feature = "rand"))] nonce: [u8; 12], ) -> Result { + // We only encrypt with last version + self.version = Version::V1; if self.content == Content::Unknown { return Err(Error::UnknownContent); } @@ -251,12 +257,19 @@ impl EncryptedBackup { #[cfg(not(feature = "rand"))] nonce, )?; - Ok(Encrypted { bytes, warnings }) + Ok(Encrypted { + version: self.version, + bytes, + warnings, + }) } _ => Err(Error::NotImplemented), } } - pub fn set_encrypted_payload(self, bytes: &[u8]) -> Result { + pub fn set_encrypted_payload( + #[cfg_attr(not(feature = "v0"), allow(unused_mut))] mut self, + bytes: &[u8], + ) -> Result { // Auto-detect: the binary BIPXXX blob always starts with the // 6-byte ASCII magic "BIPXXX". If the input does not start with // that prefix, try decoding it as standard RFC 4648 base64 (the @@ -265,6 +278,14 @@ impl EncryptedBackup { if bytes.starts_with(ll::MAGIC.as_bytes()) { return self.set_encrypted_payload_binary(bytes); } + #[cfg(feature = "v0")] + if bytes.starts_with(V0_MAGIC) { + self.payload = Payload::DecryptV0 { + raw: bytes.to_vec(), + }; + self.version = Version::V0; + return Ok(self); + } #[cfg(feature = "base64")] { use base64::Engine as _; @@ -279,7 +300,15 @@ impl EncryptedBackup { } fn set_encrypted_payload_binary(mut self, bytes: &[u8]) -> Result { + #[cfg(feature = "v0")] + if bytes.starts_with(V0_MAGIC) { + self.payload = Payload::DecryptV0 { + raw: bytes.to_vec(), + }; + return Ok(self); + } let version: Version = ll::decode_version(bytes).map(|v| v.into())?; + self.version = version; match version { Version::V1 => { let (derivation_paths, individual_secrets, encryption_type, nonce, cyphertext) = @@ -314,9 +343,16 @@ impl EncryptedBackup { if self.keys.is_empty() { return Err(Error::NoKey); } + #[cfg(feature = "v0")] + if let Payload::DecryptV0 { raw } = &self.payload { + return self.try_v0_decrypt(raw); + } + match &self.payload { + Payload::None | Payload::Encrypt { .. } => return Err(Error::WrongPayload), + _ => {} + } match self.version { Version::V1 => match &self.payload { - Payload::None | Payload::Encrypt { .. } => Err(Error::WrongPayload), Payload::DecryptV1 { cyphertext, individual_secrets, @@ -334,13 +370,45 @@ impl EncryptedBackup { } Err(Error::WrongKey) } + _ => Err(Error::WrongPayload), // unreachable }, Version::V0 => Err(Error::NotImplemented), Version::Unknown => Err(Error::UnknownVersion), } } + + /// Decrypt a backup produced by bitcoin-encrypted-backup 0.0.2. + /// Both crates pin the same miniscript version with identical + /// features, so `Descriptor` is the same type + /// across the boundary and no re-parsing is needed. + #[cfg(feature = "v0")] + fn try_v0_decrypt(&self, raw: &[u8]) -> Result { + use bitcoin_encrypted_backup_v0 as v0; + let res = v0::EncryptedBackup::new() + .set_encrypted_payload(raw) + .map_err(|_| Error::WrongPayload)? + .set_keys(self.keys.clone()) + .decrypt() + .map_err(|e| match e { + v0::Error::NoKey => Error::NoKey, + v0::Error::WrongKey => Error::WrongKey, + _ => Error::WrongPayload, + })?; + Ok(match res { + v0::Decrypted::Descriptor(d) => Decrypted::Descriptor(d), + v0::Decrypted::Policy => Decrypted::Policy, + v0::Decrypted::Labels => Decrypted::Labels, + v0::Decrypted::WalletBackup(b) => Decrypted::WalletBackup(b), + v0::Decrypted::Raw(b) => Decrypted::Raw(b), + }) + } } +/// Magic of bitcoin-encrypted-backup 0.0.2 (the published crates.io +/// release). Hard-coded because the v0 crate keeps it as a private const. +#[cfg(feature = "v0")] +const V0_MAGIC: &[u8] = b"BEB"; + #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Encryption { Undefined, @@ -496,15 +564,10 @@ mod tests { backp = backp.set_encryption(Encryption::ChaCha20Poly1305); assert_eq!(backp.get_encryption(), Encryption::ChaCha20Poly1305); - backp = backp.set_version(Version::Unknown); - let fail = backp.clone().encrypt().unwrap_err(); - assert_eq!(fail, Error::InvalidVersion); - backp = backp.set_version(Version::V0); - assert_eq!(backp.get_version(), Version::V0); - backp = backp.set_version(Version::V1); - assert_eq!(backp.get_version(), Version::V1); - - let bytes = backp.encrypt().unwrap().bytes; + let encrypted = backp.encrypt().unwrap(); + // We always encrypt with v1 + assert_eq!(encrypted.version, Version::V1); + let bytes = encrypted.bytes; let fail = EncryptedBackup::new() .set_encrypted_payload(&bytes) @@ -591,18 +654,6 @@ mod tests { .decrypt() .unwrap_err(); assert_eq!(fail, Error::WrongPayload); - - let dummy = dummy_encrypted_payload(); - - // unknown version - let fail = EncryptedBackup::new() - .set_keys(vec![key]) - .set_encrypted_payload(&dummy) - .unwrap() - .set_version(Version::Unknown) - .decrypt() - .unwrap_err(); - assert_eq!(fail, Error::UnknownVersion); } #[test] @@ -1073,3 +1124,140 @@ mod tests { } } } + +#[cfg(all(test, feature = "rand", feature = "v0"))] +mod v0_tests { + use super::*; + use crate::descriptor::dpk_to_pk; + use bitcoin_encrypted_backup_v0 as v0; + use miniscript::bitcoin; + + // The v0 dep is built with default-features = false, so its + // `encrypt(nonce)` signature requires a fixed nonce. + const NONCE: [u8; 12] = [42u8; 12]; + + fn descriptor_and_key() -> (Descriptor, secp256k1::PublicKey) { + let d = descriptor::tests::descr_1(); + let pk = dpk_to_pk(&descriptor::tests::dpk_1()).unwrap(); + (d, pk) + } + + #[test] + fn test_v0_roundtrip_descriptor() { + // Encrypt with the published 0.0.2 crate, decrypt via the current + // crate's transparent fallback. The two crates share the same + // miniscript dep, so the Descriptor type round-trips without + // re-parsing. + let (descriptor, _) = descriptor_and_key(); + + let bytes = v0::EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt(NONCE) + .unwrap(); + + // v0 magic check pins the assumption that BEB is the prefix. + assert!(bytes.starts_with(V0_MAGIC), "v0 blob should start with BEB"); + + let backp = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap(); + let keys = v0::EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .get_keys(); + let restored = backp.set_keys(keys).decrypt().unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + } + + #[test] + fn test_v0_wrong_key_surfaces_wrong_key() { + let (descriptor, _) = descriptor_and_key(); + let bytes = v0::EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt(NONCE) + .unwrap(); + + // Unrelated valid pubkey. + let secp = bitcoin::secp256k1::Secp256k1::new(); + let mut sk = [0u8; 32]; + sk[31] = 99; + let unrelated = bitcoin::secp256k1::PublicKey::from_secret_key( + &secp, + &bitcoin::secp256k1::SecretKey::from_slice(&sk).unwrap(), + ); + + let err = EncryptedBackup::new() + .set_encrypted_payload(&bytes) + .unwrap() + .set_keys(vec![unrelated]) + .decrypt() + .unwrap_err(); + assert_eq!(err, Error::WrongKey); + } + + #[test] + fn test_garbage_returns_error_not_v0_panic() { + // Bytes that match neither magic must return the existing parse + // error path, not get silently routed to v0. + let garbage = b"not-a-backup-of-any-version"; + let err = EncryptedBackup::new() + .set_encrypted_payload(garbage) + .unwrap_err(); + // Either Base64 (auto-detect failed to decode) or Ll (decode_v1 + // failure if base64 happened to decode); both are acceptable + // failures - the point is we don't panic and don't silently + // route to v0. + assert!(matches!(err, Error::Base64 | Error::Ll(_))); + } + + #[cfg(feature = "base64")] + #[test] + fn test_v0_base64_input() { + // base64-wrapped v0 blob: auto-detect must base64-decode first, + // then the inner bytes route to v0 via magic. + use base64::Engine as _; + let (descriptor, _) = descriptor_and_key(); + let bytes = v0::EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt(NONCE) + .unwrap(); + let b64 = base64::engine::general_purpose::STANDARD.encode(&bytes); + + let keys = v0::EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .get_keys(); + let restored = EncryptedBackup::new() + .set_encrypted_payload(b64.as_bytes()) + .unwrap() + .set_keys(keys) + .decrypt() + .unwrap(); + assert_eq!(restored, Decrypted::Descriptor(descriptor)); + } + + #[test] + fn test_encrypt_never_emits_v0() { + // Pin "decrypt-only": the current crate must always produce + // BIPXXX blobs, never BEB. A future refactor cannot accidentally + // re-introduce v0-format output. + let descriptor = descriptor::tests::descr_1(); + let bytes = EncryptedBackup::new() + .set_payload(&descriptor) + .unwrap() + .encrypt() + .unwrap() + .bytes; + assert!( + bytes.starts_with(ll::MAGIC.as_bytes()), + "current encrypt must emit BIPXXX magic" + ); + assert!( + !bytes.starts_with(V0_MAGIC), + "current encrypt must not emit BEB magic" + ); + } +} From 9c0fcf2241bc9e1c58a95ffa66d18626233ccb4b Mon Sep 17 00:00:00 2001 From: pythcoiner Date: Thu, 30 Apr 2026 07:05:37 +0200 Subject: [PATCH 6/6] CI: bump toolchain to 1.85 --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 2fb6b44..ded73ae 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -30,7 +30,7 @@ jobs: strategy: matrix: toolchain: - - 1.82 + - 1.85 - nightly os: - ubuntu-latest @@ -49,7 +49,7 @@ jobs: strategy: matrix: toolchain: - - 1.82 + - 1.85 os: - ubuntu-latest runs-on: ${{ matrix.os }} @@ -69,7 +69,7 @@ jobs: strategy: matrix: toolchain: - - 1.82 + - 1.85 os: - ubuntu-latest runs-on: ${{ matrix.os }}