about summary refs log tree commit diff
path: root/library/std/src/sys/random/zkvm.rs
blob: 3011942f6b26be545f8d7400ed28ef2643cf45d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use crate::sys::pal::abi;

pub fn fill_bytes(bytes: &mut [u8]) {
    let (pre, words, post) = unsafe { bytes.align_to_mut::<u32>() };
    if !words.is_empty() {
        unsafe {
            abi::sys_rand(words.as_mut_ptr(), words.len());
        }
    }

    let mut buf = [0u32; 2];
    let len = (pre.len() + post.len() + size_of::<u32>() - 1) / size_of::<u32>();
    if len != 0 {
        unsafe { abi::sys_rand(buf.as_mut_ptr(), len) };
    }

    let buf = buf.map(u32::to_ne_bytes);
    let buf = buf.as_flattened();
    pre.copy_from_slice(&buf[..pre.len()]);
    post.copy_from_slice(&buf[pre.len()..pre.len() + post.len()]);
}