DafnyBench: A Benchmark for Formal Software Verification

Chloe R Loughridge, Qinyi Sun, Seth Ahrenbach, Federico Cassano, Chuyue Sun, Ying Sheng, Anish Mudide, Md Rakib Hossain Misu, Nada Amin, Max Tegmark

TL;DR

This paper introduces DafnyBench, the largest benchmark to date for evaluating Large Language Models (LLMs) in formal software verification. They demonstrate that even advanced LLMs struggle to automatically generate the necessary "hints" (like loop invariants and assertions) to verify complex code, achieving a best-case success rate of 68%. This work highlights the significant gap between current LLM capabilities and the demands of formal verification, paving the way for future research.

What Problem Does This Paper Address?

Formal verification promises perfectly reliable software by mathematically proving that code meets its specifications. However, its adoption is hindered by high costs and a steep learning curve. While LLMs are revolutionizing software development with tools like code completion, ensuring the correctness of LLM-generated code remains a major challenge. Existing benchmarks for formal software verification are small, limiting the ability to train and evaluate LLMs for this critical task. This paper aims to address this by creating a larger, more comprehensive benchmark to push the boundaries of LLM-assisted formal verification.

Methods

The core of this work is the creation of the DafnyBench benchmark. The authors developed a "fill_hints" task where LLMs are given a Dafny program with its core logic but missing annotations (like assert and invariant statements). The LLM's goal is to reconstruct these annotations so that the Dafny verifier can successfully prove the program's correctness. They evaluated several LLMs (GPT-4 variants, Claude 3 Opus, CodeLlama) using this task, testing their ability to generate correct hints without altering the program's original signature, preconditions, or postconditions. They also investigated how performance changes with multiple attempts and by providing Dafny error messages as feedback.

Datasets

The DafnyBench benchmark consists of 782 ground-truth Dafny programs. These were sourced from:

  • GitHub Scrape: A large collection of publicly available Dafny files scraped from GitHub.
  • Clover: 62 programs from the existing Clover benchmark.
  • Dafny-synthesis: 164 programs translated from the MBPP benchmark.

This dataset is significantly larger than previous benchmarks like Clover (66 programs) and dafny-synthesis (153 programs).

Key Insights and Ideas

  • LLMs are not yet masters of formal verification: Despite impressive capabilities in code generation, current LLMs struggle to reliably generate the precise annotations required for formal verification. The 68% success rate for the best model (Claude 3 Opus) indicates a substantial room for improvement.
  • Hint generation is a complex task: The success rate drops significantly as program size and the quantity of required hints increase, suggesting that LLMs find it challenging to grasp the intricate dependencies and logical steps needed for verification.
  • Error feedback helps, but only so much: While providing Dafny error messages as feedback improves success rates, the gains diminish rapidly after a few attempts, indicating that LLMs don't fully leverage this information to correct their mistakes.
  • The "No LLM" baseline is surprisingly high: Dafny itself can verify a significant portion of programs (26.9%) without any LLM assistance, highlighting the power of the Dafny verifier and the inherent verifiability of some code.

Key Results

  • Claude 3 Opus achieved the highest success rate at 67.8% on the DafnyBench benchmark with 10 attempts per program.
  • GPT-4 Turbo followed closely with a 59.8% success rate.
  • The "No LLM" baseline (programs verifiable by Dafny without hints) was 26.9%.
  • Success rates decrease with increasing program length and hint quantity. For instance, programs with over 1000 characters had a lower success rate than shorter ones.
  • The cumulative success rate plateaued around 65% after approximately 5 attempts, suggesting limited benefit from further retries.

Limitations and Future Work

  • Data Contamination: The authors acknowledge the risk of LLMs being trained on data that overlaps with their benchmark, potentially inflating performance metrics.
  • Limited Scope of Evaluation: The current benchmark focuses on "fill_hints," not on generating specifications from natural language or creating test cases.
  • Prompt Engineering: The prompts used were manually tuned, and further systematic optimization could yield better results.
  • Future Work: The authors suggest expanding the benchmark with more complex, multi-file programs, incorporating better code documentation, and exploring more advanced LLM techniques like fine-tuning or search-based inference. They also propose investigating LLMs' ability to generate positive and negative test cases for specifications.

Summary updated

BibTeX
@article{loughridge2025dafnybench,
  author = {Chloe R Loughridge and Qinyi Sun and Seth Ahrenbach and Federico Cassano and Chuyue Sun and Ying Sheng and Anish Mudide and Md Rakib Hossain Misu and Nada Amin and Max Tegmark},
  title = {{DafnyBench: A Benchmark for Formal Software Verification}},
  journal = {Trans. Mach. Learn. Res.},
  year = {2025},
}
Figure