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:
- Standard LLM Logits: The original probability distribution from the base model (e.g., Code Llama).
- 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.
- 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.
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."
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