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

Who Wrote this Code? Watermarking for Code Generation

Taehyun Lee, Seokhee Hong, Jaewoo Ahn, Ilgee Hong, Hwaran Lee, Sangdoo Yun, Jamin Shin, Gunhee Kim

Introduction

As Large Language Models (LLMs) become increasingly proficient at software engineering tasks, the ability to distinguish between human-authored and AI-generated code has become a critical necessity. Reliable detection is essential for addressing academic integrity, managing licensing disputes, and mitigating the spread of AI-generated malware. While watermarking—the process of secretly biasing a model's token selection to create a detectable statistical signature—has proven effective for natural language, its application to source code has historically been problematic.

The Entropy Dilemma

Source code is syntactically brittle. Unlike natural language, where synonyms often allow for flexible word choice, code frequently requires specific characters (keywords, brackets, operators) to remain functional. Standard watermarking techniques, such as the Watermarking for Large Language Models (WLLM) framework, randomly partition the vocabulary into "green" and "red" lists, nudging the model toward green tokens.

In code, many positions have "low entropy," meaning the next token is essentially predetermined by syntax. Forcing a green-list token in these positions often results in syntax errors or logical failures. Conversely, applying a "weak" watermark to avoid these errors results in a signal that is too faint to detect reliably.

Conceptual comparison of standard watermarking (WLLM) versus SWEET. WLLM either breaks code logic or fails detection, while SWEET preserves both.

SWEET: Selective Watermarking via Entropy Thresholding

To address this, the authors propose SWEET (Selective WatErmarking via Entropy Thresholding). The core innovation lies in applying the watermark selectively based on the local uncertainty of the model.

  1. Entropy Calculation: At each generation step, the model calculates the entropy ($H_t$) of the probability distribution for the next token. High entropy indicates that the model has multiple valid choices (e.g., variable names or logic structures), while low entropy indicates a syntactically constrained choice.
  2. Selective Application: SWEET only applies the green-list bias if the entropy exceeds a predefined threshold ($\tau$). By leaving low-entropy tokens untouched, SWEET ensures that critical keywords and syntax remain intact.
  3. Detection: During the detection phase, the algorithm recalculates entropy for each token in the code. It focuses its statistical analysis only on high-entropy positions, leading to a much cleaner signal and a higher $z$-score for AI-generated content.

Key Results

SWEET was evaluated across several prominent benchmarks, including HumanEval, MBPP, and DS-1000. The results demonstrate a significant improvement in the trade-off between detection reliability and code functionality.

Detection performance (AUROC) vs. code correctness (Pass@1) on HumanEval, MBPP, and DS1000 benchmarks.

On the HumanEval benchmark, SWEET achieved an AUROC of 0.943, vastly outperforming standard WLLM (0.822) and post-hoc methods like DetectGPT (~0.60). Crucially, SWEET maintained high code quality; while strong standard watermarking typically causes a precipitous drop in "Pass@1" scores, SWEET kept performance within 2-4% of the non-watermarked baseline. The method also proved robust across multiple programming languages, including C++ and Java.

Limitations and Future Work

Despite its success, SWEET faces challenges. Because the watermark is concentrated in high-entropy tokens like variable names, it is susceptible to "paraphrasing attacks" where a user systematically renames variables to obscure the signal. Additionally, detection currently requires access to the model's probability distributions (a "white-box" or "gray-box" requirement), though the authors found that smaller "surrogate" models can often approximate these distributions effectively. Future research may explore making these watermarks more resilient to renaming or developing entirely distortion-free methods.

Summary updated

BibTeX
@inproceedings{lee2024who,
  author = {Taehyun Lee and Seokhee Hong and Jaewoo Ahn and Ilgee Hong and Hwaran Lee and Sangdoo Yun and Jamin Shin and Gunhee Kim},
  title = {{Who Wrote this Code? Watermarking for Code Generation}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  doi = {10.18653/v1/2024.acl-long.268},
}
Figure