CN-122020620-A - Self-adaptive code watermark embedding method and system based on real-time feedback
Abstract
The invention discloses a self-adaptive code watermark embedding method and system based on real-time feedback, wherein the method comprises the steps of acquiring a currently generated code sequence in real time in the code generation process, inputting the acquired current code sequence into a pre-trained lightweight nature degree discriminator module to acquire scores reflecting the nature degree of the current code, inputting the acquired nature degree scores into a dynamic bias controller module, presetting target nature degree in the modules, calculating watermark bias values, inputting the watermark bias values into a watermark strategy and code generation module to generate new probability distribution after self-adaptive watermark bias adjustment, and constructing a real-time feedback control loop comprising perception, decision and execution by utilizing a newly generated Token from the new probability distribution.
Inventors
- YU DONGJIN
- CHEN SHUGUANG
- HU BIN
- HU TIANYI
Assignees
- 杭州电子科技大学
Dates
- Publication Date
- 20260512
- Application Date
- 20260123
Claims (10)
- 1. The adaptive code watermark embedding method based on real-time feedback is characterized by comprising the following operation steps: step S1, in the process of generating codes, acquiring a currently generated code sequence in real time; S2, inputting the obtained current code sequence into a pre-trained lightweight naturalness discriminator module to obtain a score reflecting the naturalness of the current code; S3, inputting the obtained naturalness score into a dynamic bias controller module, presetting a target naturalness expected to be maintained in the module, and calculating a watermark bias value; s4, inputting watermark offset values into a watermark strategy and code generation module to generate new probability distribution subjected to self-adaptive watermark offset adjustment; And S5, selecting the t+1th Token from the new probability distribution in a sampling mode, adding the t+1th Token to a code sequence, sending the newly generated Token to a lightweight nature degree discriminator, and starting a new round of evaluation and control cycle.
- 2. The method of claim 1, wherein the lightweight nature determination module in step S2 comprises a lightweight nature determination module, wherein the lightweight nature determination module is a pre-trained two-class model for distinguishing types of generated codes.
- 3. The method of claim 2, wherein in step S2, the increased code sequences are evaluated in parallel in real time during the code generation process by a lightweight nature discriminator module, and a nature score between 0 and 1 is output.
- 4. The method of claim 3, wherein the dynamic bias controller module in step S3 comprises a dynamic bias controller.
- 5. The method for adaptively embedding a code watermark based on real-time feedback as in claim 4, wherein said dynamically biasing controller comprises: calculating an error between the current naturalness and the target value; Calculating a watermark offset value to be adopted in the next generation step according to the error; the dynamic bias controller adopts proportional control to obtain a control law, and feedback is formed through the control law.
- 6. The method for adaptively embedding a code watermark based on real-time feedback as in claim 5, wherein an error between a current naturalness and a target value is detected Expressed as: , Wherein, the In order for the degree of naturalness of the object, Scoring naturalness; the control law is expressed as: , Wherein, the For the watermark offset value to be used in the next time step, For the watermark offset value used for the current time step, Is a proportional gain coefficient.
- 7. The method for adaptively embedding the code watermark based on real-time feedback according to claim 6, wherein the feedback is formed by the control law, wherein the method comprises the steps of automatically reducing watermark bias and weakening the watermark when the naturalness of the code is lower than a set target, and increasing watermark bias and strengthening watermark embedding when the naturalness of the code is higher than the set target.
- 8. The method for adaptively embedding a code watermark based on real-time feedback as in claim 7, wherein the operation of the watermark strategy and code generation module in step S4 comprises the steps of: when the t+1th Token is predicted by the large language model, dividing the whole word list into a green list and a red list by the watermark strategy and code generation module according to a preset watermark key and a current generation context; then, the original logic value distribution output by the large language model is interfered; Generating and outputting a new probability distribution after the self-adaptive watermark offset adjustment.
- 9. The method for embedding the adaptive code watermark based on real-time feedback according to claim 8, wherein the intervening of the original logic value distribution output by the large language model comprises the following steps: uniformly adding watermark offset values to all Token belonging to a green list and the original logic values of the Token; The original logical value remains unchanged for all Token belonging to the red list.
- 10. An adaptive code watermark embedding system based on real-time feedback is characterized in that the adaptive code watermark embedding method based on real-time feedback as set forth in any one of claims 1 to 9 is executed, and the adaptive code watermark embedding system comprises the following functional modules: the lightweight naturalness discriminator module is used for evaluating naturalness of the generated code sequence in real time; The dynamic bias controller module is used for dynamically calculating and outputting a watermark bias value at the next moment according to the naturalness evaluation result; And the watermark strategy and code generation module is used for receiving the watermark offset value, adjusting Token generation probability distribution of the large-scale language model and completing sampling generation.
Description
Self-adaptive code watermark embedding method and system based on real-time feedback Technical Field The invention relates to the technical field of artificial intelligence and software security, in particular to copyright protection of a large language model generated code, and particularly relates to a method and a system capable of dynamically adjusting watermark strength according to real-time quality of the generated code so as to balance watermark robustness and code quality. Background Along with the wide application of the large language model generated by codes, the demands of copyright authentication and traceability on the output codes of the codes are increasingly urgent. Code watermarking techniques are one solution aimed at embedding specific, detectable patterns in the model-generated code to enable copyright statement or content tracking. Existing mainstream large-scale language model code watermarking technology, such as a method based on a green list and a variant thereof, generally adopts a global fixed offset value to promote the generation probability of Token in the green list so as to embed watermark. However, this fixed strength embedding has a fundamental contradiction in the detectability (robustness) of the watermark and the quality of the generated code. The method is characterized in that the code quality is damaged by pursuing high detection rate, and a larger bias value is required to be set in order to ensure that the watermark has high robustness in subsequent detection. This can significantly distort the original Token probability distribution of the model, resulting in unnatural patterns in the generated code (e.g., particular variable names, function names, or grammar structures are used abnormally frequently). Such patterned code not only reduces its naturalness and usability, but also is easily analyzed and located by an attacker due to statistical anomalies. Maintaining the code quality results in weak watermark, and if a smaller offset value is adopted to maintain the naturalness of the code, the watermark signal is too weak, which results in low detection rate and is difficult to meet the actual requirement of copyright protection. The prior art generally adopts a compromise strategy, i.e. sets a global, moderate fixed offset value to seek a static balance between detectability and code quality. Although this approach reconciles the contradictions to some extent, it has the fundamental disadvantage of lacking flexibility-it cannot accommodate the ever-changing context in the code generation process. In practical generation, the Token sensitivity of different positions is quite different from disturbance, namely, in certain 'safe' positions (such as user-defined identifiers and annotation contents), the naturalness and correctness of codes are not damaged by using larger bias values, and in grammar key or semantic sensitive positions, the same bias is extremely easy to cause unnatural or wrong output, and uniform fixed bias cannot realize the fine and context-aware intensity adjustment, so that the dynamic optimization problem between robustness and code quality is still a global compromise, and the dynamic optimization problem between robustness and code quality is not fundamentally solved. Disclosure of Invention The invention aims to provide a self-adaptive code watermark embedding method and system based on real-time feedback, which are used for solving the problems in the background technology by constructing a real-time feedback control loop comprising sensing, decision and execution, and dynamically adjusting the magnitude of a bias value according to the real-time quality feedback of generated codes instead of using a fixed bias value. The invention provides a method for embedding a self-adaptive code watermark based on real-time feedback, which comprises the following operation steps: and step S1, acquiring a currently generated code sequence in real time in the code generation process. And S2, inputting the obtained current code sequence into a pre-trained lightweight naturalness discriminator module to obtain a score reflecting the naturalness of the current code. Preferably, the lightweight nature discriminator module includes a lightweight nature discriminator, which is a pre-trained two-classification model for distinguishing "human written code" from "AI generated code". The model has light structure and rapid response, can evaluate continuously growing code sequences in parallel in the generation process in real time, and outputs a naturalness fraction between 0 and 1。 And step S3, inputting the obtained naturalness score into a dynamic bias controller module, presetting a target naturalness expected to be maintained in the module, and calculating a watermark bias value. Preferably, the dynamic bias controller first calculates an error between the current naturalness and the target value: Then calculates the watermark offset value to be adopted in the