Abstract
Distinguishing AI-generated code from human-written code is becoming crucial for tasks such as authorship attribution and content tracking. N-gram-based watermarking schemes have emerged as a prominent solution, injecting secret signals during generation. However, their robustness remains insufficiently evaluated. This work formally models code obfuscation and proves the theoretical impossibility of N-gram-based watermarking robustness under the assumption of distribution consistency. Through extensive experiments on three state-of-the-art watermarking schemes and multiple LLMs, we demonstrate that standard obfuscators reduce detection performance to the level of random guessing (AUROC ≈ 0.5) while preserving code functionality.
Introduction
As Large Language Models (LLMs) like LLaMA and DeepSeek-Coder become staples in software engineering, the ability to identify AI-generated code is critical for security and academic integrity. Current watermarking approaches are largely N-gram-based, subtly biasing token selection to create recognizable patterns. While these schemes claim robustness against simple transformations, they have not been rigorously tested against code obfuscation—a standard practice that transforms code syntax while strictly preserving its semantic behavior.
Theoretical Framework: The Equivalent Space
The authors formalize code obfuscation as a Markov random walk on a graph of semantically equivalent code versions. They introduce the concept of a Rule-based Equivalent Space, where an obfuscator can move code freely between functional equivalents.
If an obfuscator satisfies distribution consistency—meaning the statistical distribution of N-grams in obfuscated code matches that of natural code—the watermark signal is effectively diluted. The authors prove that as code moves through this equivalence space, the watermark becomes indistinguishable from non-watermarked code, increasing the failure rate of detectors to $1 - fpr$ (where $fpr$ is the false positive rate).
Empirical Evaluation
The study evaluated three prominent schemes (SWEET, WLLM, and SynthID) across Python and JavaScript using benchmarks like HumanEval and MBPP. They employed off-the-shelf obfuscators (e.g., UglifyJS, Python-Minifier) to attack code generated by LLaMA-3.1-8B and DeepSeek-Coder-33B.
Key Results
The results were stark across all models and languages:
- Total Detection Failure: Before the attack, watermarking schemes showed strong detection (AUROC > 0.9). After obfuscation, AUROC scores plummeted to approximately 0.5, indicating that the detectors were no better than a coin flip.
- Quality Preservation: The obfuscated code maintained its functional correctness, measured by the Pass@1 metric, proving that the watermark was removed without damaging the utility of the code.
Discussion and Future Directions
The vulnerability of N-gram watermarks stems from a "Semantic Gap." These watermarks operate at the surface level of syntax, while obfuscators operate at the logic level. Because code is naturally low-entropy, there is limited "room" to hide signals that can survive structural changes like variable renaming or loop reordering.
To achieve true robustness, the authors suggest moving toward semantic-aware watermarking. This would involve embedding signals into high-level architectural patterns—such as API call structures or logic flow—that are far more difficult to alter without breaking the program's functionality. Future research must bridge the gap between surface-level token patterns and the core logic of the code.
Summary updated