Abstract
The proliferation of AI-generated code and open-source software has increased the risk of plagiarism and unauthorized redistribution. Traditional code watermarking techniques often rely on brittle, language-specific rules or require expensive model training. This paper introduces CodeMark-LLM, a training-free framework that leverages the semantic reasoning capabilities of Large Language Models (LLMs) to embed and extract invisible watermarks. By applying context-aware, functionality-preserving transformations, CodeMark-LLM achieves high robustness and near-perfect functional correctness across diverse programming languages.
Introduction
Code watermarking is a critical defense mechanism for protecting intellectual property in software development. Unlike image or audio watermarking, code watermarking is uniquely challenging because even minor syntactic changes can alter program semantics or break execution. Existing methodologies typically suffer from three primary limitations: reliance on hand-crafted rules that do not scale, dependency on language-specific parsers, and vulnerability to simple refactoring attacks.
As shown in the comparison above, CodeMark-LLM addresses these gaps by being the first framework to simultaneously offer training-free deployment, automatic rule generation, and language-agnostic robustness.
Methodology: The CodeMark-LLM Framework
CodeMark-LLM replaces rigid manual logic with the fluid reasoning of an LLM, such as GPT-4o. The framework operates through two primary modules: Semantically Consistent Embedding and Differential Comparison Extraction.
Semantically Consistent Embedding
Instead of applying a fixed set of transformations, the system uses an LLM to analyze code snippets and select natural-looking, functionality-preserving changes. These may include swapping loop types (e.g., for to while), renaming variables, or reordering independent declarations. Watermark bits are embedded by choosing whether to apply specific categories of these transformations. This context-aware approach ensures the resulting code remains readable and "natural."
Differential Comparison Extraction
To recover the watermark, CodeMark-LLM utilizes a "detective" approach. It first retrieves the most likely original code from a database using multi-feature similarity search. An LLM then compares the original and the suspect watermarked code to identify which specific transformations were applied. This reasoning-based extraction is highly resilient to further modifications by attackers, as the LLM can often infer the original intent despite subsequent obfuscation.
Experimental Results
The framework was evaluated using diverse benchmarks, including CodeSearchNet (Java, JavaScript) and MBXP (C++, Java, JavaScript, Python).
- Accuracy and Integrity: CodeMark-LLM achieved a bit accuracy exceeding 97% and a message accuracy over 95%. Crucially, in execution-based tests (MBXP), the watermarked code maintained a ~99% pass rate, significantly outperforming baselines like AWTcode, which often failed to preserve functional logic.
- Naturalness: By leveraging LLM reasoning, the transformations appear more "natural" than rule-based methods. This is reflected in high CodeBLEU and MRR scores, making the watermark difficult for human observers or automated tools to detect.
- Robustness: The system demonstrated high resilience against "Random Removal Attacks" (e.g., total variable renaming) and "Adaptive De-watermarking" attempts using other LLMs to paraphrase the code.
- Efficiency: Being training-free, CodeMark-LLM is highly cost-effective. The estimated cost of watermarking a single sample is approximately $0.0020, avoiding the substantial GPU costs associated with training specialized models.
Conclusion
CodeMark-LLM represents a significant advancement in code protection by utilizing the inherent knowledge and reasoning of LLMs. While currently focused on function-level watermarking and dependent on commercial APIs, its ability to provide robust, language-agnostic protection without training makes it a highly practical solution for modern software ecosystems. Future work will explore extending these techniques to module-level protection and cross-file watermarking for large-scale projects.
Summary updated