Repository navigation
feat: add validation-only validate API - #28
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add
validate(input: impl AsRef<[u8]>) -> Result<(), DecodeError>onCodecand as an inherent method onbase16::Base16,base32::Base32, andbase64::Base64. Callers that store or forward encoded fields can check wire conformance inno_stdwithout allocating or writing decoded bytes, while receiving the exact sameDecodeErrorvariant, precedence, and byte offset as decoding with sufficient output capacity.basesfaster-hexconst-hexbase64-simdbase16/validate/321.21x)1.38x)base16/validate/163841.33x)1.33x)base32/validate/32base32/validate/16384base64/validate/320.86x)base64/validate/163841.40x)Measured on Apple M4 Max (
aarch64-apple-darwin,rustcstable,--repeat 5); note thatbase64-simd::checkreturns an opaque error without byte offsets.Design Notes
Validation reuses each profile's
decode_planfor length and padding checks (Padded,Unpadded,PaddedOptional,UnpaddedOptional), then validates alldata_lensymbols 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]) != 0withhi_mask[8..16] == 0, one per-architecture kernel family (aarch64NEON,x86/x86_64AVX2 and SSSE3, andwasm32simd128, 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 teston Rust 1.88.0 and stable,cargo x check,cargo x lint, cross-target SIMD/no_stdchecks (x86_64+avx2/+ssse3,aarch64+neon,wasm32+simd128,thumbv7em-none-eabi), andcargo x bench -- --test.Closes #12
Closes #22