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 7, 2020
1 parent a5147bb commit a86e11f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ script:
- cargo test --verbose --features="rand rand-std"
- cargo test --verbose --features="rand serde"
- cargo test --verbose --features="rand serde recovery endomorphism"
- cargo test --verbose --features global-context
- cargo build --verbose
- cargo test --verbose
- cargo build --verbose --release
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
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
25 changes: 25 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
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,24 @@ mod tests {
assert_tokens(&sig.readable(), &[Token::BorrowedStr(SIG_STR)]);
}

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

let sk_data = hex!("e6dd32f8761625f105c39a39f19370b3521d845a12456d60ce44debd0a362641");
let sk = SecretKey::from_slice(&sk_data).unwrap();
let msg_data = hex!("a4965ca63b7d8562736ceec36dfa5a11bf426eb65be8ea3f7a49ae363032da0d");
let msg = Message::from_slice(&msg_data).unwrap();

// Check usagfe as explicit parameter
let pk = PublicKey::from_secret_key(&SECP256K1, &sk);

// Check usage as self
let sig = SECP256K1.sign(&msg, &sk);
assert!(SECP256K1.verify(&msg, &sig, &pk).is_ok());
}

// 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 a86e11f

Please sign in to comment.