Introduction
As blockchain technology matures, Large Language Models (LLMs) are increasingly being used to translate natural language legal agreements into executable smart contracts. However, the transition from "legalese" to Solidity code is fraught with risk. If a smart contract fails to accurately reflect the rights, obligations, and penalties defined in the original legal text, it can lead to significant financial loss and regulatory non-compliance.
Traditional software metrics, such as cyclomatic complexity or basic unit testing, often fail to capture the nuanced "legal positions" inherent in a contract. This research addresses the gap by introducing a formal evaluation framework that models contracts as state-transition systems to measure legal compliance objectively.
The Evaluation Framework
The authors adopt a "contracts-as-processes" perspective, utilizing Petri Nets (PNs)—a mathematical modeling language for describing distributed systems. The evaluation pipeline consists of several key stages:
- Formal Modeling: Legal contracts are manually translated into Petri Nets, where "places" represent legal states (e.g., "User has GCDC") and "transitions" represent actions (e.g., "XYZ issues GCDC").
- Behavioral Mapping: From these Petri Nets, Reachability Graphs (RGs) are generated. These graphs map every possible sequence of events allowed by the contract, providing a comprehensive "ground truth" of legal behavior.
- LLM Generation: Leading LLMs, including GPT-4o, Gemini 1.5 Pro, Claude 3.5 Sonnet, and Llama 3.1 70b, are prompted to generate Solidity code using a structured "Plan-and-Solve" strategy.
- Comparison: The generated code is also converted into a Petri Net and Reachability Graph. The behaviors of the generated contract are then compared against the legal ground truth.
Compliance Metrics
To quantify the alignment between the legal text and the generated code, the researchers introduced three novel metrics based on the overlap of behaviors:
- Fitness: Measures the proportion of required legal behaviors present in the generated code (analogous to Recall).
- Precision: Measures the proportion of behaviors in the code that are actually relevant to the legal contract, identifying "extra" or hallucinated logic.
- Functional Equivalence Score (FES): A holistic measure of alignment that accounts for the overall state-space coverage.
Experimental Results
The study evaluated 20 smart contracts generated from five distinct legal agreements, ranging from simple shipping contracts to complex multi-party energy trading terms.
Key Findings
- The Precision-Coverage Trade-off: A clear divide emerged among top-tier models. Claude 3.5 Sonnet prioritized coverage (high fitness), attempting to implement more complex features like "pause" and "blacklist" functionality, though sometimes at the cost of precision. In contrast, Gemini 1.5 Pro and GPT-4o were more conservative, generating precise code that avoided hallucinations but occasionally missed edge cases.
- Library Utilization: High-performing models consistently leveraged industry-standard libraries like OpenZeppelin. Models that attempted to self-implement security features, such as Llama 3.1 70b, often produced error-prone code with poor compliance scores.
- Complexity Limits: While models performed perfectly on simple three-step processes (achieving an FES of 1.0), performance plummeted as contract complexity increased. On the most complex "Transactive Energy" contract, almost all models failed to produce legally compliant logic, highlighting a "path explosion" problem where the number of possible behaviors exceeds the model's reasoning capacity.
Conclusion and Future Directions
While current LLMs can generate syntactically correct Solidity code, ensuring legal compliance remains a significant challenge. The primary bottleneck is the manual effort required to create "ground truth" Petri Nets. Future research will focus on using LLMs to automate the generation of these formal models and developing more efficient graph-comparison algorithms to handle the complexity of real-world legal agreements.
Summary updated