]> git.bitcoin.ninja Git - rust-lightning/blob - src/util/rng.rs
Added tests to check the bolt 2 specs for Sending Node Channel
[rust-lightning] / src / util / rng.rs
1 #[cfg(not(feature = "fuzztarget"))]
2 mod real_rng {
3         use rand::{thread_rng,Rng};
4
5         pub fn fill_bytes(data: &mut [u8]) {
6                 let mut rng = thread_rng();
7                 rng.fill_bytes(data);
8         }
9
10         pub fn rand_f32() -> f32 {
11                 let mut rng = thread_rng();
12                 rng.next_f32()
13         }
14 }
15 #[cfg(not(feature = "fuzztarget"))]
16 pub use self::real_rng::*;
17
18 #[cfg(feature = "fuzztarget")]
19 mod fuzzy_rng {
20         use util::byte_utils;
21
22         static mut RNG_ITER: u64 = 0;
23
24         pub fn fill_bytes(data: &mut [u8]) {
25                 let rng = unsafe { RNG_ITER += 1; RNG_ITER -1 };
26                 for i in 0..data.len() / 8 {
27                         data[i*8..(i+1)*8].copy_from_slice(&byte_utils::be64_to_array(rng));
28                 }
29                 let rem = data.len() % 8;
30                 let off = data.len() - rem;
31                 data[off..].copy_from_slice(&byte_utils::be64_to_array(rng)[0..rem]);
32         }
33
34         pub fn rand_f32() -> f32 {
35                 let rng = unsafe { RNG_ITER += 1; RNG_ITER - 1 };
36                 f64::from_bits(rng) as f32
37         }
38
39         pub fn reset_rng_state() {
40                 unsafe { RNG_ITER = 0; }
41         }
42 }
43 #[cfg(feature = "fuzztarget")]
44 pub use self::fuzzy_rng::*;