Skip to content

Commit 3b9b943

Browse files
committed
Add serde framework
1 parent 06fb7d9 commit 3b9b943

File tree

5 files changed

+143
-45
lines changed

5 files changed

+143
-45
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ matrix:
2828
script:
2929
- cargo test
3030
- cargo test --tests --no-default-features
31+
- cargo test --features serde-1
3132
- cargo test --manifest-path rand-derive/Cargo.toml
3233

3334
env:

Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,26 @@ alloc = [] # enables Vec and Box support without std
2222

2323
i128_support = [] # enables i128 and u128 support
2424

25+
serde-1 = ["serde", "serde_derive"]
26+
27+
2528
[target.'cfg(unix)'.dependencies]
2629
libc = { version = "0.2", optional = true }
2730

2831
[target.'cfg(windows)'.dependencies]
2932
winapi = { version = "0.3", features = ["minwindef", "ntsecapi", "profileapi", "winnt"], optional = true }
3033

34+
[dependencies]
35+
serde = {version="1",optional=true}
36+
serde_derive = {version="1", optional=true}
37+
38+
[dev-dependencies]
39+
log = "0.3.0"
40+
# This is for testing serde, unfortunately
41+
# we can't specify feature-gated dev deps yet,
42+
# see: https://github.com/rust-lang/cargo/issues/1596
43+
bincode = "0.9"
44+
3145
[workspace]
3246
members = ["rand-derive"]
3347

appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ build: false
3434
test_script:
3535
- cargo test --benches
3636
- cargo test
37+
- cargo test --features serde-1
3738
- cargo test --features nightly
3839
- cargo test --tests --no-default-features --features=alloc
3940
- cargo test --manifest-path rand-derive/Cargo.toml

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@
249249

250250
#[cfg(feature="std")] extern crate std as core;
251251
#[cfg(all(feature = "alloc", not(feature="std")))] extern crate alloc;
252+
#[cfg(test)] #[macro_use] extern crate log;
253+
#[cfg(test)] #[cfg(feature="serde-1")] extern crate bincode;
254+
#[cfg(feature="serde-1")] extern crate serde;
255+
#[cfg(feature="serde-1")] #[macro_use] extern crate serde_derive;
252256

253257
use core::marker;
254258
use core::mem;

src/prng/chacha.rs

Lines changed: 123 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
//! The ChaCha random number generator.
1212
1313
use core::fmt;
14-
use {Rng, SeedableRng, Rand};
14+
use {Rand, Rng, SeedableRng};
1515
use impls;
1616

17-
const KEY_WORDS : usize = 8; // 8 words for the 256-bit key
18-
const STATE_WORDS : usize = 16;
17+
const KEY_WORDS: usize = 8; // 8 words for the 256-bit key
18+
const STATE_WORDS: usize = 16;
1919
const CHACHA_ROUNDS: u32 = 20; // Cryptographically secure from 8 upwards as of this writing
2020

2121
/// A random number generator that uses the ChaCha20 algorithm [1].
@@ -29,9 +29,9 @@ const CHACHA_ROUNDS: u32 = 20; // Cryptographically secure from 8 upwards as of
2929
/// Salsa20*](https://cr.yp.to/chacha.html)
3030
#[derive(Clone)]
3131
pub struct ChaChaRng {
32-
buffer: [u32; STATE_WORDS], // Internal buffer of output
33-
state: [u32; STATE_WORDS], // Initial state
34-
index: usize, // Index into state
32+
buffer: [u32; STATE_WORDS], // Internal buffer of output
33+
state: [u32; STATE_WORDS], // Initial state
34+
index: usize, // Index into state
3535
}
3636

3737
// Custom Debug implementation that does not expose the internal state
@@ -79,7 +79,6 @@ fn core(new: &mut [u32; STATE_WORDS], input: &[u32; STATE_WORDS]) {
7979
}
8080

8181
impl ChaChaRng {
82-
8382
/// Create an ChaCha random number generator using the default
8483
/// fixed key of 8 zero words.
8584
///
@@ -101,9 +100,9 @@ impl ChaChaRng {
101100
/// - 2419978656
102101
pub fn new_unseeded() -> ChaChaRng {
103102
let mut rng = ChaChaRng {
104-
buffer: [0; STATE_WORDS],
105-
state: [0; STATE_WORDS],
106-
index: STATE_WORDS
103+
buffer: [0; STATE_WORDS],
104+
state: [0; STATE_WORDS],
105+
index: STATE_WORDS,
107106
};
108107
rng.init(&[0; KEY_WORDS]);
109108
rng
@@ -129,9 +128,9 @@ impl ChaChaRng {
129128
/// println!("{:?}", ra.next_u32());
130129
/// ```
131130
pub fn set_counter(&mut self, counter_low: u64, counter_high: u64) {
132-
self.state[12] = (counter_low >> 0) as u32;
131+
self.state[12] = (counter_low >> 0) as u32;
133132
self.state[13] = (counter_low >> 32) as u32;
134-
self.state[14] = (counter_high >> 0) as u32;
133+
self.state[14] = (counter_high >> 0) as u32;
135134
self.state[15] = (counter_high >> 32) as u32;
136135
self.index = STATE_WORDS; // force recomputation
137136
}
@@ -161,7 +160,7 @@ impl ChaChaRng {
161160
self.state[3] = 0x6B206574;
162161

163162
for i in 0..KEY_WORDS {
164-
self.state[4+i] = key[i];
163+
self.state[4 + i] = key[i];
165164
}
166165

167166
self.state[12] = 0;
@@ -178,11 +177,17 @@ impl ChaChaRng {
178177
self.index = 0;
179178
// update 128-bit counter
180179
self.state[12] = self.state[12].wrapping_add(1);
181-
if self.state[12] != 0 { return };
180+
if self.state[12] != 0 {
181+
return;
182+
};
182183
self.state[13] = self.state[13].wrapping_add(1);
183-
if self.state[13] != 0 { return };
184+
if self.state[13] != 0 {
185+
return;
186+
};
184187
self.state[14] = self.state[14].wrapping_add(1);
185-
if self.state[14] != 0 { return };
188+
if self.state[14] != 0 {
189+
return;
190+
};
186191
self.state[15] = self.state[15].wrapping_add(1);
187192
}
188193
}
@@ -216,8 +221,7 @@ impl Rng for ChaChaRng {
216221
}
217222

218223
let (consumed_u32, filled_u8) =
219-
impls::fill_via_u32_chunks(&self.buffer[self.index..],
220-
&mut dest[read_len..]);
224+
impls::fill_via_u32_chunks(&self.buffer[self.index..], &mut dest[read_len..]);
221225

222226
self.index += consumed_u32;
223227
read_len += filled_u8;
@@ -226,7 +230,6 @@ impl Rng for ChaChaRng {
226230
}
227231

228232
impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
229-
230233
fn reseed(&mut self, seed: &'a [u32]) {
231234
*self = Self::from_seed(seed);
232235
}
@@ -237,14 +240,14 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
237240
/// words are used, the remaining are set to zero.
238241
fn from_seed(seed: &'a [u32]) -> ChaChaRng {
239242
let mut rng = ChaChaRng {
240-
buffer: [0; STATE_WORDS],
241-
state: [0; STATE_WORDS],
242-
index: STATE_WORDS
243+
buffer: [0; STATE_WORDS],
244+
state: [0; STATE_WORDS],
245+
index: STATE_WORDS,
243246
};
244247
rng.init(&[0u32; KEY_WORDS]);
245248
// set key in place
246249
{
247-
let key = &mut rng.state[4 .. 4+KEY_WORDS];
250+
let key = &mut rng.state[4..4 + KEY_WORDS];
248251
for (k, s) in key.iter_mut().zip(seed.iter()) {
249252
*k = *s;
250253
}
@@ -255,15 +258,14 @@ impl<'a> SeedableRng<&'a [u32]> for ChaChaRng {
255258

256259
impl Rand for ChaChaRng {
257260
fn rand<R: Rng>(other: &mut R) -> ChaChaRng {
258-
let mut key : [u32; KEY_WORDS] = [0; KEY_WORDS];
261+
let mut key: [u32; KEY_WORDS] = [0; KEY_WORDS];
259262
for word in key.iter_mut() {
260263
*word = other.gen();
261264
}
262265
SeedableRng::from_seed(&key[..])
263266
}
264267
}
265268

266-
267269
#[cfg(test)]
268270
mod test {
269271
use {Rng, SeedableRng};
@@ -289,22 +291,54 @@ mod test {
289291
let mut rng1: ChaChaRng = SeedableRng::from_seed(seed);
290292

291293
let mut results = [0u32; 16];
292-
for i in results.iter_mut() { *i = rng1.next_u32(); }
293-
let expected = [0xade0b876, 0x903df1a0, 0xe56a5d40, 0x28bd8653,
294-
0xb819d2bd, 0x1aed8da0, 0xccef36a8, 0xc70d778b,
295-
0x7c5941da, 0x8d485751, 0x3fe02477, 0x374ad8b8,
296-
0xf4b8436a, 0x1ca11815, 0x69b687c3, 0x8665eeb2];
294+
for i in results.iter_mut() {
295+
*i = rng1.next_u32();
296+
}
297+
let expected = [
298+
0xade0b876,
299+
0x903df1a0,
300+
0xe56a5d40,
301+
0x28bd8653,
302+
0xb819d2bd,
303+
0x1aed8da0,
304+
0xccef36a8,
305+
0xc70d778b,
306+
0x7c5941da,
307+
0x8d485751,
308+
0x3fe02477,
309+
0x374ad8b8,
310+
0xf4b8436a,
311+
0x1ca11815,
312+
0x69b687c3,
313+
0x8665eeb2,
314+
];
297315
assert_eq!(results, expected);
298316

299-
for i in results.iter_mut() { *i = rng1.next_u32(); }
300-
let expected = [0xbee7079f, 0x7a385155, 0x7c97ba98, 0x0d082d73,
301-
0xa0290fcb, 0x6965e348, 0x3e53c612, 0xed7aee32,
302-
0x7621b729, 0x434ee69c, 0xb03371d5, 0xd539d874,
303-
0x281fed31, 0x45fb0a51, 0x1f0ae1ac, 0x6f4d794b];
317+
for i in results.iter_mut() {
318+
*i = rng1.next_u32();
319+
}
320+
let expected = [
321+
0xbee7079f,
322+
0x7a385155,
323+
0x7c97ba98,
324+
0x0d082d73,
325+
0xa0290fcb,
326+
0x6965e348,
327+
0x3e53c612,
328+
0xed7aee32,
329+
0x7621b729,
330+
0x434ee69c,
331+
0xb03371d5,
332+
0xd539d874,
333+
0x281fed31,
334+
0x45fb0a51,
335+
0x1f0ae1ac,
336+
0x6f4d794b,
337+
];
304338
assert_eq!(results, expected);
305339

306340

307-
let seed: &[_] = &[0,1,2,3,4,5,6,7];
341+
let seed: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
308342
let mut rng2: ChaChaRng = SeedableRng::from_seed(seed);
309343

310344
// Store the 17*i-th 32-bit word,
@@ -315,10 +349,24 @@ mod test {
315349
rng2.next_u32();
316350
}
317351
}
318-
let expected = [0xf225c81a, 0x6ab1be57, 0x04d42951, 0x70858036,
319-
0x49884684, 0x64efec72, 0x4be2d186, 0x3615b384,
320-
0x11cfa18e, 0xd3c50049, 0x75c775f6, 0x434c6530,
321-
0x2c5bad8f, 0x898881dc, 0x5f1c86d9, 0xc1f8e7f4];
352+
let expected = [
353+
0xf225c81a,
354+
0x6ab1be57,
355+
0x04d42951,
356+
0x70858036,
357+
0x49884684,
358+
0x64efec72,
359+
0x4be2d186,
360+
0x3615b384,
361+
0x11cfa18e,
362+
0xd3c50049,
363+
0x75c775f6,
364+
0x434c6530,
365+
0x2c5bad8f,
366+
0x898881dc,
367+
0x5f1c86d9,
368+
0xc1f8e7f4,
369+
];
322370
assert_eq!(results, expected);
323371
}
324372

@@ -329,16 +377,46 @@ mod test {
329377
let mut results = [0u8; 32];
330378
rng.fill_bytes(&mut results);
331379
// Same as first values in test_isaac_true_values as bytes in LE order
332-
let expected = [118, 184, 224, 173, 160, 241, 61, 144,
333-
64, 93, 106, 229, 83, 134, 189, 40,
334-
189, 210, 25, 184, 160, 141, 237, 26,
335-
168, 54, 239, 204, 139, 119, 13, 199];
380+
let expected = [
381+
118,
382+
184,
383+
224,
384+
173,
385+
160,
386+
241,
387+
61,
388+
144,
389+
64,
390+
93,
391+
106,
392+
229,
393+
83,
394+
134,
395+
189,
396+
40,
397+
189,
398+
210,
399+
25,
400+
184,
401+
160,
402+
141,
403+
237,
404+
26,
405+
168,
406+
54,
407+
239,
408+
204,
409+
139,
410+
119,
411+
13,
412+
199,
413+
];
336414
assert_eq!(results, expected);
337415
}
338416

339417
#[test]
340418
fn test_chacha_clone() {
341-
let seed: &[_] = &[0,1,2,3,4,5,6,7];
419+
let seed: &[_] = &[0, 1, 2, 3, 4, 5, 6, 7];
342420
let mut rng: ChaChaRng = SeedableRng::from_seed(seed);
343421
let mut clone = rng.clone();
344422
for _ in 0..16 {

0 commit comments

Comments
 (0)