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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use awint::awi::*;
use rand_xoshiro::{
rand_core::{RngCore, SeedableRng},
Xoshiro128StarStar,
};
/// A deterministic psuedo-random-number-generator. Is a wrapper around
/// `Xoshiro128StarStar` that buffers rng calls down to the bit level
#[derive(Debug)]
pub struct StarRng {
rng: Xoshiro128StarStar,
buf: inlawi_ty!(64),
// invariant: `used < buf.bw()` and indicates the number of bits used out of `buf`
used: u8,
}
macro_rules! next {
($($name:ident $x:ident $from:ident $to:ident),*,) => {
$(
/// Returns an output with all bits being randomized
pub fn $name(&mut self) -> $x {
let mut res = InlAwi::$from(0);
let mut processed = 0;
loop {
let remaining_in_buf = usize::from(Self::BW_U8.wrapping_sub(self.used));
let remaining = res.bw().wrapping_sub(processed);
if remaining == 0 {
break
}
if remaining < remaining_in_buf {
res.field(
processed,
&self.buf,
usize::from(self.used),
remaining
).unwrap();
self.used = self.used.wrapping_add(remaining as u8);
break
} else {
res.field(
processed,
&self.buf,
usize::from(self.used),
remaining_in_buf
).unwrap();
processed = processed.wrapping_add(remaining_in_buf);
self.buf = InlAwi::from_u64(self.rng.next_u64());
self.used = 0;
}
}
res.$to()
}
)*
};
}
macro_rules! out_of {
($($fn:ident, $max:expr, $bw:expr);*;) => {
$(
/// Fractional chance of the output being true.
///
/// If `num` is zero, it will always return `false`.
/// If `num` is equal to or larger than the denominator,
/// it will always return `true`.
pub fn $fn(&mut self, num: u8) -> bool {
if num == 0 {
false
} else if num >= $max {
true
} else {
let mut tmp: inlawi_ty!($bw) = InlAwi::zero();
tmp.u8_(num);
self.next_bits(&mut tmp);
num > tmp.to_u8()
}
}
)*
};
}
impl StarRng {
const BW_U8: u8 = 64;
next!(
next_u8 u8 from_u8 to_u8,
next_u16 u16 from_u16 to_u16,
next_u32 u32 from_u32 to_u32,
next_u64 u64 from_u64 to_u64,
next_u128 u128 from_u128 to_u128,
);
// note: do not implement `next_usize`, if it exists then there will inevitably
// be arch-dependent rng code in a lot of places
out_of!(
out_of_4, 4, 2;
out_of_8, 8, 3;
out_of_16, 16, 4;
out_of_32, 32, 5;
out_of_64, 64, 6;
out_of_128, 128, 7;
);
/// Creates a new `StarRng` with the given seed
pub fn new(seed: u64) -> Self {
let mut rng = Xoshiro128StarStar::seed_from_u64(seed);
let buf = InlAwi::from_u64(rng.next_u64());
Self { rng, buf, used: 0 }
}
/// Returns a random boolean
pub fn next_bool(&mut self) -> bool {
let res = self.buf.get(usize::from(self.used)).unwrap();
self.used += 1;
if self.used >= Self::BW_U8 {
self.buf = InlAwi::from_u64(self.rng.next_u64());
self.used = 0;
}
res
}
/// Fractional chance of the output being true.
///
/// If `num` is zero, it will always return `false`.
/// If `num` is equal to or larger than the denominator,
/// it will always return `true`.
pub fn out_of_256(&mut self, num: u8) -> bool {
if num == 0 {
false
} else {
let mut tmp = InlAwi::from_u8(num);
tmp.u8_(num);
self.next_bits(&mut tmp);
num > tmp.to_u8()
}
}
/// Assigns random value to `bits`
pub fn next_bits(&mut self, bits: &mut Bits) {
let mut processed = 0;
loop {
let remaining_in_buf = usize::from(Self::BW_U8.wrapping_sub(self.used));
let remaining = bits.bw().wrapping_sub(processed);
if remaining == 0 {
break
}
if remaining < remaining_in_buf {
bits.field(processed, &self.buf, usize::from(self.used), remaining)
.unwrap();
self.used = self.used.wrapping_add(remaining as u8);
break
} else {
bits.field(
processed,
&self.buf,
usize::from(self.used),
remaining_in_buf,
)
.unwrap();
processed = processed.wrapping_add(remaining_in_buf);
self.buf = InlAwi::from_u64(self.rng.next_u64());
self.used = 0;
}
}
}
/// Returns a random index, given an exclusive maximum of `len`. Returns
/// `None` if `len == 0`.
#[must_use]
pub fn index(&mut self, len: usize) -> Option<usize> {
// TODO there are more sophisticated methods to reduce bias
if len == 0 {
None
} else if len <= (u8::MAX as usize) {
let inx = self.next_u16();
Some((inx as usize) % len)
} else if len <= (u16::MAX as usize) {
let inx = self.next_u32();
Some((inx as usize) % len)
} else {
let inx = self.next_u64();
Some((inx as usize) % len)
}
}
/// Takes a random index of a slice. Returns `None` if `slice.is_empty()`.
#[must_use]
pub fn index_slice<'a, T>(&mut self, slice: &'a [T]) -> Option<&'a T> {
let inx = self.index(slice.len())?;
slice.get(inx)
}
/// Takes a random index of a slice. Returns `None` if `slice.is_empty()`.
#[must_use]
pub fn index_slice_mut<'a, T>(&mut self, slice: &'a mut [T]) -> Option<&'a mut T> {
let inx = self.index(slice.len())?;
slice.get_mut(inx)
}
/// This performs one step of a fuzzer where a random width of ones is
/// rotated randomly and randomly ORed, ANDed, or XORed to `x`. `pad` needs
/// to have the same bitwidth as `x`.
///
/// In many cases there are issues that involve long lines of all set or
/// unset bits, and the `next_bits` function is unsuitable for this as
/// `x.bw()` gets larger than a few bits. This function produces random
/// length strings of ones and zeros concatenated together, which can
/// rapidly probe a more structured space even for large `x`.
///
/// ```
/// use starlight::{awi::*, utils::StarRng};
///
/// let mut rng = StarRng::new(0);
/// let mut x = awi!(0u128);
/// let mut pad = x.clone();
/// // this should be done in a loop with thousands of iterations,
/// // here I have unrolled a few for example
/// rng.linear_fuzz_step(&mut x, &mut pad);
/// assert_eq!(x, awi!(0x1ff_ffffffc0_00000000_u128));
/// rng.linear_fuzz_step(&mut x, &mut pad);
/// assert_eq!(x, awi!(0xffffffff_fffffe00_3fffffc0_0000000f_u128));
/// rng.linear_fuzz_step(&mut x, &mut pad);
/// assert_eq!(x, awi!(0xffffffff_e00001ff_c01fffc0_0000000f_u128));
/// rng.linear_fuzz_step(&mut x, &mut pad);
/// assert_eq!(x, awi!(0x1ffffe00_3fe0003f_fffffff0_u128));
/// rng.linear_fuzz_step(&mut x, &mut pad);
/// assert_eq!(x, awi!(0xffffffff_e03fffff_c01fffc0_0000000f_u128));
/// ```
pub fn linear_fuzz_step(&mut self, x: &mut Bits, pad: &mut Bits) {
let r0 = self.index(x.bw()).unwrap();
let r1 = self.index(x.bw()).unwrap();
pad.umax_();
pad.shl_(r0).unwrap();
pad.rotl_(r1).unwrap();
// note: it needs to be 2 parts XOR to 1 part OR and 1 part AND, the ordering
// guarantees this
if self.next_bool() {
x.xor_(pad).unwrap();
} else if self.next_bool() {
x.or_(pad).unwrap();
} else {
x.and_(pad).unwrap();
}
}
}
impl RngCore for StarRng {
fn next_u32(&mut self) -> u32 {
self.next_u32()
}
fn next_u64(&mut self) -> u64 {
self.next_u64()
}
fn fill_bytes(&mut self, dest: &mut [u8]) {
// TODO make faster
for byte in dest {
*byte = self.next_u8();
}
}
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_xoshiro::rand_core::Error> {
for byte in dest {
*byte = self.next_u8();
}
Ok(())
}
}
impl SeedableRng for StarRng {
type Seed = [u8; 8];
fn from_seed(seed: Self::Seed) -> Self {
Self::new(u64::from_le_bytes(seed))
}
}