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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! Generate random data in such a way as to make rare edge-cases very likely.
//!
//! > Disclaimer: the random number generators used in this crate are NOT
//! CRYPTOGRAPHICALLY SECURE. Only use these generators for generating testing
//! inputs, do not rely on them for cryptographic purposes in production code!
//! For instance, you may test a cryptographic tool with these generators, but
//! you may not deploy code that relies on these generators for security in
//! production.
//!
//! For instance, if generating a random `f32` by uniformly sampling 32 bits of
//! data, certain values will rarely appear, such as `NAN` and `INFINITY`. When
//! doing randomized testing, like fuzzing, it isn't very useful to repeatedly
//! generate well-behaved data. It is much more useful if we can artificially
//! increase the likelihood of these special values, so that we test with them
//! more often.
//!
//! Additionally, some random number crates will never generate certain
//! problematic bit-patterns, such as `NAN`.
//!
//! This crate is based on the [fastrand]() crate.
//!
//! This crate can work with `no_std`, if you disable the `std` feature. You
//! cannot use the global functions when in a `no_std` environment. In that
//! case, you can explicitly instantiate [Wdg] and call the methods on it.
//! They are equivalent.
//!
//! If using `std`, it's more ergonomic to use the global functions in the
//! [global_functions] module.

#![cfg_attr(not(feature = "std"), no_std)]

use fastrand as fr;

#[cfg(feature = "std")]
mod global_functions;

#[cfg(feature = "std")]
pub use global_functions::*;

#[cfg(test)]
mod float_utils;

/// A weird data generator
#[derive(Clone)]
pub struct Wdg(fr::Rng);

impl Wdg {
    #[must_use]
    pub fn with_seed(seed: u64) -> Self {
        Self(fr::Rng::with_seed(seed))
    }

    #[must_use]
    pub fn fork(&mut self) -> Self {
        Self(self.0.fork())
    }

    pub fn seed(&mut self, seed: u64) {
        self.0.seed(seed);
    }

    pub fn get_seed(&mut self) -> u64 {
        self.0.get_seed()
    }

    /// Generates a random f32 `NAN` value.
    ///
    /// There are multiple bit patterns that are equivalent to a `NAN`.
    /// This generator covers all possible `NAN` values as specified in
    /// IEEE-754, even ones that Rust would normally not generate.
    pub fn nan_f32(&mut self) -> f32 {
        let sign: u32 = self.0.u32(0..=1) << 31;
        let exponent: u32 = 0b1111_1111 << 23;

        // mantissa 00...00 is INFINITY not NAN!
        let mantissa: u32 = self.0.u32(1..(1 << 23));

        let bits = sign | exponent | mantissa;
        f32::from_bits(bits)
    }

    /// Generates a random f64 `NAN` value.
    ///
    /// There are multiple bit patterns that are equivalent to a `NAN`.
    /// This generator covers all possible `NAN` values as specified in
    /// IEEE-754, even ones that Rust would normally not generate.
    pub fn nan_f64(&mut self) -> f64 {
        let sign: u64 = self.0.u64(0..=1) << 63;
        let exponent: u64 = 0b0111_1111_1111 << 52;

        // mantissa 00...00 is INFINITY not NAN!
        let mantissa: u64 = self.0.u64(1..(1 << 52));

        let bits = sign | exponent | mantissa;
        f64::from_bits(bits)
    }

    /// Generates a random f32 denormal value.
    ///
    /// This generator covers all possible denormal values as specified in
    /// IEEE-754.
    pub fn subnormal_f32(&mut self) -> f32 {
        let sign: u32 = self.0.u32(0..=1) << 31;

        // mantissa 00...00 is zero not denormal!
        let mantissa: u32 = self.0.u32(1..(1 << 23));

        let bits = sign | mantissa;
        f32::from_bits(bits)
    }

    /// Generates a random f64 denormal value.
    ///
    /// This generator covers all possible denormal values as specified in
    /// IEEE-754.
    pub fn subnormal_f64(&mut self) -> f64 {
        let sign: u64 = self.0.u64(0..=1) << 63;

        // mantissa 00...00 is zero not denormal!
        let mantissa: u64 = self.0.u64(1..(1 << 52));

        let bits = sign | mantissa;
        f64::from_bits(bits)
    }

    /// Generate a random f32 normal value
    pub fn normal_f32(&mut self) -> f32 {
        let sign: u32 = self.0.u32(0..=1) << 31;

        // careful with this range, all zeros and all ones are not normal
        let exponent: u32 = self.0.u32(0b0000_0001..=0b1111_1110) << 23;

        let mantissa: u32 = self.0.u32(0..=(1 << 23));
        let bits = sign | exponent | mantissa;
        f32::from_bits(bits)
    }

    /// Generate a random f64 normal value
    pub fn normal_f64(&mut self) -> f64 {
        let sign: u64 = self.0.u64(0..=1) << 63;

        // careful with this range, all zeros and all ones are not normal
        let exponent: u64 = self.0.u64(0b000_0000_0001..=0b111_1111_1110) << 52;

        let mantissa: u64 = self.0.u64(0..=(1 << 52));
        let bits = sign | exponent | mantissa;
        f64::from_bits(bits)
    }

    /// Generate a random f32 "special" value
    ///
    /// A special value is what I call specific float values that are unique and
    /// are pretty much impossible to generate by chance, and have some unusual
    /// properties.
    pub fn special_f32(&mut self) -> f32 {
        match self.0.u8(0..=11) {
            0 => 0.0,
            1 => -0.0,
            2 => f32::INFINITY,
            3 => -f32::INFINITY,
            4 => 1.0,
            5 => -1.0,
            6 => f32::MIN,
            7 => f32::MAX,
            8 => f32::MIN_POSITIVE,
            9 => -f32::MIN_POSITIVE,
            10 => f32::EPSILON,
            11 => -f32::EPSILON,
            _ => unreachable!(),
        }
    }

    /// Generate a random f64 "special" value
    ///
    /// A special value is what I call specific float values that are unique and
    /// are pretty much impossible to generate by chance, and have some unusual
    /// properties.
    pub fn special_f64(&mut self) -> f64 {
        match self.0.u8(0..=11) {
            0 => 0.0,
            1 => -0.0,
            2 => f64::INFINITY,
            3 => -f64::INFINITY,
            4 => 1.0,
            5 => -1.0,
            6 => f64::MIN,
            7 => f64::MAX,
            8 => f64::MIN_POSITIVE,
            9 => -f64::MIN_POSITIVE,
            10 => f64::EPSILON,
            11 => -f64::EPSILON,
            _ => unreachable!(),
        }
    }

    /// Generate a random f32, such that special or problematic values are much
    /// more common than normal.
    ///
    /// The distribution is not statistically useful, but it ensures that all edge-case
    /// values get a fair chance of being generated. This is better than using a regular
    /// random number generator, because in the vast majority of cases, a random number
    /// generator will generate perfectly regular and well-behaved values, and certain
    /// values, like `INFINITY` and `NAN` may be impossible to generate.
    ///
    /// The distribution is as follows:
    /// - 25% normal values
    /// - 25% subnormal values
    /// - 25% `NAN` values, including all possible payloads, quiet and signaling `NAN`.
    /// - 25% "special" values, i.e. unique values with special properties such as `INFINITY` and `-0.0`
    pub fn f32(&mut self) -> f32 {
        match self.0.u8(0..4) {
            0 => self.normal_f32(),
            1 => self.subnormal_f32(),
            2 => self.nan_f32(),
            3 => self.special_f32(),
            _ => unreachable!(),
        }
    }

    /// Generate a random f64, such that special or problematic values are much
    /// more common than normal.
    ///
    /// The distribution is not statistically useful, but it ensures that all edge-case
    /// values get a fair chance of being generated. This is better than using a regular
    /// random number generator, because in the vast majority of cases, a random number
    /// generator will generate perfectly regular and well-behaved values, and certain
    /// values, like `INFINITY` and `NAN` may be impossible to generate.
    ///
    /// The distribution is as follows:
    /// - 25% normal values
    /// - 25% subnormal values
    /// - 25% `NAN` values, including all possible payloads, quiet and signaling `NAN`.
    /// - 25% "special" values, i.e. unique values with special properties such as `INFINITY` and `-0.0`
    pub fn f64(&mut self) -> f64 {
        match self.0.u8(0..4) {
            0 => self.normal_f64(),
            1 => self.subnormal_f64(),
            2 => self.nan_f64(),
            3 => self.special_f64(),
            _ => unreachable!(),
        }
    }
}

#[cfg(test)]
mod test_unit {
    extern crate std;

    use super::*;

    #[test]
    fn nan_f32() {
        let mut gen = Wdg::with_seed(0);
        assert!(gen.nan_f32().is_nan());
    }

    #[test]
    fn nan_f64() {
        let mut gen = Wdg::with_seed(0);
        assert!(gen.nan_f64().is_nan());
    }

    #[test]
    fn subnormal_f32() {
        let mut gen = Wdg::with_seed(0);
        assert!(gen.subnormal_f32().is_subnormal());
    }

    #[test]
    fn subnormal_f64() {
        let mut gen = Wdg::with_seed(0);
        assert!(gen.subnormal_f64().is_subnormal());
    }

    #[test]
    fn normal_f32() {
        let mut gen = Wdg::with_seed(0);
        assert!(!gen.normal_f32().is_subnormal());
    }

    #[test]
    fn normal_f64() {
        let mut gen = Wdg::with_seed(0);
        assert!(!gen.normal_f64().is_subnormal());
    }
}

#[cfg(test)]
mod test_fuzz {
    // fuzzing tests, they may take a while to run. Shouldn't last more than
    // a minute per test (or I'll get impatient/cargo will complain)

    extern crate std;

    use crate::float_utils::{f32_exact_eq, f64_exact_eq};

    use super::*;

    // TODO: all seeds here should be picked at random from RANDOM.org

    #[test]
    #[ignore]
    fn nan_f32_is_nan() {
        let mut gen = Wdg::with_seed(0x0b_65_58_2b_4e_d8_20_fe);
        for i in 0..(1 << 30) {
            let num = gen.nan_f32();
            assert!(num.is_nan(), "{}: {:032b}", i, num.to_bits());
        }
    }

    #[test]
    #[ignore]
    fn nan_f64_is_nan() {
        let mut gen = Wdg::with_seed(0x36_44_3e_f8_40_af_6e_49);
        // TODO: this test has poor coverage, there are 1 << 52 possible mantissas
        //       way too many to guess the bad ones at random. Maybe do something
        //       meta where you use this crate to fuzz itself?
        for i in 0..1 << 30 {
            let num = gen.nan_f64();
            assert!(num.is_nan(), "{}: {:064b}", i, num.to_bits());
        }
    }

    #[test]
    fn nan_f32_range() {
        let mut gen = Wdg::with_seed(0x29_21_f1_bd_8b_a9_c6_b6);
        let mut coverage: u32 = 0b0;
        for _ in 0..10000 {
            let num = gen.nan_f32();
            coverage |= num.to_bits();
        }

        // every bit should be generated at least once, given enough attempts
        assert_eq!(coverage, u32::MAX, "{:032b}", coverage);
    }

    #[test]
    fn nan_f64_range() {
        let mut gen = Wdg::with_seed(0x6f_35_67_53_e6_37_13_c3);
        let mut coverage: u64 = 0b0;
        for _ in 0..10000 {
            let num = gen.nan_f64();
            coverage |= num.to_bits();
        }

        // every bit should be generated at least once, given enough attempts
        assert_eq!(coverage, u64::MAX, "{:064b}", coverage);
    }

    #[test]
    #[ignore]
    fn subnoraml_f32_is_subnormal() {
        let mut gen = Wdg::with_seed(0x52_58_4a_d1_55_e1_72_10);
        for i in 0..(1 << 30) {
            let num = gen.subnormal_f32();
            assert!(num.is_subnormal(), "{}: {:032b}", i, num.to_bits());
        }
    }

    #[test]
    #[ignore]
    fn subnormal_f64_is_subnormal() {
        let mut gen = Wdg::with_seed(0x2d_46_cc_c0_45_c5_ec_03);
        // TODO: this test has poor coverage, there are 1 << 52 possible mantissas
        //       way too many to guess the bad ones at random. Maybe do something
        //       meta where you use this crate to fuzz itself?
        for i in 0..1 << 30 {
            let num = gen.subnormal_f64();
            assert!(num.is_subnormal(), "{}: {:064b}", i, num.to_bits());
        }
    }

    #[test]
    fn subnormal_f32_range() {
        let mut gen = Wdg::with_seed(0x98_fb_6b_ef_ac_5d_81_f3);
        let mut coverage: u32 = 0b1111_1111 << 23;
        for _ in 0..10000 {
            let num = gen.subnormal_f32();
            coverage |= num.to_bits();
        }

        // every bit should be generated at least once, given enough attempts
        assert_eq!(coverage, u32::MAX, "{:032b}", coverage);
    }

    #[test]
    fn subnormal_f64_range() {
        let mut gen = Wdg::with_seed(0x7a_07_58_14_f4_b8_2f_49);
        let mut coverage: u64 = 0b111_1111_1111 << 52;
        for _ in 0..10000 {
            let num = gen.subnormal_f64();
            coverage |= num.to_bits();
        }

        // every bit should be generated at least once, given enough attempts
        assert_eq!(coverage, u64::MAX, "{:064b}", coverage);
    }

    #[test]
    #[ignore]
    fn noraml_f32_is_not_subnormal() {
        let mut gen = Wdg::with_seed(0x2c_fe_59_bb_7a_56_28_20);
        for i in 0..(1 << 30) {
            let num = gen.normal_f32();
            assert!(!num.is_subnormal(), "{}: {:032b}", i, num.to_bits());
        }
    }

    #[test]
    #[ignore]
    fn normal_f64_is_not_subnormal() {
        let mut gen = Wdg::with_seed(0xa9_26_d1_d9_7b_d7_94_15);
        // TODO: this test has poor coverage, there are 1 << 52 possible mantissas
        //       way too many to guess the bad ones at random. Maybe do something
        //       meta where you use this crate to fuzz itself?
        for i in 0..1 << 30 {
            let num = gen.normal_f64();
            assert!(!num.is_subnormal(), "{}: {:064b}", i, num.to_bits());
        }
    }

    #[test]
    fn normal_f32_range() {
        let mut gen = Wdg::with_seed(0x15_63_e3_11_09_cb_11_b5);
        let mut coverage: u32 = 0;
        for _ in 0..10000 {
            let num = gen.normal_f32();
            coverage |= num.to_bits();
        }

        // every bit should be generated at least once, given enough attempts
        assert_eq!(coverage, u32::MAX, "{:032b}", coverage);
    }

    #[test]
    fn normal_f64_range() {
        let mut gen = Wdg::with_seed(0x56_e5_19_b1_47_f2_5e_0d);
        let mut coverage: u64 = 0;
        for _ in 0..10000 {
            let num = gen.normal_f64();
            coverage |= num.to_bits();
        }

        // every bit should be generated at least once, given enough attempts
        assert_eq!(coverage, u64::MAX, "{:064b}", coverage);
    }

    #[test]
    fn special_f32() {
        let mut gen = Wdg::with_seed(0x69_1b_e9_82_15_ed_a0_7d);
        for _ in 0..10000 {
            gen.special_f32();
        }
    }

    #[test]
    fn special_f64() {
        let mut gen = Wdg::with_seed(0xf5_31_9e_51_c4_1f_9e_35);
        for _ in 0..10000 {
            gen.special_f64();
        }
    }

    #[test]
    fn special_f32_range() {
        let mut gen = Wdg::with_seed(0x90_ae_72_03_34_a0_d7_4b);
        let mut had_infinite = false;
        let mut had_neg_infinite = false;
        let mut had_zero = false;
        let mut had_neg_zero = false;
        let mut had_one = false;
        let mut had_neg_one = false;
        let mut had_min_positive = false;
        let mut had_max_negative = false;
        let mut had_epsilon = false;
        let mut had_neg_epsilon = false;
        for _ in 0..10000 {
            let num = gen.special_f32();
            had_infinite |= f32_exact_eq(num, f32::INFINITY);
            had_neg_infinite |= f32_exact_eq(num, f32::NEG_INFINITY);
            had_zero |= f32_exact_eq(num, 0.0);
            had_neg_zero |= f32_exact_eq(num, -0.0);
            had_one |= f32_exact_eq(num, 1.0);
            had_neg_one |= f32_exact_eq(num, -1.0);
            had_min_positive |= f32_exact_eq(num, f32::MIN_POSITIVE);
            had_max_negative |= f32_exact_eq(num, -f32::MIN_POSITIVE);
            had_epsilon |= f32_exact_eq(num, f32::EPSILON);
            had_neg_epsilon |= f32_exact_eq(num, -f32::EPSILON);
        }
        assert!(
            had_infinite
                && had_neg_infinite
                && had_zero
                && had_neg_zero
                && had_one
                && had_neg_one
                && had_min_positive
                && had_max_negative
                && had_epsilon
                && had_neg_epsilon
        );
    }

    #[test]
    fn special_f64_range() {
        let mut gen = Wdg::with_seed(0x10_6c_a1_34_a5_6d_03_97);
        let mut had_infinite = false;
        let mut had_neg_infinite = false;
        let mut had_zero = false;
        let mut had_neg_zero = false;
        let mut had_one = false;
        let mut had_neg_one = false;
        let mut had_min_positive = false;
        let mut had_max_negative = false;
        let mut had_epsilon = false;
        let mut had_neg_epsilon = false;
        for _ in 0..10000 {
            let num = gen.special_f64();
            had_infinite |= f64_exact_eq(num, f64::INFINITY);
            had_neg_infinite |= f64_exact_eq(num, f64::NEG_INFINITY);
            had_zero |= f64_exact_eq(num, 0.0);
            had_neg_zero |= f64_exact_eq(num, -0.0);
            had_one |= f64_exact_eq(num, 1.0);
            had_neg_one |= f64_exact_eq(num, -1.0);
            had_min_positive |= f64_exact_eq(num, f64::MIN_POSITIVE);
            had_max_negative |= f64_exact_eq(num, -f64::MIN_POSITIVE);
            had_epsilon |= f64_exact_eq(num, f64::EPSILON);
            had_neg_epsilon |= f64_exact_eq(num, -f64::EPSILON);
        }
        assert!(
            had_infinite
                && had_neg_infinite
                && had_zero
                && had_neg_zero
                && had_one
                && had_neg_one
                && had_min_positive
                && had_max_negative
                && had_epsilon
                && had_neg_epsilon
        );
    }

    #[test]
    fn f32_range() {
        let mut gen = Wdg::with_seed(0x7c_65_54_c7_d6_a9_d4_b7);

        // these should all be true by the end, given enough attempts
        let mut had_normal = false;
        let mut had_subnormal = false;
        let mut had_nan = false;
        let mut had_special = false;
        for _ in 0..10000 {
            let num = gen.f32();
            had_normal |= num.is_normal();
            had_subnormal |= num.is_subnormal();
            had_nan |= num.is_nan();
            had_special |= num.is_infinite()
                | f32_exact_eq(num, 0.0)
                | f32_exact_eq(num, -0.0)
                | f32_exact_eq(num, 1.0)
                | f32_exact_eq(num, -1.0)
                | f32_exact_eq(num, f32::MIN)
                | f32_exact_eq(num, f32::MAX)
                | f32_exact_eq(num, f32::MIN_POSITIVE)
                | f32_exact_eq(num, -f32::MIN_POSITIVE)
                | f32_exact_eq(num, f32::EPSILON)
                | f32_exact_eq(num, -f32::EPSILON);
        }
        assert!(had_normal && had_subnormal && had_nan && had_special);
    }

    #[test]
    fn f64_range() {
        let mut gen = Wdg::with_seed(0x9a_a4_ee_0f_08_ba_d9_de);

        // these should all be true by the end, given enough attempts
        let mut had_normal = false;
        let mut had_subnormal = false;
        let mut had_nan = false;
        let mut had_special = false;
        for _ in 0..10000 {
            let num = gen.f64();
            had_normal |= num.is_normal();
            had_subnormal |= num.is_subnormal();
            had_nan |= num.is_nan();
            had_special |= num.is_infinite()
                | f64_exact_eq(num, 0.0)
                | f64_exact_eq(num, -0.0)
                | f64_exact_eq(num, 1.0)
                | f64_exact_eq(num, -1.0)
                | f64_exact_eq(num, f64::MIN)
                | f64_exact_eq(num, f64::MAX)
                | f64_exact_eq(num, f64::MIN_POSITIVE)
                | f64_exact_eq(num, -f64::MIN_POSITIVE)
                | f64_exact_eq(num, f64::EPSILON)
                | f64_exact_eq(num, -f64::EPSILON);
        }
        assert!(had_normal && had_subnormal && had_nan && had_special);
    }
}