| 1 |
use proc_macro::Span; |
| 2 |
use std::option_env; |
| 3 |
|
| 4 |
use once_cell::race::OnceBox; |
| 5 |
use tiny_keccak::{Xof, Hasher, Shake}; |
| 6 |
|
| 7 |
|
| 8 |
static SEED: OnceBox<Vec<u8>> = OnceBox::new(); |
| 9 |
|
| 10 |
fn get_seed() -> &'static [u8] { |
| 11 |
&SEED.get_or_init(|| { |
| 12 |
if let Some(value) = option_env!("CONST_RANDOM_SEED") { |
| 13 |
Box::new(value.as_bytes().to_vec()) |
| 14 |
} else { |
| 15 |
let mut value = [0u8; 32]; |
| 16 |
getrandom::getrandom(&mut value).unwrap(); |
| 17 |
Box::new(value.to_vec()) |
| 18 |
} |
| 19 |
})[..] |
| 20 |
} |
| 21 |
|
| 22 |
pub(crate) fn gen_random<T: Random>() -> T { |
| 23 |
Random::random() |
| 24 |
} |
| 25 |
|
| 26 |
pub(crate) fn gen_random_bytes(output: &mut [u8]) { |
| 27 |
hash_stuff().squeeze(output) |
| 28 |
} |
| 29 |
|
| 30 |
pub(crate) trait Random { |
| 31 |
fn random() -> Self; |
| 32 |
} |
| 33 |
|
| 34 |
fn hash_stuff() -> impl Xof { |
| 35 |
let span = Span::call_site(); |
| 36 |
let mut hasher = Shake::v256(); |
| 37 |
hasher.update(get_seed()); |
| 38 |
hasher.update(&format!("{:?}", span).as_bytes()); |
| 39 |
hasher |
| 40 |
} |
| 41 |
|
| 42 |
impl Random for u64 { |
| 43 |
fn random() -> Self { |
| 44 |
let mut output = [0; 8]; |
| 45 |
hash_stuff().squeeze(&mut output); |
| 46 |
Self::from_ne_bytes(output) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
impl Random for u128 { |
| 51 |
fn random() -> Self { |
| 52 |
let mut output = [0; 16]; |
| 53 |
hash_stuff().squeeze(&mut output); |
| 54 |
Self::from_ne_bytes(output) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
impl Random for u8 { |
| 59 |
fn random() -> Self { |
| 60 |
u64::random() as u8 |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
impl Random for u16 { |
| 65 |
fn random() -> Self { |
| 66 |
u64::random() as u16 |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
impl Random for u32 { |
| 71 |
fn random() -> Self { |
| 72 |
u64::random() as u32 |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
impl Random for i8 { |
| 77 |
fn random() -> Self { |
| 78 |
i64::random() as i8 |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
impl Random for i16 { |
| 83 |
fn random() -> Self { |
| 84 |
i64::random() as i16 |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
impl Random for i32 { |
| 89 |
fn random() -> Self { |
| 90 |
i64::random() as i32 |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
impl Random for i64 { |
| 95 |
fn random() -> Self { |
| 96 |
u64::random() as i64 |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
impl Random for i128 { |
| 101 |
fn random() -> Self { |
| 102 |
u128::random() as i128 |
| 103 |
} |
| 104 |
} |