zstd-native
Zstandard (libzstd 1.5.7), rewritten in Rust: the same compressed bytes as libzstd at every level, the same strictness on bad input, at or above libzstd's speed, without the C, the build scripts or the memory-safety risk. Sibling of xz-native.
Status: 0.1, the first release. Decompression and compression are complete and heavily tested; the encoder output is byte-identical to libzstd 1.5.7 for the same parameters. The API may still change before 1.0 (see Versioning). See the roadmap and the issues.
Why zstd-native
-
A supply chain you can audit. No C code, no build scripts and no binary blobs in the library: one dependency-free crate. C libzstd is used only by the benchmark and fuzz crates, as a speed and behaviour reference.
-
Memory safety on untrusted input. Decompressors parse hostile data.
unsafeis limited to benchmarked hot loops (bit readers, Huffman and sequence decoding, match finders) and the x86-64 assembly, each with a SAFETY argument, debug assertions and a safe fallback that theportablefeature selects. It's fuzzed against libzstd and checked with Miri. -
As fast as libzstd, or faster, and the fastest Rust zstd. One core of an AMD EPYC 7543, geometric mean over the Silesia corpus, against the
zstdcrate's libzstd build (-O3, BMI2, asm Huffman decoder). Machines, methods and per-file tables are in BENCHMARK.md:zstd-native speed (1.25x = 25% faster) vs C libzstd 1.5.7 vs structured-zstd 0.2.6 Compression, level 1 1.04x 1.06x Compression, level 3 (default) 1.05x 1.33x Compression, level 9 1.15x 1.18x Compression, level 19 1.30x 1.11x Decompression (frames of levels 1-19) 1.05-1.09x 1.20-1.29x Compression, 16 threads (levels 1-19) 1.03-1.26x The structured-zstd column was measured before the latest speedups (level 9 has gained about 10% since). structured-zstd is the fastest other pure-Rust zstd, and its compressed output differs from libzstd's. ruzstd decompresses 3-7x slower than zstd-native.
Against every C libzstd 1.5.7 build, including upstream's own gcc and clang builds and
-march=native:- Decompression: zstd-native is 1.05-1.09x faster at levels 1-19.
- Compression: it is 1.01-1.16x faster at levels 5-15 and 1.15x or
more at level 19. At levels 1-4 it is 1.04-1.05x faster than the
zstdcrate's build. - Multithreaded: it is 1.03-1.26x faster with 16 workers, one-shot or streaming.
Ports in other languages (Go, C#, Java, JavaScript) are slower at every level. They were measured once, as recorded in BENCHMARK.md.
-
Drop-in compatible. It compresses to the same bytes as libzstd for the same parameters. That covers every level, streaming with flushes, dictionaries, long distance matching and multithreading. Anything it decompresses, libzstd decompresses to the same output.
-
Builds anywhere.
cargo addand done: no C compiler, simple cross-compilation, wasm, andno_std+alloc.
Usage
[]
= "0.1"
The full API reference is on docs.rs; changes are in CHANGELOG.md.
let compressed = compress?; // same bytes as libzstd level 3
let data = decompress?;
Reuse tables and buffers across calls, and write into your own buffer:
let mut c = new;
let compressed = c.compress?;
let mut d = new;
let n = d.decompress_to_slice?;
Streaming with std::io. The Encoder writes the same bytes as libzstd's
ZSTD_compressStream2 for the same writes and flushes:
let mut out = Vecnew;
copy?;
let mut enc = new;
copy?;
enc.finish?;
Dictionaries, zstd-format (zstd --train) or raw content. They can be
loaded into a compressor, or prepared once and shared (libzstd's
ZSTD_CDict):
let mut c = with_dictionary?;
let compressed = c.compress?;
let prepared = new?;
let enc = with_prepared_dictionary;
let mut d = with_dictionary;
Multithreaded compression, with std. The output is the same as libzstd's
with ZSTD_c_nbWorkers set to the same number:
let mut c = new;
c.set_workers;
let compressed = c.compress?;
Frame options follow libzstd's defaults, and can be changed on
Compressor, StreamEncoder and Encoder:
- the content checksum is off by default (
ZSTD_c_checksumFlag); - the content size and the dictionary ID are written (
ZSTD_c_contentSizeFlag,ZSTD_c_dictIDFlag).
let mut c = new;
c.set_checksum; // like `zstd` the CLI, which writes one
let compressed = c.compress?;
Without std, the push-style
StreamDecoder
and
StreamEncoder
take input and output in pieces of any size, like ZSTD_decompressStream
and ZSTD_compressStream2.
Byte identity
For the same input and parameters, zstd-native writes the same bytes as libzstd 1.5.7. That holds one-shot and streaming, for every write and flush pattern, and covers:
- every level from the negative ones to 22 and every strategy, with libzstd's block splitters;
- dictionaries, attached, copied or reloaded as libzstd would;
- long distance matching, including where libzstd turns it on by itself (levels 16 and up, with large inputs);
- multithreaded compression, for the same worker count, job size and overlap.
It's tested on every dataset and size edge, and fuzzed with arbitrary
parameters (see How it's tested). Not ported yet:
prefix dictionaries (ZSTD_CCtx_refPrefix). Another libzstd release
can change libzstd's output, which is why the version names the release
it matches.
Untrusted input
Corrupt input returns an Error and never panics. zstd-native is never
more lenient than libzstd: anything it accepts, libzstd 1.5.7 accepts
with the same output. Where libzstd's own decoders disagree, it follows
the stricter one. Streaming decompression refuses frames that need a
window over 2^27 bytes (128 MiB), libzstd's default; set_window_log_max
changes the limit.
Supported
| Feature | Decode | Encode |
|---|---|---|
| Zstandard frames (RFC 8878): every block type, checksums, concatenated and skippable frames | yes | yes (byte-identical to libzstd) |
Levels -131072 to 22, every strategy (fast to btultra2), block splitters |
- | yes |
Dictionaries: zstd format and raw content, prepared (ZSTD_DDict / ZSTD_CDict) |
yes | yes |
| Long distance matching | yes | yes |
Multithreaded compression (ZSTD_c_nbWorkers) |
- | yes |
Streaming: std::io::Read / Write, push API without std |
yes | yes |
no_std + alloc |
yes | yes |
Prefix dictionaries (refPrefix) |
yes, as a raw-content Dictionary |
not yet |
| Legacy formats (zstd 0.x) | no | no |
How it's tested
The ports follow libzstd 1.5.7's sources, kept read-only in third_party/zstd.
| Layer | What it checks |
|---|---|
| Encoder byte identity | Compressed output is compared byte for byte with C libzstd 1.5.7 on every dataset and size edge. It covers every level and strategy, streaming with flushes and pledged sizes, reused contexts, dictionaries (loaded and prepared), long distance matching and multithreading. Every frame must decode with both libraries. |
| Differential decoding | Everything libzstd produces at levels -5 to 22, with long distance matching, dictionaries, checksums and every block-size edge, must decode to identical bytes. That holds through every API: one-shot, streaming with 1-byte buffers, and io::Read. |
| Corruption | Hundreds of thousands of mutated frames (bit flips, truncation, header damage), with debug assertions on. Each must return an error or the same output as libzstd, never panic, and never be accepted when libzstd rejects it. |
| Fuzzing | cargo-fuzz with libFuzzer, differential against libzstd. Four targets: arbitrary bytes into every decoder, streaming with arbitrary piece sizes and dictionaries, libzstd's output with arbitrary parameters, and arbitrary data and parameters through the encoder. They run on every push and PR and nightly. Before the release they ran for 3 hours on 56 cores, under AddressSanitizer and with the portable fallbacks. |
| SIMD and assembly paths | The whole suite also runs with --features portable, which turns off the BMI2 and assembly paths, so the safe fallbacks are tested on every platform. Hot-path unsafe has debug assertions, which the CI-profile tests and the fuzzers run with. |
| Miri | On x86-64 and big-endian s390x, over the portable code paths: a fast set on every push and PR, and every test weekly. |
CI (GitHub Actions, every PR):
- fmt, clippy with
-D warnings(also for aarch64 and the fuzz crate), docs with-D warnings - tests (run-time CPU selection and
portable) on Linux x64/arm64, Windows x64 and macOS arm64, plus 32-bit Windows (i686) - builds for
no_std(thumbv7em), wasm32, i686 and aarch64 - MSRV (Rust 1.89)
- the four fuzz targets and Miri
- cargo-deny (RustSec advisories, licenses, sources)
The OpenSSF Scorecard watches the workflows themselves: actions are pinned by SHA and tokens are read-only.
Versioning
The crate follows SemVer for its own API. The build metadata names the
libzstd release whose output and behaviour it matches: 0.1.0+zstd.1.5.7
is release 0.1.0, compatible with libzstd 1.5.7. Depend on it as
zstd-native = "0.1"; Cargo ignores the metadata.
Before 1.0, a minor release (0.2) may break code, and a patch release
(0.1.1) adds API or fixes bugs. Following a new libzstd release that
changes the compressed output is at least a minor release, with new
metadata (for example 0.2.0+zstd.1.5.8), never a patch. The minimum
Rust version (1.89) is raised only in minor releases. Git tags are
vX.Y.Z.
Development
third_party/zstd is the libzstd 1.5.7 source, the reference for
porting. It is never compiled into this crate. C libzstd (the zstd
crate) is used only by the benchmark and fuzz crates. Design notes and
the roadmap are in docs/PROPOSAL.md.
Sibling project
xz-native (crates.io) applies the same approach to xz/liblzma 5.8.4:
- pure Rust;
.xz,.lzmaand.lzdecoding, and.xzand.lzmaencoding byte-identical to liblzma;- the same strictness on bad input;
- at or above liblzma's speed.
The two crates share their design: run-time CPU dispatch with a portable
fallback, differential tests and fuzzing against the C library, and
versions that name the upstream release they match.
License
BSD-3-Clause, like zstd. zstd-native is a port of parts of it.