Skip to content

feat: add validation-only validate API - #28

Merged
tisonkun merged 2 commits into
mainfrom
feat/validate
Nov 22, 2026
Merged

tisonkun merged 2 commits into
mainfrom
feat/validate

Conversation

@tisonkun

Copy link
Copy Markdown
Contributor

Summary

Add validate(input: impl AsRef<[u8]>) -> Result<(), DecodeError> on Codec and as an inherent method on base16::Base16, base32::Base32, and base64::Base64. Callers that store or forward encoded fields can check wire conformance in no_std without allocating or writing decoded bytes, while receiving the exact same DecodeError variant, precedence, and byte offset as decoding with sufficient output capacity.

Scenario Rounds bases faster-hex const-hex base64-simd
base16/validate/32 90,000,000 145 ms 176 ms (1.21x) 201 ms (1.38x) -
base16/validate/16384 380,000 281 ms 373 ms (1.33x) 373 ms (1.33x) -
base32/validate/32 60,000,000 221 ms - - -
base32/validate/16384 400,000 238 ms - - -
base64/validate/32 75,000,000 211 ms - - 181 ms (0.86x)
base64/validate/16384 420,000 209 ms - - 293 ms (1.40x)

Measured on Apple M4 Max (aarch64-apple-darwin, rustc stable, --repeat 5); note that base64-simd::check returns an opaque error without byte offsets.

Design Notes

Validation reuses each profile's decode_plan for length and padding checks (Padded, Unpadded, PaddedOptional, UnpaddedOptional), then validates all data_len symbols through a shared nibble-lookup backend (backend::validate_symbols) before checking unused trailing bits on partial final quanta. Because every Base16, Base32, and Base64 alphabet classifies valid ASCII symbols via (hi_mask[b >> 4] & lo_mask[b & 0x0f]) != 0 with hi_mask[8..16] == 0, one per-architecture kernel family (aarch64 NEON, x86/x86_64 AVX2 and SSSE3, and wasm32 simd128, with an 8-byte scalar fallback) covers all 24 profiles without per-profile SIMD duplication or decoded bit-packing work. Bulk batches aggregate unsigned byte minima across vector groups before checking for zero lanes, and a cold sequential scan recovers the earliest invalid byte offset on failure.

Verified with cargo x test on Rust 1.88.0 and stable, cargo x check, cargo x lint, cross-target SIMD/no_std checks (x86_64 +avx2/+ssse3, aarch64 +neon, wasm32 +simd128, thumbv7em-none-eabi), and cargo x bench -- --test.

Closes #12
Closes #22

tisonkun and others added 2 commits November 23, 2026 00:41
Add `validate(input: impl AsRef<[u8]>) -> Result<(), DecodeError>` on `Codec` and as an inherent method on `base16::Base16`, `base32::Base32`, and `base64::Base64` so callers that store or forward encoded text can verify alphabet, padding, case, and canonical trailing-bit rules without allocating or writing decoded bytes.

Reuse each profile's `decode_plan` for length and padding validation, then validate all data symbols through a shared nibble-lookup SIMD backend (`aarch64` NEON, `x86`/`x86_64` AVX2 and SSSE3, and `wasm32` `simd128`, with an 8-byte scalar fallback) followed by a single trailing-bit check on partial final quanta, preserving exact `DecodeError` precedence and byte offsets.

Co-authored-by: patrickZWY <154947644+patrickZWY@users.noreply.github.com>
Signed-off-by: tison <wander4096@gmail.com>
Wrap the direct `validate_ssse3` call in a local helper closure with its `// SAFETY:` comment immediately above the `unsafe` block to satisfy `clippy::undocumented_unsafe_blocks`.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun merged commit d2b5df9 into main Nov 22, 2026
13 checks passed
@tisonkun
tisonkun deleted the feat/validate branch November 22, 2026 16:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation-only validate API across codec profiles

1 participant