forked from rust-random/rand
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcached.rs
110 lines (97 loc) · 3.15 KB
/
cached.rs
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
// Copyright 2025 Developers of the Rand project.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! A cache over `OsRng`
use crate::rngs::OsRng;
use crate::Rng;
use core::fmt;
use rand_core::block::{BlockRng, BlockRngCore};
use rand_core::{RngCore, TryRngCore};
#[repr(transparent)]
struct Array64<T>([T; 64]);
impl<T> Default for Array64<T>
where
T: Default,
{
#[rustfmt::skip]
fn default() -> Self {
Self([
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(), T::default(),
])
}
}
impl<T> AsRef<[T]> for Array64<T> {
fn as_ref(&self) -> &[T] {
&self.0
}
}
impl<T> AsMut<[T]> for Array64<T> {
fn as_mut(&mut self) -> &mut [T] {
&mut self.0
}
}
impl<T> Clone for Array64<T>
where
T: Copy + Default,
{
fn clone(&self) -> Self {
let mut new = Self::default();
new.0.copy_from_slice(&self.0);
new
}
}
/// A cache over `OsRng`
#[derive(Default)]
pub struct CachedOsRng(BlockRng<OsRngCore>);
impl fmt::Debug for CachedOsRng {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CachedOsRng {{ .. }}")
}
}
impl CachedOsRng {
/// Immediately discard any remaining random data in the cache
pub fn reset(&mut self) {
self.0.reset();
}
}
#[derive(Default)]
struct OsRngCore;
impl BlockRngCore for OsRngCore {
type Item = u32;
type Results = Array64<u32>;
#[inline]
fn generate(&mut self, r: &mut Self::Results) {
OsRng.unwrap_err().fill(r.as_mut());
}
}
// TODO: we should implement TryRngCore instead? But then BlockRng needs to use that.
impl RngCore for CachedOsRng {
#[inline]
fn next_u32(&mut self) -> u32 {
self.0.next_u32()
}
#[inline]
fn next_u64(&mut self) -> u64 {
self.0.next_u64()
}
#[inline]
fn fill_bytes(&mut self, bytes: &mut [u8]) {
if bytes.len() >= 256 {
OsRng.unwrap_err().fill_bytes(bytes);
} else {
self.0.fill_bytes(bytes)
}
}
}
// CryptoRng