Skip to content

Commit 2021778

Browse files
committed
Update secp256k1 to 0.30.0 (v0.32.5 branch in bitcoin)
Migrated `from_slice` usage to `from_byte_array` with proper error forwarding when needed. Converted to Message::as_ref<[u8]> for signing due to API change of receiving arbitrary byte arrays in lieu of Message hashes only. Updated lockfiles. `from_slice` has been deprecated by rust-bitcoin#3102 due to better support for arrays in Rust. BIP340 supports arbitrary byte arrays as discussed on rust-bitcoin/rust-secp256k1#706 Closes rust-bitcoin#3482
1 parent 17ce61c commit 2021778

13 files changed

+59
-32
lines changed

Cargo-minimal.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ dependencies = [
352352

353353
[[package]]
354354
name = "secp256k1"
355-
version = "0.29.0"
355+
version = "0.30.0"
356356
source = "registry+https://github.com/rust-lang/crates.io-index"
357-
checksum = "0e0cc0f1cf93f4969faf3ea1c7d8a9faed25918d96affa959720823dfe86d4f3"
357+
checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252"
358358
dependencies = [
359359
"bitcoin_hashes",
360360
"rand",

Cargo-recent.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ dependencies = [
341341

342342
[[package]]
343343
name = "secp256k1"
344-
version = "0.29.0"
344+
version = "0.30.0"
345345
source = "registry+https://github.com/rust-lang/crates.io-index"
346-
checksum = "0e0cc0f1cf93f4969faf3ea1c7d8a9faed25918d96affa959720823dfe86d4f3"
346+
checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252"
347347
dependencies = [
348348
"bitcoin_hashes",
349349
"rand",

bitcoin/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ exclude = ["tests", "contrib"]
1616
[features]
1717
default = [ "std", "secp-recovery" ]
1818
std = ["base58/std", "bech32/std", "hashes/std", "hex/std", "internals/std", "io/std", "secp256k1/std", "units/std"]
19-
rand-std = ["secp256k1/rand-std", "std"]
19+
rand-std = ["secp256k1/rand", "secp256k1/std", "std"]
2020
rand = ["secp256k1/rand"]
2121
serde = ["actual-serde", "hashes/serde", "secp256k1/serde", "internals/serde", "units/serde"]
2222
secp-lowmemory = ["secp256k1/lowmemory"]
@@ -35,7 +35,7 @@ hex = { package = "hex-conservative", version = "0.2.0", default-features = fals
3535
hex_lit = "0.1.1"
3636
internals = { package = "bitcoin-internals", version = "0.3.0", features = ["alloc"] }
3737
io = { package = "bitcoin-io", version = "0.1.1", default-features = false, features = ["alloc"] }
38-
secp256k1 = { version = "0.29.0", default-features = false, features = ["hashes", "alloc"] }
38+
secp256k1 = { version = "0.30.0", default-features = false, features = ["hashes", "alloc"] }
3939
units = { package = "bitcoin-units", version = "0.1.0", default-features = false, features = ["alloc"] }
4040

4141
base64 = { version = "0.21.3", optional = true }

bitcoin/examples/sign-tx-taproot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn main() {
7272
// Sign the sighash using the secp256k1 library (exported by rust-bitcoin).
7373
let tweaked: TweakedKeypair = keypair.tap_tweak(&secp, None);
7474
let msg = Message::from(sighash);
75-
let signature = secp.sign_schnorr(&msg, &tweaked.to_inner());
75+
let signature = secp.sign_schnorr(msg.as_ref(), &tweaked.to_inner());
7676

7777
// Update the witness stack.
7878
let signature = bitcoin::taproot::Signature { signature, sighash_type };

bitcoin/examples/taproot-psbt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ fn sign_psbt_taproot(
739739
};
740740

741741
let msg = secp256k1::Message::from(hash);
742-
let signature = secp.sign_schnorr(&msg, &keypair);
742+
let signature = secp.sign_schnorr(msg.as_ref(), &keypair);
743743

744744
let final_signature = taproot::Signature { signature, sighash_type };
745745

bitcoin/src/bip32.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,11 @@ impl Xpriv {
574574
depth: 0,
575575
parent_fingerprint: Default::default(),
576576
child_number: ChildNumber::from_normal_idx(0)?,
577-
private_key: secp256k1::SecretKey::from_slice(&hmac_result[..32])?,
577+
private_key: secp256k1::SecretKey::from_byte_array(
578+
&hmac_result.as_byte_array()[..32]
579+
.try_into()
580+
.expect("Internal HMAC hash is broken: shorter than 32 bytes"),
581+
)?,
578582
chain_code: ChainCode::from_hmac(hmac_result),
579583
})
580584
}
@@ -629,8 +633,12 @@ impl Xpriv {
629633

630634
hmac_engine.input(&u32::from(i).to_be_bytes());
631635
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
632-
let sk = secp256k1::SecretKey::from_slice(&hmac_result[..32])
633-
.expect("statistically impossible to hit");
636+
let sk = secp256k1::SecretKey::from_byte_array(
637+
&hmac_result.as_byte_array()[..32]
638+
.try_into()
639+
.expect("HMAC internal hash is broken: shorter than 32 bytes"),
640+
)
641+
.expect("statistically impossible to hit");
634642
let tweaked =
635643
sk.add_tweak(&self.private_key.into()).expect("statistically impossible to hit");
636644

@@ -669,7 +677,9 @@ impl Xpriv {
669677
chain_code: data[13..45]
670678
.try_into()
671679
.expect("45 - 13 == 32, which is the ChainCode length"),
672-
private_key: secp256k1::SecretKey::from_slice(&data[46..78])?,
680+
private_key: secp256k1::SecretKey::from_byte_array(
681+
data[46..78].try_into().expect("78 - 46 == 32, which is the SecretKey length"),
682+
)?,
673683
})
674684
}
675685

@@ -750,7 +760,11 @@ impl Xpub {
750760

751761
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
752762

753-
let private_key = secp256k1::SecretKey::from_slice(&hmac_result[..32])?;
763+
let private_key = secp256k1::SecretKey::from_byte_array(
764+
&hmac_result.as_byte_array()[..32]
765+
.try_into()
766+
.expect("HMAC internal hash is broken: shorter than 32 bytes"),
767+
)?;
754768
let chain_code = ChainCode::from_hmac(hmac_result);
755769
Ok((private_key, chain_code))
756770
}

bitcoin/src/crypto/key.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,12 @@ impl PrivateKey {
443443
data: &[u8],
444444
network: impl Into<NetworkKind>,
445445
) -> Result<PrivateKey, secp256k1::Error> {
446-
Ok(PrivateKey::new(secp256k1::SecretKey::from_slice(data)?, network))
446+
Ok(PrivateKey::new(
447+
secp256k1::SecretKey::from_byte_array(
448+
data.try_into().map_err(|_| secp256k1::Error::InvalidSecretKey)?,
449+
)?,
450+
network,
451+
))
447452
}
448453

449454
/// Format the private key to WIF format.
@@ -493,7 +498,9 @@ impl PrivateKey {
493498
Ok(PrivateKey {
494499
compressed,
495500
network,
496-
inner: secp256k1::SecretKey::from_slice(&data[1..33])?,
501+
inner: secp256k1::SecretKey::from_byte_array(
502+
data[1..33].try_into().expect("slice len checked == 32"),
503+
)?,
497504
})
498505
}
499506
}

bitcoin/src/crypto/sighash.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,8 @@ mod tests {
19381938
.unwrap();
19391939

19401940
let msg = secp256k1::Message::from(sighash);
1941-
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);
1941+
let key_spend_sig =
1942+
secp.sign_schnorr_with_aux_rand(msg.as_ref(), &tweaked_keypair, &[0u8; 32]);
19421943

19431944
assert_eq!(expected.internal_pubkey, internal_key);
19441945
assert_eq!(expected.tweak, tweak);

bitcoin/src/crypto/taproot.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,17 @@ impl Signature {
3131
match sl.len() {
3232
64 => {
3333
// default type
34-
let signature = secp256k1::schnorr::Signature::from_slice(sl)?;
34+
let signature = secp256k1::schnorr::Signature::from_byte_array(
35+
sl.try_into().expect("slice len checked == 64"),
36+
);
3537
Ok(Signature { signature, sighash_type: TapSighashType::Default })
3638
}
3739
65 => {
3840
let (sighash_type, signature) = sl.split_last().expect("Slice len checked == 65");
3941
let sighash_type = TapSighashType::from_consensus_u8(*sighash_type)?;
40-
let signature = secp256k1::schnorr::Signature::from_slice(signature)?;
42+
let signature = secp256k1::schnorr::Signature::from_byte_array(
43+
signature.try_into().expect("slice len checked == 64"),
44+
);
4145
Ok(Signature { signature, sighash_type })
4246
}
4347
len => Err(SigFromSliceError::InvalidSignatureSize(len)),
@@ -70,7 +74,7 @@ impl Signature {
7074
/// You can get a slice from it using deref coercions or turn it into an iterator.
7175
pub fn serialize(self) -> SerializedSignature {
7276
let mut buf = [0; serialized_signature::MAX_LEN];
73-
let ser_sig = self.signature.serialize();
77+
let ser_sig = self.signature.to_byte_array();
7478
buf[..64].copy_from_slice(&ser_sig);
7579
let len = if self.sighash_type == TapSighashType::Default {
7680
// default sighash type, don't add extra sighash byte

bitcoin/src/psbt/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,9 @@ impl Psbt {
445445
.to_inner();
446446

447447
#[cfg(feature = "rand-std")]
448-
let signature = secp.sign_schnorr(&msg, &key_pair);
448+
let signature = secp.sign_schnorr(msg.as_ref(), &key_pair);
449449
#[cfg(not(feature = "rand-std"))]
450-
let signature = secp.sign_schnorr_no_aux_rand(&msg, &key_pair);
450+
let signature = secp.sign_schnorr_no_aux_rand(msg.as_ref(), &key_pair);
451451

452452
let signature = taproot::Signature { signature, sighash_type };
453453
input.tap_key_sig = Some(signature);
@@ -472,9 +472,9 @@ impl Psbt {
472472
self.sighash_taproot(input_index, cache, Some(lh))?;
473473

474474
#[cfg(feature = "rand-std")]
475-
let signature = secp.sign_schnorr(&msg, &key_pair);
475+
let signature = secp.sign_schnorr(msg.as_ref(), &key_pair);
476476
#[cfg(not(feature = "rand-std"))]
477-
let signature = secp.sign_schnorr_no_aux_rand(&msg, &key_pair);
477+
let signature = secp.sign_schnorr_no_aux_rand(msg.as_ref(), &key_pair);
478478

479479
let signature = taproot::Signature { signature, sighash_type };
480480
input.tap_script_sigs.insert((xonly, lh), signature);

bitcoin/src/psbt/serialize.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,8 @@ impl Serialize for XOnlyPublicKey {
262262

263263
impl Deserialize for XOnlyPublicKey {
264264
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
265-
XOnlyPublicKey::from_slice(bytes).map_err(|_| Error::InvalidXOnlyPublicKey)
265+
XOnlyPublicKey::from_byte_array(bytes.try_into().map_err(|_| Error::InvalidXOnlyPublicKey)?)
266+
.map_err(|_| Error::InvalidXOnlyPublicKey)
266267
}
267268
}
268269

bitcoin/src/sign_message.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,7 @@ mod message_signing {
100100
pub fn serialize(&self) -> [u8; 65] {
101101
let (recid, raw) = self.signature.serialize_compact();
102102
let mut serialized = [0u8; 65];
103-
serialized[0] = 27;
104-
serialized[0] += recid.to_i32() as u8;
105-
if self.compressed {
106-
serialized[0] += 4;
107-
}
103+
serialized[0] = i32::from(recid) as u8 + if self.compressed { 31 } else { 27 };
108104
serialized[1..].copy_from_slice(&raw[..]);
109105
serialized
110106
}
@@ -120,7 +116,7 @@ mod message_signing {
120116
secp256k1::Error::InvalidRecoveryId,
121117
));
122118
};
123-
let recid = RecoveryId::from_i32(((bytes[0] - 27) & 0x03) as i32)?;
119+
let recid = RecoveryId::try_from(((bytes[0] - 27) & 0x03) as i32)?;
124120
Ok(MessageSignature {
125121
signature: RecoverableSignature::from_compact(&bytes[1..], recid)?,
126122
compressed: ((bytes[0] - 27) & 0x04) != 0,

bitcoin/src/taproot/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,12 @@ impl ControlBlock {
11031103
};
11041104

11051105
let leaf_version = LeafVersion::from_consensus(sl[0] & TAPROOT_LEAF_MASK)?;
1106-
let internal_key = UntweakedPublicKey::from_slice(&sl[1..TAPROOT_CONTROL_BASE_SIZE])
1107-
.map_err(TaprootError::InvalidInternalKey)?;
1106+
let internal_key = UntweakedPublicKey::from_byte_array(
1107+
sl[1..TAPROOT_CONTROL_BASE_SIZE].try_into().map_err(|_| {
1108+
TaprootError::InvalidInternalKey(secp256k1::Error::InvalidPublicKey)
1109+
})?,
1110+
)
1111+
.map_err(TaprootError::InvalidInternalKey)?;
11081112
let merkle_branch = TaprootMerkleBranch::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?;
11091113
Ok(ControlBlock { leaf_version, output_key_parity, internal_key, merkle_branch })
11101114
}

0 commit comments

Comments
 (0)