NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

CODE ACROSTIC: Robust Watermarking for Code Generation

Li Lin, Siyuan Xin, Yang Cao, Xiaochun CaoarXiv 2025 · DOI

Introduction

As Large Language Models (LLMs) become ubiquitous in software development, the ability to distinguish between human-written and AI-generated code is vital for intellectual property protection and academic integrity. However, watermarking code presents unique challenges compared to natural language. Code is inherently "low-entropy"—it must adhere to strict syntax and logic, where minor alterations can lead to execution errors.

Current state-of-the-art watermarking techniques, such as KGW, SWEET, and EWD, typically embed signals in high-entropy regions. In the context of source code, these regions are almost exclusively natural language comments. This creates a significant security flaw: an attacker can trivially bypass the watermark by stripping all comments—a "comment removal attack"—without affecting the program's functionality.

The Vulnerability of Existing Watermarks

Existing methods are often "comment-deep." When comments are removed, the detection rate of these tools plummets because the watermark signal is lost.

Comparison of watermarking performance (TPR) between raw results containing comments and pure code samples after a comment removal attack.

As illustrated in the figure above, methods like SWEET and EWD see their True Positive Rate (TPR) drop from over 87% to approximately 16-20% when the code is stripped of its natural language components. This highlights a massive gap in current AI security measures for code generation.

Methodology: Code Acrostic (CA)

To address this fragility, we propose Code Acrostic (CA). Instead of relying on comments, CA embeds the watermark directly into the code's logic by targeting high-entropy transitions within the source code itself. The approach consists of three phases:

  1. Cue List Generation: Using a 1-gram model trained on a large code corpus, we identify "cue tokens"—specific tokens that are frequently followed by a diverse set of subsequent tokens (high entropy). These tokens form a confidential "Cue List."
  2. Cue Sampling (Injection): During the generation process, if the model produces a token present in the Cue List, a "green list" bias is applied to the sampling of the next token. This effectively "hides" the watermark signal within these specific, high-entropy logical transitions.
  3. Cue Detection: The detector identifies AI-generated code by analyzing only the tokens that immediately follow entries in the Cue List. By ignoring predictable, low-entropy code segments, the detector achieves a superior signal-to-noise ratio.

Experimental Setup

We evaluated Code Acrostic using the HumanEval benchmark, which contains 164 coding problems. Code was generated using DeepSeek-Coder-1.3b. We utilized the MarkLLM Framework to implement and compare CA against several baselines, including KGW, SWEET, and EWD.

Results and Discussion

Code Acrostic demonstrates superior performance in both detection accuracy and robustness. On the HumanEval dataset, CA achieved an AUROC of 0.772, outperforming KGW (0.685), SWEET (0.692), and EWD (0.750).

Robustness Against Comment Removal

The primary advantage of CA is its resilience to the removal of natural language comments. Because the watermark is embedded in the code logic, it remains detectable even after the "fluff" is stripped away.

Detection performance (TPR) comparison across different watermarking methods with and without natural language comments.

As detailed in the table, when both natural language and code are present, CA performs competitively. However, when only code remains, CA maintains a TPR of 34.76%, nearly doubling the performance of its closest competitors like SWEET (16.46%) and KGW (17.07%).

Impact on Code Quality

Crucially, this robustness does not come at the cost of utility. CA maintained a Pass@1 rate of 51.20%, only a slight decrease from the unwatermarked "Raw" model's 53.05%. This indicates that the "sparse" injection method successfully preserves the functional correctness of the generated programs while ensuring detectability.

Conclusion

Code Acrostic provides a more "sticky" watermark by leveraging high-entropy pockets within code logic. While highly effective against comment removal, future work should explore its resilience against more sophisticated semantics-preserving attacks, such as variable renaming or structural refactoring. Additionally, the security of the method relies on the confidentiality of the Cue List, suggesting a need for robust key management in practical deployments.

Summary updated

BibTeX
@misc{lin2025code,
  author = {Li Lin and Siyuan Xin and Yang Cao and Xiaochun Cao},
  title = {{CODE ACROSTIC: Robust Watermarking for Code Generation}},
  year = {2025},
  eprint = {2512.14753},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2512.14753},
}
Figure