Skip to content

Commit

Permalink
Isaac Serde
Browse files Browse the repository at this point in the history
  • Loading branch information
UserAB1236872 committed Jan 22, 2018
1 parent 73ec0f7 commit 44455f0
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/prng/isaac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
///
/// [3]: Jean-Philippe Aumasson, [*On the pseudo-random generator ISAAC*](
/// https://eprint.iacr.org/2006/438)
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct IsaacRng {
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
rsl: [u32; RAND_SIZE],
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
mem: [w32; RAND_SIZE],
a: w32,
b: w32,
Expand Down Expand Up @@ -465,4 +468,38 @@ mod test {
assert_eq!(rng.next_u64(), clone.next_u64());
}
}

#[test]
#[cfg(feature="serde-1")]
fn test_rng_serde() {
use bincode;
use std::io::{BufWriter, BufReader};

let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng: IsaacRng = SeedableRng::from_seed(seed);

let buf: Vec<u8> = Vec::new();
let mut buf = BufWriter::new(buf);
bincode::serialize_into(&mut buf, &rng, bincode::Infinite).expect("Could not serialize");

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: IsaacRng = bincode::deserialize_from(&mut read, bincode::Infinite).expect("Could not deserialize");

assert_eq!(rng.index, deserialized.index);
/* Can't assert directly because of the array size */
for (orig,deser) in rng.rsl.iter().zip(deserialized.rsl.iter()) {
assert_eq!(orig, deser);
}
for (orig,deser) in rng.mem.iter().zip(deserialized.mem.iter()) {
assert_eq!(orig, deser);
}
assert_eq!(rng.a, deserialized.a);
assert_eq!(rng.b, deserialized.b);
assert_eq!(rng.c, deserialized.c);

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
}
}
}
38 changes: 38 additions & 0 deletions src/prng/isaac64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;
///
/// [1]: Bob Jenkins, [*ISAAC and RC4*](
/// http://burtleburtle.net/bob/rand/isaac.html)
#[cfg_attr(feature="serde-1", derive(Serialize,Deserialize))]
pub struct Isaac64Rng {
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
rsl: [u64; RAND_SIZE],
#[cfg_attr(feature="serde-1",serde(with="super::isaac_serde::rand_size_serde"))]
mem: [w64; RAND_SIZE],
a: w64,
b: w64,
Expand Down Expand Up @@ -473,4 +476,39 @@ mod test {
assert_eq!(rng.next_u64(), clone.next_u64());
}
}

#[test]
#[cfg(feature="serde-1")]
fn test_rng_serde() {
use bincode;
use std::io::{BufWriter, BufReader};

let seed: &[_] = &[1, 23, 456, 7890, 12345];
let mut rng: Isaac64Rng = SeedableRng::from_seed(seed);

let buf: Vec<u8> = Vec::new();
let mut buf = BufWriter::new(buf);
bincode::serialize_into(&mut buf, &rng, bincode::Infinite).expect("Could not serialize");

let buf = buf.into_inner().unwrap();
let mut read = BufReader::new(&buf[..]);
let mut deserialized: Isaac64Rng = bincode::deserialize_from(&mut read, bincode::Infinite).expect("Could not deserialize");

assert_eq!(rng.index, deserialized.index);
assert_eq!(rng.half_used, deserialized.half_used);
/* Can't assert directly because of the array size */
for (orig,deser) in rng.rsl.iter().zip(deserialized.rsl.iter()) {
assert_eq!(orig, deser);
}
for (orig,deser) in rng.mem.iter().zip(deserialized.mem.iter()) {
assert_eq!(orig, deser);
}
assert_eq!(rng.a, deserialized.a);
assert_eq!(rng.b, deserialized.b);
assert_eq!(rng.c, deserialized.c);

for _ in 0..16 {
assert_eq!(rng.next_u64(), deserialized.next_u64());
}
}
}
68 changes: 68 additions & 0 deletions src/prng/isaac_serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#[cfg(feature="serde-1")]
pub(super) mod rand_size_serde {
const RAND_SIZE_LEN: usize = 8;
const RAND_SIZE: usize = 1 << RAND_SIZE_LEN;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Visitor,SeqAccess};
use serde::de;

use std::fmt;

pub fn serialize<T, S>(arr: &[T;RAND_SIZE], ser: S) -> Result<S::Ok, S::Error>
where
T: Serialize,
S: Serializer
{
use serde::ser::SerializeTuple;

let mut seq = ser.serialize_tuple(RAND_SIZE)?;

for e in arr.iter() {
seq.serialize_element(&e)?;
}

seq.end()
}

#[inline]
pub fn deserialize<'de, T, D>(de: D) -> Result<[T;RAND_SIZE], D::Error>
where
T: Deserialize<'de>+Default+Copy,
D: Deserializer<'de>,
{
use std::marker::PhantomData;
struct ArrayVisitor<T> {
_pd: PhantomData<T>,
};
impl<'de,T> Visitor<'de> for ArrayVisitor<T>
where
T: Deserialize<'de>+Default+Copy
{
type Value = [T; RAND_SIZE];

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("Isaac state array")
}

#[inline]
fn visit_seq<A>(self, mut seq: A) -> Result<[T; RAND_SIZE], A::Error>
where
A: SeqAccess<'de>,
{
let mut out = [Default::default();RAND_SIZE];

for i in 0..RAND_SIZE {
match seq.next_element()? {
Some(val) => out[i] = val,
None => return Err(de::Error::invalid_length(i, &self)),
};
}

Ok(out)
}
}

de.deserialize_tuple(RAND_SIZE, ArrayVisitor{_pd: PhantomData})
}
}
5 changes: 4 additions & 1 deletion src/prng/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ mod isaac;
mod isaac64;
mod xorshift;

#[cfg(feature="serde-1")]
mod isaac_serde;

pub use self::chacha::ChaChaRng;
pub use self::hc128::Hc128Rng;
pub use self::isaac::IsaacRng;
pub use self::isaac64::Isaac64Rng;
pub use self::xorshift::XorShiftRng;
pub use self::xorshift::XorShiftRng;

0 comments on commit 44455f0

Please sign in to comment.