r/C_Programming • • 15d ago

Kheiron: A zero-dependency, single-header CSPRNG in C99 with 8-block AVX2 vectorization (~2.5 GB/s)

Hey everyone,

I've been working on Kheiron, an amalgamated, zero-dependency single-header CSPRNG library in C99/C11 built on top of ChaCha20 (RFC 8439), and I'd love to get feedback and code reviews from the community.

Why build this?

Most high-performance CSPRNG implementations either pull in the entire OpenSSL / libcrypto dependency tree (~7.8 MB binary footprint and dynamic runtime baggage) or provide scalar, single-block loops that struggle with high-throughput workloads.

Kheiron was built to be completely self-contained in a single file (kheiron.h, ~35 KB compiled) with:

  1. Parallel Vectorization: An AVX2 kernel processing 8 parallel ChaCha20 blocks simultaneously (512 bytes per iteration), yielding up to ~2.5 GB/s on modern x86_64 CPUs.
  2. Dynamic Runtime Dispatch: Automatic CPUID detection at runtime—no global -mavx2 compiler flags required. It falls back gracefully to pure scalar C99 on older x86, ARM, and RISC-V targets.
  3. Continuous Forward Secrecy: Automatic key re-erasure every 1 MB without measurable throughput degradation.
  4. Unbiased Uniform Sampling: Daniel Lemire’s nearly divisionless bit-multiplication method for integer intervals, plus IEEE 754 float/double conversion without modulo bias.
  5. Strict Fail-Closed Entropy: Direct integration with OS entropy (getrandom, /dev/urandom, BCryptGenRandom) with zero weak jitter fallbacks.

Current Test & CI Coverage

  • 235 E2E and adversarial tests passing across 12 GitHub Actions matrix jobs.
  • Clean builds under GCC and Clang with -Wall -Wextra -Wpedantic -Werror.
  • Full dynamic memory sanitization (ASan + UBSan) on Linux.
  • Continuous AST semantic scanning via GitHub CodeQL (security-and-quality).
  • Formal benchmarking methodology and reproduction steps documented in BENCHMARK.md.

Looking for:

  • Feedback on low-level AVX2 instruction scheduling and ChaCha20 quarter-round unrolling.
  • Edge-case testing on non-x86 platforms (e.g. Raspberry Pi, RISC-V, BSD).
  • General critiques on API ergonomics and memory wiping routines.

Any thoughts, critiques, or PRs are warmly welcome!

Note: LLM tooling was strictly limited to editor autocomplete (scaffolding repetitive structs/typedefs) and proofreading markdown documentation and benchmark tables. All cryptographic state machines, RFC 8439 logic, runtime CPUID dynamic dispatch, manual AVX2 SIMD pipelines (instruction interleaving/register scheduling), and the 235 E2E test cases were designed, implemented, and mathematically verified entirely by hand.

0 Upvotes

2 comments sorted by

•

u/AutoModerator 15d ago

Hi /u/Good-Ad-8991,

Your submission in r/C_Programming was filtered because it links to a git project.

You must edit the submission or respond to this comment with an explanation about how AI was involved in the creation of your project.

While AI-generated code is not disallowed, low-effort "slop" projects may be removed and it's likely that other users push back strongly on substantially AI-generated projects.

You can read more at How do I post my Project?


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

→ More replies (1)