use ffi;
#[cfg(not(feature = "std"))]
use prelude::*;
pub const SEEDBYTES: usize = ffi::randombytes_SEEDBYTES as usize;
pub fn randombytes(size: usize) -> Vec<u8> {
unsafe {
let mut buf = vec![0u8; size];
ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, size);
buf
}
}
pub fn randombytes_into(buf: &mut [u8]) {
unsafe {
ffi::randombytes_buf(buf.as_mut_ptr() as *mut _, buf.len());
}
}
pub fn randombytes_uniform(upper_bound: u32) -> u32 {
unsafe { ffi::randombytes_uniform(upper_bound) }
}
new_type! {
secret Seed(SEEDBYTES);
}
pub fn randombytes_buf_deterministic(size: usize, seed: &Seed) -> Vec<u8> {
unsafe {
let mut buf = vec![0u8; size];
ffi::randombytes_buf_deterministic(buf.as_mut_ptr() as *mut _, size, seed.0.as_ptr());
buf
}
}
pub fn randombytes_buf_deterministic_into(buf: &mut [u8], seed: &Seed) {
unsafe {
ffi::randombytes_buf_deterministic(buf.as_mut_ptr() as *mut _, buf.len(), seed.0.as_ptr());
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_randombytes_uniform_0() {
::init().unwrap();
assert_eq!(randombytes_uniform(0), 0);
}
#[test]
fn test_randombytes_uniform_1() {
::init().unwrap();
assert_eq!(randombytes_uniform(1), 0);
}
#[test]
fn test_randombytes_uniform_7() {
::init().unwrap();
assert!(randombytes_uniform(7) < 7);
}
#[test]
fn test_randombytes_buf_deterministic() {
::init().unwrap();
let seed = Seed([0u8; SEEDBYTES]);
let res_1 = randombytes_buf_deterministic(10, &seed);
let res_2 = randombytes_buf_deterministic(10, &seed);
assert_eq!(res_1, res_2);
}
#[test]
fn test_randombytes_buf_deterministic_into() {
::init().unwrap();
let seed = Seed([0u8; SEEDBYTES]);
let mut buf_1 = vec![0u8; 10];
let mut buf_2 = vec![0u8; 10];
randombytes_buf_deterministic_into(buf_1.as_mut_slice(), &seed);
randombytes_buf_deterministic_into(buf_2.as_mut_slice(), &seed);
assert_eq!(buf_1, buf_2);
}
#[test]
fn test_randombytes_buf_deterministic_unique_given_seed() {
::init().unwrap();
let seed_1 = Seed([
0, 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,
]);
let seed_2 = Seed([
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,
]);
let res_1 = randombytes_buf_deterministic(1 << 10, &seed_1);
let res_2 = randombytes_buf_deterministic(1 << 10, &seed_2);
assert_ne!(res_1, res_2);
}
}