Skip to content

Commit

Permalink
Merge pull request rust-num#8 from dignifiedquire/updates
Browse files Browse the repository at this point in the history
Updates
  • Loading branch information
dignifiedquire authored Mar 26, 2019
2 parents 93fcffd + a0ba8ac commit a87d254
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 42 deletions.
12 changes: 8 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ harness = false
name = "bench_main"
required-features = ["prime"]

[dependencies]

[dependencies.smallvec]
version = "0.6.7"
default-features = false
Expand All @@ -37,15 +39,14 @@ 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"]

[dependencies.zeroize]
version = "0.5"
version = "0.6"
optional = true

[dependencies.serde]
Expand All @@ -63,13 +64,16 @@ 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"

[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", "rand/nightly"]
4 changes: 3 additions & 1 deletion src/algorithms/gcd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigUint>, b: Cow<BigUint>) -> (BigInt, BigInt, BigInt) {
Expand Down
18 changes: 9 additions & 9 deletions src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ pub enum Sign {
Plus,
}

#[cfg(feature = "zeroize")]
impl Zeroize for Sign {
fn zeroize(&mut self) {
// TODO: Figure out how to better clear the sign.
*self = Sign::NoSign;
}
}

impl Neg for Sign {
type Output = Sign;

Expand Down Expand Up @@ -118,20 +126,12 @@ impl<'de> serde::Deserialize<'de> for Sign {

/// A big signed integer type.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "zeroize", derive(Zeroize))]
pub struct BigInt {
pub(crate) sign: Sign,
pub(crate) data: BigUint,
}

#[cfg(feature = "zeroize")]
impl Zeroize for BigInt {
fn zeroize(&mut self) {
// TODO: Figure out how to better clear the sign.
self.sign = Sign::NoSign;
self.data.zeroize();
}
}

/// Return the magnitude of a `BigInt`.
///
/// This is in a private module, pseudo pub(crate)
Expand Down
70 changes: 55 additions & 15 deletions src/bigrand.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -128,18 +128,29 @@ impl UniformSampler for UniformBigUint {
type X = BigUint;

#[inline]
fn new(low: Self::X, high: Self::X) -> Self {
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + 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<B1, B2>(low_b: B1, high_b: B2) -> Self
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
Self::new(low_b, high_b.borrow() + 1u32)
}

#[inline]
Expand All @@ -148,8 +159,15 @@ impl UniformSampler for UniformBigUint {
}

#[inline]
fn sample_single<R: Rng + ?Sized>(low: Self::X, high: Self::X, rng: &mut R) -> Self::X {
rng.gen_biguint_range(&low, &high)
fn sample_single<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) -> Self::X
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
let low = low_b.borrow();
let high = high_b.borrow();

rng.gen_biguint_range(low, high)
}
}

Expand All @@ -168,16 +186,31 @@ impl UniformSampler for UniformBigInt {
type X = BigInt;

#[inline]
fn new(low: Self::X, high: Self::X) -> Self {
#[inline]
fn new<B1, B2>(low_b: B1, high_b: B2) -> Self
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + 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<B1, B2>(low_b: B1, high_b: B2) -> Self
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
let low = low_b.borrow();
let high = high_b.borrow();

assert!(low <= high);
Self::new(low, high + 1u32)
}
Expand All @@ -188,8 +221,15 @@ impl UniformSampler for UniformBigInt {
}

#[inline]
fn sample_single<R: Rng + ?Sized>(low: Self::X, high: Self::X, rng: &mut R) -> Self::X {
rng.gen_bigint_range(&low, &high)
fn sample_single<R: Rng + ?Sized, B1, B2>(low_b: B1, high_b: B2, rng: &mut R) -> Self::X
where
B1: SampleBorrow<Self::X> + Sized,
B2: SampleBorrow<Self::X> + Sized,
{
let low = low_b.borrow();
let high = high_b.borrow();

rng.gen_bigint_range(low, high)
}
}

Expand Down
8 changes: 1 addition & 7 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,11 @@ use UsizePromotion;

/// A big unsigned integer type.
#[derive(Clone, Debug)]
#[cfg_attr(feature = "zeroize", derive(Zeroize))]
pub struct BigUint {
pub(crate) data: SmallVec<[BigDigit; VEC_SIZE]>,
}

#[cfg(feature = "zeroize")]
impl Zeroize for BigUint {
fn zeroize(&mut self) {
self.data.zeroize();
}
}

impl PartialEq for BigUint {
#[inline]
fn eq(&self, other: &BigUint) -> bool {
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 9 additions & 6 deletions tests/rand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -135,7 +138,7 @@ mod biguint {

#[test]
fn test_chacha_value_stability() {
use rand::prng::ChaChaRng;
use rand_chacha::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED_CHACHA);
}

Expand Down Expand Up @@ -170,7 +173,7 @@ mod biguint {
];
#[test]
fn test_isaac_value_stability() {
use rand::prng::IsaacRng;
use rand_isaac::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED_ISAAC);
}

Expand Down Expand Up @@ -204,7 +207,7 @@ mod biguint {

#[test]
fn test_xorshift_value_stability() {
use rand::prng::XorShiftRng;
use rand_xorshift::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED_XOR);
}
}
Expand Down Expand Up @@ -344,7 +347,7 @@ mod bigint {

#[test]
fn test_chacha_value_stability() {
use rand::prng::ChaChaRng;
use rand_chacha::ChaChaRng;
seeded_value_stability::<ChaChaRng>(EXPECTED_CHACHA);
}

Expand Down Expand Up @@ -379,7 +382,7 @@ mod bigint {

#[test]
fn test_isaac_value_stability() {
use rand::prng::IsaacRng;
use rand_isaac::IsaacRng;
seeded_value_stability::<IsaacRng>(EXPECTED_ISAAC);
}

Expand Down Expand Up @@ -415,7 +418,7 @@ mod bigint {

#[test]
fn test_xorshift_value_stability() {
use rand::prng::XorShiftRng;
use rand_xorshift::XorShiftRng;
seeded_value_stability::<XorShiftRng>(EXPECTED_XOR);
}
}
Expand Down

0 comments on commit a87d254

Please sign in to comment.