1#[derive(Debug)]
4pub enum Error {
5 InsufficientRandomness,
6}
7
8pub struct Randomness {
10 pub(crate) bytes: Vec<u8>,
11 pub(crate) pointer: usize,
12}
13
14impl Randomness {
15 pub fn new(bytes: Vec<u8>) -> Self {
17 Self { bytes, pointer: 0 }
18 }
19
20 pub fn bytes(&mut self, len: usize) -> Result<&[u8], Error> {
23 if self.pointer + len > self.bytes.len() {
24 return Err(Error::InsufficientRandomness);
25 }
26
27 let out = &self.bytes[self.pointer..self.pointer + len];
28 self.pointer += len;
29 Ok(out)
30 }
31
32 pub fn bit(&mut self) -> Result<bool, Error> {
35 if self.pointer + 1 > self.bytes.len() {
36 return Err(Error::InsufficientRandomness);
37 }
38
39 let out = &self.bytes[self.pointer];
40 self.pointer += 1;
41 Ok(out & 0x1 == 0x1)
42 }
43}