crc64fast_nvme/
lib.rs

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
// Copyright 2019 TiKV Project Authors. Licensed under MIT or Apache-2.0.

//! `crc64fast-nvme`
//! ===========
//!
//! SIMD-accelerated CRC-64/NVME computation
//! (similar to [`crc32fast`](https://crates.io/crates/crc32fast)).
//!
//! ## Usage
//!
//! ```
//! use crc64fast_nvme::Digest;
//!
//! let mut c = Digest::new();
//! c.write(b"hello ");
//! c.write(b"world!");
//! let checksum = c.sum64();
//! assert_eq!(checksum, 0xd9160d1fa8e418e3);
//! ```
//!
//! Tracking links for unstable features used here (which require nightly builds):
//!
//! - simd_ffi: https://github.com/rust-lang/rust/issues/27731
//! - link_llvm_intrinsics: https://github.com/rust-lang/rust/issues/29602
//! - avx512_target_feature: https://github.com/rust-lang/rust/issues/111137

#![cfg_attr(
    feature = "vpclmulqdq",
    feature(avx512_target_feature, stdarch_x86_avx512)
)]

use std::os::raw::c_char;
use std::slice;

mod pclmulqdq;
mod table;

type UpdateFn = unsafe fn(u64, &[u8]) -> u64;

/// Represents an in-progress CRC-64 computation.
#[derive(Clone)]
pub struct Digest {
    computer: UpdateFn,
    state: u64,
}

/// Begin functionality for building a C-compatible library
///
/// Opaque type for C for use in FFI
#[repr(C)]
pub struct DigestHandle(*mut Digest);

#[no_mangle]
pub extern "C" fn digest_new() -> *mut DigestHandle {
    let digest = Box::new(Digest::new());
    let handle = Box::new(DigestHandle(Box::into_raw(digest)));
    Box::into_raw(handle)
}

/// # Safety
///
/// Uses unsafe method calls
#[no_mangle]
pub unsafe extern "C" fn digest_write(handle: *mut DigestHandle, data: *const c_char, len: usize) {
    if handle.is_null() || data.is_null() {
        return;
    }

    let digest = &mut *(*handle).0;
    let bytes = slice::from_raw_parts(data as *const u8, len);
    digest.write(bytes);
}

/// # Safety
///
/// Uses unsafe method calls
#[no_mangle]
pub unsafe extern "C" fn digest_sum64(handle: *const DigestHandle) -> u64 {
    if handle.is_null() {
        return 0;
    }

    let digest = &*(*handle).0;
    digest.sum64()
}

/// # Safety
///
/// Uses unsafe method calls
#[no_mangle]
pub unsafe extern "C" fn digest_free(handle: *mut DigestHandle) {
    if !handle.is_null() {
        let handle = Box::from_raw(handle);
        let _ = Box::from_raw(handle.0);
    }
}

// end C-compatible library

impl Digest {
    /// Creates a new `Digest`.
    ///
    /// It will perform runtime CPU feature detection to determine which
    /// algorithm to choose.
    pub fn new() -> Self {
        Self {
            computer: pclmulqdq::get_update(),
            state: !0,
        }
    }

    /// Creates a new `Digest` using table-based algorithm.
    pub fn new_table() -> Self {
        Self {
            computer: table::update,
            state: !0,
        }
    }

    /// Writes some data into the digest.
    pub fn write(&mut self, bytes: &[u8]) {
        unsafe {
            self.state = (self.computer)(self.state, bytes);
        }
    }

    /// Computes the current CRC-64/NVME value.
    pub fn sum64(&self) -> u64 {
        !self.state
    }
}

impl Default for Digest {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::collection::size_range;
    use proptest::prelude::*;
    use std::ptr;

    // CRC-64/NVME
    //
    // NVM Express® NVM Command Set Specification (Revision 1.0d, December 2023)
    //
    // https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf
    //
    // Note: The Check value published in the spec is incorrect (Section 5.2.1.3.4, Figure 120, page 83).
    const CRC_NVME: crc::Algorithm<u64> = crc::Algorithm {
        width: 64,
        poly: 0xAD93D23594C93659,
        init: 0xFFFFFFFFFFFFFFFF,
        refin: true,
        refout: true,
        xorout: 0xFFFFFFFFFFFFFFFF,
        check: 0xae8b14860a799888,
        residue: 0x0000000000000000,
    };

    #[test]
    fn test_standard_vectors() {
        static CASES: &[(&[u8], u64)] = &[
            // from the NVM Express® NVM Command Set Specification (Revision 1.0d, December 2023),
            // Section 5.2.1.3.5, Figure 122, page 84.
            // https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf
            // and the Linux kernel
            // https://github.com/torvalds/linux/blob/f3813f4b287e480b1fcd62ca798d8556644b8278/crypto/testmgr.h#L3685-L3695
            (&[0; 4096], 0x6482d367eb22b64e),
            (&[255; 4096], 0xc0ddba7302eca3ac),

            // from our own internal tests, since the Check value in the  NVM Express® NVM Command
            // Set Specification (Revision 1.0d, December 2023) is incorrect (Section 5.2.1.3.4, Figure 120, page 83).
            (b"123456789", 0xae8b14860a799888),

            // updated values from the original CRC-64/XZ fork of this project
            (b"", 0),
            (b"@", 0x2808afa9582aa47),
            (b"1\x97", 0xb4af0ae0feb08e0f),
            (b"M\"\xdf", 0x85d7cd041a2a8a5d),
            (b"l\xcd\x13\xd7", 0x1860820ea79b0fa3),

            (&[0; 32], 0xcf3473434d4ecf3b),
            (&[255; 32], 0xa0a06974c34d63c4),
            (b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", 0xb9d9d4a8492cbd7f),

            (&[0; 1024], 0x691bb2b09be5498a),

            (b"hello world!", 0xd9160d1fa8e418e3),
        ];

        for (input, result) in CASES {
            let mut hasher = Digest::new();
            hasher.write(input);
            assert_eq!(hasher.sum64(), *result, "test case {:x?}", input);
        }
    }

    fn any_buffer() -> <Box<[u8]> as Arbitrary>::Strategy {
        any_with::<Box<[u8]>>(size_range(..65536).lift())
    }

    prop_compose! {
        fn bytes_and_split_index()
            (bytes in any_buffer())
            (index in 0..=bytes.len(), bytes in Just(bytes)) -> (Box<[u8]>, usize)
        {
            (bytes, index)
        }
    }

    proptest! {
        #[test]
        fn equivalent_to_crc(bytes in any_buffer()) {
            let mut hasher = Digest::new();
            hasher.write(&bytes);

            // CRC-64/NVME
            let crc = crc::Crc::<u64>::new(&CRC_NVME);
            let mut digest = crc.digest();
            digest.update(&bytes);

            prop_assert_eq!(hasher.sum64(), digest.finalize());
        }

        #[test]
        fn concatenation((bytes, split_index) in bytes_and_split_index()) {
            let mut hasher_1 = Digest::new();
            hasher_1.write(&bytes);
            let mut hasher_2 = Digest::new();
            let (left, right) = bytes.split_at(split_index);
            hasher_2.write(left);
            hasher_2.write(right);
            prop_assert_eq!(hasher_1.sum64(), hasher_2.sum64());
        }

        #[test]
        fn state_cloning(left in any_buffer(), right in any_buffer()) {
            let mut hasher_1 = Digest::new();
            hasher_1.write(&left);
            let mut hasher_2 = hasher_1.clone();
            hasher_1.write(&right);
            hasher_2.write(&right);
            prop_assert_eq!(hasher_1.sum64(), hasher_2.sum64());
        }
    }

    // test the FFI Digest functions
    #[test]
    fn test_ffi_digest_lifecycle() {
        unsafe {
            // Create new digest
            let handle = digest_new();
            assert!(!handle.is_null(), "Digest creation failed");

            // Write some data
            let data = b"hello world!";
            digest_write(handle, data.as_ptr() as *const c_char, data.len());

            // Get sum and verify against known value
            let sum = digest_sum64(handle);
            assert_eq!(sum, 0xd9160d1fa8e418e3, "CRC64 calculation incorrect");

            // Clean up
            digest_free(handle);
        }
    }

    #[test]
    fn test_ffi_null_handling() {
        unsafe {
            // Test null handle with write
            digest_write(ptr::null_mut(), b"test".as_ptr() as *const c_char, 4);

            // Test null data with valid handle
            let handle = digest_new();
            digest_write(handle, ptr::null(), 0);

            // Test null handle with sum64
            let sum = digest_sum64(ptr::null());
            assert_eq!(sum, 0, "Null handle should return 0");

            // Clean up
            digest_free(handle);
        }
    }

    #[test]
    fn test_ffi_empty_data() {
        unsafe {
            let handle = digest_new();

            // Write empty data
            digest_write(handle, b"".as_ptr() as *const c_char, 0);
            let sum = digest_sum64(handle);
            assert_eq!(sum, 0, "Empty data should produce 0");

            digest_free(handle);
        }
    }

    #[test]
    fn test_ffi_binary_data() {
        unsafe {
            let handle = digest_new();

            // Test with binary data including null bytes
            let data = [0u8, 1, 2, 3, 0, 4, 5, 0, 6];
            digest_write(handle, data.as_ptr() as *const c_char, data.len());

            // Write additional data to test streaming
            let more_data = [7u8, 8, 9];
            digest_write(handle, more_data.as_ptr() as *const c_char, more_data.len());

            let sum = digest_sum64(handle);
            assert_ne!(sum, 0, "Binary data should produce non-zero CRC");

            digest_free(handle);
        }
    }

    #[test]
    fn test_ffi_large_vectors() {
        unsafe {
            let zeros = vec![0u8; 4096];
            let ones = vec![255u8; 4096];

            let handle = digest_new();
            digest_write(handle, zeros.as_ptr() as *const c_char, zeros.len());
            let sum = digest_sum64(handle);
            assert_eq!(sum, 0x6482d367eb22b64e, "Failed on 4096 zeros");
            digest_free(handle);

            let handle = digest_new();
            digest_write(handle, ones.as_ptr() as *const c_char, ones.len());
            let sum = digest_sum64(handle);
            assert_eq!(sum, 0xc0ddba7302eca3ac, "Failed on 4096 ones");
            digest_free(handle);
        }
    }

    #[test]
    fn test_ffi_standard_strings() {
        unsafe {
            let test_cases: Vec<(&[u8], u64)> = vec![(b"123456789", 0xae8b14860a799888), (b"", 0)];

            for (input, expected) in test_cases {
                let handle = digest_new();
                digest_write(handle, input.as_ptr() as *const c_char, input.len());
                let sum = digest_sum64(handle);
                assert_eq!(sum, expected, "Failed on test vector: {:?}", input);
                digest_free(handle);
            }
        }
    }

    #[test]
    fn test_ffi_incremental_update() {
        unsafe {
            let handle = digest_new();

            // Write data incrementally
            let data = "hello world!";
            for byte in data.bytes() {
                digest_write(handle, &byte as *const u8 as *const c_char, 1);
            }

            let sum = digest_sum64(handle);
            assert_eq!(sum, 0xd9160d1fa8e418e3, "Incremental update failed");

            digest_free(handle);
        }
    }
}