arXiv:2610.05323cs.CROct 2026

Manuscript Offprint

arXiv:2610.05323

Grammar-Guided Code Watermarking with Green Temperature

Hyundong Jin, Hyeseon An, Soohan Lim, Yo-Sub Han

ABSTRACT

Large language model watermarking embeds detectable statistical signals during decoding, but the resulting changes to token probabilities can degrade generation quality. This trade-off is particularly important for code, where small changes in token selection can break syntax or alter program behavior. Existing code watermarking methods mitigate this risk through entropy-based insertion or syntax-aware token selection, but they do not directly construct the watermark over the set of continuations admitted by the current grammar state. We propose Grammar-Guided Code Watermarking with Green Temperature (GTCW), which integrates grammar-constrained decoding with probability-aware watermarking. At each decoding step, GTCW restricts the candidate set to grammar-admissible tokens and partitions this support into keyed green and red subsets. At eligible high-entropy positions, green temperature reweights the green tokens according to the model's relative preferences, strengthening the watermark signal while retaining the grammar constraint. Across five models and five benchmarks spanning four programming languages, GTCW achieves a mean AUROC of 73.61%, compared with 67.83% for the strongest baseline, while maintaining a mean Pass@1 of 59.18% versus 59.58% for unwatermarked generation. Our implementation is available at https://github.com/hyundong98/GTCW.

1 INTRODUCTION

Large language models are increasingly used to generate executable code that can be directly incorporated into software systems (Hou et al., 2024). Unlike transient model interactions, generated code may be copied, modified, and redistributed independently of the model or service that produced it, making its provenance difficult to recover after generation. Reliable attribution of model-generated

Figure

Figure

Figure 1: AUROC-Pass@1 tradeoff (left) and TPR@5%FPR (right), averaged equally over five models, five benchmarks, and three seeds. The star marks GTCW and the dashed line denotes unwatermarked Pass@1 in the left panel.

code is therefore becoming increasingly important as code generation is adopted more broadly in software development. Generation-time watermarking addresses this problem by embedding a detectable statistical signal into the generated code without retraining the underlying model (Kirchenbauer et al., 2023; Zhao et al., 2024; Dathathri et al., 2024).

Many generation-time watermarks partition candidate tokens into keyed green and red sets and modify the sampling distribution in favor of the green set (Kirchenbauer et al., 2023; Zhao et al., 2024; Mao et al., 2025; Guo et al., 2024). For code, however, perturbing token probabilities can break syntax or alter program behavior. Existing code watermarking methods reduce this risk through entropy-based insertion or lexical and structural constraints (Lee et al., 2024; Kim et al., 2026; Guan et al., 2024; Guo & Cheng, 2025). These approaches improve the choice of watermark carriers, but do not directly construct the watermark over the token set admitted by the current grammar state. A second source of distortion comes from how the selected tokens are reweighted. Common green-list methods apply the same additive bias to all green tokens, including those that the model originally assigns low probability (Kirchenbauer et al., 2023; Lee et al., 2024; Kim et al., 2026). In code generation, promoting such weak candidates can move probability mass toward syntactically or functionally undesirable continuations rather than preserving the model's relative preference within the green set.

We propose Grammar-Guided Code Watermarking with Green Temperature (GTCW) to address both carrier selection and probability reweighting in code watermarking. For carrier selection, grammar-constrained decoding restricts the candidate set to admissible continuations, and GTCW constructs the keyed green and red partition only within this support. For probability reweighting, green temperature operates at eligible high-entropy positions and adjusts green tokens according to their model logits. This gives greater emphasis to candidates already preferred by the model rather than uniformly boosting all green tokens. Detection replays the same grammar and keyed partitioning procedure on the observed completion without requiring stored generation traces.

We evaluate GTCW on five models and five benchmarks covering Python, C++, Java, and Go. Across the 25 model-benchmark configurations, GTCW achieves a mean AUROC of 73.61%, compared with 67.83% for the strongest baseline, while its mean Pass@1 is 59.18% compared with 59.58% for unwatermarked generation. At TPR@5%FPR, GTCW reaches 41.86%, compared with 24.50% for the strongest baseline. Figure 1 summarizes these aggregate results.

References (47)