NFT intelligent contract defect detection method and system based on large language model
Through the NFT smart contract defect detection method based on large language model, abstract syntax tree features of smart contracts are extracted and text embedding models are combined to solve the problem of poor accuracy of traditional detection methods and achieve higher detection accuracy and performance.
Patent Information
- Application Number
- CN202411542051.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-31
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2044-10-31
AI Technical Summary
Traditional smart contract defect detection methods have the problem of low detection performance, resulting in poor accuracy of detection results.
Using the NFT smart contract defect detection method based on large language model, by obtaining the source code and sample code of the smart contract, generating an abstract syntax tree, extracting syntax structure and behavioral features, filtering and identifying, and combining the text embedding model to convert it into a feature vector and Code vectors, stored in the knowledge base, and audit reports are generated through large language models.
It improves the detection accuracy of smart contract defect detection, can identify programming errors and deep-level semantic vulnerabilities, and enhances detection performance.
Smart Images

Figure CN119396690A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer network security technology, and in particular to an NFT smart contract defect detection method and system based on a large language model. Background Art
[0002] A fungible token (NFT) is a non-replicable digital asset or unique identifier managed on a blockchain that is used to assign, link, or prove ownership of different physical and digital goods. When an NFT is created, a smart contract is deployed on the blockchain as a digital certificate of ownership, which contains information about the NFT, such as its title, description, and ownership history. With the rapid development of the NFT market, the security issues of smart contracts have gradually been exposed, becoming a key factor hindering the healthy development of the market. Once deployed, smart contracts cannot be changed. Although this immutability ensures the transparency and reliability of transactions, it also means that if there are vulnerabilities, attackers can exploit these flaws for malicious purposes, which may lead to significant financial losses. These vulnerabilities mainly stem from logical flaws, contract design flaws, and the lack of full consideration of potential security risks during the development process.
[0003] Traditional methods for detecting smart contract vulnerabilities, such as analysis tools such as Slither and Truffle, can conduct a comprehensive review of smart contracts without executing the code to identify common security flaws. However, these methods have certain limitations. Static analysis often relies on predefined rules and patterns, which is prone to false positives when dealing with complex smart contracts, making it difficult to accurately identify real security threats; while dynamic analysis detects runtime behavior by executing code, thereby being able to discover more environment-related vulnerabilities, it also faces challenges such as high computational cost, limited coverage, and difficulty in simulating real attack scenarios.
[0004] Therefore, the traditional smart contract defect detection method has the problem of low detection performance, resulting in poor accuracy of detection results. Summary of the invention
[0005] Based on this, in order to solve the above technical problems, a NFT smart contract defect detection method and system based on a large language model is provided, which can improve the detection accuracy of smart contract defect detection.
[0006] A method for detecting defects in NFT smart contracts based on a large language model, the method comprising:
[0007] Get the source code and sample code with NFT smart contract defects;
[0008] Generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements;
[0009] Screening and filtering the key elements, and identifying the key elements based on the abstract syntax tree to obtain defect-related features;
[0010] Combining the defect-related features with the sample code, inputting the combined features into a text embedding model to convert them into feature vectors and code vectors, and storing them in a knowledge base;
[0011] Determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result;
[0012] According to the comparison result, the target feature vector and the target code vector are input into the large language model to generate an audit report for smart contract defect detection.
[0013] In one embodiment, the source code and sample code having the NFT smart contract defect are obtained, including:
[0014] Get the source code with ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy flaws;
[0015] Based on the source code, the smart contracts with defect types of ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy are classified, and the corresponding defect sample codes are extracted.
[0016] In one embodiment, the grammatical structure and behavior features in the abstract syntax tree are extracted to obtain key elements, including:
[0017] According to different defect types, the code structure and grammatical hierarchy of the smart contract are extracted;
[0018] Key elements are extracted based on the abstract syntax tree; the key elements include state variables, function definitions, control flows, function call records, and function call locations.
[0019] In one embodiment, the key elements are screened and filtered, and key elements are identified based on the abstract syntax tree to obtain defect-related features, including:
[0020] Filter according to the key elements to obtain key information corresponding to ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy defects;
[0021] The syntax pattern in the function definition is extracted based on the abstract syntax tree, the logical relationship of the function call record is analyzed, and the usage status of the state variable is checked to obtain defect-related features corresponding to the ERC 721Reentrancy, PublicBurn, and Risky Mutable Proxy defects.
[0022] In one embodiment, the defect-related features are combined with the sample code, the combined features are input into a text embedding model to be converted into feature vectors and code vectors, and stored in a knowledge base, including:
[0023] Combine the defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects with the defect sample codes corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects, respectively, to obtain the combined features;
[0024] Inputting the combined features into a text embedding model, converting the combined features into feature vectors and code vectors through the text embedding model, and performing high-dimensional vector representation of the feature vectors and code vectors;
[0025] The feature vector and the code vector are stored in a knowledge base.
[0026] In one embodiment, determining a target feature vector and a target code vector corresponding to a contract to be detected includes:
[0027] Acquire a smart contract to be detected, and extract an actual abstract syntax tree from the smart contract to be detected to obtain structural information corresponding to the smart contract to be detected;
[0028] Filtering the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected;
[0029] The defect-related features corresponding to the smart contract to be detected are combined with the sample code and input into the text embedding model to be converted into a target feature vector and a target code vector.
[0030] In one embodiment, comparing the target feature vector and the target code vector with the feature vector and the code vector in the knowledge base by cosine similarity includes:
[0031] Calculating the dot product of the target feature vector and the feature vector divided by the product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector;
[0032] The dot product of the target code vector and the code vector is calculated and divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
[0033] In one embodiment, the target feature vector and the target code vector are input into a large language model according to the comparison result to generate an audit report for smart contract defect detection, including:
[0034] According to the comparison result, inputting the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into a large language model;
[0035] Performing similarity verification and contextual relationship analysis through the large language model;
[0036] The target feature vector and the target code vector are semantically analyzed by the large language model and an audit report is generated.
[0037] A NFT smart contract defect detection system based on a large language model, the system comprising:
[0038] Code acquisition module, used to obtain source code and sample code with NFT smart contract defects;
[0039] A feature extraction module is used to generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements;
[0040] A screening and filtering module, used to screen and filter the key elements, and identify the key elements based on the abstract syntax tree to obtain defect-related features;
[0041] A feature conversion module, used to combine the defect-related features with the sample code, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base;
[0042] A vector comparison module is used to determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result;
[0043] A defect detection module is used to input the target feature vector and the target code vector into a large language model according to the comparison result, and generate an audit report for smart contract defect detection.
[0044] The above-mentioned NFT smart contract defect detection method and system based on a large language model extracts the grammatical structure and behavioral features in the abstract syntax tree, converts them into vector representation through a text embedding model and stores them in the knowledge base, so as to facilitate the subsequent extraction of features for cosine similarity calculation to determine the defect type. The use of a large language model can analyze and understand complex semantics and contextual relationships, which can not only identify programming errors, but also capture deep semantic vulnerabilities, thereby improving the detection performance of smart contract defects and improving detection accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] Figure 1 This is an application environment diagram of an NFT smart contract defect detection method based on a large language model in one embodiment;
[0046] Figure 2 It is a flowchart of an NFT smart contract defect detection method based on a large language model in one embodiment;
[0047] Figure 3 A schematic diagram of the process architecture of knowledge retrieval and audit report generation in one embodiment;
[0048] Figure 4 A schematic diagram of the process architecture of an NFT smart contract defect detection method based on a large language model in one embodiment;
[0049] Figure 5 It is a structural block diagram of an NFT smart contract defect detection system based on a large language model in one embodiment;
[0050] Figure 6 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0051] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0052] The NFT smart contract defect detection method based on a large language model provided in the embodiment of the present application can be applied to Figure 1 In the application environment shown. Figure 1As shown, the application environment includes a computer device 110. The computer device 110 can obtain source code and sample code with NFT smart contract defects; the computer device 110 can generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavior features in the abstract syntax tree, and obtain key elements; the computer device 110 can filter the key elements, and identify the key elements based on the abstract syntax tree to obtain defect-related features; the computer device 110 can combine the defect-related features with the sample code, input the combined features into the text embedding model to convert them into feature vectors and code vectors, and store them in the knowledge base; the computer device 110 can determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result; the computer device 110 can input the target feature vector and target code vector into the large language model according to the comparison result to generate an audit report for smart contract defect detection. Among them, the computer device 110 can be, but not limited to, various personal computers, laptops, smart phones, robots, unmanned aerial vehicles and other devices.
[0053] In one embodiment, the overall workflow of a NFT smart contract defect detection method based on a large language model can be composed of three main stages, namely, knowledge base construction, knowledge base retrieval, and large language model reasoning. Figure 2 As shown, a method for detecting defects in NFT smart contracts based on a large language model is provided, comprising the following steps:
[0054] Step 202, obtain source code and sample code with NFT smart contract defects.
[0055] In an NFT smart contract defect detection method based on a large language model provided in this application, static analysis can be used to first extract structural and grammatical information from the smart contract defect code.
[0056] Specifically, in one embodiment, a large language model-based NFT smart contract defect detection method provided may also include a process of obtaining code, the specific process including: obtaining source code with ERC 721Reentrancy, PublicBurn, and Risky Mutable Proxy defects; based on the source code, classifying smart contracts with defect types of ERC721Reentrancy, Public Burn, and Risky Mutable Proxy, and extracting corresponding defect sample codes. That is, the computer device can classify the three types of defect smart contracts in the collected database and extract the corresponding defect sample codes.
[0057] Step 204, generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements.
[0058] After obtaining the source code and sample code, the computer device can extract the abstract syntax tree of the smart contract. Specifically, in one embodiment, a method for detecting defects in NFT smart contracts based on a large language model can also include a feature extraction process, which includes: extracting the code structure and grammatical hierarchy of the smart contract according to different defect types; extracting key elements based on the abstract syntax tree; key elements include state variables, function definitions, control flows, function call records, and function call locations.
[0059] The computer device can extract the abstract syntax tree and obtain key elements, including state variables, function definitions, and function call records, while also recording the locations of these calls. Specifically, the computer device can first extract the structure and grammatical hierarchy of the contract code based on the characteristics of different types of defects; then extract key elements, including state variables stae_var, function definitions fun_def, and function call records fun_call, while also recording the locations of these calls call_loc. In this embodiment, for ERC 721 standard security functions, the location and function name are recorded, and if any state variables are modified, the names of these modified variables are extracted and associated with the corresponding function call locations.
[0060] Step 206 , filter the key elements, and identify the key elements based on the abstract syntax tree to obtain defect-related features.
[0061] The computer device can further filter the abstract syntax tree to identify key information related to known defects, thereby obtaining defect-related features. Specifically, it can filter and identify syntax patterns in function definitions, analyze the logical relationship of function calls, and check the use of state variables.
[0062] In one embodiment, a NFT smart contract defect detection method based on a large language model is provided, which may also include a process of filtering and selecting related features, and the specific process includes: filtering according to key elements to obtain key information corresponding to ERC721Reentrancy, Public Burn, and Risky Mutable Proxy defects; extracting the syntax pattern in the function definition based on the abstract syntax tree, analyzing the logical relationship of the function call record, and checking the usage status of the state variables to obtain defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects.
[0063] The computer device can further filter the acquired key elements to obtain key information related to the three defects, thereby obtaining defect-related features.
[0064] Step 208, combining the defect-related features with the sample code, inputting the combined features into the text embedding model to convert them into feature vectors and code vectors, and storing them in the knowledge base.
[0065] The computer device can combine the extracted defect-related features with the known defect code snippet, i.e., the sample code, input them into the text embedding model text-embeding-004, and store them in the knowledge base.
[0066] In one embodiment, a large language model-based NFT smart contract defect detection method provided may also include a vector conversion process, the specific process including: combining the defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy defects with the defect example codes corresponding to the ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy defects, respectively, to obtain combined features; inputting the combined features into a text embedding model, converting the combined features into feature vectors and code vectors through the text embedding model, and representing the feature vectors and code vectors as high-dimensional vectors; storing the feature vectors and code vectors in a knowledge base.
[0067] The computer device can combine the three defect features obtained with the corresponding code example snippets and convert them into high-dimensional vector representation using a text embedding model (text-embedding-004). Then, the converted feature vectors of the three defects and the code example vectors can be stored in a knowledge base (VectorDB) to store vectors of different defect types.
[0068] Step 210, determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result.
[0069] In one embodiment, a NFT smart contract defect detection method based on a large language model is provided, which may also include a process of determining a target feature vector and a target code vector. The specific process includes: obtaining the smart contract to be detected, and extracting the actual abstract syntax tree from the smart contract to be detected to obtain structural information corresponding to the smart contract to be detected; filtering the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected; combining the defect-related features corresponding to the smart contract to be detected with the sample code and inputting them into the text embedding model to convert them into target feature vectors and target code vectors.
[0070] When actually performing smart contract defect detection, it is necessary to determine the contract to be detected, and then extract the features in the abstract syntax tree based on the contract to be detected, and execute steps 204 to 208, that is, extract the abstract syntax tree from the contract to be detected to obtain the structural information of the basic contract; further filter the abstract syntax tree to obtain key information related to known defects, including the syntax pattern in the function definition, analyze the logical relationship of the function call, and check the use of state variables, etc., so as to obtain defect-related features; input the obtained key information together with the source code into the text embedding model (text-embedding-004) to generate feature vectors and code vectors as target feature vectors and target code vectors.
[0071] In one embodiment, a NFT smart contract defect detection method based on a large language model is provided, which may also include a process of performing cosine similarity comparison, and the specific process includes: calculating the dot product of the target feature vector and the feature vector divided by the product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector; calculating the dot product of the target code vector and the code vector divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
[0072] like Figure 3 As shown, when the computer device performs preliminary defect matching, the target feature vector and the target code vector can be evaluated for similarity with the known defect vectors in the knowledge base through cosine similarity. Specifically, the closer the similarity value is to 1, the more similar the directions of the two vectors in the feature space are, indicating that the degree of match between the contract to be analyzed and the known defect is higher.
[0073] Step 212: Input the target feature vector and the target code vector into the large language model according to the comparison result to generate an audit report for smart contract defect detection.
[0074] The computer device can input the target feature vector and the target code vector into the large language model gemini-pro-1.5 and generate an audit report.
[0075] Specifically, in one embodiment, a NFT smart contract defect detection method based on a large language model is provided, which may also include a process of reasoning and generating an audit report through a large language model, and the specific process includes: according to the comparison result, inputting the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into the large language model; performing similarity verification and context relationship analysis through the large language model; performing semantic analysis on the target feature vector and the target code vector through the large language model and generating an audit report.
[0076] The computer device can obtain vectors with high similarity, and then input these defect type vectors and the feature vectors and code vectors of the contract to be detected into the large language model for reasoning; the large language model deeply analyzes the contextual relationship between the identified defect type and the current contract, as well as the behavior pattern of known vulnerabilities; then the large language model performs semantic analysis on the input vector and generates a detailed audit report. Among them, the audit report includes the detected defect type, including a detailed defect description, potential security impact, and recommended remedial measures.
[0077] In one embodiment, an application framework of an NFT smart contract defect detection method based on a large language model is provided as follows: Figure 3 , Figure 4 As shown in the figure: obtain the smart contract source code of three defect types, then extract the feature vectors and corresponding defect sample codes from the smart contract source code based on the abstract syntax tree, convert them into vector representations through the text embedding model, and store them in the vector database, where the database acts as a knowledge base; extract features from the contract to be tested, and input them into the embedding model with the contract code to generate feature vectors and code vectors, compare these vectors with the known defect vectors in the knowledge base, use cosine similarity to perform preliminary defect types, and complete knowledge retrieval; use a large language model to infer and analyze the matching defect types in detail, and finally produce an audit report.
[0078] It should be understood that, although the various steps in the above-mentioned flow chart are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the above-mentioned flow chart may include a plurality of sub-steps or a plurality of stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these sub-steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the sub-steps or stages of other steps.
[0079] In one embodiment, Figure 5 As shown, a NFT smart contract defect detection system based on a large language model is provided, including: a code acquisition module 510, a feature extraction module 520, a screening and filtering module 530, a feature conversion module 540, a vector comparison module 550 and a defect detection module 560, wherein:
[0080] A code acquisition module 510, used to obtain source code and sample code with NFT smart contract defects;
[0081] A feature extraction module 520 is used to generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements;
[0082] The screening and filtering module 530 is used to screen and filter key elements, and identify key elements based on the abstract syntax tree to obtain defect-related features;
[0083] A feature conversion module 540 is used to combine defect-related features with sample codes, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base;
[0084] The vector comparison module 550 is used to determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result;
[0085] The defect detection module 560 is used to input the target feature vector and the target code vector into the large language model according to the comparison result, and generate an audit report for smart contract defect detection.
[0086] In one embodiment, the code acquisition module 510 is also used to obtain source code with ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects; based on the source code, the smart contracts with defect types of ERC721Reentrancy, Public Burn, and Risky Mutable Proxy are classified, and the corresponding defect sample codes are extracted.
[0087] In one embodiment, the feature extraction module 520 is also used to extract the code structure and grammatical hierarchy of the smart contract according to different defect types; extract key elements based on the abstract syntax tree; the key elements include state variables, function definitions, control flows, function call records, and function call locations.
[0088] In one embodiment, the screening and filtering module 530 is also used to filter according to key elements to obtain key information corresponding to ERC721Reentrancy, Public Burn, and Risky Mutable Proxy defects; extract the syntax pattern in the function definition based on the abstract syntax tree, analyze the logical relationship of the function call record, and check the usage status of the state variable to obtain the defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects.
[0089] In one embodiment, the feature conversion module 540 is also used to combine the defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects with the defect example codes corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects, respectively, to obtain combined features; input the combined features into a text embedding model, convert the combined features into feature vectors and code vectors through the text embedding model, and represent the feature vectors and code vectors as high-dimensional vectors; store the feature vectors and code vectors in a knowledge base.
[0090] In one embodiment, the vector comparison module 550 is also used to obtain the smart contract to be detected, and extract the actual abstract syntax tree from the smart contract to be detected to obtain the structural information corresponding to the smart contract to be detected; filter the actual abstract syntax tree to obtain the defect-related features corresponding to the smart contract to be detected; combine the defect-related features corresponding to the smart contract to be detected with the sample code and input them into the text embedding model to convert them into target feature vectors and target code vectors.
[0091] In one embodiment, the vector comparison module 550 is also used to calculate the dot product of the target feature vector and the feature vector divided by the product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector; and calculate the dot product of the target code vector and the code vector divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
[0092] In one embodiment, the defect detection module 560 is also used to input the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into the large language model according to the comparison result; perform similarity verification and context relationship analysis through the large language model; perform semantic analysis on the target feature vector and the target code vector through the large language model and generate an audit report.
[0093] In one embodiment, a computer device is provided. The computer device may be a terminal, and its internal structure diagram may be as follows: Figure 6 As shown. The computer device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for detecting defects in an NFT smart contract based on a large language model is implemented. The display screen of the computer device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covered on the display screen, or a button, trackball or touchpad provided on the housing of the computer device, or an external keyboard, touchpad or mouse, etc.
[0094] Those skilled in the art will understand that Figure 6 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0095] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the steps of the NFT smart contract defect detection method based on a large language model are implemented.
[0096] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the NFT smart contract defect detection method based on a large language model are implemented.
[0097] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
[0098] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0099] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.
Claims
1. A method for detecting defects in NFT smart contracts based on a large language model, characterized in that: The method comprises: Get the source code and sample code with NFT smart contract defects; Generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements; Screening and filtering the key elements, and identifying the key elements based on the abstract syntax tree to obtain defect-related features; Combining the defect-related features with the sample code, inputting the combined features into a text embedding model to convert them into feature vectors and code vectors, and storing them in a knowledge base; Determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result; According to the comparison result, the target feature vector and the target code vector are input into the large language model to generate an audit report for smart contract defect detection.
2. The NFT smart contract defect detection method based on a large language model according to claim 1 is characterized in that: Get source code with NFT smart contract flaws and sample code, including: Get the source code with ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy flaws; Based on the source code, the smart contracts with defect types of ERC 721Reentrancy, Public Burn, and Risky MutableProxy are classified, and the corresponding defect sample codes are extracted.
3. The NFT smart contract defect detection method based on a large language model according to claim 2 is characterized in that: The grammatical structure and behavioral features in the abstract syntax tree are extracted to obtain key elements, including: According to different defect types, the code structure and grammatical hierarchy of the smart contract are extracted; Key elements are extracted based on the abstract syntax tree; the key elements include state variables, function definitions, control flows, function call records, and function call locations.
4. The NFT smart contract defect detection method based on a large language model according to claim 2 is characterized in that: The key elements are screened and filtered, and key elements are identified based on the abstract syntax tree to obtain defect-related features, including: Filter according to the key elements to obtain key information corresponding to ERC 721Reentrancy, Public Burn, and RiskyMutable Proxy defects; The syntax pattern in the function definition is extracted based on the abstract syntax tree, the logical relationship of the function call record is analyzed, and the usage status of the state variable is checked to obtain defect-related features corresponding to ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects.
5. The NFT smart contract defect detection method based on a large language model according to claim 4 is characterized in that: Combining the defect-related features with the sample code, inputting the combined features into a text embedding model to convert them into feature vectors and code vectors, and storing them in a knowledge base, including: Combine the defect-related features corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects with the defect sample codes corresponding to the ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy defects, respectively, to obtain the combined features; Inputting the combined features into a text embedding model, converting the combined features into feature vectors and code vectors through the text embedding model, and performing high-dimensional vector representation of the feature vectors and code vectors; The feature vector and the code vector are stored in a knowledge base.
6. The NFT smart contract defect detection method based on a large language model according to claim 1 is characterized in that: Determine the target feature vector and target code vector corresponding to the contract to be tested, including: Acquire a smart contract to be detected, and extract an actual abstract syntax tree from the smart contract to be detected to obtain structural information corresponding to the smart contract to be detected; Filtering the actual abstract syntax tree to obtain defect-related features corresponding to the smart contract to be detected; The defect-related features corresponding to the smart contract to be detected are combined with the sample code and input into the text embedding model to be converted into a target feature vector and a target code vector.
7. The NFT smart contract defect detection method based on a large language model according to claim 6 is characterized in that: Comparing the target feature vector and the target code vector with the feature vector and the code vector in the knowledge base by cosine similarity, including: Calculating the dot product of the target feature vector and the feature vector divided by the product of their amplitudes to obtain a first similarity value between the target feature vector and the feature vector; The dot product of the target code vector and the code vector is calculated and divided by the product of their amplitudes to obtain a second similarity value between the target code vector and the code vector.
8. The NFT smart contract defect detection method based on a large language model according to claim 7 is characterized in that: According to the comparison result, the target feature vector and the target code vector are input into the large language model to generate an audit report for smart contract defect detection, including: According to the comparison result, inputting the target feature vector with the highest first similarity value and the target code vector with the highest second similarity value into a large language model; Performing similarity verification and contextual relationship analysis through the large language model; The target feature vector and the target code vector are semantically analyzed by the large language model and an audit report is generated.
9. A NFT smart contract defect detection system based on a large language model, characterized in that: The system comprises: Code acquisition module, used to obtain source code and sample code with NFT smart contract defects; A feature extraction module, used to generate an abstract syntax tree of the NFT smart contract based on the source code, extract the grammatical structure and behavioral features in the abstract syntax tree, and obtain key elements; A screening and filtering module, used to screen and filter the key elements, and identify the key elements based on the abstract syntax tree to obtain defect-related features; A feature conversion module, used to combine the defect-related features with the sample code, input the combined features into a text embedding model to convert them into feature vectors and code vectors, and store them in a knowledge base; A vector comparison module is used to determine the target feature vector and target code vector corresponding to the contract to be detected, and compare the cosine similarity with the feature vector and code vector in the knowledge base to obtain a comparison result; A defect detection module is used to input the target feature vector and the target code vector into a large language model according to the comparison result, and generate an audit report for smart contract defect detection.
10. The NFT smart contract defect detection system based on a large language model according to claim 9 is characterized in that: The code acquisition module is also used to: obtain source code with ERC 721Reentrancy, Public Burn, and Risky MutableProxy defects; based on the source code, classify smart contracts with defect types of ERC 721Reentrancy, Public Burn, and Risky Mutable Proxy, and extract corresponding defect sample codes.
Citation Information
Patent Citations
Vulnerability detection method for smart contract code snippets and related device
CN114201756A
Ethereum intelligent contract vulnerability detection and positioning method based on deep learning
CN116484384A