Skip to content

Commit

Permalink
Remove uses of ThreadRng
Browse files Browse the repository at this point in the history
ThreadRng shouldn't be used directly as it restricts the kinds of random
number generators that people can pass as an input
  • Loading branch information
andreacorbellini authored and andiflabs committed Feb 7, 2024
1 parent 63b5541 commit 9b83e1a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions ironfish-rust/src/frost_utils/signing_commitment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions ironfish-rust/src/frost_utils/split_secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -18,9 +18,9 @@ pub struct SecretShareConfig {
pub secret: Vec<u8>,
}

pub(crate) fn split_secret(
pub(crate) fn split_secret<R: RngCore + CryptoRng>(
config: &SecretShareConfig,
rng: &mut ThreadRng,
mut rng: R,
) -> Result<(HashMap<Identifier, KeyPackage>, PublicKeyPackage), IronfishError> {
let secret_bytes: [u8; 32] = config
.secret
Expand All @@ -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() {
Expand Down Expand Up @@ -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),
Expand All @@ -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();

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

Expand Down

0 comments on commit 9b83e1a

Please sign in to comment.