Vanishing Ink: Code Obfuscation Breaks N-gram Code Watermarks in Theory and PracticeDisappearing Ink: Obfuscation Breaks N-gram Code Watermarks in Theory and Practice
The authors model code obfuscation as a Markov random walk, theoretically prove and experimentally verify that it can reduce the detection AUROC of N-gram code watermarks to around 0.5.
The attacker obtains watermarked high-quality code and applies semantics-preserving transformations (code obfuscation) to it while keeping the program's functional behavior unchanged, with the goal of making the watermark detector fail (miss detection rate approaching 1 − ϵpos). The attack uses off-the-shelf CPU tools and requires no access to the model's internal parameters.
- Existing robustness evaluations of N-gram code watermarks mostly target simple edits, which the authors argue overestimates security, because mature code obfuscation techniques in software engineering can break N-gram features while preserving semantics.
- Model semantics-preserving transformations as a Markov random walk, prove that under the distribution consistency assumption obfuscation can make the detector's miss rate approach 1 − ϵpos; verify using off-the-shelf obfuscation tools under multiple settings.
- Across 3 watermarking schemes, 2 models, 2 languages, and 4 benchmarks, detection AUROC after obfuscation all dropped to around 0.5; the distribution consistency assumption held in 96.11% of the test space.
- The authors note that the study is limited to N-gram watermarking schemes and does not cover non-N-gram methods; the code quality evaluation focuses only on execution behavior (Pass@1) and does not cover dimensions such as readability and maintainability.
- Evaluated models
- LLaMA-3.1-8B-InstructDeepSeek-Coder-33B-Base
- Benchmarks
- HumanEvalMBPP+HumanEval-X-JSMBPP-JSSWE-bench Verified
- Metrics
- AUROCPass@1FNRz-score
The paper models code obfuscation as a Markov random walk over equivalent programs, and proves that under the distribution consistency assumption, obfuscation raises the miss rate of N-gram code watermarks to about 1−εpos, causing the detector to lose its ability to distinguish. The authors validate this on three watermarking schemes, SWEET, WLLM and SynthID, two models, LLaMA-3.1-8B-Instruct and DeepSeek-Coder-33B-Base, two languages, Python and JavaScript, four benchmarks and four off-the-shelf obfuscators; after obfuscation AUROC is concentrated around 0.5, and for each language at least one obfuscator brings all results below 0.6. Anderson–Darling tests on 1,620 equivalence spaces did not reject the distribution consistency assumption for 96.11%. On this basis the authors argue that surface-level N-gram-based signals are insufficient to support robust code watermarks, and propose the design direction of semantics-aware watermarking.
Why it was selectedThe paper proves and empirically tests that obfuscation can degrade detection by multiple N-gram code watermarks to random levels, providing a reproducible attack and criterion for evaluating watermark robustness.
Paper analysis
What problem does this paper attempt to solve?
Whether existing N-gram code watermarks remain robust under semantics-preserving code obfuscation attacks.
Core question: whether N-gram code watermarks are robust under realistic code obfuscation attacks.
- Problem setting: LLM-generated code is increasingly prevalent, requiring reliable watermarking techniques for attribution tracking and misuse detection; existing code watermarking schemes are mostly based on N-gram statistical features.
- Existing gaps: The authors point out that prior robustness evaluations of code watermarks mostly targeted only simple edits, optimizations, or refactoring, rather than realistic code obfuscation attacks, which overestimates security.
- Core observation: Code obfuscation can perturb N-gram features more aggressively than natural language paraphrasing while preserving program semantics; mature obfuscation tools can already automate such transformations.
- Paper contributions: Model obfuscation as a Markov random walk, theoretically prove that under the distribution consistency assumption N-gram watermarks can be made ineffective, and verify this experimentally.
Threat model:
- Attacker goal: Make watermarked code evade detection, i.e., make the detector output 0 (not watermarked).
- Attacker knowledge: No access to model internal parameters or watermark keys; uses off-the-shelf CPU obfuscation tools.
- Attacker capability: Perform semantics-preserving transformations on watermarked high-quality code, keeping program functional behavior unchanged.
- Victim system: Code watermark detectors based on N-gram statistical features (e.g., WLLM, SWEET, SynthID).
What related research exists?
Related research covers LLM watermarking, code obfuscation, paraphrase attacks, and watermark impossibility results.
LLM watermarking techniques
- Kirchenbauer et al. (2023) proposed the Green-Red watermark (WLLM), which partitions a green list by hashing preceding tokens and adds a logit bias.
- Lee et al. (2024) proposed SWEET, optimized for code generation scenarios, using an entropy threshold to select watermark positions.
- Dathathri et al. (2024) proposed SynthID, which uses a distortion-free sampling strategy and has been deployed industrially.
Code obfuscation
- Banescu and Pretschner (2018), Behera and Bhaskari (2015), and others systematically summarized code obfuscation techniques, including variable renaming, dead code insertion, control flow transformation, etc.
- These tools (e.g., Python-Minifier, UglifyJS) can significantly change code surface features while preserving functionality.
Paraphrase attacks against watermarks
- Krishna et al. (2023), Sadasivan et al. (2023), and others studied paraphrase attacks in natural language, using recursive paraphrasing methods such as DIPPER to break watermarks.
- The authors point out that code obfuscation preserves semantics better than natural language paraphrasing and perturbs N-grams more strongly.
Watermark impossibility results
- Zhang et al. (2024) gave an impossibility proof for the robustness of general language model watermarks, but the authors argue that its assumption (quality oracle) does not hold in code generation scenarios (Appendix G).
Baselines and comparisons
- The paper selects WLLM, SWEET, and SynthID as target watermarking schemes, and uses Python-Minifier, PyMinifier, JS Obfuscator, and UglifyJS as attack tools.
The authors position the distinction of this paper from prior work: it proposes a realizable attack model based on semantics-preserving rules for the code scenario, rather than relying on unrealistic quality oracle assumptions.
How does the paper address this problem?
Model code obfuscation as a Markov random walk and prove that under the distribution consistency assumption it can break N-gram watermarks.
Overall approach: abstract semantics-preserving code obfuscation as a Markov random walk, theoretically prove that it can make N-gram watermark detection ineffective, and instantiate the attack with off-the-shelf obfuscation tools.
Threat model and formalization
- The attacker receives watermarked high-quality code c ∈ ΦF and outputs a transformed program Att(x, c) ∈ ΦF, keeping functional behavior unchanged.
- The attack goal is to maximize the detector's miss rate Pr[Detectk(x, c′) = 0].
Random walk and equivalence space
- Model the obfuscation rule set Γ as a random walk on the high-quality program space ΦF; each vertex is a program, and edges are single-step semantics-preserving transformations.
- Theorem 3.1: For an ergodic rule set, the random walk converges to a unique stationary distribution π, independent of the starting program.
- Theorem 3.2: An ergodic rule set partitions the high-quality space into disjoint rule equivalence spaces.
Distribution consistency assumption
- Definition 3.3: A detector satisfies distribution consistency if its output distribution within each equivalence space is the same as its distribution over the entire code space (i.e., the false positive rate ϵpos).
- Assumption 3.1: For N-gram watermarks, there exist realizable semantics-preserving transformation rules whose induced partition satisfies distribution consistency.
Robustness impossibility
- Theorem 3.3: Under Assumption 3.1, an attacker using a random walk can make the post-obfuscation miss rate approach 1 − ϵpos.
- Theorem 3.4: Under a finite number of steps, the miss rate is at least 1 − ϵpos − ϵ, where ϵ grows logarithmically with the mixing time.
Attack instantiation
- Use four off-the-shelf obfuscation tools: Python-Minifier, PyMinifier (Python); JS Obfuscator, UglifyJS (JavaScript).
- These tools apply fixed transformation rules and can be viewed as deterministic implementations of the random walk; Appendix L also implements a random-walk obfuscator corresponding to the theory.
What experiments did the paper conduct?
The attack effect was verified on 3 watermarks, 2 models, 2 languages, and 4 benchmarks, and detection AUROC all dropped to around 0.5.
Experimental setup
- Models tested: LLaMA-3.1-8B-Instruct, DeepSeek-Coder-33B-Base; the SWE-bench experiments use Qwen3.5-35B-A3B.
- Watermarking schemes: WLLM, SWEET, SynthID; Appendix O also evaluates ACW.
- Benchmarks: HumanEval and MBPP+ for Python; HumanEval-X-JS and MBPP-JS for JavaScript; SWE-bench Verified (50 tasks) for repository level.
- Attack tools: Python-Minifier, PyMinifier, JS Obfuscator, UglifyJS; Appendix L uses a theoretically corresponding random-walk obfuscator.
- Metrics: code quality uses Pass@1; watermark detectability uses AUROC (comparing the z-score distribution with the standard normal distribution).
- Hyperparameters: WLLM/SWEET sweep γ ∈ {0.1, 0.25, 0.5}, δ ∈ {0.5, 1.0, 2.0, 3.0, 4.0}; SWEET additionally sweeps τ ∈ {0.3, 0.6, 0.9, 1.2}; SynthID sweeps temperature {0.25, 0.5, 0.75, 1.0, 1.25}.
Main results
Swipe horizontally to read the table
Watermarking scheme Model Language AUROC before attack AUROC after attack Source WLLM LLaMA-3.1-8B Python >0.9 (some settings) concentrated near 0.5 Figure 5 SWEET LLaMA-3.1-8B Python >0.9 (some settings) concentrated near 0.5 Figure 5 SynthID LLaMA-3.1-8B Python rises to 0.9 with temperature concentrated near 0.5 Figure 6 WLLM DeepSeek-Coder Python ≈0.8 concentrated near 0.5 Figure 8 SWEET DeepSeek-Coder Python ≈0.8 concentrated near 0.5 Figure 8 SynthID DeepSeek-Coder Python 0.9 (temperature 1.25) concentrated near 0.5 Figure 9 WLLM LLaMA-3.1-8B JavaScript >0.9 (some settings) concentrated near 0.5 Figure 5 SWEET LLaMA-3.1-8B JavaScript >0.9 (some settings) concentrated near 0.5 Figure 5 - The authors state that before the attack, all schemes reach AUROC > 0.9 while maintaining more than 80% non-watermarked Pass@1; after the attack, AUROC does not exceed 0.6 in all settings.
- For each language, at least one obfuscator (Python-Minifier and JS Obfuscator) brings AUROC in all settings of all three watermarking schemes into (0.4, 0.6).
- Only 9/1280 (0.7%) of the post-obfuscation data points fall outside (0.4, 0.6), and none of these points maintain more than 80% non-watermarked Pass@1.
Effect of N-gram length
- Using WLLM, JS Obfuscator, and LLaMA 3.1, 2- to 5-grams were tested.
- Average AUROC before the attack is 0.812–0.824; after the attack, AUROC in all settings is around 0.5, with no data points exceeding 0.6.
- The authors state that changing the N-gram length cannot restore robustness.
Distribution consistency test
- 1,620 highly watermarked Python programs (z-score > 4) were extracted from the experiments, and a random-walk obfuscator was used to sample equivalence spaces.
- An Anderson–Darling test was performed on each space, with the null hypothesis that z-scores follow the standard normal distribution.
- At the 5% significance level, 1,557/1,620 (96.11%) of the spaces do not reject the null hypothesis; the proportion per scheme ranges from 95.39% to 98.11% (Table 1).
Other ablations and analyses
- Ideal watermarking scheme: An ideal N-gram watermark with global awareness and test-oracle assistance was constructed; AUROC before the attack is 0.9747 and Pass@1 is 0.9634; after the UglifyJS attack, AUROC drops to 0.5085 (Table 3).
- Random-walk obfuscator: Across 43 valid settings, AUROC in all settings falls into (0.4, 0.6), averaging 0.512; with advanced rules disabled, 41/43 still fall into the interval (Table 4).
- Repository-level tasks: On SWE-bench Verified, WLLM's average AUROC before the attack is 0.612 and drops to 0.491 after the attack; AUROC in all 7 quality-preserving settings is around 0.5 (Table 5).
- Alternative negative classes: For the WLLM Python experiments, AUROC was recomputed using four negative classes (non-watermarked LLM code, human code, and non-watermarked code processed by two obfuscators); after the attack, the means are all about 0.518, and 240/240 fall into (0.4, 0.6) (Table 6).
- ACW scheme: 3,280 programs were generated on HumanEval; after Python-Minifier obfuscation, AUROC drops to 52.92%.
What are some points that can be further explored?
The authors point out that the study is limited to N-gram watermarks and code execution behavior quality, and does not cover other watermark types or code quality dimensions.
Limitations and future directions noted by the authors
- Limited to N-gram watermarks: The authors explicitly state that this work is limited to N-gram watermarking schemes and does not cover recently proposed non-N-gram methods (e.g., Zhao et al., 2024; Liu and Bu, 2024) (Limitations section).
- Single dimension of code quality: The authors acknowledge that this work evaluates code quality only through execution behavior (Pass@1) and does not cover dimensions such as readability, maintainability, or efficiency; future work could develop obfuscation techniques that preserve multi-dimensional code quality (Limitations section).
- Semantics-aware watermark direction: The authors discuss a possible defense path of breaking distribution consistency by making watermarks more semantics-aware and give a proof-of-concept design (Appendix H), but note that demonstrating code watermark robustness is outside the scope of this paper and may come with high implementation cost.
Experimental coverage
- The models tested are LLaMA-3.1-8B-Instruct, DeepSeek-Coder-33B-Base, and Qwen3.5-35B-A3B (SWE-bench only), 3 in total.
- The watermarking schemes evaluated include WLLM, SWEET, SynthID, and ACW (Appendix O), 4 in total.
- The obfuscation tools used are Python-Minifier, PyMinifier, JS Obfuscator, and UglifyJS, 4 in total; a theoretically corresponding random-walk obfuscator was also implemented (Appendix L).
- Benchmarks cover function level (HumanEval, MBPP+, HumanEval-X-JS, MBPP-JS) and repository level (SWE-bench Verified, 50 tasks).
- The paper does not report specific numbers for the number of queries or iteration budget required for the attack; it does not report agreement between review and human evaluation.
Summarize the main content of the paper
The paper proves and verifies that code obfuscation can break N-gram code watermarks, reducing their detection performance to random level.
This paper studies the robustness of N-gram code watermarks under semantics-preserving code obfuscation attacks. The authors model obfuscation as a Markov random walk and prove that, under the distribution consistency assumption, the attack can make the detector's miss detection rate approach 1 − ϵpos (Theorem 3.3).
- Method: Off-the-shelf obfuscation tools (Python-Minifier, PyMinifier, JS Obfuscator, UglifyJS) are used as attack instances, requiring no access to the model's internal parameters.
- Theoretical verification: Of 1,620 equivalence spaces, 96.11% pass the distribution consistency test (Table 1).
- Experimental results: Across the three watermarking schemes WLLM, SWEET, and SynthID, two models, two languages, and four benchmarks, detection AUROC after obfuscation is concentrated near 0.5, with no setting exceeding 0.6; only 0.7% of data points fall outside the (0.4, 0.6) interval.
- Extended experiments: For the ideal watermarking scheme, AUROC drops from 0.9747 to 0.5085 after the attack; on repository-level tasks (SWE-bench Verified), average AUROC drops from 0.612 to 0.491; for the ACW scheme, AUROC drops to 52.92% after the attack.
- Authors' conclusion: Current N-gram code watermarks are not robust to realistic obfuscation attacks and require more semantics-aware alternatives; the authors note that the study is limited to N-gram schemes and that the code quality evaluation focuses only on execution behavior.