8000 GitHub - jrahman/crc-turbo: SIMD-accelerated CRC-16, CRC-32 and CRC-64 for every known variant, with a C-compatible library; a maintained fork of crc-fast · GitHub
[go: up one dir, main page]

Skip to content

About

SIMD-accelerated CRC-16, CRC-32 and CRC-64 for every known variant, with a C-compatible library; a maintained fork of crc-fast

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

crc-turbo

ci crates.io docs.rs

This is a fork

crc-turbo is a maintained fork of crc-fast by Don MacAskill (MIT OR Apache-2.0), taken at its 1.10.0 release. Nearly all of the code here is upstream's. The public Rust API is upstream's, so migrating is the dependency line plus crc_fast:: → crc_turbo:::

crc-turbo = "1.11"

What differs is recorded in the ## Fork section of the CHANGELOG: the security and hygiene fixes that were waiting on upstream, an AVX2 + VPCLMULQDQ tier for x86_64 CPUs that have 256-bit carry-less multiply but no AVX-512, checksum_combine in a few field multiplies with CrcCombine and checksum_zeros beside it, the generated-tables and digest-0-11 features, the CRC_TURBO_TIER tier pin, and the C library's symbols, which are crc_turbo_* so that crc-fast and crc-turbo can be linked into one binary.

The rest of this document is upstream's, adapted to the new name.

crc-fast's introduction

World's fastest generic CRC calculator for all known CRC-16, CRC-32, and CRC-64 variants, as well as bring-your-own custom parameters, using SIMD intrinsics, which can exceed 100GiB/s on modern systems.

Supports acceleration on aarch64, x86_64, and x86 architectures, plus has a safe non-accelerated table-based software fallback for others.

The crc crate is ~0.5GiB/s by default, so this is up to >220X faster.

This is unique, not just because of the performance, but also because I couldn't find a single generic SIMD-accelerated implementation (in any language) which worked for all known variants, using the Rocksoft model, especially the "non-reflected" variants.

So I wrote one. :)

Other languages

Supplies a C/C++ compatible library for use with other non-Rust languages.

Implementations using upstream crc-fast

Changes

See CHANGELOG.

Build & Install

Library

cargo build --release builds the Rust library. The C/C++ compatible dynamic and static libraries are built from capi/: cargo build --release --manifest-path capi/Cargo.toml.

CLI tools

There are some command-line tools available:

  • checksum calculates CRC checksums from the supplied string or file
  • get-custom-params generates the custom CRC parameters for the supplied Rocksoft model values
  • arch-check checks the current architecture's hardware acceleration features (primarily for debugging)

To build them, enable the cli feature: cargo build --features cli --release.

Everything

To build the Rust library and the CLI tools, use the --all-features flag: cargo build --all-features --release.

A very basic Makefile is supplied which supports make install to build and install the C libraries, header file, and CLI binaries to the local system. Specifying the DESTDIR environment variable will allow you to customize the install location.

DESTDIR=/my/custom/path make install

Features

The library supports various feature flags for different environments:

Default Features

  • std - Standard library support, includes alloc
  • ffi - the C functions declared in libcrc_turbo.h, which capi/ builds into the C libraries
  • panic-handler - provides a panic handler in a no_std build; an embedded application leaves it off and brings its own

Optional Features

  • alloc - Heap allocation support (enables Digest trait, custom CRC params, checksum combining)
  • cache - Caches generated constants for custom CRC parameters (requires alloc)
  • generated-tables - Builds each built-in algorithm's 16-slice lookup table on first use instead of storing all fifty in the binary (requires alloc); see Binary size
  • digest-0-11 - Implements digest 0.11's DynDigest for Digest, alongside digest 0.10's (requires alloc)
  • cli - Enables command-line tools (checksum, arch-check, get-custom-params)

Binary size

The table-driven software fallback holds a 16-slice lookup table for each of the fifty built-in algorithms, 664 KiB in all:

width tables each total
CRC-16 31 8 KiB 248 KiB
CRC-32 12 16 KiB 192 KiB
CRC-64 7 32 KiB 224 KiB
50 664 KiB

When the algorithm is a constant, link-time optimisation keeps only its table. When it is chosen at run time, as in Digest::new(algorithm) with a stored algorithm, the linker keeps all fifty. The generated-tables feature stores none of them: each algorithm's table is generated on its first software-path use and kept on the heap. A release build on aarch64 that picks one of three algorithms at run time goes from 1,062,560 to 385,584 bytes; one that names a single algorithm goes from 335,792 to 319,296 bytes. CPUs with a SIMD tier never take the software path, so there the tables are never built at all.

Building for no_std

For embedded targets without standard library:

# Minimal no_std (core CRC only, no heap)
cargo build --target thumbv7em-none-eabihf --no-default-features --lib

# With heap allocation (enables Digest, custom params)
cargo build --target thumbv7em-none-eabihf --no-default-features --features alloc --lib

# With caching (requires alloc)
cargo build --target thumbv7em-none-eabihf --no-default-features --features cache --lib

Tested on ARM Cortex-M (thumbv7em-none-eabihf, thumbv8m.main-none-eabihf) and RISC-V ( riscv32imac-unknown-none-elf).

Building for WASM

For WebAssembly targets:

# Minimal WASM
cargo build --target wasm32-unknown-unknown --no-default-features --lib

# With heap allocation (typical use case)
cargo build --target wasm32-unknown-unknown --no-default-features --features alloc --lib

The crate is a plain Rust library (rlib), so a browser package wraps it in a cdylib crate of its own; wasm-pack does not package crc-turbo directly.

Tested on wasm32-unknown-unknown, wasm32-wasip1, and wasm32-wasip2 targets.

Usage

Add crc-turbo = "1.11" to your Cargo.toml dependencies, which will enable every available optimization for the stable toolchain.

Fast helper functions

For the most common and popular CRC variants, there are specialized one-shot functions to make adoption easier and performance faster, particularly for smaller input sizes, since it reduces some of the overhead of the generic checksum path.

CRC-32/ISCSI

Also commonly known as crc32c in many, but not all, implementations.

use crc_turbo::crc32_iscsi;

let checksum = crc32_iscsi(b"123456789");

assert_eq!(checksum, 0xe3069283);

CRC-32/ISO-HDLC

Also commonly known as crc32 in many, but not all, implementations.

use crc_turbo::crc32_iso_hdlc;

let checksum = crc32_iso_hdlc(b"123456789");

assert_eq!(checksum, 0xcbf43926);

CRC-64/NVME

use crc_turbo::crc64_nvme;

let checksum = crc64_nvme(b"123456789");

assert_eq!(checksum, 0xae8b14860a799888);

Digest

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

Creates a Digest which can be updated over time, for stream processing, intermittent workloads, etc, enabling finalizing the checksum once processing is complete.

use crc_turbo::{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 Rust code.

use std::env;
use std::fs::File;
use crc_turbo::{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

Checksums a string.

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

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

assert_eq!(checksum, 0xcbf43926);

checksum_combine

Combines checksums from two different sources, which can be useful for distributed or multithreaded workloads, etc.

 use crc_turbo::{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);

CrcCombine

When the same length is combined many times, as with fixed-size blocks hashed in parallel, build the operator once. Each combine is then one field multiply: three carry-less multiplies on x86_64 and aarch64. uncombine runs it backwards, and zeros gives the CRC of that many zero bytes without reading them.

use crc_turbo::{checksum, CrcAlgorithm::Crc32IsoHdlc, CrcCombine};

let append_five = CrcCombine::new(Crc32IsoHdlc, 5);
let head = checksum(Crc32IsoHdlc, b"1234");
let tail = checksum(Crc32IsoHdlc, b"56789");

let whole = append_five.combine(head, tail);
assert_eq!(whole, 0xcbf43926);
assert_eq!(append_five.uncombine(whole, tail), Some(head));
assert_eq!(append_five.zeros(), checksum(Crc32IsoHdlc, &[0; 5]));

checksum_zeros

The CRC of len zero bytes, in time logarithmic in len.

use crc_turbo::{checksum, checksum_zeros, CrcAlgorithm::Crc64Nvme};

assert_eq!(checksum_zeros(Crc64Nvme, 4096), checksum(Crc64Nvme, &[0; 4096]));

checksum_file

Checksums a file, which will chunk through the file optimally, limiting RAM usage and maximizing throughput. Chunk size is optional.

 use crc_turbo::{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);

Custom CRC Parameters

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

Digest with custom parameters

Creates a Digest with custom CRC parameters for stream processing.

use crc_turbo::{Digest, 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 mut digest = Digest::new_with_params(custom_params);
digest.update(b"123456789");
let checksum = digest.finalize();

assert_eq!(checksum, 0xcbf43926);

che 10BC8 cksum_with_params

Checksums data using custom CRC parameters.

use crc_turbo::{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);

checksum_combine_with_params

Combines checksums from two different sources using custom CRC parameters.

use crc_turbo::{checksum_with_params, checksum_combine_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_1 = checksum_with_params(custom_params, b"1234");
let checksum_2 = checksum_with_params(custom_params, b"56789");
let checksum = checksum_combine_with_params(custom_params, checksum_1, checksum_2, 5);

assert_eq!(checksum, 0xcbf43926);

checksum_file_with_params

Checksums a file using custom CRC parameters, chunking through the file optimally.

use std::env;
use crc_turbo::{checksum_file_with_params, CrcParams};

// 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();

// 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_file_with_params(custom_params, file_on_disk, None);

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

Errors

CrcParams::new, and the functions that take a CrcAlgorithm, panic on misuse: a width other than 16, 32 or 64, or a Custom algorithm passed where parameters are needed. Each has a fallible form that returns a CrcError instead, for callers that would rather handle it than abort, which matters in no_std builds: CrcParams::try_new, try_checksum, Digest::try_new, Digest::try_new_with_init_state, CrcCombine::try_new and CrcCombine::try_new_with_params. Checksumming itself never fails.

use crc_turbo::{try_checksum, CrcAlgorithm, CrcError, CrcParams};

assert_eq!(
    CrcParams::try_new("CRC-8/CUSTOM", 8, 0x07, 0, false, 0, 0xf4).unwrap_err(),
    CrcError::UnsupportedWidth(8)
);
assert_eq!(
    try_checksum(CrcAlgorithm::CrcCustom, b"123456789").unwrap_err(),
    CrcError::MissingParams(CrcAlgorithm::CrcCustom)
);

C/C++ compatible library

cargo build --release --manifest-path capi/Cargo.toml produces a shared library (.so on Linux, .dll on Windows, .dylib on macOS) and a static library (.a on Linux and macOS, .lib on Windows) in capi/target/release/, to use with the auto-generated libcrc_turbo.h header file for use in non-Rust projects, such as through FFI. make capi-test builds both and runs a C smoke test against each. The C library lives in its own package so that a Rust dependency on crc-turbo is an ordinary library: Cargo builds every crate type a library declares, even for a dependency, and a static library needs a panic handler an embedded no_std application provides for itself.

Upstream's crc-fast PHP extension is an example of a consumer of that library (it links crc-fast's, whose symbols are crc_fast_* rather than crc_turbo_*).

Background

This 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.

This library 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.

Important CRC variants

While there are many variants, three stand out as being the most important and widely used (all of which are "reflected"):

Many, but not all, implementations simply call this crc32c and it's probably the 2nd most popular and widely used, after CRC-32/ISO-HDLC. It's used in iSCSI, ext4, btrfs, etc.

Both x86_64 and aarch64 have native hardware support for this CRC variant, so we can use fusion in many cases to accelerate it further by fusing SIMD CLMUL instructions with the native CRC instructions.

Many, but not all, implementations simply call this crc32 and it may be the most popular and widely used. It's used in Ethernet, PKZIP, xz, etc.

Only aarch64 has native hardware support for this CRC variant, so we can use fusion on that platform, but not x86_64.

CRC-64/NVME comes from the NVM Express® NVM Command Set Specification (Revision 1.0d, December 2023), is AWS S3's recommended checksum option (as CRC64-NVME), and has also been implemented in the Linux kernel (where it's been called CRC-64/Rocksoft in the past).

Note that the Check value in the NVMe spec uses incorrect endianness (see Section 5.2.1.3.4, Figure 120, page 83) but all known public & private implementations agree on the correct value, which this library produces.

Acceleration targets

This library has baseline support for accelerating all known CRC-16, CRC-32, and CRC-64 variants on aarch64, x86_64, and x86 internally in pure Rust.

It uses the best available acceleration method for the detected CPU features at runtime, including:

  • aarch64:
    • neon-pmull-sha3 (preferred, if available)
    • neon-pmull
  • x86_64 and x86:
    • avx512-vpclmulqdq (preferred, if available)
    • avx512-pclmulqdq
    • avx2-vpclmulqdq (x86_64 CPUs with 256-bit VPCLMULQDQ and no AVX-512: AMD Zen 3, Intel Alder Lake through Arrow Lake)
    • sse-pclmulqdq

There is a safe table-based software fallback for other architectures, or if no acceleration features are detected.

Checking your platform capabilities

There's an arch-check binary which will explain the selected target architecture.

// test it works on your system (patches welcome!)
cargo test

// examine the chosen acceleration targets
cargo run arch-check

// build for release
cargo build --release

Pinning a tier

With the std feature, the CRC_TURBO_TIER environment variable pins the tier, for comparing one tier against another on the same binary or running a narrower tier than the CPU would select. Its value is a target string as arch-check and get_calculator_target report it, such as x86_64-avx2-vpclmulqdq, x86_64-sse-pclmulqdq or software-fallback-tables. It is read once, when the first CRC is computed. It only ever narrows the choice: a tier the CPU cannot run, or a value that names no tier, is ignored. The pin covers the CRC-32/ISCSI and CRC-32/ISO-HDLC fusion kernels as well: a tier below AVX-512 or below SHA3 runs their SSE or PMULL forms.

CRC_TURBO_TIER=x86_64-sse-pclmulqdq cargo bench

Minimum Supported Rust Version (MSRV)

This crate targets a stable-2 policy (ie, the latest stable Rust version minus 2 minor versions) as the Minimum Supported Rust Version (MSRV).

Bumping the rust-version will be considered a MINOR version bump.

We'll try to support even older versions when possible, but given the high-performance use of SIMD intrinsics and other modern Rust features, this is merely a stated goal. We'll move up to stable-2 ASAP when a sufficiently high performance improvement or necessary feature requires it.

Performance

Modern systems can exceed 100 GiB/s for calculating CRC-32/ISCSI, and nearly 90 GiB/s for CRC-32/ISO-HDLC, CRC-64/NVME, and all other reflected variants. (Forward variants are slower, due to the extra shuffle-masking, but are still extremely fast in this library).

This is a summary of the performance for the most important and popular CRC checksums.

CRC-32/ISCSI (reflected)

AKA crc32c in many, but not all, implementations.

Arch Brand CPU System Target 1KiB (GiB/s) 1MiB (GiB/s)
x86_64 Intel Sapphire Rapids EC2 c7i.metal-24xl avx512-vpclmulqdq ~61 ~111
x86_64 AMD Genoa EC2 c7a.metal-48xl avx512-vpclmulqdq ~26 ~54
aarch64 AWS Graviton4 EC2 c8g.metal-48xl neon-pmull-sha3 ~23 ~54
aarch64 AWS Graviton2 EC2 c6g.metal neon-pmull ~11 ~17
aarch64 Apple M3 Ultra Mac Studio (32 core) neon-pmull-sha3 ~60 ~99
aarch64 Apple M4 Max MacBook Pro 16" (16 core) neon-pmull-sha3 ~56 ~94

CRC-32/ISO-HDLC (reflected)

AKA crc32 in many, but not all, implementations.

Arch Brand CPU System Target 1KiB (GiB/s) 1MiB (GiB/s)
x86_64 Intel Sapphire Rapids EC2 c7i.metal-248xl avx512-vpclmulqdq ~28 ~88
x86_64 AMD Genoa EC2 c7a.metal-48xl avx512-vpclmulqdq ~21 ~55
aarch64 AWS Graviton4 EC2 c8g.metal-48xl neon-pmull-sha3 ~23 ~54
aarch64 AWS Graviton2 EC2 c6g.metal neon-pmull ~11 ~17
aarch64 Apple M3 Ultra Mac Studio (32 core) neon-pmull-sha3 ~48 ~98
aarch64 Apple M4 Max MacBook Pro 16" (16 core) neon-pmull-sha3 ~56 ~94

CRC-64/NVME (reflected)

AWS S3's recommended checksum option

Arch Brand CPU System Target 1KiB (GiB/s) 1MiB (GiB/s)
x86_64 Intel Sapphire Rapids EC2 c7i.metal-24xl avx512-vpclmulqdq ~28 ~88
x86_64 AMD Genoa EC2 c7a.metal-48xl avx512-vpclmulqdq ~22 ~55
aarch64 AWS Graviton4 EC2 c8g.metal-48xl neon-pmull-sha3 ~28 ~41
aarch64 AWS Graviton2 EC2 c6g.metal neon-pmull ~11 ~16
aarch64 Apple M3 Ultra Mac Studio (32 core) neon-pmull-sha3 ~58 ~72
aarch64 Apple M4 Max MacBook Pro 16" (16 core) neon-pmull-sha3 ~52 ~72

CRC-32/BZIP2 (forward)

Arch Brand CPU System Target 1KiB (GiB/s) 1MiB (GiB/s)
x86_64 Intel Sapphire Rapids EC2 c7i.metal-24xl avx512-vpclmulqdq ~20 ~56
x86_64 AMD Genoa EC2 c7a.metal-48xl avx512-vpclmulqdq ~14 ~43
aarch64 AWS Graviton4 EC2 c8g.metal-48xl neon-pmull-eor3 ~18 ~40
aarch64 AWS Graviton2 EC2 c6g.metal neon-pmull ~9 ~14
aarch64 Apple M3 Ultra Mac Studio (32 core) neon-pmull-eor3 ~41 ~59
aarch64 Apple M4 Max MacBook Pro 16" (16 core) neon-pmull-eor3 ~47 ~64

CRC-64/ECMA-182 (forward)

Arch Brand CPU System Target 1KiB (GiB/s) 1MiB (GiB/s)
x86_64 Intel Sapphire Rapids EC2 c7i.metal-24xl avx512-vpclmulqdq ~21 ~56
x86_64 AMD Genoa EC2 c7a.metal-48xl avx512-vpclmulqdq ~14 ~43
aarch64 AWS Graviton4 EC2 c8g.metal-48xl neon-pmull-eor3 ~19 ~40
aarch64 AWS Graviton2 EC2 c6g.metal neon-pmull ~9 ~14
aarch64 Apple M3 Ultra Mac Studio (32 core) neon-pmull-eor3 ~40 ~59
aarch64 Apple M4 Max MacBook Pro 16" (16 core) neon-pmull-eor3 ~46 ~61

Other CRC widths

There are a lot of other known CRC widths and variants, ranging from CRC-3/GSM to CRC-82/DARC, and everything in between.

Since Awesome only uses CRC-32 or CRC-64 widths in our products, this library began by supporting only those widths, including all known variants plus support for custom Rocksoft parameters.

CRC-16 has since been added, including all known variants plus support for custom parameters as well.

In theory, much of the "heavy lifting" has been done, so it should be possible to add other widths with minimal effort.

PRs welcome!

Memory Safety

Given the heavy use of hardware intrinsics, this crate uses a decent amount of unsafe code.

To help ensure memory safety and stability, this crate is validated using Miri on x86_64 as well as fuzz tested using libFuzzer over millions of iterations.

References

License

crc-turbo, like crc-fast before it, is dual-licensed under

src/combine.rs also carries Mark Adler's crccomb.c, under the Zlib licence whose notice is kept in that file, so the crate's SPDX expression is (MIT OR Apache-2.0) AND Zlib.

About

SIMD-accelerated CRC-16, CRC-32 and CRC-64 for every known variant, with a C-compatible library; a maintained fork of crc-fast

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0