Skip to content

Commit ebe46a4

Browse files
committed
Update rand to 0.8 and replace CounterRng with mock::StepRng
1 parent 626835f commit ebe46a4

File tree

6 files changed

+18
-102
lines changed

6 files changed

+18
-102
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,16 @@ serde = { version = "1.0", default-features = false, optional = true }
4343
# You likely only want to enable these if you explicitly do not want to use "std", otherwise enable
4444
# the respective -std feature e.g., bitcoin-hashes-std
4545
bitcoin_hashes = { version = "0.10", default-features = false, optional = true }
46-
rand = { version = "0.6", default-features = false, optional = true }
46+
rand = { version = "0.8", default-features = false, optional = true }
4747

4848
[dev-dependencies]
49-
rand = "0.6"
50-
rand_core = "0.4"
49+
rand = "0.8"
50+
rand_core = "0.6"
5151
serde_test = "1.0"
5252
bitcoin_hashes = "0.10"
5353

5454
[target.wasm32-unknown-unknown.dev-dependencies]
5555
wasm-bindgen-test = "0.3"
56-
rand = { version = "0.6", features = ["wasm-bindgen"] }
5756

5857

5958
[[example]]

examples/generate_keys.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
extern crate secp256k1;
22

3-
use secp256k1::rand::rngs::OsRng;
3+
use secp256k1::rand::thread_rng;
44
use secp256k1::{PublicKey, Secp256k1, SecretKey};
55

66
fn main() {
77
let secp = Secp256k1::new();
8-
let mut rng = OsRng::new().unwrap();
8+
let mut rng = thread_rng();
99
// First option:
1010
let (seckey, pubkey) = secp.generate_keypair(&mut rng);
1111

src/ecdsa/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,11 @@ impl<C: Verification> Secp256k1<C> {
467467
///
468468
/// ```rust
469469
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
470-
/// # use secp256k1::rand::rngs::OsRng;
470+
/// # use secp256k1::rand::thread_rng;
471471
/// # use secp256k1::{Secp256k1, Message, Error};
472472
/// #
473473
/// # let secp = Secp256k1::new();
474-
/// # let mut rng = OsRng::new().expect("OsRng");
475-
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rng);
474+
/// # let (secret_key, public_key) = secp.generate_keypair(&mut thread_rng());
476475
/// #
477476
/// let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
478477
/// let sig = secp.sign(&message, &secret_key);
@@ -496,12 +495,11 @@ impl<C: Verification> Secp256k1<C> {
496495
///
497496
/// ```rust
498497
/// # #[cfg(all(feature = "std", feature = "rand-std"))] {
499-
/// # use secp256k1::rand::rngs::OsRng;
498+
/// # use secp256k1::rand::thread_rng;
500499
/// # use secp256k1::{Secp256k1, Message, Error};
501500
/// #
502501
/// # let secp = Secp256k1::new();
503-
/// # let mut rng = OsRng::new().expect("OsRng");
504-
/// # let (secret_key, public_key) = secp.generate_keypair(&mut rng);
502+
/// # let (secret_key, public_key) = secp.generate_keypair(&mut thread_rng());
505503
/// #
506504
/// let message = Message::from_slice(&[0xab; 32]).expect("32 bytes");
507505
/// let sig = secp.sign_ecdsa(&message, &secret_key);

src/key.rs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,10 +1512,7 @@ mod test {
15121512
use core::str::FromStr;
15131513

15141514
#[cfg(any(feature = "alloc", feature = "std"))]
1515-
use rand::{Error, ErrorKind, RngCore, thread_rng};
1516-
1517-
#[cfg(any(feature = "alloc", feature = "std"))]
1518-
use rand_core::impls;
1515+
use rand::{Error, RngCore, thread_rng, rngs::mock::StepRng};
15191516

15201517
#[cfg(target_arch = "wasm32")]
15211518
use wasm_bindgen_test::wasm_bindgen_test as test;
@@ -1594,7 +1591,6 @@ mod test {
15941591
#[test]
15951592
#[cfg(any(feature = "alloc", feature = "std"))]
15961593
fn test_out_of_range() {
1597-
15981594
struct BadRng(u8);
15991595
impl RngCore for BadRng {
16001596
fn next_u32(&mut self) -> u32 { unimplemented!() }
@@ -1687,28 +1683,9 @@ mod test {
16871683
#[test]
16881684
#[cfg(all(feature = "rand", any(feature = "alloc", feature = "std")))]
16891685
fn test_debug_output() {
1690-
use to_hex;
1691-
1692-
struct DumbRng(u32);
1693-
impl RngCore for DumbRng {
1694-
fn next_u32(&mut self) -> u32 {
1695-
self.0 = self.0.wrapping_add(1);
1696-
self.0
1697-
}
1698-
fn next_u64(&mut self) -> u64 {
1699-
self.next_u32() as u64
1700-
}
1701-
fn fill_bytes(&mut self, dest: &mut [u8]) {
1702-
impls::fill_bytes_via_next(self, dest);
1703-
}
1704-
1705-
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
1706-
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
1707-
}
1708-
}
17091686

17101687
let s = Secp256k1::new();
1711-
let (sk, _) = s.generate_keypair(&mut DumbRng(0));
1688+
let (sk, _) = s.generate_keypair(&mut StepRng::new(1, 1));
17121689

17131690
assert_eq!(&format!("{:?}", sk),
17141691
"SecretKey(#d3e0c51a23169bb5)");
@@ -1785,26 +1762,9 @@ mod test {
17851762
#[cfg(not(fuzzing))]
17861763
#[cfg(any(feature = "alloc", feature = "std"))]
17871764
fn test_pubkey_serialize() {
1788-
struct DumbRng(u32);
1789-
impl RngCore for DumbRng {
1790-
fn next_u32(&mut self) -> u32 {
1791-
self.0 = self.0.wrapping_add(1);
1792-
self.0
1793-
}
1794-
fn next_u64(&mut self) -> u64 {
1795-
self.next_u32() as u64
1796-
}
1797-
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
1798-
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
1799-
}
1800-
1801-
fn fill_bytes(&mut self, dest: &mut [u8]) {
1802-
impls::fill_bytes_via_next(self, dest);
1803-
}
1804-
}
18051765

18061766
let s = Secp256k1::new();
1807-
let (_, pk1) = s.generate_keypair(&mut DumbRng(0));
1767+
let (_, pk1) = s.generate_keypair(&mut StepRng::new(1,1));
18081768
assert_eq!(&pk1.serialize_uncompressed()[..],
18091769
&[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][..]);
18101770
assert_eq!(&pk1.serialize()[..],

src/lib.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
//! use secp256k1::hashes::sha256;
4848
//!
4949
//! let secp = Secp256k1::new();
50-
//! let mut rng = OsRng::new().expect("OsRng");
51-
//! let (secret_key, public_key) = secp.generate_keypair(&mut rng);
50+
//! let (secret_key, public_key) = secp.generate_keypair(&mut OsRng);
5251
//! let message = Message::from_hashed_data::<sha256::Hash>("Hello World!".as_bytes());
5352
//!
5453
//! let sig = secp.sign_ecdsa(&message, &secret_key);
@@ -1068,38 +1067,16 @@ mod benches {
10681067
use test::{Bencher, black_box};
10691068

10701069
use rand::{RngCore, thread_rng};
1070+
use rand::rngs::mock::StepRng;
10711071

10721072
use super::{Message, Secp256k1};
10731073

10741074
#[bench]
10751075
#[cfg(any(feature = "alloc", feature = "std"))]
10761076
pub fn generate(bh: &mut Bencher) {
1077-
struct CounterRng(u64);
1078-
impl RngCore for CounterRng {
1079-
fn next_u32(&mut self) -> u32 {
1080-
self.next_u64() as u32
1081-
}
1082-
1083-
fn next_u64(&mut self) -> u64 {
1084-
self.0 += 1;
1085-
self.0
1086-
}
1087-
1088-
fn fill_bytes(&mut self, dest: &mut [u8]) {
1089-
for chunk in dest.chunks_mut(64/8) {
1090-
let rand: [u8; 64/8] = unsafe {std::mem::transmute(self.next_u64())};
1091-
chunk.copy_from_slice(&rand[..chunk.len()]);
1092-
}
1093-
}
1094-
1095-
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
1096-
Ok(self.fill_bytes(dest))
1097-
}
1098-
}
1099-
11001077

11011078
let s = Secp256k1::new();
1102-
let mut r = CounterRng(0);
1079+
let mut r = StepRng::new(1, 1);
11031080
bh.iter( || {
11041081
let (sk, pk) = s.generate_keypair(&mut r);
11051082
black_box(sk);

src/schnorr.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ impl <C: Signing> Secp256k1<C> {
273273
mod tests {
274274
use core::str::FromStr;
275275

276-
use rand::{Error, ErrorKind, RngCore, rngs::ThreadRng, thread_rng};
277-
use rand_core::impls;
276+
use rand::{RngCore, rngs::ThreadRng, thread_rng};
278277
#[cfg(target_arch = "wasm32")]
279278
use wasm_bindgen_test::wasm_bindgen_test as test;
280279

@@ -516,26 +515,9 @@ mod tests {
516515
#[cfg(not(fuzzing))]
517516
#[cfg(all(feature = "rand", any(feature = "alloc", feature = "std")))]
518517
fn test_pubkey_serialize() {
519-
struct DumbRng(u32);
520-
impl RngCore for DumbRng {
521-
fn next_u32(&mut self) -> u32 {
522-
self.0 = self.0.wrapping_add(1);
523-
self.0
524-
}
525-
fn next_u64(&mut self) -> u64 {
526-
self.next_u32() as u64
527-
}
528-
fn try_fill_bytes(&mut self, _dest: &mut [u8]) -> Result<(), Error> {
529-
Err(Error::new(ErrorKind::Unavailable, "not implemented"))
530-
}
531-
532-
fn fill_bytes(&mut self, dest: &mut [u8]) {
533-
impls::fill_bytes_via_next(self, dest);
534-
}
535-
}
536-
518+
use rand::rngs::mock::StepRng;
537519
let secp = Secp256k1::new();
538-
let kp = KeyPair::new(&secp, &mut DumbRng(0));
520+
let kp = KeyPair::new(&secp, &mut StepRng::new(1, 1));
539521
let (pk, _parity) = kp.x_only_public_key();
540522
assert_eq!(
541523
&pk.serialize()[..],

0 commit comments

Comments
 (0)