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 binding = env::current_dir().expect("missing working dir").join("crc-check.txt");
let file_on_disk = binding.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 binding = env::current_dir().expect("missing working dir").join("crc-check.txt");
 let file_on_disk = binding.to_str().unwrap();

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

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

Structs§

Digest
Represents a CRC Digest, which is used to compute CRC checksums.

Enums§

CrcAlgorithm
Supported CRC-32 and CRC-64 variants

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_file
Computes the CRC checksum for the given file using the specified algorithm.
get_calculator_target
Returns the target used to calculate the CRC checksum for the specified algorithm.