|
| 1 | +use core::mem::MaybeUninit; |
| 2 | +use getrandom::Error; |
| 3 | + |
| 4 | +/// Chosen by fair dice roll. |
| 5 | +const SEED: u64 = 0x9095_810F_1B2B_E175; |
| 6 | + |
| 7 | +struct Xoshiro128PlusPlus { |
| 8 | + s: [u32; 4], |
| 9 | +} |
| 10 | + |
| 11 | +impl Xoshiro128PlusPlus { |
| 12 | + fn new(mut seed: u64) -> Self { |
| 13 | + const PHI: u64 = 0x9e3779b97f4a7c15; |
| 14 | + let mut s = [0u32; 4]; |
| 15 | + for val in s.iter_mut() { |
| 16 | + seed = seed.wrapping_add(PHI); |
| 17 | + let mut z = seed; |
| 18 | + z = (z ^ (z >> 30)).wrapping_mul(0xbf58476d1ce4e5b9); |
| 19 | + z = (z ^ (z >> 27)).wrapping_mul(0x94d049bb133111eb); |
| 20 | + z = z ^ (z >> 31); |
| 21 | + *val = z as u32; |
| 22 | + } |
| 23 | + Self { s } |
| 24 | + } |
| 25 | + |
| 26 | + fn next_u32(&mut self) -> u32 { |
| 27 | + let res = self.s[0] |
| 28 | + .wrapping_add(self.s[3]) |
| 29 | + .rotate_left(7) |
| 30 | + .wrapping_add(self.s[0]); |
| 31 | + |
| 32 | + let t = self.s[1] << 9; |
| 33 | + |
| 34 | + self.s[2] ^= self.s[0]; |
| 35 | + self.s[3] ^= self.s[1]; |
| 36 | + self.s[1] ^= self.s[2]; |
| 37 | + self.s[0] ^= self.s[3]; |
| 38 | + |
| 39 | + self.s[2] ^= t; |
| 40 | + |
| 41 | + self.s[3] = self.s[3].rotate_left(11); |
| 42 | + |
| 43 | + res |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +pub fn custom_impl(dst: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 48 | + let mut rng = Xoshiro128PlusPlus::new(SEED); |
| 49 | + |
| 50 | + let mut chunks = dst.chunks_exact_mut(4); |
| 51 | + for chunk in &mut chunks { |
| 52 | + let val = rng.next_u32(); |
| 53 | + let dst_ptr = chunk.as_mut_ptr().cast::<u32>(); |
| 54 | + unsafe { core::ptr::write_unaligned(dst_ptr, val) }; |
| 55 | + } |
| 56 | + let rem = chunks.into_remainder(); |
| 57 | + if !rem.is_empty() { |
| 58 | + let val = rng.next_u32(); |
| 59 | + let src_ptr = &val as *const u32 as *const MaybeUninit<u8>; |
| 60 | + assert!(rem.len() <= 4); |
| 61 | + unsafe { core::ptr::copy(src_ptr, rem.as_mut_ptr(), rem.len()) }; |
| 62 | + } |
| 63 | + Ok(()) |
| 64 | +} |
| 65 | + |
| 66 | +#[cfg(getrandom_backend = "custom")] |
| 67 | +#[unsafe(no_mangle)] |
| 68 | +unsafe extern "Rust" fn __getrandom_v03_custom(dst_ptr: *mut u8, len: usize) -> Result<(), Error> { |
| 69 | + let dst = unsafe { core::slice::from_raw_parts_mut(dst_ptr.cast(), len) }; |
| 70 | + custom_impl(dst) |
| 71 | +} |
| 72 | + |
| 73 | +#[cfg(getrandom_backend = "extern_impl")] |
| 74 | +#[getrandom::implementation::fill_uninit] |
| 75 | +fn my_fill_uninit_implementation(dst: &mut [MaybeUninit<u8>]) -> Result<(), Error> { |
| 76 | + custom_impl(dst) |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn test_custom_fill() { |
| 81 | + let mut buf1 = [0u8; 256]; |
| 82 | + getrandom::fill(&mut buf1).unwrap(); |
| 83 | + |
| 84 | + let mut buf2 = [0u8; 256]; |
| 85 | + custom_impl(unsafe { core::slice::from_raw_parts_mut(buf2.as_mut_ptr().cast(), buf2.len()) }) |
| 86 | + .unwrap(); |
| 87 | + |
| 88 | + assert_eq!(buf1, buf2); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn test_custom_u32() { |
| 93 | + let res = getrandom::u32().unwrap(); |
| 94 | + assert_eq!(res, 0xEAD5_840A); |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn test_custom_u64() { |
| 99 | + let res = getrandom::u64().unwrap(); |
| 100 | + assert_eq!(res, 0xA856_FCC4_EAD5_840A); |
| 101 | +} |
0 commit comments