Simple High-Level Code for Cryptographic Arithmetic - With Proofs, Without Compromises

Andres Erbsen, Jade Philipoom, Jason Gross, Robert Sloan, Adam Chlipala

Abstract

Writing fast, secure cryptographic code usually requires painstakingly hand-coding low-level assembly, a process that is highly error-prone and difficult to verify. This paper introduces Fiat Cryptography, a framework that generates highly optimized, constant-time C code from simple, high-level functional programs using machine-checked proofs of correctness. The resulting code has been adopted by Google's BoringSSL, securing approximately half of all HTTPS connections globally via Chrome and Android.

Introduction

Cryptographic primitives, such as elliptic curve cryptography (ECC), are the backbone of secure web communication. These primitives rely heavily on big-integer arithmetic modulo specific large prime numbers. To achieve the performance necessary for global scale and to ensure "constant-time" execution—preventing secret keys from leaking via timing side-channels—experts traditionally write this arithmetic in flat C or raw assembly.

However, this low-level approach is fraught with difficulty. It is tedious, prone to subtle bugs (with 26 real-world vulnerabilities documented in popular libraries), and requires duplicated effort for every new curve or hardware architecture. Fiat Cryptography addresses these challenges by automating the generation of verified, high-performance code.

The Fiat Cryptography Framework

The authors propose a paradigm shift: instead of writing and verifying low-level code, developers write high-level, generic algorithms and use a verified compiler to generate the low-level implementation. The process involves several key stages:

  1. High-Level Functional Library: Generic arithmetic algorithms (e.g., Montgomery reduction) are written in the Coq proof assistant. Numbers are represented as abstract lists of weights and values to simplify mathematical proofs.
  2. Partial Evaluation: The framework uses partial evaluation to specialize generic algorithms for specific prime numbers and hardware architectures (e.g., 32-bit vs. 64-bit). This process unrolls loops and flattens abstract lists into straight-line code.
  3. Certified Bounds Inference: A verified compiler pass analyzes the code to determine the value ranges of every intermediate variable, assigning them to the tightest possible machine integer types (32-bit or 64-bit) to prevent overflows.
  4. Code Generation: The output is optimized, straight-line, constant-time C code.
Table 1: Examples of prime representations across different architectures and limb configurations.

As shown in Table 1, the framework can adapt the same mathematical prime to different architectures by varying the number of "limbs" (the components of the big integer) and the base used for representation. For instance, Curve25519 is represented using 5 limbs on 64-bit architectures but requires 10 limbs on 32-bit architectures to maintain efficiency and safety.

Key Insights and Results

The core philosophy of Fiat Cryptography is that abstraction does not have to cost performance. By leveraging partial evaluation, clean functional code can compile into C code that rivals hand-tuned assembly. Furthermore, by verifying the generator rather than a specific implementation, the authors can produce guaranteed code for new curves or architectures almost instantly.

Real-World Impact

  • BoringSSL Integration: The generated code replaced older hand-written routines in Google's BoringSSL library, which is now deployed in Chrome and Android.
  • Performance: For X25519 scalar multiplication, the generated 64-bit C code is nearly as fast as the best hand-written assembly and significantly faster than previous high-performance C implementations.
  • Generalization: Benchmarks against the GNU Multiple Precision Arithmetic Library (GMP) showed speedups of 1.25x to 10x across a dataset of over 80 different cryptographic primes.

Limitations and Future Work

While highly effective, the current pipeline outputs C code, meaning the C compiler (e.g., GCC or Clang) remains in the Trusted Computing Base (TCB). Standard compilers sometimes struggle with saturated arithmetic, leaving a small performance gap compared to pure assembly. Additionally, while the intermediate representation is constant-time by construction, the framework currently relies on the C compiler not to introduce timing leaks during final compilation.

Future work aims to connect the pipeline directly to certified assembly compilers like Jasmin or Vale. This would remove the C compiler from the TCB, guarantee constant-time execution at the hardware level, and enable even better performance through superior register allocation.

Summary updated

BibTeX
@inproceedings{erbsen2019simple,
  author = {Andres Erbsen and Jade Philipoom and Jason Gross and Robert Sloan and Adam Chlipala},
  title = {{Simple High-Level Code for Cryptographic Arithmetic - With Proofs, Without Compromises}},
  booktitle = {IEEE Symposium on Security and Privacy},
  year = {2019},
  doi = {10.1109/sp.2019.00005},
}
Figure