summaryrefslogtreecommitdiff
path: root/rand/rand_isaac/src
diff options
context:
space:
mode:
Diffstat (limited to 'rand/rand_isaac/src')
-rw-r--r--rand/rand_isaac/src/isaac.rs42
-rw-r--r--rand/rand_isaac/src/isaac64.rs49
-rw-r--r--rand/rand_isaac/src/isaac_array.rs10
-rw-r--r--rand/rand_isaac/src/lib.rs11
4 files changed, 40 insertions, 72 deletions
diff --git a/rand/rand_isaac/src/isaac.rs b/rand/rand_isaac/src/isaac.rs
index 2bfdd94..2caf61a 100644
--- a/rand/rand_isaac/src/isaac.rs
+++ b/rand/rand_isaac/src/isaac.rs
@@ -11,9 +11,10 @@
use core::{fmt, slice};
use core::num::Wrapping as w;
+#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
use rand_core::{RngCore, SeedableRng, Error, le};
use rand_core::block::{BlockRngCore, BlockRng};
-use isaac_array::IsaacArray;
+use crate::isaac_array::IsaacArray;
#[allow(non_camel_case_types)]
type w32 = w<u32>;
@@ -34,8 +35,8 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
/// In spite of being designed with cryptographic security in mind, ISAAC hasn't
/// been stringently cryptanalyzed and thus cryptographers do not not
/// consensually trust it to be secure. When looking for a secure RNG, prefer
-/// [`Hc128Rng`] instead, which, like ISAAC, is an array-based RNG and one of
-/// the stream-ciphers selected the by eSTREAM contest.
+/// `Hc128Rng` from the [`rand_hc`] crate instead, which, like ISAAC, is an
+/// array-based RNG and one of the stream-ciphers selected the by eSTREAM
///
/// In 2006 an improvement to ISAAC was suggested by Jean-Philippe Aumasson,
/// named ISAAC+[^3]. But because the specification is not complete, because
@@ -86,28 +87,28 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
/// [^3]: Jean-Philippe Aumasson, [*On the pseudo-random generator ISAAC*](
/// https://eprint.iacr.org/2006/438)
///
-/// [`Hc128Rng`]: ../../rand_hc/struct.Hc128Rng.html
-/// [`BlockRng`]: ../../rand_core/block/struct.BlockRng.html
-/// [`RngCore`]: ../../rand_core/trait.RngCore.html
+/// [`rand_hc`]: https://docs.rs/rand_hc
#[derive(Clone, Debug)]
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
pub struct IsaacRng(BlockRng<IsaacCore>);
impl RngCore for IsaacRng {
- #[inline(always)]
+ #[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}
- #[inline(always)]
+ #[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
+ #[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}
+ #[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
@@ -116,33 +117,26 @@ impl RngCore for IsaacRng {
impl SeedableRng for IsaacRng {
type Seed = <IsaacCore as SeedableRng>::Seed;
+ #[inline]
fn from_seed(seed: Self::Seed) -> Self {
IsaacRng(BlockRng::<IsaacCore>::from_seed(seed))
}
-
+
/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
+ #[inline]
fn seed_from_u64(seed: u64) -> Self {
IsaacRng(BlockRng::<IsaacCore>::seed_from_u64(seed))
}
+ #[inline]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
BlockRng::<IsaacCore>::from_rng(rng).map(|rng| IsaacRng(rng))
}
}
-impl IsaacRng {
- /// Create an ISAAC random number generator using an `u64` as seed.
- /// If `seed == 0` this will produce the same stream of random numbers as
- /// the reference implementation when used unseeded.
- #[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
- pub fn new_from_u64(seed: u64) -> Self {
- Self::seed_from_u64(seed)
- }
-}
-
-/// The core of `IsaacRng`, used with `BlockRng`.
+/// The core of [`IsaacRng`], used with [`BlockRng`].
#[derive(Clone)]
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
pub struct IsaacCore {
@@ -165,10 +159,10 @@ impl BlockRngCore for IsaacCore {
type Results = IsaacArray<Self::Item>;
/// Refills the output buffer, `results`. See also the pseudocode desciption
- /// of the algorithm in the [`IsaacRng`] documentation.
+ /// of the algorithm in the `IsaacRng` documentation.
///
/// Optimisations used (similar to the reference implementation):
- ///
+ ///
/// - The loop is unrolled 4 times, once for every constant of mix().
/// - The contents of the main loop are moved to a function `rngstep`, to
/// reduce code duplication.
@@ -183,8 +177,6 @@ impl BlockRngCore for IsaacCore {
/// from `results` in reverse. We read them in the normal direction, to
/// make `fill_bytes` a memcopy. To maintain compatibility we fill in
/// reverse.
- ///
- /// [`IsaacRng`]: struct.IsaacRng.html
fn generate(&mut self, results: &mut IsaacArray<Self::Item>) {
self.c += w(1);
// abbreviations
@@ -324,7 +316,7 @@ impl SeedableRng for IsaacCore {
}
Self::init(seed_extended, 2)
}
-
+
/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
diff --git a/rand/rand_isaac/src/isaac64.rs b/rand/rand_isaac/src/isaac64.rs
index 2712762..7d4b88c 100644
--- a/rand/rand_isaac/src/isaac64.rs
+++ b/rand/rand_isaac/src/isaac64.rs
@@ -11,9 +11,10 @@
use core::{fmt, slice};
use core::num::Wrapping as w;
+#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
use rand_core::{RngCore, SeedableRng, Error, le};
use rand_core::block::{BlockRngCore, BlockRng64};
-use isaac_array::IsaacArray;
+use crate::isaac_array::IsaacArray;
#[allow(non_camel_case_types)]
type w64 = w<u64>;
@@ -40,8 +41,8 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
/// In spite of being designed with cryptographic security in mind, ISAAC hasn't
/// been stringently cryptanalyzed and thus cryptographers do not not
/// consensually trust it to be secure. When looking for a secure RNG, prefer
-/// [`Hc128Rng`] instead, which, like ISAAC, is an array-based RNG and one of
-/// the stream-ciphers selected the by eSTREAM contest.
+/// `Hc128Rng` from the [`rand_hc`] crate instead, which, like ISAAC, is an
+/// array-based RNG and one of the stream-ciphers selected the by eSTREAM
///
/// ## Overview of the ISAAC-64 algorithm:
/// (in pseudo-code)
@@ -75,29 +76,30 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
/// [^1]: Bob Jenkins, [*ISAAC and RC4*](
/// http://burtleburtle.net/bob/rand/isaac.html)
///
-/// [`IsaacRng`]: ../isaac/struct.IsaacRng.html
-/// [`Hc128Rng`]: ../../rand_hc/struct.Hc128Rng.html
-/// [`BlockRng64`]: ../../rand_core/block/struct.BlockRng64.html
-/// [`RngCore`]: ../../rand_core/trait.RngCore.html
+/// [`IsaacRng`]: crate::isaac::IsaacRng
+/// [`rand_hc`]: https://docs.rs/rand_hc
+/// [`BlockRng64`]: rand_core::block::BlockRng64
#[derive(Clone, Debug)]
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
pub struct Isaac64Rng(BlockRng64<Isaac64Core>);
impl RngCore for Isaac64Rng {
- #[inline(always)]
+ #[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}
- #[inline(always)]
+ #[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
+ #[inline]
fn fill_bytes(&mut self, dest: &mut [u8]) {
self.0.fill_bytes(dest)
}
+ #[inline]
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Error> {
self.0.try_fill_bytes(dest)
}
@@ -106,6 +108,7 @@ impl RngCore for Isaac64Rng {
impl SeedableRng for Isaac64Rng {
type Seed = <Isaac64Core as SeedableRng>::Seed;
+ #[inline]
fn from_seed(seed: Self::Seed) -> Self {
Isaac64Rng(BlockRng64::<Isaac64Core>::from_seed(seed))
}
@@ -113,25 +116,17 @@ impl SeedableRng for Isaac64Rng {
/// Create an ISAAC random number generator using an `u64` as seed.
/// If `seed == 0` this will produce the same stream of random numbers as
/// the reference implementation when used unseeded.
+ #[inline]
fn seed_from_u64(seed: u64) -> Self {
Isaac64Rng(BlockRng64::<Isaac64Core>::seed_from_u64(seed))
}
+ #[inline]
fn from_rng<S: RngCore>(rng: S) -> Result<Self, Error> {
BlockRng64::<Isaac64Core>::from_rng(rng).map(|rng| Isaac64Rng(rng))
}
}
-impl Isaac64Rng {
- /// Create an ISAAC-64 random number generator using an `u64` as seed.
- /// If `seed == 0` this will produce the same stream of random numbers as
- /// the reference implementation when used unseeded.
- #[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
- pub fn new_from_u64(seed: u64) -> Self {
- Self::seed_from_u64(seed)
- }
-}
-
/// The core of `Isaac64Rng`, used with `BlockRng`.
#[derive(Clone)]
#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
@@ -155,10 +150,10 @@ impl BlockRngCore for Isaac64Core {
type Results = IsaacArray<Self::Item>;
/// Refills the output buffer, `results`. See also the pseudocode desciption
- /// of the algorithm in the [`Isaac64Rng`] documentation.
+ /// of the algorithm in the `Isaac64Rng` documentation.
///
/// Optimisations used (similar to the reference implementation):
- ///
+ ///
/// - The loop is unrolled 4 times, once for every constant of mix().
/// - The contents of the main loop are moved to a function `rngstep`, to
/// reduce code duplication.
@@ -173,8 +168,6 @@ impl BlockRngCore for Isaac64Core {
/// from `results` in reverse. We read them in the normal direction, to
/// make `fill_bytes` a memcopy. To maintain compatibility we fill in
/// reverse.
- ///
- /// [`Isaac64Rng`]: struct.Isaac64Rng.html
fn generate(&mut self, results: &mut IsaacArray<Self::Item>) {
self.c += w(1);
// abbreviations
@@ -274,14 +267,6 @@ impl Isaac64Core {
Self { mem, a: w(0), b: w(0), c: w(0) }
}
-
- /// Create an ISAAC-64 random number generator using an `u64` as seed.
- /// If `seed == 0` this will produce the same stream of random numbers as
- /// the reference implementation when used unseeded.
- #[deprecated(since="0.6.0", note="use SeedableRng::seed_from_u64 instead")]
- pub fn new_from_u64(seed: u64) -> Self {
- Self::seed_from_u64(seed)
- }
}
impl SeedableRng for Isaac64Core {
@@ -297,7 +282,7 @@ impl SeedableRng for Isaac64Core {
}
Self::init(seed_extended, 2)
}
-
+
fn seed_from_u64(seed: u64) -> Self {
let mut key = [w(0); RAND_SIZE];
key[0] = w(seed);
diff --git a/rand/rand_isaac/src/isaac_array.rs b/rand/rand_isaac/src/isaac_array.rs
index 0fa6147..cbe4a59 100644
--- a/rand/rand_isaac/src/isaac_array.rs
+++ b/rand/rand_isaac/src/isaac_array.rs
@@ -13,7 +13,7 @@
// implement `AsRef`, `Default`, `Serialize`, `Deserialize`, or any other
// traits for that matter.
-#[cfg(feature="serde1")] use serde::{Serialize, Deserialize};
+#[cfg(feature="serde")] use serde::{Serialize, Deserialize};
const RAND_SIZE_LEN: usize = 8;
const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
@@ -21,10 +21,10 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
#[derive(Copy, Clone)]
#[allow(missing_debug_implementations)]
-#[cfg_attr(feature="serde1", derive(Serialize, Deserialize))]
+#[cfg_attr(feature="serde", derive(Serialize, Deserialize))]
pub struct IsaacArray<T> {
- #[cfg_attr(feature="serde1",serde(with="isaac_array_serde"))]
- #[cfg_attr(feature="serde1", serde(bound(
+ #[cfg_attr(feature="serde",serde(with="isaac_array_serde"))]
+ #[cfg_attr(feature="serde", serde(bound(
serialize = "T: Serialize",
deserialize = "T: Deserialize<'de> + Copy + Default")))]
inner: [T; RAND_SIZE]
@@ -66,7 +66,7 @@ impl<T> ::core::default::Default for IsaacArray<T> where T: Copy + Default {
}
-#[cfg(feature="serde1")]
+#[cfg(feature="serde")]
pub(super) mod isaac_array_serde {
const RAND_SIZE_LEN: usize = 8;
const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
diff --git a/rand/rand_isaac/src/lib.rs b/rand/rand_isaac/src/lib.rs
index 285d631..84cdf21 100644
--- a/rand/rand_isaac/src/lib.rs
+++ b/rand/rand_isaac/src/lib.rs
@@ -16,16 +16,7 @@
#![deny(missing_debug_implementations)]
#![doc(test(attr(allow(unused_variables), deny(warnings))))]
-#![cfg_attr(not(all(feature="serde1", test)), no_std)]
-
-pub extern crate rand_core;
-
-#[cfg(feature="serde1")] extern crate serde;
-#[cfg(feature="serde1")] #[macro_use] extern crate serde_derive;
-
-// To test serialization we need bincode and the standard library
-#[cfg(all(feature="serde1", test))] extern crate bincode;
-#[cfg(all(feature="serde1", test))] extern crate std as core;
+#![cfg_attr(not(all(feature="serde", test)), no_std)]
pub mod isaac;
pub mod isaac64;