Update rand to 0.8 and replace CounterRng with mock::StepRng

This commit is contained in:
Elichai Turkel 2021-09-14 13:36:50 +03:00
parent 626835f540
commit ebe46a4d4e
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
6 changed files with 18 additions and 102 deletions

View File

@ -43,17 +43,16 @@ serde = { version = "1.0", default-features = false, optional = true }
# You likely only want to enable these if you explicitly do not want to use "std", otherwise enable
# the respective -std feature e.g., bitcoin-hashes-std
bitcoin_hashes = { version = "0.10", default-features = false, optional = true }
rand = { version = "0.6", default-features = false, optional = true }
rand = { version = "0.8", default-features = false, optional = true }
[dev-dependencies]
rand = "0.6"
rand_core = "0.4"
rand = "0.8"
rand_core = "0.6"
serde_test = "1.0"
bitcoin_hashes = "0.10"
[target.wasm32-unknown-unknown.dev-dependencies]
wasm-bindgen-test = "0.3"
rand = { version = "0.6", features = ["wasm-bindgen"] }
[[example]]

View File

@ -1,11 +1,11 @@
extern crate secp256k1;
use secp256k1::rand::rngs::OsRng;
use secp256k1::rand::thread_rng;
use secp256k1::{PublicKey, Secp256k1, SecretKey};
fn main() {
let secp = Secp256k1::new();
let mut rng = OsRng::new().unwrap();
let mut rng = thread_rng();
// First option:
let (seckey, pubkey) = secp.generate_keypair(&mut rng);

View File

@ -467,12 +467,11 @@ impl<C: Verification> Secp256k1<C> {
///
/// ```rust
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
/// # use secp256k1::rand::rngs::OsRng;
/// # use secp256k1::rand::thread_rng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// #
/// # let secp = Secp256k1::new();
/// # let mut rng = OsRng::new().expect("OsRng");
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rng);
/// # let (secret_key, public_key) = secp.generate_keypair(&mut thread_rng());
/// #
/// let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
/// let sig = secp.sign(&message, &secret_key);
@ -496,12 +495,11 @@ impl<C: Verification> Secp256k1<C> {
///
/// ```rust
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
/// # use secp256k1::rand::rngs::OsRng;
/// # use secp256k1::rand::thread_rng;
/// # use secp256k1::{Secp256k1, Message, Error};
/// #
/// # let secp = Secp256k1::new();
/// # let mut rng = OsRng::new().expect("OsRng");
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rng);
/// # let (secret_key, public_key) = secp.generate_keypair(&mut thread_rng());
/// #
/// let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
/// let sig = secp.sign_ecdsa(&message, &secret_key);

View File

@ -1512,10 +1512,7 @@ mod test {
use core::str::FromStr;
#[cfg(any(feature = "alloc", feature = "std"))]
use rand::{Error, ErrorKind, RngCore, thread_rng};
#[cfg(any(feature = "alloc", feature = "std"))]
use rand_core::impls;
use rand::{Error, RngCore, thread_rng, rngs::mock::StepRng};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
@ -1594,7 +1591,6 @@ mod test {
#[test]
#[cfg(any(feature = "alloc", feature = "std"))]
fn test_out_of_range() {
struct BadRng(u8);
impl RngCore for BadRng {
fn next_u32(&mut self) -> u32 { unimplemented!() }
@ -1687,28 +1683,9 @@ mod test {
#[test]
#[cfg(all(feature = "rand", any(feature = "alloc", feature = "std")))]
fn test_debug_output() {
use to_hex;
struct DumbRng(u32);
impl RngCore for DumbRng {
fn next_u32(&mut self) -> u32 {
self.0 = self.0.wrapping_add(1);
self.0
}
fn next_u64(&mut self) -> u64 {
self.next_u32() as u64
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_next(self, dest);
}
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
}
}
let s = Secp256k1::new();
let (sk, _) = s.generate_keypair(&mut DumbRng(0));
let (sk, _) = s.generate_keypair(&mut StepRng::new(1, 1));
assert_eq!(&format!("{:?}", sk),
"SecretKey(#d3e0c51a23169bb5)");
@ -1785,26 +1762,9 @@ mod test {
#[cfg(not(fuzzing))]
#[cfg(any(feature = "alloc", feature = "std"))]
fn test_pubkey_serialize() {
struct DumbRng(u32);
impl RngCore for DumbRng {
fn next_u32(&mut self) -> u32 {
self.0 = self.0.wrapping_add(1);
self.0
}
fn next_u64(&mut self) -> u64 {
self.next_u32() as u64
}
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_next(self, dest);
}
}
let s = Secp256k1::new();
let (_, pk1) = s.generate_keypair(&mut DumbRng(0));
let (_, pk1) = s.generate_keypair(&mut StepRng::new(1,1));
assert_eq!(&pk1.serialize_uncompressed()[..],
&[4, 124, 121, 49, 14, 253, 63, 197, 50, 39, 194, 107, 17, 193, 219, 108, 154, 126, 9, 181, 248, 2, 12, 149, 233, 198, 71, 149, 134, 250, 184, 154, 229, 185, 28, 165, 110, 27, 3, 162, 126, 238, 167, 157, 242, 221, 76, 251, 237, 34, 231, 72, 39, 245, 3, 191, 64, 111, 170, 117, 103, 82, 28, 102, 163][..]);
assert_eq!(&pk1.serialize()[..],

View File

@ -47,8 +47,7 @@
//! use secp256k1::hashes::sha256;
//!
//! let secp = Secp256k1::new();
//! let mut rng = OsRng::new().expect("OsRng");
//! let (secret_key, public_key) = secp.generate_keypair(&mut rng);
//! let (secret_key, public_key) = secp.generate_keypair(&mut OsRng);
//! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
//!
//! let sig = secp.sign_ecdsa(&message, &secret_key);
@ -1068,38 +1067,16 @@ mod benches {
use test::{Bencher, black_box};
use rand::{RngCore, thread_rng};
use rand::rngs::mock::StepRng;
use super::{Message, Secp256k1};
#[bench]
#[cfg(any(feature = "alloc", feature = "std"))]
pub fn generate(bh: &mut Bencher) {
struct CounterRng(u64);
impl RngCore for CounterRng {
fn next_u32(&mut self) -> u32 {
self.next_u64() as u32
}
fn next_u64(&mut self) -> u64 {
self.0 += 1;
self.0
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
for chunk in dest.chunks_mut(64/8) {
let rand: [u8; 64/8] = unsafe {std::mem::transmute(self.next_u64())};
chunk.copy_from_slice(&rand[..chunk.len()]);
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
Ok(self.fill_bytes(dest))
}
}
let s = Secp256k1::new();
let mut r = CounterRng(0);
let mut r = StepRng::new(1, 1);
bh.iter( || {
let (sk, pk) = s.generate_keypair(&mut r);
black_box(sk);

View File

@ -273,8 +273,7 @@ impl <C: Signing> Secp256k1<C> {
mod tests {
use core::str::FromStr;
use rand::{Error, ErrorKind, RngCore, rngs::ThreadRng, thread_rng};
use rand_core::impls;
use rand::{RngCore, rngs::ThreadRng, thread_rng};
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::wasm_bindgen_test as test;
@ -516,26 +515,9 @@ mod tests {
#[cfg(not(fuzzing))]
#[cfg(all(feature = "rand", any(feature = "alloc", feature = "std")))]
fn test_pubkey_serialize() {
struct DumbRng(u32);
impl RngCore for DumbRng {
fn next_u32(&mut self) -> u32 {
self.0 = self.0.wrapping_add(1);
self.0
}
fn next_u64(&mut self) -> u64 {
self.next_u32() as u64
}
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
impls::fill_bytes_via_next(self, dest);
}
}
use rand::rngs::mock::StepRng;
let secp = Secp256k1::new();
let kp = KeyPair::new(&secp, &mut DumbRng(0));
let kp = KeyPair::new(&secp, &mut StepRng::new(1, 1));
let (pk, _parity) = kp.x_only_public_key();
assert_eq!(
&pk.serialize()[..],