Skip to content

Commit

Permalink
feat: Use rand_xoshiro in account id builder
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Jan 7, 2025
1 parent ec67e26 commit 40cc791
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 14 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions objects/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ bench = false
concurrent = ["std"]
default = ["std"]
std = ["assembly/std", "miden-crypto/std", "miden-verifier/std", "vm-core/std", "vm-processor/std"]
testing = ["dep:winter-rand-utils", "dep:rand"]
testing = ["dep:winter-rand-utils", "dep:rand", "dep:rand_xoshiro"]

[dependencies]
assembly = { workspace = true }
log = { version = "0.4", optional = true }
miden-crypto = { workspace = true }
miden-verifier = { workspace = true }
rand = { workspace = true, optional = true, features = ["small_rng"] }
rand = { workspace = true, optional = true }
rand_xoshiro = { version = "0.6.0", default-features = false, optional = true }
thiserror = { workspace = true }
vm-core = { workspace = true }
vm-processor = { workspace = true }
Expand Down
10 changes: 4 additions & 6 deletions objects/src/accounts/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ impl AccountType {
impl rand::distributions::Distribution<AccountType> for rand::distributions::Standard {
/// Samples a uniformly random [`AccountType`] from the given `rng`.
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AccountType {
let account_type_distribution = rand::distributions::Uniform::new_inclusive(0, 3);
match account_type_distribution.sample(rng) {
match rng.gen_range(0..4) {
0 => AccountType::RegularAccountImmutableCode,
1 => AccountType::RegularAccountUpdatableCode,
2 => AccountType::FungibleFaucet,
3 => AccountType::NonFungibleFaucet,
_ => unreachable!("the uniform distribution should not produce higher values"),
_ => unreachable!("gen_range should not produce higher values"),
}
}
}
Expand Down Expand Up @@ -115,11 +114,10 @@ impl FromStr for AccountStorageMode {
impl rand::distributions::Distribution<AccountStorageMode> for rand::distributions::Standard {
/// Samples a uniformly random [`AccountStorageMode`] from the given `rng`.
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> AccountStorageMode {
let account_type_distribution = rand::distributions::Uniform::new_inclusive(0, 1);
match account_type_distribution.sample(rng) {
match rng.gen_range(0..2) {
0 => AccountStorageMode::Public,
1 => AccountStorageMode::Private,
_ => unreachable!("the uniform distribution should not produce higher values"),
_ => unreachable!("gen_range should not produce higher values"),
}
}
}
Expand Down
20 changes: 14 additions & 6 deletions objects/src/testing/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,25 @@ impl AccountIdBuilder {
AccountId::dummy(rng.gen(), account_type, storage_mode)
}

/// Builds an [`AccountId`] using the provided seed as input for a [`rand::rngs::StdRng`].
/// Builds an [`AccountId`] using the provided seed as input for an RNG implemented in
/// [`rand_xoshiro`].
///
/// If no [`AccountType`] or [`AccountStorageMode`] were previously set, random ones are
/// generated.
pub fn build_with_seed(self, rng_seed: [u8; 32]) -> AccountId {
// On non-64 bit architectures, SmallRng expects 16 bytes as seed input.
// Match the implementation of rand::rngs::SmallRng and use different RNGs depending on the
// platform.
#[cfg(not(target_pointer_width = "64"))]
let rng_seed: [u8; 16] = rng_seed.as_slice()[0..16]
.try_into()
.expect("we should have sliced off 16 elements");
let mut rng = rand::rngs::SmallRng::from_seed(rng_seed);
let mut rng = {
let rng_seed: [u8; 16] = rng_seed.as_slice()[0..16]
.try_into()
.expect("we should have sliced off 16 elements");
rand_xoshiro::Xoshiro128PlusPlus::from_seed(rng_seed)
};

#[cfg(target_pointer_width = "64")]
let mut rng = rand_xoshiro::Xoshiro256PlusPlus::from_seed(rng_seed);

self.build_with_rng(&mut rng)
}
}
Expand Down

0 comments on commit 40cc791

Please sign in to comment.