feat: Safety wrapper and constant-time implementation (#55)
All checks were successful
Smoke Test / smoke (pull_request) Successful in 14s

Safety wrapper (llama-turbo.h, llama-turbo.cpp):
- Input validation (dimension must be power of 2, 16-4096)
- Null pointer checks
- Invalid norm detection (NaN/Inf/negative)
- Error codes for all failure modes
- Safe API: polar_quant_encode_turbo4_safe()

Constant-time quantization:
- ct_fabsf: bitwise absolute value (no branches)
- ct_select: bitwise selection (no branches)
- Always examines all 16 centroids
- No data-dependent branches in packing

Metal shader (ggml-metal-turbo.metal):
- Buffer bounds checking on all accesses
- Invalid norm handling (outputs zeros)
- Thread ID validation
- Constant-time dequantization kernel

Tests (tests/test_safety.py):
- 15 tests, all passing
- Power of 2 validation
- Dimension bounds checking
- Buffer size verification
- Packing correctness

Closes #55
This commit is contained in:
Alexander Whitestone
2026-04-14 22:14:51 -04:00
parent 7a7ce0e652
commit d750ca4224
5 changed files with 561 additions and 41 deletions