Skip to content

Commit b0d0b2a

Browse files
committed
Improve feature usage bitcoin-hashes[-std]
Currently we have a feature `bitcoin-hashes-std` and a dependency `bitcoin_hashes`, this means one has to think about and change the `_` and `-` when coding. The underscore in `bitcoin_hashes` is an artifact of days gone by and we cannot fix it but we can cover it up and make our lives easier, especially now we have `bitcoin-hashes-std`. Improve feature usage of the `bitcoin_hashes` library by: - Add a feature `bitcoin-hashes` that enables `bitcoin_hashes`. - Use the new feature in all feature gated code - Use `bitcoin-hashes-std` in feature gated code that includes other `std` features (e.g. `rand-std`)
1 parent 497654e commit b0d0b2a

File tree

6 files changed

+30
-27
lines changed

6 files changed

+30
-27
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ default = ["std"]
2222
std = ["alloc", "secp256k1-sys/std"]
2323
# allow use of Secp256k1::new and related API that requires an allocator
2424
alloc = ["secp256k1-sys/alloc"]
25-
bitcoin-hashes-std = ["bitcoin_hashes/std"]
25+
bitcoin-hashes = ["bitcoin_hashes"] # Feature alias because of the underscore.
26+
bitcoin-hashes-std = ["bitcoin-hashes", "bitcoin_hashes/std"]
2627
rand-std = ["rand/std", "rand/std_rng"]
2728
recovery = ["secp256k1-sys/recovery"]
2829
lowmemory = ["secp256k1-sys/lowmemory"]

contrib/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -ex
44

5-
FEATURES="bitcoin_hashes global-context lowmemory rand recovery serde std alloc"
5+
FEATURES="bitcoin-hashes global-context lowmemory rand recovery serde std alloc"
66
# These features are typically enabled along with the 'std' feature, so we test
77
# them together with 'std'.
88
STD_FEATURES="rand-std bitcoin-hashes-std"

src/ecdh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ impl AsRef<[u8]> for SharedSecret {
127127
///
128128
/// # Examples
129129
/// ```
130-
/// # #[cfg(all(feature = "bitcoin_hashes", feature = "rand-std", feature = "std"))] {
130+
/// # #[cfg(all(feature = "bitcoin-hashes-std", feature = "rand-std", feature = "std"))] {
131131
/// # use secp256k1::{ecdh, Secp256k1, PublicKey, SecretKey};
132132
/// # use secp256k1::hashes::{Hash, sha512};
133133
/// # use secp256k1::rand::thread_rng;
@@ -239,7 +239,7 @@ mod tests {
239239

240240
#[test]
241241
#[cfg(not(fuzzing))]
242-
#[cfg(all(feature="rand-std", feature = "std", feature = "bitcoin_hashes"))]
242+
#[cfg(all(feature="std", feature = "rand-std", feature = "bitcoin-hashes-std"))]
243243
fn bitcoin_hashes_and_sys_generate_same_secret() {
244244
use bitcoin_hashes::{sha256, Hash, HashEngine};
245245
use crate::ecdh::shared_secret_point;

src/key.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use crate::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey
2525
use crate::ffi::{self, CPtr, impl_array_newtype};
2626
use crate::ffi::types::c_uint;
2727

28-
#[cfg(feature = "bitcoin_hashes")]
28+
#[cfg(feature = "bitcoin-hashes")]
2929
use crate::{hashes, ThirtyTwoByteHash};
3030

3131
#[cfg(feature = "serde")]
@@ -246,8 +246,8 @@ impl SecretKey {
246246
/// assert_eq!(sk1, sk2);
247247
/// # }
248248
/// ```
249-
#[cfg(feature = "bitcoin_hashes")]
250-
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
249+
#[cfg(feature = "bitcoin-hashes")]
250+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin-hashes")))]
251251
#[inline]
252252
pub fn from_hashed_data<H: ThirtyTwoByteHash + hashes::Hash>(data: &[u8]) -> Self {
253253
<H as hashes::Hash>::hash(data).into()
@@ -377,7 +377,7 @@ impl SecretKey {
377377
}
378378
}
379379

380-
#[cfg(feature = "bitcoin_hashes")]
380+
#[cfg(feature = "bitcoin-hashes")]
381381
impl<T: ThirtyTwoByteHash> From<T> for SecretKey {
382382
/// Converts a 32-byte hash directly to a secret key without error paths.
383383
fn from(t: T) -> SecretKey {

src/lib.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
//! trigger any assertion failures in the upstream library.
4242
//!
4343
//! ```rust
44-
//! # #[cfg(all(feature = "std", feature="rand-std", feature="bitcoin_hashes"))] {
44+
//! # #[cfg(all(feature = "std", feature="rand-std", feature="bitcoin-hashes-std"))] {
4545
//! use secp256k1::rand::rngs::OsRng;
4646
//! use secp256k1::{Secp256k1, Message};
4747
//! use secp256k1::hashes::sha256;
@@ -58,7 +58,7 @@
5858
//! If the "global-context" feature is enabled you have access to an alternate API.
5959
//!
6060
//! ```rust
61-
//! # #[cfg(all(feature="global-context", feature = "std", feature="rand-std", features = "bitcoin_hashes"))] {
61+
//! # #[cfg(all(feature="global-context", feature = "std", feature="rand-std", features = "bitcoin-hashes-std"))] {
6262
//! use secp256k1::rand::thread_rng;
6363
//! use secp256k1::{generate_keypair, Message};
6464
//! use secp256k1::hashes::sha256;
@@ -71,7 +71,7 @@
7171
//! # }
7272
//! ```
7373
//!
74-
//! The above code requires `rust-secp256k1` to be compiled with the `rand-std` and `bitcoin_hashes`
74+
//! The above code requires `rust-secp256k1` to be compiled with the `rand-std` and `bitcoin-hashes-std`
7575
//! feature enabled, to get access to [`generate_keypair`](struct.Secp256k1.html#method.generate_keypair)
7676
//! Alternately, keys and messages can be parsed from slices, like
7777
//!
@@ -83,7 +83,7 @@
8383
//! let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
8484
//! let public_key = PublicKey::from_secret_key(&secp, &secret_key);
8585
//! // This is unsafe unless the supplied byte slice is the output of a cryptographic hash function.
86-
//! // See the above example for how to use this library together with `bitcoin_hashes`.
86+
//! // See the above example for how to use this library together with `bitcoin-hashes-std`.
8787
//! let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
8888
//!
8989
//! let sig = secp.sign_ecdsa(&message, &secret_key);
@@ -141,12 +141,14 @@
141141
//! * `alloc` - use the `alloc` standard Rust library to provide heap allocations.
142142
//! * `rand` - use `rand` library to provide random generator (e.g. to generate keys).
143143
//! * `rand-std` - use `rand` library with its `std` feature enabled. (Implies `rand`.)
144+
//! * `bitcoin-hashes` - use the `bitcoin-hashes` library.
145+
//! * `bitcoin-hashes-std` - use the `bitcoin-hashes` library with its `std` feature enabled (implies `bitcoin-hashes`).
144146
//! * `recovery` - enable functions that can compute the public key from signature.
145147
//! * `lowmemory` - optimize the library for low-memory environments.
146148
//! * `global-context` - enable use of global secp256k1 context (implies `std`).
147149
//! * `serde` - implements serialization and deserialization for types in this crate using `serde`.
148150
//! **Important**: `serde` encoding is **not** the same as consensus encoding!
149-
//! * `bitcoin_hashes` - enables interaction with the `bitcoin-hashes` crate (e.g. conversions).
151+
//!
150152
151153
// Coding conventions
152154
#![deny(non_upper_case_globals, non_camel_case_types, non_snake_case)]
@@ -188,8 +190,8 @@ pub use rand;
188190
#[cfg(feature = "serde")]
189191
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
190192
pub use serde;
191-
#[cfg(feature = "bitcoin_hashes")]
192-
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
193+
#[cfg(feature = "bitcoin-hashes")]
194+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin-hashes")))]
193195
pub use bitcoin_hashes as hashes;
194196
pub use secp256k1_sys as ffi;
195197
pub use crate::key::{PublicKey, SecretKey};
@@ -203,7 +205,7 @@ pub use context::global::SECP256K1;
203205

204206
use core::{fmt, str, mem, marker::PhantomData};
205207
use crate::ffi::{CPtr, impl_array_newtype, types::AlignedType};
206-
#[cfg(feature = "bitcoin_hashes")]
208+
#[cfg(feature = "bitcoin-hashes")]
207209
use crate::hashes::Hash;
208210

209211
// Backwards compatible changes
@@ -233,24 +235,24 @@ pub trait ThirtyTwoByteHash {
233235
fn into_32(self) -> [u8; 32];
234236
}
235237

236-
#[cfg(feature = "bitcoin_hashes")]
237-
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
238+
#[cfg(feature = "bitcoin-hashes")]
239+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin-hashes")))]
238240
impl ThirtyTwoByteHash for hashes::sha256::Hash {
239241
fn into_32(self) -> [u8; 32] {
240242
self.into_inner()
241243
}
242244
}
243245

244-
#[cfg(feature = "bitcoin_hashes")]
245-
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
246+
#[cfg(feature = "bitcoin-hashes")]
247+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin-hashes")))]
246248
impl ThirtyTwoByteHash for hashes::sha256d::Hash {
247249
fn into_32(self) -> [u8; 32] {
248250
self.into_inner()
249251
}
250252
}
251253

252-
#[cfg(feature = "bitcoin_hashes")]
253-
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
254+
#[cfg(feature = "bitcoin-hashes")]
255+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin-hashes")))]
254256
impl<T: hashes::sha256t::Tag> ThirtyTwoByteHash for hashes::sha256t::Hash<T> {
255257
fn into_32(self) -> [u8; 32] {
256258
self.into_inner()
@@ -299,8 +301,8 @@ impl Message {
299301
/// assert_eq!(m1, m2);
300302
/// # }
301303
/// ```
302-
#[cfg(feature = "bitcoin_hashes")]
303-
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin_hashes")))]
304+
#[cfg(feature = "bitcoin-hashes")]
305+
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin-hashes")))]
304306
pub fn from_hashed_data<H: ThirtyTwoByteHash + hashes::Hash>(data: &[u8]) -> Self {
305307
<H as hashes::Hash>::hash(data).into()
306308
}
@@ -1040,7 +1042,7 @@ mod tests {
10401042
assert!(SECP256K1.verify_ecdsa(&msg, &sig, &pk).is_ok());
10411043
}
10421044

1043-
#[cfg(feature = "bitcoin_hashes")]
1045+
#[cfg(feature = "bitcoin-hashes")]
10441046
#[test]
10451047
fn test_from_hash() {
10461048
use crate::hashes::{self, Hash};

src/secret.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ macro_rules! impl_display_secret {
4343
}
4444
}
4545

46-
#[cfg(all(not(feature = "std"), feature = "bitcoin_hashes"))]
46+
#[cfg(all(not(feature = "std"), feature = "bitcoin-hashes"))]
4747
impl ::core::fmt::Debug for $thing {
4848
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
4949
use crate::hashes::{sha256, Hash, HashEngine};
@@ -63,7 +63,7 @@ macro_rules! impl_display_secret {
6363
}
6464
}
6565

66-
#[cfg(all(not(feature = "std"), not(feature = "bitcoin_hashes")))]
66+
#[cfg(all(not(feature = "std"), not(feature = "bitcoin-hashes")))]
6767
impl ::core::fmt::Debug for $thing {
6868
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
6969
write!(f, "<secret requires std or bitcoin_hashes feature to display>")

0 commit comments

Comments
 (0)