make `rand` crate optional

This commit is contained in:
Andrew Poelstra 2017-12-19 20:49:01 +00:00
parent dba0d67912
commit 298929600b
3 changed files with 12 additions and 4 deletions

View File

@ -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

View File

@ -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<R: Rng>(rng: &mut R) -> [u8; 32] {
let mut ret = [0u8; 32];
rng.fill_bytes(&mut ret);
@ -63,6 +64,7 @@ fn random_32_bytes<R: Rng>(rng: &mut R) -> [u8; 32] {
impl SecretKey {
/// Creates a new random secret key
#[inline]
#[cfg(any(test, feature = "rand"))]
pub fn new<R: Rng>(secp: &Secp256k1, rng: &mut R) -> SecretKey {
let mut data = random_32_bytes(rng);
unsafe {

View File

@ -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<R: Rng>(&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<R: Rng>(&self, rng: &mut R)
-> Result<(key::SecretKey, key::PublicKey), Error> {
let sk = key::SecretKey::new(self, rng);