From 0e5e021b690c5dccea18b44e33137c888164023b Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Mon, 21 Apr 2025 22:19:30 +0000 Subject: [PATCH] bip32: change several cryptographically unreachable paths to expects These paths cannot be reached. In general, key derivation cannot fail for cryptographic reasons. --- bitcoin/src/bip32.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bitcoin/src/bip32.rs b/bitcoin/src/bip32.rs index b335b09b3..b1bf0bc18 100644 --- a/bitcoin/src/bip32.rs +++ b/bitcoin/src/bip32.rs @@ -610,7 +610,8 @@ impl Xpriv { child_number: ChildNumber::ZERO_NORMAL, private_key: secp256k1::SecretKey::from_byte_array( hmac.as_byte_array().split_array::<32, 32>().0, - )?, + ) + .expect("cryptographically unreachable"), chain_code: ChainCode::from_hmac(hmac), }) } @@ -828,7 +829,8 @@ impl Xpub { let hmac = engine.finalize(); let private_key = secp256k1::SecretKey::from_byte_array( hmac.as_byte_array().split_array::<32, 32>().0, - )?; + ) + .expect("cryptographically unreachable"); let chain_code = ChainCode::from_hmac(hmac); Ok((private_key, chain_code)) } @@ -842,7 +844,8 @@ impl Xpub { i: ChildNumber, ) -> Result { let (sk, chain_code) = self.ckd_pub_tweak(i)?; - let tweaked = self.public_key.add_exp_tweak(secp, &sk.into())?; + let tweaked = + self.public_key.add_exp_tweak(secp, &sk.into()).expect("cryptographically unreachable"); Ok(Xpub { network: self.network,