monero 0.21.0

Rust Monero Library.
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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
// Rust Monero Library
// Written in 2019-2023 by
//   Monero Rust Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//

use crate::blockdata::transaction::{ExtraField, KeyImage, RawExtraField, SubField, TxOutTarget};
use crate::consensus::encode::Encodable;
use crate::consensus::{deserialize, serialize};
use crate::cryptonote::hash::Hashable;
use crate::util::key::H;
use crate::util::ringct::{CtKey, EcdhInfo, Key, RctSig, RctSigBase, RctSigPrunable, RctType};
use crate::{
    Amount, Block, BlockHeader, Hash, PrivateKey, PublicKey, Transaction, TransactionPrefix, TxIn,
    TxOut, VarInt, ViewPair,
};
use hex::{FromHex, ToHex};
use std::io;
use std::str::FromStr;

/// Fuzz for block deserialization, called from the fuzz target
pub fn fuzz_block_deserialize(fuzz_data: &[u8]) -> bool {
    let fuzz_bytes = fuzz_data.to_vec();

    // Block
    if let Ok(val) = deserialize::<Block>(&fuzz_bytes[..]) {
        assert_eq!(fuzz_bytes, serialize(&val), "\nfuzz_data: {:?}", fuzz_data);
    }

    true
}

/// Fuzz for block header deserialization, called from the fuzz target
pub fn fuzz_block_header_deserialize(fuzz_data: &[u8]) -> bool {
    let fuzz_bytes = fuzz_data.to_vec();

    // BlockHeader
    if let Ok(val) = deserialize::<BlockHeader>(&fuzz_bytes[..]) {
        assert_eq!(fuzz_bytes, serialize(&val), "\nfuzz_data: {:?}", fuzz_data);
    }

    true
}

/// Fuzz for transaction prefix deserialization, called from the fuzz target
pub fn fuzz_transaction_prefix_deserialize(fuzz_data: &[u8]) -> bool {
    let fuzz_bytes = fuzz_data.to_vec();

    // TransactionPrefix
    if let Ok(val) = deserialize::<TransactionPrefix>(&fuzz_bytes[..]) {
        assert_eq!(fuzz_bytes, serialize(&val), "\nfuzz_data: {:?}", fuzz_data);
    }

    true
}

/// Padding sub-field position in extra data
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum AddPadding {
    /// Add padding to the front of the extra field
    ToFront,
    /// Add padding to the middle of the extra field
    ToMiddle,
    /// Add padding to the rear of the extra field
    ToRear,
}

fn u64_val_from_fuzz_data(fuzz_data: &[u8]) -> u64 {
    if fuzz_data.is_empty() {
        0
    } else {
        let mut vec = fuzz_data.to_vec().clone();
        vec.resize(8, 0);
        let mut bytes = [0u8; 8];
        bytes.copy_from_slice(vec.as_slice());
        u64::from_le_bytes(bytes)
    }
}

/// Fuzz helper function to create an extra field, called from the fuzz target
pub fn fuzz_create_extra_field(fuzz_data: &[u8], add_padding: AddPadding) -> ExtraField {
    let fuzz_bytes = fuzz_data.to_vec();
    let hash = Hash::new(fuzz_data);

    // SubField::TxPublicKey
    let tx_pub_key_field = SubField::TxPublicKey(match PublicKey::from_slice(fuzz_data) {
        Ok(val) => val,
        Err(_) => H,
    });
    let nonce_field = SubField::Nonce(fuzz_bytes.clone());

    // SubField::Padding
    let padding_field = SubField::Padding(fuzz_bytes.first().copied().unwrap_or_default());

    // SubField::MergeMining
    let u64_val = u64_val_from_fuzz_data(fuzz_data);
    let merge_mining_field = SubField::MergeMining(VarInt(u64_val), hash);

    // SubField::AdditionalPublickKey
    let additional_public_key_field =
        SubField::AdditionalPublickKey(match PublicKey::from_slice(fuzz_data) {
            Ok(val) => {
                if fuzz_bytes.is_empty() {
                    vec![]
                } else {
                    vec![val; (fuzz_bytes[0] % 10 + 1) as usize]
                }
            }
            Err(_) => vec![H],
        });

    // SubField::MysteriousMinerGate
    let mysterious_miner_gate_field = SubField::MysteriousMinerGate(fuzz_bytes);

    let sub_fields = match add_padding {
        AddPadding::ToFront => vec![
            padding_field, // This will fail if the padding is not maximum
            tx_pub_key_field,
            nonce_field,
            merge_mining_field,
            additional_public_key_field,
            mysterious_miner_gate_field,
        ],
        AddPadding::ToMiddle => vec![
            tx_pub_key_field,
            nonce_field,
            merge_mining_field,
            padding_field, // This will fail if the padding is not maximum
            additional_public_key_field,
            mysterious_miner_gate_field,
        ],
        AddPadding::ToRear => vec![
            tx_pub_key_field,
            nonce_field,
            merge_mining_field,
            additional_public_key_field,
            mysterious_miner_gate_field,
            padding_field,
        ],
    };

    // ExtraField
    ExtraField(sub_fields)
}

/// Fuzz for extra field's sub fields parse, called from the fuzz target
pub fn fuzz_extra_field_parse_sub_fields(extra_field: &ExtraField) -> bool {
    for sub_field in &extra_field.0 {
        let ser_sub_field = serialize(sub_field);
        match deserialize::<SubField>(&ser_sub_field) {
            Ok(des_sub_field) => {
                assert_eq!(sub_field, &des_sub_field, "\nsub field: {}", sub_field)
            }
            Err(err) => {
                panic!(
                    "Deserializing a serialized SubField may not fail\n({})\nsub field: {:?}",
                    err, sub_field
                )
            }
        }
    }

    true
}

/// Fuzz for extra field try parse, called from the fuzz target
pub fn fuzz_extra_field_try_parse(
    extra_field: &ExtraField,
    add_padding: AddPadding,
    fuzz_data: &[u8],
) -> bool {
    let raw_extra_field = RawExtraField::from(extra_field.clone());
    match ExtraField::try_parse(&raw_extra_field) {
        Ok(parsed_extra_field) => {
            assert_eq!(
                extra_field, &parsed_extra_field,
                "\nOn 'Ok(_)\noriginal: {:?}\nparsed:   {:?}\n'fuzz_data: {:?}",
                extra_field, parsed_extra_field, fuzz_data
            )
        }
        Err(parsed_extra_field) => {
            if parsed_extra_field.0.len() > extra_field.0.len() {
                panic!(
                    "On 'Err(_)', parsed extra field has to many sub fields\noriginal: {:?}\nparsed:   {:?}\nfuzz_data: {:?}",
                    extra_field,
                    parsed_extra_field,
                    fuzz_data,
                );
            }
            for parsed_sub_field in parsed_extra_field.0.iter() {
                match parsed_sub_field {
                    SubField::Padding(_) => {
                        // The padding sub-field may be different on error
                    }
                    _ => {
                        // Other sub-fields must be the same on error
                        extra_field.0.iter().find(|&x| x == parsed_sub_field).unwrap_or_else(|| {
                            panic!(
                                "On 'Err(_)', parsed sub field '{}' is not in original extra field\noriginal: {:?}\nparsed:   {:?}\nfuzz_data: {:?}",
                                parsed_sub_field,
                                extra_field,
                                parsed_extra_field,
                                fuzz_data,
                            )
                        });
                    }
                }
            }
            if add_padding == AddPadding::ToRear {
                panic!(
                    "\nOn 'Err(_)', parsing a serialized ExtraField with padding at the rear may not fail\n({:?})\n({:?})\nfuzz_data: {:?}",
                    extra_field,
                    parsed_extra_field,
                    fuzz_data,
                );
            }
        }
    };

    true
}

/// Fuzz helper function to create a raw extra field, called from the fuzz target
pub fn fuzz_create_raw_extra_field(fuzz_data: &[u8]) -> RawExtraField {
    let add_padding = if fuzz_data.is_empty() {
        AddPadding::ToMiddle
    } else {
        match fuzz_data.len() % 3 {
            0 => AddPadding::ToFront,
            1 => AddPadding::ToMiddle,
            _ => AddPadding::ToRear,
        }
    };
    RawExtraField::from(fuzz_create_extra_field(fuzz_data, add_padding))
}

/// Fuzz for transaction deserialization, called from the fuzz target
pub fn fuzz_transaction_deserialize(fuzz_data: &[u8]) -> bool {
    let fuzz_bytes = fuzz_data.to_vec();

    // Transaction
    if let Ok(val) = deserialize::<Transaction>(&fuzz_bytes[..]) {
        assert_eq!(fuzz_bytes, serialize(&val));
    }

    let raw_extra_field = fuzz_create_raw_extra_field(fuzz_data);

    let transaction = fuzz_create_transaction_alternative_1(fuzz_data, &raw_extra_field);
    let serialized_tx = serialize(&transaction);
    let _ = deserialize::<Transaction>(&serialized_tx[..]);

    let transaction = fuzz_create_transaction_alternative_2(fuzz_data, &raw_extra_field);
    let serialized_tx = serialize(&transaction);
    let _ = deserialize::<Transaction>(&serialized_tx[..]);

    true
}

/// Fuzz for transaction serialization and deserialization, called from the fuzz target
pub fn fuzz_transaction_components(fuzz_data: &[u8]) -> bool {
    let fuzz_bytes = fuzz_data.to_vec().clone();
    let (rct_type, inputs, outputs, mixin) = if fuzz_bytes.is_empty() {
        (RctType::Null, 0, 0, 0)
    } else {
        (
            match fuzz_bytes[0] % 8 {
                0 => RctType::Null,
                1 => RctType::Full,
                3 => RctType::Clsag,
                4 => RctType::Simple,
                5 => RctType::Bulletproof,
                6 => RctType::Bulletproof2,
                7 => RctType::BulletproofPlus,
                _ => RctType::Null,
            },
            fuzz_bytes[0] % 5,
            fuzz_bytes[0] % 7,
            fuzz_bytes[0] % 3,
        )
    };

    // TransactionPrefix
    if let Ok(val) = deserialize::<TransactionPrefix>(&fuzz_bytes[..]) {
        assert_eq!(fuzz_bytes, serialize(&val));
    }

    // RctSigBase
    let fuzz_bytes = fuzz_data.to_vec().clone();
    let mut decoder = io::Cursor::new(&fuzz_bytes);
    if let Ok(Some(rct_sig)) =
        RctSigBase::consensus_decode(&mut decoder, inputs as usize, outputs as usize)
    {
        let mut encoder = Vec::new();
        if rct_sig.consensus_encode(&mut encoder).is_ok() {
            // This fails! Should it?
            // assert_eq!(fuzz_bytes, encoder);
        }
    }

    // RctSigPrunable
    let fuzz_bytes = fuzz_data.to_vec().clone();
    let mut decoder = io::Cursor::new(&fuzz_bytes);
    if let Ok(Some(rct_sig)) = RctSigPrunable::consensus_decode(
        &mut decoder,
        rct_type,
        inputs as usize,
        outputs as usize,
        mixin as usize,
    ) {
        let mut encoder = Vec::new();
        if rct_sig.consensus_encode(&mut encoder, rct_type).is_ok() {
            // This fails! Should it?
            // assert_eq!(fuzz_bytes, encoder);
        }
    }

    true
}

/// Fuzz helper function to create a transaction, called from the fuzz target
pub fn fuzz_create_transaction_alternative_1(
    fuzz_data: &[u8],
    raw_extra_field: &RawExtraField,
) -> Transaction {
    let hash_1 = Hash::new(fuzz_data);
    let hash_2 = Hash::new(hash_1.0);
    let hash_3 = Hash::new(hash_2.0);
    let hash_4 = Hash::new(hash_3.0);
    let hash_5 = Hash::new(hash_4.0);
    let hash_6 = Hash::new(hash_5.0);
    let hash_7 = Hash::new(hash_6.0);
    let hash_8 = Hash::new(hash_7.0);
    let u64_val = u64_val_from_fuzz_data(fuzz_data);

    let prefix = TransactionPrefix {
        version: VarInt(u64_val),
        unlock_time: VarInt(u64_val),
        inputs: vec![
            TxIn::ToKey {
                amount: VarInt(u64_val),
                key_offsets: vec![],
                k_image: KeyImage { image: hash_1 },
            },
            TxIn::Gen {
                height: VarInt(u64_val),
            },
        ],
        outputs: vec![
            TxOut {
                amount: VarInt(u64_val),
                target: TxOutTarget::ToKey { key: hash_2.0 },
            },
            TxOut {
                amount: VarInt(u64_val),
                target: TxOutTarget::ToTaggedKey {
                    key: hash_3.0,
                    view_tag: hash_1.0[0],
                },
            },
        ],
        extra: raw_extra_field.clone(),
    };

    let rct_signatures = RctSig {
        sig: Option::from(RctSigBase {
            rct_type: RctType::Full,
            txn_fee: Amount::from_pico(u64_val),
            pseudo_outs: vec![
                Key { key: hash_4.0 },
                Key { key: hash_8.0 },
                Key { key: hash_7.0 },
            ],
            ecdh_info: vec![
                EcdhInfo::Standard {
                    mask: Key { key: hash_5.0 },
                    amount: Key { key: hash_6.0 },
                },
                EcdhInfo::Standard {
                    mask: Key { key: hash_5.0 },
                    amount: Key { key: hash_6.0 },
                },
            ],
            out_pk: vec![CtKey {
                mask: Key { key: hash_7.0 },
            }],
        }),
        p: None,
    };

    Transaction {
        prefix,
        signatures: vec![],
        rct_signatures,
    }
}

/// Fuzz helper function to create a transaction, called from the fuzz target
pub fn fuzz_create_transaction_alternative_2(
    fuzz_data: &[u8],
    raw_extra_field: &RawExtraField,
) -> Transaction {
    let hash_1 = Hash::new(fuzz_data);
    let hash_2 = Hash::new(hash_1.0);
    let hash_3 = Hash::new(hash_2.0);
    let hash_4 = Hash::new(hash_3.0);
    let hash_5 = Hash::new(hash_4.0);
    let hash_6 = Hash::new(hash_5.0);
    let hash_7 = Hash::new(hash_6.0);
    let hash_8 = Hash::new(hash_7.0);
    let u64_val = u64_val_from_fuzz_data(fuzz_data);

    let prefix = TransactionPrefix {
        version: VarInt(u64_val),
        unlock_time: VarInt(u64_val),
        inputs: vec![
            // Adding this results in `Error: "attempt to subtract with overflow"`
            TxIn::ToKey {
                amount: VarInt(u64_val),
                key_offsets: vec![],
                k_image: KeyImage { image: hash_1 },
            },
            TxIn::Gen {
                height: VarInt(u64_val),
            },
            TxIn::ToKey {
                amount: VarInt(u64_val),
                key_offsets: vec![VarInt(u64_val)],
                k_image: KeyImage { image: hash_1 },
            },
        ],
        outputs: vec![
            TxOut {
                amount: VarInt(u64_val),
                target: TxOutTarget::ToKey { key: hash_2.0 },
            },
            TxOut {
                amount: VarInt(u64_val),
                target: TxOutTarget::ToTaggedKey {
                    key: hash_3.0,
                    view_tag: hash_1.0[0],
                },
            },
        ],
        extra: raw_extra_field.clone(),
    };

    let rct_signatures = RctSig {
        sig: Option::from(RctSigBase {
            rct_type: RctType::Full,
            txn_fee: Amount::from_pico(u64_val),
            pseudo_outs: vec![Key { key: hash_4.0 }, Key { key: hash_8.0 }],
            ecdh_info: vec![
                EcdhInfo::Standard {
                    mask: Key { key: hash_5.0 },
                    amount: Key { key: hash_6.0 },
                },
                EcdhInfo::Standard {
                    mask: Key { key: hash_5.0 },
                    amount: Key { key: hash_6.0 },
                },
            ],
            out_pk: vec![
                CtKey {
                    mask: Key { key: hash_7.0 },
                },
                CtKey {
                    mask: Key { key: hash_8.0 },
                },
            ],
        }),
        p: Option::from(RctSigPrunable {
            range_sigs: vec![],
            bulletproofs: vec![],
            bulletproofplus: vec![],
            MGs: vec![],
            Clsags: vec![],
            pseudo_outs: vec![],
        }),
    };

    Transaction {
        prefix,
        signatures: vec![],
        rct_signatures,
    }
}

/// Fuzz for hash conversion, called from the fuzz target
pub fn fuzz_hash_convert(fuzz_data: &[u8]) -> bool {
    // Hash
    let hash = Hash::new(fuzz_data);

    let hash_str: String = hash.encode_hex();
    if let Ok(hash2) = Hash::from_hex(hash_str.clone()) {
        assert_eq!(hash, hash2);
    }

    let hash_str_with_0x = format!("0x{hash_str}");
    if let Ok(hash2) = Hash::from_hex(hash_str_with_0x) {
        assert_eq!(hash, hash2);
    }

    assert_eq!(hash.as_scalar(), Hash::hash_to_scalar(fuzz_data));

    true
}

/// Fuzz for raw extra field deserialize, called from the fuzz target
pub fn fuzz_raw_extra_field_deserialize(raw_extra_field: &RawExtraField) -> bool {
    let raw_extra_field_bytes = serialize(raw_extra_field);
    if let Ok(raw_extra_field_2) = deserialize::<RawExtraField>(&raw_extra_field_bytes) {
        assert_eq!(raw_extra_field, &raw_extra_field_2);
    }

    true
}

/// Fuzz for raw extra field, called from the fuzz target
pub fn fuzz_raw_extra_field_from(fuzz_data: &[u8]) -> bool {
    let extra_field = fuzz_create_extra_field(fuzz_data, AddPadding::ToRear);
    let _ = RawExtraField::from(extra_field.clone());
    let _ = fuzz_create_raw_extra_field(fuzz_data);

    true
}

/// Fuzz for transaction hash, called from the fuzz target
pub fn fuzz_transaction_hash(transaction: &Transaction) -> bool {
    let _hash = transaction.hash();
    true
}

/// Fuzz for transaction check outputs, called from the fuzz target
pub fn fuzz_transaction_check_outputs(transaction: &Transaction) -> bool {
    let secret_view = match PrivateKey::from_str(
        "bcfdda53205318e1c14fa0ddca1a45df363bb427972981d0249d0f4652a7df07",
    ) {
        Ok(val) => val,
        Err(_) => {
            // This may not fail, otherwise the test cannot continue
            return true;
        }
    };
    let secret_spend = match PrivateKey::from_str(
        "e5f4301d32f3bdaef814a835a18aaaa24b13cc76cf01a832a7852faf9322e907",
    ) {
        Ok(val) => val,
        Err(_) => {
            // This may not fail, otherwise the test cannot continue
            return true;
        }
    };
    let public_spend = PublicKey::from_private_key(&secret_spend);
    let viewpair = ViewPair {
        view: secret_view,
        spend: public_spend,
    };

    let _ = transaction.check_outputs(&viewpair, 0..3, 0..3);
    true
}

#[cfg(test)]
mod tests {
    use crate::{Address, AddressType, Network};
    use quickcheck::QuickCheck;

    use crate::util::fuzz_utils::{
        fuzz_block_deserialize, fuzz_block_header_deserialize, fuzz_create_extra_field,
        fuzz_create_raw_extra_field, fuzz_create_transaction_alternative_1,
        fuzz_create_transaction_alternative_2, fuzz_extra_field_parse_sub_fields,
        fuzz_extra_field_try_parse, fuzz_hash_convert, fuzz_raw_extra_field_deserialize,
        fuzz_raw_extra_field_from, fuzz_transaction_check_outputs, fuzz_transaction_components,
        fuzz_transaction_deserialize, fuzz_transaction_hash, fuzz_transaction_prefix_deserialize,
        AddPadding,
    };

    #[test]
    fn test_fuzz_block_deserialize() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_block_deserialize(&data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_block_header_deserialize() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_block_header_deserialize(&data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_transaction_prefix_deserialize() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_transaction_prefix_deserialize(&data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_transaction_deserialize() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_transaction_deserialize(&data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_transaction_components() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_transaction_components(&data)
        }

        const TESTS: u64 = 1_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_hash_convert() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_hash_convert(&data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_raw_extra_field_from() {
        fn internal(data: Vec<u8>) -> bool {
            fuzz_raw_extra_field_from(&data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_raw_extra_field_deserialize() {
        fn internal(data: Vec<u8>) -> bool {
            let raw_extra_field = fuzz_create_raw_extra_field(&data);
            fuzz_raw_extra_field_deserialize(&raw_extra_field)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_extra_field_parse_sub_fields() {
        fn internal(data: Vec<u8>) -> bool {
            let add_padding = if data.is_empty() {
                AddPadding::ToMiddle
            } else {
                match data.len() % 3 {
                    0 => AddPadding::ToFront,
                    1 => AddPadding::ToMiddle,
                    _ => AddPadding::ToRear,
                }
            };
            let extra_field = fuzz_create_extra_field(&data, add_padding);
            fuzz_extra_field_parse_sub_fields(&extra_field)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_extra_field_try_parse() {
        fn internal(data: Vec<u8>) -> bool {
            let add_padding = if data.is_empty() {
                AddPadding::ToMiddle
            } else {
                match data.len() % 3 {
                    0 => AddPadding::ToFront,
                    1 => AddPadding::ToMiddle,
                    _ => AddPadding::ToRear,
                }
            };
            let extra_field = fuzz_create_extra_field(&data, add_padding);
            fuzz_extra_field_try_parse(&extra_field, add_padding, &data)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_transaction_hash() {
        fn internal(data: Vec<u8>) -> bool {
            let raw_extra_field = fuzz_create_raw_extra_field(&data);
            let transaction = fuzz_create_transaction_alternative_1(&data, &raw_extra_field);
            let _ = fuzz_transaction_hash(&transaction);
            let transaction = fuzz_create_transaction_alternative_2(&data, &raw_extra_field);
            fuzz_transaction_hash(&transaction)
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_transaction_check_outputs() {
        fn internal(data: Vec<u8>) -> bool {
            let raw_extra_field = fuzz_create_raw_extra_field(&data);
            let transaction = fuzz_create_transaction_alternative_1(&data, &raw_extra_field);
            let _ = fuzz_transaction_check_outputs(&transaction);
            let transaction = fuzz_create_transaction_alternative_2(&data, &raw_extra_field);
            let _ = fuzz_transaction_check_outputs(&transaction);
            true
        }

        const TESTS: u64 = 1_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_address_from_bytes() {
        fn internal(data: Vec<u8>) -> bool {
            let _ = Address::from_bytes(&data);
            true
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    #[test]
    fn test_fuzz_address_type_from_slice() {
        fn internal(data: Vec<u8>) -> bool {
            let network = if data.is_empty() {
                Network::Mainnet
            } else {
                match data.len() % 3 {
                    0 => Network::Mainnet,
                    1 => Network::Testnet,
                    _ => Network::Stagenet,
                }
            };
            let _ = AddressType::from_slice(&data, network);
            true
        }

        const TESTS: u64 = 10_000;

        QuickCheck::new()
            .min_tests_passed(TESTS)
            .tests(TESTS)
            .max_tests(TESTS)
            .quickcheck(internal as fn(Vec<u8>) -> bool);
    }

    // ---------------------------------------------------------------------------------------------
    // Code coverage section
    // ---------------------------------------------------------------------------------------------
    #[test]
    fn test_fuzz_block_deserialize_coverage() {
        let data = [
            12, 12, 148, 222, 186, 248, 5, 190, 179, 72, 156, 114, 42, 40, 92, 9, 42, 50, 231, 198,
            137, 58, 191, 199, 208, 105, 105, 156, 131, 38, 252, 52, 69, 167, 73, 197, 39, 107, 98,
            0, 0, 0, 0, 2, 155, 137, 34, 1, 255, 223, 136, 34, 1, 182, 153, 212, 200, 177, 236, 2,
            2, 35, 223, 82, 74, 242, 162, 239, 95, 135, 10, 219, 110, 28, 235, 3, 164, 117, 195,
            159, 139, 158, 247, 106, 165, 11, 70, 221, 210, 161, 131, 73, 64, 43, 1, 40, 57, 191,
            161, 155, 117, 36, 236, 116, 136, 145, 119, 20, 194, 22, 202, 37, 75, 56, 237, 4, 36,
            202, 101, 174, 130, 138, 124, 0, 106, 234, 241, 2, 8, 245, 49, 106, 127, 107, 153, 204,
            166, 0, 0,
        ];
        fuzz_block_deserialize(&data);
    }

    #[test]
    fn test_fuzz_block_header_deserialize_coverage() {
        let data = [
            12, 12, 148, 222, 186, 248, 5, 190, 179, 72, 156, 114, 42, 40, 92, 9, 42, 50, 231, 198,
            137, 58, 191, 199, 208, 105, 105, 156, 131, 38, 252, 52, 69, 167, 73, 197, 39, 107, 98,
            0, 0, 0, 0,
        ];
        fuzz_block_header_deserialize(&data);
    }

    #[test]
    fn test_fuzz_transaction_prefix_deserialize_coverage() {
        let data = [
            2, 155, 137, 34, 1, 255, 223, 136, 34, 1, 182, 153, 212, 200, 177, 236, 2, 2, 35, 223,
            82, 74, 242, 162, 239, 95, 135, 10, 219, 110, 28, 235, 3, 164, 117, 195, 159, 139, 158,
            247, 106, 165, 11, 70, 221, 210, 161, 131, 73, 64, 43, 1, 40, 57, 191, 161, 155, 117,
            36, 236, 116, 136, 145, 119, 20, 194, 22, 202, 37, 75, 56, 237, 4, 36, 202, 101, 174,
            130, 138, 124, 0, 106, 234, 241, 2, 8, 245, 49, 106, 127, 107, 153, 204, 166,
        ];
        fuzz_transaction_prefix_deserialize(&data);
    }

    #[test]
    fn test_fuzz_extra_field_parse_sub_fields_coverage() {
        let extra_field_1 = fuzz_create_extra_field(&[0, 1, 2], AddPadding::ToFront);
        let extra_field_2 = fuzz_create_extra_field(&[0, 1, 2], AddPadding::ToMiddle);
        assert_ne!(extra_field_1, extra_field_2);

        let mut extra_field = fuzz_create_extra_field(&[], AddPadding::ToRear);
        extra_field.0.pop();
        extra_field.0.pop();
        fuzz_extra_field_parse_sub_fields(&extra_field);
    }

    #[test]
    fn test_fuzz_extra_field_try_parse_coverage() {
        let extra_field = fuzz_create_extra_field(&[], AddPadding::ToRear);
        fuzz_extra_field_try_parse(&extra_field, AddPadding::ToRear, &[]);
        let data = [
            230, 196, 73, 143, 43, 56, 217, 81, 1, 244, 76, 0, 106, 157, 99, 164, 0, 128, 107, 252,
            189, 156, 211, 217, 79, 231, 213, 136, 104, 65, 213, 255, 90, 255, 15, 64, 244, 201,
            135, 97, 135, 0, 21, 174, 185, 65, 184, 27, 229, 84, 182, 255, 236, 217, 32, 1, 63,
        ];
        let mut extra_field = fuzz_create_extra_field(&data, AddPadding::ToMiddle);
        extra_field.0.pop();
        fuzz_extra_field_try_parse(&extra_field, AddPadding::ToMiddle, &[]);
    }

    #[test]
    fn test_fuzz_transaction_deserialize_coverage() {
        use std::panic;
        let _result = panic::catch_unwind(|| {
            let data = [1];
            fuzz_transaction_deserialize(&data);
        });
    }

    #[test]
    fn test_fuzz_remaining_coverage() {
        let data = [1];
        fuzz_transaction_components(&data);
        fuzz_hash_convert(&data);
        let raw_extra_field = fuzz_create_raw_extra_field(&data);
        fuzz_raw_extra_field_deserialize(&raw_extra_field);
        fuzz_raw_extra_field_from(&data);
        fuzz_transaction_hash(&fuzz_create_transaction_alternative_2(
            &data,
            &raw_extra_field,
        ));
        fuzz_transaction_check_outputs(&fuzz_create_transaction_alternative_2(
            &data,
            &raw_extra_field,
        ));
    }
}