A method, apparatus, device, and medium for detecting smart contract vulnerabilities based on large language models and retrieval enhancement.

By combining large language models and retrieval-enhanced generation methods with ThinkChain technology and the RAG system, the problem of existing technologies failing to fully cover complex code paths in smart contract vulnerability detection has been solved, achieving efficient and accurate vulnerability detection and ensuring the security of smart contracts.

CN120541845BActive Publication Date: 2026-11-17GUANGDONG UNIV OF TECH
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202510611664.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-05-13
Publication Date
2026-11-17
Estimated Expiration
2045-05-13

AI Technical Summary

Technical Problem

Existing smart contract vulnerability detection methods based on large language models and retrieval enhancements struggle to fully cover execution paths when faced with complex code, easily missing hidden vulnerabilities and resulting in insufficient detection.

Method used

By employing a large language model and retrieval-enhanced generation method, and guiding the model to gradually analyze code logic through thinking chain technology, combined with static code analysis and dynamic behavior monitoring, the RAG system is used to learn the feature patterns of massive annotated vulnerability cases to build a comprehensive detection system.

Benefits of technology

It significantly improves the accuracy and efficiency of smart contract vulnerability detection, enabling rapid identification of abnormal structures and risk points in the code, and ensuring the reliability and comprehensiveness of the detection results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120541845B_ABST
    Figure CN120541845B_ABST
Patent Text Reader

Abstract

This application relates to a method, apparatus, device, and medium for detecting smart contract vulnerabilities based on a large language model and retrieval enhancement. The method includes: inputting the smart contract to be detected into a smart contract detection tool to determine the tool's vulnerability detection results; inputting a code block into a preset RAG model, retrieving similar data items corresponding to the code block from a preset vulnerability contract dataset, and using a RAG model generator to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data items corresponding to the code block to determine the RAG model's vulnerability detection results; and inputting the code block corresponding to the smart contract to be detected, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model into a large language model enhanced by a thought chain to determine the smart contract vulnerability type of the smart contract to be detected and its corresponding vulnerability scenario description. This application achieves efficient and accurate detection of smart contract vulnerabilities.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of cybersecurity, and in particular to a method, apparatus, electronic device and computer-readable storage medium for detecting smart contract vulnerabilities based on large language models and retrieval enhancement generation. Background Technology

[0002] In recent years, with the widespread and in-depth application of blockchain technology in finance, healthcare, supply chain, and other fields, the security of smart contracts, as a core functional component of blockchain, has become increasingly critical. Smart contracts achieve automatic execution and tamper-proof transaction verification through the distributed ledger characteristics of blockchain. Their security vulnerabilities directly threaten the stability of various industry application systems built on blockchain, often leading to serious economic losses and other security risks. Traditional detection methods for smart contract vulnerabilities include static analysis, dynamic analysis, and formal verification, but their limitations become apparent when dealing with complex code.

[0003] The decentralized, transparent, and tamper-proof characteristics of blockchain are transforming the operating models of numerous industries. In the financial sector, it makes cross-border payments and securities trading more efficient and secure; in the medical field, it enables the secure sharing of electronic medical records, significantly improving the quality and efficiency of medical care; in the supply chain, the flow of goods becomes accurate and traceable thanks to blockchain, ensuring quality and origin. These deep applications of blockchain all rely on the stable operation of smart contracts. As a core automated program within the blockchain, smart contracts are responsible for automatically executing, controlling, or documenting legal events and actions, and their security is crucial to the stability and reliability of the blockchain system. Once vulnerabilities appear in the code or logic of a contract, they can be easily exploited maliciously, leading to cryptocurrency theft, user information leaks, and causing huge economic losses and a crisis of trust for users and businesses. The danger of smart contract vulnerabilities often stems from their vulnerability to malicious manipulation by attackers. For example, attackers can exploit reentrancy vulnerabilities to repeatedly call functions, illegally obtaining funds or tampering with data, threatening user assets and system operation. For example, in The DAO incident, a reentrancy vulnerability in the smart contract was exploited by hackers, resulting in the theft of over $60 million worth of Ethereum. This caused serious damage to investors' interests, triggered a huge industry trust crisis, and reduced public confidence in the security of smart contracts.

[0004] In response to this situation, various smart contract vulnerability detection tools based on large language models and retrieval enhancements have been used to detect vulnerabilities in contracts. However, these traditional methods often have limitations when dealing with complex code. Static analysis discovers vulnerabilities through code syntax and structure analysis without executing the code, but it struggles to fully cover the execution path when faced with complex structures, nested functions, and loops, easily missing hidden vulnerabilities. Dynamic analysis requires running the smart contract and monitoring its behavior to find anomalies, but it heavily relies on the quality and coverage of test cases. Incomplete testing prevents the triggering of specific vulnerabilities, leading to insufficient detection. Formal verification uses mathematical methods to model and verify the logic of smart contracts, theoretically offering high security guarantees, but it requires deep mathematical and logical knowledge, is complex and time-consuming, and is difficult to use on a large scale for complex detection.

[0005] In summary, existing technologies for detecting smart contract vulnerabilities based on large language models and retrieval enhancement often have limitations when dealing with complex code. Static analysis discovers vulnerabilities through code syntax and structure analysis without executing the code, but it is difficult to fully cover the execution path when dealing with complex structures, nested functions, and loops, and it is easy to miss hidden vulnerabilities. The applicant has made corresponding explorations to solve this problem. Summary of the Invention

[0006] The purpose of this application is to solve the above problems by providing a smart contract vulnerability detection method, corresponding device, electronic device and computer-readable storage medium based on large language model and retrieval enhancement generation.

[0007] To achieve the various objectives of this application, the following technical solution is adopted:

[0008] A smart contract vulnerability detection method based on large language models and retrieval enhancement, proposed to meet one of the purposes of this application, includes:

[0009] Obtain the code block corresponding to the smart contract to be detected and its corresponding code attributes. The code block includes multiple code fragments, each code fragment representing a specific functional module. The code attributes include interface name, function definition and its corresponding variable declaration.

[0010] The code block and its corresponding code attributes are input into a preset large language model. The MindChain technology is used to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be tested according to the set of detection prompt words, and to determine the vulnerability detection result of the large language model.

[0011] A large language model is used to determine the most suitable smart contract detection tool based on the smart contract to be detected. The smart contract to be detected is then input into the smart contract detection tool to determine the vulnerability detection results of the tool.

[0012] The code block is input into a preset RAG model, and similar data items corresponding to the code block are retrieved from a preset vulnerability contract dataset. The generator of the RAG model is used to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data items corresponding to the code block, so as to determine the vulnerability detection result of the RAG model.

[0013] The code block corresponding to the smart contract to be tested, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model are input into the large language model after being enhanced by the MindChain, so as to determine the smart contract vulnerability type of the smart contract to be tested and its corresponding vulnerability scenario description, so as to complete the vulnerability detection of the smart contract.

[0014] Optionally, the set of detection prompts includes the types of smart contract vulnerabilities that may occur in the code block and their corresponding vulnerability attributes. The vulnerability attributes characterize the vulnerability scenario description of each smart contract vulnerability type, and the vulnerability scenario description characterizes the code segment in which the smart contract vulnerability occurs and its corresponding code attributes or code operations.

[0015] The vulnerability detection results are constructed from the smart contract vulnerability types of the detected code blocks and their corresponding vulnerability scenario descriptions;

[0016] The vulnerability contract dataset includes code blocks of multiple smart contracts known to have vulnerabilities and their corresponding vulnerability scenario descriptions;

[0017] The similar data item representation refers to the code blocks in the preset vulnerability contract dataset that are similar in function, structure, or logic to the code blocks corresponding to the smart contract to be detected, as well as the corresponding vulnerability scenario descriptions.

[0018] Optionally, before the step of obtaining the code block corresponding to the smart contract to be tested and its corresponding code attributes, the following steps are included:

[0019] Respond to the code block preprocessing instruction to obtain the code block corresponding to the smart contract to be tested;

[0020] The blank lines in the code block corresponding to the smart contract to be tested are deleted, the blank lines in the comments of the code block corresponding to the smart contract to be tested are deleted, and the newline characters in the function definition of the code block corresponding to the smart contract to be tested are merged to determine the code block corresponding to the smart contract to be tested and its corresponding code attributes.

[0021] Optionally, the step of using thought chain technology to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be tested based on the set of detection prompt words, includes:

[0022] Based on the set of detection prompts corresponding to the smart contract to be tested, the large language model is guided to first identify external calls and state change functions, then analyze the interface, function call order and conditions, and finally determine the type of smart contract vulnerability of the smart contract to be tested.

[0023] Optionally, the step of inputting the code block into a preset RAG model and retrieving similar data items corresponding to the code block from a preset vulnerability contract dataset includes:

[0024] An embedded vector model is used to convert each data item in the vulnerability contract dataset into its corresponding first vector representation, and to convert the code block corresponding to the smart contract to be detected into its corresponding second vector representation.

[0025] Calculate the cosine similarity between the second vector representation and the first vector representation, sort the cosine similarities from high to low, and select the top k most relevant data items as similar data items corresponding to the code block.

[0026] Optionally, the types of smart contract vulnerabilities include reentrancy vulnerabilities, time-dependent vulnerabilities, integer overflow and underflow vulnerabilities, and access control vulnerabilities.

[0027] Optionally, the underlying network architecture of the large language model includes the GPT model.

[0028] A smart contract vulnerability detection device based on large language models and retrieval enhancement generation, provided for another purpose of this application, includes:

[0029] The preprocessing module is configured to obtain the code block corresponding to the smart contract to be detected and its corresponding code attributes. The code block includes multiple code fragments, each code fragment representing a specific functional module. The code attributes include interface name, function definition and its corresponding variable declaration.

[0030] The first vulnerability detection module is configured to input the code block and its corresponding code attributes into a preset large language model, and use the thinking chain technology to determine the set of detection prompt words corresponding to the code block of the smart contract to be detected, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be detected according to the set of detection prompt words, so as to determine the vulnerability detection result of the large language model.

[0031] The second vulnerability detection module is configured to use a large language model to determine the most suitable smart contract detection tool based on the smart contract to be detected, and input the smart contract to be detected into the smart contract detection tool to determine the vulnerability detection result of the tool.

[0032] The third vulnerability detection module is configured to input the code block into a preset RAG model, retrieve similar data items corresponding to the code block from a preset vulnerability contract dataset, and use the generator of the RAG model to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data items corresponding to the code block, so as to determine the vulnerability detection result of the RAG model.

[0033] The vulnerability detection and verification module is configured to input the code block corresponding to the smart contract to be detected, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model into the large language model after being enhanced by the mind chain, so as to determine the smart contract vulnerability type of the smart contract to be detected and its corresponding vulnerability scenario description, so as to complete the vulnerability detection of the smart contract.

[0034] An electronic device provided for another purpose of this application includes a central processing unit and a memory, the central processing unit being configured to invoke and run a computer program stored in the memory to perform the steps of the smart contract vulnerability detection method based on large language model and retrieval enhancement described in this application.

[0035] A computer-readable storage medium is provided for another purpose of this application, which stores, in the form of computer-readable instructions, a computer program implemented according to the smart contract vulnerability detection method based on the large language model and retrieval enhancement, which, when invoked by a computer, executes the steps included in the corresponding method.

[0036] Compared to existing technologies, this application addresses the limitations of traditional smart contract vulnerability detection methods based on large language models and retrieval enhancement when dealing with complex code. Static analysis discovers vulnerabilities through code syntax and structure analysis without executing the code, but it struggles to fully cover the execution path when faced with complex structures, nested functions, and loops, easily missing hidden vulnerabilities. This application offers the following advantages, including but not limited to:

[0037] Firstly, with the rapid development of blockchain technology, smart contracts, as a core component of blockchain, are crucial for maintaining the stability of the blockchain ecosystem. However, current smart contract vulnerability detection technologies based on large language models and retrieval enhancement exhibit limitations such as insufficient detection accuracy and low efficiency when dealing with complex and ever-changing contract code structures and emerging attack vectors. This application aims to propose an innovative smart contract vulnerability detection framework based on large language models and retrieval enhancement to overcome existing technological bottlenecks. Specifically, by inputting smart contract code into a pre-trained large language model, its powerful syntactic and semantic understanding and code analysis capabilities are utilized to perform deep semantic parsing of the contract code. Based on this, a thought process enhancement mechanism is introduced. This mechanism simulates the multi-step reasoning process of human experts, guiding the model to gradually delve deeper into the analysis of code logic and identify potential vulnerability feature patterns, thereby significantly improving the model's reasoning depth and detection accuracy.

[0038] Secondly, to construct a comprehensive detection system, this application integrates a proven smart contract vulnerability detection tool based on a large language model and retrieval-enhanced generation. This tool possesses efficient static code analysis and dynamic behavior monitoring capabilities, enabling rapid identification of abnormal structures and risk points in the code. Furthermore, a retrieval-enhanced generation (RAG) system trained on a large-scale vulnerability contract dataset learns the feature patterns of massive amounts of labeled vulnerability cases, providing data-driven intelligent assistance to the detection process and further enhancing the comprehensiveness and accuracy of the detection.

[0039] Third, through the synergistic integration of multiple technologies, this application achieves efficient and accurate detection of smart contract vulnerabilities. In the detection process, each technical module complements and verifies the others, forming a closed-loop detection chain to ensure the reliability of the detection results. Simultaneously, to guarantee the correctness of the detection results, a multi-angle verification mechanism is designed, verifying and analyzing the results from multiple dimensions such as code repair experiments and vulnerability reproduction tests. This provides a solid guarantee for the security of smart contracts and promotes the continuous development of blockchain technology on a safer and more trustworthy basis. Attached Figure Description

[0040] The above and / or additional aspects and advantages of this application will become apparent and readily understood from the following description of the embodiments taken in conjunction with the accompanying drawings, wherein:

[0041] Figure 1 This is a flowchart illustrating the smart contract vulnerability detection method based on a large language model and retrieval enhancement in the embodiments of this application;

[0042] Figure 2 This is an exemplary network architecture used in the smart contract vulnerability detection system based on large language models and retrieval enhancement in the embodiments of this application;

[0043] Figure 3 This is a schematic diagram of the smart contract vulnerability detection device based on a large language model and retrieval enhancement in the embodiments of this application;

[0044] Figure 4 This is a schematic diagram of the structure of the computer device in the embodiments of this application. Detailed Implementation

[0045] The embodiments of this application are described in detail below. Examples of these embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals denote the same or similar elements or elements having the same or similar functions throughout. The embodiments described below with reference to the accompanying drawings are exemplary and are only used to explain this application, and should not be construed as limiting this application.

[0046] Those skilled in the art will understand that, unless specifically stated otherwise, the singular forms “a,” “an,” “the,” and “the” used herein may also include the plural forms. It should be further understood that the term “comprising” as used in this application means the presence of the stated features, integers, steps, operations, elements, and / or components, but does not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof. It should be understood that when we say an element is “connected” or “coupled” to another element, it can be directly connected or coupled to the other element, or there may be intermediate elements. Furthermore, “connected” or “coupled” as used herein can include wireless connections or wireless coupling. The term “and / or” as used herein includes all or any units and all combinations of one or more associated listed items.

[0047] It will be understood by those skilled in the art that, unless otherwise defined, all terms used herein (including technical and scientific terms) have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains. It should also be understood that terms such as those defined in general dictionaries should be understood to have the same meaning as in the context of the prior art, and should not be interpreted in an idealized or overly formal sense unless specifically defined as herein.

[0048] Those skilled in the art will understand that the terms "client," "terminal," and "terminal device" as used herein include both devices that receive wireless signals, devices that only possess wireless signal receiver capabilities without transmission capabilities, and devices with receiving and transmitting hardware, devices that have receiving and transmitting hardware capable of bidirectional communication over a bidirectional communication link. Such devices may include: cellular or other communication devices such as personal computers or tablets, having single-line displays, multi-line displays, or cellular or other communication devices without multi-line displays; PCS (Personal Communications Service) that can combine voice, data processing, fax, and / or data communication capabilities; PDA (Personal Digital Assistant) that may include a radio frequency receiver, pager, internet / intranet access, web browser, notepad, calendar, and / or GPS (Global Positioning System) receiver; and conventional laptops and / or handheld computers or other devices that have and / or include radio frequency receivers. As used herein, "client," "terminal," and "terminal device" can be portable, transportable, installed in a means of transportation (air, sea, and / or land), or suitable and / or configured to operate locally and / or in a distributed manner, operating in any other location on Earth and / or in space. "Client," "terminal," and "terminal device" as used herein can also be a communication terminal, an internet access terminal, or a music / video playback terminal, such as a PDA, a MID (Mobile Internet Device), and / or a mobile phone with music / video playback capabilities, or a smart TV, set-top box, etc.

[0049] The hardware referred to by the names "server," "client," and "service node" in this application is essentially an electronic device with the equivalent capabilities of a personal computer. It is a hardware device with the necessary components revealed by the von Neumann architecture, such as a central processing unit (including an arithmetic logic unit and a control unit), memory, input devices, and output devices. The computer program is stored in its memory, and the central processing unit loads the program stored in the secondary storage into the main memory to run it, execute the instructions in the program, and interact with the input and output devices to complete specific functions.

[0050] It should be noted that the concept of "server" used in this application can also be extended to the case of server clusters. Based on the network deployment principles understood by those skilled in the art, the servers should be logically divided. Physically, these servers can be independent of each other but accessible through interfaces, or they can be integrated into a single physical computer or a computer cluster. Those skilled in the art should understand this flexibility and should not use it to constrain the implementation of the network deployment method in this application.

[0051] One or more of the technical features of this application, unless explicitly specified herein, can be deployed on a server and accessed by a client remotely calling the online service interface provided by the server, or can be directly deployed and run on a client for access.

[0052] Unless otherwise specified, the neural network models referenced or potentially referenced in this application may be deployed on a remote server and invoked remotely on the client, or deployed on a client with the capability to invoke directly. In some embodiments, when running on the client, the corresponding intelligence may be acquired through transfer learning in order to reduce the requirements on the client's hardware resources and avoid excessive consumption of the client's hardware resources.

[0053] Unless otherwise specified, all data involved in this application may be stored remotely on a server or on a local terminal device, as long as it is suitable for use by the technical solution of this application.

[0054] Those skilled in the art will understand that although the various methods in this application are described based on the same concept and thus present commonality among them, they can be performed independently unless otherwise specified. Similarly, the various embodiments disclosed in this application are all based on the same inventive concept; therefore, concepts expressed in the same way, as well as concepts that are appropriately changed for convenience but are expressed differently, should be understood equivalently.

[0055] Unless otherwise expressly stated, the various embodiments disclosed in this application can be combined in a cross-cutting manner to flexibly construct new embodiments, as long as such combination does not depart from the inventive spirit of this application and can meet the needs of the prior art or solve a certain deficiency in the prior art. Those skilled in the art should be aware of such modifications.

[0056] Please see Figure 1 In one embodiment of the smart contract vulnerability detection method based on large language models and retrieval enhancement of this application, the method includes:

[0057] Step S10: Obtain the code block corresponding to the smart contract to be detected and its corresponding code attributes. The code block includes multiple code fragments, each code fragment representing a specific functional module. The code attributes include interface name, function definition and its corresponding variable declaration.

[0058] A smart contract vulnerability detection system based on a large language model and retrieval enhancement in a terminal device can respond to instructions to perform vulnerability detection on a smart contract under test. It obtains the corresponding code block and its corresponding code attributes. The code block comprises multiple code fragments, each representing a specific functional module. The code attributes include interface names, function definitions, and their corresponding variable declarations. Specifically, the code block corresponding to the smart contract under test is split into code blocks, each representing a functional module or logical part. Code blocks are tagged or categorized according to their functions, such as contract initialization, data storage, and payment logic. The code block is stored along with its associated code attributes, including interface names, function definitions, and their corresponding variable declarations. The interface name represents the interface through which the function or contract interacts with the outside world; the function definition represents the function declaration, including input and output types and modifiers; and the variable declaration represents information related to state variables and local variables within the function or contract.

[0059] like Figure 2 As shown, this application constructs a general smart contract vulnerability detection system based on large language models and retrieval enhancement generation. This smart contract vulnerability detection system based on large language models and retrieval enhancement generation mainly includes the following core components:

[0060] Smart contract code input module: responsible for receiving the smart contract code to be tested and converting it into a format suitable for processing by large language models.

[0061] Large Language Model and Thinking Chain Module: Input smart contract code into the large language model, and use thinking chain technology to guide the model to perform step-by-step reasoning and analyze the types of vulnerabilities that may exist in the code.

[0062] The integrated module for smart contract vulnerability detection tools based on large language models and retrieval enhancement integrates multiple suitable smart contract vulnerability detection tools based on large language models and retrieval enhancement, and selects appropriate tools to further analyze the contract based on the analysis results of the large language model.

[0063] Vulnerable Contract Dataset and RAG Model Module: Construct a dataset containing a large number of vulnerable contract examples, and use this dataset to train a RAG model, enabling it to efficiently search and analyze smart contract vulnerabilities.

[0064] Detection result verification module: The contract itself, the detection results of the large language model + tool, and the RAG detection results are simultaneously input into a large language model assisted by the MindChain to determine whether the detection results are correct, thereby improving the reliability and accuracy of the detection.

[0065] In some embodiments, before the step of obtaining the code block corresponding to the smart contract to be detected and its corresponding code attributes, the following steps are included:

[0066] Step S101: Response code block preprocessing instruction to obtain the code block corresponding to the smart contract to be detected;

[0067] Step S102: Delete blank lines in the code block corresponding to the smart contract to be tested, delete blank lines in the comments of the code block corresponding to the smart contract to be tested, and merge newline characters in the function definition of the code block corresponding to the smart contract to be tested, so as to determine the code block corresponding to the smart contract to be tested and its corresponding code attributes.

[0068] Specifically, let C be the smart contract code uploaded by the user. After preprocessing, a formatted and syntactically correct code block C′ is obtained, and the code attributes K = {k1, k2, ..., k} are extracted. n}, where k i This could be a function definition, variable declaration, etc. The mathematical expression for this module can be represented as:

[0069] f input (C)=(C′,K),

[0070] Among them, f input This represents the processing function of the input module.

[0071] In some embodiments, the smart contract vulnerability types include reentrancy vulnerabilities, time-dependent vulnerabilities, integer overflow and underflow vulnerabilities, and access control vulnerabilities; the detection prompt word set includes the smart contract vulnerability types that may occur in the code block and their corresponding vulnerability attributes, the vulnerability attributes characterizing the vulnerability scenario description of each smart contract vulnerability type, and the vulnerability scenario description characterizing the code segment where the smart contract vulnerability occurs and its corresponding code attributes or code operations; the vulnerability detection result is constructed from the detected smart contract vulnerability types of the code block and their corresponding vulnerability scenario descriptions; the vulnerability contract dataset includes code blocks of multiple known vulnerable smart contracts and their corresponding vulnerability scenario descriptions; the similar data item characterizes, in the preset vulnerability contract dataset, code blocks that are functionally, structurally, or logically similar to the code block corresponding to the smart contract to be detected, and their corresponding vulnerability scenario descriptions.

[0072] Step S20: Input the code block and its corresponding code attributes into the preset large language model, and use the thinking chain technology to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be tested according to the set of detection prompt words, so as to determine the vulnerability detection result of the large language model.

[0073] After obtaining the code block and its corresponding code attributes corresponding to the smart contract to be tested, the code block and its corresponding code attributes are input into a preset large language model. The Big Language Model uses the MindChain technology to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested. This guides the Big Language Model to perform vulnerability detection on the code block of the smart contract to be tested based on the detection prompt word set, thereby determining the vulnerability detection result of the Big Language Model. The basic network architecture of the Big Language Model includes the GPT model. Specifically, the MindChain technology tracks each step of the reasoning process during the analysis, analyzes potential vulnerabilities in the contract code, and combines them with vulnerability scenarios, vulnerability types, etc., to form a preliminary set of detection prompt words. The set of detection prompt words provides guidance for subsequent vulnerability detection. The set of detection prompt words includes: possible smart contract vulnerability types of the code block and their corresponding vulnerability scenarios, code operations related to the vulnerability scenario descriptions (such as modification of variables, function calls, etc.), etc. The smart contract vulnerability types include reentrancy attack vulnerabilities, time-dependent vulnerabilities, integer overflow and underflow vulnerabilities, and access control vulnerabilities. The set of detection prompts includes the types of smart contract vulnerabilities that may occur in the code block and their corresponding vulnerability attributes. The vulnerability attributes characterize the vulnerability scenario description for each type of smart contract vulnerability. The vulnerability scenario description characterizes the code segment where the smart contract vulnerability occurs and its corresponding code attributes or code operations. The vulnerability detection result is constructed from the detected smart contract vulnerability types of the code block and their corresponding vulnerability scenario descriptions.

[0074] In some embodiments, in this era of rapid digital development, Large Language Models (LLMs) demonstrate superior natural language understanding and generation capabilities, enabling them to deeply understand and analyze smart contract code. Users simply input the smart contract code into the LLM, and with the help of Chain of Thought (CoT), its reasoning ability can be enhanced, thereby more effectively analyzing potential vulnerabilities in the contract. Furthermore, combining this with appropriate vulnerability detection tools can further improve the accuracy and efficiency of detection.

[0075] Large language models, trained on massive amounts of text, can understand the semantics, syntax, and logic of human language. When smart contract code is input, it can interpret it line by line, just like a human developer, combining information such as comments and variable names to fully grasp the contract's functionality, laying the foundation for vulnerability detection.

[0076] Thought Chain (CoT) is a technology that guides large language models through step-by-step reasoning. In vulnerability detection, it enables the model to analyze contract code step by step, like an expert, from architecture to details, from logic to risks. For example, when detecting reentrancy vulnerabilities, the model first identifies external calls and state-changing functions, then analyzes the call order and conditions, and finally infers whether there is a reentrancy attack path. This reasoning allows the model to discover potential vulnerabilities more systematically and comprehensively.

[0077] In some embodiments, a specific implementation of the thought chain technology includes:

[0078] Step S201: Collect and organize various issues and cases related to smart contract vulnerabilities, covering common vulnerability types such as integer overflow, reentrancy attacks, and permission issues, as well as detection scenarios of varying complexity.

[0079] Step S202: Design a template and format for the thought chain problem to ensure that the problem guides the model to gradually delve into the causes, detection methods, and remediation strategies of the vulnerability. For example, for reentrancy attack vulnerabilities, design the following question: "In the following smart contract code, is there a possibility of a reentrancy attack? Please analyze the function call flow, identify the key code snippets that may lead to reentrancy, and explain which tool is most suitable for detecting reentrancy vulnerabilities."

[0080] Step S203: Train the large language model using the designed thought chain problem. During training, record the model's reasoning process and output results, and analyze its understanding and resolution capabilities regarding vulnerability issues. Based on the training results, adjust the model's parameters and training strategies, such as learning rate, batch size, and number of training rounds, to improve the model's reasoning accuracy and stability. Simultaneously, optimize and supplement the thought chain problem to make it more targeted and effective.

[0081] Specifically, code block C′ is input into a large language model, and the model is guided step by step to analyze potential vulnerabilities using the thought chain technique. Let the set of detection prompt words be Q = {q1, q2, ..., q}. m}, where Q represents the set of detection prompt words, and the model output is the vulnerability detection result A of the large language model. The mathematical expression of this module can be expressed as:

[0082] f LLM (C′,Q)=A

[0083] Among them, f LLM This represents the analytical function that combines the large language model with the thought chain.

[0084] The thought chain analysis process is iterative; at each step, the model proceeds to the next step based on the current analysis results and guiding questions. Let the analysis state at step t be S. t The analysis process can then be represented as:

[0085] S t+1 =f chain (S t q t ),

[0086] Where, q t It's a guidance problem at step t, f chain It is a thought chain analysis function that updates the analysis state based on the current state and the guiding question.

[0087] After multiple steps of thought chain analysis, the model finally generates vulnerability detection result A for the large language model. This result contains detailed analysis and verification information on potential vulnerabilities in the code block, which can be represented as:

[0088] A = f analyze (C′,M,Q,S T )

[0089] Among them, S T It is the final state after the T-step thought chain analysis, f analyze It is a function for generating vulnerability detection results.

[0090] In a specific embodiment, the step of using thought chain technology to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be tested based on the set of detection prompt words, includes:

[0091] Based on the set of detection prompts corresponding to the smart contract to be tested, the large language model is guided to first identify external calls and state change functions, then analyze the interface, function call order and conditions, and finally determine the type of smart contract vulnerability of the smart contract to be tested.

[0092] Step S30: Using a large language model, determine the most suitable smart contract detection tool based on the smart contract to be detected, and input the smart contract to be detected into the smart contract detection tool to determine the vulnerability detection result of the tool;

[0093] The code block and its corresponding code attributes are input into a preset large language model. The MindChain technology is used to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be tested according to the set of detection prompt words. After determining the vulnerability detection result of the large language model, the large language model determines the most suitable smart contract detection tool according to the smart contract to be tested, and the smart contract to be tested is input into the smart contract detection tool to determine the vulnerability detection result of the tool.

[0094] Specifically, the smart contract vulnerability detection system based on large language model and retrieval enhancement in this application integrates multiple smart contract vulnerability detection tools based on large language model and retrieval enhancement. It uses a large language model to select the most suitable tool for further detection. Let the set of smart contract vulnerability detection tools based on large language model and retrieval enhancement be T = {T1, T2, ..., T...} p The module employs a large language model to determine the most suitable smart contract detection tool based on the smart contract to be detected. The smart contract to be detected is then input into the smart contract detection tool to determine the vulnerability detection result R. The mathematical expression for this module can be represented as:

[0095] f tools (A, T) = R

[0096] Among them, f tools This represents the detection function of the tool integration module.

[0097] Step S40: Input the code block into the preset RAG model, retrieve similar data items corresponding to the code block from the preset vulnerability contract dataset, and use the generator of the RAG model to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data items corresponding to the code block, so as to determine the vulnerability detection result of the RAG model.

[0098] A large language model is used to determine the most suitable smart contract detection tool based on the smart contract to be detected. The smart contract to be detected is then input into the smart contract detection tool to determine the vulnerability detection result. Next, the code block is input into a preset RAG model. Similar data items corresponding to the code block are retrieved from a preset vulnerability contract dataset. The generator of the RAG model performs vulnerability detection on the code block of the smart contract to be detected based on the similar data items corresponding to the code block, to determine the vulnerability detection result of the RAG model. The vulnerability contract dataset includes code blocks of multiple smart contracts known to have vulnerabilities and their corresponding vulnerability scenario descriptions. The similar data items represent code blocks in the preset vulnerability contract dataset that are functionally, structurally, or logically similar to the code block corresponding to the smart contract to be detected, along with their corresponding vulnerability scenario descriptions.

[0099] In some embodiments, Retrieval Augmented Generation (RAG) technology is a novel architecture that combines information retrieval and generative models. Its core idea is to use external knowledge bases or document collections to provide real-time and accurate background information for generative models, thereby compensating for the "illusion" problem that may occur in pure generative models.

[0100] Specifically, RAG consists of two parts: a retrieval module and a generation module. The retrieval module is responsible for retrieving documents or fragments related to the current input question from the knowledge base, while the generation module generates accurate and rich answers based on the retrieval results and the original input through a large model.

[0101] The advantages of RAG technology lie in improving factual accuracy, enriching contextual information, and having dynamic update capabilities. By introducing external knowledge, the model-generated answers are more factually based, reducing the probability of misinformation or false content. At the same time, the retrieval results can provide additional contextual support for the generation model, making the answer content more detailed and professional. The knowledge base can be updated regularly to ensure that the model output content is synchronized with the latest information, solving the problem of insufficient timeliness of large model training data.

[0102] In the field of smart contract vulnerability detection based on large language models and retrieval enhancement, RAG can leverage the similarity between query alignment vectors to retrieve relevant data, thereby enhancing user prompts within the context and enabling LLMs to generate accurate and context-appropriate answers. As smart contract programming paradigms continue to evolve, new attack patterns and vulnerability types are on the rise, and the same vulnerability can spawn multiple variants through code obfuscation, logic refactoring, and other techniques. By constructing a dynamically updated, multi-dimensional vulnerability sample library, not only can the characteristics of the latest emerging zero-day vulnerabilities be captured, but also diverse variant samples of known vulnerabilities can be covered. This significantly improves the RAG model's adaptability to new vulnerabilities and its generalized detection accuracy for hidden variant vulnerabilities, providing crucial support for building a proactive defense system.

[0103] In some embodiments, the step of inputting the code block into a preset RAG model and retrieving similar data items corresponding to the code block from a preset vulnerability contract dataset includes:

[0104] Step S401: Using an embedded vector model, each data item in the vulnerability contract dataset is converted into its corresponding first vector representation, and the code block corresponding to the smart contract to be detected is converted into its corresponding second vector representation.

[0105] Step S402: Calculate the cosine similarity between the second vector representation and the first vector representation, sort the cosine similarity from high to low, and select the top k most relevant data items as similar data items corresponding to the code block.

[0106] Specifically, a vulnerability contract dataset D is constructed, and a RAG model is trained to retrieve and generate accurate detection results for the corresponding code blocks of the smart contracts to be detected. Let the output of the RAG model be the detection result S based on the dataset. The mathematical expression of this module can be represented as:

[0107] f RAG (C′,D)=S,

[0108] Among them, f RAG This represents the detection function of the RAG model.

[0109] The vulnerability contract dataset D contains code blocks of multiple smart contracts known to have vulnerabilities, along with their corresponding vulnerability scenario descriptions. Each data item can be represented as d. i =(c i v i ), where d i c represents a data item in the vulnerability contract dataset D. i It is a code block of a smart contract, v iThis is a description of the corresponding vulnerability scenario, dataset D = {d1, d2, ..., d...} N}, where N is the number of data items in the dataset.

[0110] The retrieval function retrieves the most similar data items from the dataset D that correspond to the code block C′ of the smart contract to be detected. The retrieval function can be based on a vector space model, BM25, or other advanced retrieval algorithms. This application uses cosine similarity for modeling.

[0111] Document vectorization: By using an embedded vector model, each data item d in the vulnerability contract dataset is vectorized. i Convert to first vector representation d i .

[0112] Query vectorization: Convert the code block C′ corresponding to the smart contract to be detected into its corresponding second vector representation q, using the same or compatible method as document vectorization.

[0113] Similarity calculation: Calculate the similarity between the second vector representation q and each first vector representation d. i The cosine similarity between the two is commonly determined by methods such as cosine similarity and inner product. This embodiment uses cosine similarity, which does not constitute any limitation on this application. The formula is:

[0114]

[0115] Search Results: Based on cosine similarity sorted from highest to lowest, the top k most relevant data items were selected as the search results, denoted as .

[0116] The generator uses the retrieved data item D retrieved Generate the detection result S for the code block C′ corresponding to the smart contract to be detected. Approximate Nearest Neighbor Search (ANN) is used to find the most similar document. Let the ANN search function be expressed as:

[0117] ANN(q, D, k)

[0118] Its output is the set of indices I = {i1, i2, ..., ik} of the top k documents most similar to the query vector q. k}

[0119] The generation process can be represented as:

[0120] S = f gen (C′,D retrieυed )

[0121] Among them, f genIt is the generator function.

[0122] By combining the retriever and the generator, a complete RAG model function f is formed. RAG :

[0123] f RAG (C′,D)=f gen (C′,Retrieυe(C′,D))

[0124] Among them, f RAG Represents the complete RAG model function, Retrieυe(C′,D) means that the retriever retrieves the data items related to code block C′ from the vulnerability contract data D.

[0125] In some embodiments, the detailed steps for constructing the vulnerable contract dataset include:

[0126] Step S40001: Collect a large number of smart contract instances known to have vulnerabilities from public blockchain platforms, vulnerability databases, academic research papers, and other sources. Perform preliminary screening and sorting on the collected contract instances, removing duplicate, invalid, or low-quality samples to ensure the purity and usability of the dataset.

[0127] Step S40002: Organize professional security researchers and developers to perform detailed vulnerability labeling and classification on the selected contract instances, clarifying the type, location, impact, and remediation suggestions for each vulnerability. Digitize the labeled data, converting it into a format suitable for model training, such as JSON or CSV, and perform data augmentation operations, such as code obfuscation and variable renaming, to improve the diversity and generalization ability of the dataset.

[0128] Step S40003: Perform quality assessment and verification on the constructed dataset, check the accuracy, completeness and consistency of the data, and ensure that it can provide high-quality data support for the training of the RAG model.

[0129] In some embodiments, the specific methods for training and optimizing the RAG model include:

[0130] Step S100: Select a suitable pre-trained language model as the basis for the RAG model, such as BERT or RoBERTa, and fine-tune and optimize it according to the characteristics of the smart contract vulnerability detection task based on large language models and retrieval enhancement. Design the retrieval module of the RAG model, and use efficient text retrieval algorithms, such as inverted index and BM25, to quickly retrieve vulnerability information related to the input contract code from the vulnerability contract dataset.

[0131] Step S200: Construct the RAG model generation module. Based on the retrieved vulnerability information and the input contract code, generate vulnerability detection results for smart contracts, including vulnerability type, location, and description. Train the RAG model using the constructed vulnerability contract dataset. During training, tools such as cross-entropy loss function and optimizers are used to adjust the model parameters, minimize the model's prediction error, and improve its detection performance.

[0132] Step S300: The RAG model is evaluated and optimized through methods such as cross-validation and hyperparameter search to determine the optimal model parameters and structure, ensuring its effectiveness and stability in different scenarios.

[0133] Step S50: Input the code block corresponding to the smart contract to be tested, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model into the large language model after the MindChain enhancement, so as to determine the smart contract vulnerability type of the smart contract to be tested and its corresponding vulnerability scenario description, so as to complete the vulnerability detection of the smart contract.

[0134] The code block is input into a preset RAG model, and similar data items corresponding to the code block are retrieved from a preset vulnerability contract dataset. The generator of the RAG model is used to perform vulnerability detection on the code block of the smart contract to be tested based on the similar data items corresponding to the code block. After determining the vulnerability detection result of the RAG model, the code block corresponding to the smart contract to be tested, the vulnerability detection result of the large language model, the vulnerability detection result of the tool, and the vulnerability detection result of the RAG model are input into the large language model reinforced by the mind chain to determine the smart contract vulnerability type of the smart contract to be tested and its corresponding vulnerability scenario description, so as to complete the vulnerability detection of the smart contract.

[0135] Specifically, the code block corresponding to the smart contract to be tested, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model are input into the large language model reinforced by the MindChain algorithm for comprehensive analysis and verification, resulting in the final detection conclusion V. The mathematical expression of this module can be represented as:

[0136] f verify (C, R, S) = V

[0137] Among them, f verifg This represents the comprehensive analysis function of the verification module.

[0138] By connecting all the modules above, the mathematical model of the entire smart contract vulnerability detection system based on large language models and retrieval enhancement can be expressed as:

[0139] V = f verify (C, f) tools (f LLM (f input (C)1, Q), T), f RAG (f input (C)1, D));

[0140] Among them, f input (C)1 represents the preprocessed code block C′ output by the input module.

[0141] In some embodiments, the detailed steps of a smart contract vulnerability detection process based on large language models and retrieval enhancements include:

[0142] Step S501: Input the smart contract code into the large language model to ensure that the model can correctly parse and understand the structure and logic of the code.

[0143] Step S502: Train the large language model using the thought chain technology. By gradually guiding the model to consider vulnerabilities in smart contracts, its reasoning ability is improved. For example, for common integer overflow vulnerabilities, design a series of thought chain questions of increasing difficulty, allowing the model to learn how to identify operations and conditions that may lead to overflow from the code. Analyze the input contract code using the trained large language model, outputting the possible vulnerability types and their corresponding confidence levels. Based on the vulnerability types output by the model, select appropriate smart contract vulnerability detection tools based on the large language model and retrieval enhancement, such as Slither and Mythril, to conduct further static analysis or dynamic testing of the contract, obtaining more detailed vulnerability information and detection reports.

[0144] Step S503: Collect known vulnerable contract instances, label and classify them, and construct a vulnerable contract dataset. The dataset should include information such as contract code, vulnerability type, vulnerability location, and remediation suggestions to provide rich data support for training the RAG model. Based on the constructed dataset, design and train the RAG model. During training, optimize the model's parameters and structure so that it can accurately retrieve vulnerability-related information from the contract code and generate effective detection results.

[0145] Step S504: Integrate the contract itself, the large language model + tool detection results, and the RAG detection results as inputs into a large language model assisted by the MindChain. Within the model, the MindChain guides a comprehensive analysis and judgment of each detection result, considering the correlation and consistency between results to ultimately determine whether the detection result is correct. For example, if both the large language model and the RAG model detect a vulnerability, and the detection tool also verifies the existence of the vulnerability, then the detection result can be determined to be correct. If there are discrepancies between the results, the MindChain guides the model to further analyze the reasons and determine which result is more credible.

[0146] In some embodiments, the implementation details of the multi-model collaborative verification mechanism include:

[0147] Step S5001: Design a framework for multi-model collaborative verification, and clarify the inputs, outputs, and data flow relationships between each model.

[0148] Step S5002: Develop a data fusion module to effectively integrate and combine the contract itself, the large language model + tool detection results, and the RAG detection results to ensure data consistency and integrity.

[0149] Step S5003: Construct a judgment model based on the thought chain. This model comprehensively analyzes and judges each detection result, considering the correlation and consistency between the results, to determine whether the final detection result is correct. During the judgment process, the thought chain guides the model to gradually analyze the credibility and rationality of each result. For example, by comparing factors such as the confidence level of each result, the reliability of the detection method, and the matching degree of vulnerability features, a final judgment decision is made.

[0150] Step S5004: Set reasonable judgment rules and thresholds. Based on actual detection needs and risk preferences, determine under what circumstances the detection result is judged to be correct or incorrect. For example, it can be set that when two or more models detect the same vulnerability and the confidence level is higher than a certain threshold, the vulnerability is judged to exist; or when there are significant differences in the detection results of various models, further analyze the reasons and combine the judgment logic of the thought chain to determine the final result.

[0151] Step S5005: Test and verify the multi-model collaborative verification mechanism. Through actual smart contract cases, evaluate its detection accuracy and reliability in different scenarios, continuously optimize and improve the mechanism, and enhance its performance and effectiveness.

[0152] Compared to existing technologies, this application addresses the limitations of traditional smart contract vulnerability detection methods based on large language models and retrieval enhancement when dealing with complex code. Static analysis discovers vulnerabilities through code syntax and structure analysis without executing the code, but it struggles to fully cover the execution path when faced with complex structures, nested functions, and loops, easily missing hidden vulnerabilities. This application offers the following advantages, including but not limited to:

[0153] Firstly, with the rapid development of blockchain technology, smart contracts, as a core component of blockchain, are crucial for maintaining the stability of the blockchain ecosystem. However, current smart contract vulnerability detection technologies based on large language models and retrieval enhancement exhibit limitations such as insufficient detection accuracy and low efficiency when dealing with complex and ever-changing contract code structures and emerging attack vectors. This application aims to propose an innovative smart contract vulnerability detection framework based on large language models and retrieval enhancement to overcome existing technological bottlenecks. Specifically, by inputting smart contract code into a pre-trained large language model, its powerful syntactic and semantic understanding and code analysis capabilities are utilized to perform deep semantic parsing of the contract code. Based on this, a thought process enhancement mechanism is introduced. This mechanism simulates the multi-step reasoning process of human experts, guiding the model to gradually delve deeper into the analysis of code logic and identify potential vulnerability feature patterns, thereby significantly improving the model's reasoning depth and detection accuracy.

[0154] Secondly, to construct a comprehensive detection system, this application integrates a proven smart contract vulnerability detection tool based on a large language model and retrieval-enhanced generation. This tool possesses efficient static code analysis and dynamic behavior monitoring capabilities, enabling rapid identification of abnormal structures and risk points in the code. Furthermore, a retrieval-enhanced generation (RAG) system trained on a large-scale vulnerability contract dataset learns the feature patterns of massive amounts of labeled vulnerability cases, providing data-driven intelligent assistance to the detection process and further enhancing the comprehensiveness and accuracy of the detection.

[0155] Third, through the synergistic integration of multiple technologies, this application achieves efficient and accurate detection of smart contract vulnerabilities. In the detection process, each technical module complements and verifies the others, forming a closed-loop detection chain to ensure the reliability of the detection results. Simultaneously, to guarantee the correctness of the detection results, a multi-angle verification mechanism is designed, verifying and analyzing the results from multiple dimensions such as code repair experiments and vulnerability reproduction tests. This provides a solid guarantee for the security of smart contracts and promotes the continuous development of blockchain technology on a safer and more trustworthy basis.

[0156] Please see Figure 3This application provides a smart contract vulnerability detection device based on a large language model and retrieval enhancement, comprising a preprocessing module 1100, a first vulnerability detection module 1200, a second vulnerability detection module 1300, a third vulnerability detection module 1400, and a vulnerability detection verification module 1500. The preprocessing module 1100 is configured to acquire the code block corresponding to the smart contract to be detected and its corresponding code attributes. The code block includes multiple code fragments, each representing a specific functional module. The code attributes include interface names, function definitions, and their corresponding variable declarations. The first vulnerability detection module 1200 is configured to input the code block and its corresponding code attributes into a preset large language model, and use a chain-of-thoughts technique to determine a set of detection prompt words corresponding to the code block of the smart contract to be detected, thereby guiding the large language model to perform vulnerability detection on the code block of the smart contract to be detected based on the set of detection prompt words, and determining the vulnerability detection result of the large language model. The second vulnerability detection module 1300 is configured to use the large language model to determine the most suitable smart contract detection tool based on the smart contract to be detected, and then... The code block is input into the smart contract detection tool to determine the tool's vulnerability detection results; the third vulnerability detection module 1400 is configured to input the code block into a preset RAG model, retrieve similar data items corresponding to the code block from a preset vulnerability contract dataset, and use the generator of the RAG model to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data items corresponding to the code block, so as to determine the vulnerability detection results of the RAG model; the vulnerability detection verification module 1500 is configured to input the code block corresponding to the smart contract to be detected, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model into the large language model after being enhanced by the mind chain, so as to determine the smart contract vulnerability type of the smart contract to be detected and its corresponding vulnerability scenario description, so as to complete the vulnerability detection of the smart contract.

[0157] Based on any embodiment of this application, please refer to Figure 4 Another embodiment of this application also provides an electronic device, which can be implemented by a computer device, such as... Figure 4The diagram shows the internal structure of a computer device. The computer device includes a processor, a computer-readable storage medium, a memory, and a network interface connected via a system bus. The computer-readable storage medium stores an operating system, a database, and computer-readable instructions. The database stores control information sequences. When the computer-readable instructions are executed by the processor, they enable the processor to implement a smart contract vulnerability detection method based on a large language model and retrieval enhancement generation. The processor of the computer device provides computing and control capabilities to support the operation of the entire computer device. The memory of the computer device stores computer-readable instructions. When these computer-readable instructions are executed by the processor, they enable the processor to execute the smart contract vulnerability detection method based on a large language model and retrieval enhancement generation of this application. The network interface of the computer device is used for communication with a terminal. Those skilled in the art will understand that… Figure 4 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.

[0158] In this embodiment, the processor is used to execute... Figure 3 The specific functions of each module are defined within the device, and the memory stores the program code and various data required to execute these modules. The network interface is used for data transmission between the user terminal and the server. In this embodiment, the memory stores the program code and data required to execute all modules in the smart contract vulnerability detection device based on a large language model and enhanced retrieval generation proposed in this application. The server can call the server's program code and data to execute the functions of all modules.

[0159] This application also provides a storage medium storing computer-readable instructions, which, when executed by one or more processors, cause the one or more processors to perform the steps of the smart contract vulnerability detection method based on large language model and retrieval enhancement as described in any embodiment of this application.

[0160] This application also provides a computer program product, including a computer program / instructions that, when executed by one or more processors, implement the steps of the smart contract vulnerability detection method based on large language model and retrieval enhancement as described in any embodiment of this application.

[0161] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments of this application can be implemented by a computer program instructing related hardware. This computer program can be stored in a computer-readable storage medium, and when executed, it can include the processes of the embodiments of the methods described above. The aforementioned storage medium can be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.

[0162] The above description is only a partial embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.

Claims

1. A smart contract vulnerability detection method based on large language models and retrieval enhancement, characterized in that, include: Obtain the code block corresponding to the smart contract to be detected and its corresponding code attributes. The code block includes multiple code fragments, each code fragment representing a specific functional module. The code attributes include interface name, function definition and its corresponding variable declaration. The code block and its corresponding code attributes are input into a preset large language model. The `thinking chain` technique is used to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested. This guides the large language model to perform vulnerability detection on the code block of the smart contract to be tested based on the set of detection prompt words, thereby determining the vulnerability detection result of the large language model. This includes: based on the set of detection prompt words corresponding to the smart contract to be tested, guiding the large language model to first identify external calls and state change functions, then analyze the interface, function call order and conditions, and finally determine the smart contract vulnerability type of the smart contract to be tested, including: code block The input is fed into a large language model, and the model is guided step by step to analyze potential vulnerabilities using the thought chain technique. Let the set of detection prompt words be... ,in, This represents the set of detection prompt words, and the model's output is the vulnerability detection result of a large language model. The mathematical expression for this module can be represented as: ,in, An analytical function representing the combination of a large language model and a thought chain; The thought chain analysis process is iterative; at each step, the model proceeds to the next step based on the current analysis results and guiding questions. Let the first step be... The analysis status of the step is The analysis process can then be represented as: ,in, It is the first The issue of guiding steps, It is a thought chain analysis function that updates the analysis state based on the current state and the guiding question; After multiple steps of thought chain analysis, the model finally generates vulnerability detection results for a large language model. This result contains detailed analysis and verification information on potential vulnerabilities in the code block, and can be represented as: , in, It was through The final state after step-by-step mind chain analysis It is a function for generating vulnerability detection results; The set of detection prompts includes the types of smart contract vulnerabilities that may occur in the code block and their corresponding vulnerability attributes. The vulnerability attributes characterize the vulnerability scenario description of each smart contract vulnerability type. The vulnerability scenario description characterizes the code segment in which the smart contract vulnerability occurs and its corresponding code attributes or code operations. The vulnerability detection result is constructed from the detected smart contract vulnerability types of the code block and their corresponding vulnerability scenario descriptions. A large language model is used to determine the most suitable smart contract detection tool based on the smart contract to be detected. The smart contract to be detected is then input into the smart contract detection tool to determine the vulnerability detection results of the tool. The code block is input into a preset RAG model, and similar data items corresponding to the code block are retrieved from a preset vulnerability contract dataset. This includes: using an embedded vector model to convert each data item in the vulnerability contract dataset into its corresponding first vector representation; converting the code block corresponding to the smart contract to be detected into its corresponding second vector representation; calculating the cosine similarity between the second vector representation and the first vector representation; sorting the cosine similarities from high to low; and selecting the top... The most relevant data item is selected as the similar data item corresponding to the code block; the generator of the RAG model is used to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data item corresponding to the code block, so as to determine the vulnerability detection result of the RAG model. The vulnerability contract dataset includes code blocks of multiple smart contracts with known vulnerabilities and their corresponding vulnerability scenario descriptions. The similar data item represents the code block in the preset vulnerability contract dataset that is similar to the code block corresponding to the smart contract to be detected in terms of function, structure or logic, and the corresponding vulnerability scenario description. The code block corresponding to the smart contract to be tested, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model are input into the large language model enhanced by the MindChain algorithm to determine the smart contract vulnerability type and its corresponding vulnerability scenario description, thereby completing the smart contract vulnerability detection. In the large language model enhanced by the MindChain algorithm, the MindChain algorithm guides the comprehensive analysis and judgment of each detection result, considering the correlation and consistency between the results, and finally determines whether the detection result is correct. If there are differences between the results, the MindChain algorithm guides the model to further analyze the reasons and determine which result is more credible. When two or more models detect the same vulnerability and the confidence level is higher than a certain threshold, the vulnerability is determined to exist. Or when there are large differences between the detection results of each model, the reasons are further analyzed, and the judgment logic of the MindChain algorithm is combined to determine the final result. The smart contract vulnerability types include reentrancy attack vulnerabilities, time-dependent vulnerabilities, integer overflow and underflow vulnerabilities, and access control vulnerabilities.

2. The smart contract vulnerability detection method based on large language model and retrieval enhancement as described in claim 1, characterized in that, Before obtaining the code block corresponding to the smart contract to be tested and its corresponding code attributes, the following steps are included: Respond to the code block preprocessing instruction to obtain the code block corresponding to the smart contract to be tested; The blank lines in the code block corresponding to the smart contract to be tested are deleted, the blank lines in the comments of the code block corresponding to the smart contract to be tested are deleted, and the newline characters in the function definition of the code block corresponding to the smart contract to be tested are merged to determine the code block corresponding to the smart contract to be tested and its corresponding code attributes.

3. The smart contract vulnerability detection method based on large language model and retrieval enhancement generation according to any one of claims 1 to 2, characterized in that, The underlying network architecture of the large language model includes the GPT model.

4. A smart contract vulnerability detection device based on large language models and retrieval enhancement, characterized in that, include: The preprocessing module is configured to obtain the code block corresponding to the smart contract to be detected and its corresponding code attributes. The code block includes multiple code fragments, each code fragment representing a specific functional module. The code attributes include interface name, function definition and its corresponding variable declaration. The first vulnerability detection module is configured to input the code block and its corresponding code attributes into a preset large language model, and use the MindChain technique to determine the set of detection prompt words corresponding to the code block of the smart contract to be tested, so as to guide the large language model to perform vulnerability detection on the code block of the smart contract to be tested according to the set of detection prompt words, and to determine the vulnerability detection result of the large language model. This includes: based on the set of detection prompt words corresponding to the smart contract to be tested, guiding the large language model to first identify external calls and state change functions, then analyze the interface, function call order and conditions, and finally determine the smart contract vulnerability type of the smart contract to be tested, including: code block The input is fed into a large language model, and the model is guided step by step to analyze potential vulnerabilities using the thought chain technique. Let the set of detection prompt words be... ,in, This represents the set of detection prompt words, and the model's output is the vulnerability detection result of a large language model. The mathematical expression for this module can be represented as: ,in, An analytical function representing the combination of a large language model and a thought chain; The thought chain analysis process is iterative; at each step, the model proceeds to the next step based on the current analysis results and guiding questions. Let the first step be... The analysis status of the step is The analysis process can then be represented as: ,in, It is the first The issue of guiding steps, It is a thought chain analysis function that updates the analysis state based on the current state and the guiding question; After multiple steps of thought chain analysis, the model finally generates vulnerability detection results for a large language model. This result contains detailed analysis and verification information on potential vulnerabilities in the code block, and can be represented as: , in, It was through The final state after step-by-step mind chain analysis It is a function for generating vulnerability detection results; The set of detection prompts includes the types of smart contract vulnerabilities that may occur in the code block and their corresponding vulnerability attributes. The vulnerability attributes characterize the vulnerability scenario description of each smart contract vulnerability type. The vulnerability scenario description characterizes the code segment in which the smart contract vulnerability occurs and its corresponding code attributes or code operations. The vulnerability detection result is constructed from the detected smart contract vulnerability types of the code block and their corresponding vulnerability scenario descriptions. The second vulnerability detection module is configured to use a large language model to determine the most suitable smart contract detection tool based on the smart contract to be detected, and input the smart contract to be detected into the smart contract detection tool to determine the vulnerability detection result of the tool. The third vulnerability detection module is configured to input the code block into a preset RAG model and retrieve similar data items corresponding to the code block from a preset vulnerability contract dataset. This includes: using an embedded vector model to convert each data item in the vulnerability contract dataset into its corresponding first vector representation; converting the code block corresponding to the smart contract to be detected into its corresponding second vector representation; calculating the cosine similarity between the second vector representation and the first vector representation; sorting the cosine similarities from high to low; and selecting the top... The most relevant data item is selected as the similar data item corresponding to the code block; the generator of the RAG model is used to perform vulnerability detection on the code block of the smart contract to be detected based on the similar data item corresponding to the code block, so as to determine the vulnerability detection result of the RAG model. The vulnerability contract dataset includes code blocks of multiple smart contracts with known vulnerabilities and their corresponding vulnerability scenario descriptions. The similar data item represents the code block in the preset vulnerability contract dataset that is similar to the code block corresponding to the smart contract to be detected in terms of function, structure or logic, and the corresponding vulnerability scenario description. The vulnerability detection and verification module is configured to input the code block corresponding to the smart contract to be detected, the vulnerability detection results of the large language model, the vulnerability detection results of the tool, and the vulnerability detection results of the RAG model into the large language model enhanced by the MindChain algorithm. This determines the type of smart contract vulnerability and its corresponding vulnerability scenario description, thereby completing the vulnerability detection of the smart contract. In the large language model enhanced by the MindChain algorithm, the MindChain algorithm guides the comprehensive analysis and judgment of each detection result, considering the correlation and consistency between the results, and finally determines whether the detection result is correct. If there are differences between the results, the MindChain algorithm guides the model to further analyze the reasons and determine which result is more credible. When two or more models detect the same vulnerability and the confidence level is higher than a certain threshold, the vulnerability is determined to exist. Or, when there are significant differences between the detection results of each model, the reasons are further analyzed, and the judgment logic of the MindChain algorithm is combined to determine the final result. The types of smart contract vulnerabilities include reentrancy attack vulnerabilities, time-dependent vulnerabilities, integer overflow and underflow vulnerabilities, and access control vulnerabilities.

5. An electronic device comprising a central processing unit and a memory, characterized in that, The central processing unit is used to invoke and run a computer program stored in the memory to perform the steps of the method as described in any one of claims 1 to 3.

6. A computer-readable storage medium, characterized in that, It stores, in the form of computer-readable instructions, a computer program implemented according to any one of claims 1 to 3, which, when invoked by a computer, executes the steps included in the corresponding method.

Citation Information

Patent Citations

  • Vulnerability code tool generation method and system based on large language model and medium

    CN118192949A

  • NFT intelligent contract vulnerability detection method based on large language agent

    CN119203156A

  • Large language model intelligent contract code auditing method and system based on retrieval enhancement generation and back one-step cue word

    CN119416217A