CodeIP: A Grammar-Guided Multi-Bit Watermark for Large Language Models of Code

Batu Guan, Yao Wan, Zhangqian Bi, Zheng Wang, Hongyu Zhang, Pan Zhou, Lichao Sun

Introduction

As Large Language Models (LLMs) like GitHub Copilot and ChatGPT become integral to modern software development, establishing the provenance of AI-generated code has become a critical challenge. Whether for protecting intellectual property or ensuring academic integrity, there is a growing need for reliable methods to "tag" AI-generated code. However, traditional watermarking techniques often struggle with the syntactic fragility of programming languages, where minor token changes can render code unrunnable.

This paper introduces CodeIP, a sophisticated grammar-guided watermarking technique designed to embed multi-bit information into code without compromising its utility or correctness.

Methodology: The CodeIP Architecture

CodeIP operates during the "next-token prediction" phase of code generation. Unlike standard watermarking that might prioritize message embedding at the expense of syntax, CodeIP integrates three distinct signals to determine the final output:

  1. Standard LLM Logits: The original probability distribution from the base model (e.g., Code Llama).
  2. Watermark Logits ($L_{WM}$): A hash function uses the previous token and a secret multi-bit message to partition the vocabulary into "green" and "red" lists, boosting the probability of tokens that carry the watermark.
  3. Type Predictor Logits ($L_{TP}$): This component uses an LSTM-based neural network to predict the valid lexical type (e.g., Keyword, Operator, Name) of the next token based on the programming language's grammar.
Figure 2: The CodeIP architecture combines standard LLM outputs with watermark and type predictor logits to ensure the generated code is both watermarked and syntactically valid.

By combining these three components, the system encourages the model to select tokens that satisfy the watermark requirements while strictly adhering to the rules of the language. This multi-bit approach allows for embedding up to 20 bits of data, enabling provenance tracking that can identify specific vendors or even individual users.

Key Insights: Grammar as a Guardrail

A fundamental insight of the CodeIP framework is that watermarking acts as "noise" in the generation process. In natural language, this noise is often tolerable, but in code, it frequently leads to syntax errors. CodeIP addresses this by using the Type Predictor as a "guardrail."

Figure 1: Comparison of watermarking with and without the Type Predictor. The predictor prevents the watermark from forcing syntactically incorrect tokens, such as choosing a colon instead of the 'in' keyword.

The researchers also identified a "tokenization conflict": LLMs use sub-word tokenization (like BPE), while programming language lexers operate on whole tokens. The Type Predictor helps bridge this gap, ensuring that sub-word choices do not aggregate into invalid code structures.

Experimental Results

The researchers evaluated CodeIP using the CodeSearchNet dataset across five languages: Java, Python, Go, JavaScript, and PHP, testing on 7B-parameter versions of Code Llama, StarCoder, and DeepSeek Coder. Key findings include:

  • High Extraction Accuracy: CodeIP achieved an average watermark extraction rate of 0.95, meaning the hidden message was correctly identified 95% of the time.
  • Superior Utility: Compared to baseline watermarking methods without grammar guidance, CodeIP showed 50% less degradation in CodeBLEU scores, a metric for code similarity and correctness.
  • Robustness: The watermark proved resilient against "Crop Attacks," maintaining high extraction rates even when 25% of the generated code was removed.

Conclusion and Future Work

CodeIP represents a significant step forward in securing the provenance of AI-generated code. By making the watermarking process grammar-aware, the authors have created a system that is both robust and practical for real-world development. Future work will focus on scaling the watermark capacity beyond 20 bits and enhancing resilience against intentional tampering, such as variable name obfuscation or heavy manual refactoring.

Summary updated

BibTeX
@inproceedings{guan2024codeip,
  author = {Batu Guan and Yao Wan and Zhangqian Bi and Zheng Wang and Hongyu Zhang and Pan Zhou and Lichao Sun},
  title = {{CodeIP: A Grammar-Guided Multi-Bit Watermark for Large Language Models of Code}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2024},
  doi = {10.18653/v1/2024.findings-emnlp.541},
}
Figure