From 9b83e1a52f7f2ab0cf54268ac1d64119191963bb Mon Sep 17 00:00:00 2001 From: Andrea Corbellini Date: Tue, 6 Feb 2024 16:28:22 -0800 Subject: [PATCH] Remove uses of `ThreadRng` ThreadRng shouldn't be used directly as it restricts the kinds of random number generators that people can pass as an input --- .../src/frost_utils/signing_commitment.rs | 5 ++--- ironfish-rust/src/frost_utils/split_secret.rs | 16 ++++++++-------- .../src/frost_utils/split_spender_key.rs | 4 ++-- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/ironfish-rust/src/frost_utils/signing_commitment.rs b/ironfish-rust/src/frost_utils/signing_commitment.rs index a28c00511d..1c5a17d3fc 100644 --- a/ironfish-rust/src/frost_utils/signing_commitment.rs +++ b/ironfish-rust/src/frost_utils/signing_commitment.rs @@ -76,7 +76,7 @@ mod test { use crate::test_util::create_identifiers; use ff::Field; use jubjub::Fr; - use rand::rngs::ThreadRng; + use rand::thread_rng; #[test] pub fn test_seed_provides_same_result() { @@ -85,14 +85,13 @@ mod test { let identifiers = create_identifiers(10); - let mut rng = ThreadRng::default(); let key_packages = split_secret( &SecretShareConfig { identifiers, min_signers: 2, secret: key.to_bytes().to_vec(), }, - &mut rng, + thread_rng(), ) .expect("key shares to be created"); let key_package = key_packages diff --git a/ironfish-rust/src/frost_utils/split_secret.rs b/ironfish-rust/src/frost_utils/split_secret.rs index 7d72dfd1b5..05ea804c5c 100644 --- a/ironfish-rust/src/frost_utils/split_secret.rs +++ b/ironfish-rust/src/frost_utils/split_secret.rs @@ -7,7 +7,7 @@ use ironfish_frost::frost::{ keys::{IdentifierList, KeyPackage, PublicKeyPackage}, Identifier, SigningKey, }; -use rand::rngs::ThreadRng; +use rand::{CryptoRng, RngCore}; use std::collections::HashMap; use crate::errors::{IronfishError, IronfishErrorKind}; @@ -18,9 +18,9 @@ pub struct SecretShareConfig { pub secret: Vec, } -pub(crate) fn split_secret( +pub(crate) fn split_secret( config: &SecretShareConfig, - rng: &mut ThreadRng, + mut rng: R, ) -> Result<(HashMap, PublicKeyPackage), IronfishError> { let secret_bytes: [u8; 32] = config .secret @@ -37,7 +37,7 @@ pub(crate) fn split_secret( config.identifiers.len() as u16, config.min_signers, identifier_list, - rng, + &mut rng, )?; for (_k, v) in shares.clone() { @@ -71,8 +71,8 @@ mod test { secret: vec, }; - let mut rng = rand::thread_rng(); - let result = split_secret(&config, &mut rng); + let rng = rand::thread_rng(); + let result = split_secret(&config, rng); assert!(result.is_err()); assert!( matches!(result.unwrap_err().kind, IronfishErrorKind::InvalidSecret), @@ -85,7 +85,7 @@ mod test { let identifiers = create_identifiers(10); let identifiers_length = identifiers.len(); - let mut rng = rand::thread_rng(); + let rng = rand::thread_rng(); let key = SaplingKey::generate_key().spend_authorizing_key.to_bytes(); @@ -95,7 +95,7 @@ mod test { secret: key.to_vec(), }; - let (key_packages, _) = split_secret(&config, &mut rng).unwrap(); + let (key_packages, _) = split_secret(&config, rng).unwrap(); assert_eq!(key_packages.len(), identifiers_length); let key_parts: Vec<_> = key_packages.values().cloned().collect(); diff --git a/ironfish-rust/src/frost_utils/split_spender_key.rs b/ironfish-rust/src/frost_utils/split_spender_key.rs index d9813fbb69..4c2af8bc8a 100644 --- a/ironfish-rust/src/frost_utils/split_spender_key.rs +++ b/ironfish-rust/src/frost_utils/split_spender_key.rs @@ -48,9 +48,9 @@ pub fn split_spender_key( secret, }; - let mut rng: rand::prelude::ThreadRng = thread_rng(); + let rng = thread_rng(); - let (key_packages, public_key_package) = split_secret(&secret_config, &mut rng)?; + let (key_packages, public_key_package) = split_secret(&secret_config, rng)?; let authorizing_key_bytes = public_key_package.verifying_key().serialize();