针对代码生成中的水印质量与可检测性矛盾,提出语法驱动的水印方法。
Toward Stronger Code Watermarking: A Grammar-Driven Approach to Optimizing the Trade-off Between Quality and Detectability

- 通过语法引导的三级掩码保持代码结构正确性。
- 在内容词元上强加水印偏置,语法词元则保守处理,提升可检测性。
- 对多种语言和模型均有效,且抗变量重命名攻击。
随着大语言模型(LLMs)的快速发展,文本水印已成为识别机器生成内容的关键技术。然而,直接将现有的基于logits的水印方法应用于代码生成仍具挑战性,因为代码的低熵特性加剧了代码质量与水印可检测性之间的权衡。本文提出一种名为语法驱动水印(Grammar-Driven Watermark, GDW)的新方法。GDW通过语法引导的三级掩码机制保持语法有效性,并通过结构角色感知调制注入水印信号,对承载内容的词元施加更强偏置,而对语法关键词元采用更保守的偏置策略。同时,根据生成过程设计角色感知加权检测统计量以提升可检测性。在多种编程语言、模型及解码策略下的实验表明,GDW在质量-可检测性权衡曲线上优于现有方法,且对变量重命名攻击具有鲁棒性。
原文摘要 · Abstract (English)
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.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。