SmartCoder-R1: Secure, Explainable Solidity Framework
- SmartCoder-R1 is a framework for secure, explainable Solidity code synthesis that combines continual pre-training, chain-of-thought fine-tuning, and security-aware policy optimization.
- It specializes a 7B-parameter model on Solidity by leveraging a large corpus of verified contracts to master domain-specific syntax and security best practices.
- Evaluation on 756 real-world functions shows state-of-the-art performance in compilability, vulnerability reduction, and functional correctness, outperforming existing baselines.
Searching arXiv for the specified paper and closely related references. SmartCoder-R1 is a framework for secure and explainable smart contract generation built on Qwen2.5-Coder-7B. It is designed to address two linked deficiencies identified in LLMs for Solidity code synthesis: the absence of a transparent reasoning process and the tendency to generate code with critical security vulnerabilities. The framework combines Continual Pre-training (CPT), Long Chain-of-Thought Supervised Fine-Tuning (L-CoT SFT), and Security-Aware Group Relative Policy Optimization (S-GRPO) to specialize the model in Solidity, train it to emulate human security analysis, and directly optimize against vulnerable outputs. On a benchmark of 756 real-world functions, it reports state-of-the-art results across compilability, vulnerability rate, secure availability, functional correctness, and end-to-end correctness, while also producing reasoning traces that score highly in human evaluation (Yu et al., 12 Sep 2025).
1. Definition and scope
SmartCoder-R1 is presented as a framework for generating Solidity smart contract code that is both secure by design and explainable by construction (Yu et al., 12 Sep 2025). Its central premise is that smart contract generation requires more than syntactic code completion: because smart contracts manage high-value assets, generated code must satisfy compilability, functional correctness, and explicit security constraints, while exposing a reasoning process that can be audited.
The system is based on Qwen2.5-Coder-7B, described as a 7-billion-parameter decoder-only model in the Qwen family with strong code generation performance and amenability to reinforcement learning. The base architecture uses a standard Transformer decoder stack with causal attention and a vocabulary augmented for programming tokens. SmartCoder-R1 does not modify this core architecture; instead, it applies a staged training curriculum intended to instill domain expertise in Solidity, structured security reasoning, and vulnerability-aware optimization (Yu et al., 12 Sep 2025).
A defining characteristic of the framework is its explicit separation between reasoning and code. Training samples contain a reasoning trace enclosed in > ...</think> tags and a final Solidity implementation enclosed in <answer>...</answer> tags. This formatting operationalizes explainability in a concrete model interface: the model is trained not only to emit code, but also to articulate a multi-step security analysis before code generation (Yu et al., 12 Sep 2025).
2. Base model and continual pre-training
The first stage of SmartCoder-R1 is Continual Pre-training, which specializes the base Qwen2.5-Coder-7B model to Solidity. During this phase, all comments and documentation are stripped so that training focuses purely on Solidity syntax and idioms. The CPT corpus contains 286,397 code instances, or 620.56 million tokens, comprising 186,397 verified Ethereum contracts with 501.62 million tokens and 100,000 examples of general code/text with 118.94 million tokens. Contracts are normalized into windows of up to 2,048 tokens, yielding windows in total (Yu et al., 12 Sep 2025).
The optimization target is next-token prediction:
Training uses AdamW with learning rate , batch size 64, 2 epochs, gradient accumulation , sequence cutoff , and checkpoints every 500 steps. The paper states that this stage instills robust knowledge of Solidity grammar, inheritance, access-control modifiers, and common libraries including SafeMath and ERC-20/721 (Yu et al., 12 Sep 2025).
This phase defines the domain-adaptation layer of the framework. Rather than treating smart contract generation as a purely instruction-following problem, SmartCoder-R1 first reorients the model’s token-level priors toward the lexical and structural regularities of deployed Ethereum contracts. A plausible implication is that later reasoning and reinforcement stages benefit from a representation space already aligned with Solidity-specific control flow, access control patterns, and interface conventions.
3. Long chain-of-thought supervised fine-tuning
The second stage, Long Chain-of-Thought Supervised Fine-Tuning, is intended to teach the model human-like security reasoning. The SFT dataset contains 7,998 samples totaling 16.44 million tokens. Each sample pairs a functional requirement and cleaned NatSpec documentation extracted from verified contracts, the full contract context including imports, state variables, and modifiers, a rigorously validated reasoning trace inside
<think>...tags, and a final Solidity implementation inside<answer>...</answer>tags (Yu et al., 12 Sep 2025).
The samples originate from an LLM-driven draft produced by DeepSeek-V3 and are subsequently audited by security experts. The audit criteria are compilability via solc, functional correctness through unit tests, and absence of known vulnerability patterns such as reentrancy, unchecked calls, and access control omissions. Fine-tuning is run for three epochs with learning rate , batch size 8, and maximum length 8192, maximizing the tokenwise log-likelihood over the combined reasoning-and-code sequence:
where is the instruction, the input context, and the 0-th output token (Yu et al., 12 Sep 2025).
This stage is significant because it treats explainability as a supervised behavior rather than an emergent by-product. The model is explicitly trained to reason about the contract context before emitting code. The paper characterizes this as teaching the model to articulate a multi-step security analysis before implementation. This suggests that SmartCoder-R1 uses chain-of-thought not merely to improve raw task accuracy, but as a mechanism for aligning code generation with security review practice.
4. Security-aware group relative policy optimization
The third stage, Security-Aware Group Relative Policy Optimization, addresses the limitation that SFT alone cannot guarantee vulnerability elimination. In S-GRPO, the policy network 1 is refined with group-based reinforcement learning. For each prompt 2, defined as contract context plus requirement, the model samples 3 candidate outputs 4, each containing a <think> chain and an <answer> code block (Yu et al., 12 Sep 2025).
Each candidate is evaluated against three binary criteria:
- Compilation Success: 5
- Security Compliance: 6, using conservative pattern-matching plus static analysis to detect reentrancy, unchecked low-level calls, missing
onlyOwner, and related issues - Format Correctness: 7, requiring a substantive reasoning block and a distinct answer block
These are combined into a weighted scalar reward
8
with 9, explicitly prioritizing security. The optimization objective uses a normalized group-relative advantage 0 and a clipped policy gradient update with KL penalty:
1
where
2
with 3 and 4. Training spans five epochs on 1,691 distinct samples totaling 2.74 million tokens, split 1,200/491 train/eval, using AdamW with learning rate 5, batch size 8, gradient checkpointing, and eight NVIDIA H800 GPUs (Yu et al., 12 Sep 2025).
The role of S-GRPO is narrower and more operational than that of the SFT stage. Whereas L-CoT SFT teaches the model to produce structured reasoning, S-GRPO directly shapes the generation policy toward outputs that compile, satisfy a security filter, and preserve the prescribed reasoning-plus-answer format. The choice of weights indicates that security compliance is the dominant objective, but not the only one. Parameter sweeps reported in the paper show that the selected weighting 6 produces the best FullRate, while over-emphasizing security or compilability degrades overall balance (Yu et al., 12 Sep 2025).
5. Evaluation design and metrics
SmartCoder-R1 is evaluated on 756 real-world functions drawn from 289 deployed contracts, with 335 distinct ground-truth implementations across 53 Solidity versions. The data split is strictly disjoint, with Jaccard similarity less than 0.9 at function level to prevent leakage. Seventeen baselines are included, spanning general code LLMs such as CodeLlama, DeepSeek-V3, GPT-4.1, and Qwen-3, domain-specific contract LLMs including Storhaug et al., and reasoning-enhanced models such as DeepSeek-R1 and QwQ-32B. Decoding is greedy for reproducibility (Yu et al., 12 Sep 2025).
Five metrics are defined:
- ComPass: compilability
- VulRate: vulnerable among the compilable; lower is better
- SafeAval: compilable and secure
- FuncRate: compilable and functionally correct
- FullRate: compilable, secure, and functionally correct
Formally,
7
8
9
Compilability is checked by solc. Vulnerabilities are first flagged by Slither and then manually audited on a high-severity subset. Functional correctness is verified by per-function unit tests integrated into the contract harness (Yu et al., 12 Sep 2025).
The metric design reflects the paper’s insistence that smart contract generation should not be assessed solely by syntactic executability or semantic plausibility. In particular, FullRate combines the three target properties that the training pipeline attempts to optimize jointly. This suggests that the benchmark is structured to penalize systems that perform well on isolated dimensions but fail under compound deployment-relevant criteria.
6. Empirical performance, ablations, and reasoning quality
SmartCoder-R1 reports the following benchmark results: ComPass 0, VulRate 1, SafeAval 2, FuncRate 3, and FullRate 4 (Yu et al., 12 Sep 2025). The FullRate is described as a 45.79% relative improvement over the strongest baseline, DeepSeek-R1.
The comparison with DeepSeek-R1 is numerically explicit in the paper. DeepSeek-R1 achieves 84.39% ComPass, 32.29% VulRate, 57.14% SafeAval, 50.66% FuncRate, and 34.66% FullRate. Relative to that baseline, SmartCoder-R1 improves ComPass by 5 percentage points, reduces VulRate by 6 percentage points, improves SafeAval by 7 percentage points, improves FuncRate by 8 percentage points, and improves FullRate by 9 percentage points, reported as 0 relative (Yu et al., 12 Sep 2025). The paper also notes that GPT-4.1 reaches only 30.29% FullRate, which it interprets as evidence for the necessity of both reasoning and security-focused reinforcement learning.
Ablation results are used to isolate the contribution of each training stage. Without CPT, FullRate falls to 20.37%. Without L-CoT SFT, it drops to 32.67%. Without S-GRPO, it is 24.21%. These results are presented as confirmation that all three stages are indispensable (Yu et al., 12 Sep 2025).
Human evaluation further targets the quality of generated reasoning rather than only code outcomes. On 150 random <think> chains scored on a 4-point Likert scale, SmartCoder-R1 obtains “Good/Excellent” ratings of 82.7% for Functionality, 85.3% for Security, and 90.7% for Clarity. The corresponding DeepSeek-R1 scores are 76%, 54.7%, and 85.3%, respectively (Yu et al., 12 Sep 2025).
The paper also includes qualitative case studies. In examples such as renounceOwnership, SmartCoder-R1 is reported to adhere consistently to Checks-Effects-Interactions and to emit events in a security-aware order, whereas baselines often omit these best practices. This is important because it indicates that the model’s advantage is not confined to aggregate metrics; it also appears in recognizable audit-relevant coding patterns (Yu et al., 12 Sep 2025).
7. Significance, limitations, and open directions
SmartCoder-R1 is positioned at the intersection of code generation, smart contract security, and explainable reasoning. Its contribution lies not in a new foundation model architecture but in a staged training procedure that combines domain-specific pre-training, expert-validated reasoning supervision, and reward-driven policy refinement. Within the paper’s framing, the resulting system delivers both transparent reasoning and low residual vulnerability rates, thereby addressing the coupled problems of unauditable generation and insecure outputs (Yu et al., 12 Sep 2025).
The limitations are stated with some precision. Although S-GRPO uses programmatic rewards to reduce vulnerabilities, the automated reward function may under-penalize stylistic bloat or overly verbose reasoning. The evaluation suite, while broad, cannot cover every possible Solidity pattern, and out-of-distribution contracts may therefore remain challenging. These constraints are particularly relevant for deployment settings in which novel protocol patterns, unusual inheritance structures, or ecosystem-specific libraries diverge from the training and evaluation distributions (Yu et al., 12 Sep 2025).
The paper identifies three future directions: enriching the S-GRPO reward with dynamic analysis feedback, expanding the reasoning dataset to cover more exotic vulnerability classes, and exploring parameter-efficient adaptation for on-chain custom policies. This suggests that the current formulation is best understood as a framework template rather than a closed endpoint. Its broader significance lies in demonstrating a concrete way to bind explainability to structured output format and to bind security to explicit reinforcement objectives in the domain of smart contract synthesis (Yu et al., 12 Sep 2025).