Skip to content

Commit a0465ea

Browse files
committed
Remove feature global-context-less-secure
Instead of providing a mechanism for users to opt out of randomization we can just feature gate the call site i.e., opportunistically randomize the global context on creation if `rand-std` feature is enabled.
1 parent 7a3736a commit a0465ea

File tree

4 files changed

+23
-19
lines changed

4 files changed

+23
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ alloc = []
2626
rand-std = ["rand/std"]
2727
recovery = ["secp256k1-sys/recovery"]
2828
lowmemory = ["secp256k1-sys/lowmemory"]
29-
global-context = ["std", "rand-std", "global-context-less-secure"]
30-
global-context-less-secure = []
29+
global-context = ["std"]
3130

3231
[dependencies]
3332
secp256k1-sys = { version = "0.4.2", default-features = false, path = "./secp256k1-sys" }

src/context.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use Secp256k1;
99
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
1010
pub use self::alloc_only::*;
1111

12-
#[cfg(all(feature = "global-context-less-secure", feature = "std"))]
13-
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context-less-secure"))))]
12+
#[cfg(all(feature = "global-context", feature = "std"))]
13+
#[cfg_attr(docsrs, doc(cfg(all(feature = "global-context", feature = "std"))))]
1414
/// Module implementing a singleton pattern for a global `Secp256k1` context
1515
pub mod global {
16-
#[cfg(feature = "global-context")]
16+
#[cfg(feature = "rand-std")]
1717
use rand;
1818

1919
use std::ops::Deref;
@@ -26,22 +26,29 @@ pub mod global {
2626
__private: (),
2727
}
2828

29-
/// A global, static context to avoid repeatedly creating contexts where one can't be passed
29+
/// A global static context to avoid repeatedly creating contexts.
3030
///
31-
/// If the global-context feature is enabled (and not just the global-context-less-secure),
32-
/// this will have been randomized.
31+
/// If `rand-std` feature is enabled, context will have been randomized using `thread_rng`.
32+
///
33+
/// ```
34+
/// # #[cfg(all(feature = "global-context", feature = "rand-std"))] {
35+
/// use secp256k1::{PublicKey, SECP256K1};
36+
/// use secp256k1::rand::thread_rng;
37+
/// let _ = SECP256K1.generate_keypair(&mut thread_rng());
38+
/// # }
39+
/// ```
3340
pub static SECP256K1: &GlobalContext = &GlobalContext { __private: () };
3441

3542
impl Deref for GlobalContext {
3643
type Target = Secp256k1<All>;
3744

38-
#[allow(unused_mut)] // Unused when "global-context" is not enabled.
45+
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
3946
fn deref(&self) -> &Self::Target {
4047
static ONCE: Once = Once::new();
4148
static mut CONTEXT: Option<Secp256k1<All>> = None;
4249
ONCE.call_once(|| unsafe {
4350
let mut ctx = Secp256k1::new();
44-
#[cfg(feature = "global-context")]
51+
#[cfg(feature = "rand-std")]
4552
{
4653
ctx.randomize(&mut rand::thread_rng());
4754
}

src/key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ impl Ord for PublicKey {
641641
/// feature active. This is due to security considerations, see the [`serde_keypair`] documentation
642642
/// for details.
643643
///
644-
/// If the `serde` and `global-context[-less-secure]` features are active `KeyPair`s can be serialized and
644+
/// If the `serde` and `global-context` features are active `KeyPair`s can be serialized and
645645
/// deserialized by annotating them with `#[serde(with = "secp256k1::serde_keypair")]`
646646
/// inside structs or enums for which [`Serialize`] and [`Deserialize`] are being derived.
647647
///
@@ -1320,7 +1320,7 @@ impl<'de> ::serde::Deserialize<'de> for XOnlyPublicKey {
13201320
///
13211321
/// [`SecretKey`]: crate::SecretKey
13221322
/// [global context]: crate::SECP256K1
1323-
#[cfg(all(feature = "global-context-less-secure", feature = "serde"))]
1323+
#[cfg(all(feature = "global-context", feature = "serde"))]
13241324
pub mod serde_keypair {
13251325
use serde::{Deserialize, Deserializer, Serialize, Serializer};
13261326
use key::KeyPair;
@@ -1924,7 +1924,7 @@ mod test {
19241924
}
19251925

19261926
#[test]
1927-
#[cfg(all(feature = "global-context-less-secure", feature = "serde"))]
1927+
#[cfg(all(feature = "global-context", feature = "serde"))]
19281928
fn test_serde_keypair() {
19291929
use serde::{Deserialize, Deserializer, Serialize, Serializer};
19301930
use serde_test::{Configure, Token, assert_tokens};

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,7 @@
125125
//! * `rand-std` - use `rand` library with its `std` feature enabled. (Implies `rand`.)
126126
//! * `recovery` - enable functions that can compute the public key from signature.
127127
//! * `lowmemory` - optimize the library for low-memory environments.
128-
//! * `global-context` - enable use of global secp256k1 context. (Implies `std`, `rand-std` and
129-
//! `global-context-less-secure`.)
130-
//! * `global-context-less-secure` - enables global context without extra sidechannel protection.
128+
//! * `global-context` - enable use of global secp256k1 context (implies `std`).
131129
//! * `serde` - implements serialization and deserialization for types in this crate using `serde`.
132130
//! **Important**: `serde` encoding is **not** the same as consensus encoding!
133131
//! * `bitcoin_hashes` - enables interaction with the `bitcoin-hashes` crate (e.g. conversions).
@@ -195,8 +193,8 @@ use core::marker::PhantomData;
195193
use core::{mem, fmt, str};
196194
use ffi::{CPtr, types::AlignedType};
197195

198-
#[cfg(feature = "global-context-less-secure")]
199-
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context-less-secure"))))]
196+
#[cfg(feature = "global-context")]
197+
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context"))))]
200198
pub use context::global::SECP256K1;
201199

202200
#[cfg(feature = "bitcoin_hashes")]
@@ -955,7 +953,7 @@ mod tests {
955953

956954
}
957955

958-
#[cfg(feature = "global-context-less-secure")]
956+
#[cfg(feature = "global-context")]
959957
#[test]
960958
fn test_global_context() {
961959
use super::SECP256K1;

0 commit comments

Comments
 (0)