diff --git a/Cargo.toml b/Cargo.toml index b1965ef..3fe668e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,10 +26,14 @@ default = [] [dev-dependencies] serde_json = "1.0" +rand = "0.3" [dependencies] -rand = "0.3" libc = "0.2" rustc-serialize = "0.3" serde = "1.0" +[dependencies.rand] +version = "0.3" +optional = true + diff --git a/src/key.rs b/src/key.rs index cf4acf8..72a75a4 100644 --- a/src/key.rs +++ b/src/key.rs @@ -16,7 +16,7 @@ //! # Public and secret keys use std::marker; -use rand::Rng; +#[cfg(any(test, feature = "rand"))] use rand::Rng; use serialize::{Decoder, Decodable, Encoder, Encodable}; use serde::{Serialize, Deserialize, Serializer, Deserializer}; @@ -54,6 +54,7 @@ pub const ONE_KEY: SecretKey = SecretKey([0, 0, 0, 0, 0, 0, 0, 0, pub struct PublicKey(ffi::PublicKey); +#[cfg(any(test, feature = "rand"))] fn random_32_bytes(rng: &mut R) -> [u8; 32] { let mut ret = [0u8; 32]; rng.fill_bytes(&mut ret); @@ -63,6 +64,7 @@ fn random_32_bytes(rng: &mut R) -> [u8; 32] { impl SecretKey { /// Creates a new random secret key #[inline] + #[cfg(any(test, feature = "rand"))] pub fn new(secp: &Secp256k1, rng: &mut R) -> SecretKey { let mut data = random_32_bytes(rng); unsafe { diff --git a/src/lib.rs b/src/lib.rs index 4e89ee8..f09b76d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,16 +39,16 @@ #![cfg_attr(all(test, feature = "unstable"), feature(test))] #[cfg(all(test, feature = "unstable"))] extern crate test; #[cfg(test)] extern crate serde_json as json; +#[cfg(any(test, feature = "rand"))] extern crate rand; extern crate rustc_serialize as serialize; extern crate serde; extern crate libc; -extern crate rand; use libc::size_t; use std::{error, fmt, ops, ptr}; -use rand::Rng; +#[cfg(any(test, feature = "rand"))] use rand::Rng; #[macro_use] mod macros; @@ -519,6 +519,7 @@ impl Secp256k1 { /// (Re)randomizes the Secp256k1 context for cheap sidechannel resistence; /// see comment in libsecp256k1 commit d2275795f by Gregory Maxwell + #[cfg(any(test, feature = "rand"))] pub fn randomize(&mut self, rng: &mut R) { let mut seed = [0; 32]; rng.fill_bytes(&mut seed); @@ -540,6 +541,7 @@ impl Secp256k1 { /// and `key::PublicKey::from_secret_key`; call those functions directly for /// batch key generation. Requires a signing-capable context. #[inline] + #[cfg(any(test, feature = "rand"))] pub fn generate_keypair(&self, rng: &mut R) -> Result<(key::SecretKey, key::PublicKey), Error> { let sk = key::SecretKey::new(self, rng);