[Paper Review] CodeIP: A Grammar-Guided Multi-Bit Watermark for Large Language Models of Code
CodeIP is a grammar-guided, multi-bit watermarking technique for LLM-generated code that embeds semantic-preserving watermarks by leveraging a type predictor to guide token generation. It achieves a high average watermark extraction rate of 0.95 across five programming languages while maintaining code utility and semantic correctness.
Large Language Models (LLMs) have achieved remarkable progress in code generation. It now becomes crucial to identify whether the code is AI-generated and to determine the specific model used, particularly for purposes such as protecting Intellectual Property (IP) in industry and preventing cheating in programming exercises. To this end, several attempts have been made to insert watermarks into machine-generated code. However, existing approaches are limited to inserting only a single bit of information. In this paper, we introduce CodeIP, a novel multi-bit watermarking technique that inserts additional information to preserve crucial provenance details, such as the vendor ID of an LLM, thereby safeguarding the IPs of LLMs in code generation. Furthermore, to ensure the syntactical correctness of the generated code, we propose constraining the sampling process for predicting the next token by training a type predictor. Experiments conducted on a real-world dataset across five programming languages demonstrate the effectiveness of CodeIP in watermarking LLMs for code generation while maintaining the syntactical correctness of code.
Motivation & Objective
- To address the lack of flexible, multi-bit watermarking solutions for LLM-generated code that preserve semantic correctness.
- To enhance watermark robustness and diversity by integrating grammar-aware token prediction during code generation.
- To enable reliable identification of AI-generated code for intellectual property protection and academic integrity.
- To overcome limitations of single-bit or hard watermarking methods that are detectable or semantically disruptive.
- To maintain high code utility while embedding verifiable, multi-bit messages such as model vendor IDs.
Proposed method
- A type predictor is trained to predict the grammatical category (e.g., keyword, identifier) of the next token, guiding the watermark insertion process.
- Watermark bits are embedded during decoding by manipulating the logit probabilities of candidate tokens based on a predefined hash function and random seed.
- Grammar constraints are enforced by modifying the sampling distribution to favor tokens that conform to the predicted grammar type.
- The watermark is inserted in real-time during code generation, ensuring minimal disruption to the model’s output.
- A multi-bit message (e.g., ASCII for 'GPT') is encoded and embedded using a soft watermarking strategy that avoids detectable patterns.
- The method integrates watermarking directly into the LLM inference pipeline, enabling seamless deployment.

Experimental results
Research questions
- RQ1Can a grammar-guided approach improve the semantic correctness of watermarked code generated by LLMs?
- RQ2How effective is CodeIP in extracting multi-bit watermarks while preserving code utility across diverse programming languages?
- RQ3Does integrating grammar prediction enhance watermark robustness against common attacks like cropping or obfuscation?
- RQ4How does CodeIP compare to single-bit or hard watermarking methods in terms of extraction accuracy and semantic fidelity?
- RQ5Can CodeIP reliably embed identifiable model information (e.g., vendor ID) in generated code without altering its functionality?
Key findings
- CodeIP achieves an average watermark extraction rate of 0.95 across five programming languages, demonstrating high reliability.
- The method maintains high semantic correctness, as evidenced by consistent performance on CodeBLEU metrics across all evaluated languages.
- The integration of grammar prediction significantly improves token selection accuracy—e.g., correctly predicting 'in' instead of ':' in a keyword context.
- CodeIP shows resilience to crop attacks, with strong extraction performance even under partial input truncation.
- The watermark remains robust under variable-length message insertion, with high extraction accuracy across different message lengths.
- The approach outperforms baseline methods in both semantic fidelity and watermark extraction rate, especially in complex code structures.

Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.