mod-rand 0.9.3

Tiered random number generation for Rust. Fast PRNG, process-unique seeds, and OS-backed cryptographic random in one zero-dependency library. Pick the tier appropriate to your threat model.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//! # Tier 1 — Fast deterministic PRNG
//!
//! Pseudorandom number generator suitable for simulation, test
//! fixtures, and non-security shuffling. Seedable so runs are
//! reproducible. **Not cryptographically secure** — outputs are
//! predictable given the seed.
//!
//! Algorithm: xoshiro256\*\* by Blackman & Vigna. State expansion from
//! a single `u64` is performed with splitmix64. See
//! <https://prng.di.unimi.it/xoshiro256starstar.c> for the canonical
//! reference.
//!
//! Performance: ~1ns per `u64` on x86_64 (single rotation, multiply,
//! and a handful of xors per output).
//!
//! Available in `no_std`.

/// Golden-ratio increment used by splitmix64.
const SPLITMIX_GAMMA: u64 = 0x9E37_79B9_7F4A_7C15;

/// Polynomial-jump constants for `jump()` — equivalent to 2^128 calls
/// to `next_u64()`. Sourced verbatim from the canonical reference.
const JUMP: [u64; 4] = [
    0x180E_C6D3_3CFD_0ABA,
    0xD5A6_1266_F0C9_392C,
    0xA958_2618_E03F_C9AA,
    0x39AB_DC45_29B1_661C,
];

/// Polynomial-jump constants for `long_jump()` — equivalent to 2^192
/// calls to `next_u64()`. Sourced verbatim from the canonical reference.
const LONG_JUMP: [u64; 4] = [
    0x76E1_5D3E_FEFD_CBBF,
    0xC500_4E44_1C52_2FB3,
    0x7771_0069_854E_E241,
    0x3910_9BB0_2ACB_E635,
];

/// xoshiro256\*\* — fast, statistically sound, deterministic PRNG.
///
/// 256 bits of state. Period of `2^256 - 1`. Passes the BigCrush test
/// battery. Seedable from a single `u64` via splitmix64 expansion.
///
/// # Example
///
/// ```
/// use mod_rand::tier1::Xoshiro256;
///
/// let mut rng = Xoshiro256::seed_from_u64(42);
/// let n: u64 = rng.next_u64();
/// # let _ = n;
/// ```
///
/// # Reproducibility
///
/// The output stream is fully determined by the seed. Two generators
/// constructed with the same seed produce identical streams forever:
///
/// ```
/// use mod_rand::tier1::Xoshiro256;
///
/// let mut a = Xoshiro256::seed_from_u64(2026);
/// let mut b = Xoshiro256::seed_from_u64(2026);
/// for _ in 0..1024 {
///     assert_eq!(a.next_u64(), b.next_u64());
/// }
/// ```
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Xoshiro256 {
    state: [u64; 4],
}

impl Xoshiro256 {
    /// Construct a generator from a single 64-bit seed.
    ///
    /// The seed is expanded to 256 bits of internal state by running
    /// four rounds of splitmix64 — the seeding strategy recommended by
    /// the xoshiro authors. Any seed value, including zero, is
    /// accepted; splitmix64 cannot produce the all-zero state from a
    /// nonzero counter, and the counter starts at `seed + GAMMA`.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(0);
    /// let _ = rng.next_u64();
    /// ```
    #[inline]
    pub fn seed_from_u64(seed: u64) -> Self {
        let mut x = seed;
        Self {
            state: [
                splitmix64(&mut x),
                splitmix64(&mut x),
                splitmix64(&mut x),
                splitmix64(&mut x),
            ],
        }
    }

    /// Construct a generator directly from a 256-bit state.
    ///
    /// The state MUST NOT be all zero — that is the single fixed point
    /// of the xoshiro256\*\* transition. This constructor returns
    /// `None` in that case.
    ///
    /// Prefer [`seed_from_u64`](Self::seed_from_u64) for most uses;
    /// this exists for cases where you have a pre-derived 256-bit seed
    /// (e.g., from a key-derivation function).
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let rng = Xoshiro256::from_state([1, 2, 3, 4]).unwrap();
    /// # let _ = rng;
    /// assert!(Xoshiro256::from_state([0; 4]).is_none());
    /// ```
    #[inline]
    pub fn from_state(state: [u64; 4]) -> Option<Self> {
        if state == [0; 4] {
            None
        } else {
            Some(Self { state })
        }
    }

    /// Return the current internal state.
    ///
    /// Useful for checkpointing a stream so it can be resumed later
    /// via [`from_state`](Self::from_state).
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(7);
    /// let _ = rng.next_u64();
    /// let snapshot = rng.state();
    /// let resumed = Xoshiro256::from_state(snapshot).unwrap();
    /// assert_eq!(rng, resumed);
    /// ```
    #[inline]
    pub fn state(&self) -> [u64; 4] {
        self.state
    }

    /// Produce the next 64-bit output.
    ///
    /// One call performs a single rotation, two multiplies, a shift,
    /// six xors, and one further rotation — about a nanosecond on
    /// modern x86_64.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n = rng.next_u64();
    /// # let _ = n;
    /// ```
    #[inline]
    pub fn next_u64(&mut self) -> u64 {
        // result = rotl(s[1] * 5, 7) * 9
        let result = self.state[1].wrapping_mul(5).rotate_left(7).wrapping_mul(9);

        let t = self.state[1] << 17;

        self.state[2] ^= self.state[0];
        self.state[3] ^= self.state[1];
        self.state[1] ^= self.state[2];
        self.state[0] ^= self.state[3];
        self.state[2] ^= t;
        self.state[3] = self.state[3].rotate_left(45);

        result
    }

    /// Produce the next 32-bit output by discarding the low half of a
    /// 64-bit draw.
    ///
    /// The high bits of xoshiro256\*\* are slightly stronger than the
    /// low bits — taking the high 32 is the recommended way to obtain
    /// a 32-bit value.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let n: u32 = rng.next_u32();
    /// # let _ = n;
    /// ```
    #[inline]
    pub fn next_u32(&mut self) -> u32 {
        (self.next_u64() >> 32) as u32
    }

    /// Produce a value uniformly in `[0.0, 1.0)`.
    ///
    /// Uses the upper 53 bits of a `u64` output — the standard way to
    /// obtain a uniform double-precision draw without modulo bias.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let f = rng.next_f64();
    /// assert!((0.0..1.0).contains(&f));
    /// ```
    #[inline]
    pub fn next_f64(&mut self) -> f64 {
        // 53 bits of mantissa precision. 1.0 / 2^53.
        (self.next_u64() >> 11) as f64 * (1.0 / ((1u64 << 53) as f64))
    }

    /// Fill the entire buffer with PRNG output.
    ///
    /// Equivalent to repeatedly calling [`next_u64`](Self::next_u64)
    /// and writing the little-endian bytes, but avoids the overhead of
    /// per-byte calls. The final partial chunk (1..7 bytes) is filled
    /// from a single fresh `u64`.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut rng = Xoshiro256::seed_from_u64(1);
    /// let mut buf = [0u8; 32];
    /// rng.fill_bytes(&mut buf);
    /// ```
    pub fn fill_bytes(&mut self, buf: &mut [u8]) {
        let mut chunks = buf.chunks_exact_mut(8);
        for chunk in &mut chunks {
            let bytes = self.next_u64().to_le_bytes();
            chunk.copy_from_slice(&bytes);
        }
        let tail = chunks.into_remainder();
        if !tail.is_empty() {
            let bytes = self.next_u64().to_le_bytes();
            tail.copy_from_slice(&bytes[..tail.len()]);
        }
    }

    /// Advance the state by 2^128 calls to [`next_u64`](Self::next_u64).
    ///
    /// Use this to obtain non-overlapping parallel streams: clone the
    /// generator, call `jump()` on the clone, and the clone now
    /// produces a stream that will not collide with the original for
    /// 2^128 outputs.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut a = Xoshiro256::seed_from_u64(1);
    /// let mut b = a.clone();
    /// b.jump();
    /// assert_ne!(a.next_u64(), b.next_u64());
    /// ```
    pub fn jump(&mut self) {
        self.apply_jump(&JUMP);
    }

    /// Advance the state by 2^192 calls to [`next_u64`](Self::next_u64).
    ///
    /// Use this to partition the period into 2^64 non-overlapping
    /// streams, each itself capable of 2^128 further [`jump`](Self::jump)
    /// substreams.
    ///
    /// # Example
    ///
    /// ```
    /// use mod_rand::tier1::Xoshiro256;
    ///
    /// let mut a = Xoshiro256::seed_from_u64(1);
    /// let mut b = a.clone();
    /// b.long_jump();
    /// assert_ne!(a.next_u64(), b.next_u64());
    /// ```
    pub fn long_jump(&mut self) {
        self.apply_jump(&LONG_JUMP);
    }

    fn apply_jump(&mut self, constants: &[u64; 4]) {
        let mut s0 = 0u64;
        let mut s1 = 0u64;
        let mut s2 = 0u64;
        let mut s3 = 0u64;

        for &word in constants {
            for b in 0..64 {
                if (word & (1u64 << b)) != 0 {
                    s0 ^= self.state[0];
                    s1 ^= self.state[1];
                    s2 ^= self.state[2];
                    s3 ^= self.state[3];
                }
                let _ = self.next_u64();
            }
        }

        self.state[0] = s0;
        self.state[1] = s1;
        self.state[2] = s2;
        self.state[3] = s3;
    }
}

/// splitmix64 — fast, full-period mixing of a 64-bit counter into a
/// 64-bit output. Used here purely to expand a single-`u64` seed into
/// the four-`u64` state required by xoshiro256\*\*.
#[inline]
fn splitmix64(x: &mut u64) -> u64 {
    *x = x.wrapping_add(SPLITMIX_GAMMA);
    let mut z = *x;
    z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
    z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
    z ^ (z >> 31)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn seed_produces_a_value() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        let _ = rng.next_u64();
    }

    #[test]
    fn different_seeds_produce_different_streams() {
        let mut a = Xoshiro256::seed_from_u64(1);
        let mut b = Xoshiro256::seed_from_u64(2);
        assert_ne!(a.next_u64(), b.next_u64());
    }

    #[test]
    fn same_seed_produces_same_stream() {
        let mut a = Xoshiro256::seed_from_u64(42);
        let mut b = Xoshiro256::seed_from_u64(42);
        for _ in 0..256 {
            assert_eq!(a.next_u64(), b.next_u64());
        }
    }

    #[test]
    fn zero_seed_does_not_yield_zero_state() {
        // splitmix64 of seed=0 starts at counter = GAMMA, so the state
        // is non-zero. The first output must therefore also be nonzero
        // for any reasonable initial state.
        let mut rng = Xoshiro256::seed_from_u64(0);
        assert_ne!(rng.state(), [0; 4]);
        assert_ne!(rng.next_u64(), 0);
    }

    #[test]
    fn from_state_rejects_all_zero() {
        assert!(Xoshiro256::from_state([0, 0, 0, 0]).is_none());
        assert!(Xoshiro256::from_state([0, 0, 0, 1]).is_some());
    }

    #[test]
    fn state_roundtrip_resumes_stream() {
        let mut rng = Xoshiro256::seed_from_u64(99);
        for _ in 0..17 {
            let _ = rng.next_u64();
        }
        let snap = rng.state();
        let mut resumed = Xoshiro256::from_state(snap).unwrap();
        for _ in 0..32 {
            assert_eq!(rng.next_u64(), resumed.next_u64());
        }
    }

    #[test]
    fn splitmix64_known_value_zero_counter() {
        // splitmix64(seed=0) — first output is fully determined by the
        // algorithm constants and is a canonical regression vector.
        let mut x = 0u64;
        assert_eq!(splitmix64(&mut x), 0xE220_A839_7B1D_CDAF);
    }

    #[test]
    fn xoshiro_known_first_outputs_for_seed_zero() {
        // Regression vectors: generated from the canonical
        // splitmix64 + xoshiro256** construction with seed 0. Any
        // change here is either a bug or an intentional algorithm
        // change (which would be a breaking change to determinism).
        let mut rng = Xoshiro256::seed_from_u64(0);
        let v0 = rng.next_u64();
        let v1 = rng.next_u64();
        let v2 = rng.next_u64();
        let v3 = rng.next_u64();
        // First, ensure values are non-degenerate.
        assert_ne!(v0, 0);
        assert_ne!(v0, v1);
        assert_ne!(v1, v2);
        assert_ne!(v2, v3);
        // Lock in determinism: replaying from the same seed produces
        // the same sequence.
        let mut rng2 = Xoshiro256::seed_from_u64(0);
        assert_eq!(rng2.next_u64(), v0);
        assert_eq!(rng2.next_u64(), v1);
        assert_eq!(rng2.next_u64(), v2);
        assert_eq!(rng2.next_u64(), v3);
    }

    #[test]
    fn next_u32_takes_high_bits() {
        // The relationship `next_u32 = (next_u64 >> 32) as u32` must
        // hold against a fresh, identically-seeded generator.
        let mut a = Xoshiro256::seed_from_u64(7);
        let mut b = Xoshiro256::seed_from_u64(7);
        for _ in 0..64 {
            let hi = (a.next_u64() >> 32) as u32;
            assert_eq!(hi, b.next_u32());
        }
    }

    #[test]
    fn next_f64_bounds() {
        let mut rng = Xoshiro256::seed_from_u64(1);
        for _ in 0..10_000 {
            let f = rng.next_f64();
            assert!((0.0..1.0).contains(&f));
        }
    }

    #[test]
    fn fill_bytes_exact_chunks() {
        let mut a = Xoshiro256::seed_from_u64(123);
        let mut b = Xoshiro256::seed_from_u64(123);
        let mut buf = [0u8; 64];
        a.fill_bytes(&mut buf);
        for chunk in buf.chunks_exact(8) {
            let expected = b.next_u64().to_le_bytes();
            assert_eq!(chunk, expected);
        }
    }

    #[test]
    fn fill_bytes_partial_tail() {
        // Length 13 = one full 8-byte chunk plus 5 trailing bytes.
        let mut rng = Xoshiro256::seed_from_u64(2026);
        let mut buf = [0u8; 13];
        rng.fill_bytes(&mut buf);
        // Trivially: at least one byte must be nonzero — a 13-byte
        // all-zero output would be a 2^-104 freak event indicating a
        // bug, not chance.
        assert!(buf.iter().any(|&b| b != 0));
    }

    #[test]
    fn jump_produces_different_state() {
        let mut a = Xoshiro256::seed_from_u64(1);
        let mut b = a.clone();
        b.jump();
        assert_ne!(a.state(), b.state());
        // The two streams must not coincide on the first output.
        assert_ne!(a.next_u64(), b.next_u64());
    }

    #[test]
    fn long_jump_produces_different_state() {
        let mut a = Xoshiro256::seed_from_u64(1);
        let mut b = a.clone();
        b.long_jump();
        assert_ne!(a.state(), b.state());
        assert_ne!(a.next_u64(), b.next_u64());
    }

    #[test]
    fn jump_is_deterministic() {
        let mut a = Xoshiro256::seed_from_u64(5);
        let mut b = Xoshiro256::seed_from_u64(5);
        a.jump();
        b.jump();
        assert_eq!(a.state(), b.state());
    }
}