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
// Copyright © 2024 kyberlib. All rights reserved.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! RNG helpers.
//!
//! kyberlib's public surface consumes generic [`rand_core::RngCore`]
//! + [`rand_core::CryptoRng`] — see the function signatures in
//! [`crate::api`] and [`crate::ml_kem`]. The single helper exported
//! here ([`randombytes`](crate::rng::randombytes)) wraps
//! `RngCore::try_fill_bytes` to surface kyberlib's
//! [`KyberLibError::RandomBytesGeneration`](crate::KyberLibError::RandomBytesGeneration)
//! variant on external-RNG faults.
use crateKyberLibError;
use ;
/// Fills a buffer `x` with `len` bytes of random data.
///
/// This function uses a random number generator (RNG) that satisfies both
/// the `RngCore` and `CryptoRng` traits. The RNG is used to generate `len`
/// random bytes and fill the buffer `x` with these bytes.
///
/// # Arguments
///
/// * `x` - A mutable slice of bytes to be filled with random data.
/// * `len` - The number of random bytes to generate.
/// * `rng` - A mutable reference to the RNG.
///
/// # Errors
///
/// If the RNG fails to generate the required number of bytes, an error
/// of type `KyberLibError::RandomBytesGeneration` is returned.
///
/// # Examples
///
/// ```
/// use kyberlib::rng::randombytes;
/// use rand_core::OsRng;
///
/// let mut buffer = [0u8; 32];
/// let len = buffer.len();
/// randombytes(&mut buffer, len, &mut OsRng).expect("OsRng cannot fail");
/// ```
///
/// # Notes
///
/// Ensure that the length of the buffer `x` is at least `len` bytes,
/// otherwise the function will panic due to out-of-bounds memory access.