commonware-utils 2026.5.0

Leverage common functionality across multiple primitives.
Documentation
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
//! Property-style fuzz harness for roaring bitmaps.
//!
//! Defines a [`Plan`] enum whose variants describe the scenarios we want to drive
//! random inputs through, plus a [`Plan::run`] body that asserts the relevant
//! invariants for each scenario. The same [`Plan`] is consumed by both:
//!
//! - The in-process `minifuzz` harness via a `#[test]` entry point at the bottom of
//!   this file, which runs as part of `cargo test` and exercises hundreds-to-thousands
//!   of random plans per invocation.
//! - The external `cargo-fuzz` target at `utils/fuzz/fuzz_targets/roaring.rs`, which
//!   parses a [`Plan`] from raw fuzzer bytes and calls [`Plan::run`] on it.
//!
//! Sharing the harness body means a regression caught by the long-running
//! coverage-guided fuzzer is also catchable by the regular test suite, and a
//! reproduction reported by `minifuzz` (a hex `MINIFUZZ_BRANCH` token) can be
//! replayed deterministically against the same code path.

use super::{super::BitMap, Bitmap, Prunable};
use arbitrary::{Arbitrary, Unstructured};
use bytes::Bytes;
use commonware_codec::{Decode, Encode, EncodeSize, Read, Write};

const MAX_VALUES: usize = 10_000;
const MAX_CONTAINERS: usize = 100;

/// Maximum value that keeps us within `MAX_CONTAINERS` containers. Each container
/// covers 65536 values (2^16), so we cap at `MAX_CONTAINERS * 65536` to prevent
/// pathological inputs (e.g. `u64::MAX`) from blowing up memory at fuzz time.
const MAX_VALUE: u64 = (MAX_CONTAINERS as u64) << 16;

/// A `u64` value constrained to stay within `MAX_CONTAINERS` containers.
#[derive(Debug, Clone, Copy)]
pub struct BoundedU64(u64);

impl<'a> Arbitrary<'a> for BoundedU64 {
    fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
        let v: u64 = u.arbitrary()?;
        Ok(Self(v % MAX_VALUE))
    }
}

impl From<BoundedU64> for u64 {
    fn from(v: BoundedU64) -> Self {
        v.0
    }
}

/// Operation against a `Bitmap`, used by the `MixedOperations` variant to
/// drive interleaved mutate / query sequences.
#[derive(Arbitrary, Debug)]
pub enum MixedOp {
    Insert(BoundedU64),
    InsertRange { start: BoundedU64, len: u8 },
    Contains(BoundedU64),
}

/// Operation scoped to a single 16-bit shelf (single `Container`). Used by the
/// `ConcentratedShelf` variant to cross the Array->Bitmap and Bitmap↔Run
/// auto-conversion thresholds repeatedly within one container's lifetime.
#[derive(Arbitrary, Debug)]
pub enum ShelfOp {
    Insert(u16),
    InsertRange { start: u16, len: u16 },
}

/// Operation against a `Prunable`. Used by the `PrunableOps` variant.
#[derive(Arbitrary, Debug)]
pub enum PrunableOp {
    Insert(BoundedU64),
    InsertRange { start: BoundedU64, len: u16 },
    PruneBelow(BoundedU64),
}

/// Scenarios driven by random inputs. Each variant's [`Plan::run`] body asserts
/// an invariant that should hold for that operation.
#[derive(Arbitrary, Debug)]
pub enum Plan {
    Insert {
        values: Vec<BoundedU64>,
    },
    InsertRange {
        start: BoundedU64,
        len: u16,
    },
    Contains {
        values: Vec<BoundedU64>,
        query: BoundedU64,
    },
    Codec {
        values: Vec<BoundedU64>,
    },
    CodecRange {
        start: BoundedU64,
        len: u16,
    },
    Union {
        values_a: Vec<BoundedU64>,
        values_b: Vec<BoundedU64>,
        limit: u64,
    },
    Intersection {
        values_a: Vec<BoundedU64>,
        values_b: Vec<BoundedU64>,
        limit: u64,
    },
    Difference {
        values_a: Vec<BoundedU64>,
        values_b: Vec<BoundedU64>,
        limit: u64,
    },
    Iterator {
        values: Vec<BoundedU64>,
    },
    IterRange {
        values: Vec<BoundedU64>,
        start: BoundedU64,
        end: BoundedU64,
    },
    MinMax {
        values: Vec<BoundedU64>,
    },
    MultipleRanges {
        ranges: Vec<(BoundedU64, u16)>,
    },
    MixedOperations {
        ops: Vec<MixedOp>,
    },
    /// Concentrate many inserts inside a single 16-bit shelf so that
    /// Array->Bitmap->Run and Run->Bitmap auto-conversion paths get crossed
    /// repeatedly within one container's lifetime. Codec roundtrip at the end
    /// implicitly verifies `Bitmap::run_count` correctness (decode recomputes
    /// from `words`).
    ConcentratedShelf {
        shelf: u8,
        ops: Vec<ShelfOp>,
    },
    /// Drive a `Prunable` through interleaved inserts and `prune_below` calls.
    /// Inserts are guarded against the panic-on-access-below-watermark
    /// invariant; codec roundtrip implicitly validates that `prune_below`
    /// leaves no straggler containers below the watermark (decode rejects such
    /// states).
    PrunableOps {
        ops: Vec<PrunableOp>,
    },
}

fn build_bitmap(values: &[BoundedU64]) -> Bitmap {
    let mut bitmap = Bitmap::new();
    for v in values.iter().take(MAX_VALUES) {
        bitmap.insert(v.0);
    }
    bitmap
}

fn reference_len(groups: &[&[BoundedU64]]) -> u64 {
    groups
        .iter()
        .flat_map(|values| values.iter().take(MAX_VALUES))
        .map(|value| value.0)
        .max()
        .map_or(0, |value| value + 1)
}

fn set_reference(reference: &mut BitMap, values: &[BoundedU64]) {
    for value in values.iter().take(MAX_VALUES) {
        if value.0 < reference.len() {
            reference.set(value.0, true);
        }
    }
}

fn build_reference(values: &[BoundedU64], len: u64) -> BitMap {
    let mut reference = BitMap::zeroes(len);
    set_reference(&mut reference, values);
    reference
}

fn expected_values(reference: &BitMap, limit: u64) -> Vec<u64> {
    if limit == 0 {
        return Vec::new();
    }

    let mut expected = Vec::new();
    for value in 0..reference.len() {
        if reference.get(value) {
            expected.push(value);
            if expected.len() as u64 == limit {
                break;
            }
        }
    }
    expected
}

fn assert_matches_reference(result: &Bitmap, reference: &BitMap, limit: u64, op: &str) {
    let actual: Vec<_> = result.iter().collect();
    let expected = expected_values(reference, limit);
    assert_eq!(actual, expected, "{op} mismatch");
}

impl Plan {
    pub fn run(self, _u: &mut Unstructured<'_>) -> arbitrary::Result<()> {
        match self {
            Self::Insert { values } => {
                let mut bitmap = Bitmap::new();
                let mut expected_len = 0u64;
                for v in values.iter().take(MAX_VALUES) {
                    let v = v.0;
                    let was_new = bitmap.insert(v);
                    if was_new {
                        expected_len += 1;
                    }
                    assert!(bitmap.contains(v));
                }
                assert_eq!(bitmap.len(), expected_len);
            }

            Self::InsertRange { start, len } => {
                if len == 0 {
                    return Ok(());
                }
                let start = start.0;
                let end = start.saturating_add(len as u64).min(MAX_VALUE);
                let expected_len = end - start;
                if expected_len == 0 {
                    return Ok(());
                }
                let mut bitmap = Bitmap::new();
                let inserted = bitmap.insert_range(start..end);
                assert_eq!(inserted, expected_len);
                assert_eq!(bitmap.len(), expected_len);
                for i in start..end {
                    assert!(bitmap.contains(i), "missing value {}", i);
                }
                if start > 0 {
                    assert!(!bitmap.contains(start - 1));
                }
                if end < MAX_VALUE {
                    assert!(!bitmap.contains(end));
                }
            }

            Self::Contains { values, query } => {
                let bitmap = build_bitmap(&values);
                let query = query.0;
                let expected = values.iter().take(MAX_VALUES).any(|v| v.0 == query);
                assert_eq!(bitmap.contains(query), expected);
            }

            Self::Codec { values } => {
                let bitmap = build_bitmap(&values);
                let encoded_size = bitmap.encode_size();
                assert!(encoded_size > 0 || bitmap.is_empty());

                let mut buf = Vec::new();
                bitmap.write(&mut buf);

                let mut cursor = std::io::Cursor::new(buf.clone());
                let decoded = Bitmap::read_cfg(&mut cursor, &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(bitmap.len(), decoded.len());
                assert_eq!(bitmap, decoded);

                let decoded2 =
                    Bitmap::decode_cfg(Bytes::from(buf), &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(bitmap, decoded2);

                let encoded2 = decoded.encode();
                let encoded1 = bitmap.encode();
                assert_eq!(encoded1, encoded2);
            }

            Self::CodecRange { start, len } => {
                if len == 0 {
                    return Ok(());
                }
                let start = start.0;
                let end = start.saturating_add(len as u64).min(MAX_VALUE);
                if start >= end {
                    return Ok(());
                }
                let mut bitmap = Bitmap::new();
                bitmap.insert_range(start..end);

                let encoded = bitmap.encode();
                let decoded =
                    Bitmap::decode_cfg(encoded.clone(), &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(bitmap.len(), decoded.len());
                assert_eq!(bitmap, decoded);
                for i in start..end {
                    assert!(decoded.contains(i), "decoded missing value {}", i);
                }
                let re_encoded = decoded.encode();
                assert_eq!(encoded, re_encoded);
            }

            Self::Union {
                values_a,
                values_b,
                limit,
            } => {
                let a = build_bitmap(&values_a);
                let b = build_bitmap(&values_b);
                let result = a.union(&b, limit);
                let len = reference_len(&[&values_a, &values_b]);
                let mut reference = build_reference(&values_a, len);
                set_reference(&mut reference, &values_b);
                assert_matches_reference(&result, &reference, limit, "union");

                if limit == u64::MAX {
                    for v in a.iter() {
                        assert!(result.contains(v), "union missing value from a: {}", v);
                    }
                    for v in b.iter() {
                        assert!(result.contains(v), "union missing value from b: {}", v);
                    }
                }
                assert!(result.len() <= limit);

                let values: Vec<_> = result.iter().collect();
                for window in values.windows(2) {
                    assert!(window[0] < window[1], "union not sorted");
                }
            }

            Self::Intersection {
                values_a,
                values_b,
                limit,
            } => {
                let a = build_bitmap(&values_a);
                let b = build_bitmap(&values_b);
                let result = a.intersection(&b, limit);
                let len = reference_len(&[&values_a, &values_b]);
                let b_reference = build_reference(&values_b, len);
                let mut reference = BitMap::zeroes(len);
                for value in values_a.iter().take(MAX_VALUES) {
                    if value.0 < len && b_reference.get(value.0) {
                        reference.set(value.0, true);
                    }
                }
                assert_matches_reference(&result, &reference, limit, "intersection");

                for v in result.iter() {
                    assert!(a.contains(v), "intersection contains value not in a: {}", v);
                    assert!(b.contains(v), "intersection contains value not in b: {}", v);
                }
                assert!(result.len() <= limit);

                let values: Vec<_> = result.iter().collect();
                for window in values.windows(2) {
                    assert!(window[0] < window[1], "intersection not sorted");
                }
            }

            Self::Difference {
                values_a,
                values_b,
                limit,
            } => {
                let a = build_bitmap(&values_a);
                let b = build_bitmap(&values_b);
                let result = a.difference(&b, limit);
                let len = reference_len(&[&values_a, &values_b]);
                let mut reference = build_reference(&values_a, len);
                for value in values_b.iter().take(MAX_VALUES) {
                    if value.0 < len {
                        reference.set(value.0, false);
                    }
                }
                assert_matches_reference(&result, &reference, limit, "difference");

                for v in result.iter() {
                    assert!(a.contains(v), "difference contains value not in a: {}", v);
                    assert!(!b.contains(v), "difference contains value in b: {}", v);
                }
                assert!(result.len() <= limit);

                let values: Vec<_> = result.iter().collect();
                for window in values.windows(2) {
                    assert!(window[0] < window[1], "difference not sorted");
                }
            }

            Self::Iterator { values } => {
                let bitmap = build_bitmap(&values);
                let collected: Vec<_> = bitmap.iter().collect();
                assert_eq!(collected.len() as u64, bitmap.len());
                for window in collected.windows(2) {
                    assert!(window[0] < window[1], "iterator not sorted");
                }
                for &v in &collected {
                    assert!(bitmap.contains(v));
                }
            }

            Self::IterRange { values, start, end } => {
                let start = start.0;
                let end = end.0;
                if start >= end {
                    return Ok(());
                }
                let bitmap = build_bitmap(&values);
                let collected: Vec<_> = bitmap.iter_range(start..end).collect();
                for &v in &collected {
                    assert!(
                        v >= start && v < end,
                        "value {} out of range [{}, {})",
                        v,
                        start,
                        end
                    );
                    assert!(bitmap.contains(v));
                }
                for window in collected.windows(2) {
                    assert!(window[0] < window[1], "iter_range not sorted");
                }
            }

            Self::MinMax { values } => {
                let bitmap = build_bitmap(&values);
                if bitmap.is_empty() {
                    assert_eq!(bitmap.min(), None);
                    assert_eq!(bitmap.max(), None);
                } else {
                    let min = bitmap.min().unwrap();
                    let max = bitmap.max().unwrap();
                    assert!(bitmap.contains(min));
                    assert!(bitmap.contains(max));
                    assert!(min <= max);
                    for v in bitmap.iter() {
                        assert!(v >= min);
                        assert!(v <= max);
                    }
                }
            }

            Self::MultipleRanges { ranges } => {
                let mut bitmap = Bitmap::new();
                for (start, len) in ranges.iter().take(100) {
                    if *len == 0 {
                        continue;
                    }
                    let start = start.0;
                    let end = start.saturating_add(*len as u64).min(MAX_VALUE);
                    if start < end {
                        bitmap.insert_range(start..end);
                    }
                }
                let encoded = bitmap.encode();
                let decoded = Bitmap::decode_cfg(encoded, &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(bitmap.len(), decoded.len());
                assert_eq!(bitmap, decoded);
            }

            Self::MixedOperations { ops } => {
                let mut bitmap = Bitmap::new();
                for op in ops.iter().take(1000) {
                    match op {
                        MixedOp::Insert(v) => {
                            let v = v.0;
                            bitmap.insert(v);
                            assert!(bitmap.contains(v));
                        }
                        MixedOp::InsertRange { start, len } => {
                            if *len > 0 {
                                let start = start.0;
                                let end = start.saturating_add(*len as u64).min(MAX_VALUE);
                                if start < end {
                                    bitmap.insert_range(start..end);
                                }
                            }
                        }
                        MixedOp::Contains(v) => {
                            let _ = bitmap.contains(v.0);
                        }
                    }
                }
                let encoded = bitmap.encode();
                let decoded = Bitmap::decode_cfg(encoded, &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(bitmap.len(), decoded.len());
                assert_eq!(bitmap, decoded);
            }

            Self::ConcentratedShelf { shelf, ops } => {
                let shelf = (shelf as u64) % (MAX_CONTAINERS as u64);
                let base = shelf << 16;

                let mut bitmap = Bitmap::new();
                for op in ops.iter().take(5000) {
                    match op {
                        ShelfOp::Insert(idx) => {
                            bitmap.insert(base + *idx as u64);
                        }
                        ShelfOp::InsertRange { start, len } => {
                            if *len == 0 {
                                continue;
                            }
                            let s = base + *start as u64;
                            let e = (s + *len as u64).min(base + 65536);
                            if s < e {
                                bitmap.insert_range(s..e);
                            }
                        }
                    }
                }

                // Codec roundtrip implicitly validates `Bitmap::run_count`: encode
                // writes only `words`; decode recomputes `run_count` via scan.
                // PartialEq compares run_count, so any drift in incremental tracking
                // surfaces as inequality.
                let encoded = bitmap.encode();
                let decoded = Bitmap::decode_cfg(encoded, &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(bitmap.len(), decoded.len());
                assert_eq!(bitmap, decoded);

                let collected: Vec<_> = bitmap.iter().collect();
                assert_eq!(collected.len() as u64, bitmap.len());
                for window in collected.windows(2) {
                    assert!(window[0] < window[1], "iterator not sorted");
                }
                for &v in &collected {
                    assert!(bitmap.contains(v));
                }
            }

            Self::PrunableOps { ops } => {
                let mut p = Prunable::new();
                for op in ops.iter().take(1000) {
                    match op {
                        PrunableOp::Insert(v) => {
                            let v = u64::from(*v);
                            if v >= p.pruned_below() {
                                p.insert(v);
                            }
                        }
                        PrunableOp::InsertRange { start, len } => {
                            if *len == 0 {
                                continue;
                            }
                            let s = u64::from(*start);
                            let e = s.saturating_add(*len as u64).min(MAX_VALUE);
                            if s >= p.pruned_below() && s < e {
                                p.insert_range(s..e);
                            }
                        }
                        PrunableOp::PruneBelow(threshold) => {
                            p.prune(u64::from(*threshold));
                        }
                    }
                }
                assert_eq!(p.pruned_below() % (1 << 16), 0);
                let encoded = p.encode();
                let decoded = Prunable::decode_cfg(encoded, &(..=MAX_CONTAINERS).into()).unwrap();
                assert_eq!(p, decoded);
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::Plan;

    #[test]
    fn test_fuzz() {
        commonware_invariants::minifuzz::test(|u| u.arbitrary::<Plan>()?.run(u));
    }
}