Crate crc_fast

Crate crc_fast 

Source
Expand description

§crc-fast

Hardware-accelerated CRC calculation for all known CRC-32 and CRC-64 variants using SIMD intrinsics which can exceed 100GiB/s for CRC-32 and 50GiB/s for CRC-64 on modern systems.

§Other languages

Supplies a C-compatible shared library for use with other non-Rust languages. See PHP extension example.

§Background

The implementation is based on Intel’s Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction, white paper though it folds 8-at-a-time, like other modern implementations, rather than the 4-at-a-time as in Intel’s paper.

Works on aarch64, x86_64, and x86 architectures, and is hardware-accelerated and optimized for each architecture.

Inspired by crc32fast, crc64fast, and crc64fast-nvme, each of which only accelerates a single, different CRC variant, and all of them were “reflected” variants.

In contrast, this library accelerates every known variant (and should accelerate any future variants without changes), including all the “non-reflected” variants.

§Usage

§Digest

Implements the digest::DynDigest trait for easier integration with existing code.

use crc_fast::{Digest, CrcAlgorithm::Crc32IsoHdlc};

let mut digest = Digest::new(Crc32IsoHdlc);
digest.update(b"1234");
digest.update(b"56789");
let checksum = digest.finalize();

assert_eq!(checksum, 0xcbf43926);

§Digest Write

Implements the std::io::Write trait for easier integration with existing code.

use std::env;
use std::fs::File;
use crc_fast::{Digest, CrcAlgorithm::Crc32IsoHdlc};

// for example/test purposes only, use your own file path
let file_path = env::current_dir().expect("missing working dir").join("crc-check.txt");
let file_on_disk = file_path.to_str().unwrap();

// actual usage
let mut digest = Digest::new(Crc32IsoHdlc);
let mut file = File::open(file_on_disk).unwrap();
std::io::copy(&mut file, &mut digest).unwrap();
let checksum = digest.finalize();

assert_eq!(checksum, 0xcbf43926);

§checksum

 use crc_fast::{checksum, CrcAlgorithm::Crc32IsoHdlc};

 let checksum = checksum(Crc32IsoHdlc, b"123456789");

 assert_eq!(checksum, 0xcbf43926);

§checksum_combine

 use crc_fast::{checksum, checksum_combine, CrcAlgorithm::Crc32IsoHdlc};

 let checksum_1 = checksum(Crc32IsoHdlc, b"1234");
 let checksum_2 = checksum(Crc32IsoHdlc, b"56789");
 let checksum = checksum_combine(Crc32IsoHdlc, checksum_1, checksum_2, 5);

 assert_eq!(checksum, 0xcbf43926);

§checksum_file

 use std::env;
 use crc_fast::{checksum_file, CrcAlgorithm::Crc32IsoHdlc};

 // for example/test purposes only, use your own file path
 let file_path = env::current_dir().expect("missing working dir").join("crc-check.txt");
 let file_on_disk = file_path.to_str().unwrap();

 let checksum = checksum_file(Crc32IsoHdlc, file_on_disk, None);

 assert_eq!(checksum.unwrap(), 0xcbf43926);

§Custom CRC Parameters

For cases where you need to use CRC variants not included in the predefined algorithms, you can define custom CRC parameters using CrcParams::new() and use the *_with_params functions.

§checksum_with_params

 use crc_fast::{checksum_with_params, CrcParams};

 // Define custom CRC-32 parameters (equivalent to CRC-32/ISO-HDLC)
 let custom_params = CrcParams::new(
     "CRC-32/CUSTOM",
     32,
     0x04c11db7,
     0xffffffff,
     true,
     0xffffffff,
     0xcbf43926,
 );

 let checksum = checksum_with_params(custom_params, b"123456789");

 assert_eq!(checksum, 0xcbf43926);

§no_std Support

Supports no_std environments. Use default-features = false in Cargo.toml.

Note: When using this library in a no_std environment, the final binary must provide:

  • A #[panic_handler] (e.g., via the panic-halt crate)
  • A #[global_allocator] if using the alloc feature

Structs§

CrcParams
Parameters for CRC computation, including polynomial, initial value, and other settings.
Digest
Represents a CRC Digest, which is used to compute CRC checksums.

Enums§

CrcAlgorithm
Supported CRC-32 and CRC-64 variants
CrcKeysStorage
Internal storage for CRC folding keys that can accommodate different array sizes. This enum allows future expansion to support larger folding distances while maintaining backwards compatibility with existing const definitions.

Functions§

checksum
Computes the CRC checksum for the given data using the specified algorithm.
checksum_combine
Combines two CRC checksums using the specified algorithm.
checksum_combine_with_params
Combines two CRC checksums using custom CRC parameters.
checksum_file
Computes the CRC checksum for the given file using the specified algorithm.
checksum_file_with_params
Computes the CRC checksum for the given file using custom CRC parameters.
checksum_with_params
Computes the CRC checksum for the given data using custom CRC parameters.
get_calculator_target
Returns the target used to calculate the CRC checksum for the specified algorithm.