From af35fd30355b6ed8a4e5b757680a8db697a7b2ca Mon Sep 17 00:00:00 2001 From: TheIronBorn <> Date: Wed, 30 May 2018 21:57:38 -0700 Subject: [PATCH] switch gen_biguint to fill_bytes --- src/bigrand.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/bigrand.rs b/src/bigrand.rs index 610fdb32..077d7447 100644 --- a/src/bigrand.rs +++ b/src/bigrand.rs @@ -2,6 +2,7 @@ use rand::prelude::*; use rand::distributions::uniform::{SampleUniform, UniformSampler}; +use rand::AsByteSliceMut; use BigInt; use BigUint; @@ -39,13 +40,15 @@ impl RandBigInt for R { fn gen_biguint(&mut self, bit_size: usize) -> BigUint { use super::big_digit::BITS; let (digits, rem) = bit_size.div_rem(&BITS); - let mut data = Vec::with_capacity(digits + 1); - for _ in 0..digits { - data.push(self.gen()); - } + let mut data = vec![BigDigit::default(); digits + (rem > 0) as usize]; + // `fill_bytes` is faster than many `gen::` calls + self.fill_bytes(data[..].as_byte_slice_mut()); + // Swap bytes per the `Rng::fill` source. This might be + // unnecessary if reproducibility across architectures is not + // desired. + data.to_le(); if rem > 0 { - let final_digit: BigDigit = self.gen(); - data.push(final_digit >> (BITS - rem)); + data[digits] >>= BITS - rem; } BigUint::new(data) }