Skip to main content
QUICK REVIEW

[Paper Review] Who Wrote this Code? Watermarking for Code Generation

T. Paul Lee, Seok‐Hee Hong|arXiv (Cornell University)|May 24, 2023
Advanced Malware Detection TechniquesComputer Science3 citations
TL;DR

This paper proposes SWEET, a watermarking method for code generation that selectively embeds watermarks only at high-entropy token positions to preserve code correctness while enabling reliable detection. By leveraging entropy-based thresholding, SWEET achieves a significant improvement in the trade-off between code correctness (pass@1 up to 3.3 points higher) and watermark detection performance, outperforming both post-hoc detection methods and prior watermarking approaches like WLLM in code generation tasks.

ABSTRACT

Since the remarkable generation performance of large language models raised ethical and legal concerns, approaches to detect machine-generated text by embedding watermarks are being developed. However, we discover that the existing works fail to function appropriately in code generation tasks due to the task's nature of having low entropy. Extending a logit-modifying watermark method, we propose Selective WatErmarking via Entropy Thresholding (SWEET), which enhances detection ability and mitigates code quality degeneration by removing low-entropy segments at generating and detecting watermarks. Our experiments show that SWEET significantly improves code quality preservation while outperforming all baselines, including post-hoc detection methods, in detecting machine-generated code text. Our code is available in https://github.com/hongcheki/sweet-watermark.

Motivation & Objective

  • Address the lack of effective watermarking techniques for code generation due to the syntactic rigidity and low entropy of programming language outputs.
  • Overcome the limitations of existing watermarking (e.g., WLLM) and post-hoc detection methods (e.g., DetectGPT) that fail to maintain code correctness or detectability in code generation.
  • Develop a watermarking strategy that selectively embeds signals only where they are least likely to disrupt code functionality.
  • Demonstrate that entropy-based thresholding enables robust watermark detection without degrading code quality.

Proposed method

  • SWEET uses entropy-based thresholding to identify generation steps with high uncertainty (high entropy) in the token probability distribution.
  • At high-entropy positions, the model is encouraged to generate 'green' tokens (watermark-enabled) via logit adjustment, while low-entropy positions remain unmodified to preserve correctness.
  • The watermark is detected via a statistical hypothesis test using the z-score of green token counts, leveraging entropy-aware thresholds.
  • The method dynamically adjusts watermarking intensity based on local entropy, avoiding interference in deterministic code segments.
  • It is designed to be compatible with general-purpose LLMs and code-specific models, showing transferability across architectures.
  • The approach avoids the pitfalls of uniform watermarking by focusing only on positions where token choice has high flexibility.
Figure 1: Illustrated comparison of WLLM (Kirchenbauer et al., 2023a ) and SWEET (ours). Note that this example is a short hypothetical explanatory example. LLMs can generate working source code (a) without a watermark. Strong watermark (b) or weak watermark (c) may result in detection or correctnes
Figure 1: Illustrated comparison of WLLM (Kirchenbauer et al., 2023a ) and SWEET (ours). Note that this example is a short hypothetical explanatory example. LLMs can generate working source code (a) without a watermark. Strong watermark (b) or weak watermark (c) may result in detection or correctnes

Experimental results

Research questions

  • RQ1Can watermarking be effectively applied to code generation without compromising code correctness due to the low entropy and syntactic constraints of programming languages?
  • RQ2How does entropy-based selection of watermarking positions improve the balance between detection performance and code correctness?
  • RQ3To what extent do existing post-hoc detection methods (e.g., DetectGPT, GPTZero) fail on short, low-entropy code outputs?
  • RQ4Is there a practical and robust way to set the entropy threshold without extensive hyperparameter tuning?
  • RQ5Can the proposed method be generalized across different LLM architectures and code generation benchmarks?

Key findings

  • SWEET significantly improves the Pareto frontier between code correctness (measured by pass@1) and watermark detection performance on HumanEval and MBPP benchmarks.
  • On HumanEval, SWEET achieved a 3.3-point improvement in pass@1 compared to baseline models, demonstrating strong preservation of code functionality.
  • Post-hoc detection methods like DetectGPT and GPTZero showed poor performance, with AUROC scores below 0.6 and TPR around 10% or lower on code generation tasks.
  • The failure of post-hoc methods is attributed to short code lengths (average 49–59 tokens) and poor perturbation curvature estimation due to low rephrasing flexibility in code.
  • The entropy threshold can be set reasonably without extensive tuning, as the method remains robust across different models and datasets.
  • SWEET also works effectively with general-purpose LLMs, not just code-specific models, indicating broad applicability.
Figure 2: An example of HumanEval/4 for comparing between (a) WLLM and (b)–(d) our SWEET with different thresholds. Text colors annotate whether tokens are in the green or red list. Gray tokens have entropy smaller than the threshold and are not watermarked. The intensity of the yellow background co
Figure 2: An example of HumanEval/4 for comparing between (a) WLLM and (b)–(d) our SWEET with different thresholds. Text colors annotate whether tokens are in the green or red list. Gray tokens have entropy smaller than the threshold and are not watermarked. The intensity of the yellow background co

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.