Verified low-level programming embedded in F

Jonathan Protzenko, Jean Karim Zinzindohoué, Aseem Rastogi, Tahina Ramananandro, Peng Wang, Santiago Zanella Béguelin, Antoine Delignat-Lavaud, Catalin Hritcu, Karthikeyan Bhargavan, Cédric Fournet, Nikhil Swamy

Abstract

This paper presents Low, a domain-specific language embedded in the F proof assistant designed for writing low-level, C-like code with high-level verification guarantees. By leveraging the KreMLin compiler, Low programs are translated into readable, idiomatic C code. This approach was used to develop HACL, a verified cryptographic library that matches the performance of optimized C while providing mathematical guarantees for memory safety, functional correctness, and side-channel resistance.

Introduction

Cryptographic software is a critical component of modern security infrastructure, yet its implementation is fraught with peril. To meet performance demands, developers often resort to C or assembly, languages that lack inherent safety. A single memory error, such as an out-of-bounds access, can lead to severe vulnerabilities like Heartbleed. Furthermore, timing-based side-channel attacks can leak sensitive keys if branching or memory access patterns depend on secret data.

While high-level languages offer safety, they often suffer from performance overheads or rely on unverified C libraries for performance-critical tasks. Verifying existing optimized C code is also prohibitively labor-intensive. This work bridges the gap by enabling safe-by-construction code that compiles to high-performance C.

The Low* Language and KreMLin Compiler

The authors introduce Low, a subset of F that adopts a C-like memory model while retaining the power of a proof assistant.

Memory Management and Verification

Low utilizes a "hyper-stack" memory model to manage memory without a garbage collector. This structured approach allows for manual memory management similar to C, while the F type system ensures memory safety, preventing use-after-free and buffer overflow errors. Developers can write functional correctness proofs using dependent types, which are then erased during compilation.

The KreMLin Compilation Pipeline

A central contribution is the KreMLin compiler, which translates Low* into idiomatic C. The pipeline involves several stages of transformation and refinement to ensure the output is both efficient and readable.

The KreMLin compilation pipeline, showing the translation from F* through various intermediate representations to C and executable code.

As shown in the figure, the process begins within F, moving from EMF to Low*. KreMLin then handles the transition through intermediate representations like $\lambda ow^*$, $C^*$, and Clight, eventually producing standard C code that can be compiled by GCC, Clang, or CompCert.

Results and Evaluation

The methodology was validated through the creation of HACL*, a comprehensive cryptographic library implementing the NaCl API.

  • Performance: Benchmarks using the eBACS SUPERCOP framework show that HACL* is competitive with industry-standard libraries. For instance, its ChaCha20 implementation achieves 6.17 CPU cycles/byte, outperforming libsodium (6.97 cycles/byte) and matching OpenSSL.
  • Security: The use of abstract types for secret data ensures side-channel resistance by construction, as the compiler enforces that control flow and memory access patterns never depend on secret values.
  • Integration: The library has been successfully integrated into miTLS, demonstrating its practical utility in real-world verified systems.

Conclusion and Future Work

Low* and KreMLin provide a robust framework for developing verified, high-performance low-level software. While the current trusted computing base includes the unverified KreMLin compiler and standard C compilers, future work aims to verify KreMLin and improve the performance of code compiled with CompCert. Additionally, the authors are exploring the integration of verified assembly to further close the performance gap with hand-optimized implementations.

Summary updated

BibTeX
@article{protzenko2017verified,
  author = {Jonathan Protzenko and Jean Karim Zinzindohoué and Aseem Rastogi and Tahina Ramananandro and Peng Wang and Santiago Zanella Béguelin and Antoine Delignat-Lavaud and Catalin Hritcu and Karthikeyan Bhargavan and Cédric Fournet and Nikhil Swamy},
  title = {{Verified low-level programming embedded in F}},
  journal = {Proc. ACM Program. Lang.},
  year = {2017},
  doi = {10.1145/3110261},
}
Figure