Expand description
§Base64 Turbo
A Base64 codec that peaks past 100 GiB/s, with every vector load and store of its SIMD kernels proven by the Kani model checker.
The best kernel is picked per call: AVX-512 VBMI or AVX2 on x86 via
runtime detection, NEON on aarch64, and a table-driven scalar kernel in
safe Rust everywhere else. no_std is supported.
§Usage
use base64_turbo::STANDARD;
let encoded = STANDARD.encode(b"Hello world");
assert_eq!(encoded, "SGVsbG8gd29ybGQ=");
assert_eq!(STANDARD.decode(&encoded).unwrap(), b"Hello world");The slice APIs write into a caller-provided buffer and need no allocator:
use base64_turbo::STANDARD;
let mut output = [0u8; 64];
let len = STANDARD.encode_slice(b"Raw bytes", &mut output).unwrap();
assert_eq!(&output[..len], b"UmF3IGJ5dGVz");§Custom Alphabets
Alphabet::new is a const fn, so its lookup tables are built at compile
time into a static:
use base64_turbo::{Alphabet, Engine};
// bcrypt / crypt(3)
static BCRYPT: Alphabet = match Alphabet::new(
b"./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
) {
Some(a) => a,
None => unreachable!(),
};
static ENGINE: Engine = Engine::custom(&BCRYPT, false);
assert_eq!(ENGINE.encode(b"hello"), "YETqZE6");Scalar and AVX-512 VBMI run custom alphabets at full speed; AVX2 and NEON
fall back to scalar for them. See Engine::custom.
§Padding
| Engine | Encode | Decode |
|---|---|---|
STANDARD, URL_SAFE | pads | requires padding |
STANDARD_NO_PAD, URL_SAFE_NO_PAD | no padding | rejects padding |
STANDARD_PAD_INDIFFERENT, URL_SAFE_PAD_INDIFFERENT | pads | accepts either (scalar only) |
§Feature Flags
| Feature | Default | Description |
|---|---|---|
std | Yes | String/Vec APIs. The slice APIs and every SIMD kernel work without it. |
unstable | No | Exposes the raw kernels (encode_avx2, decode_avx512_vbmi, …), compiled per architecture; each returns None where the CPU lacks it. |
§Selecting a Backend
By default every kernel the target architecture has is compiled in, and the
best one this CPU supports is picked at runtime (CPUID, read once, on x86;
NEON is baseline on aarch64). Target features enabled at compile time are
used without detection:
RUSTFLAGS='-C target-cpu=native' cargo build
RUSTFLAGS='-C target-feature=+avx512f,+avx512bw,+avx512vbmi' cargo buildThe kernels themselves are safe code: a kernel is entered only with a
token that exists where its CPU features are enabled or detected, and every
vector load and store moves a whole array of exactly its width. On targets
without SIMD kernels the crate is #![forbid(unsafe_code)].
§Safety & Verification
The README lists exactly what the Kani proofs, tests and fuzzing cover, and what they don’t.
Structs§
- Alphabet
- A Base64 alphabet and the lookup tables derived from it.
- Engine
- A stateless Base64 encoder/decoder: an alphabet plus a padding rule.
Enums§
- Error
- Errors returned by the encode and decode entry points.
Constants§
- STANDARD
- RFC 4648 §4 alphabet (
+,/) with=padding. - STANDARD_
NO_ PAD - RFC 4648 §4 alphabet (
+,/) without padding. - STANDARD_
PAD_ INDIFFERENT - Standard alphabet; pads on encode, accepts padded or unpadded input on decode.
- URL_
SAFE - RFC 4648 §5 URL-safe alphabet (
-,_) with=padding. - URL_
SAFE_ NO_ PAD - RFC 4648 §5 URL-safe alphabet (
-,_) without padding, as used by JWTs. - URL_
SAFE_ PAD_ INDIFFERENT - URL-safe alphabet; pads on encode, accepts padded or unpadded input on decode.