Skip to content

Commit 8979a93

Browse files
committed
Document proper usage of from_slice
1 parent 0d85753 commit 8979a93

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/lib.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,31 @@
3838
//!
3939
//! ```rust
4040
//! extern crate secp256k1;
41+
//! # #[cfg(feature="bitcoin_hashes")]
42+
//! extern crate bitcoin_hashes;
4143
//! # #[cfg(feature="rand")]
4244
//! extern crate rand;
4345
//!
4446
//! #
4547
//! # fn main() {
46-
//! # #[cfg(feature="rand")] {
47-
//! use rand::OsRng;
48+
//! # #[cfg(all(feature="rand", feature="bitcoin_hashes"))] {
49+
//! use rand::rngs::OsRng;
4850
//! use secp256k1::{Secp256k1, Message};
51+
//! use bitcoin_hashes::sha256;
4952
//!
5053
//! let secp = Secp256k1::new();
5154
//! let mut rng = OsRng::new().expect("OsRng");
5255
//! let (secret_key, public_key) = secp.generate_keypair(&mut rng);
53-
//! let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
56+
//! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
5457
//!
5558
//! let sig = secp.sign(&message, &secret_key);
5659
//! assert!(secp.verify(&message, &sig, &public_key).is_ok());
5760
//! # } }
5861
//! ```
5962
//!
60-
//! The above code requires `rust-secp256k1` to be compiled with the `rand`
63+
//! The above code requires `rust-secp256k1` to be compiled with the `rand` and `bitcoin_hashes`
6164
//! feature enabled, to get access to [`generate_keypair`](struct.Secp256k1.html#method.generate_keypair)
62-
//! Alternately, keys can be parsed from slices, like
65+
//! Alternately, keys and messages can be parsed from slices, like
6366
//!
6467
//! ```rust
6568
//! # fn main() {
@@ -68,6 +71,8 @@
6871
//! let secp = Secp256k1::new();
6972
//! let secret_key = SecretKey::from_slice(&[0xcd; 32]).expect("32 bytes, within curve order");
7073
//! let public_key = PublicKey::from_secret_key(&secp, &secret_key);
74+
//! // This is unsafe unless the supplied byte slice is the output of a cryptographic hash function.
75+
//! // See the above example for how to use this library together with bitcoin_hashes.
7176
//! let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
7277
//!
7378
//! let sig = secp.sign(&message, &secret_key);
@@ -476,7 +481,12 @@ impl_array_newtype!(Message, u8, constants::MESSAGE_SIZE);
476481
impl_pretty_debug!(Message);
477482

478483
impl Message {
479-
/// Converts a `MESSAGE_SIZE`-byte slice to a message object
484+
/// **If you just want to sign an arbitrary message use `Message::from_hashed_data` instead.**
485+
///
486+
/// Converts a `MESSAGE_SIZE`-byte slice to a message object. **WARNING:** the slice has to be a
487+
/// cryptographically secure hash of the actual message that's going to be signed. Otherwise
488+
/// the result of signing isn't a
489+
/// [secure signature](https://twitter.com/pwuille/status/1063582706288586752).
480490
#[inline]
481491
pub fn from_slice(data: &[u8]) -> Result<Message, Error> {
482492
if data == [0; constants::MESSAGE_SIZE] {
@@ -493,7 +503,8 @@ impl Message {
493503
}
494504
}
495505

496-
/// Constructs a `Message` by hashing `data` with hash algorithm `H`.
506+
/// Constructs a `Message` by hashing `data` with hash algorithm `H`. This requires the feature
507+
/// `bitcoin_hashes` to be enabled.
497508
/// ```rust
498509
/// extern crate bitcoin_hashes;
499510
/// use secp256k1::Message;

0 commit comments

Comments
 (0)