use crc_fast::{checksum, CrcAlgorithm::Crc32IsoHdlc};
210
211
let checksum = checksum(Crc32IsoHdlc, b"123456789");
212
213
assert_eq!(checksum, 0xcbf43926);
214
```
215
216
###checksum_combine
217
218
Combines checksums from two different sources, which can be useful for distributed or multithreaded workloads, etc.
219
220
```rust
221
use crc_fast::{checksum, checksum_combine, CrcAlgorithm::Crc32IsoHdlc};
222
223
let checksum_1 = checksum(Crc32IsoHdlc, b"1234");
224
let checksum_2 = checksum(Crc32IsoHdlc, b"56789");
225
let checksum = checksum_combine(Crc32IsoHdlc, checksum_1, checksum_2, 5);
226
227
assert_eq!(checksum, 0xcbf43926);
228
```
229
230
###checksum_file
231
232
Checksums a file, which will chunk through the file optimally, limiting RAM usage and maximizing throughput. Chunk size
233
is optional.
234
235
```rust
236
use crc_fast::{checksum_file, CrcAlgorithm::Crc32IsoHdlc};
237
238
// for example/test purposes only, use your own file path
239
let binding = env::current_dir().expect("missing working dir").join("crc-check.txt");
240
let file_on_disk = binding.to_str().unwrap();
241
242
let checksum = checksum_file(Crc32IsoHdlc, file_on_disk, None);
243
244
assert_eq!(checksum.unwrap(), 0xcbf43926);
245
```
246
247
##Custom CRC Parameters
248
249
For cases where you need to use CRC variants not included in the predefined algorithms, you can define custom CRC
250
parameters and use the `*_with_params` functions.
251
252
###Digest with custom parameters
253
254
Creates a `Digest` with custom CRC parameters for stream processing.
255
256
```rust
257
use crc_fast::{Digest, CrcParams};
258
259
// Define custom CRC-32 parameters (equivalent to CRC-32/ISO-HDLC)
260
let custom_params = CrcParams::new(
261
"CRC-32/CUSTOM",
262
32,
263
0x04c11db7,
264
0xffffffff,
265
true,
266
0xffffffff,
267
0xcbf43926,
268
);
269
270
let mut digest = Digest::new_with_params(custom_params);
271
digest.update(b"123456789");
272
let checksum = digest.finalize();
273
274
assert_eq!(checksum, 0xcbf43926);
275
```
276
277
###checksum_with_params
278
279
Checksums data using custom CRC parameters.
280
281
```rust
282
use crc_fast::{checksum_with_params, CrcParams};
283
284
// Define custom CRC-32 parameters (equivalent to CRC-32/ISO-HDLC)
285
let custom_params = CrcParams::new(
286
"CRC-32/CUSTOM",
287
32,
288
0x04c11db7,
289
0xffffffff,
290
true,
291
0xffffffff,
292
0xcbf43926,
293
);
294
295
let checksum = checksum_with_params(custom_params, b"123456789");
296
297
assert_eq!(checksum, 0xcbf43926);
298
```
299
300
###checksum_combine_with_params
301
302
Combines checksums from two different sources using custom CRC parameters.
303
304
```rust
305
use crc_fast::{checksum_with_params, checksum_combine_with_params, CrcParams};
306
307
// Define custom CRC-32 parameters (equivalent to CRC-32/ISO-HDLC)
308
let custom_params = CrcParams::new(
309
"CRC-32/CUSTOM",
310
32,
311
0x04c11db7,
312
0xffffffff,
313
true,
314
0xffffffff,
315
0xcbf43926,
316
);
317
318
let checksum_1 = checksum_with_params(custom_params, b"1234");
319
let checksum_2 = checksum_with_params(custom_params, b"56789");
320
let checksum = checksum_combine_with_params(custom_params, checksum_1, checksum_2, 5);
321
322
assert_eq!(checksum, 0xcbf43926);
323
```
324
325
###checksum_file_with_params
326
327
Checksums a file using custom CRC parameters, chunking through the file optimally.
328
329
```rust
330
use std::env;
331
use crc_fast::{checksum_file_with_params, CrcParams};
332
333
// for example/test purposes only, use your own file path
334
let binding = env::current_dir().expect("missing working dir").join("crc-check.txt");
335
let file_on_disk = binding.to_str().unwrap();
336
337
// Define custom CRC-32 parameters (equivalent to CRC-32/ISO-HDLC)
338
let custom_params = CrcParams::new(
339
"CRC-32/CUSTOM",
340
32,
341
0x04c11db7,
342
0xffffffff,
343
true,
344
0xffffffff,
345
0xcbf43926,
346
);
347
348
let checksum = checksum_file_with_params(custom_params, file_on_disk, None);
349
350
assert_eq!(checksum.unwrap(), 0xcbf43926);
351
```
352
353
##C/C++ compatible library
354
355
`cargo build` will produce a shared library target (`.so` on Linux, `.dll` on Windows, `.dylib` on macOS, etc) and an
356
auto-generated [libcrc_fast.h](libcrc_fast.h) header file for use in non-Rust projects, such as through
357
[FFI](https://en.wikipedia.org/wiki/Foreign_function_interface). It will also produce a static library target (`.a` on
358
Linux and macOS, `.lib` on Windows, etc) for projects
359
which prefer statically linking.
360
361
There is a [crc-fast PHP extension](https://github.com/awesomized/crc-fast-php-ext) using it, for example.
362
363
##Background
364
365
This implementation is based on Intel's
366
[Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction](https://web.archive.org/web/20131224125630/https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/fast-crc-computation-generic-polynomials-pclmulqdq-paper.pdf)
367
white paper, though it folds 8-at-a-time, like other modern implementations, rather than the 4-at-a-time as in Intel's
368
paper.
369
370
This library works on `aarch64`, `x86_64`, and `x86` architectures, and is hardware-accelerated and optimized for each
371
architecture.
372
373
Inspired by [`crc32fast`](https://crates.io/crates/crc32fast),
the [NVM Express® NVM Command Set Specification](https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf)
407
(Revision 1.0d, December 2023),
408
is [AWS S3's recommended checksum option](https://docs.aws.amazon.com/AmazonS3/latest/userguide/checking-object-integrity.html)
409
(as `CRC64-NVME`), and has also been implemented in the
| aarch64 | Apple | M3 Ultra | Mac Studio (32 core) | neon-pmull-eor3 | ~40 | ~59 |
526
| aarch64 | Apple | M4 Max | MacBook Pro 16" (16 core) | neon-pmull-eor3 | ~46 | ~61 |
527
528
##Other CRC widths
529
530
There are [a lot of other known CRC widths and variants](https://reveng.sourceforge.io/crc-catalogue/all.htm), ranging
531
from `CRC-3/GSM` to `CRC-82/DARC`, and everything in between.
532
533
Since [Awesome](https://awesome.co) only uses `CRC-32` or `CRC-64` widths in our products, this library began by supporting only those
534
widths, including all known variants plus support for custom [Rocksoft](http://www.ross.net/crc/download/crc_v3.txt)
535
parameters.
536
537
`CRC-16` has since been added, including all known variants plus support for custom parameters as well.
538
539
In theory, much of the "heavy lifting" has been done, so it should be possible to add other widths with minimal effort.
540
541
PRs welcome!
542
543
##Memory Safety
544
545
Given the heavy use of hardware intrinsics, this crate uses a decent amount of `unsafe` code.
546
547
To help ensure memory safety and stability, this crate is validated using [Miri](https://github.com/rust-lang/miri) on
548
`x86_64` as well as fuzz tested using [libFuzzer](https://github.com/rust-fuzz/libfuzzer) over millions of iterations.
549
550
##References
551
552
*[Catalogue of parametrised CRC algorithms](https://reveng.sourceforge.io/crc-catalogue/all.htm)
553
*[crc32-fast](https://crates.io/crates/crc32fast) Original `CRC-32/ISO-HDLC` (`crc32`) implementation in `Rust`.
554
*[crc64-fast](https://github.com/tikv/crc64fast) Original `CRC-64/XZ` implementation in `Rust`.
555
*[crc64fast-nvme](https://github.com/awesomized/crc64fast-nvme) Original `CRC-64/NVME` implementation in `Rust`.
556
*[Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction](https://web.archive.org/web/20131224125630/https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/fast-crc-computation-generic-polynomials-pclmulqdq-paper.pdf)
557
Intel's paper.
558
*[NVM Express® NVM Command Set Specification](https://nvmexpress.org/wp-content/uploads/NVM-Express-NVM-Command-Set-Specification-1.0d-2023.12.28-Ratified.pdf)
559
The NVMe spec, including `CRC-64-NVME` (with incorrect endian `Check` value in
560
`Section 5.2.1.3.4, Figure 120, page 83`).
561
*[CRC-64/NVME](https://reveng.sourceforge.io/crc-catalogue/all.htm#crc.cat.crc-64-nvme) The `CRC-64/NVME` quick
562
definition.
563
*[A PAINLESS GUIDE TO CRC ERROR DETECTION ALGORITHMS](http://www.ross.net/crc/download/crc_v3.txt) Best description of
564
CRC I've seen to date (and the definition of the Rocksoft model).