Skip to content

Commit eb07e6b

Browse files
committed
Update secp256k1 to 0.30.0
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 dc76043 commit eb07e6b

14 files changed

+81
-41
lines changed

Cargo-minimal.lock

+12-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ dependencies = [
6161
"bech32",
6262
"bincode",
6363
"bitcoin-internals",
64-
"bitcoin-io",
64+
"bitcoin-io 0.2.0",
6565
"bitcoin-primitives",
6666
"bitcoin-units",
6767
"bitcoin_hashes 0.16.0",
@@ -100,6 +100,12 @@ dependencies = [
100100
"serde_json",
101101
]
102102

103+
[[package]]
104+
name = "bitcoin-io"
105+
version = "0.1.3"
106+
source = "registry+https://github.com/rust-lang/crates.io-index"
107+
checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf"
108+
103109
[[package]]
104110
name = "bitcoin-io"
105111
version = "0.2.0"
@@ -114,7 +120,7 @@ dependencies = [
114120
"arbitrary",
115121
"bincode",
116122
"bitcoin-internals",
117-
"bitcoin-io",
123+
"bitcoin-io 0.2.0",
118124
"bitcoin-units",
119125
"bitcoin_hashes 0.16.0",
120126
"hex-conservative 0.3.0",
@@ -142,14 +148,15 @@ version = "0.14.0"
142148
source = "registry+https://github.com/rust-lang/crates.io-index"
143149
checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16"
144150
dependencies = [
151+
"bitcoin-io 0.1.3",
145152
"hex-conservative 0.2.0",
146153
]
147154

148155
[[package]]
149156
name = "bitcoin_hashes"
150157
version = "0.16.0"
151158
dependencies = [
152-
"bitcoin-io",
159+
"bitcoin-io 0.2.0",
153160
"hex-conservative 0.3.0",
154161
"serde",
155162
"serde_json",
@@ -389,9 +396,9 @@ checksum = "c92464b447c0ee8c4fb3824ecc8383b81717b9f1e74ba2e72540aef7b9f82997"
389396

390397
[[package]]
391398
name = "secp256k1"
392-
version = "0.29.0"
399+
version = "0.30.0"
393400
source = "registry+https://github.com/rust-lang/crates.io-index"
394-
checksum = "0e0cc0f1cf93f4969faf3ea1c7d8a9faed25918d96affa959720823dfe86d4f3"
401+
checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252"
395402
dependencies = [
396403
"bitcoin_hashes 0.14.0",
397404
"rand",

Cargo-recent.lock

+12-5
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ dependencies = [
6060
"bech32",
6161
"bincode",
6262
"bitcoin-internals",
63-
"bitcoin-io",
63+
"bitcoin-io 0.2.0",
6464
"bitcoin-primitives",
6565
"bitcoin-units",
6666
"bitcoin_hashes 0.16.0",
@@ -99,6 +99,12 @@ dependencies = [
9999
"serde_json",
100100
]
101101

102+
[[package]]
103+
name = "bitcoin-io"
104+
version = "0.1.3"
105+
source = "registry+https://github.com/rust-lang/crates.io-index"
106+
checksum = "0b47c4ab7a93edb0c7198c5535ed9b52b63095f4e9b45279c6736cec4b856baf"
107+
102108
[[package]]
103109
name = "bitcoin-io"
104110
version = "0.2.0"
@@ -113,7 +119,7 @@ dependencies = [
113119
"arbitrary",
114120
"bincode",
115121
"bitcoin-internals",
116-
"bitcoin-io",
122+
"bitcoin-io 0.2.0",
117123
"bitcoin-units",
118124
"bitcoin_hashes 0.16.0",
119125
"hex-conservative 0.3.0",
@@ -141,14 +147,15 @@ version = "0.14.0"
141147
source = "registry+https://github.com/rust-lang/crates.io-index"
142148
checksum = "bb18c03d0db0247e147a21a6faafd5a7eb851c743db062de72018b6b7e8e4d16"
143149
dependencies = [
150+
"bitcoin-io 0.1.3",
144151
"hex-conservative 0.2.1",
145152
]
146153

147154
[[package]]
148155
name = "bitcoin_hashes"
149156
version = "0.16.0"
150157
dependencies = [
151-
"bitcoin-io",
158+
"bitcoin-io 0.2.0",
152159
"hex-conservative 0.3.0",
153160
"serde",
154161
"serde_json",
@@ -390,9 +397,9 @@ checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
390397

391398
[[package]]
392399
name = "secp256k1"
393-
version = "0.29.1"
400+
version = "0.30.0"
394401
source = "registry+https://github.com/rust-lang/crates.io-index"
395-
checksum = "9465315bc9d4566e1724f0fffcbcc446268cb522e60f9a27bcded6b19c108113"
402+
checksum = "b50c5943d326858130af85e049f2661ba3c78b26589b8ab98e65e80ae44a1252"
396403
dependencies = [
397404
"bitcoin_hashes 0.14.0",
398405
"rand",

bitcoin/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ exclude = ["tests", "contrib"]
1717
[features]
1818
default = [ "std", "secp-recovery" ]
1919
std = ["base58/std", "bech32/std", "hashes/std", "hex/std", "internals/std", "io/std", "primitives/std", "secp256k1/std", "units/std", "bitcoinconsensus?/std"]
20-
rand-std = ["secp256k1/rand-std", "std"]
20+
rand-std = ["secp256k1/std", "secp256k1/rand", "std"]
2121
rand = ["secp256k1/rand"]
2222
serde = ["dep:serde", "hashes/serde", "internals/serde", "primitives/serde", "secp256k1/serde", "units/serde"]
2323
secp-lowmemory = ["secp256k1/lowmemory"]
@@ -32,7 +32,7 @@ hex = { package = "hex-conservative", version = "0.3.0", default-features = fals
3232
internals = { package = "bitcoin-internals", version = "0.4.0", features = ["alloc"] }
3333
io = { package = "bitcoin-io", version = "0.2.0", default-features = false, features = ["alloc"] }
3434
primitives = { package = "bitcoin-primitives", version = "0.101.0", default-features = false, features = ["alloc"] }
35-
secp256k1 = { version = "0.29.0", default-features = false, features = ["hashes", "alloc"] }
35+
secp256k1 = { version = "0.30.0", default-features = false, features = ["hashes", "alloc"] }
3636
units = { package = "bitcoin-units", version = "0.2.0", default-features = false, features = ["alloc"] }
3737

3838
arbitrary = { version = "1.4", optional = true }

bitcoin/examples/sighash.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,7 @@ fn compute_sighash_p2wsh(raw_tx: &[u8], inp_idx: usize, amount: Amount) {
125125
assert!((70..=72).contains(&sig_len), "signature length {} out of bounds", sig_len);
126126
//here we assume that all sighash_flags are the same. Can they be different?
127127
let sighash = cache
128-
.p2wsh_signature_hash(
129-
inp_idx,
130-
witness_script,
131-
amount,
132-
sig.sighash_type,
133-
)
128+
.p2wsh_signature_hash(inp_idx, witness_script, amount, sig.sighash_type)
134129
.expect("failed to compute sighash");
135130
println!("SegWit p2wsh sighash: {:x} ({})", sighash, sig.sighash_type);
136131
}

bitcoin/examples/sign-tx-taproot.rs

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

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

bitcoin/examples/taproot-psbt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,7 @@ fn sign_psbt_taproot(
747747
};
748748

749749
let msg = secp256k1::Message::from(hash);
750-
let signature = secp.sign_schnorr(&msg, &keypair);
750+
let signature = secp.sign_schnorr(msg.as_ref(), &keypair);
751751

752752
let final_signature = taproot::Signature { signature, sighash_type };
753753

bitcoin/src/bip32.rs

+19-5
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,11 @@ impl Xpriv {
593593
depth: 0,
594594
parent_fingerprint: Default::default(),
595595
child_number: ChildNumber::ZERO_NORMAL,
596-
private_key: secp256k1::SecretKey::from_slice(&hmac_result.as_ref()[..32])?,
596+
private_key: secp256k1::SecretKey::from_byte_array(
597+
&hmac_result.as_ref()[..32]
598+
.try_into()
599+
.expect("Internal HMAC hash is broken: shorter than 32 bytes"),
600+
)?,
597601
chain_code: ChainCode::from_hmac(hmac_result),
598602
})
599603
}
@@ -665,8 +669,12 @@ impl Xpriv {
665669

666670
hmac_engine.input(&u32::from(i).to_be_bytes());
667671
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
668-
let sk = secp256k1::SecretKey::from_slice(&hmac_result.as_ref()[..32])
669-
.expect("statistically impossible to hit");
672+
let sk = secp256k1::SecretKey::from_byte_array(
673+
&hmac_result.as_ref()[..32]
674+
.try_into()
675+
.expect("HMAC internal hash is broken: shorter than 32 bytes"),
676+
)
677+
.expect("statistically impossible to hit");
670678
let tweaked =
671679
sk.add_tweak(&self.private_key.into()).expect("statistically impossible to hit");
672680

@@ -705,7 +713,9 @@ impl Xpriv {
705713
chain_code: data[13..45]
706714
.try_into()
707715
.expect("45 - 13 == 32, which is the ChainCode length"),
708-
private_key: secp256k1::SecretKey::from_slice(&data[46..78])?,
716+
private_key: secp256k1::SecretKey::from_byte_array(
717+
data[46..78].try_into().expect("78 - 46 == 32, which is the SecretKey length"),
718+
)?,
709719
})
710720
}
711721

@@ -813,7 +823,11 @@ impl Xpub {
813823

814824
let hmac_result: Hmac<sha512::Hash> = Hmac::from_engine(hmac_engine);
815825

816-
let private_key = secp256k1::SecretKey::from_slice(&hmac_result.as_ref()[..32])?;
826+
let private_key = secp256k1::SecretKey::from_byte_array(
827+
&hmac_result.as_ref()[..32]
828+
.try_into()
829+
.expect("HMAC internal hash is broken: shorter than 32 bytes"),
830+
)?;
817831
let chain_code = ChainCode::from_hmac(hmac_result);
818832
Ok((private_key, chain_code))
819833
}

bitcoin/src/crypto/key.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,12 @@ impl PrivateKey {
464464
data: &[u8],
465465
network: impl Into<NetworkKind>,
466466
) -> Result<PrivateKey, secp256k1::Error> {
467-
Ok(PrivateKey::new(secp256k1::SecretKey::from_slice(data)?, network))
467+
Ok(PrivateKey::new(
468+
secp256k1::SecretKey::from_byte_array(
469+
data.try_into().map_err(|_| secp256k1::Error::InvalidSecretKey)?,
470+
)?,
471+
network,
472+
))
468473
}
469474

470475
/// Formats the private key to WIF format.
@@ -514,7 +519,9 @@ impl PrivateKey {
514519
Ok(PrivateKey {
515520
compressed,
516521
network,
517-
inner: secp256k1::SecretKey::from_slice(&data[1..33])?,
522+
inner: secp256k1::SecretKey::from_byte_array(
523+
data[1..33].try_into().expect("slice len checked == 32"),
524+
)?,
518525
})
519526
}
520527
}

bitcoin/src/crypto/sighash.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2012,7 +2012,8 @@ mod tests {
20122012
.unwrap();
20132013

20142014
let msg = secp256k1::Message::from(sighash);
2015-
let key_spend_sig = secp.sign_schnorr_with_aux_rand(&msg, &tweaked_keypair, &[0u8; 32]);
2015+
let key_spend_sig =
2016+
secp.sign_schnorr_with_aux_rand(msg.as_ref(), &tweaked_keypair, &[0u8; 32]);
20162017

20172018
assert_eq!(expected.internal_pubkey, internal_key);
20182019
assert_eq!(expected.tweak, tweak);

bitcoin/src/crypto/taproot.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ impl Signature {
3232
match sl.len() {
3333
64 => {
3434
// default type
35-
let signature = secp256k1::schnorr::Signature::from_slice(sl)?;
35+
let signature = secp256k1::schnorr::Signature::from_byte_array(
36+
sl.try_into().expect("slice len checked == 64"),
37+
);
3638
Ok(Signature { signature, sighash_type: TapSighashType::Default })
3739
}
3840
65 => {
3941
let (sighash_type, signature) = sl.split_last().expect("slice len checked == 65");
4042
let sighash_type = TapSighashType::from_consensus_u8(*sighash_type)?;
41-
let signature = secp256k1::schnorr::Signature::from_slice(signature)?;
43+
let signature = secp256k1::schnorr::Signature::from_byte_array(
44+
signature.try_into().expect("slice len checked == 64"),
45+
);
4246
Ok(Signature { signature, sighash_type })
4347
}
4448
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
@@ -140,7 +144,7 @@ impl<'a> Arbitrary<'a> for Signature {
140144
let arbitrary_bytes: [u8; secp256k1::constants::SCHNORR_SIGNATURE_SIZE] = u.arbitrary()?;
141145

142146
Ok(Signature {
143-
signature: secp256k1::schnorr::Signature::from_slice(&arbitrary_bytes).unwrap(),
147+
signature: secp256k1::schnorr::Signature::from_byte_array(arbitrary_bytes),
144148
sighash_type: TapSighashType::arbitrary(u)?,
145149
})
146150
}

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
@@ -260,7 +260,8 @@ impl Serialize for XOnlyPublicKey {
260260

261261
impl Deserialize for XOnlyPublicKey {
262262
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
263-
XOnlyPublicKey::from_slice(bytes).map_err(|_| Error::InvalidXOnlyPublicKey)
263+
XOnlyPublicKey::from_byte_array(bytes.try_into().map_err(|_| Error::InvalidXOnlyPublicKey)?)
264+
.map_err(|_| Error::InvalidXOnlyPublicKey)
264265
}
265266
}
266267

bitcoin/src/sign_message.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ mod message_signing {
104104
pub fn serialize(&self) -> [u8; 65] {
105105
let (recid, raw) = self.signature.serialize_compact();
106106
let mut serialized = [0u8; 65];
107-
serialized[0] = recid.to_i32() as u8 + if self.compressed { 31 } else { 27 };
107+
serialized[0] = i32::from(recid) as u8 + if self.compressed { 31 } else { 27 };
108108
serialized[1..].copy_from_slice(&raw[..]);
109109
serialized
110110
}
@@ -115,7 +115,7 @@ mod message_signing {
115115
if bytes[0] < 27 {
116116
return Err(secp256k1::Error::InvalidRecoveryId);
117117
};
118-
let recid = RecoveryId::from_i32(((bytes[0] - 27) & 0x03) as i32)?;
118+
let recid = RecoveryId::try_from(((bytes[0] - 27) & 0x03) as i32)?;
119119
Ok(MessageSignature {
120120
signature: RecoverableSignature::from_compact(&bytes[1..], recid)?,
121121
compressed: ((bytes[0] - 27) & 0x04) != 0,

bitcoin/src/taproot/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1121,8 +1121,12 @@ impl ControlBlock {
11211121
};
11221122

11231123
let leaf_version = LeafVersion::from_consensus(sl[0] & TAPROOT_LEAF_MASK)?;
1124-
let internal_key = UntweakedPublicKey::from_slice(&sl[1..TAPROOT_CONTROL_BASE_SIZE])
1125-
.map_err(TaprootError::InvalidInternalKey)?;
1124+
let internal_key = UntweakedPublicKey::from_byte_array(
1125+
sl[1..TAPROOT_CONTROL_BASE_SIZE].try_into().map_err(|_| {
1126+
TaprootError::InvalidInternalKey(secp256k1::Error::InvalidPublicKey)
1127+
})?,
1128+
)
1129+
.map_err(TaprootError::InvalidInternalKey)?;
11261130
let merkle_branch = TaprootMerkleBranch::decode(&sl[TAPROOT_CONTROL_BASE_SIZE..])?;
11271131
Ok(ControlBlock { leaf_version, output_key_parity, internal_key, merkle_branch })
11281132
}

0 commit comments

Comments
 (0)