Skip to content

Commit 3d17551

Browse files
committed
Delete the SECP256K1 global context
Delete the old global context, make the new ones `pub`, and re-export them in the `context` modulue (implies they are also re-exported at crate root). Exactly which one is re-exported depends on whether the crate is built with `std` or not.
1 parent 155c7c1 commit 3d17551

File tree

5 files changed

+8
-44
lines changed

5 files changed

+8
-44
lines changed

src/context/internal_nostd.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ mod self_contained_context {
7373
// because we need a const constructor.)
7474
pub(super) use self_contained_context::SelfContainedContext;
7575

76-
static SECP256K1: SpinLock<SelfContainedContext> = SpinLock::<SelfContainedContext>::new();
76+
/// A global static context to avoid repeatedly creating contexts.
77+
pub static SECP256K1: SpinLock<SelfContainedContext> = SpinLock::<SelfContainedContext>::new();
7778

7879
/// Borrows the global context and does some operation on it.
7980
///

src/context/internal_std.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use secp256k1_sys as ffi;
1010
use crate::{All, Context, Secp256k1};
1111

1212
thread_local! {
13-
static SECP256K1: RefCell<Secp256k1<All>> = RefCell::new(Secp256k1::new());
13+
/// A global static context to avoid repeatedly creating contexts.
14+
///
15+
/// If `rand` feature is enabled, context will have been randomized using `rng`.
16+
pub static SECP256K1: RefCell<Secp256k1<All>> = RefCell::new(Secp256k1::new());
1417
}
1518

1619
/// Borrows the global context and does some operation on it.

src/context/mod.rs

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,7 @@ mod internal;
1717
#[cfg(not(feature = "std"))]
1818
mod spinlock;
1919

20-
pub use internal::{rerandomize_global_context, with_global_context, with_raw_global_context};
21-
22-
#[cfg(all(feature = "global-context", feature = "std"))]
23-
/// Module implementing a singleton pattern for a global `Secp256k1` context.
24-
pub mod global {
25-
26-
use std::ops::Deref;
27-
use std::sync::Once;
28-
29-
use crate::{All, Secp256k1};
30-
31-
/// Proxy struct for global `SECP256K1` context.
32-
#[derive(Debug, Copy, Clone)]
33-
pub struct GlobalContext {
34-
__private: (),
35-
}
36-
37-
/// A global static context to avoid repeatedly creating contexts.
38-
///
39-
/// If `rand` and `std` feature is enabled, context will have been randomized using
40-
/// `rng`.
41-
pub static SECP256K1: &GlobalContext = &GlobalContext { __private: () };
42-
43-
impl Deref for GlobalContext {
44-
type Target = Secp256k1<All>;
45-
46-
#[allow(unused_mut)] // Unused when `rand` + `std` is not enabled.
47-
#[allow(static_mut_refs)] // The "proper" way to do this is with OnceLock (MSRV 1.70) or LazyLock (MSRV 1.80)
48-
// See https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html
49-
fn deref(&self) -> &Self::Target {
50-
static ONCE: Once = Once::new();
51-
static mut CONTEXT: Option<Secp256k1<All>> = None;
52-
ONCE.call_once(|| unsafe {
53-
CONTEXT = Some(Secp256k1::new());
54-
});
55-
unsafe { CONTEXT.as_ref().unwrap() }
56-
}
57-
}
58-
}
20+
pub use internal::{rerandomize_global_context, with_global_context, with_raw_global_context, SECP256K1};
5921

6022
/// A trait for all kinds of contexts that lets you define the exact flags and a function to
6123
/// deallocate memory. It isn't possible to implement this for types outside this crate.

src/key/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1940,7 +1940,6 @@ mod test {
19401940
use serde_test::{assert_tokens, Configure, Token};
19411941

19421942
use crate::key::Keypair;
1943-
use crate::SECP256K1;
19441943

19451944
#[rustfmt::skip]
19461945
static SK_BYTES: [u8; 32] = [

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ use crate::ffi::CPtr;
191191
#[rustfmt::skip] // Keep public re-exports separate.
192192
pub use secp256k1_sys as ffi;
193193

194-
#[cfg(all(feature = "global-context", feature = "std"))]
195-
pub use crate::context::global::{self, SECP256K1};
194+
pub use crate::context::SECP256K1;
196195
#[cfg(feature = "alloc")]
197196
pub use crate::context::{All, SignOnly, VerifyOnly};
198197
#[doc(inline)]

0 commit comments

Comments
 (0)