Skip to content

Commit a05078f

Browse files
committed
Merge #686: Deprecate ThirtyTwoByteHash
9f28cf6 Deprecate ThirtyTwoByteHash (Tobin C. Harding) 88c8c58 Fix import warnings (Tobin C. Harding) Pull request description: The implementations of `ThirtyTwoByteHash` for types from the `hashes` crate are problematic during upgrades because both `bitcoin` and `secp256k1` depend on `hashes` and when the versions of `hashes` get out of sync usage of the trait breaks. Deprecate the `ThirtyTwoByteHash` trait and remove the impls for types from `bitcoin_hashes`. Add an explanation in the changelog because its too long to go in the deprecation message. Close: #673 ACKs for top commit: apoelstra: ACK 9f28cf6 Tree-SHA512: a7598b09c6a2f49913a9effad3e1ed9b0663970ac20fbfe5fc2f1329daaa2b4cab78b00128a03d0f3f6559ed7964b09e0bd939e60cf130b2cc6e609c90df6868
2 parents d279c13 + 9f28cf6 commit a05078f

File tree

6 files changed

+26
-106
lines changed

6 files changed

+26
-106
lines changed

CHANGELOG.md

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Unreleased
22

3+
* Deprecate `ThirtyTwoByteHash`
4+
5+
This trait turned out to be problematic during upgrade because we support a ranged dependency for
6+
`bitcoin_hashes`. Consider implementing `From<T> for Message` for your type iff your type is a 32
7+
byte hash (ie, output from a hash algorithm that produces a 32 byte digest like sha256). When
8+
using the impl, consider using `Message::from` instead of `hash.into()` because we will be
9+
introducing generics in a future version and the compiler will not be able to work out the target
10+
type.
11+
312
* Bump MSRV to Rust `v1.56.1`
413
* Upgrade `hashes` using range dependency `version = ">= 0.12, <= 0.14"`.
514

api/all-features.txt

-8
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,6 @@ impl secp256k1_sys::CPtr for secp256k1::PublicKey
519519
impl secp256k1_sys::CPtr for secp256k1::schnorr::Signature
520520
impl secp256k1_sys::CPtr for secp256k1::SecretKey
521521
impl secp256k1_sys::CPtr for secp256k1::XOnlyPublicKey
522-
impl secp256k1::ThirtyTwoByteHash for bitcoin_hashes::sha256d::Hash
523-
impl secp256k1::ThirtyTwoByteHash for bitcoin_hashes::sha256::Hash
524522
impl secp256k1::Verification for secp256k1::All
525523
impl secp256k1::Verification for secp256k1::VerifyOnly
526524
impl secp256k1::XOnlyPublicKey
@@ -532,7 +530,6 @@ impl serde::ser::Serialize for secp256k1::PublicKey
532530
impl serde::ser::Serialize for secp256k1::schnorr::Signature
533531
impl serde::ser::Serialize for secp256k1::SecretKey
534532
impl serde::ser::Serialize for secp256k1::XOnlyPublicKey
535-
impl<T: bitcoin_hashes::sha256t::Tag> secp256k1::ThirtyTwoByteHash for bitcoin_hashes::sha256t::Hash<T>
536533
impl<T: secp256k1::ThirtyTwoByteHash> core::convert::From<T> for secp256k1::Message
537534
impl<T: secp256k1::ThirtyTwoByteHash> core::convert::From<T> for secp256k1::SecretKey
538535
#[non_exhaustive] pub struct secp256k1::scalar::OutOfRangeError
@@ -580,9 +577,6 @@ pub enum secp256k1::SignOnly
580577
pub enum secp256k1::VerifyOnly
581578
pub extern crate secp256k1::hashes
582579
pub fn &'a secp256k1::ecdsa::serialized_signature::SerializedSignature::into_iter(self) -> Self::IntoIter
583-
pub fn bitcoin_hashes::sha256d::Hash::into_32(self) -> [u8; 32]
584-
pub fn bitcoin_hashes::sha256::Hash::into_32(self) -> [u8; 32]
585-
pub fn bitcoin_hashes::sha256t::Hash<T>::into_32(self) -> [u8; 32]
586580
pub fn i32::from(parity: secp256k1::Parity) -> i32
587581
pub fn secp256k1::All::clone(&self) -> secp256k1::All
588582
pub fn secp256k1::All::cmp(&self, other: &secp256k1::All) -> core::cmp::Ordering
@@ -772,7 +766,6 @@ pub fn secp256k1::Message::eq(&self, other: &secp256k1::Message) -> bool
772766
pub fn secp256k1::Message::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
773767
pub fn secp256k1::Message::from_digest(digest: [u8; 32]) -> secp256k1::Message
774768
pub fn secp256k1::Message::from_digest_slice(digest: &[u8]) -> core::result::Result<secp256k1::Message, secp256k1::Error>
775-
pub fn secp256k1::Message::from_hashed_data<H: secp256k1::ThirtyTwoByteHash + bitcoin_hashes::Hash>(data: &[u8]) -> Self
776769
pub fn secp256k1::Message::from_slice(digest: &[u8]) -> core::result::Result<secp256k1::Message, secp256k1::Error>
777770
pub fn secp256k1::Message::from(t: T) -> secp256k1::Message
778771
pub fn secp256k1::Message::hash<__H: core::hash::Hasher>(&self, state: &mut __H)
@@ -904,7 +897,6 @@ pub fn secp256k1::SecretKey::deserialize<D: serde::de::Deserializer<'de>>(d: D)
904897
pub fn secp256k1::SecretKey::display_secret(&self) -> DisplaySecret
905898
pub fn secp256k1::SecretKey::eq(&self, other: &Self) -> bool
906899
pub fn secp256k1::SecretKey::fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result
907-
pub fn secp256k1::SecretKey::from_hashed_data<H: secp256k1::ThirtyTwoByteHash + bitcoin_hashes::Hash>(data: &[u8]) -> Self
908900
pub fn secp256k1::SecretKey::from_keypair(keypair: &secp256k1::Keypair) -> Self
909901
pub fn secp256k1::SecretKey::from(pair: &'a secp256k1::Keypair) -> Self
910902
pub fn secp256k1::SecretKey::from(pair: secp256k1::Keypair) -> Self

src/ecdsa/serialized_signature.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! serialized signatures and since it's a bit more complicated it has its own module.
99
1010
use core::borrow::Borrow;
11-
use core::convert::TryFrom;
1211
use core::{fmt, ops};
1312

1413
pub use into_iter::IntoIter;

src/key.rs

+4-27
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
//! Public and secret keys.
44
//!
55
6-
use core::convert::TryFrom;
76
use core::ops::{self, BitXor};
87
use core::{fmt, ptr, str};
98

@@ -14,13 +13,14 @@ use crate::ellswift::ElligatorSwift;
1413
use crate::ffi::types::c_uint;
1514
use crate::ffi::{self, CPtr};
1615
use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
16+
#[cfg(feature = "hashes")]
17+
#[allow(deprecated)]
18+
use crate::ThirtyTwoByteHash;
1719
#[cfg(feature = "global-context")]
1820
use crate::SECP256K1;
1921
use crate::{
2022
constants, ecdsa, from_hex, schnorr, Message, Scalar, Secp256k1, Signing, Verification,
2123
};
22-
#[cfg(feature = "hashes")]
23-
use crate::{hashes, ThirtyTwoByteHash};
2424

2525
/// Secret key - a 256-bit key used to create ECDSA and Taproot signatures.
2626
///
@@ -257,30 +257,6 @@ impl SecretKey {
257257
SecretKey(sk)
258258
}
259259

260-
/// Constructs a [`SecretKey`] by hashing `data` with hash algorithm `H`.
261-
///
262-
/// Requires the feature `hashes` to be enabled.
263-
///
264-
/// # Examples
265-
///
266-
/// ```
267-
/// # #[cfg(feature="hashes")] {
268-
/// use secp256k1::hashes::{sha256, Hash};
269-
/// use secp256k1::SecretKey;
270-
///
271-
/// let sk1 = SecretKey::from_hashed_data::<sha256::Hash>("Hello world!".as_bytes());
272-
/// // is equivalent to
273-
/// let sk2 = SecretKey::from(sha256::Hash::hash("Hello world!".as_bytes()));
274-
///
275-
/// assert_eq!(sk1, sk2);
276-
/// # }
277-
/// ```
278-
#[cfg(feature = "hashes")]
279-
#[inline]
280-
pub fn from_hashed_data<H: ThirtyTwoByteHash + hashes::Hash>(data: &[u8]) -> Self {
281-
<H as hashes::Hash>::hash(data).into()
282-
}
283-
284260
/// Returns the secret key as a byte value.
285261
#[inline]
286262
pub fn secret_bytes(&self) -> [u8; constants::SECRET_KEY_SIZE] { self.0 }
@@ -373,6 +349,7 @@ impl SecretKey {
373349
}
374350

375351
#[cfg(feature = "hashes")]
352+
#[allow(deprecated)]
376353
impl<T: ThirtyTwoByteHash> From<T> for SecretKey {
377354
/// Converts a 32-byte hash directly to a secret key without error paths.
378355
fn from(t: T) -> SecretKey {

src/lib.rs

+12-67
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
//! # #[cfg(all(feature = "rand-std", feature = "hashes-std"))] {
3232
//! use secp256k1::rand::rngs::OsRng;
3333
//! use secp256k1::{Secp256k1, Message};
34-
//! use secp256k1::hashes::sha256;
34+
//! use secp256k1::hashes::{sha256, Hash};
3535
//!
3636
//! let secp = Secp256k1::new();
3737
//! let (secret_key, public_key) = secp.generate_keypair(&mut OsRng);
38-
//! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
38+
//! let digest = sha256::Hash::hash("Hello World!".as_bytes());
39+
//! let message = Message::from_digest(digest.to_byte_array());
3940
//!
4041
//! let sig = secp.sign_ecdsa(&message, &secret_key);
4142
//! assert!(secp.verify_ecdsa(&message, &sig, &public_key).is_ok());
@@ -47,10 +48,11 @@
4748
//! ```rust
4849
//! # #[cfg(all(feature = "global-context", feature = "hashes-std", feature = "rand-std"))] {
4950
//! use secp256k1::{generate_keypair, Message};
50-
//! use secp256k1::hashes::sha256;
51+
//! use secp256k1::hashes::{sha256, Hash};
5152
//!
5253
//! let (secret_key, public_key) = generate_keypair(&mut rand::thread_rng());
53-
//! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
54+
//! let digest = sha256::Hash::hash("Hello World!".as_bytes());
55+
//! let message = Message::from_digest(digest.to_byte_array());
5456
//!
5557
//! let sig = secret_key.sign_ecdsa(message);
5658
//! assert!(sig.verify(&message, &public_key).is_ok());
@@ -176,8 +178,6 @@ use core::{fmt, mem, str};
176178

177179
#[cfg(all(feature = "global-context", feature = "std"))]
178180
pub use context::global::{self, SECP256K1};
179-
#[cfg(feature = "hashes")]
180-
use hashes::Hash;
181181
#[cfg(feature = "rand")]
182182
pub use rand;
183183
pub use secp256k1_sys as ffi;
@@ -198,34 +198,23 @@ pub use crate::scalar::Scalar;
198198
/// Trait describing something that promises to be a 32-byte random number; in particular,
199199
/// it has negligible probability of being zero or overflowing the group order. Such objects
200200
/// may be converted to `Message`s without any error paths.
201+
#[deprecated(
202+
since = "0.29.0",
203+
note = "Please see v0.29.0 rust-secp256k1/CHANGELOG.md for suggestion"
204+
)]
201205
pub trait ThirtyTwoByteHash {
202206
/// Converts the object into a 32-byte array
203207
fn into_32(self) -> [u8; 32];
204208
}
205209

206-
#[cfg(feature = "hashes")]
207-
impl ThirtyTwoByteHash for hashes::sha256::Hash {
208-
fn into_32(self) -> [u8; 32] { self.to_byte_array() }
209-
}
210-
211-
#[cfg(feature = "hashes")]
212-
impl ThirtyTwoByteHash for hashes::sha256d::Hash {
213-
fn into_32(self) -> [u8; 32] { self.to_byte_array() }
214-
}
215-
216-
#[cfg(feature = "hashes")]
217-
impl<T: hashes::sha256t::Tag> ThirtyTwoByteHash for hashes::sha256t::Hash<T> {
218-
fn into_32(self) -> [u8; 32] { self.to_byte_array() }
219-
}
220-
221210
/// A (hashed) message input to an ECDSA signature.
222211
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
223212
pub struct Message([u8; constants::MESSAGE_SIZE]);
224213
impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE);
225214
impl_pretty_debug!(Message);
226215

227216
impl Message {
228-
/// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
217+
/// Creates a [`Message`] from a 32 byte slice `digest`.
229218
///
230219
/// Converts a `MESSAGE_SIZE`-byte slice to a message object. **WARNING:** the slice has to be a
231220
/// cryptographically secure hash of the actual message that's going to be signed. Otherwise
@@ -239,8 +228,6 @@ impl Message {
239228

240229
/// Creates a [`Message`] from a `digest`.
241230
///
242-
/// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
243-
///
244231
/// The `digest` array has to be a cryptographically secure hash of the actual message that's
245232
/// going to be signed. Otherwise the result of signing isn't a [secure signature].
246233
///
@@ -250,8 +237,6 @@ impl Message {
250237

251238
/// Creates a [`Message`] from a 32 byte slice `digest`.
252239
///
253-
/// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
254-
///
255240
/// The slice has to be 32 bytes long and be a cryptographically secure hash of the actual
256241
/// message that's going to be signed. Otherwise the result of signing isn't a [secure
257242
/// signature].
@@ -272,31 +257,9 @@ impl Message {
272257
_ => Err(Error::InvalidMessage),
273258
}
274259
}
275-
276-
/// Constructs a [`Message`] by hashing `data` with hash algorithm `H`.
277-
///
278-
/// Requires the feature `hashes` to be enabled.
279-
///
280-
/// # Examples
281-
///
282-
/// ```
283-
/// # #[cfg(feature = "hashes")] {
284-
/// use secp256k1::hashes::{sha256, Hash};
285-
/// use secp256k1::Message;
286-
///
287-
/// let m1 = Message::from_hashed_data::<sha256::Hash>("Hello world!".as_bytes());
288-
/// // is equivalent to
289-
/// let m2 = Message::from(sha256::Hash::hash("Hello world!".as_bytes()));
290-
///
291-
/// assert_eq!(m1, m2);
292-
/// # }
293-
/// ```
294-
#[cfg(feature = "hashes")]
295-
pub fn from_hashed_data<H: ThirtyTwoByteHash + hashes::Hash>(data: &[u8]) -> Self {
296-
<H as hashes::Hash>::hash(data).into()
297-
}
298260
}
299261

262+
#[allow(deprecated)]
300263
impl<T: ThirtyTwoByteHash> From<T> for Message {
301264
/// Converts a 32-byte hash directly to a message without error paths.
302265
fn from(t: T) -> Message { Message(t.into_32()) }
@@ -1043,24 +1006,6 @@ mod tests {
10431006
let sig = SECP256K1.sign_ecdsa(&msg, &sk);
10441007
assert!(SECP256K1.verify_ecdsa(&msg, &sig, &pk).is_ok());
10451008
}
1046-
1047-
#[cfg(feature = "hashes")]
1048-
#[test]
1049-
fn test_from_hash() {
1050-
use hashes::{sha256, sha256d, Hash};
1051-
1052-
let test_bytes = "Hello world!".as_bytes();
1053-
1054-
let hash = sha256::Hash::hash(test_bytes);
1055-
let msg = Message::from(hash);
1056-
assert_eq!(msg.0, hash.to_byte_array());
1057-
assert_eq!(msg, Message::from_hashed_data::<hashes::sha256::Hash>(test_bytes));
1058-
1059-
let hash = sha256d::Hash::hash(test_bytes);
1060-
let msg = Message::from(hash);
1061-
assert_eq!(msg.0, hash.to_byte_array());
1062-
assert_eq!(msg, Message::from_hashed_data::<hashes::sha256d::Hash>(test_bytes));
1063-
}
10641009
}
10651010

10661011
#[cfg(bench)]

src/schnorr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ use crate::ffi::{self, CPtr};
1212
use crate::key::{Keypair, XOnlyPublicKey};
1313
#[cfg(feature = "global-context")]
1414
use crate::SECP256K1;
15-
use crate::{
16-
constants, from_hex, impl_array_newtype, Error, Message, Secp256k1, Signing, Verification,
17-
};
15+
use crate::{constants, from_hex, Error, Message, Secp256k1, Signing, Verification};
1816

1917
/// Represents a schnorr signature.
2018
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]

0 commit comments

Comments
 (0)