Towards a Scalable Proof Engine: A Performant Prototype Rewriting Primitive for Coq

Jason Gross, Andres Erbsen, Jade Philipoom, Rajashree Agrawal, Adam Chlipala · DOI

Abstract

Formal verification is essential for ensuring the correctness of critical software systems, yet it faces significant scalability challenges. Current proof assistants like Coq often struggle with the computational overhead of equational rewriting, particularly in large-scale projects. This paper presents a novel, high-performance rewriting engine for Coq that utilizes proof by reflection and advanced syntax encoding to achieve a 1000× speedup in verified compiler tasks. By addressing the asymptotic bottlenecks associated with variable binders, this prototype demonstrates a path toward verifying massive systems, including large language models.

Introduction

The "verification overhead"—the ratio of proof code to functional code—remains a primary barrier to the widespread adoption of formal methods. In complex systems, this overhead can reach 100:1. A significant portion of this difficulty stems from the performance limitations of existing proof engines. Specifically, equational rewriting—the process of replacing expressions with equivalent ones based on proven rules—is a frequent bottleneck.

In large-scale applications such as Fiat Cryptography, which generates verified code for cryptographic primitives, standard Coq tactics like setoid_rewrite exhibit poor scaling. As the number of "binders" (e.g., let statements) increases, these tools often show cubic or exponential slowdowns. In real-world scenarios, standard tactics can consume over 60GB of RAM or fail to complete within any reasonable timeframe.

Methodology: A High-Performance Rewriting Primitive

To overcome these limitations, we propose a redesigned rewriting engine built on several key technical pillars:

Proof by Reflection

Rather than requiring the Coq kernel to validate every individual step of a rewrite, we implement a specialized rewriting program in Gallina (Coq's functional language). By proving this program correct once, the engine can execute rewrites as high-speed computations, significantly reducing the overhead per operation.

Advanced Syntax and Variable Handling

We employ Parametric Higher-Order Abstract Syntax (PHOAS) to represent program syntax. PHOAS efficiently manages variable binders by offloading name-tracking to Coq’s optimized kernel. To prevent the "exponential explosion" of term sizes during rewriting, we use a "telescope" data structure called UnderLets to preserve and share common sub-expressions.

Optimization Techniques

  • Decision Tree Compilation: Inspired by functional language compilers, we compile sets of rewrite rules into decision trees. This ensures that common patterns across multiple rules are checked only once.
  • Normalization by Evaluation (NbE): This technique handles higher-order functions and ensures the rewriting process terminates while maintaining high performance.
  • Specialization: Our tool, Make, generates a specialized, verified rewriter for a specific set of user-provided rules, removing the overhead associated with general-purpose pattern matching.

Evaluation

The prototype was evaluated using both real-world applications and targeted microbenchmarks.

Fiat Cryptography Integration

When integrated into the Fiat Cryptography toolchain, the extracted command-line compiler achieved a 1000× speedup over the previous implementation. The tool successfully synthesized verified code for the 32-bit P-521 elliptic curve in under four minutes—a task previously considered impractical.

Microbenchmarks

Microbenchmarks focused on scaling with respect to nested binders and recursive functions. While standard Coq tactics typically timed out when faced with a few hundred binders, our prototype demonstrated linear scaling, handling over 5,000 binders in approximately 10 seconds. This confirms that the "binder bottleneck"—where work is proportional to the total number of binders above a subterm—has been effectively mitigated.

Discussion and Future Work

This work represents a significant step toward the "Blue Sky" goal of verifying systems at the scale of Large Language Models (LLMs). Achieving such a feat would require a six-order-of-magnitude increase in current verification capacity, necessitating the asymptotic improvements demonstrated here.

Current limitations include a lack of support for the full breadth of Coq’s language, such as certain dependent types and primitive floats. Future research will explore "autospecialization" to bypass theoretical limits imposed by Gödel’s incompleteness theorem and the "fusion" of this rewriter with abstract interpretation for even more sophisticated optimizations.

Summary updated

BibTeX
@article{gross2024towards,
  author = {Jason Gross and Andres Erbsen and Jade Philipoom and Rajashree Agrawal and Adam Chlipala},
  title = {{Towards a Scalable Proof Engine: A Performant Prototype Rewriting Primitive for Coq}},
  journal = {J. Autom. Reason.},
  year = {2024},
  doi = {10.1007/s10817-024-09705-6},
}
Figure