Skip to main content

randombytes

Function randombytes 

Source
pub fn randombytes<R>(
    x: &mut [u8],
    len: usize,
    rng: &mut R,
) -> Result<(), KyberLibError>
where R: RngCore + CryptoRng,
Expand description

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.