Abstract
Formal verification is the gold standard for securing smart contracts, yet it is hindered by the difficulty of writing formal specifications. We introduce MSG (Agentic Specification Generator), an automated system that utilizes Large Language Models (LLMs) to generate formal specifications for the Move programming language. By employing a modular, multi-agent architecture and a "verifier-in-the-loop" feedback mechanism, MSG successfully generates verifiable specifications for 84% of tested functions. Our results demonstrate that MSG not only outperforms traditional monolithic prompting strategies but also identifies critical edge cases frequently overlooked by human experts.
Introduction
Smart contracts manage billions of dollars in digital assets, making their security paramount. Formal verification provides mathematical certainty of correctness but requires formal specifications—declarative descriptions of intended program behavior. Writing these specifications is a specialized skill, distinct from standard programming, and requires deep expertise in verification toolchains.
While LLMs have shown promise in code generation, applying them to formal specifications for niche languages like Move presents unique challenges, including limited training data and the need for strict logical consistency. MSG addresses these challenges through an agentic design that decomposes specification generation into specialized tasks.
Methodology
The core of MSG is its agentic, modular architecture, which moves away from "all-in-one" prompts. The system is structured around four specialized ClauseGen Agents, each responsible for a specific type of Move Specification Language (MSL) clause: aborts_if, modifies, ensures, and loop_invariants.
Key technical components include:
- Verifier-in-the-Loop: MSG utilizes the Move Prover as an oracle. If a specification fails verification, the error messages and counterexamples are fed back to the LLM for autonomous self-correction.
- Context Scaffolding: To manage the LLM's context window, MSG employs static analysis for "best-effort inlining," selectively merging relevant function dependencies into the target function's body.
- Specification Coverage: A novel mutation-based metric evaluates specification completeness by checking if the spec can detect implementation errors introduced via random code deletions.
Experimental Evaluation
MSG was evaluated on 357 functions from foundational Move libraries on the Aptos blockchain, including move-stdlib, aptos-stdlib, and aptos-framework. These datasets represent a range of complexity, from basic data structures to core smart contract management logic. The tool's performance was benchmarked against existing expert-written specifications found in the Aptos repository.
Results
MSG achieved a high success rate, generating verifiable specifications for 84% of the target functions. The agentic design proved significantly more effective than a conventional "all-in-one" (AIO) baseline, producing 57% more verifiable clauses.
As shown in Figure 3, the full MSG system (utilizing agentic decomposition and prover feedback) reached an 84% success rate compared to just 53.5% for the AIO approach. Notably, MSG identified 291 unique clauses missing from official expert-written specifications, particularly those related to edge cases like integer overflows. In simpler libraries like move-stdlib, MSG achieved a 100% match with expert-written clauses.
Conclusion
MSG demonstrates that agentic LLM architectures can effectively bridge the gap between high-level code logic and low-level formal solvers. While challenges remain—such as LLM hallucinations of non-existent functions and the limitations of underlying SMT solvers—the "verifier-in-the-loop" approach provides a robust framework for automated formal verification. Future work will explore extending this methodology to other verification-centric languages like Solidity and Dafny.
Summary updated