Skip to content

Commit

Permalink
Add an optional global, static context
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeisler committed Jul 4, 2020
1 parent a5147bb commit a2b461e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -29,6 +29,7 @@ rand-std = ["rand/std"]
recovery = ["secp256k1-sys/recovery"]
endomorphism = ["secp256k1-sys/endomorphism"]
lowmemory = ["secp256k1-sys/lowmemory"]
global-context = ["lazy_static"]

# Use this feature to not compile the bundled libsecp256k1 C symbols,
# but use external ones. Use this only if you know what you are doing!
Expand All @@ -38,6 +39,7 @@ external-symbols = ["secp256k1-sys/external-symbols"]
fuzztarget = ["secp256k1-sys/fuzztarget"]

[dependencies]
lazy_static = { version = "1.4.0", optional = true }
secp256k1-sys = { version = "0.1.1", default-features = false, path = "./secp256k1-sys" }

[dev-dependencies]
Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Expand Up @@ -160,6 +160,7 @@ pub use secp256k1_sys as ffi;
#[cfg(all(test, feature = "serde"))] extern crate serde_test;
#[cfg(any(test, feature = "rand"))] use rand::Rng;
#[cfg(any(test, feature = "std"))] extern crate core;
#[cfg(feature = "global-context")] #[macro_use] extern crate lazy_static;

use core::{fmt, ptr, str};

Expand All @@ -179,6 +180,12 @@ use core::marker::PhantomData;
use core::ops::Deref;
use ffi::CPtr;

#[cfg(feature = "global-context")]
lazy_static! {
/// A global, static context to avoid repeatedly creating contexts where one can't be passed
pub static ref SECP256K1: Secp256k1<All> = Secp256k1::new();
}

#[cfg(feature = "bitcoin_hashes")]
use bitcoin_hashes::Hash;

Expand Down Expand Up @@ -1138,6 +1145,16 @@ mod tests {
assert_tokens(&sig.readable(), &[Token::BorrowedStr(SIG_STR)]);
}

#[cfg(feature = "global-context")]
#[test]
fn test_global_context() {
use super::SECP256K1;

let (sk, pk) = SECP256K1.generate_keypair(&mut thread_rng());
let pk_from_sk = PublicKey::from_secret_key(&SECP256K1, &sk);
assert_eq!(pk, pk_from_sk);
}

// For WASM, just run through our general tests in this file all at once.
#[cfg(target_arch = "wasm32")]
extern crate wasm_bindgen_test;
Expand Down

0 comments on commit a2b461e

Please sign in to comment.