Abstract
With the development of large language models, multiple AIs are now made available for code generation (such as ChatGPT and StarCoder) and are adopted widely. It is often desirable to know whether a piece of code is generated by AI, and furthermore, which AI is the author. Existing approaches are not satisfactory as watermarking codes are challenging compared with watermarking text data, as codes can be altered with relative ease via widely-used code refactoring methods. In this work, we propose ACW (AI Code Watermarking), a novel method for watermarking AI-generated codes. ACW is efficient as it requires no training or fine-tuning and works in a black-box manner. It is resilient as the watermark cannot be easily removed or tampered through common code refactoring methods. The key idea of ACW is to selectively apply a set of carefully-designed semantic-preserving, idempotent code transformations, whose presence (or absence) allows us to determine the existence of the watermark. Our experimental results show that ACW is effective, resilient and efficient, significantly outperforming existing approaches.
Introduction
As Large Language Models (LLMs) become staples in software development, distinguishing between human-written and AI-generated code has become a critical challenge for academic integrity, software licensing, and security. Existing watermarking techniques often suffer from two major drawbacks: the "White-Box" constraint, requiring access to internal model probabilities, and significant performance bottlenecks that slow down generation.
To address these issues, we introduce ACW (AI Code Watermarking), a universal, efficient, and robust post-processing framework. Unlike previous methods that intervene during token generation, ACW applies watermarks to the final output, making it compatible with any LLM, including closed-source APIs like GPT-4.
Methodology: The ACW Framework
The core of ACW lies in shifting watermarking from the generation phase to a post-processing phase. This is achieved through a library of 46 semantic-preserving and idempotent code transformations.
Idempotent Transformations
The transformations are categorized into three types:
- Refactoring: Structural changes such as converting loops to list comprehensions.
- Reordering: Swapping operands in commutative operations (e.g.,
a + btob + a) based on a secret hash. - Formatting: Standardizing whitespace and bracket alignment.
A transformation $t$ is idempotent if applying it twice yields the same result as applying it once: $t(C) = t(t(C))$. This property is the "secret sauce" for detection. To identify a watermark, the system attempts to re-apply the transformations. If the code remains unchanged, it indicates the transformations were already present, flagging the code as AI-generated.
Experimental Evaluation
ACW was evaluated using Python code generated from three major datasets: MBPP (Mostly Basic Python Problems), APPS, and HumanEval+. The method was tested against outputs from state-of-the-art models including GPT-4, GPT-4o, and Qwen2.5-Coder-14B, demonstrating its model-agnostic nature.
Key Results
Experimental results confirm that ACW is both highly effective and efficient. The system achieved an Accuracy (ACC) > 97% and a True Positive Rate (TPR) > 94%, while maintaining a False Positive Rate (FPR) below 1%.
Key performance metrics include:
- Efficiency: Watermarking takes less than 0.1 seconds on average, significantly faster than baseline methods like SWEET (5–8 seconds).
- Resource Usage: ACW runs on the CPU with negligible memory overhead, requiring no GPU VRAM.
- Utility Preservation: The "Pass@1" correctness rate dropped by less than 2%, ensuring the code remains functional.
- Robustness: ACW maintained high detection rates even when subjected to heavy manual editing or automated refactoring tools like
Ruff.
Discussion and Limitations
While ACW is highly effective for Python, extending it to other languages like C++ or Java requires implementing language-specific transformation rules. Additionally, very short code snippets (e.g., single lines) may offer fewer opportunities for transformation, potentially impacting detection confidence. Future work will explore "multi-bit" watermarking to encode specific metadata, such as model versions or user IDs.
Conclusion
ACW provides a resilient, training-free, and "plug-and-play" solution for identifying AI-generated code. By leveraging idempotent transformations in a post-processing stage, it overcomes the limitations of previous white-box methods, offering a scalable tool for the evolving landscape of AI-assisted programming.
Summary updated