SPHGen: A Program Generator for Fast Polynomial Hash Functions

Tommaso Pegolotti, Jan Gilcher, Kenneth G. Paterson, Markus Püschel

Universal hash functions (UHFs) are fundamental building blocks in modern cryptography, underpinning widely-used schemes like AES-GCM and ChaCha20-Poly1305. Given their ubiquity, the efficiency of these functions is paramount. However, achieving peak performance on modern hardware is a complex task that often requires manual, architecture-specific optimization. In this paper, we introduce SPHGen, a program generator designed to automate the creation of high-performance, verified polynomial hash functions.

The Implementation Challenge

Implementing polynomial hash functions efficiently involves navigating a vast design space. Developers must make critical decisions regarding finite field parameters, prime selection, and "limb" sizes—how large integers are decomposed for processing by the CPU. Furthermore, the diversity of modern vector Instruction Set Architectures (ISAs), such as AVX2, AVX512, and AVX512_IFMA, means that a configuration optimized for one processor may perform poorly on another.

Historically, this has forced a trade-off: developers could use generic, portable C code that sacrifices performance, or invest significant effort into hand-writing assembly code that is difficult to verify, maintain, and port to new architectures. SPHGen addresses this by automating the optimization process, bridging the gap between high-level design and low-level performance.

The SPHGen Pipeline

SPHGen employs a multi-stage pipeline to generate, optimize, and verify hash functions:

  • Program Generation: Using templates, SPHGen generates code in C or Jasmin. It supports essential big-integer operations, including addition, modular reduction, and various multiplication algorithms like Schoolbook and Karatsuba.
  • Vectorization: The tool leverages SIMD (Single Instruction, Multiple Data) instructions to process multiple message blocks in parallel. It specifically targets advanced x86 extensions like AVX2 and AVX512, including the specialized Integer Fused Multiply-Add (IFMA) instructions.
  • Performance Modeling: Rather than relying on exhaustive benchmarking, SPHGen uses an accurate performance model based on instruction latency and throughput. This allows the tool to predict the runtime of thousands of potential configurations, identifying the Pareto front—the set of parameters offering the optimal trade-off between security (prime size) and speed—without executing a single line of code.
  • Verification and Safety: To ensure functional correctness, the generated code is verified using symbolic execution. Furthermore, by generating code in Jasmin, SPHGen guarantees memory safety and constant-time execution, which is critical for mitigating side-channel attacks.

Key Insights

The development of SPHGen revealed several important insights into modern cryptographic implementation:

  • The Cost of Legacy Features: The authors found that "key clamping" in Poly1305—a feature originally intended to aid 2005-era floating-point hardware—actually degrades performance on modern vector units. SPHGen demonstrates that using a "pure" version of the prime $2^{130}-5$ is significantly more efficient for contemporary CPUs.
  • Model-Driven Precision: A relatively simple instruction-level model proved highly effective at identifying optimal parameters. For instance, the model can reliably determine whether 52-bit or 32-bit limbs are superior for a specific ISA, a task that often eludes human intuition.
  • ISA Specialization: No single configuration is optimal across all hardware. The unique 52-bit multipliers in AVX512_IFMA, for example, favor different algorithmic choices than standard AVX512.

Performance Evaluation

The effectiveness of SPHGen is demonstrated through rigorous benchmarking on modern hardware, specifically an Intel Xeon Silver 4410Y (Sapphire Rapids architecture).

  • Outperforming Hand-Optimized Assembly: For large messages, SPHGen’s generated AVX512 code achieved speedups of up to 37% compared to the highly-optimized Poly1305 implementation in OpenSSL, which is hand-coded in assembly.
  • Significant Vectorization Gains: Compared to non-vectorized (scalar) code, SPHGen provided average speedups of 1.52x for AVX2, 2.21x for AVX512, and 4.78x for AVX512_IFMA.
  • Security-Performance Trade-offs: The tool successfully mapped the Pareto front for primes ranging from 100 to 400 bits, providing a comprehensive menu of optimal hash functions for various security requirements.

Conclusion and Future Work

SPHGen demonstrates that automated program generation can surpass the performance of expert-written assembly while providing stronger correctness guarantees. While the current tool focuses on prime fields and x86 architectures, future work includes extending support to binary fields (essential for GHASH) and retargeting the generator to other architectures like ARM Neon and RISC-V. By automating the "last mile" of cryptographic optimization, SPHGen enables the rapid deployment of secure, high-performance hash functions across diverse hardware landscapes.

Summary updated

BibTeX
@inproceedings{pegolotti2026sphgen,
  author = {Tommaso Pegolotti and Jan Gilcher and Kenneth G. Paterson and Markus Püschel},
  title = {{SPHGen: A Program Generator for Fast Polynomial Hash Functions}},
  booktitle = {IACR},
  year = {2026},
  doi = {10.46586/tches.v2026.i1.161-184},
}
Figure