Towards Tracing Code Provenance with Code Watermarking

Wei Li, Borui Yang, Yujie Sun, Suyu Chen, Ziyun Song, Liyao Xiang, Xinbing Wang, Chenghu ZhouarXiv 2023 · DOI

Abstract

The rapid adoption of Large Language Models (LLMs) for code generation has intensified the need for reliable code provenance methods. This paper introduces CodeMark, a novel watermarking system designed to embed digital signatures into source code without compromising functionality or naturalness. CodeMark renames variables based on their local context, using Graph Neural Networks (GNNs) and a pre-trained CodeBERT "teacher" model to ensure the resulting code remains semantically identical and human-readable. Evaluation shows that CodeMark achieves a 99.81% functional preservation rate and significantly higher capacity than existing state-of-the-art methods.

Introduction

As AI-generated code becomes ubiquitous, identifying the source of code snippets is crucial for intellectual property protection and security auditing. Source code is unique in that it serves two purposes: a "formal channel" for machine execution and a "natural channel" for human comprehension. Traditional watermarking often disrupts one of these channels, either by obfuscating the code or by introducing syntax errors that prevent compilation. CodeMark addresses this by hiding data within variable names—the natural channel—while strictly adhering to the formal constraints of the programming language.

Methodology

CodeMark treats source code as a graph rather than a sequence of tokens to better capture semantic relationships. The process involves three main stages:

  1. Variable Context Graph Construction: For each target variable, CodeMark generates a graph based on the Abstract Syntax Tree (AST), capturing relationships between the variable and its neighboring operations and tokens.
  2. Contextual Embedding and Extraction: A GNN with multi-head attention is employed. The watermark bit string determines which attention head is used during embedding. This ensures the watermark is intrinsically linked to the variable's context. For extraction, a GNN-based classifier identifies the head used, thereby retrieving the embedded bits.
  3. Knowledge Distillation for Naturalness: To prevent the generation of suspicious or nonsensical variable names, CodeMark uses knowledge distillation from a pre-trained CodeBERT model. This "teacher" model guides the system toward selecting variable names that are contextually appropriate and natural to human developers.
Overview of CodeMark's variable context graph and embedding/extraction architecture.

Results and Discussion

The effectiveness of CodeMark was evaluated against state-of-the-art natural language watermarking tools, including AWT and CALS, using the CodeSearchNet-Java dataset.

  • Operational Semantics: CodeMark demonstrated superior performance in preserving code logic, achieving a 99.81% pass rate on AST and keyword checks. In contrast, baseline methods frequently failed these checks, as shown in Figure 5.
  • Capacity and Efficiency: CodeMark provides a capacity increase of 1.29x to 5.13x over baselines, allowing more information to be stored per snippet. Furthermore, the system is highly efficient, with embedding and extraction times under 0.1 seconds.
  • Robustness: The system proved resilient against common code transformations, such as block-level and attribute changes, maintaining high extraction accuracy.
Comparison of semantic preservation (AST and Keyword checks) between CodeMark and baseline methods.

Conclusion

CodeMark represents a significant advancement in code watermarking by successfully balancing the dual requirements of formal correctness and natural readability. By leveraging graph-based representations and deep learning, it provides a high-capacity, robust solution for tracing code provenance in the age of AI-assisted programming. Future research will focus on expanding support to other programming languages and improving capacity in code with low variable density.

Reference: Li, W., Yang, B., Sun, Y., et al. (2023). Towards Tracing Code Provenance with Code Watermarking. arXiv:2305.12461v1 [cs.CR].

Summary updated

BibTeX
@misc{li2023towards,
  author = {Wei Li and Borui Yang and Yujie Sun and Suyu Chen and Ziyun Song and Liyao Xiang and Xinbing Wang and Chenghu Zhou},
  title = {{Towards Tracing Code Provenance with Code Watermarking}},
  year = {2023},
  eprint = {2305.12461},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2305.12461},
}
Figure