Skip to content

Commit

Permalink
make rand crate optional
Browse files Browse the repository at this point in the history
  • Loading branch information
apoelstra committed Dec 19, 2017
1 parent dba0d67 commit 2989296
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

4 changes: 3 additions & 1 deletion src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2989296

Please sign in to comment.