Toward Stronger Code Watermarking: A Grammar-Driven Approach to Optimizing the Trade-off Between Quality and Detectability

📅 2026-07-11
📈 Citations: 0
Influential: 0
📄 PDF
🤖 AI Summary
This work addresses the inherent tension between code quality and watermark detectability in existing logits-based text watermarking methods for code generation, a conflict exacerbated by the low entropy of source code. To resolve this, the authors propose Grammar-Driven Watermarking (GDW), the first approach that integrates syntactic structure into watermark design. GDW employs a grammar-guided three-level masking mechanism to guarantee syntactic validity, applies strong bias to content-bearing tokens and weak bias to syntax-critical tokens through structure-aware modulation, and introduces a role-aware weighted detection statistic aligned with the generation process. Experiments demonstrate that GDW consistently outperforms state-of-the-art methods across multiple programming languages, models, and decoding strategies, achieving superior watermark detectability without compromising code quality and exhibiting robustness against variable renaming attacks.
📝 Abstract
With the rapid development of Large Language Models (LLMs), text watermarking has emerged as a crucial technique for identifying machine-generated content. However, directly applying existing logits-based watermarking methods to code generation remains challenging, since the low-entropy nature of code exacerbates the trade-off between code quality and watermark detectability. In this paper, we propose a novel code watermarking approach called Grammar-Driven Watermark (GDW) for LLMs. GDW preserves syntactic validity through a grammar-guided three-level masking mechanism and injects watermark signals via structural role-aware modulation, assigning a stronger bias to content-bearing tokens while applying a more conservative bias to syntax-critical tokens. Aligning with the generation process, we further design a role-aware weighted detection statistic to improve detectability. Experiments across multiple programming languages, models, and decoding strategies show that GDW establishes a stronger quality-detectability trade-off frontier than existing methods, while maintaining robustness against variable-renaming attacks.
Problem

Research questions and friction points this paper is trying to address.

code watermarking
quality-detectability trade-off
low-entropy code
LLM-generated code
watermark detectability
Innovation

Methods, ideas, or system contributions that make the work stand out.

code watermarking
grammar-guided masking
role-aware modulation
quality-detectability trade-off
LLM-generated code