Skip to content

Commit 5c2b80e

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 f7d637e commit 5c2b80e

File tree

4 files changed

+23
-18
lines changed

4 files changed

+23
-18
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 & 7 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(feature = "global-context-less-secure")]
13-
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context-less-secure"))))]
12+
#[cfg(feature = "global-context")]
13+
#[cfg_attr(docsrs, doc(cfg(feature = "global-context")))]
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,21 +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

45+
#[allow(unused_mut)] // Unused when `rand-std` is not enabled.
3846
fn deref(&self) -> &Self::Target {
3947
static ONCE: Once = Once::new();
4048
static mut CONTEXT: Option<Secp256k1<All>> = None;
4149
ONCE.call_once(|| unsafe {
4250
let mut ctx = Secp256k1::new();
43-
#[cfg(feature = "global-context")]
51+
#[cfg(feature = "rand-std")]
4452
{
4553
ctx.randomize(&mut rand::thread_rng());
4654
}

src/key.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ impl Ord for PublicKey {
639639
/// feature active. This is due to security considerations, see the [`serde_keypair`] documentation
640640
/// for details.
641641
///
642-
/// If the `serde` and `global-context[-less-secure]` features are active `KeyPair`s can be serialized and
642+
/// If the `serde` and `global-context` features are active `KeyPair`s can be serialized and
643643
/// deserialized by annotating them with `#[serde(with = "secp256k1::serde_keypair")]`
644644
/// inside structs or enums for which [`Serialize`] and [`Deserialize`] are being derived.
645645
///
@@ -1318,7 +1318,7 @@ impl<'de> ::serde::Deserialize<'de> for XOnlyPublicKey {
13181318
///
13191319
/// [`SecretKey`]: crate::SecretKey
13201320
/// [global context]: crate::SECP256K1
1321-
#[cfg(all(feature = "global-context-less-secure", feature = "serde"))]
1321+
#[cfg(all(feature = "global-context", feature = "serde"))]
13221322
pub mod serde_keypair {
13231323
use serde::{Deserialize, Deserializer, Serialize, Serializer};
13241324
use key::KeyPair;
@@ -1899,7 +1899,7 @@ mod test {
18991899
}
19001900

19011901
#[test]
1902-
#[cfg(all(feature = "global-context-less-secure", feature = "serde"))]
1902+
#[cfg(all(feature = "global-context", feature = "serde"))]
19031903
fn test_serde_keypair() {
19041904
use serde::{Deserialize, Deserializer, Serialize, Serializer};
19051905
use serde_test::{Configure, Token, assert_tokens};

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@
121121
//! * `rand-std` - use `rand` library with its `std` feature enabled. (Implies `rand`.)
122122
//! * `recovery` - enable functions that can compute the public key from signature.
123123
//! * `lowmemory` - optimize the library for low-memory environments.
124-
//! * `global-context` - enable use of global secp256k1 context. (Implies `std`, `rand-std` and
125-
//! `global-context-less-secure`.)
126-
//! * `global-context-less-secure` - enables global context without extra sidechannel protection.
124+
//! * `global-context` - enable use of global secp256k1 context (implies `std`).
127125
//! * `serde` - implements serialization and deserialization for types in this crate using `serde`.
128126
//! **Important**: `serde` encoding is **not** the same as consensus encoding!
129127
//! * `bitcoin_hashes` - enables interaction with the `bitcoin-hashes` crate (e.g. conversions).
@@ -191,8 +189,8 @@ use core::marker::PhantomData;
191189
use core::{mem, fmt, str};
192190
use ffi::{CPtr, types::AlignedType};
193191

194-
#[cfg(feature = "global-context-less-secure")]
195-
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context-less-secure"))))]
192+
#[cfg(feature = "global-context")]
193+
#[cfg_attr(docsrs, doc(cfg(any(feature = "global-context", feature = "global-context"))))]
196194
pub use context::global::SECP256K1;
197195

198196
#[cfg(feature = "bitcoin_hashes")]
@@ -940,7 +938,7 @@ mod tests {
940938

941939
}
942940

943-
#[cfg(feature = "global-context-less-secure")]
941+
#[cfg(feature = "global-context")]
944942
#[test]
945943
fn test_global_context() {
946944
use super::SECP256K1;

0 commit comments

Comments
 (0)