Luhn Ultra 🚀
High-performance SIMD-optimized Luhn algorithm validator for payment card numbers with zero-allocation design.
Luhn Ultra is a blazingly fast, memory-efficient Rust library for validating payment card numbers using the Luhn algorithm (Mod-10). Built for production systems requiring PCI DSS compliance and high-throughput payment processing.
- ⚡ Zero Allocations: No heap allocations in validation hot paths
- 🧪 Battle Tested: Comprehensive test suite including fuzzing and property-based tests
- 🔧 Flexible API: Both fast boolean paths and detailed error reporting
- 📦 Batch Processing: Efficient batch validation with optional parallelization
- 🛡️ Memory Safe: Unsafe code limited to documented SIMD kernels only
Performance
- SSE2: ≥1.5 GB/s throughput
- Latency: p50 ≤200ns per validation
- Scaling: ≥85% linear scaling with parallelization
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
# Optional: Enable parallel batch processing
= { = "0.1", = ["parallel"] }
Usage Examples
Basic Validation
use Validator;
let validator = new;
// Fast path: boolean result for 16-digit ASCII PANs
let pan16: = *b"4111111111111116";
let is_valid = validator.validate_pan16_ascii;
println!; // Valid: true
// Relaxed validation: handles spaces, dashes, variable length
let is_valid = validator.validate_pan_relaxed;
println!; // Valid: true
Strict Validation with Error Details
use ;
let validator = new;
match validator.validate_pan_strict
Batch Processing
use Validator;
let validator = new;
// Batch validation for 16-digit PANs
let pans = vec!;
let results = validator.validate_pan16_batch;
println!; // [true, true, true]
// Batch validation with error details
let pan_strings = vec!;
let results = validator.validate_pan_batch_strict;
for in results.iter.enumerate
Utility Functions
use ;
// Compute check digit for a PAN body
let body = b"411111111111111";
if let Some = check_digit_ascii
// Complete a PAN with computed check digit
let body = b"411111111111111";
if let Some = complete_ascii
Parallel Processing
Enable the parallel feature and use batch operations:
use Validator;
let validator = new;
// Large batch processing automatically uses parallel workers
let pans: =
.map
.collect;
let results = validator.validate_pan16_batch;
println!;
API Reference
Core Types
Validator: Main validation engine (zero-sized, cheap to create)ValidationError: Detailed error information for strict validation
Validation Methods
validate_pan16_ascii(&[u8; 16]) -> bool: Fastest path for 16-digit ASCII PANsvalidate_pan_relaxed(&str) -> bool: Flexible validation with separator handlingvalidate_pan_strict(&str) -> Result<bool, ValidationError>: Detailed error reportingvalidate_pan16_batch(&[[u8; 16]]) -> Vec<bool>: Batch validation for 16-digit PANsvalidate_pan_batch_strict(&[&str]) -> Vec<Result<bool, ValidationError>>: Batch with errors
Utility Functions
check_digit_ascii(&[u8]) -> Option<u8>: Compute Luhn check digitcomplete_ascii(&[u8]) -> Option<[u8; 32]>: Complete PAN with check digit
Supported PAN Lengths
The library supports Payment Card Numbers with lengths from 13 to 19 digits, covering:
- 13 digits: Some Visa cards
- 14 digits: Diners Club
- 15 digits: American Express
- 16 digits: Visa, Mastercard, Discover (most common)
- 17-19 digits: Some specialized cards
Architecture
SIMD Optimization
- Runtime Detection: Automatically uses best available SIMD instructions
- SSE2 Baseline: Optimized for x86/x86_64 with SSE2 support
- Scalar Fallback: Pure Rust implementation for other architectures
- Future Ready: Designed for easy AVX2/AVX-512 extensions
Memory Safety
- Minimal Unsafe: Unsafe code limited to SIMD intrinsics only
- Documented Safety: All unsafe blocks have detailed SAFETY comments
- Extensive Testing: Fuzzing and property-based tests ensure correctness
Testing
Run the comprehensive test suite:
# Unit and integration tests
# Property-based tests
# Benchmarks
# Fuzzing (requires nightly)
Performance Tuning
For maximum performance:
- Use the fast path:
validate_pan16_ascii()for 16-digit ASCII PANs - Enable parallelization: Add
features = ["parallel"]for large batches - Batch processing: Use batch methods for multiple validations
- Avoid allocations: Prefer byte slices over String conversions
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT License (LICENSE-MIT)
at your option.
Contributing
Contributions are welcome! Please ensure:
- All tests pass:
cargo test - Code is formatted:
cargo fmt - No clippy warnings:
cargo clippy - Benchmarks don't regress:
cargo bench
Security
This library is designed with PCI DSS compliance in mind. For security issues, please contact the maintainers directly rather than opening public issues.