Skip to content

Commit 1033fdd

Browse files
committed
Update dependencies and refactor random number generation for improved consistency and performance
1 parent e83e1f8 commit 1033fdd

17 files changed

Lines changed: 123 additions & 99 deletions

File tree

‎Cargo.toml‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,26 +97,26 @@ plotters = { version = "0.3.7", default_features = false, features = [
9797
"line_series",
9898
"histogram",
9999
] }
100-
pretty = "0.10.0"
101-
prettydiff = "0.4.0"
102-
primitive-types = { version = "0.10.1", features = ["impl-serde"] }
103-
proc-macro2 = "1.0.24"
104-
proptest = "1.0.0"
105-
proptest-derive = "0.3.0"
106-
quote = "1.0.9"
107-
rand = "0.8.0"
108-
rayon = "1.5.0"
109-
ref-cast = "1.0.6"
110-
regex = "1.5.5"
111-
ripemd160 = "0.9.1"
100+
pretty = "0.12.5"
101+
prettydiff = "0.9.0"
102+
primitive-types = { version = "0.14.0", features = ["impl-serde"] }
103+
proc-macro2 = "1.0.106"
104+
proptest = "1.11.0"
105+
proptest-derive = "0.8.0"
106+
quote = "1.0.45"
107+
rand = "0.10.1"
108+
rayon = "1.12.0"
109+
ref-cast = "1.0.25"
110+
regex = "1.12.4"
111+
ripemd = "0.2.0"
112112
serde = { version = "1.0.228", features = ["derive"] }
113113
serde-name = "0.3.0"
114114
serde-reflection = "0.6.0"
115115
serde_bytes = "0.11.19"
116116
serde_json = "1.0.150"
117117
serde_yaml = "0.9.34"
118-
sha2 = "0.9.3"
119-
sha3 = "0.9.1"
118+
sha2 = "0.11.0"
119+
sha3 = "0.12.0"
120120
shell-words = "1.0.0"
121121
simplelog = "0.12.2"
122122
similar = "3.1.1"

‎crates/module-generation/src/generator.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use move_core_types::account_address::AccountAddress;
99
use move_ir_to_bytecode::compiler::compile_module;
1010
use move_ir_types::{ast::*, location::*};
1111
use move_symbol_pool::Symbol;
12-
use rand::{Rng, rngs::StdRng};
12+
use rand::{RngExt, rngs::StdRng};
1313
use std::{
1414
collections::{BTreeSet, VecDeque},
1515
iter::FromIterator,
@@ -87,11 +87,11 @@ pub struct ModuleGenerator<'a> {
8787

8888
impl<'a> ModuleGenerator<'a> {
8989
fn index(&mut self, bound: usize) -> usize {
90-
self.r#gen.gen_range(0..bound)
90+
self.r#gen.random_range(0..bound)
9191
}
9292

9393
fn identifier(&mut self) -> String {
94-
let len = self.r#gen.gen_range(10..self.options.max_string_size);
94+
let len = self.r#gen.random_range(10..self.options.max_string_size);
9595
random_string(self.r#gen, len)
9696
}
9797

@@ -156,8 +156,8 @@ impl<'a> ModuleGenerator<'a> {
156156
// TODO: Always change the base type to a reference if it's resource type. Then we can
157157
// allow functions to take resources.
158158
// if typ.is_nominal_resource { .... }
159-
if self.options.references_allowed && self.r#gen.gen_bool(0.25) {
160-
let is_mutable = self.r#gen.gen_bool(0.25);
159+
if self.options.references_allowed && self.r#gen.random_bool(0.25) {
160+
let is_mutable = self.r#gen.random_bool(0.25);
161161
Type::Reference(is_mutable, Box::new(typ))
162162
} else {
163163
typ
@@ -223,7 +223,7 @@ impl<'a> ModuleGenerator<'a> {
223223
fn struct_fields(&mut self, ty_params: &[StructTypeParameter]) -> StructDefinitionFields {
224224
let num_fields = self
225225
.r#gen
226-
.gen_range(self.options.min_fields..self.options.max_fields);
226+
.random_range(self.options.min_fields..self.options.max_fields);
227227
let fields: Fields<Type> = init!(num_fields, {
228228
(
229229
Spanned::unsafe_no_loc(Field_(self.identifier().into())),
@@ -324,7 +324,7 @@ impl<'a> ModuleGenerator<'a> {
324324
) -> ModuleDefinition {
325325
// TODO: Generation of struct and function handles to the `callable_modules`
326326
let module_name = {
327-
let len = r#gen.gen_range(10..options.max_string_size);
327+
let len = r#gen.random_range(10..options.max_string_size);
328328
random_string(r#gen, len)
329329
};
330330
let current_module = ModuleDefinition {

‎crates/module-generation/src/padding.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use crate::{options::ModuleGeneratorOptions, utils::random_string};
66
use move_binary_format::file_format::{Bytecode, CompiledModule, Signature};
77
use move_core_types::{account_address::AccountAddress, identifier::Identifier};
8-
use rand::{Rng, SeedableRng, rngs::StdRng};
8+
use rand::{RngExt, SeedableRng, rngs::StdRng};
99

1010
///////////////////////////////////////////////////////////////////////////
1111
// Padding of tables in compiled modules
@@ -40,7 +40,7 @@ impl Pad {
4040
fn pad_identifier_table(&mut self, module: &mut CompiledModule) {
4141
module.identifiers = (0..(self.table_size + module.identifiers.len()))
4242
.map(|_| {
43-
let len = self.r#gen.gen_range(10..self.options.max_string_size);
43+
let len = self.r#gen.random_range(10..self.options.max_string_size);
4444
Identifier::new(random_string(&mut self.r#gen, len)).unwrap()
4545
})
4646
.collect()

‎crates/module-generation/src/utils.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (c) The Move Contributors
33
// SPDX-License-Identifier: Apache-2.0
44

5-
use rand::{Rng, distributions::Alphanumeric, rngs::StdRng};
5+
use rand::{RngExt, distr::Alphanumeric, rngs::StdRng};
66

77
pub fn random_string(rng: &mut StdRng, len: usize) -> String {
88
if len == 0 {

‎crates/move-core-types/src/account_address.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: Apache-2.0
44

55
use hex::FromHex;
6-
use rand::{Rng, rngs::OsRng};
6+
use rand::{TryRng, rngs::SysRng};
77
use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as _};
88
use std::{convert::TryFrom, fmt, str::FromStr};
99

@@ -45,8 +45,10 @@ impl AccountAddress {
4545
}
4646

4747
pub fn random() -> Self {
48-
let mut rng = OsRng;
49-
let buf: [u8; Self::LENGTH] = rng.r#gen();
48+
let mut buf = [0u8; Self::LENGTH];
49+
SysRng
50+
.try_fill_bytes(&mut buf)
51+
.expect("failed to obtain randomness from the operating system");
5052
Self(buf)
5153
}
5254

‎crates/move-core-types/src/u256.rs‎

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use num::{BigInt, bigint::Sign};
66
#[cfg(any(test, feature = "fuzzing"))]
77
use proptest::strategy::BoxedStrategy;
88
use rand::{
9-
Rng,
10-
distributions::{
11-
Distribution, Standard,
12-
uniform::{SampleUniform, UniformSampler},
9+
Rng, RngExt,
10+
distr::{
11+
Distribution, StandardUniform,
12+
uniform::{Error as UniformError, SampleBorrow, SampleUniform, UniformSampler},
1313
},
1414
};
1515
use std::{
@@ -308,9 +308,7 @@ impl U256 {
308308

309309
/// U256 to 32 little endian bytes
310310
pub fn to_le_bytes(self) -> [u8; U256_NUM_BYTES] {
311-
let mut bytes = [0u8; U256_NUM_BYTES];
312-
self.0.to_little_endian(&mut bytes);
313-
bytes
311+
self.0.to_little_endian()
314312
}
315313

316314
/// Leading zeros of the number
@@ -553,11 +551,11 @@ impl TryFrom<U256> for u128 {
553551
}
554552
}
555553

556-
impl Distribution<U256> for Standard {
554+
impl Distribution<U256> for StandardUniform {
557555
#[inline]
558556
fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> U256 {
559557
let mut dest = [0; U256_NUM_BYTES];
560-
rng.fill_bytes(&mut dest);
558+
rng.fill(&mut dest);
561559
U256::from_le_bytes(&dest)
562560
}
563561
}
@@ -578,28 +576,29 @@ impl SampleUniform for U256 {
578576
impl UniformSampler for UniformU256 {
579577
type X = U256;
580578

581-
fn new<B1, B2>(low: B1, high: B2) -> Self
579+
fn new<B1, B2>(low: B1, high: B2) -> Result<Self, UniformError>
582580
where
583-
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
584-
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
581+
B1: SampleBorrow<Self::X> + Sized,
582+
B2: SampleBorrow<Self::X> + Sized,
585583
{
586584
let low = *low.borrow();
587585
let high = *high.borrow();
588-
assert!(low < high, "Uniform::new called with `low >= high`");
586+
if low >= high {
587+
return Err(UniformError::EmptyRange);
588+
}
589589
UniformSampler::new_inclusive(low, high - U256::one())
590590
}
591591

592-
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Self
592+
fn new_inclusive<B1, B2>(low: B1, high: B2) -> Result<Self, UniformError>
593593
where
594-
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
595-
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
594+
B1: SampleBorrow<Self::X> + Sized,
595+
B2: SampleBorrow<Self::X> + Sized,
596596
{
597597
let low = *low.borrow();
598598
let high = *high.borrow();
599-
assert!(
600-
low <= high,
601-
"Uniform::new_inclusive called with `low > high`"
602-
);
599+
if low > high {
600+
return Err(UniformError::EmptyRange);
601+
}
603602
let unsigned_max = U256::max_value();
604603

605604
let range = high.wrapping_sub(low).wrapping_add(U256::one());
@@ -610,11 +609,11 @@ impl UniformSampler for UniformU256 {
610609
U256::zero()
611610
};
612611

613-
UniformU256 {
612+
Ok(UniformU256 {
614613
low,
615614
range,
616615
z: ints_to_reject,
617-
}
616+
})
618617
}
619618

620619
fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> Self::X {
@@ -623,59 +622,64 @@ impl UniformSampler for UniformU256 {
623622
let unsigned_max = U256::max_value();
624623
let zone = unsigned_max - self.z;
625624
loop {
626-
let v: U256 = rng.r#gen();
625+
let v: U256 = rng.random();
627626
let (hi, lo) = v.wmul(range);
628627
if lo <= zone {
629628
return self.low.wrapping_add(hi);
630629
}
631630
}
632631
} else {
633632
// Sample from the entire integer range.
634-
rng.r#gen()
633+
rng.random()
635634
}
636635
}
637636

638-
fn sample_single<R: rand::Rng + ?Sized, B1, B2>(low: B1, high: B2, rng: &mut R) -> Self::X
637+
fn sample_single<R: rand::Rng + ?Sized, B1, B2>(
638+
low: B1,
639+
high: B2,
640+
rng: &mut R,
641+
) -> Result<Self::X, UniformError>
639642
where
640-
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
641-
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
643+
B1: SampleBorrow<Self::X> + Sized,
644+
B2: SampleBorrow<Self::X> + Sized,
642645
{
643646
let low = *low.borrow();
644647
let high = *high.borrow();
645-
assert!(low < high, "UniformSampler::sample_single: low >= high");
648+
if low >= high {
649+
return Err(UniformError::EmptyRange);
650+
}
646651
Self::sample_single_inclusive(low, high - U256::one(), rng)
647652
}
648653

649654
fn sample_single_inclusive<R: rand::Rng + ?Sized, B1, B2>(
650655
low: B1,
651656
high: B2,
652657
rng: &mut R,
653-
) -> Self::X
658+
) -> Result<Self::X, UniformError>
654659
where
655-
B1: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
656-
B2: rand::distributions::uniform::SampleBorrow<Self::X> + Sized,
660+
B1: SampleBorrow<Self::X> + Sized,
661+
B2: SampleBorrow<Self::X> + Sized,
657662
{
658663
let low = *low.borrow();
659664
let high = *high.borrow();
660-
assert!(
661-
low <= high,
662-
"UniformSampler::sample_single_inclusive: low > high"
663-
);
665+
if low > high {
666+
return Err(UniformError::EmptyRange);
667+
}
664668
let range = high.wrapping_sub(low).wrapping_add(U256::one());
665669
// If the above resulted in wrap-around to 0, the range is U256::MIN..=U256::MAX,
666670
// and any integer will do.
667671
if range == U256::zero() {
668-
return rng.r#gen();
672+
return Ok(rng.random());
669673
}
670674
// conservative but fast approximation. `- 1` is necessary to allow the
671675
// same comparison without bias.
672676
let zone = (range << range.leading_zeros()).wrapping_sub(U256::one());
673677

674678
loop {
675-
let v: U256 = rng.r#gen();
679+
let v: U256 = rng.random();
676680
let (hi, lo) = v.wmul(range);
677681
if lo <= zone {
678-
return low.wrapping_add(hi);
682+
return Ok(low.wrapping_add(hi));
679683
}
680684
}
681685
}

‎crates/move-core-types/src/unit_tests/staged_enum_variant_order/move_type_layout.yaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
0: Bool
32
1: U8
43
2: U64

‎crates/move-core-types/src/unit_tests/staged_enum_variant_order/transaction_argument.yaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
0: U8
32
1: U64
43
2: U128

‎crates/move-core-types/src/unit_tests/staged_enum_variant_order/type_tag.yaml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
---
21
0: Bool
32
1: U8
43
2: U64

‎crates/move-package/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ colored.workspace = true
1919
serde_yaml.workspace = true
2020
tempfile.workspace = true
2121
sha2.workspace = true
22+
hex.workspace = true
2223
regex.workspace = true
2324
treeline.workspace = true
2425
once_cell.workspace = true

0 commit comments

Comments
 (0)