From ca8c31e09737b6d8e8b899bed9fa208e4b1ce229 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 26 Mar 2019 14:40:53 +0100 Subject: [PATCH] feat(deps): upgrade to rand@0.6 --- Cargo.toml | 11 ++++--- src/algorithms/gcd.rs | 4 ++- src/bigrand.rs | 70 +++++++++++++++++++++++++++++++++---------- src/lib.rs | 6 ++++ tests/rand.rs | 15 ++++++---- 5 files changed, 80 insertions(+), 26 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d956117..53729a6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,8 @@ harness = false name = "bench_main" required-features = ["prime"] +[dependencies] + [dependencies.smallvec] version = "0.6.7" default-features = false @@ -37,10 +39,9 @@ default-features = false version = "0.1.37" default-features = false - [dependencies.rand] optional = true -version = "0.5" +version = "0.6" default-features = false features = ["std"] @@ -63,6 +64,8 @@ version = "1.2.7" [dev-dependencies] criterion = "0.2" rand_chacha = "0.1" +rand_xorshift = "0.1" +rand_isaac = "0.1" [dev-dependencies.serde_test] version = "1.0" @@ -70,7 +73,7 @@ version = "1.0" [features] default = ["std", "i128", "u64_digit"] i128 = ["num-integer/i128", "num-traits/i128"] -std = ["num-integer/std", "num-traits/std", "smallvec/std"] +std = ["num-integer/std", "num-traits/std", "smallvec/std", "rand/std"] u64_digit = [] prime = ["rand"] -nightly = ["zeroize/nightly"] \ No newline at end of file +nightly = ["zeroize/nightly", "rand/nightly"] diff --git a/src/algorithms/gcd.rs b/src/algorithms/gcd.rs index e266580..ec34d23 100644 --- a/src/algorithms/gcd.rs +++ b/src/algorithms/gcd.rs @@ -325,7 +325,9 @@ mod tests { #[cfg(feature = "rand")] use num_traits::{One, Zero}; #[cfg(feature = "rand")] - use rand::{SeedableRng, XorShiftRng}; + use rand::SeedableRng; + #[cfg(feature = "rand")] + use rand_xorshift::XorShiftRng; #[cfg(feature = "rand")] fn extended_gcd_euclid(a: Cow, b: Cow) -> (BigInt, BigInt, BigInt) { diff --git a/src/bigrand.rs b/src/bigrand.rs index 03e1d84..f4b9aa7 100644 --- a/src/bigrand.rs +++ b/src/bigrand.rs @@ -1,6 +1,6 @@ //! Randomization of big integers -use rand::distributions::uniform::{SampleUniform, UniformSampler}; +use rand::distributions::uniform::{SampleBorrow, SampleUniform, UniformSampler}; use rand::prelude::*; use rand::AsByteSliceMut; use rand::Rng; @@ -128,18 +128,29 @@ impl UniformSampler for UniformBigUint { type X = BigUint; #[inline] - fn new(low: Self::X, high: Self::X) -> Self { + fn new(low_b: B1, high_b: B2) -> Self + where + B1: SampleBorrow + Sized, + B2: SampleBorrow + Sized, + { + let low = low_b.borrow(); + let high = high_b.borrow(); + assert!(low < high); + UniformBigUint { - len: high - &low, - base: low, + len: high - low, + base: low.clone(), } } #[inline] - fn new_inclusive(low: Self::X, high: Self::X) -> Self { - assert!(low <= high); - Self::new(low, high + 1u32) + fn new_inclusive(low_b: B1, high_b: B2) -> Self + where + B1: SampleBorrow + Sized, + B2: SampleBorrow + Sized, + { + Self::new(low_b, high_b.borrow() + 1u32) } #[inline] @@ -148,8 +159,15 @@ impl UniformSampler for UniformBigUint { } #[inline] - fn sample_single(low: Self::X, high: Self::X, rng: &mut R) -> Self::X { - rng.gen_biguint_range(&low, &high) + fn sample_single(low_b: B1, high_b: B2, rng: &mut R) -> Self::X + where + B1: SampleBorrow + Sized, + B2: SampleBorrow + Sized, + { + let low = low_b.borrow(); + let high = high_b.borrow(); + + rng.gen_biguint_range(low, high) } } @@ -168,16 +186,31 @@ impl UniformSampler for UniformBigInt { type X = BigInt; #[inline] - fn new(low: Self::X, high: Self::X) -> Self { + #[inline] + fn new(low_b: B1, high_b: B2) -> Self + where + B1: SampleBorrow + Sized, + B2: SampleBorrow + Sized, + { + let low = low_b.borrow(); + let high = high_b.borrow(); + assert!(low < high); UniformBigInt { - len: into_magnitude(high - &low), - base: low, + len: into_magnitude(high - low), + base: low.clone(), } } #[inline] - fn new_inclusive(low: Self::X, high: Self::X) -> Self { + fn new_inclusive(low_b: B1, high_b: B2) -> Self + where + B1: SampleBorrow + Sized, + B2: SampleBorrow + Sized, + { + let low = low_b.borrow(); + let high = high_b.borrow(); + assert!(low <= high); Self::new(low, high + 1u32) } @@ -188,8 +221,15 @@ impl UniformSampler for UniformBigInt { } #[inline] - fn sample_single(low: Self::X, high: Self::X, rng: &mut R) -> Self::X { - rng.gen_bigint_range(&low, &high) + fn sample_single(low_b: B1, high_b: B2, rng: &mut R) -> Self::X + where + B1: SampleBorrow + Sized, + B2: SampleBorrow + Sized, + { + let low = low_b.borrow(); + let high = high_b.borrow(); + + rng.gen_bigint_range(low, high) } } diff --git a/src/lib.rs b/src/lib.rs index e23b3ad..38d544d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,6 +86,12 @@ #[cfg(feature = "rand")] extern crate rand; +#[cfg(all(test, feature = "rand"))] +extern crate rand_chacha; +#[cfg(all(test, feature = "rand"))] +extern crate rand_isaac; +#[cfg(all(test, feature = "rand"))] +extern crate rand_xorshift; #[cfg(feature = "serde")] extern crate serde; diff --git a/tests/rand.rs b/tests/rand.rs index 3ea34e1..17fb83a 100644 --- a/tests/rand.rs +++ b/tests/rand.rs @@ -3,6 +3,9 @@ extern crate num_bigint_dig as num_bigint; extern crate num_traits; extern crate rand; +extern crate rand_chacha; +extern crate rand_isaac; +extern crate rand_xorshift; mod biguint { use num_bigint::{BigUint, RandBigInt, RandomBits}; @@ -135,7 +138,7 @@ mod biguint { #[test] fn test_chacha_value_stability() { - use rand::prng::ChaChaRng; + use rand_chacha::ChaChaRng; seeded_value_stability::(EXPECTED_CHACHA); } @@ -170,7 +173,7 @@ mod biguint { ]; #[test] fn test_isaac_value_stability() { - use rand::prng::IsaacRng; + use rand_isaac::IsaacRng; seeded_value_stability::(EXPECTED_ISAAC); } @@ -204,7 +207,7 @@ mod biguint { #[test] fn test_xorshift_value_stability() { - use rand::prng::XorShiftRng; + use rand_xorshift::XorShiftRng; seeded_value_stability::(EXPECTED_XOR); } } @@ -344,7 +347,7 @@ mod bigint { #[test] fn test_chacha_value_stability() { - use rand::prng::ChaChaRng; + use rand_chacha::ChaChaRng; seeded_value_stability::(EXPECTED_CHACHA); } @@ -379,7 +382,7 @@ mod bigint { #[test] fn test_isaac_value_stability() { - use rand::prng::IsaacRng; + use rand_isaac::IsaacRng; seeded_value_stability::(EXPECTED_ISAAC); } @@ -415,7 +418,7 @@ mod bigint { #[test] fn test_xorshift_value_stability() { - use rand::prng::XorShiftRng; + use rand_xorshift::XorShiftRng; seeded_value_stability::(EXPECTED_XOR); } }