Skip to content

Commit

Permalink
Refactor random generation to CryptoRngCore
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed Dec 13, 2024
1 parent 0011f19 commit 4605cf3
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 20 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/datatool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ argh.workspace = true
bech32 = "0.11.0"
bitcoin = { workspace = true, features = ["std"] }
hex.workspace = true
rand.workspace = true
rand_core.workspace = true
secp256k1 = { workspace = true, features = ["global-context", "std"] }
serde_json.workspace = true
terrors = "0.3.0"
Expand Down
2 changes: 1 addition & 1 deletion bin/datatool/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::path::PathBuf;

use argh::FromArgs;
use bitcoin::Network;
use rand::rngs::OsRng;
use rand_core::OsRng;

/// Args.
#[derive(FromArgs)]
Expand Down
2 changes: 1 addition & 1 deletion bin/datatool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod util;
use std::path::PathBuf;

use args::CmdContext;
use rand::rngs::OsRng;
use rand_core::OsRng;
use util::{exec_subc, resolve_network};

fn main() {
Expand Down
6 changes: 3 additions & 3 deletions bin/datatool/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use bitcoin::{
bip32::{Xpriv, Xpub},
Network,
};
use rand::{CryptoRng, RngCore};
use rand_core::CryptoRngCore;
use strata_key_derivation::{
operator::{convert_base_xpub_to_message_xpub, convert_base_xpub_to_wallet_xpub, OperatorKeys},
sequencer::SequencerKeys,
Expand Down Expand Up @@ -237,9 +237,9 @@ fn exec_genparams(cmd: SubcParams, ctx: &mut CmdContext) -> anyhow::Result<()> {
/// # Notes
///
/// Takes a mutable reference to an RNG to allow flexibility in testing.
/// The actual generation requires a high-entropy source like [`OsRng`](rand::rngs::OsRng)
/// The actual generation requires a high-entropy source like [`OsRng`](rand_core::OsRng)
/// to securely generate extended private keys.
fn gen_priv<R: CryptoRng + RngCore>(rng: &mut R, net: Network) -> ZeroizableXpriv {
fn gen_priv<R: CryptoRngCore>(rng: &mut R, net: Network) -> ZeroizableXpriv {

Check warning on line 242 in bin/datatool/src/util.rs

View check run for this annotation

Codecov / codecov/patch

bin/datatool/src/util.rs#L242

Added line #L242 was not covered by tests
let mut seed = [0u8; 32];
rng.fill_bytes(&mut seed);
let mut xpriv = Xpriv::new_master(net, &seed).expect("valid seed");
Expand Down
2 changes: 1 addition & 1 deletion bin/strata-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ console = "0.15.8"
dialoguer = "0.11.0"
directories = "5.0.1"
indicatif = { version = "0.17.8", features = ["improved_unicode", "tokio"] }
rand.workspace = true
rand_core.workspace = true
reqwest.workspace = true
serde.workspace = true
sha2 = { version = "0.10.8", features = ["loongarch64_asm"] }
Expand Down
2 changes: 1 addition & 1 deletion bin/strata-cli/src/cmd/change_pwd.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use argh::FromArgs;
use console::Term;
use rand::rngs::OsRng;
use rand_core::OsRng;

use crate::seed::{password::Password, EncryptedSeedPersister, Seed};

Expand Down
5 changes: 3 additions & 2 deletions bin/strata-cli/src/cmd/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use argh::FromArgs;
use bdk_wallet::{bitcoin::Address, KeychainKind};
use console::Term;
use indicatif::ProgressBar;
use rand::{distributions::uniform::SampleRange, rngs::OsRng};
use reqwest::{StatusCode, Url};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
Expand Down Expand Up @@ -71,15 +70,17 @@ pub async fn faucet(args: FaucetArgs, seed: Seed, settings: Settings) {
hasher
};
let pb = ProgressBar::new_spinner();
let mut counter = 0u64;

Check warning on line 73 in bin/strata-cli/src/cmd/faucet.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-cli/src/cmd/faucet.rs#L73

Added line #L73 was not covered by tests
while !pow_valid(
prehash.clone(),
challenge.difficulty,
solution.to_le_bytes(),
) {
solution += 1;
if (0..100).sample_single(&mut OsRng) == 0 {
if counter % 100 == 0 {

Check warning on line 80 in bin/strata-cli/src/cmd/faucet.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-cli/src/cmd/faucet.rs#L80

Added line #L80 was not covered by tests
pb.set_message(format!("Trying {solution}"));
}
counter += 1;

Check warning on line 83 in bin/strata-cli/src/cmd/faucet.rs

View check run for this annotation

Codecov / codecov/patch

bin/strata-cli/src/cmd/faucet.rs#L83

Added line #L83 was not covered by tests
}
pb.finish_with_message(format!(
"✔ Solved challenge after {solution} attempts. Claiming now."
Expand Down
2 changes: 1 addition & 1 deletion bin/strata-cli/src/recovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bdk_wallet::{
miniscript::{descriptor::DescriptorKeyParseError, Descriptor},
template::DescriptorTemplateOut,
};
use rand::{rngs::OsRng, RngCore};
use rand_core::{OsRng, RngCore};
use sha2::{Digest, Sha256};
use sled::IVec;
use terrors::OneOf;
Expand Down
8 changes: 4 additions & 4 deletions bin/strata-cli/src/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use bip39::{Language, Mnemonic};
use console::Term;
use dialoguer::{Confirm, Input};
use password::{HashVersion, IncorrectPassword, Password};
use rand::{rngs::OsRng, CryptoRng, RngCore};
use rand_core::{CryptoRngCore, OsRng};
use sha2::{Digest, Sha256};
use terrors::OneOf;
use zeroize::Zeroizing;
Expand All @@ -31,7 +31,7 @@ impl BaseWallet {
pub struct Seed(Zeroizing<[u8; SEED_LEN]>);

impl Seed {
fn gen<R: CryptoRng + RngCore>(rng: &mut R) -> Self {
fn gen<R: CryptoRngCore>(rng: &mut R) -> Self {
let mut bytes = Zeroizing::new([0u8; SEED_LEN]);
rng.fill_bytes(bytes.as_mut());
Self(bytes)
Expand All @@ -50,7 +50,7 @@ impl Seed {
hasher.finalize().into()
}

pub fn encrypt<R: CryptoRng + RngCore>(
pub fn encrypt<R: CryptoRngCore>(
&self,
password: &mut Password,
rng: &mut R,
Expand Down Expand Up @@ -264,7 +264,7 @@ pub mod password;

#[cfg(test)]
mod test {
use rand::rngs::OsRng;
use rand_core::OsRng;
use sha2::digest::generic_array::GenericArray;

use super::*;
Expand Down
1 change: 1 addition & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bitcoin = { workspace = true, features = ["rand-std"] }
hex.workspace = true
musig2.workspace = true
rand.workspace = true
rand_core.workspace = true
revm.workspace = true
rockbound.workspace = true
serde_json.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions crates/test-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use arbitrary::{Arbitrary, Unstructured};
use rand::{rngs::OsRng, CryptoRng, RngCore};
use rand_core::{CryptoRngCore, OsRng};

pub mod bitcoin;
pub mod bridge;
Expand Down Expand Up @@ -57,15 +57,15 @@ impl ArbitraryGenerator {
/// # Arguments
///
/// * `rng` - An RNG to be used for generating the arbitrary instance. Provided RNG must
/// implement the [`RngCore`] and [`CryptoRng`] traits.
/// implement the [`CryptoRngCore`] trait.
///
/// # Returns
///
/// An arbitrary instance of type `T`.
pub fn generate_with_rng<'a, T, R>(&'a mut self, rng: &mut R) -> T
where
T: Arbitrary<'a> + Clone,
R: RngCore + CryptoRng,
R: CryptoRngCore,
{
rng.fill_bytes(&mut self.buf);
let mut u = Unstructured::new(&self.buf);
Expand Down

0 comments on commit 4605cf3

Please sign in to comment.