FTSmartAudit: A Knowledge Distillation-Enhanced Framework for Automated Smart Contract Auditing Using Fine-Tuned LLMs

Zhiyuan Wei, Jing Sun, Zijian Zhang, Xianhao Zhang, Zhe HouarXiv 2024

TL;DR

HKT-SmartAudit introduces a novel framework to train lightweight, specialized language models (<10B parameters) specifically optimized for smart contract vulnerability detection. By combining multi-stage knowledge distillation, domain-specific data curation, and reward-guided reinforcement learning, these compact models achieve state-of-the-art accuracy. They successfully outperform both traditional static analysis tools and massive commercial LLMs while drastically reducing computational overhead and preserving data privacy.

What Problem Does This Paper Address?

Smart contracts secure billions of dollars in decentralized finance (DeFi) and Web3 ecosystems, making any code vulnerability incredibly costly. Traditional auditing methods—such as static analysis, fuzzing, and formal verification—are either too time-consuming or fail to scale with the increasing complexity of modern multi-contract systems.

While massive Large Language Models (LLMs) like GPT-4 or DeepSeek-R1 show immense promise in catching subtle, logic-driven bugs, deploying them in production is bottlenecked by two major challenges:

  1. High Computational Costs: Running models with hundreds of billions of parameters requires specialized, expensive infrastructure (e.g., 350+ GB of GPU memory).
  2. Data Privacy Risks: Relying on commercial third-party APIs requires enterprises to send proprietary, sensitive source code to external servers.

To bridge this gap, the authors address the challenge of building small, locally deployable, yet highly accurate specialized models tailored for smart contract auditing.

Methods

The authors propose HKT-SmartAudit (Hybrid Knowledge Transfer for Smart Contract Audit), a framework that systematically transfers the reasoning capabilities of giant "teacher" models into compact "student" models.

Figure 1: An overview of HKT-SmartAudit framework.

The pipeline consists of three core phases:

  1. Multi-Stage Teacher-Student Distillation: Instead of distilling directly from a massive model to a tiny one, the framework utilizes a progressive transfer mechanism, leveraging an intermediate medium-sized model (Llama-3.1-8B) to help the smaller student models absorb nuanced expertise effectively.
  2. Knowledge Elicitation: To enrich the training data, the framework combines four distinct distillation strategies: Direct Labeling (generating soft target probability distributions), Expansion Distillation (extracting domain knowledge from corpora like OpenZeppelin and CVEs), Expert Knowledge Distillation (curating "gold" examples from real-world audit platforms like Code4rena), and Self-Knowledge Distillation (a human-in-the-loop feedback loop to review and correct teacher errors).
Figure 2: An illustration of knowledge transfer methods from teacher LLMs to small models.
  1. Reinforcement Learning with Reward-Guided Refinement: The student model is iteratively refined via Proximal Policy Optimization (PPO). The reward model evaluates both label prediction accuracy and rationale alignment (ensuring the model explains why a bug exists), while heavily penalizing safety violations (such as missing critical vulnerabilities). Fine-tuning is executed efficiently using QLoRA to minimize the memory footprint.

Datasets

The framework was trained and evaluated using a variety of custom-curated and standard datasets:

  • Training Datasets:
  • Seed Set: 120 human-labeled contracts covering distinct vulnerability types to prompt the teacher.
  • HKT-vul: A final distilled dataset containing 2,434 vulnerable contract entries.
  • HKT-mix: A balanced dataset of 4,868 entries containing both vulnerable and secure contracts to ensure the model learns to minimize false positives.
  • Validation Datasets:
  • Standard Vulnerability Set: 143 annotated contracts containing 182 tagged vulnerabilities from the widely recognized SmartBugs-curated dataset.
  • Real-World Projects Set: A highly complex dataset consisting of 72 projects and 6,454 contracts sourced from live Code4rena audit reports, focusing heavily on intricate logic flaws.
  • CVE Set: 13 representative real-world smart contract CVEs covering integer overflows, access control issues, and logic bugs.

Key Insights and Ideas

  • Domain Specialization Beats Raw Scale: Through targeted hybrid knowledge transfer, a fine-tuned 8B parameter student model can match or even outperform its massive, general-purpose teacher models on domain-specific tasks.
  • Soft Labels Provide Richer Guidance: Training the student model on the teacher's probability distributions (soft labels) rather than binary "vulnerable/secure" hard targets provides much richer nuance, helping the smaller model capture subtle code patterns.
  • Optimizing for Explanations Improves Robustness: By explicitly incorporating a "Rationale Score" into the reinforcement learning reward function, the framework ensures the model doesn't just guess correctly, but actually understands the underlying security flaw.
  • Commodity Hardware Accessibility: The resulting student models are highly efficient and can be successfully trained and deployed in resource-constrained environments, such as a single NVIDIA Tesla T4 GPU with 16GB of memory.

Key Results

The distilled HKT models achieved remarkable performance across all evaluation benchmarks:

  • Standard Benchmark Success: On the Standard Set, HKT-vul-DeepSeek-R1-Llama3 achieved a top-max accuracy of 95.6%, outperforming traditional static analysis tools like Mythril (72.0%) and Slither (67.0%), as well as its own un-tuned base model (59.3%). It even slightly surpassed the massive DeepSeek-R1 teacher model (95.1%).
  • Real-World Logic Auditing: On the highly challenging Real-World Projects Set, the framework significantly boosted capabilities. HKT-vul-Llama3.1 achieved the highest top-max accuracy at 25.3% (compared to just 9.2% for its base model) while maintaining a low average number of outputs, demonstrating an optimal trade-off between precision and efficiency.
Figure 4: Performance Comparison on Two Sets
  • CVE Detection: In the representative CVE benchmark, the distilled model correctly identified vulnerabilities in 10 out of 13 complex cases, matching the performance of the state-of-the-art GPT-o1 reasoning model.

Limitations and Future Work

The authors note a few key limitations that pave the way for future research:

  • Generalization to Zero-Days: Because the training data is primarily composed of known vulnerability classes, the model may struggle to detect entirely novel, unseen exploit techniques that deviate from historical patterns.
  • Static Knowledge Post-Training: Unlike rule-based systems, the model's knowledge remains static after training. It lacks an automated mechanism to incrementally update its weights as new Solidity language features or vulnerability patterns emerge, requiring periodic retraining.
  • Future Directions: The team plans to focus future work on enhancing the model's ability to handle multi-contract logic flaws, lowering false positive rates, and extending the framework to automated vulnerability patching.

Summary updated

BibTeX
@misc{wei2024ftsmartaudit,
  author = {Zhiyuan Wei and Jing Sun and Zijian Zhang and Xianhao Zhang and Zhe Hou},
  title = {{FTSmartAudit: A Knowledge Distillation-Enhanced Framework for Automated Smart Contract Auditing Using Fine-Tuned LLMs}},
  year = {2024},
  eprint = {2410.13918},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2410.13918},
}
Figure