crc21-pure
Pure-stdlib CRC-21/CAN-FD (Bosch CAN-FD Specification v1.0 §3.2.1) — zero dependencies, single function, byte-exact against RevEng canonical check
0x0ed841.
⚡ Performance
| Workload | Time | Notes |
|---|---|---|
Canonical check crc21_canfd(b'123456789') |
≈ 4.2 µs/call | 100,000 iters on Python 3.11.15, time.perf_counter |
| 256-byte payload | ≈ 117 µs/call | 10,000 iters, bit-serial MSB-first |
Measured on Python 3.11.15 (Linux x86_64), single-thread, no warm-up tricks. Run the micro-benchmark yourself:
python3 -c "
import timeit
from crc21_pure import crc21_canfd
print('canonical:', round(timeit.timeit(lambda: crc21_canfd(b'123456789'), number=100000) * 10, 3), 'us/call')
print('256-byte :', round(timeit.timeit(lambda: crc21_canfd(b'\\xff'*256), number=10000) * 100, 3), 'us/call')
"
This package does not compete on raw throughput against compiled C extensions such as crcmod or hardware-accelerated CRC peripherals. It is a faithful, pure-stdlib reference implementation whose value is correctness, zero dependencies, and single-call ergonomics for embedded/automotive tooling that already has Python and cannot pull in python-can (30+ transitive deps) or crcmod (no CRC-21/CAN-FD entry bundled).
Quick Start
pip install git+https://github.com/prasad-a-abhishek/crc21-pure.git
from crc21_pure import crc21_canfd, verify_canfd
# Canonical RevEng check — returns 0x0ed841
crc = crc21_canfd(b"123456789")
assert crc == 0x0ed841
# Streaming: feed chunks, pass previous result as `seed`
mid = crc21_canfd(chunk_a, seed=0x000000)
crc = crc21_canfd(chunk_b, seed=mid)
# `verify_canfd` is an alias — same return type
assert verify_canfd(b"123456789") == crc21_canfd(b"123456789")
Why crc21-pure?
CRC-21/CAN-FD is the CRC used by the Bosch CAN with Flexible Data-Rate protocol (CAN-FD frames, ISO 11898-1:2015). It is parameterised in the RevEng catalogue as width=21, poly=0x102899, init=0x000000, refin=false, refout=false, xorout=0x000000, check=0x0ed841.
python-canis overkill. The reference Python library for CAN bus work pulls in 30+ transitive dependencies (canlib,pyxat,wrapt, …). For pure frame-CRC validation in CI, telemetry pipelines, or test rigs, you do not need any of that.crcmoddoes not bundle CRC-21/CAN-FD. The popularcrcmodlibrary exposes a genericmkCrcFunfactory but ships no preset for the 21-bit width, and constructing one by hand is error-prone (the spec table'sbyte << 16alignment is a well-known off-by-one trap — see source comment at lines 59–64).- No other zero-dep pure-Python implementation exists on PyPI. Searches for
crc21,canfd,bosch crcreturned only wrappers around C extensions or unrelated CRC widths.
crc21-pure is a 21-bit bit-serial MSB-first reference implementation matching the canonical RevEng check to the bit, with explicit alignment math, streaming via a seed argument, and Invariant-21 strict input validation (raises TypeError on None, int, str, etc.).
Key Features
- Zero runtime dependencies. Standard library only.
- Byte-exact against the Greg Cook / RevEng canonical check
0x0ed841(13/13 seed vectors pre-computed Python↔Cgcc -O2byte-exact). - Streaming via
seed. Chunkedcrc21_canfd(chunk, seed=previous_crc)is equivalent to single-shot over the concatenation. - Input validation. Accepts
bytes,bytearray,memoryview. RaisesTypeErrorwith a descriptive message forNone,int,str, etc. verify_canfdalias. Provided for symmetry with naming conventions in automotive test rigs.
API Reference
crc21_pure.crc21_canfd(data, seed=0x000000)
Compute the CRC-21/CAN-FD checksum of data.
| Parameter | Type | Default | Description |
|---|---|---|---|
data |
bytes, bytearray, or memoryview |
(required) | Input bytes to checksum. |
seed |
int |
0x000000 |
Initial CRC register value. Pass the previous chunk's result for streaming. |
Returns: int in [0, 0x1fffff].
Raises: TypeError if data is not bytes-like.
crc21_pure.verify_canfd(data, seed=0x000000)
Alias for crc21_canfd. Same signature, same return type, same validation. Provided for naming symmetry with other Bosch-CAN verification utilities.
Constants
| Name | Value | Meaning |
|---|---|---|
WIDTH |
21 |
CRC width in bits. |
POLY |
0x102899 |
Polynomial (normal form, MSB-first). |
TOPBIT |
0x100000 |
MSB mask (1 << (WIDTH - 1)). |
MASK |
0x1fffff |
Width mask (1 << WIDTH) - 1. |
__version__ |
"0.1.0" |
Package version (matches pyproject.toml). |
CLI
This package does not ship a CLI — crc21_canfd is the entire public surface.
License
CC0-1.0 — public domain dedication. See LICENSE.
CRC-21/CAN-FD algorithm and parameters © Bosch GmbH "CAN with Flexible Data-Rate: Specification" v1.0 (17 April 2012), §3.2.1. Reference test vector 0x0ed841 from Greg Cook's RevEng CRC catalogue (reveng.cat-3.0.txt), used under the catalogue's permissive terms.
Metadata
Release files for crc21-pure 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| crc21_pure-0.1.0.tar.gz | 18.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| crc21_pure-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.2 kB
Release files / crc21_pure-0.1.0.tar.gz
| Download URL | crc21_pure-0.1.0.tar.gz |
|---|---|
| Size | 18.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5dfbff5eef02b4d7a272dfe95f3348929ee32f90f4f160e4bd568cecf561d36b
|
|
BLAKE2b-256 checksum How to use checksums |
591a1fb48b6b54c714915e9e86567a60207add143d3aae853b992fa4938b8493
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|
Release files / crc21_pure-0.1.0-py3-none-any.whl
| Download URL | crc21_pure-0.1.0-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8231fd0ac5a8da6f8da2b95522b74a916b3d388bfc32ff909103371946a3b3e6
|
|
BLAKE2b-256 checksum How to use checksums |
fe6aa77594f7f46e83094eaf536ff40e41af46444122fa1b2c6bf3441a0af1d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.11.15
|