reallyme-codec-cbor 0.4.4

Canonical CBOR codec support for ReallyMe Codec.
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
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
// SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::{
    admit_map_key, bounded_capacity, collect_map_entries, decode_key, decode_map, decode_map_entry,
    decode_simple, read_head, read_value_head, reserve_map, DecodeLimits, DeterministicCborError,
};
use crate::check_map_key_order::{MapKeyOrder, MapKeyOrderError};
use crate::nested_map_proof_tests::{
    TwoEntryMap, MIXED_KEY_BYTEWISE, MIXED_KEY_CANONICAL, MIXED_LENGTH_CANONICAL,
    MIXED_LENGTH_REVERSED, TEXT_KEY_ENCODING, UNSIGNED_KEY, UNSIGNED_KEY_ENCODING,
};
use crate::{
    DeterministicCborInteger, DeterministicCborMapEntry, DeterministicCborMapKey,
    DeterministicCborValue,
};
use core::cmp::Ordering;

const MAX_HEADER_BYTES: usize = 9;
const MAX_KEY_BYTES: usize = 8;

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_wire_admission_rejects_bytewise_order() {
    let unsigned_first: bool = kani::any();
    let (wire, first_end, second_start, first_key, second_key, expected) = if unsigned_first {
        (
            &MIXED_KEY_BYTEWISE,
            6,
            7,
            UNSIGNED_KEY_ENCODING,
            TEXT_KEY_ENCODING,
            Err(DeterministicCborError::MapKeysOutOfOrder),
        )
    } else {
        (
            &MIXED_KEY_CANONICAL,
            5,
            6,
            TEXT_KEY_ENCODING,
            UNSIGNED_KEY_ENCODING,
            Ok(()),
        )
    };
    assert!(wire[3..first_end] == *first_key);
    assert!(wire[second_start..9] == *second_key);
    // The unsigned key starts with the smaller byte. A bytewise comparator
    // would give the opposite admission decision on both wires.
    assert!(TEXT_KEY_ENCODING.len() < UNSIGNED_KEY_ENCODING.len());
    assert!(UNSIGNED_KEY_ENCODING[0] < TEXT_KEY_ENCODING[0]);
    let mut order = MapKeyOrder::default();
    assert!(admit_map_key(&mut order, wire, 3, first_end).is_ok());
    let result = admit_map_key(&mut order, wire, second_start, 9);
    assert_eq!(result, expected);
    kani::cover!(unsigned_first && result.is_err(), "bytewise order rejected");
    kani::cover!(
        !unsigned_first && result.is_ok(),
        "length-first order admitted"
    );
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_canonical_text_span_is_exact() {
    verify_mixed_length_entry::<3, 5, 6>(
        &MIXED_KEY_CANONICAL,
        ExpectedKey::Text(b"z"),
        false,
        0,
        Ok(()),
    );
    kani::cover!(true, "canonical first key span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_canonical_unsigned_span_is_exact() {
    verify_mixed_length_entry::<6, 9, 10>(
        &MIXED_KEY_CANONICAL,
        ExpectedKey::Unsigned(UNSIGNED_KEY),
        true,
        1,
        Ok(()),
    );
    kani::cover!(true, "canonical unsigned key span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_bytewise_unsigned_span_is_exact() {
    verify_mixed_length_entry::<3, 6, 7>(
        &MIXED_KEY_BYTEWISE,
        ExpectedKey::Unsigned(UNSIGNED_KEY),
        true,
        0,
        Ok(()),
    );
    kani::cover!(true, "bytewise first key span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_bytewise_text_span_is_exact() {
    verify_mixed_length_entry::<7, 9, 10>(
        &MIXED_KEY_BYTEWISE,
        ExpectedKey::Text(b"z"),
        false,
        0,
        Err(DeterministicCborError::MapKeysOutOfOrder),
    );
    kani::cover!(true, "bytewise text key rejected before reading its value");
}

#[kani::proof]
#[kani::unwind(3)]
fn mixed_key_prefix_starts_at_the_same_offset_in_both_orders() {
    verify_nested_two_entry_prefix(&MIXED_KEY_CANONICAL);
    verify_nested_two_entry_prefix(&MIXED_KEY_BYTEWISE);
    kani::cover!(true, "both mixed-key prefixes reserve their entries");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_wire_admission_uses_length_before_lexical_order() {
    let long_first: bool = kani::any();
    let (wire, first_end, second_start, expected) = if long_first {
        (
            &MIXED_LENGTH_REVERSED,
            6,
            7,
            Err(DeterministicCborError::MapKeysOutOfOrder),
        )
    } else {
        (&MIXED_LENGTH_CANONICAL, 5, 6, Ok(()))
    };
    let mut order = MapKeyOrder::default();
    assert!(admit_map_key(&mut order, wire, 3, first_end).is_ok());
    let result = admit_map_key(&mut order, wire, second_start, 9);
    assert_eq!(result, expected);
    kani::cover!(
        long_first && result.is_err(),
        "longer text key first rejected"
    );
    kani::cover!(!long_first && result.is_ok(), "length-first order admitted");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_canonical_first_span_is_exact() {
    verify_mixed_length_entry::<3, 5, 6>(
        &MIXED_LENGTH_CANONICAL,
        ExpectedKey::Text(b"z"),
        false,
        0,
        Ok(()),
    );
    kani::cover!(true, "first canonical span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_canonical_second_span_is_exact() {
    verify_mixed_length_entry::<6, 9, 10>(
        &MIXED_LENGTH_CANONICAL,
        ExpectedKey::Text(b"aa"),
        true,
        1,
        Ok(()),
    );
    kani::cover!(true, "second canonical span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_reversed_first_span_is_exact() {
    verify_mixed_length_entry::<3, 6, 7>(
        &MIXED_LENGTH_REVERSED,
        ExpectedKey::Text(b"aa"),
        true,
        0,
        Ok(()),
    );
    kani::cover!(true, "first reversed span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_reversed_second_span_is_exact() {
    verify_mixed_length_entry::<7, 9, 10>(
        &MIXED_LENGTH_REVERSED,
        ExpectedKey::Text(b"z"),
        false,
        2,
        Err(DeterministicCborError::MapKeysOutOfOrder),
    );
    kani::cover!(true, "second reversed span checked");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_traversal_forwards_the_cursor_and_stops_on_rejection() {
    let long_first: bool = kani::any();
    let mut calls = 0;
    let result = collect_map_entries(2, 3, |start| {
        calls += 1;
        match calls {
            1 => {
                assert!(start == 3);
                // Both two- and three-byte key pairs establish these same
                // offsets. This traversal only receives entries and cursors.
                Ok((long_first, if long_first { 7 } else { 6 }))
            }
            2 if long_first => {
                assert!(start == 7);
                Err(DeterministicCborError::MapKeysOutOfOrder)
            }
            2 => {
                assert!(start == 6);
                Ok((true, 10))
            }
            _ => {
                kani::assert(false, "a two-entry map visits at most two keys");
                Err(DeterministicCborError::UnexpectedEnd)
            }
        }
    });
    assert!(calls == 2);
    if long_first {
        assert_eq!(result, Err(DeterministicCborError::MapKeysOutOfOrder));
        kani::cover!(result.is_err(), "traversal returns the second key error");
    } else {
        assert!(matches!(result, Ok((ref entries, 10)) if entries.as_slice() == [false, true]));
        kani::cover!(result.is_ok(), "canonical traversal consumes ten bytes");
    }
}

enum ExpectedKey<'a> {
    Text(&'a [u8]),
    Unsigned(u64),
}

fn verify_mixed_length_entry<const START: usize, const END: usize, const NEXT: usize>(
    wire: &[u8; 10],
    expected_key: ExpectedKey<'_>,
    bit: bool,
    preceding_key_bytes: usize,
    admission: Result<(), DeterministicCborError>,
) {
    let second = START != 3;
    let mut limits = DecodeLimits {
        nodes: if second { 5 } else { 3 },
        pending_nodes: if second { 2 } else { 4 },
        aggregate_text_bytes: preceding_key_bytes,
        ..DecodeLimits::default()
    };
    let mut key_span = None;
    let mut child_called = false;
    let entry = decode_map_entry(
        wire,
        START,
        2,
        &mut limits,
        |source, start, depth, budget| {
            child_called = true;
            assert!(core::ptr::eq(source, wire.as_slice()));
            assert!(start == END && depth == 2);
            let (major, argument, next) = read_value_head(source, start, budget)?;
            assert!(major == 7 && next == NEXT);
            Ok((decode_simple(argument)?.into_value(), next))
        },
        |span| {
            // Observe the span prior to the independent admission decision.
            key_span = Some((span.start, span.end));
            admission
        },
    );
    let entry = core::mem::ManuallyDrop::new(entry);
    assert!(key_span == Some((START, END)));
    assert!(child_called == admission.is_ok());
    let text_len = match expected_key {
        ExpectedKey::Text(text) => text.len(),
        ExpectedKey::Unsigned(_) => 0,
    };
    assert!(limits.aggregate_text_bytes.checked_sub(preceding_key_bytes) == Some(text_len));
    if let Err(expected) = admission {
        assert!(matches!(&*entry, Err(actual) if *actual == expected));
        assert!(limits.nodes == 6 && limits.pending_nodes == 1);
        return;
    }
    let Ok((entry, next)) = &*entry else {
        kani::assert(false, "entry supplies its key span and value");
        return;
    };
    assert!(*next == NEXT);
    assert!(limits.nodes == if second { 7 } else { 5 });
    assert!(limits.pending_nodes == if second { 0 } else { 2 });
    let matches_expected_key = match expected_key {
        ExpectedKey::Text(expected) => {
            matches!(entry.key(), DeterministicCborMapKey::Text(key) if key.as_bytes() == expected)
        }
        ExpectedKey::Unsigned(expected) => matches!(
            entry.key(),
            DeterministicCborMapKey::Integer(DeterministicCborInteger::Unsigned(key)) if *key == expected
        ),
    };
    assert!(matches_expected_key);
    assert!(matches!(entry.value(), DeterministicCborValue::Bool(value) if *value == bit));
}

#[kani::proof]
#[kani::unwind(3)]
fn nested_prefix_reserves_two_entries_at_the_first_key() {
    let example = TwoEntryMap::symbolic();
    let wire = example.wire();
    verify_nested_two_entry_prefix(&wire);
    kani::cover!(
        example.first == 0 && example.second == 0x7f,
        "inner traversal can start"
    );
}

#[kani::proof]
#[kani::unwind(3)]
fn mixed_length_prefix_starts_at_the_same_offset_in_both_orders() {
    // The nested prefix is identical. Check that the additional payload byte
    // does not change the reservation or the initial cursor in either order.
    verify_nested_two_entry_prefix(&MIXED_LENGTH_CANONICAL);
    verify_nested_two_entry_prefix(&MIXED_LENGTH_REVERSED);
    kani::cover!(true, "both mixed-length prefixes reserve their entries");
}

fn verify_nested_two_entry_prefix(wire: &[u8]) {
    let mut limits = DecodeLimits::default();
    let Ok((5, count, 1)) = read_value_head(wire, 0, &mut limits) else {
        kani::assert(false, "outer map header");
        return;
    };
    assert!(reserve_map(wire.len(), 1, 0, count, &mut limits) == Ok((1, 1)));
    let Ok((DeterministicCborMapKey::Text(ref key), 2)) = decode_key(wire, 1, &mut limits) else {
        kani::assert(false, "outer empty text key consumes one byte");
        return;
    };
    assert!(key.is_empty());
    let mut outer_order = MapKeyOrder::default();
    assert!(admit_map_key(&mut outer_order, wire, 1, 2).is_ok());
    let Ok((5, count, 3)) = read_value_head(wire, 2, &mut limits) else {
        kani::assert(false, "inner map header");
        return;
    };
    assert!(reserve_map(wire.len(), 3, 1, count, &mut limits) == Ok((2, 2)));
    assert!(limits.nodes == 3 && limits.pending_nodes == 4);
    assert!(limits.aggregate_text_bytes == 0 && limits.aggregate_byte_string_bytes == 0);
}

#[kani::proof]
#[kani::unwind(4)]
fn inner_traversal_passes_each_returned_offset_to_the_next_entry() {
    let example = TwoEntryMap::symbolic();
    let mut calls = 0;
    let result = collect_map_entries(2, 3, |start| {
        // These are the offsets established by the individual entry checks.
        let (key, bit, next) = if calls == 0 {
            assert!(start == 3);
            (example.first, false, 6)
        } else {
            assert!(calls == 1 && start == 6);
            (example.second, true, 9)
        };
        calls += 1;
        Ok(((key, bit), next))
    });
    let Ok((entries, 9)) = result else {
        kani::assert(false, "traversal consumes the complete inner map");
        return;
    };
    assert!(calls == 2);
    assert!(
        matches!(entries.as_slice(), [(first, false), (second, true)]
        if *first == example.first && *second == example.second)
    );
    kani::cover!(
        example.first == 0 && example.second == 0x7f,
        "both key positions retained"
    );
}

#[kani::proof]
#[kani::unwind(4)]
fn first_inner_entry_supplies_exact_key_span() {
    verify_inner_entry::<3, 5, 6>(false);
}

#[kani::proof]
#[kani::unwind(4)]
fn second_inner_entry_supplies_exact_key_span() {
    verify_inner_entry::<6, 8, 9>(true);
}

fn verify_inner_entry<const START: usize, const END: usize, const NEXT: usize>(bit: bool) {
    let example = TwoEntryMap::symbolic();
    let wire = example.wire();
    let mut limits = DecodeLimits {
        nodes: if bit { 5 } else { 3 },
        pending_nodes: if bit { 2 } else { 4 },
        aggregate_text_bytes: usize::from(bit),
        ..DecodeLimits::default()
    };
    let mut observed = None;
    let rejected: bool = kani::any();
    let mut child_called = false;
    let result = decode_map_entry(
        &wire,
        START,
        2,
        &mut limits,
        |source, offset, depth, limits| {
            child_called = true;
            assert!(core::ptr::eq(source, wire.as_slice()));
            assert!(offset == END && depth == 2);
            let (major, argument, next) = read_value_head(source, offset, limits)?;
            assert!(major == 7 && next == NEXT);
            Ok((decode_simple(argument)?.into_value(), next))
        },
        |span| {
            observed = Some((span.start, span.end));
            if rejected {
                Err(DeterministicCborError::DuplicateMapKey)
            } else {
                Ok(())
            }
        },
    );
    let result = core::mem::ManuallyDrop::new(result);
    assert!(observed == Some((START, END)));
    assert!(child_called != rejected);
    if rejected {
        assert!(matches!(
            &*result,
            Err(DeterministicCborError::DuplicateMapKey)
        ));
        kani::cover!(
            rejected,
            "key admission error stops before decoding the child"
        );
        return;
    }
    let Ok((entry, next)) = &*result else {
        kani::assert(false, "entry parses");
        return;
    };
    assert!(*next == NEXT);
    let expected = if bit { example.second } else { example.first };
    assert_text_entry(entry, expected, bit);
    assert!(limits.nodes == if bit { 7 } else { 5 });
    assert!(limits.pending_nodes == if bit { 0 } else { 2 });
    assert!(limits.aggregate_text_bytes == if bit { 2 } else { 1 });
    kani::cover!(
        expected == 0x7f,
        "highest single ASCII byte reaches admission"
    );
}

fn assert_text_entry(entry: &DeterministicCborMapEntry, expected: u8, bit: bool) {
    assert!(matches!(entry.key(), DeterministicCborMapKey::Text(key)
        if matches!(key.as_bytes(), [byte] if *byte == expected)));
    assert!(matches!(entry.value(), DeterministicCborValue::Bool(actual) if *actual == bit));
}

#[kani::proof]
#[kani::unwind(3)]
fn two_map_layers_retain_the_leaf_and_consume_the_wire() {
    let bit: bool = kani::any();
    let wire = [0xa1, 0x60, 0xa1, 0x60, 0xf4 | u8::from(bit)];
    let mut limits = DecodeLimits::default();
    let decoded = decode_proof_map(
        &wire,
        0,
        0,
        1,
        &mut limits,
        |wire, offset, depth, limits| {
            assert!(offset == 2 && depth == 1);
            decode_proof_map(
                wire,
                offset,
                depth,
                1,
                limits,
                |wire, offset, depth, limits| {
                    assert!(offset == 4 && depth == 2);
                    let (major, arg, end) = read_value_head(wire, offset, limits)?;
                    assert!(major == 7);
                    Ok((decode_simple(arg)?.into_value(), end))
                },
            )
        },
    );
    // Retain the returned tree so the harness ends at the parser's result.
    // The runtime zeroization tests exercise the owner's subsequent drop.
    let decoded = core::mem::ManuallyDrop::new(decoded);
    let Ok((outer, end)) = &*decoded else {
        kani::assert(false, "both map layers decode");
        return;
    };
    assert!(*end == wire.len());
    let Some(inner) = single_empty_key_child(outer) else {
        kani::assert(false, "outer map owns the inner map under its empty key");
        return;
    };
    assert!(
        matches!(single_empty_key_child(inner), Some(DeterministicCborValue::Bool(actual)) if *actual == bit)
    );
    assert!(limits.nodes == 5 && limits.pending_nodes == 0);
    kani::cover!(bit, "true leaf retained through both map layers");
    kani::cover!(!bit, "false leaf retained through both map layers");
}

#[kani::proof]
#[kani::unwind(4)]
fn nested_two_entry_key_spans_admit_only_increasing_keys() {
    let example = TwoEntryMap::symbolic();
    let wire = example.wire();
    let mut order = MapKeyOrder::default();
    assert!(admit_map_key(&mut order, &wire, 3, 5).is_ok());
    let result = admit_map_key(&mut order, &wire, 6, 8);
    match example.first.cmp(&example.second) {
        Ordering::Less => {
            assert_eq!(result, Ok(()));
            kani::cover!(
                example.first == 0 && example.second == 0x7f,
                "increasing keys"
            );
        }
        Ordering::Equal => {
            assert_eq!(result, Err(DeterministicCborError::DuplicateMapKey));
            kani::cover!(result.is_err(), "duplicate keys");
        }
        Ordering::Greater => {
            assert_eq!(result, Err(DeterministicCborError::MapKeysOutOfOrder));
            kani::cover!(result.is_err(), "descending keys");
        }
    }
}

#[kani::proof]
#[kani::unwind(3)]
fn one_byte_text_key_decodes_to_the_same_ascii_byte() {
    let example = TwoEntryMap::symbolic();
    let wire = [0x61, example.first, 0xf4];
    let mut limits = DecodeLimits::default();
    let decoded = decode_key(&wire, 0, &mut limits);
    assert!(
        matches!(&decoded, Ok((DeterministicCborMapKey::Text(key), 2))
        if matches!(key.as_bytes(), [byte] if *byte == example.first))
    );
    assert!(limits.nodes == 1 && limits.aggregate_text_bytes == 1);
    kani::cover!(example.first == 0x7f, "highest single ASCII byte");
}

fn decode_proof_map(
    wire: &[u8],
    offset: usize,
    depth: usize,
    expected_entries: u64,
    limits: &mut DecodeLimits,
    decode_child: impl FnMut(
        &[u8],
        usize,
        usize,
        &mut DecodeLimits,
    ) -> Result<(DeterministicCborValue, usize), DeterministicCborError>,
) -> Result<(DeterministicCborValue, usize), DeterministicCborError> {
    let (major, argument, start) = read_value_head(wire, offset, limits)?;
    assert!(major == 5 && argument == expected_entries);
    decode_map(wire, start, depth, argument, limits, decode_child)
}

fn single_empty_key_child(value: &DeterministicCborValue) -> Option<&DeterministicCborValue> {
    let DeterministicCborValue::Map(entries) = value else {
        return None;
    };
    let [entry] = entries.as_slice() else {
        return None;
    };
    match entry.key() {
        DeterministicCborMapKey::Text(key) if key.is_empty() => Some(entry.value()),
        _ => None,
    }
}

#[kani::proof]
#[kani::unwind(9)]
fn map_key_admission_matches_length_first_order() {
    let previous: [u8; MAX_KEY_BYTES] = kani::any();
    let current: [u8; MAX_KEY_BYTES] = kani::any();
    let previous_len: usize = kani::any();
    let current_len: usize = kani::any();
    kani::assume((1..=MAX_KEY_BYTES).contains(&previous_len));
    kani::assume((1..=MAX_KEY_BYTES).contains(&current_len));

    // Equal-length octet strings order like unsigned big-endian integers.
    // Ignore the bytes outside the prefix by shifting them out of each rank.
    let previous_rank = u64::from_be_bytes(previous) >> ((MAX_KEY_BYTES - previous_len) * 8);
    let current_rank = u64::from_be_bytes(current) >> ((MAX_KEY_BYTES - current_len) * 8);
    let expected = if (previous_len, previous_rank) < (current_len, current_rank) {
        Ok(())
    } else if (previous_len, previous_rank) == (current_len, current_rank) {
        Err(MapKeyOrderError::Duplicate)
    } else {
        Err(MapKeyOrderError::Descending)
    };
    let mut order = MapKeyOrder::default();
    assert_eq!(order.admit(&previous[..previous_len]), Ok(()));
    let actual = order.admit(&current[..current_len]);
    kani::cover!(
        previous_len < current_len && previous[0] > current[0] && actual.is_ok(),
        "length takes precedence over the first byte"
    );
    kani::cover!(
        previous_len == current_len && actual.is_ok(),
        "equal-length keys in byte order"
    );
    kani::cover!(actual == Err(MapKeyOrderError::Duplicate), "duplicate key");
    kani::cover!(
        actual == Err(MapKeyOrderError::Descending),
        "descending key order"
    );
    assert_eq!(actual, expected);
}

#[kani::proof]
#[kani::unwind(10)]
fn integer_header_requires_the_shortest_encoding() {
    let mut bytes: [u8; MAX_HEADER_BYTES] = kani::any();
    let available: usize = kani::any();
    kani::assume(available <= bytes.len());
    // Both integer major types share the same unsigned wire argument. Keeping
    // the argument symbolic covers all u64 values, including the sign boundary.
    bytes[0] &= 0x3f;
    let additional = bytes[0] & 0x1f;
    let actual = read_head(&bytes[..available], 0);
    let width = match additional {
        0..=23 => 0,
        24 => 1,
        25 => 2,
        26 => 4,
        27 => 8,
        _ => 0,
    };
    let expected = if available == 0 {
        Err(DeterministicCborError::UnexpectedEnd)
    } else if additional >= 28 {
        Err(DeterministicCborError::UnsupportedAdditionalInfo)
    } else if available <= width {
        Err(DeterministicCborError::TruncatedArgument)
    } else {
        // Interpret the payload as one zero-padded big-endian integer. Its bit
        // length determines the only permitted extended encoding width.
        let end = width + 1;
        let argument = if width == 0 {
            u64::from(additional)
        } else {
            let mut padded = [0; 8];
            padded[8 - width..].copy_from_slice(&bytes[1..end]);
            u64::from_be_bytes(padded)
        };
        let preferred_width = match argument {
            0..=23 => 0,
            _ => (u64::BITS - argument.leading_zeros())
                .div_ceil(8)
                .next_power_of_two(),
        };
        if u32::try_from(width) == Ok(preferred_width) {
            Ok((bytes[0] >> 5, argument, end))
        } else {
            Err(DeterministicCborError::NonCanonicalInteger)
        }
    };
    kani::cover!(
        matches!(actual, Ok((0, _, 9))),
        "eight-byte unsigned argument"
    );
    kani::cover!(
        matches!(actual, Ok((1, _, 9))),
        "eight-byte negative argument"
    );
    kani::cover!(
        actual == Err(DeterministicCborError::NonCanonicalInteger),
        "overlong argument"
    );
    kani::cover!(
        actual == Err(DeterministicCborError::TruncatedArgument),
        "truncated argument"
    );
    assert_eq!(actual, expected);
}

#[kani::proof]
fn container_capacity_fits_remaining_source() {
    let count: usize = kani::any();
    let total: usize = kani::any();
    let offset: usize = kani::any();
    let is_map: bool = kani::any();
    // The array and map callers require at least one and two bytes per
    // entry respectively. Widen the reference arithmetic to avoid wraparound.
    let bytes_per_entry = if is_map { 2 } else { 1 };
    let required_end = wide(count) * wide(bytes_per_entry) + wide(offset);
    let expected = if offset > total {
        Err(DeterministicCborError::OffsetOverflow)
    } else if required_end > wide(total) {
        Err(DeterministicCborError::ContainerLengthExceedsInput)
    } else {
        Ok(())
    };
    let actual = bounded_capacity(count, total, offset, bytes_per_entry);
    kani::cover!(is_map && actual.is_ok(), "map admitted");
    kani::cover!(!is_map && actual.is_ok(), "array admitted");
    kani::cover!(
        actual == Err(DeterministicCborError::ContainerLengthExceedsInput),
        "entry count exceeds remaining bytes"
    );
    assert_eq!(actual, expected);
}

fn wide(value: usize) -> u128 {
    u128::from(u64::try_from(value).unwrap())
}