From 7809a0e093c64356ea7e76d94ef11fa2cbbe9609 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 3 Mar 2025 13:12:03 -0600 Subject: [PATCH 1/5] Bump to 0.5.0-pre --- Cargo.toml | 3 ++- README.md | 4 ++-- src/lib.rs | 4 ++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 720512a6..cdd700f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,8 @@ categories = [ "algorithms", "data-structures", "science" ] license = "MIT OR Apache-2.0" name = "num-bigint" repository = "https://github.com/rust-num/num-bigint" -version = "0.4.6" +version = "0.5.0-pre" +publish = false readme = "README.md" exclude = ["/ci/*", "/.github/*"] edition = "2021" diff --git a/README.md b/README.md index cce185b9..93556383 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Add this to your `Cargo.toml`: ```toml [dependencies] -num-bigint = "0.4" +num-bigint = "0.5" ``` ## Features @@ -30,7 +30,7 @@ feature is enabled. To enable it include rand as ```toml rand = "0.8" -num-bigint = { version = "0.4", features = ["rand"] } +num-bigint = { version = "0.5", features = ["rand"] } ``` Note that you must use the version of `rand` that `num-bigint` is compatible diff --git a/src/lib.rs b/src/lib.rs index 6e47479d..20c24f0c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,7 +72,7 @@ //! //! ```toml //! rand = "0.8" -//! num-bigint = { version = "0.4", features = ["rand"] } +//! num-bigint = { version = "0.5", features = ["rand"] } //! ``` //! //! Note that you must use the version of `rand` that `num-bigint` is compatible @@ -96,7 +96,7 @@ //! The `num-bigint` crate is tested for rustc 1.60 and greater. #![cfg_attr(docsrs, feature(doc_cfg))] -#![doc(html_root_url = "https://docs.rs/num-bigint/0.4")] +#![doc(html_root_url = "https://docs.rs/num-bigint/0.5")] #![warn(rust_2018_idioms)] #![no_std] From fdc527df007588127ac20231fcc2e063f7ac6ac4 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 3 Mar 2025 13:13:27 -0600 Subject: [PATCH 2/5] Bump MSRV to 1.63 --- .github/workflows/ci.yaml | 2 +- .github/workflows/master.yaml | 2 +- .github/workflows/pr.yaml | 2 +- Cargo.toml | 2 +- README.md | 6 +++--- ci/rustup.sh | 2 +- ci/test_full.sh | 2 +- src/lib.rs | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6ed00986..6143532c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -9,7 +9,7 @@ jobs: strategy: matrix: rust: [ - 1.60.0, # MSRV + 1.63.0, # MSRV stable, beta, nightly diff --git a/.github/workflows/master.yaml b/.github/workflows/master.yaml index 034e3793..4367f2ac 100644 --- a/.github/workflows/master.yaml +++ b/.github/workflows/master.yaml @@ -13,7 +13,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [1.60.0, stable] + rust: [1.63.0, stable] steps: - uses: actions/checkout@v4 - uses: actions/cache@v4 diff --git a/.github/workflows/pr.yaml b/.github/workflows/pr.yaml index baac6484..3be41568 100644 --- a/.github/workflows/pr.yaml +++ b/.github/workflows/pr.yaml @@ -9,7 +9,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - rust: [1.60.0, stable] + rust: [1.63.0, stable] steps: - uses: actions/checkout@v4 - uses: actions/cache@v4 diff --git a/Cargo.toml b/Cargo.toml index cdd700f5..04b6abf6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ publish = false readme = "README.md" exclude = ["/ci/*", "/.github/*"] edition = "2021" -rust-version = "1.60" +rust-version = "1.63" [features] default = ["std"] diff --git a/README.md b/README.md index 93556383..4e00c68b 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![crate](https://img.shields.io/crates/v/num-bigint.svg)](https://crates.io/crates/num-bigint) [![documentation](https://docs.rs/num-bigint/badge.svg)](https://docs.rs/num-bigint) -[![minimum rustc 1.60](https://img.shields.io/badge/rustc-1.60+-red.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) +[![minimum rustc 1.63](https://img.shields.io/badge/rustc-1.63+-red.svg)](https://rust-lang.github.io/rfcs/2495-min-rust-version.html) [![build status](https://github.com/rust-num/num-bigint/workflows/master/badge.svg)](https://github.com/rust-num/num-bigint/actions) Big integer types for Rust, `BigInt` and `BigUint`. @@ -42,7 +42,7 @@ Release notes are available in [RELEASES.md](RELEASES.md). ## Compatibility -The `num-bigint` crate is tested for rustc 1.60 and greater. +The `num-bigint` crate is tested for rustc 1.63 and greater. ## Alternatives @@ -52,7 +52,7 @@ table offers a brief comparison to a few alternatives. | Crate | License | Min rustc | Implementation | Features | | :--------------- | :------------- | :-------- | :------------- | :------- | -| **`num-bigint`** | MIT/Apache-2.0 | 1.60 | pure rust | dynamic width, number theoretical functions | +| **`num-bigint`** | MIT/Apache-2.0 | 1.63 | pure rust | dynamic width, number theoretical functions | | [`awint`] | MIT/Apache-2.0 | 1.66 | pure rust | fixed width, heap or stack, concatenation macros | | [`bnum`] | MIT/Apache-2.0 | 1.65 | pure rust | fixed width, parity with Rust primitives including floats | | [`crypto-bigint`] | MIT/Apache-2.0 | 1.73 | pure rust | fixed width, stack only | diff --git a/ci/rustup.sh b/ci/rustup.sh index 6cfe67ef..e33a607e 100755 --- a/ci/rustup.sh +++ b/ci/rustup.sh @@ -5,6 +5,6 @@ set -ex ci=$(dirname $0) -for version in 1.60.0 stable beta nightly; do +for version in 1.63.0 stable beta nightly; do rustup run "$version" "$ci/test_full.sh" done diff --git a/ci/test_full.sh b/ci/test_full.sh index 3924be90..0c80f2dd 100755 --- a/ci/test_full.sh +++ b/ci/test_full.sh @@ -3,7 +3,7 @@ set -e CRATE=num-bigint -MSRV=1.60 +MSRV=1.63 get_rust_version() { local array=($(rustc --version)); diff --git a/src/lib.rs b/src/lib.rs index 20c24f0c..50885d77 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -93,7 +93,7 @@ //! //! ## Compatibility //! -//! The `num-bigint` crate is tested for rustc 1.60 and greater. +//! The `num-bigint` crate is tested for rustc 1.63 and greater. #![cfg_attr(docsrs, feature(doc_cfg))] #![doc(html_root_url = "https://docs.rs/num-bigint/0.5")] From 0549d10a97b97635241ccff9864fd768f0f110de Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 3 Mar 2025 13:22:34 -0600 Subject: [PATCH 3/5] Upgrade to rand 0.9 --- Cargo.toml | 2 +- README.md | 4 +-- benches/rng/mod.rs | 4 --- ci/big_rand/Cargo.toml | 8 ++--- ci/big_rand/src/lib.rs | 58 +++++++++++++++++----------------- ci/big_rand/src/torture.rs | 4 +-- src/bigrand.rs | 64 +++++++++++++++++++++++++++----------- 7 files changed, 83 insertions(+), 61 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 04b6abf6..adeaa102 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,7 +57,7 @@ features = ["i128"] [dependencies.rand] optional = true -version = "0.8" +version = "0.9" default-features = false [dependencies.serde] diff --git a/README.md b/README.md index 4e00c68b..ae5ce486 100644 --- a/README.md +++ b/README.md @@ -29,12 +29,12 @@ if your compiler is not new enough. feature is enabled. To enable it include rand as ```toml -rand = "0.8" +rand = "0.9" num-bigint = { version = "0.5", features = ["rand"] } ``` Note that you must use the version of `rand` that `num-bigint` is compatible -with: `0.8`. +with: `0.9`. ## Releases diff --git a/benches/rng/mod.rs b/benches/rng/mod.rs index 33e4f0fa..6659da36 100644 --- a/benches/rng/mod.rs +++ b/benches/rng/mod.rs @@ -31,8 +31,4 @@ impl RngCore for XorShiftStar { chunk.copy_from_slice(slice) } } - - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> { - Ok(self.fill_bytes(dest)) - } } diff --git a/ci/big_rand/Cargo.toml b/ci/big_rand/Cargo.toml index d2bfe353..d808134e 100644 --- a/ci/big_rand/Cargo.toml +++ b/ci/big_rand/Cargo.toml @@ -6,10 +6,10 @@ edition = "2018" [dependencies] num-traits = "0.2.11" -rand = "0.8" -rand_chacha = "0.3" -rand_isaac = "0.3" -rand_xorshift = "0.3" +rand = "0.9" +rand_chacha = "0.9" +rand_isaac = "0.4" +rand_xorshift = "0.4" [dependencies.num-bigint] features = ["rand"] diff --git a/ci/big_rand/src/lib.rs b/ci/big_rand/src/lib.rs index 69728777..4a932baf 100644 --- a/ci/big_rand/src/lib.rs +++ b/ci/big_rand/src/lib.rs @@ -1,6 +1,6 @@ //! Test randomization of `BigUint` and `BigInt` //! -//! This test is in a completely separate crate so `rand::thread_rng()` +//! This test is in a completely separate crate so `rand::rng()` //! can be available without "infecting" the rest of the build with //! `rand`'s default features, especially not `rand/std`. @@ -11,13 +11,13 @@ mod torture; mod biguint { use num_bigint::{BigUint, RandBigInt, RandomBits}; use num_traits::Zero; - use rand::distributions::{Distribution, Uniform}; - use rand::thread_rng; + use rand::distr::{Distribution, Uniform}; + use rand::rng; use rand::{Rng, SeedableRng}; #[test] fn test_rand() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: BigUint = rng.gen_biguint(137); assert!(n.bits() <= 137); assert!(rng.gen_biguint(0).is_zero()); @@ -25,7 +25,7 @@ mod biguint { #[test] fn test_rand_bits() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: BigUint = rng.sample(&RandomBits::new(137)); assert!(n.bits() <= 137); let z: BigUint = rng.sample(&RandomBits::new(0)); @@ -34,7 +34,7 @@ mod biguint { #[test] fn test_rand_range() { - let mut rng = thread_rng(); + let mut rng = rng(); for _ in 0..10 { assert_eq!( @@ -58,13 +58,13 @@ mod biguint { #[test] #[should_panic] fn test_zero_rand_range() { - thread_rng().gen_biguint_range(&BigUint::from(54u32), &BigUint::from(54u32)); + rng().gen_biguint_range(&BigUint::from(54u32), &BigUint::from(54u32)); } #[test] #[should_panic] fn test_negative_rand_range() { - let mut rng = thread_rng(); + let mut rng = rng(); let l = BigUint::from(2352u32); let u = BigUint::from(3513u32); // Switching u and l should fail: @@ -73,17 +73,17 @@ mod biguint { #[test] fn test_rand_uniform() { - let mut rng = thread_rng(); + let mut rng = rng(); - let tiny = Uniform::new(BigUint::from(236u32), BigUint::from(237u32)); + let tiny = Uniform::new(BigUint::from(236u32), BigUint::from(237u32)).unwrap(); for _ in 0..10 { assert_eq!(rng.sample(&tiny), BigUint::from(236u32)); } let l = BigUint::from(403469000u32 + 2352); let u = BigUint::from(403469000u32 + 3513); - let below = Uniform::new(BigUint::zero(), u.clone()); - let range = Uniform::new(l.clone(), u.clone()); + let below = Uniform::new(BigUint::zero(), u.clone()).unwrap(); + let range = Uniform::new(l.clone(), u.clone()).unwrap(); for _ in 0..1000 { let n: BigUint = rng.sample(&below); assert!(n < u); @@ -190,8 +190,8 @@ mod biguint { assert_eq!((&hi - 1u32).nth_root(n), root); } - let mut rng = thread_rng(); - let bit_range = Uniform::new(0, 2048); + let mut rng = rng(); + let bit_range = Uniform::new(0, 2048).unwrap(); let sample_bits: Vec<_> = bit_range.sample_iter(&mut rng).take(100).collect(); for bits in sample_bits { let x = rng.gen_biguint(bits); @@ -206,13 +206,13 @@ mod biguint { mod bigint { use num_bigint::{BigInt, RandBigInt, RandomBits}; use num_traits::Zero; - use rand::distributions::Uniform; - use rand::thread_rng; + use rand::distr::Uniform; + use rand::rng; use rand::{Rng, SeedableRng}; #[test] fn test_rand() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: BigInt = rng.gen_bigint(137); assert!(n.bits() <= 137); assert!(rng.gen_bigint(0).is_zero()); @@ -220,7 +220,7 @@ mod bigint { #[test] fn test_rand_bits() { - let mut rng = thread_rng(); + let mut rng = rng(); let n: BigInt = rng.sample(&RandomBits::new(137)); assert!(n.bits() <= 137); let z: BigInt = rng.sample(&RandomBits::new(0)); @@ -229,7 +229,7 @@ mod bigint { #[test] fn test_rand_range() { - let mut rng = thread_rng(); + let mut rng = rng(); for _ in 0..10 { assert_eq!( @@ -239,7 +239,7 @@ mod bigint { } fn check(l: BigInt, u: BigInt) { - let mut rng = thread_rng(); + let mut rng = rand::rng(); for _ in 0..1000 { let n: BigInt = rng.gen_bigint_range(&l, &u); assert!(n >= l); @@ -256,13 +256,13 @@ mod bigint { #[test] #[should_panic] fn test_zero_rand_range() { - thread_rng().gen_bigint_range(&BigInt::from(54), &BigInt::from(54)); + rng().gen_bigint_range(&BigInt::from(54), &BigInt::from(54)); } #[test] #[should_panic] fn test_negative_rand_range() { - let mut rng = thread_rng(); + let mut rng = rng(); let l = BigInt::from(2352); let u = BigInt::from(3513); // Switching u and l should fail: @@ -271,16 +271,16 @@ mod bigint { #[test] fn test_rand_uniform() { - let mut rng = thread_rng(); + let mut rng = rng(); - let tiny = Uniform::new(BigInt::from(236u32), BigInt::from(237u32)); + let tiny = Uniform::new(BigInt::from(236u32), BigInt::from(237u32)).unwrap(); for _ in 0..10 { assert_eq!(rng.sample(&tiny), BigInt::from(236u32)); } fn check(l: BigInt, u: BigInt) { - let mut rng = thread_rng(); - let range = Uniform::new(l.clone(), u.clone()); + let mut rng = rand::rng(); + let range = Uniform::new(l.clone(), u.clone()).unwrap(); for _ in 0..1000 { let n: BigInt = rng.sample(&range); assert!(n >= l); @@ -366,11 +366,11 @@ mod bigint { #[test] fn test_random_shr() { - use rand::distributions::Standard; + use rand::distr::StandardUniform; use rand::Rng; - let rng = rand::thread_rng(); + let rng = rand::rng(); - for p in rng.sample_iter::(&Standard).take(1000) { + for p in rng.sample_iter::(&StandardUniform).take(1000) { let big = BigInt::from(p); let bigger = &big << 1000; assert_eq!(&bigger >> 1000, big); diff --git a/ci/big_rand/src/torture.rs b/ci/big_rand/src/torture.rs index 72059ef2..fb0d3f33 100644 --- a/ci/big_rand/src/torture.rs +++ b/ci/big_rand/src/torture.rs @@ -13,8 +13,8 @@ fn test_mul_divide_torture_count(count: usize) { for _ in 0..count { // Test with numbers of random sizes: - let xbits = rng.gen_range(0..bits_max); - let ybits = rng.gen_range(0..bits_max); + let xbits = rng.random_range(0..bits_max); + let ybits = rng.random_range(0..bits_max); let x = rng.gen_biguint(xbits); let y = rng.gen_biguint(ybits); diff --git a/src/bigrand.rs b/src/bigrand.rs index e5cbacdb..8b29aa81 100644 --- a/src/bigrand.rs +++ b/src/bigrand.rs @@ -2,7 +2,7 @@ #![cfg(feature = "rand")] #![cfg_attr(docsrs, doc(cfg(feature = "rand")))] -use rand::distributions::uniform::{SampleBorrow, SampleUniform, UniformSampler}; +use rand::distr::uniform::{Error, SampleBorrow, SampleUniform, UniformSampler}; use rand::prelude::*; use crate::BigInt; @@ -96,12 +96,12 @@ impl RandBigInt for R { // again with probability 0.5. This is because otherwise, // the probability of generating a zero BigInt would be // double that of any other number. - if self.gen() { + if self.random() { continue; } else { NoSign } - } else if self.gen() { + } else if self.random() { Plus } else { Minus @@ -154,29 +154,33 @@ impl UniformSampler for UniformBigUint { type X = BigUint; #[inline] - fn new(low_b: B1, high_b: B2) -> Self + fn new(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low < high); - UniformBigUint { + if low >= high { + return Err(Error::EmptyRange); + } + Ok(UniformBigUint { len: high - low, base: low.clone(), - } + }) } #[inline] - fn new_inclusive(low_b: B1, high_b: B2) -> Self + fn new_inclusive(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low <= high); + if low > high { + return Err(Error::EmptyRange); + } Self::new(low, high + 1u32) } @@ -186,12 +190,21 @@ impl UniformSampler for UniformBigUint { } #[inline] - fn sample_single(low: B1, high: B2, rng: &mut R) -> Self::X + fn sample_single( + low_b: B1, + high_b: B2, + rng: &mut R, + ) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { - rng.gen_biguint_range(low.borrow(), high.borrow()) + let low = low_b.borrow(); + let high = high_b.borrow(); + if low >= high { + return Err(Error::EmptyRange); + } + Ok(rng.gen_biguint_range(low, high)) } } @@ -210,29 +223,33 @@ impl UniformSampler for UniformBigInt { type X = BigInt; #[inline] - fn new(low_b: B1, high_b: B2) -> Self + fn new(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low < high); - UniformBigInt { + if low >= high { + return Err(Error::EmptyRange); + } + Ok(UniformBigInt { len: (high - low).into_parts().1, base: low.clone(), - } + }) } #[inline] - fn new_inclusive(low_b: B1, high_b: B2) -> Self + fn new_inclusive(low_b: B1, high_b: B2) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { let low = low_b.borrow(); let high = high_b.borrow(); - assert!(low <= high); + if low > high { + return Err(Error::EmptyRange); + } Self::new(low, high + 1u32) } @@ -242,12 +259,21 @@ impl UniformSampler for UniformBigInt { } #[inline] - fn sample_single(low: B1, high: B2, rng: &mut R) -> Self::X + fn sample_single( + low_b: B1, + high_b: B2, + rng: &mut R, + ) -> Result where B1: SampleBorrow + Sized, B2: SampleBorrow + Sized, { - rng.gen_bigint_range(low.borrow(), high.borrow()) + let low = low_b.borrow(); + let high = high_b.borrow(); + if low >= high { + return Err(Error::EmptyRange); + } + Ok(rng.gen_bigint_range(low, high)) } } From aab6c04cf906fd552f499c663d0c7950a5200f89 Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 3 Mar 2025 13:55:28 -0600 Subject: [PATCH 4/5] Rename RandBigInt methods to align with rand 0.9 --- benches/bigint.rs | 36 ++++++++++++++-------------- benches/gcd.rs | 4 ++-- benches/roots.rs | 6 ++--- ci/big_rand/src/lib.rs | 32 ++++++++++++------------- ci/big_rand/src/torture.rs | 12 +++++----- src/bigrand.rs | 48 +++++++++++++++++++------------------- src/lib.rs | 4 ++-- 7 files changed, 71 insertions(+), 71 deletions(-) diff --git a/benches/bigint.rs b/benches/bigint.rs index 22795274..f2fedc9d 100644 --- a/benches/bigint.rs +++ b/benches/bigint.rs @@ -13,24 +13,24 @@ use rng::get_rng; fn multiply_bench(b: &mut Bencher, xbits: u64, ybits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(xbits); - let y = rng.gen_bigint(ybits); + let x = rng.random_bigint(xbits); + let y = rng.random_bigint(ybits); b.iter(|| &x * &y); } fn divide_bench(b: &mut Bencher, xbits: u64, ybits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(xbits); - let y = rng.gen_bigint(ybits); + let x = rng.random_bigint(xbits); + let y = rng.random_bigint(ybits); b.iter(|| &x / &y); } fn remainder_bench(b: &mut Bencher, xbits: u64, ybits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(xbits); - let y = rng.gen_bigint(ybits); + let x = rng.random_bigint(xbits); + let y = rng.random_bigint(ybits); b.iter(|| &x % &y); } @@ -186,7 +186,7 @@ fn fib_to_string(b: &mut Bencher) { fn to_str_radix_bench(b: &mut Bencher, radix: u32, bits: u64) { let mut rng = get_rng(); - let x = rng.gen_bigint(bits); + let x = rng.random_bigint(bits); b.iter(|| x.to_str_radix(radix)); } @@ -222,7 +222,7 @@ fn to_str_radix_36(b: &mut Bencher) { fn from_str_radix_bench(b: &mut Bencher, radix: u32) { let mut rng = get_rng(); - let x = rng.gen_bigint(1009); + let x = rng.random_bigint(1009); let s = x.to_str_radix(radix); assert_eq!(x, BigInt::from_str_radix(&s, radix).unwrap()); b.iter(|| BigInt::from_str_radix(&s, radix)); @@ -256,7 +256,7 @@ fn from_str_radix_36(b: &mut Bencher) { fn rand_bench(b: &mut Bencher, bits: u64) { let mut rng = get_rng(); - b.iter(|| rng.gen_bigint(bits)); + b.iter(|| rng.random_bigint(bits)); } #[bench] @@ -327,7 +327,7 @@ fn shr(b: &mut Bencher) { fn hash(b: &mut Bencher) { use std::collections::HashSet; let mut rng = get_rng(); - let v: Vec = (1000..2000).map(|bits| rng.gen_bigint(bits)).collect(); + let v: Vec = (1000..2000).map(|bits| rng.random_bigint(bits)).collect(); b.iter(|| { let h: HashSet<&BigInt> = v.iter().collect(); assert_eq!(h.len(), v.len()); @@ -399,8 +399,8 @@ const RFC3526_2048BIT_MODP_GROUP: &str = "\ #[bench] fn modpow(b: &mut Bencher) { let mut rng = get_rng(); - let base = rng.gen_biguint(2048); - let e = rng.gen_biguint(2048); + let base = rng.random_biguint(2048); + let e = rng.random_biguint(2048); let m = BigUint::from_str_radix(RFC3526_2048BIT_MODP_GROUP, 16).unwrap(); b.iter(|| base.modpow(&e, &m)); @@ -409,8 +409,8 @@ fn modpow(b: &mut Bencher) { #[bench] fn modpow_even(b: &mut Bencher) { let mut rng = get_rng(); - let base = rng.gen_biguint(2048); - let e = rng.gen_biguint(2048); + let base = rng.random_biguint(2048); + let e = rng.random_biguint(2048); // Make the modulus even, so monty (base-2^32) doesn't apply. let m = BigUint::from_str_radix(RFC3526_2048BIT_MODP_GROUP, 16).unwrap() - 1u32; @@ -420,7 +420,7 @@ fn modpow_even(b: &mut Bencher) { #[bench] fn to_u32_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.to_u32_digits()); } @@ -428,7 +428,7 @@ fn to_u32_digits(b: &mut Bencher) { #[bench] fn iter_u32_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.iter_u32_digits().max()); } @@ -436,7 +436,7 @@ fn iter_u32_digits(b: &mut Bencher) { #[bench] fn to_u64_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.to_u64_digits()); } @@ -444,7 +444,7 @@ fn to_u64_digits(b: &mut Bencher) { #[bench] fn iter_u64_digits(b: &mut Bencher) { let mut rng = get_rng(); - let n = rng.gen_biguint(2048); + let n = rng.random_biguint(2048); b.iter(|| n.iter_u64_digits().max()); } diff --git a/benches/gcd.rs b/benches/gcd.rs index c211b6ef..f015aa0c 100644 --- a/benches/gcd.rs +++ b/benches/gcd.rs @@ -13,8 +13,8 @@ use rng::get_rng; fn bench(b: &mut Bencher, bits: u64, gcd: fn(&BigUint, &BigUint) -> BigUint) { let mut rng = get_rng(); - let x = rng.gen_biguint(bits); - let y = rng.gen_biguint(bits); + let x = rng.random_biguint(bits); + let y = rng.random_biguint(bits); assert_eq!(euclid(&x, &y), x.gcd(&y)); diff --git a/benches/roots.rs b/benches/roots.rs index 7afc4f76..3c329b4b 100644 --- a/benches/roots.rs +++ b/benches/roots.rs @@ -37,7 +37,7 @@ fn check(x: &BigUint, n: u32) { } fn bench_sqrt(b: &mut Bencher, bits: u64) { - let x = get_rng().gen_biguint(bits); + let x = get_rng().random_biguint(bits); eprintln!("bench_sqrt({})", x); check(&x, 2); @@ -65,7 +65,7 @@ fn big4k_sqrt(b: &mut Bencher) { } fn bench_cbrt(b: &mut Bencher, bits: u64) { - let x = get_rng().gen_biguint(bits); + let x = get_rng().random_biguint(bits); eprintln!("bench_cbrt({})", x); check(&x, 3); @@ -93,7 +93,7 @@ fn big4k_cbrt(b: &mut Bencher) { } fn bench_nth_root(b: &mut Bencher, bits: u64, n: u32) { - let x = get_rng().gen_biguint(bits); + let x = get_rng().random_biguint(bits); eprintln!("bench_{}th_root({})", n, x); check(&x, n); diff --git a/ci/big_rand/src/lib.rs b/ci/big_rand/src/lib.rs index 4a932baf..e7edc497 100644 --- a/ci/big_rand/src/lib.rs +++ b/ci/big_rand/src/lib.rs @@ -18,9 +18,9 @@ mod biguint { #[test] fn test_rand() { let mut rng = rng(); - let n: BigUint = rng.gen_biguint(137); + let n: BigUint = rng.random_biguint(137); assert!(n.bits() <= 137); - assert!(rng.gen_biguint(0).is_zero()); + assert!(rng.random_biguint(0).is_zero()); } #[test] @@ -38,7 +38,7 @@ mod biguint { for _ in 0..10 { assert_eq!( - rng.gen_biguint_range(&BigUint::from(236u32), &BigUint::from(237u32)), + rng.random_biguint_range(&BigUint::from(236u32), &BigUint::from(237u32)), BigUint::from(236u32) ); } @@ -46,10 +46,10 @@ mod biguint { let l = BigUint::from(403469000u32 + 2352); let u = BigUint::from(403469000u32 + 3513); for _ in 0..1000 { - let n: BigUint = rng.gen_biguint_below(&u); + let n: BigUint = rng.random_biguint_below(&u); assert!(n < u); - let n: BigUint = rng.gen_biguint_range(&l, &u); + let n: BigUint = rng.random_biguint_range(&l, &u); assert!(n >= l); assert!(n < u); } @@ -58,7 +58,7 @@ mod biguint { #[test] #[should_panic] fn test_zero_rand_range() { - rng().gen_biguint_range(&BigUint::from(54u32), &BigUint::from(54u32)); + rng().random_biguint_range(&BigUint::from(54u32), &BigUint::from(54u32)); } #[test] @@ -68,7 +68,7 @@ mod biguint { let l = BigUint::from(2352u32); let u = BigUint::from(3513u32); // Switching u and l should fail: - let _n: BigUint = rng.gen_biguint_range(&u, &l); + let _n: BigUint = rng.random_biguint_range(&u, &l); } #[test] @@ -102,7 +102,7 @@ mod biguint { let mut rng = R::from_seed(seed); for (i, &s) in expected.iter().enumerate() { let n: BigUint = s.parse().unwrap(); - let r = rng.gen_biguint((1 << i) + i as u64); + let r = rng.random_biguint((1 << i) + i as u64); assert_eq!(n, r); } } @@ -194,7 +194,7 @@ mod biguint { let bit_range = Uniform::new(0, 2048).unwrap(); let sample_bits: Vec<_> = bit_range.sample_iter(&mut rng).take(100).collect(); for bits in sample_bits { - let x = rng.gen_biguint(bits); + let x = rng.random_biguint(bits); for n in 2..11 { check(x.clone(), n); } @@ -213,9 +213,9 @@ mod bigint { #[test] fn test_rand() { let mut rng = rng(); - let n: BigInt = rng.gen_bigint(137); + let n: BigInt = rng.random_bigint(137); assert!(n.bits() <= 137); - assert!(rng.gen_bigint(0).is_zero()); + assert!(rng.random_bigint(0).is_zero()); } #[test] @@ -233,7 +233,7 @@ mod bigint { for _ in 0..10 { assert_eq!( - rng.gen_bigint_range(&BigInt::from(236), &BigInt::from(237)), + rng.random_bigint_range(&BigInt::from(236), &BigInt::from(237)), BigInt::from(236) ); } @@ -241,7 +241,7 @@ mod bigint { fn check(l: BigInt, u: BigInt) { let mut rng = rand::rng(); for _ in 0..1000 { - let n: BigInt = rng.gen_bigint_range(&l, &u); + let n: BigInt = rng.random_bigint_range(&l, &u); assert!(n >= l); assert!(n < u); } @@ -256,7 +256,7 @@ mod bigint { #[test] #[should_panic] fn test_zero_rand_range() { - rng().gen_bigint_range(&BigInt::from(54), &BigInt::from(54)); + rng().random_bigint_range(&BigInt::from(54), &BigInt::from(54)); } #[test] @@ -266,7 +266,7 @@ mod bigint { let l = BigInt::from(2352); let u = BigInt::from(3513); // Switching u and l should fail: - let _n: BigInt = rng.gen_bigint_range(&u, &l); + let _n: BigInt = rng.random_bigint_range(&u, &l); } #[test] @@ -302,7 +302,7 @@ mod bigint { let mut rng = R::from_seed(seed); for (i, &s) in expected.iter().enumerate() { let n: BigInt = s.parse().unwrap(); - let r = rng.gen_bigint((1 << i) + i as u64); + let r = rng.random_bigint((1 << i) + i as u64); assert_eq!(n, r); } } diff --git a/ci/big_rand/src/torture.rs b/ci/big_rand/src/torture.rs index fb0d3f33..d89df12b 100644 --- a/ci/big_rand/src/torture.rs +++ b/ci/big_rand/src/torture.rs @@ -16,8 +16,8 @@ fn test_mul_divide_torture_count(count: usize) { let xbits = rng.random_range(0..bits_max); let ybits = rng.random_range(0..bits_max); - let x = rng.gen_biguint(xbits); - let y = rng.gen_biguint(ybits); + let x = rng.random_biguint(xbits); + let y = rng.random_biguint(ybits); if x.is_zero() || y.is_zero() { continue; @@ -45,10 +45,10 @@ fn test_factored_mul_torture_count(count: usize) { let mut rng = get_rng(); for _ in 0..count { - let w = rng.gen_biguint(bits); - let x = rng.gen_biguint(bits); - let y = rng.gen_biguint(bits); - let z = rng.gen_biguint(bits); + let w = rng.random_biguint(bits); + let x = rng.random_biguint(bits); + let y = rng.random_biguint(bits); + let z = rng.random_biguint(bits); let prod1 = (&w * &x) * (&y * &z); let prod2 = (&w * &y) * (&x * &z); diff --git a/src/bigrand.rs b/src/bigrand.rs index 8b29aa81..6734a135 100644 --- a/src/bigrand.rs +++ b/src/bigrand.rs @@ -19,24 +19,24 @@ use num_traits::{ToPrimitive, Zero}; /// The `rand` feature must be enabled to use this. See crate-level documentation for details. pub trait RandBigInt { /// Generate a random [`BigUint`] of the given bit size. - fn gen_biguint(&mut self, bit_size: u64) -> BigUint; + fn random_biguint(&mut self, bit_size: u64) -> BigUint; /// Generate a random [ BigInt`] of the given bit size. - fn gen_bigint(&mut self, bit_size: u64) -> BigInt; + fn random_bigint(&mut self, bit_size: u64) -> BigInt; /// Generate a random [`BigUint`] less than the given bound. Fails /// when the bound is zero. - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint; + fn random_biguint_below(&mut self, bound: &BigUint) -> BigUint; /// Generate a random [`BigUint`] within the given range. The lower /// bound is inclusive; the upper bound is exclusive. Fails when /// the upper bound is not greater than the lower bound. - fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; + fn random_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint; /// Generate a random [`BigInt`] within the given range. The lower /// bound is inclusive; the upper bound is exclusive. Fails when /// the upper bound is not greater than the lower bound. - fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt; + fn random_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt; } fn gen_bits(rng: &mut R, data: &mut [u32], rem: u64) { @@ -50,7 +50,7 @@ fn gen_bits(rng: &mut R, data: &mut [u32], rem: u64) { impl RandBigInt for R { cfg_digit!( - fn gen_biguint(&mut self, bit_size: u64) -> BigUint { + fn random_biguint(&mut self, bit_size: u64) -> BigUint { let (digits, rem) = bit_size.div_rem(&32); let len = (digits + (rem > 0) as u64) .to_usize() @@ -60,7 +60,7 @@ impl RandBigInt for R { biguint_from_vec(data) } - fn gen_biguint(&mut self, bit_size: u64) -> BigUint { + fn random_biguint(&mut self, bit_size: u64) -> BigUint { use core::slice; let (digits, rem) = bit_size.div_rem(&32); @@ -86,10 +86,10 @@ impl RandBigInt for R { } ); - fn gen_bigint(&mut self, bit_size: u64) -> BigInt { + fn random_bigint(&mut self, bit_size: u64) -> BigInt { loop { // Generate a random BigUint... - let biguint = self.gen_biguint(bit_size); + let biguint = self.random_biguint(bit_size); // ...and then randomly assign it a Sign... let sign = if biguint.is_zero() { // ...except that if the BigUint is zero, we need to try @@ -110,35 +110,35 @@ impl RandBigInt for R { } } - fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint { + fn random_biguint_below(&mut self, bound: &BigUint) -> BigUint { assert!(!bound.is_zero()); let bits = bound.bits(); loop { - let n = self.gen_biguint(bits); + let n = self.random_biguint(bits); if n < *bound { return n; } } } - fn gen_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint { + fn random_biguint_range(&mut self, lbound: &BigUint, ubound: &BigUint) -> BigUint { assert!(*lbound < *ubound); if lbound.is_zero() { - self.gen_biguint_below(ubound) + self.random_biguint_below(ubound) } else { - lbound + self.gen_biguint_below(&(ubound - lbound)) + lbound + self.random_biguint_below(&(ubound - lbound)) } } - fn gen_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt { + fn random_bigint_range(&mut self, lbound: &BigInt, ubound: &BigInt) -> BigInt { assert!(*lbound < *ubound); if lbound.is_zero() { - BigInt::from(self.gen_biguint_below(ubound.magnitude())) + BigInt::from(self.random_biguint_below(ubound.magnitude())) } else if ubound.is_zero() { - lbound + BigInt::from(self.gen_biguint_below(lbound.magnitude())) + lbound + BigInt::from(self.random_biguint_below(lbound.magnitude())) } else { let delta = ubound - lbound; - lbound + BigInt::from(self.gen_biguint_below(delta.magnitude())) + lbound + BigInt::from(self.random_biguint_below(delta.magnitude())) } } } @@ -186,7 +186,7 @@ impl UniformSampler for UniformBigUint { #[inline] fn sample(&self, rng: &mut R) -> Self::X { - &self.base + rng.gen_biguint_below(&self.len) + &self.base + rng.random_biguint_below(&self.len) } #[inline] @@ -204,7 +204,7 @@ impl UniformSampler for UniformBigUint { if low >= high { return Err(Error::EmptyRange); } - Ok(rng.gen_biguint_range(low, high)) + Ok(rng.random_biguint_range(low, high)) } } @@ -255,7 +255,7 @@ impl UniformSampler for UniformBigInt { #[inline] fn sample(&self, rng: &mut R) -> Self::X { - &self.base + BigInt::from(rng.gen_biguint_below(&self.len)) + &self.base + BigInt::from(rng.random_biguint_below(&self.len)) } #[inline] @@ -273,7 +273,7 @@ impl UniformSampler for UniformBigInt { if low >= high { return Err(Error::EmptyRange); } - Ok(rng.gen_bigint_range(low, high)) + Ok(rng.random_bigint_range(low, high)) } } @@ -299,13 +299,13 @@ impl RandomBits { impl Distribution for RandomBits { #[inline] fn sample(&self, rng: &mut R) -> BigUint { - rng.gen_biguint(self.bits) + rng.random_biguint(self.bits) } } impl Distribution for RandomBits { #[inline] fn sample(&self, rng: &mut R) -> BigInt { - rng.gen_bigint(self.bits) + rng.random_bigint(self.bits) } } diff --git a/src/lib.rs b/src/lib.rs index 50885d77..0ad6789b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,11 +46,11 @@ //! use num_bigint::{ToBigInt, RandBigInt}; //! //! let mut rng = rand::thread_rng(); -//! let a = rng.gen_bigint(1000); +//! let a = rng.random_bigint(1000); //! //! let low = -10000.to_bigint().unwrap(); //! let high = 10000.to_bigint().unwrap(); -//! let b = rng.gen_bigint_range(&low, &high); +//! let b = rng.random_bigint_range(&low, &high); //! //! // Probably an even larger number. //! println!("{}", a * b); From 9e323272ace0d7f22af0e0a440aaa008f29a4cda Mon Sep 17 00:00:00 2001 From: David Cook Date: Mon, 3 Mar 2025 14:00:24 -0600 Subject: [PATCH 5/5] Update comment --- src/bigrand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bigrand.rs b/src/bigrand.rs index 6734a135..9c624605 100644 --- a/src/bigrand.rs +++ b/src/bigrand.rs @@ -40,7 +40,7 @@ pub trait RandBigInt { } fn gen_bits(rng: &mut R, data: &mut [u32], rem: u64) { - // `fill` is faster than many `gen::` calls + // `fill` is faster than many `random::` calls rng.fill(data); if rem > 0 { let last = data.len() - 1;