OXYGENHUB
# 4476cdbb0443 oxygen / vendor / crc-fast / src / crc32 / mod.rs
2.8 KiB · Rust · last changed Strata: the cloud stores send through ureq, so every release target links 1 month ago source blame history raw
1
// Copyright 2025 Don MacAskill. Licensed under MIT or Apache-2.0.
2
3
//! This module provides CRC-32 support.
4
5
pub mod algorithm;
6
pub mod consts;
7
pub(crate) mod width32_ops;
8
9
#[cfg(all(
10
    feature = "std",
11
    any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64")
12
))]
13
pub(crate) mod fusion;
14
15
#[cfg(test)]
16
mod property_tests {
17
    use crate::test::miri_compatible_proptest_config;
18
    use crate::{checksum, CrcAlgorithm};
19
    use proptest::prelude::*;
20
21
    proptest! {
22
        #![proptest_config(miri_compatible_proptest_config())]
23
24
        /// Feature: crc16-hardware-acceleration, Property 4: CRC-32 and CRC-64 backwards compatibility
25
        /// *For any* existing CRC-32 or CRC-64 configuration and any input byte sequence, the
26
        /// computed checksum SHALL match the result from the `crc` crate reference implementation,
27
        /// ensuring no regression from CRC-16 changes.
28
        /// **Validates: Requirements 3.1, 3.2, 3.4**
29
        #[test]
30
        fn prop_crc32_compatibility(data in proptest::collection::vec(any::<u8>(), 0..1024)) {
31
            use crate::test::consts::{RUST_CRC32_ISCSI, RUST_CRC32_ISO_HDLC, RUST_CRC32_BZIP2, RUST_CRC32_MPEG_2};
32
33
            // Test CRC-32/ISCSI (reflected)
34
            let our_iscsi = checksum(CrcAlgorithm::Crc32Iscsi, &data);
35
            let ref_iscsi = RUST_CRC32_ISCSI.checksum(&data) as u64;
36
            prop_assert_eq!(
37
                our_iscsi, ref_iscsi,
38
                "CRC-32/ISCSI mismatch for {} bytes: our=0x{:08X}, ref=0x{:08X}",
39
                data.len(), our_iscsi, ref_iscsi
40
            );
41
42
            // Test CRC-32/ISO-HDLC (reflected)
43
            let our_hdlc = checksum(CrcAlgorithm::Crc32IsoHdlc, &data);
44
            let ref_hdlc = RUST_CRC32_ISO_HDLC.checksum(&data) as u64;
45
            prop_assert_eq!(
46
                our_hdlc, ref_hdlc,
47
                "CRC-32/ISO-HDLC mismatch for {} bytes: our=0x{:08X}, ref=0x{:08X}",
48
                data.len(), our_hdlc, ref_hdlc
49
            );
50
51
            // Test CRC-32/BZIP2 (forward/non-reflected)
52
            let our_bzip2 = checksum(CrcAlgorithm::Crc32Bzip2, &data);
53
            let ref_bzip2 = RUST_CRC32_BZIP2.checksum(&data) as u64;
54
            prop_assert_eq!(
55
                our_bzip2, ref_bzip2,
56
                "CRC-32/BZIP2 mismatch for {} bytes: our=0x{:08X}, ref=0x{:08X}",
57
                data.len(), our_bzip2, ref_bzip2
58
            );
59
60
            // Test CRC-32/MPEG-2 (forward/non-reflected)
61
            let our_mpeg2 = checksum(CrcAlgorithm::Crc32Mpeg2, &data);
62
            let ref_mpeg2 = RUST_CRC32_MPEG_2.checksum(&data) as u64;
63
            prop_assert_eq!(
64
                our_mpeg2, ref_mpeg2,
65
                "CRC-32/MPEG-2 mismatch for {} bytes: our=0x{:08X}, ref=0x{:08X}",
66
                data.len(), our_mpeg2, ref_mpeg2
67
            );
68
        }
69
    }
70
}