Grammar guided watermarking improves code generation detection with minimal errors

Grammar-Guided Code Watermarking with Green Temperature

Cryptography and SecurityArtificial Intelligence

Summary

Embedding hidden signals in AI-generated code can help identify if code was machine-made but often causes small mistakes that break the program. The authors propose a way to guide the watermark process using programming language grammar rules, so the watermarks don’t create syntax or behavior errors. Their method, called GTCW, identifies where watermarks can safely be added and adjusts probabilities only among grammar-allowed options, making the watermark stronger but code still works well. Tests on multiple programming languages and AI models show the watermark is easier to detect without harming code quality.

What this means in practice

  • For software development teams: Embed detectable watermarks in generated code to trace source without causing syntax errors or behavioral bugs.
  • For ai model deployers: Add robust watermark signals during code generation to verify AI authorship while maintaining output quality.

Authors

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 .