Skip to content

Commit 0805d31

Browse files
committed
Add documentation guiding users towards randomization
Now that we opportunistically randomize the context on creation if `rand-std` is enabled it would be nice to encourage users who do not wish to use `rand-std` to randomize the context. We already have an API to do this but it requires a separate call to do so. Instead of adding a bunch of additional constructors elect to add documentation to the current constructors guiding users towards randomization.
1 parent e9b53cd commit 0805d31

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/context.rs

+19
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,19 @@ mod alloc_only {
180180
/// Lets you create a context in a generic manner (sign/verify/all).
181181
///
182182
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
183+
/// If `rand-std` feature is not enabled please consider randomizing the context as follows:
184+
/// ```
185+
/// # #[cfg(all(feature = "rand-std", any(feature = "alloc", feature = "std")))] {
186+
/// # use secp256k1::Secp256k1;
187+
/// # use secp256k1::rand::{thread_rng, RngCore};
188+
/// let mut ctx = Secp256k1::new();
189+
/// # let mut rng = thread_rng();
190+
/// # let mut seed = [0u8; 32];
191+
/// # rng.fill_bytes(&mut seed);
192+
/// // let seed = <32 bytes of random data>
193+
/// ctx.seeded_randomize(&seed);
194+
/// # }
195+
/// ```
183196
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
184197
pub fn gen_new() -> Secp256k1<C> {
185198
#[cfg(target_arch = "wasm32")]
@@ -207,6 +220,8 @@ mod alloc_only {
207220
/// Creates a new Secp256k1 context with all capabilities.
208221
///
209222
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
223+
/// If `rand-std` feature is not enabled please consider randomizing the context (see docs
224+
/// for `Secp256k1::gen_new()`).
210225
pub fn new() -> Secp256k1<All> {
211226
Secp256k1::gen_new()
212227
}
@@ -216,6 +231,8 @@ mod alloc_only {
216231
/// Creates a new Secp256k1 context that can only be used for signing.
217232
///
218233
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
234+
/// If `rand-std` feature is not enabled please consider randomizing the context (see docs
235+
/// for `Secp256k1::gen_new()`).
219236
pub fn signing_only() -> Secp256k1<SignOnly> {
220237
Secp256k1::gen_new()
221238
}
@@ -225,6 +242,8 @@ mod alloc_only {
225242
/// Creates a new Secp256k1 context that can only be used for verification.
226243
///
227244
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
245+
/// If `rand-std` feature is not enabled please consider randomizing the context (see docs
246+
/// for `Secp256k1::gen_new()`).
228247
pub fn verification_only() -> Secp256k1<VerifyOnly> {
229248
Secp256k1::gen_new()
230249
}

0 commit comments

Comments
 (0)