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
// SPDX-FileCopyrightText: 2026 ReallyMe Technologies Limited
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::{
    collect_map_entries, encode_bool, encode_map, encode_map_key, preflight_map, preflight_simple,
    sort_map_entries, try_vec_with_capacity, write_map_entries, DecoratedMapEntry, Preflight,
};
use crate::nested_map_proof_tests::{
    assert_mixed_key_wire, assert_mixed_length_wire, TwoEntryMap, MIXED_KEY_CANONICAL,
    MIXED_LENGTH_CANONICAL, TEXT_KEY_ENCODING, UNSIGNED_KEY, UNSIGNED_KEY_ENCODING,
};
use crate::{
    DeterministicCborInteger, DeterministicCborMapEntry, DeterministicCborMapKey,
    DeterministicCborValue,
};
use core::mem::ManuallyDrop;
use zeroize::Zeroizing;

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_unsigned_key_writer_emits_three_preferred_bytes() {
    let key = ManuallyDrop::new(unsigned_map_key());
    let Some(mut output) = proof_output(UNSIGNED_KEY_ENCODING.len()) else {
        return;
    };
    assert!(super::encode_key(&key, &mut output).is_ok());
    assert!(matches!(output.as_slice(), [0x19, 0x01, 0x00]));
    kani::cover!(
        output.len() == 3,
        "unsigned 256 written with its shortest header"
    );
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_unsigned_preflight_reserves_the_exact_key_width() {
    let key = ManuallyDrop::new(unsigned_map_key());
    let encoded = ManuallyDrop::new(encode_map_key(&key, |actual, output| {
        assert!(core::ptr::eq(actual, &*key));
        assert!(output.is_empty() && output.capacity() == UNSIGNED_KEY_ENCODING.len());
        // The separate unsigned-key writer establishes these exact bytes.
        output.extend_from_slice(UNSIGNED_KEY_ENCODING);
        Ok(())
    }));
    assert!(matches!(&*encoded, Ok(bytes) if bytes.as_slice() == UNSIGNED_KEY_ENCODING));
    kani::cover!(
        encoded.is_ok(),
        "unsigned key preflights and fits its buffer"
    );
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_preparation_pairs_both_encoded_keys_with_their_values() {
    let unsigned_first: bool = kani::any();
    let entries = ManuallyDrop::new(mixed_key_entries(unsigned_first));
    verify_entry_preparation(&entries, mixed_key_encodings(unsigned_first));
    kani::cover!(unsigned_first, "integer key prepared first");
    kani::cover!(!unsigned_first, "text key prepared first");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_sort_uses_length_first_from_either_entry_order() {
    let unsigned_first: bool = kani::any();
    let entries = ManuallyDrop::new(mixed_key_entries(unsigned_first));
    let mut prepared = prepare_proof_entries(&entries, mixed_key_encodings(unsigned_first));
    assert!(sort_map_entries(&mut prepared).is_ok());
    assert!(core::ptr::eq(
        prepared[0].entry,
        &entries[usize::from(unsigned_first)]
    ));
    assert!(core::ptr::eq(
        prepared[1].entry,
        &entries[usize::from(!unsigned_first)]
    ));
    assert!(prepared[0].encoded_key.as_slice() == TEXT_KEY_ENCODING);
    assert!(prepared[1].encoded_key.as_slice() == UNSIGNED_KEY_ENCODING);
    kani::cover!(unsigned_first, "sorting reverses bytewise order");
    kani::cover!(!unsigned_first, "sorting preserves length-first order");
}

#[kani::proof]
#[kani::unwind(5)]
fn mixed_key_inner_preflight_accounts_for_both_key_types() {
    let entries = ManuallyDrop::new(mixed_key_entries(false));
    let stats = preflight_map(&*entries, 1, |child, depth| {
        assert!(matches!(child, DeterministicCborValue::Bool(_)) && depth == 2);
        preflight_simple()
    });
    assert!(
        matches!(stats, Ok(stats) if stats.encoded_len == 8 && stats.nodes == 5
        && stats.aggregate_text_bytes == 1 && stats.aggregate_byte_string_bytes == 0)
    );
    kani::cover!(
        stats.is_ok(),
        "inner preflight establishes the outer callback summary"
    );
}

#[kani::proof]
#[kani::unwind(5)]
fn mixed_key_inner_writer_reproduces_every_byte_from_sorted_entries() {
    let entries = ManuallyDrop::new(mixed_key_entries(false));
    let prepared = Vec::from(prepare_proof_entries(&entries, mixed_key_encodings(false)));
    let Some(mut output) = proof_output(MIXED_KEY_CANONICAL.len()) else {
        return;
    };
    output.extend_from_slice(&[0xa1, 0x60]);
    write_inner_bool_entries(prepared, &mut output);
    assert_mixed_key_wire(&output);
    kani::cover!(
        output.len() == MIXED_KEY_CANONICAL.len(),
        "complete canonical bytes written"
    );
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_key_outer_writer_preserves_the_canonical_inner_suffix() {
    let Some(output) =
        write_outer_nested_entries(mixed_key_entries(false), &MIXED_KEY_CANONICAL, 1)
    else {
        return;
    };
    assert_mixed_key_wire(&output);
    kani::cover!(
        output.len() == MIXED_KEY_CANONICAL.len(),
        "outer writer preserves canonical bytes"
    );
}

fn unsigned_map_key() -> DeterministicCborMapKey {
    DeterministicCborMapKey::Integer(DeterministicCborInteger::unsigned(UNSIGNED_KEY))
}

fn mixed_key_entries(unsigned_first: bool) -> [DeterministicCborMapEntry; 2] {
    let text = DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::from("z")),
        DeterministicCborValue::Bool(false),
    );
    let unsigned =
        DeterministicCborMapEntry::new(unsigned_map_key(), DeterministicCborValue::Bool(true));
    if unsigned_first {
        [unsigned, text]
    } else {
        [text, unsigned]
    }
}

fn mixed_key_encodings(unsigned_first: bool) -> [&'static [u8]; 2] {
    if unsigned_first {
        [UNSIGNED_KEY_ENCODING, TEXT_KEY_ENCODING]
    } else {
        [TEXT_KEY_ENCODING, UNSIGNED_KEY_ENCODING]
    }
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_key_writer_emits_preferred_headers_and_payloads() {
    verify_mixed_length_key_writer("z", b"\x61z");
    kani::cover!(true, "one-byte text key encoded");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_long_key_writer_emits_preferred_header_and_payload() {
    verify_mixed_length_key_writer("aa", b"\x62aa");
    kani::cover!(true, "two-byte text key encoded");
}

fn verify_mixed_length_key_writer(text: &str, expected: &[u8]) {
    let key = ManuallyDrop::new(DeterministicCborMapKey::text(String::from(text)));
    let Some(mut output) = proof_output(expected.len()) else {
        return;
    };
    assert!(super::encode_key(&key, &mut output).is_ok());
    assert!(output.as_slice() == expected);
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_preflight_reserves_each_key_width() {
    let long: bool = kani::any();
    let text = if long { "aa" } else { "z" };
    let expected = mixed_length_key_bytes(long);
    let key = ManuallyDrop::new(DeterministicCborMapKey::text(String::from(text)));
    let encoded = ManuallyDrop::new(encode_map_key(&key, |actual, output| {
        assert!(core::ptr::eq(actual, &*key));
        assert!(output.is_empty() && output.capacity() == expected.len());
        // The separate key-writer check establishes this output.
        output.extend_from_slice(expected);
        Ok(())
    }));
    assert!(matches!(&*encoded, Ok(bytes) if bytes.as_slice() == expected));
    kani::cover!(long && encoded.is_ok(), "three-byte encoded key reserved");
    kani::cover!(!long && encoded.is_ok(), "two-byte encoded key reserved");
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_preparation_pairs_both_encoded_keys_with_their_values() {
    let long_first: bool = kani::any();
    let entries = ManuallyDrop::new(mixed_length_entries(long_first));
    verify_entry_preparation(&entries, mixed_length_key_encodings(long_first));
    kani::cover!(long_first, "long key prepared first");
    kani::cover!(!long_first, "short key prepared first");
}

fn verify_entry_preparation(entries: &[DeterministicCborMapEntry; 2], expected_keys: [&[u8]; 2]) {
    let mut visited = 0;
    let prepared = ManuallyDrop::new(collect_map_entries(entries, |key| {
        assert!(visited < entries.len() && core::ptr::eq(key, entries[visited].key()));
        // The key-writer checks establish these bytes for the original key.
        let encoded = expected_keys[visited];
        visited += 1;
        Ok(Zeroizing::new(encoded.to_vec()))
    }));
    let Ok(prepared) = &*prepared else {
        kani::assert(false, "both map entries are prepared");
        return;
    };
    let [first, second] = prepared.as_slice() else {
        kani::assert(false, "prepared map retains both entries");
        return;
    };
    assert!(visited == 2);
    assert!(core::ptr::eq(first.entry, &entries[0]));
    assert!(core::ptr::eq(second.entry, &entries[1]));
    assert!(first.encoded_key.as_slice() == expected_keys[0]);
    assert!(second.encoded_key.as_slice() == expected_keys[1]);
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_sort_moves_the_whole_short_entry_first() {
    let long_first: bool = kani::any();
    let entries = ManuallyDrop::new(mixed_length_entries(long_first));
    let mut prepared = prepare_proof_entries(&entries, mixed_length_key_encodings(long_first));
    assert!(sort_map_entries(&mut prepared).is_ok());
    assert!(core::ptr::eq(
        prepared[0].entry,
        &entries[usize::from(long_first)]
    ));
    assert!(core::ptr::eq(
        prepared[1].entry,
        &entries[usize::from(!long_first)]
    ));
    assert!(prepared[0].encoded_key.as_slice() == b"\x61z");
    assert!(prepared[1].encoded_key.as_slice() == b"\x62aa");
    kani::cover!(
        long_first,
        "sorting moves the short entry ahead of the long entry"
    );
    kani::cover!(!long_first, "sorting preserves canonical order");
}

#[kani::proof]
#[kani::unwind(5)]
fn mixed_length_inner_writer_reproduces_every_byte_from_sorted_entries() {
    let entries = ManuallyDrop::new(mixed_length_entries(false));
    let prepared = Vec::from(prepare_proof_entries(
        &entries,
        mixed_length_key_encodings(false),
    ));
    let Some(mut output) = proof_output(MIXED_LENGTH_CANONICAL.len()) else {
        return;
    };
    output.extend_from_slice(&[0xa1, 0x60]);
    write_inner_bool_entries(prepared, &mut output);
    assert_mixed_length_wire(&output);
    kani::cover!(output.len() == 10, "complete nested wire reproduced");
}

fn write_inner_bool_entries(prepared: Vec<DecoratedMapEntry<'_>>, output: &mut Vec<u8>) {
    assert!(
        write_map_entries(prepared, output, 2, |child, output, depth| {
            let DeterministicCborValue::Bool(bit) = child else {
                kani::assert(false, "inner writer receives a boolean");
                return Err(super::DeterministicCborError::PreflightLengthMismatch);
            };
            assert!(depth == 2);
            encode_bool(*bit, output)
        })
        .is_ok()
    );
}

#[kani::proof]
#[kani::unwind(4)]
fn mixed_length_outer_writer_preserves_the_canonical_inner_suffix() {
    let Some(output) =
        write_outer_nested_entries(mixed_length_entries(false), &MIXED_LENGTH_CANONICAL, 3)
    else {
        return;
    };
    assert_mixed_length_wire(&output);
    kani::cover!(
        output.len() == 10,
        "outer map preserves complete nested wire"
    );
}

fn write_outer_nested_entries(
    inner: [DeterministicCborMapEntry; 2],
    wire: &[u8; 10],
    text_bytes: usize,
) -> Option<ManuallyDrop<Zeroizing<Vec<u8>>>> {
    let entries = ManuallyDrop::new([DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::new()),
        DeterministicCborValue::Map(Vec::from(inner)),
    )]);
    let preflight = preflight_map(&*entries, 0, |child, depth| {
        assert!(core::ptr::eq(child, entries[0].value()) && depth == 1);
        // One map header, key widths of two and three, and two boolean leaves.
        Ok(Preflight {
            encoded_len: 8,
            nodes: 5,
            aggregate_text_bytes: text_bytes,
            aggregate_byte_string_bytes: 0,
        })
    });
    let Ok(stats) = preflight else {
        kani::assert(false, "outer map preflights");
        return None;
    };
    assert!(stats.encoded_len == 10 && stats.nodes == 7);
    let mut output = proof_output(stats.encoded_len)?;
    assert!(
        encode_map(&*entries, &mut output, 0, |child, output, depth| {
            assert!(core::ptr::eq(child, entries[0].value()) && depth == 1);
            assert!(output.capacity() == 10 && output.as_slice() == [0xa1, 0x60]);
            // The inner writer check establishes the suffix for these entries.
            output.extend_from_slice(&wire[2..]);
            Ok(())
        })
        .is_ok()
    );
    Some(output)
}

fn mixed_length_entries(long_first: bool) -> [DeterministicCborMapEntry; 2] {
    let short = DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::from("z")),
        DeterministicCborValue::Bool(false),
    );
    let long = DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::from("aa")),
        DeterministicCborValue::Bool(true),
    );
    if long_first {
        [long, short]
    } else {
        [short, long]
    }
}

fn mixed_length_key_bytes(long: bool) -> &'static [u8] {
    if long {
        b"\x62aa"
    } else {
        b"\x61z"
    }
}

fn mixed_length_key_encodings(long_first: bool) -> [&'static [u8]; 2] {
    [
        mixed_length_key_bytes(long_first),
        mixed_length_key_bytes(!long_first),
    ]
}

fn prepare_proof_entries<'a>(
    entries: &'a [DeterministicCborMapEntry; 2],
    encoded_keys: [&[u8]; 2],
) -> [DecoratedMapEntry<'a>; 2] {
    [
        DecoratedMapEntry {
            encoded_key: Zeroizing::new(encoded_keys[0].to_vec()),
            entry: &entries[0],
        },
        DecoratedMapEntry {
            encoded_key: Zeroizing::new(encoded_keys[1].to_vec()),
            entry: &entries[1],
        },
    ]
}

#[kani::proof]
#[kani::unwind(4)]
fn preparation_encodes_the_exact_key_bytes() {
    let example = TwoEntryMap::symbolic();
    let key = ManuallyDrop::new(DeterministicCborMapKey::text(String::from(char::from(
        example.first,
    ))));
    let result = ManuallyDrop::new(encode_map_key(&key, |actual, output| {
        // The existing key-encoding check establishes the bytes written.
        assert!(core::ptr::eq(actual, &*key));
        assert!(output.is_empty() && output.capacity() == 2);
        output.extend_from_slice(&[0x61, example.first]);
        Ok(())
    }));
    let Ok(bytes) = &*result else {
        kani::assert(false, "single map key prepares successfully");
        return;
    };
    assert!(matches!(bytes.as_slice(), [0x61, key] if *key == example.first));
    kani::cover!(example.first == 0, "lowest ASCII key prepares");
    kani::cover!(example.first == 0x7f, "highest ASCII key prepares");
}

#[kani::proof]
#[kani::unwind(4)]
fn preparation_collects_both_entries_in_source_order() {
    let example = TwoEntryMap::symbolic();
    let entries = ManuallyDrop::new(two_entry_values(&example));
    let mut calls = 0;
    let result = collect_map_entries(&*entries, |key| {
        // The key-preparation proof establishes the returned bytes. The loop
        // must pair those bytes with the entry whose key was passed in.
        assert!(calls < entries.len() && core::ptr::eq(key, entries[calls].key()));
        let byte = if calls == 0 {
            example.first
        } else {
            example.second
        };
        calls += 1;
        Ok(Zeroizing::new(vec![0x61, byte]))
    });
    let result = ManuallyDrop::new(result);
    let Ok(decorated) = &*result else {
        kani::assert(false, "both prepared entries are collected");
        return;
    };
    assert!(calls == 2);
    assert_prepared_pair(decorated, &entries, &example);
    kani::cover!(
        example.first == example.second,
        "duplicate keys reach sorting"
    );
    kani::cover!(
        example.first < example.second,
        "increasing keys reach sorting"
    );
    kani::cover!(
        example.first > example.second,
        "descending keys reach sorting"
    );
}

fn assert_prepared_pair(
    decorated: &[DecoratedMapEntry<'_>],
    entries: &[DeterministicCborMapEntry; 2],
    example: &TwoEntryMap,
) {
    let [first, second] = decorated else {
        kani::assert(false, "preparation retains both entries");
        return;
    };
    assert!(core::ptr::eq(first.entry, &entries[0]));
    assert!(core::ptr::eq(second.entry, &entries[1]));
    assert!(matches!(first.encoded_key.as_slice(), [0x61, key] if *key == example.first));
    assert!(matches!(second.encoded_key.as_slice(), [0x61, key] if *key == example.second));
}

#[kani::proof]
#[kani::unwind(4)]
fn outer_layer_passes_inner_map_to_child_after_its_prefix() {
    let bit: bool = kani::any();
    let inner = DeterministicCborValue::Map(vec![DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::new()),
        DeterministicCborValue::Bool(bit),
    )]);
    let entries = core::mem::ManuallyDrop::new([DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::new()),
        inner,
    )]);
    let preflight = preflight_map(&*entries, 0, |value, depth| {
        assert!(core::ptr::eq(value, entries[0].value()) && depth == 1);
        Ok(Preflight {
            encoded_len: 3,
            nodes: 3,
            ..Preflight::default()
        })
    });
    let Ok(preflight) = preflight else {
        kani::assert(false, "outer preflight succeeds");
        return;
    };
    assert!(preflight.encoded_len == 5 && preflight.nodes == 5);

    let Some(mut output) = proof_output(preflight.encoded_len) else {
        return;
    };
    assert!(
        encode_map(&*entries, &mut output, 0, |value, output, depth| {
            assert!(core::ptr::eq(value, entries[0].value()) && depth == 1);
            assert!(output.len() == 2 && output.capacity() == 5);
            assert!(output[0] == 0xa1 && output[1] == 0x60);
            output.extend_from_slice(&[0xa1, 0x60, 0xf4 | u8::from(bit)]);
            Ok(())
        })
        .is_ok()
    );
    // The inner writer check establishes this suffix. The outer writer must
    // leave the complete result in place after the callback returns.
    assert_nested_wire(&output, bit);
    kani::cover!(bit, "outer map with a true leaf in its child");
    kani::cover!(!bit, "outer map with a false leaf in its child");
}

#[kani::proof]
#[kani::unwind(4)]
fn single_map_layer_writes_header_key_and_child() {
    let bit: bool = kani::any();
    let entries = [DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::new()),
        DeterministicCborValue::Bool(bit),
    )];
    let stats = preflight_map(&entries, 1, |value, depth| {
        assert!(matches!(value, DeterministicCborValue::Bool(actual) if *actual == bit));
        assert!(depth == 2);
        preflight_simple()
    });
    assert!(matches!(
        stats,
        Ok(stats) if stats.encoded_len == 3 && stats.nodes == 3
            && stats.aggregate_text_bytes == 0 && stats.aggregate_byte_string_bytes == 0
    ));
    // The inner writer starts after the outer map header and key.
    let Some(mut output) = proof_output(5) else {
        return;
    };
    output.extend_from_slice(&[0xa1, 0x60]);
    assert!(
        encode_map(&entries, &mut output, 1, |value, output, depth| {
            let DeterministicCborValue::Bool(actual) = value else {
                kani::assert(false, "encoder receives the boolean leaf");
                return Err(super::DeterministicCborError::PreflightLengthMismatch);
            };
            assert!(depth == 2);
            encode_bool(*actual, output)
        })
        .is_ok()
    );
    assert_nested_wire(&output, bit);
    kani::cover!(bit, "true leaf written after key");
    kani::cover!(!bit, "false leaf written after key");
}

#[kani::proof]
#[kani::unwind(5)]
fn two_entry_inner_writer_preserves_the_canonical_wire() {
    let example = TwoEntryMap::symbolic();
    kani::assume(example.first < example.second);
    let entries = ManuallyDrop::new(two_entry_values(&example));
    let decorated = vec![
        DecoratedMapEntry {
            encoded_key: Zeroizing::new(vec![0x61, example.first]),
            entry: &entries[0],
        },
        DecoratedMapEntry {
            encoded_key: Zeroizing::new(vec![0x61, example.second]),
            entry: &entries[1],
        },
    ];
    let Some(mut output) = proof_output(TwoEntryMap::WIRE_LENGTH) else {
        return;
    };
    output.extend_from_slice(&[0xa1, 0x60]);
    assert!(
        write_map_entries(decorated, &mut output, 2, |value, output, depth| {
            let DeterministicCborValue::Bool(bit) = value else {
                kani::assert(false, "inner writer receives a boolean leaf");
                return Err(super::DeterministicCborError::PreflightLengthMismatch);
            };
            assert!(depth == 2);
            encode_bool(*bit, output)
        })
        .is_ok()
    );
    example.assert_wire(&output);
    kani::cover!(
        example.first == 0 && example.second == 0x7f,
        "boundary ASCII keys"
    );
}

#[kani::proof]
#[kani::unwind(5)]
fn outer_writer_preserves_the_two_entry_inner_suffix() {
    let example = TwoEntryMap::symbolic();
    kani::assume(example.first < example.second);
    let entries = ManuallyDrop::new([DeterministicCborMapEntry::new(
        DeterministicCborMapKey::text(String::new()),
        DeterministicCborValue::Map(Vec::from(two_entry_values(&example))),
    )]);
    let preflight = preflight_map(&*entries, 0, |value, depth| {
        assert!(core::ptr::eq(value, entries[0].value()) && depth == 1);
        // Two encoded keys, two boolean leaves and one map header account
        // for seven bytes. Each entry contributes two nodes and the map one.
        Ok(Preflight {
            encoded_len: 7,
            nodes: 5,
            aggregate_text_bytes: 2,
            aggregate_byte_string_bytes: 0,
        })
    });
    let Ok(preflight) = preflight else {
        kani::assert(false, "outer preflight succeeds");
        return;
    };
    assert!(preflight.encoded_len == TwoEntryMap::WIRE_LENGTH && preflight.nodes == 7);

    let Some(mut output) = proof_output(preflight.encoded_len) else {
        return;
    };
    assert!(
        encode_map(&*entries, &mut output, 0, |value, output, depth| {
            assert!(core::ptr::eq(value, entries[0].value()) && depth == 1);
            assert!(output.capacity() == TwoEntryMap::WIRE_LENGTH);
            assert!(matches!(output.as_slice(), [0xa1, 0x60]));
            output.extend_from_slice(&example.wire()[2..]);
            Ok(())
        })
        .is_ok()
    );
    example.assert_wire(&output);
    kani::cover!(
        example.first == 0 && example.second == 0x7f,
        "boundary ASCII keys"
    );
}

fn two_entry_values(example: &TwoEntryMap) -> [DeterministicCborMapEntry; 2] {
    [
        DeterministicCborMapEntry::new(
            DeterministicCborMapKey::text(String::from(char::from(example.first))),
            DeterministicCborValue::Bool(false),
        ),
        DeterministicCborMapEntry::new(
            DeterministicCborMapKey::text(String::from(char::from(example.second))),
            DeterministicCborValue::Bool(true),
        ),
    ]
}

#[kani::proof]
#[kani::unwind(4)]
fn one_byte_text_key_preflights_and_encodes_exact_bytes() {
    let example = TwoEntryMap::symbolic();
    let key = ManuallyDrop::new(DeterministicCborMapKey::text(String::from(char::from(
        example.first,
    ))));
    let stats = super::preflight_key(&key);
    assert!(
        matches!(stats, Ok(stats) if stats.encoded_len == 2 && stats.nodes == 1
        && stats.aggregate_text_bytes == 1 && stats.aggregate_byte_string_bytes == 0)
    );
    let Some(mut output) = proof_output(2) else {
        return;
    };
    assert!(super::encode_key(&key, &mut output).is_ok());
    assert!(matches!(output.as_slice(), [0x61, byte] if *byte == example.first));
    kani::cover!(example.first == 0x7f, "highest single ASCII byte");
}

#[kani::proof]
#[kani::unwind(4)]
fn two_prepared_keys_preserve_canonical_order_when_sorted() {
    let example = TwoEntryMap::symbolic();
    kani::assume(example.first < example.second);
    let entries = ManuallyDrop::new(two_entry_values(&example));
    let mut decorated = [
        DecoratedMapEntry {
            encoded_key: Zeroizing::new(vec![0x61, example.first]),
            entry: &entries[0],
        },
        DecoratedMapEntry {
            encoded_key: Zeroizing::new(vec![0x61, example.second]),
            entry: &entries[1],
        },
    ];
    assert!(sort_map_entries(&mut decorated).is_ok());
    assert!(core::ptr::eq(decorated[0].entry, &entries[0]));
    assert!(core::ptr::eq(decorated[1].entry, &entries[1]));
    assert!(matches!(decorated[0].encoded_key.as_slice(), [0x61, key] if *key == example.first));
    assert!(matches!(decorated[1].encoded_key.as_slice(), [0x61, key] if *key == example.second));
    kani::cover!(
        example.first == 0 && example.second == 0x7f,
        "boundary ASCII keys"
    );
}

fn proof_output(capacity: usize) -> Option<core::mem::ManuallyDrop<Zeroizing<Vec<u8>>>> {
    let Ok(output) = try_vec_with_capacity(capacity) else {
        kani::assert(false, "bounded output reservation succeeds in the verifier");
        return None;
    };
    // The proof observes returned bytes. The ordinary owner is tested
    // separately for erasure when dropped.
    Some(core::mem::ManuallyDrop::new(Zeroizing::new(output)))
}

fn assert_nested_wire(output: &[u8], bit: bool) {
    assert!(output.len() == 5);
    assert!(output[0] == 0xa1 && output[1] == 0x60 && output[2] == 0xa1 && output[3] == 0x60);
    assert!(output[4] == (0xf4 | u8::from(bit)));
}