Security-Aware Group Policy Optimization
- The paper introduces S-GRPO, a method that refines smart contract generation through group-relative policy updates using a weighted reward on compilation, security, and formatting.
- S-GRPO serves as the final stage in the SmartCoder-R1 pipeline, integrating continual pre-training and supervised fine-tuning with an in-loop security-aware optimization.
- Reported experiments show S-GRPO reduces vulnerabilities by 54% and improves full correct outputs by 26 percentage points compared to baseline methods.
Searching arXiv for papers on S-GRPO, GRPO, and related theoretical/security-aware variants to ground the article with current literature. Security-Aware Group Relative Policy Optimization (S-GRPO) is a reinforcement-learning phase introduced in SmartCoder-R1 for secure and explainable smart contract generation. It is applied after Continual Pre-training (CPT) and Long Chain-of-Thought Supervised Fine-Tuning (L-CoT SFT), and refines the generation policy by optimizing a weighted reward signal for compilation success, security compliance, and format correctness. The method samples a group of candidate outputs per input, scores each candidate along these axes, computes a group-relative advantage, and updates the policy with a PPO-style clipped objective plus KL regularization against a reference policy. In this formulation, security-awareness is not an auxiliary post-hoc filter but an in-the-loop optimization target that shapes the generation distribution itself (Yu et al., 12 Sep 2025).
1. Placement in the SmartCoder-R1 training stack
S-GRPO is the final stage of the SmartCoder-R1 three-stage pipeline, which is based on Qwen2.5-Coder-7B. The pipeline begins with CPT to specialize the model, then applies L-CoT SFT on 7,998 expert-validated reasoning-and-code samples to train the model to emulate human security analysis, and finally uses S-GRPO to refine the policy with automated feedback. The stated objectives of this final phase are to produce smart contract code that is compilable, secure, and well-structured with explicit, auditable reasoning (Yu et al., 12 Sep 2025).
Within this stack, S-GRPO addresses a limitation of purely supervised adaptation. The method is explicitly designed to go beyond supervised learning limitations by using automated evaluation of generated samples during RL. For each input consisting of contract context and a new function requirement, the policy network samples a group of candidate outputs. These are then evaluated along security, compilation, and format-compliance axes, allowing policy improvement to be driven by relative performance within a context rather than by absolute labels alone.
This placement gives S-GRPO a specific role: CPT contributes domain specialization, L-CoT SFT establishes reasoning-and-code generation behavior, and S-GRPO aligns that behavior with programmatically verifiable constraints. A plausible implication is that the method is best understood not as a standalone optimizer, but as a post-SFT alignment layer specialized for security-critical code generation.
2. Reward construction and policy objective
The reward in S-GRPO is defined as a weighted sum of three binary components:
with empirically determined optimal weights
This weighting prioritizes security over compilation and format, reflecting the catastrophic risk of vulnerabilities (Yu et al., 12 Sep 2025).
| Reward component | Operationalization | Score |
|---|---|---|
Solidity compiler (solc) |
$1$ if compilation succeeds, else $0$ | |
| Regex-based pattern checks, static analysis, and scripts for known Solidity vulnerabilities | $1$ if all configured checks pass, else $0$ | |
Presence and structure of > ...</think> and <answer>...</answer> blocks |
$1$ if properly structured, else 0 |
The group-relative mechanism operates over a group of sampled outputs 1 for the same input. Each candidate’s reward is compared to the group average, yielding a normalized advantage used for policy updating. The paper characterizes this as a relative mechanism that promotes outputs that are not just good in absolute terms, but better than other current candidates.
The S-GRPO policy objective is given as
2
where 3 is the input prompt, 4 the 5th candidate output, 6 the token-level policy ratio, 7 the normalized advantage relative to the group baseline, 8 the PPO-style clip parameter, and 9 the KL penalty weight (Yu et al., 12 Sep 2025).
Two features of this formulation are central. First, the objective is token-averaged through the factor 0, which couples sequence length to update magnitude. Second, the KL term anchors RL updates to the SFT reference policy, with the stated role of preventing collapse or divergence.
3. Training workflow and implementation characteristics
The RL workflow is organized around automated evaluation. For each prompt, the model samples a group of outputs; each output is parsed and scored; rewards are normalized for advantage calculation; the policy gradient is estimated to increase the likelihood of more advantageous candidates; and updates are regularized against the SFT policy. The group structure provides a context-sensitive comparison signal rather than an isolated scalar reward (Yu et al., 12 Sep 2025).
In the SmartCoder-R1 implementation, batches of 8 are sampled, and for each prompt 8 candidate completions are generated per batch, so the group size is 1. The RL phase uses 1,691 high-quality samples strictly held out from SFT. The implementation leverages Logic-RL and VeRL, with a learning rate of 2 and KL coefficient 3.
The reward pipeline is fully automated. Compilation uses
solc. Security compliance combines regular expression-based pattern checks, static analysis, and scripts to detect known Solidity vulnerabilities, including reentrancy, missing access control, unchecked delegatecall, and integer over/underflow for old Solidity versions. Format correctness checks for a<think>...reasoning block with minimum length or steps, followed by an<answer>...</answer>code block. This setup makes the reward repeatable and scalable, and it permits direct penalization of insecure generations during training rather than only after generation.
This design also clarifies the scope of the “security-aware” label. The optimization target is defined by configured checks for compilation, vulnerability patterns, and output structure. This suggests that S-GRPO operationalizes security through an automated verification pipeline, not through unrestricted semantic proof of contract correctness.
4. Reported empirical performance
On a benchmark of 756 real-world functions and against 17 baselines, SmartCoder-R1 reports top performance across five key metrics: ComPass of 87.70%, VulRate of 8.60%, SafeAval of 80.16%, FuncRate of 53.84%, and FullRate of 50.53%. The FullRate is reported as a 45.79% relative improvement over the strongest baseline, DeepSeek-R1. Human evaluation of the generated reasoning reports high-quality ratings for Functionality at 82.7%, Security at 85.3%, and Clarity at 90.7% (Yu et al., 12 Sep 2025).
| Metric | Reported value |
|---|---|
| ComPass | 87.70% |
| VulRate | 8.60% |
| SafeAval | 80.16% |
| FuncRate | 53.84% |
| FullRate | 50.53% |
The ablation evidence isolates the contribution of S-GRPO. The variant without S-GRPO reports VulRate of 18.68%, SafeAval of 68.52%, and FullRate of 24.21%, whereas the full model with S-GRPO reports 8.60%, 80.16%, and 50.53%, respectively. The paper states that S-GRPO reduces vulnerabilities by 54% relative over L-CoT SFT alone and adds +26 percentage points in Full correct/secure/compilable outputs.
The reported case study further states that SmartCoder-R1 with S-GRPO learns to internalize security best practices such as the Checks-Effects-Interactions pattern, which are missed by reasoning-only or standard RL models. In the paper’s interpretation, S-GRPO improves not only security outcomes but also the auditability of the associated reasoning.
5. Theoretical reading through GRPO’s surrogate-objective analysis
A broader theoretical analysis of GRPO-style methods shows that group-based RL methods used for LLM post-training exhibit structural mismatches between reward optimization and the effective surrogate objective optimized in practice. The unified surrogate formulation identifies three recurring properties: non-uniform group weighting induces systematic gradient biases on shared prefix tokens; interactions with AdamW make training dynamics largely insensitive to reward scaling in the absence of regularization; and optimizer momentum can push policy updates beyond the intended clipping region under repeated optimization steps (Fontana et al., 8 Jan 2026).
Viewed through that surrogate lens, S-GRPO can be read as a GRPO-style instantiation with length-normalized token weighting, PPO-style clipping, and KL regularization. This is theoretically relevant because the S-GRPO objective contains the factor 4 and a clipped importance-ratio term, while also including a KL penalty. The unified analysis indicates that length- or token-dependent weighting can create systematic biases on shared prefixes, especially when outputs for the same prompt share initial tokens. A plausible implication is that a security-aware reward does not, by itself, remove objective-level inductive biases introduced by the weighting scheme.
The same analysis also bears on reward scaling. For GRPO-style objectives optimized with AdamW, the step is invariant up to 5 to global reward scaling when 6, but regularization changes that trade-off because the regularization gradient is unscaled. Since S-GRPO includes a KL term, reward design and KL tuning remain coupled rather than separable. The theoretical result on momentum overshoot further suggests that PPO-style clipping should not be treated as a guaranteed trust region under repeated inner-loop optimization, because AdamW’s first-moment accumulation can continue to move parameters after gradients vanish at the clipping boundary.
These findings do not invalidate S-GRPO; rather, they place it within a class of methods whose effective behavior depends jointly on reward construction, weighting, optimizer choice, and regularization. For security-critical deployment, that theoretical framing is significant because hidden inductive biases, reward hacking, and unintended policy drift are precisely the kinds of failure modes that a security-aware post-training method seeks to suppress.
6. Nomenclature, adjacent security variants, and scope boundaries
The term “security-aware” in S-GRPO refers to domain-specific reward shaping for security tasks, not to a single canonical GRPO variant across all domains. In vulnerability detection, GRPO has been adapted with structured, rule-based rewards, modified advantage functions, and dynamic aggregation that shifts emphasis from formatting to correctness. That line of work reports that GRPO-trained LLMs substantially outperform SFT models across in-distribution and out-of-distribution settings, and that the customized reward design helps mitigate over-prediction and reward hacking in software security analysis (Simoni et al., 3 Jul 2025).
A closely related but differently named development is SA-GRPO for Zero-Touch Networks, where the optimizer is embedded in SecLoop and uses group feedback collected from parallel environment executions. There, reward is computed on four axes—format correction, execution success, defense effectiveness, and reasoning validity or penalty—and the method is presented as feedback-driven rather than label-driven policy optimization for dynamic, adversarial network environments (Cao et al., 10 Dec 2025).
A separate nomenclature issue arises because the acronym “S-GRPO” is also used for “Stable Group-Relative Policy Optimization,” a method for mitigating Think-Answer Mismatch through noise-aware advantage reweighting in mathematical reasoning. That method models symmetric reward noise, derives an optimal group weight 7, and reports robustness under synthetic reward noise. Despite the shared acronym, it addresses reward-noise robustness rather than security-aware smart contract generation (Shen et al., 8 Aug 2025).
This naming overlap can create a common misconception that S-GRPO denotes a single universally accepted algorithm. The literature instead suggests a family resemblance: domain-customized GRPO variants preserve the group-relative PPO-like backbone while modifying reward modules, advantage computation, or feedback source to suit a specific reliability criterion—security compliance in smart contracts, vulnerability reasoning in code analysis, adaptive defense policies in networks, or noise robustness in reasoning tasks.