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

Efficient and Universal Watermarking for LLM-Generated Code Detection

Boquan Li, Mengdi Zhang, Peixin Zhang, Jun Sun, Xingmei Wang, Wang, Xingmei · DOI

Abstract

Large language models (LLMs) have significantly enhanced the usability of AI-generated code, providing effective assistance to programmers. This advancement also raises ethical and legal concerns, such as academic dishonesty or the generation of malicious code. For accountability, it is imperative to detect whether a piece of code is AI-generated. Watermarking is broadly considered a promising solution and has been successfully applied to identify LLM-generated text. However, existing efforts on code are far from ideal, suffering from limited universality and excessive time and memory consumption. In this work, we propose a plug-and-play watermarking approach for AI-generated code detection, named ACW (AI Code Watermarking). ACW is training-free and works by selectively applying a set of carefully-designed, semantic-preserving and idempotent code transformations to LLM code outputs. The presence or absence of the transformations serves as implicit watermarks, enabling the detection of AI-generated code. Our experimental results show that ACW effectively detects AI-generated code, preserves code utility, and is resilient against code optimizations. Especially, ACW is efficient and is universal across different LLMs, addressing the limitations of existing approaches.

Introduction

As Large Language Models (LLMs) become staples in software development, the need to distinguish between human-written and AI-generated code has become critical for academic integrity and security. Existing watermarking techniques for text usually work by "biasing" the model's word choices during generation (e.g., favoring certain "green" tokens). However, code is much more rigid than natural language; there are fewer ways to say x = x + 1 than there are to say "The cat sat on the mat."

Previous attempts to adapt text watermarking to code suffer from three major flaws: low accuracy, high computational overhead, and a lack of universality. Most existing methods require access to the model's internal probability distributions (logits), making them incompatible with proprietary APIs like GPT-4 or Claude.

Methodology: The ACW Approach

ACW (AI Code Watermarking) is a training-free, "plug-and-play" approach that treats watermarking as a post-processing task. Instead of modifying the generation process, it applies subtle, semantic-preserving transformations to the output.

The ACW pipeline: from code generation to watermark embedding and identification.

Idempotent Transformations

The core of ACW lies in a library of 46 idempotent transformations. A transformation is idempotent if applying it once has the same effect as applying it multiple times. These are categorized into:

  • Refactoring: Converting structures (e.g., for loops to list comprehensions).
  • Reordering: Swapping operands (e.g., a + b to b + a) based on a secret hash of variable names.
  • Formatting: Adjusting whitespace or bracket alignment to a specific standard.

Workflow

  1. Embedding: ACW identifies applicable rules for a generated snippet and applies a subset in a secret order.
  2. Identification: To detect a watermark, the system reapplies the same rules. If the code remains unchanged for a statistically significant number of rules, it indicates the transformations were already present, flagging the code as AI-generated.

Experimental Evaluation

The researchers evaluated ACW using Python benchmarks (MBPP, APPS, HumanEval+) against code generated by state-of-the-art models like GPT-4o and Qwen2.5-Coder.

Key Results

ACW achieved over 97% accuracy and a True Positive Rate (TPR) over 94%, with a False Positive Rate (FPR) below 1%. Crucially, the "Pass Rate@1" (code correctness) dropped by less than 2%, whereas previous methods saw drops up to 49%. Watermarking a code snippet takes less than 0.1 seconds on average with zero additional GPU overhead.

Detection performance (TPR) across different code lengths.

As shown in the figure above, the True Positive Rate remains consistently high (above 95%) regardless of the code length, demonstrating the robustness of the watermark signal across various snippet sizes.

Conclusion and Future Work

ACW offers a model-agnostic, efficient solution for code watermarking. By moving the watermark to a post-processing stage, it achieves zero GPU overhead and works with any LLM. While currently focused on Python, the logic is extensible to other languages. Future work involves multi-bit watermarking to identify specific models or users and enhancing resilience against sophisticated adversarial attacks.

Summary updated

BibTeX
@misc{li2024efficient,
  author = {Boquan Li and Mengdi Zhang and Peixin Zhang and Jun Sun and Xingmei Wang and Wang, Xingmei},
  title = {{Efficient and Universal Watermarking for LLM-Generated Code Detection}},
  year = {2024},
  eprint = {2402.07518},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.07518},
}
Figure