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
//! `no_std` Random Number Generator Implementation
//!
//! This module provides a complete `no_std` RNG implementation that works
//! in constrained environments without the standard library.
use core::fmt;
use rand_core::{
TryCryptoRng,
TryRng,
};
#[cfg(feature = "custom-entropy")]
use crate::custom_entropy::{
generate_custom_entropy,
has_custom_entropy_source,
};
use crate::kt128_expander::Kt128Expander;
use crate::{
Error,
Result,
};
/// A `no_std` compatible random number generator
///
/// This RNG uses getrandom for entropy and provides a secure interface
/// for cryptographic operations in `no_std` environments.
#[derive(Debug)]
pub struct NoStdRng {
/// Reseed counter for security
reseed_counter: u32,
/// Bytes generated since last reseed
bytes_generated: usize,
/// Reseed interval in bytes (1MB default)
reseed_interval: usize,
/// KT128 expander when constructed with [`Self::new_deterministic`]
deterministic_rng: Option<Kt128Expander>,
}
impl NoStdRng {
/// Create a new `no_std` RNG instance
///
/// This creates a cryptographically secure RNG that works in `no_std`
/// environments using getrandom for entropy.
///
/// # Errors
///
/// Returns an error if getrandom is not available or fails to initialize.
///
/// # Examples
///
/// ```rust,no_run
/// use lib_q_random::no_std_rng::NoStdRng;
/// use rand_core::Rng;
///
/// let mut rng = NoStdRng::new().unwrap();
/// let mut bytes = [0u8; 32];
/// rng.fill_bytes(&mut bytes);
/// ```
pub fn new() -> Result<Self> {
#[cfg(feature = "getrandom")]
{
// Test getrandom availability
let mut test_bytes = [0u8; 1];
getrandom::fill(&mut test_bytes).map_err(|_| Error::EntropySourceUnavailable {
source: "getrandom",
context: Some("initialization test failed"),
})?;
Ok(Self {
reseed_counter: 0,
bytes_generated: 0,
reseed_interval: 1024 * 1024, // 1MB reseed interval
deterministic_rng: None,
})
}
#[cfg(not(feature = "getrandom"))]
{
Err(Error::FeatureNotAvailable {
feature: "no_std RNG",
required_features: &["getrandom"],
})
}
}
/// Create a new deterministic RNG for testing
///
/// This builds a KT128 XOF byte stream from a **256-bit** seed
/// ([`Kt128Expander`] / [`crate::kt128_expander::DOMAIN_LIBQ_DET_RNG`])
/// in libQ). Output is reproducible and suitable for KATs and benchmarks.
///
/// **Security**: Unpredictability is **entirely** bounded by the secrecy of
/// `seed`. This is **not** a substitute for [`Self::new`]: anyone who knows or
/// guesses the seed knows the full stream. Do not use for production keys or
/// secrets unless the seed itself is high-entropy and handled as key material.
///
/// # Arguments
///
/// * `seed` - 32-byte seed; distinct seeds produce unrelated streams
///
/// # Examples
///
/// ```rust,no_run
/// use lib_q_random::no_std_rng::NoStdRng;
/// use rand_core::Rng;
///
/// let mut rng = NoStdRng::new_deterministic([1; 32]);
/// let mut bytes = [0u8; 32];
/// rng.fill_bytes(&mut bytes);
/// ```
#[must_use]
pub fn new_deterministic(seed: [u8; 32]) -> Self {
Self {
reseed_counter: 0,
bytes_generated: 0,
reseed_interval: 0, // No reseeding for deterministic RNG
deterministic_rng: Some(Kt128Expander::from_det_seed_32(seed)),
}
}
/// Same as [`Self::new_deterministic`] but seeds via `SplitMix64` → KT128.
#[must_use]
pub fn new_deterministic_from_u64(seed: u64) -> Self {
Self {
reseed_counter: 0,
bytes_generated: 0,
reseed_interval: 0,
deterministic_rng: Some(Kt128Expander::from_det_u64(seed)),
}
}
/// Check if this RNG is deterministic
#[must_use]
pub fn is_deterministic(&self) -> bool {
self.reseed_interval == 0
}
/// Get the number of bytes generated since last reseed
#[must_use]
pub fn bytes_generated(&self) -> usize {
self.bytes_generated
}
/// Get the reseed counter
#[must_use]
pub fn reseed_counter(&self) -> u32 {
self.reseed_counter
}
/// Get the reseed interval
#[must_use]
pub fn reseed_interval(&self) -> usize {
self.reseed_interval
}
}
impl TryRng for NoStdRng {
type Error = core::convert::Infallible;
fn try_next_u32(&mut self) -> core::result::Result<u32, Self::Error> {
let mut bytes = [0u8; 4];
self.try_fill_bytes(&mut bytes)?;
Ok(u32::from_le_bytes(bytes))
}
fn try_next_u64(&mut self) -> core::result::Result<u64, Self::Error> {
let mut bytes = [0u8; 8];
self.try_fill_bytes(&mut bytes)?;
Ok(u64::from_le_bytes(bytes))
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> core::result::Result<(), Self::Error> {
if dest.is_empty() {
return Ok(());
}
// Check if we need to reseed
if !self.is_deterministic() && self.bytes_generated >= self.reseed_interval {
self.reseed_counter = self.reseed_counter.wrapping_add(1);
self.bytes_generated = 0;
}
// Generate random bytes
if let Some(ref mut expander) = self.deterministic_rng {
expander.fill_bytes(dest);
} else {
#[cfg(feature = "custom-entropy")]
{
if has_custom_entropy_source() {
if let Err(e) = generate_custom_entropy(dest) {
#[cfg(feature = "getrandom")]
{
getrandom::fill(dest).unwrap_or_else(|_| {
panic!("both custom entropy and getrandom failed: {e:?}")
});
}
#[cfg(not(feature = "getrandom"))]
{
panic!("custom entropy failed and getrandom not available: {e:?}");
}
}
} else {
#[cfg(feature = "getrandom")]
{
getrandom::fill(dest).expect("getrandom failed");
}
#[cfg(not(feature = "getrandom"))]
{
panic!(
"no custom entropy source registered and getrandom feature not enabled"
);
}
}
}
#[cfg(not(feature = "custom-entropy"))]
{
#[cfg(feature = "getrandom")]
{
getrandom::fill(dest).expect("getrandom failed");
}
#[cfg(not(feature = "getrandom"))]
{
panic!("getrandom feature not enabled");
}
}
}
self.bytes_generated += dest.len();
Ok(())
}
}
impl TryCryptoRng for NoStdRng {}
impl fmt::Display for NoStdRng {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"NoStdRng(deterministic: {}, bytes_generated: {}, reseed_counter: {})",
self.is_deterministic(),
self.bytes_generated,
self.reseed_counter
)
}
}
#[cfg(test)]
mod tests {
use rand_core::Rng;
use super::*;
#[cfg(feature = "getrandom")]
#[test]
fn test_no_std_rng_creation() {
let rng = NoStdRng::new();
assert!(rng.is_ok());
}
#[test]
fn test_deterministic_rng_creation() {
let seed = [1u8; 32];
let rng = NoStdRng::new_deterministic(seed);
assert!(rng.is_deterministic());
}
#[cfg(feature = "getrandom")]
#[test]
fn test_rng_bytes_generation() {
let mut rng = NoStdRng::new().unwrap();
let mut bytes = [0u8; 32];
rng.fill_bytes(&mut bytes);
// Check that bytes were actually generated (not all zeros)
let all_zeros = bytes.iter().all(|&b| b == 0);
assert!(!all_zeros);
}
#[cfg(feature = "getrandom")]
#[test]
fn test_rng_reseed_counter() {
let mut rng = NoStdRng::new().unwrap();
let initial_counter = rng.reseed_counter();
let initial_bytes = rng.bytes_generated();
// Generate some bytes
let mut bytes = [0u8; 1024];
rng.fill_bytes(&mut bytes);
// Check that bytes were generated
assert!(rng.bytes_generated() > initial_bytes);
// For deterministic RNGs, reseed counter should remain the same
if rng.is_deterministic() {
assert_eq!(rng.reseed_counter(), initial_counter);
} else {
// For secure RNGs, reseed counter should be >= initial (might not increase for small amounts)
assert!(rng.reseed_counter() >= initial_counter);
}
}
#[test]
fn test_deterministic_rng_consistency() {
let seed = [42u8; 32];
let mut rng1 = NoStdRng::new_deterministic(seed);
let mut rng2 = NoStdRng::new_deterministic(seed);
let mut bytes1 = [0u8; 32];
let mut bytes2 = [0u8; 32];
rng1.fill_bytes(&mut bytes1);
rng2.fill_bytes(&mut bytes2);
assert_eq!(bytes1, bytes2);
}
/// Regression: deterministic RNG must use the full 256-bit seed (KT128), not a
/// collapsed 64-bit state where distant seed bytes could be ignored.
#[test]
fn test_deterministic_seeds_differ_in_final_byte_yield_different_streams() {
let seed_a = [0u8; 32];
let mut seed_b = [0u8; 32];
seed_b[31] = 1;
let mut rng_a = NoStdRng::new_deterministic(seed_a);
let mut rng_b = NoStdRng::new_deterministic(seed_b);
let mut out_a = [0u8; 64];
let mut out_b = [0u8; 64];
rng_a.fill_bytes(&mut out_a);
rng_b.fill_bytes(&mut out_b);
assert_ne!(
out_a, out_b,
"KT128 streams from different 32-byte keys must diverge immediately"
);
}
}