Intelligent contract similarity detection method based on hierarchical graph neural network
By using layered neural networks in smart contract detection, combined with BERT, Word2vec, and GCN models for static analysis, the problem of low accuracy of existing smart contract detection methods has been solved, and more efficient semantic similarity testing is achieved.
Patent Information
- Application Number
- CN202411519203.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-29
- Publication Date
- 2025-01-28
- Estimated Expiration
- 2044-10-29
AI Technical Summary
The existing smart contract similarity detection methods fail to fully explore the semantic representation of contract information, resulting in low detection accuracy.
Adopt the similarity detection method based on the layered neural network, to process the bytecode of the smart contract, extract information at the block node, function and contract level, and use models such as BERT, Word2vec, and GCN for static analysis. , Generate the characteristics at all levels, and eventually calculate the similarity score of smart contracts through MPNN.
Through the three levels of the contract layer, function layer and node layer, static analysis is performed to effectively tap the semantic signs of contract information, and the accuracy of smart contract similarity detection is improved.
Smart Images

Figure CN119377091A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of blockchain smart contract vulnerability mining, and specifically relates to a smart contract similarity detection method based on a hierarchical graph neural network. Background Art
[0002] At present, semantic similarity testing of smart contracts is one of the hot research directions in the field of blockchain, which includes static analysis and dynamic analysis. Static analysis is a method that analyzes source code without actually executing the code to find potential vulnerabilities and errors. In smart contracts, static analysis can detect semantic similarity and find potential security issues by performing symbolic execution and model checking on contract code; dynamic analysis is a method that finds vulnerabilities and errors by actually executing the code. In smart contracts, dynamic analysis can detect semantic similarity and find runtime errors and vulnerabilities by simulating the execution process of the contract and monitoring its behavior. When it comes to the security and correctness of smart contracts, semantic similarity testing is very necessary. For example, it can be used to detect code duplication. In smart contract development, copying and pasting code is very common. If a smart contract has a vulnerability and another contract maliciously copies the code of the contract, this may lead to problems such as overlapping functions and logical errors in the contract. By implementing semantic similarity testing, code duplication problems can be effectively identified and avoided. In addition, this testing method can prevent the injection of malicious code. Smart contracts may have security vulnerabilities, which may cause serious losses once they are discovered and exploited by malicious attackers. Attackers may try to bypass the security checks and restrictions of smart contracts by injecting malicious code, thereby damaging the contract. However, these potential malicious injection behaviors can be identified by performing semantic similarity checks. This test can deeply analyze the semantic content of the code to find code snippets that appear different but actually have similar functions. In this way, malicious code injection can be discovered and prevented in a timely manner, thereby enhancing the security of smart contracts and protecting them from attackers.
[0003] However, the existing smart contract similarity detection methods have not fully explored the semantic representation of contract information, resulting in the low accuracy of the existing smart contract similarity detection methods. It is an urgent problem to propose a new smart contract similarity detection method to solve the above problem. Summary of the invention
[0004] The purpose of this invention is to solve the problem that the existing smart contract similarity detection method does not fully explore the semantic representation of contract information, resulting in low accuracy of the smart contract similarity detection method, and proposes a smart contract similarity detection method based on a hierarchical graph neural network.
[0005] The technical solution adopted by the present invention to solve the above technical problems is: a smart contract similarity detection method based on a hierarchical graph neural network, the specific process of the method is:
[0006] After processing the bytecode of the smart contract, we obtain the block nodes, edges between block nodes, functions, function signatures corresponding to each function, edges between function signatures, and EVM instructions of the smart contract. Then, we perform static analysis from the node layer, function layer, and contract layer. The specific process of static analysis is as follows:
[0007] At the node level
[0008] Use the BERT model to transform each block node B i Convert to an initial vector Where i=0,1,…,m, m+1 is the total number of block nodes;
[0009] Then the initial vector After the MPNN network, the final feature representation of the output node layer
[0010] At the function level
[0011] Using Word2Vec to train the function F j Processing, generating function F j The context vector of Word2Vec is used to train the function F j The corresponding function signature is processed to generate function F j The corresponding function signature feature vector According to the function F j The context vector and function signature feature vector of To calculate the function F j The initial feature representation Where j = 0, 1, ..., n, n + 1 is the total number of functions;
[0012] And use GCN to transform the function F j Internal block nodes {B1,B2,...B m′} to aggregate and obtain structured function features Then and Add them together and pass the result through a multi-layer perceptron to get the function F j The updated initial feature representation
[0013] The updated initial feature representation of each function is passed through the MPNN network, and the final feature representation of the function layer is output through the MPNN network
[0014] At the contract level
[0015] Use Word2Vec to process smart contracts to generate contract context vectors, and use Word2Vec to process EVM instructions to generate contract layer instruction vectors Then according to the contract context vector and Compute the initial feature representation of the contract
[0016] GCN is used to process the feature representations of each function obtained by the function layer to obtain the processed features Then and Add, pass the addition result through a multi-layer perceptron, and use the output of the multi-layer perceptron as the final feature representation of the contract layer
[0017] Will and Perform splicing and use the splicing result as the feature of the smart contract;
[0018] For any two smart contracts to be tested for similarity, the features of each smart contract are extracted respectively, and the similarity scores of the two smart contracts to be tested for similarity are calculated based on the extracted features. If the similarity scores of the two smart contracts are greater than the score threshold, the two smart contracts are similar, otherwise the two smart contracts are not similar.
[0019] The beneficial effects of the present invention are:
[0020] The present invention first uses Octopus to process the bytecode of the smart contract, obtains the block nodes of the smart contract, the edges between the block nodes, the functions, the function signatures corresponding to each function, the edges between the function signatures and the EVM instructions, and then extracts the information of the three levels of the contract level, function level and node level from the processing results. At the node level, the pre-trained model is used to characterize the node semantic information, and the vector features of the node level can be obtained; at the function level, Word2vec is used to process and extract the function level information, and the semantic information of the node layer is vertically transmitted through the graph neural network, and the features obtained by GCN processing are spliced with the features obtained by Word2Vec processing, and the splicing result is regarded as the final representation of the function level; at the contract level, GCN is used to process the features of the function level, and then Word2vec is used to process the contract layer information, and the features obtained by GCN processing are added with the features obtained by Word2Vec processing, and the addition result is obtained by a multi-layer perceptron to obtain the final features of the contract level. Finally, the message propagation neural network (MPNN) is used to process the features of the node layer and the function layer, and the processing results are connected with the contract layer processing results, and the splicing result is the final feature of the smart contract. The above processing is performed on both smart contracts, and then the similarity scores between the two smart contracts are calculated, and the semantic similarity detection results of the two smart contracts are evaluated using this similarity score.
[0021] By performing static analysis at the contract layer, function layer, and node layer, it not only includes the semantic information of the three levels, but also transmits vertically between the levels, effectively mining the semantic representation of contract information, comprehensively considering the semantic similarity of smart contracts, and improving the accuracy of smart contract similarity detection. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] Figure 1 It is a schematic diagram of the data feature representation at three levels;
[0023] Obtain initial information at three levels for input data; Node level: Use pre-trained models to process initial information and obtain node-level feature representation; Function level: In addition to the features represented by initial information, there are also features extracted from the node level, which together constitute the features at the function level; Contract level: Similar to the function level, in addition to the features represented by initial information, there are also features extracted from the function level, which together constitute the features at the contract level;
[0024] Figure 2 It is the pre-trained model graph of the node layer;
[0025] The node-level data in the initial input data is processed through the pre-trained model to obtain a vector information representing the node-level feature representation;
[0026] Figure 3 It is a data relationship diagram at three levels;
[0027] The data interaction between the three levels is intuitively demonstrated;
[0028] Figure 4 It is a sample diagram of data evolution at three levels;
[0029] Specifically Figure 3 An example of longitudinally transmitting data in ;
[0030] Figure 5 It is a loss relationship diagram at three levels. DETAILED DESCRIPTION
[0031] Specific implementation method 1: Combination Figure 1 , Figure 2 , Figure 3 and Figure 4 This embodiment describes a method for detecting similarity of smart contracts based on a hierarchical graph neural network, and the specific process of the method is as follows:
[0032] After processing the bytecode of the smart contract, we obtain the block nodes, edges between block nodes, functions, function signatures corresponding to each function, edges between function signatures, and EVM instructions of the smart contract. Then, we perform static analysis from the node layer, function layer, and contract layer. The specific process of static analysis is as follows:
[0033] At the node level
[0034] Use the BERT model to transform each block node B i Convert to a high-dimensional initial vector To represent the node semantic information; where i = 0, 1, ..., m, m + 1 is the total number of block nodes;
[0035] Then the initial vector After the MPNN network, the final feature representation of the output node layer
[0036] At the function level
[0037] Using Word2Vec to train the function F j Processing, generating function F j The context vector of Word2Vec is used to train the function F j The corresponding function signature is processed to generate function F j The corresponding function signature feature vector According to the function F j The context vector and function signature feature vector of To calculate the function F jThe initial feature representation To enhance the relevance of features; where j = 0, 1, ..., n, n + 1 is the total number of functions;
[0038] And use GCN to transform the function F j Internal block nodes {B1,B2,...B m′} to aggregate and obtain structured function features Then and Add them together and pass the result through a multi-layer perceptron (MLP) to get the function F j The updated initial feature representation
[0039] The updated initial feature representation of each function is passed through the MPNN network, and the final feature representation of the function layer is output through the MPNN network
[0040] At the contract level
[0041] Use Word2Vec to process smart contracts to generate contract context vectors, and use Word2Vec to process EVM instructions to generate contract layer instruction vectors Then according to the contract context vector and Compute the initial feature representation of the contract
[0042] GCN is used to process the feature representations of each function obtained by the function layer to obtain the processed features Then and Add, pass the addition result through a multi-layer perceptron, and use the output of the multi-layer perceptron as the final feature representation of the contract layer
[0043] Will and Perform splicing and use the splicing result as the feature of the smart contract.
[0044] For any two smart contracts to be tested for similarity, the features of each smart contract are extracted respectively, and the similarity scores of the two smart contracts to be tested for similarity are calculated based on the extracted features. If the similarity scores of the two smart contracts are greater than the score threshold (the threshold can be set according to actual conditions), the two smart contracts are similar, otherwise the two smart contracts are not similar.
[0045] Similarity score The calculation method is:
[0046]
[0047] in, and are the characteristics of the two smart contracts to be tested for similarity, Representation calculation and The inner product of , ||·|| represents the L2 norm, and “*” represents multiplication.
[0048] Specific implementation method 2: This implementation method is different from specific implementation method 1 in that Octopus is used to process the bytecode of the smart contract.
[0049] The other steps and parameters are the same as those in the first embodiment.
[0050] Specific implementation method three: This implementation method is different from specific implementation methods one or two in that the function F j The context vector and function signature feature vector of To calculate the function F j The initial feature representation is:
[0051]
[0052] in, is the function F j The context vector of .
[0053] The other steps and parameters are the same as those in the first or second embodiment.
[0054] Specific implementation method 4: This implementation method is different from the specific implementation methods 1 to 3 in that the initial vector After the MPNN network, the final feature representation of the output node layer Specifically:
[0055] Step S1: And initialize the number of iterations l = 1;
[0056] Step S2: Calculate intermediate variables
[0057]
[0058] Among them, F msg is the message function, It is the block node B after the l-1th iteration i′ The corresponding vector, is the block node B after the l-1th iteration i The corresponding vector, e ii′ It is the head block node B i With the tail block node B i′ The edge type between Yes ii′ The vector obtained by the BERT model, "||" represents connection, N B is the set of block nodes, and E is the set of edges;
[0059] Step S3: According to the intermediate variable Update the tail block node B i′ The corresponding vector is:
[0060]
[0061] Among them, α and β are weight coefficients;
[0062] Step S4: whether l=L is satisfied, where L is the maximum number of iterations;
[0063] If satisfied, execute step S5;
[0064] If not, set l=l+1 and return to step S2;
[0065] Step S5: Aggregate the vectors corresponding to the nodes of each block after the Lth iteration through the average pooling layer to obtain the final feature representation of the node layer.
[0066] The other steps and parameters are the same as those in Specific Embodiments 1 to 3.
[0067] It should be noted that if a block node does not have a corresponding head block node, the initial vector of the block node will never be updated, that is, the initial vector is directly used as the vector after each iteration update.
[0068] Specific implementation method 5: This implementation method is different from the specific implementation methods 1 to 4 in that the contract context vector and Compute the initial feature representation of the contract Specifically:
[0069]
[0070] in, is the contract context vector.
[0071] The other steps and parameters are the same as those in Specific Embodiments 1 to 4.
[0072] Specific implementation method 6: This implementation method is different from the specific implementation methods 1 to 5 in that the specific process of step S5 is as follows:
[0073]
[0074] The other steps and parameters are the same as those in Specific Implementation Methods 1 to 5.
[0075] Specific implementation method 7: This implementation method is different from the specific implementation methods 1 to 6 in that the function F is converted into j Internal block nodes {B1,B2,...B m′} to aggregate and obtain structured function features Then and Add them together and pass the result through a multi-layer perceptron to get the function F j The updated initial feature representation of ; specifically:
[0076]
[0077] Where m′ is the function F j The total number of internal block nodes, SET(F j ) is the function F j The internal block node collection, is the function F j The updated initial feature representation of .
[0078] The other steps and parameters are the same as those in Specific Embodiments 1 to 6.
[0079] Specific implementation eight: This implementation differs from the specific implementations one to seven in that the updated initial feature representation of each function is passed through the MPNN network, and the final feature representation of the function layer is output through the MPNN network. Specifically:
[0080] Step A1: Initialize the number of iterations l = 1;
[0081] Step A2: Calculate intermediate variables
[0082]
[0083] Among them, F msg is the message function, is the function F obtained after the l-1th iteration j The characteristic representation of is the function F obtained after the l-1th iteration j′ The feature representation of z jj′ is the header function F j With the tail function F j′ The edge between Yes jj′ The vector obtained by the BERT model, N F is a set of functions, and Z is a set of edges between functions;
[0084] Step A3: Based on the intermediate variables Update function F j′ The characteristic representation of:
[0085]
[0086] Step A4: whether l=L is satisfied, where L is the maximum number of iterations;
[0087] If satisfied, proceed to step A5;
[0088] If not, set l=l+1 and return to step A2;
[0089] Step A5: Aggregate the function feature representation obtained after the Lth iteration through the average pooling layer to obtain the final feature representation of the function layer.
[0090] The other steps and parameters are the same as those in Specific Embodiments 1 to 7.
[0091] It should be noted that if a function does not have a corresponding header function, the initial feature representation of the function will never be updated, that is, the initial feature representation is directly used as the feature representation after each iteration update.
[0092] Specific implementation method 9: This implementation method is different from specific implementation methods 1 to 8 in that the specific process of step A5 is as follows:
[0093]
[0094] The other steps and parameters are the same as those in Specific Embodiments 1 to 8.
[0095] Specific implementation method 10: This implementation method is different from the specific implementation methods 1 to 9 in that the feature representation of each function obtained by the function layer is processed by using GCN to obtain the processed feature representation. Then and Add, pass the addition result through a multi-layer perceptron, and use the output of the multi-layer perceptron as the final feature representation of the contract layer Specifically:
[0096]
[0097] The other steps and parameters are the same as those in Specific Embodiments 1 to 9.
[0098] Example
[0099] The present invention proposes a smart contract similarity detection method based on a hierarchical graph neural network. The method uses Octopus to process the bytecode of the smart contract, and the data obtained is shown in Table 1:
[0100] Table 1
[0101]
[0102]
[0103] Using the block nodes of the smart contract, the edges between block nodes, functions, function signatures corresponding to each function, edges between function signatures, and EVM instructions, static analysis is performed from the node layer, function layer, and contract layer. The specific process of static analysis is as follows:
[0104] At the node level
[0105] Use the BERT model to transform each block node B i Convert to a high-dimensional initial vector To represent the node semantic information; where i = 0, 1, ..., m, m + 1 is the total number of block nodes;
[0106] In order to extract the contract structure features at the block level, the initial vector After the MPNN network, the final feature representation of the output node layer The MPNN network consists of a message passing phase and an aggregation phase, where messages are passed along all edges in one time step;
[0107] The specific process is:
[0108] Step S1: And initialize the number of iterations l = 1;
[0109] Step S2: Calculate intermediate variables
[0110]
[0111] Among them, F msg is the message function, is the block node B after the l-1th iteration i′ The corresponding vector, is the block node B after the l-1th iteration i The corresponding vector, e ii′ It is the head block node B i With the tail block node B i′ The edge type between Yes ii′ The vector obtained by the BERT model, "||" represents connection, N B is the set of block nodes, and E is the set of edges;
[0112] Step S3: According to the intermediate variable Update the tail block node B i′ The corresponding vector is:
[0113]
[0114] Among them, α and β are weight coefficients;
[0115] Step S4: whether l=L is satisfied, where L is the maximum number of iterations;
[0116] If satisfied, execute step S5;
[0117] If not, set l=l+1 and return to step S2;
[0118] Step S5: Aggregate the vectors corresponding to each block node after the Lth iteration update through the average pooling layer to obtain the final feature representation of the node layer
[0119] The working process of the MPNN network at the node layer can be described as follows: In the initial stage of message transmission, block node B i and B i′ Initial characterization of and Initialized by the context encoding module of the block node. At time l, the message goes along each edge e ii′ From Node B i Flow to B i′ , and update B i′ Characterization of B i′ Yes ii′ The tail node of m. ii′ Through the head node B i , tail node B i′ and edge type e ii′ The connection is calculated and passed through the message function F msg After multiple rounds of message propagation, the tail node B of each edge i′ Through the function F update from Updated to Finally, after all rounds of message propagation, all updated nodes are aggregated to the node-level features through the average pooling layer
[0120] At the function level
[0121] Using Word2Vec to train the function F j Processing, generating function F jThe context vector of the function signature consists of a function identifier and a list of parameter types, following the calling rules. The function identifier is derived from the first 4 bytes of the Keccak-256 hash result of the function name and its corresponding parameter type in the source code. Therefore, the byte signature feature can be extracted from the runtime bytecode, and then Word2Vec can be used to identify the function F. j The corresponding function signature is processed to generate function F j The corresponding function signature feature vector According to the function F j The context vector and function signature feature vector of To calculate the function F j The initial feature representation To enhance the relevance of features;
[0122]
[0123] in, is the function F j The context vector of , j = 0, 1, ..., n, n + 1 is the total number of functions.
[0124] And use GCN to transform the function F j Internal block nodes {B1,B2,...B m′} to aggregate and obtain structured function features Then and Add them together and pass the result through a multi-layer perceptron (MLP) to get the function F j The updated initial feature representation
[0125]
[0126] Where m′ is the function F j The total number of internal block nodes, SET(F j ) is the function F j The internal block node collection, is the function F j The updated initial feature representation is projected through a multi-layer perceptron (MLP) to further enhance the extracted features.
[0127] In order to capture the structural features of the graph at the function level, the updated initial feature representation of each function is passed through the MPNN network, and the final feature representation of the function layer is output through the MPNN network. The specific process is:
[0128] Step A1: Initialize the number of iterations l = 1;
[0129] Step A2: Calculate intermediate variables
[0130]
[0131] Among them, F msg is the message function, is the function F obtained after the l-1th iteration j The characteristic representation of is the function F obtained after the l-1th iteration j′ The feature representation of z jj′ is the header function F j With the tail function F j′ The edge between Yes jj′ The vector obtained by the BERT model, N F is a set of functions, and Z is a set of edges between functions;
[0132] Step A3: Based on the intermediate variables Update function F j′ The characteristic representation of:
[0133]
[0134] Step A4: whether l=L is satisfied, where L is the maximum number of iterations;
[0135] If satisfied, proceed to step A5;
[0136] If not, set l=l+1 and return to step A2;
[0137] Step A5: Aggregate the function feature representation obtained after the Lth iteration through the average pooling layer to obtain the final feature representation of the function layer.
[0138] At the contract level
[0139] Word2Vec is used to process smart contracts to generate contract context vectors. Unlike traditional programming languages, when creating smart contracts, all contract storage variables will be persistently stored in the storage space, which is difficult to reflect through the contract vector. Since storage variables play a vital role in indicating contract similarity, the contract-level features are introduced by extracting storage variable features from the creation bytecode. Storage variables are recorded in the creation bytecode of the smart contract and can be identified by the EVM instruction SSTORE. That is, Word2Vec is used to process EVM instructions to generate contract-level instruction vectors. Then according to the contract context vector and Compute the initial feature representation of the contract
[0140]
[0141] in, is the contract context vector.
[0142] GCN is used to process the feature representations of each function obtained by the function layer to obtain the processed features Again and Perform weighted fusion and use the weighted fusion result as the final feature representation of the contract layer
[0143]
[0144] When updating its own feature representation at the contract layer, GCN and MLP can be used to capture the graph structure information at the function node level.
[0145] Similarity Detection
[0146] In the detection phase, for any two smart contracts to be tested for similarity, the features of each smart contract at each layer are extracted and the and Perform splicing and use the splicing result as the feature of the smart contract Similarly, get the characteristics of another smart contract
[0147] The similarity scores of the two smart contracts to be tested for similarity are calculated based on the extracted features. If the similarity scores of the two smart contracts are greater than a score threshold (the threshold can be set according to actual conditions), the two smart contracts are similar; otherwise, the two smart contracts are not similar.
[0148] Similarity score The calculation method is:
[0149]
[0150] in, and are the characteristics of the two smart contracts to be tested for similarity, Representation calculation and The inner product of , ||·|| represents the L2 norm, and “*” represents multiplication.
[0151] like Figure 5As shown, during the network training process, the present invention calculates the losses loss3, loss2 and loss1 respectively based on the vector information obtained at the three levels of nodes, functions and contracts, and then merges loss3, loss2 and loss1 to obtain the final loss value. The method for constructing the training data set is as follows: there are multiple compiler versions for smart contracts. For smart contracts, the construction of similar pairs needs to be compiled under multiple versions of compilers. For the construction of non-similar pairs of contracts, it is only necessary to group and pair different contracts. The neural network model learns the semantic representation and distance information between similar pairs and non-similar pairs, characterizes the semantic information of smart contracts through graph neural networks, and finally determines whether the two contracts are similar. In order to better capture the semantic logic of smart contracts, the present invention proposes a hierarchical framework for simultaneously learning the key features and patterns of contract binaries of different granularities, in which the initial representations of all multi-level nodes are converted into higher-level features through neural networks.
[0152] The above calculation examples of the present invention are only used to explain the calculation model and calculation process of the present invention in detail, and are not intended to limit the implementation methods of the present invention. For ordinary technicians in the relevant field, other different forms of changes or modifications can be made based on the above description. It is impossible to list all the implementation methods here. All obvious changes or modifications derived from the technical solution of the present invention are still within the scope of protection of the present invention.
Claims
1. A smart contract similarity detection method based on hierarchical graph neural network, characterized in that: The specific process of the method is: After processing the bytecode of the smart contract, we obtain the block nodes, edges between block nodes, functions, function signatures corresponding to each function, edges between function signatures, and EVM instructions of the smart contract. Then, we perform static analysis from the node layer, function layer, and contract layer. The specific process of static analysis is as follows: At the node level Use the BERT model to transform each block node B i Convert to an initial vector Where i=0,1,…,m, m+1 is the total number of block nodes; Then the initial vector After the MPNN network, the final feature representation of the output node layer At the function level Using Word2Vec to train the function F j Processing, generating function F j The context vector of Word2Vec is used to train the function F j The corresponding function signature is processed to generate function F j The corresponding function signature feature vector According to the function F j The context vector and function signature feature vector of To calculate the function F j The initial feature representation Where j = 0, 1, ..., n, n + 1 is the total number of functions; And use GCN to transform the function F j Internal block nodes {B1,B2,...B m′ } to aggregate and obtain structured function features Then and Add them together and pass the result through a multi-layer perceptron to get the function F j The updated initial feature representation The updated initial feature representation of each function is passed through the MPNN network, and the final feature representation of the function layer is output through the MPNN network At the contract level Use Word2Vec to process smart contracts to generate contract context vectors, and use Word2Vec to process EVM instructions to generate contract layer instruction vectors Then according to the contract context vector and Compute the initial feature representation of the contract GCN is used to process the feature representations of each function obtained by the function layer to obtain the processed features Then and Add, pass the addition result through a multi-layer perceptron, and use the output of the multi-layer perceptron as the final feature representation of the contract layer Will and Perform splicing and use the splicing result as the feature of the smart contract; For any two smart contracts to be tested for similarity, the features of each smart contract are extracted respectively, and the similarity scores of the two smart contracts to be tested for similarity are calculated based on the extracted features. If the similarity scores of the two smart contracts are greater than the score threshold, the two smart contracts are similar, otherwise the two smart contracts are not similar.
2. According to claim 1, a smart contract similarity detection method based on hierarchical graph neural network is characterized in that: Octopus is used to process the bytecode of the smart contract.
3. According to claim 2, a method for detecting similarity of smart contracts based on a hierarchical graph neural network is characterized in that: According to the function F j The context vector and function signature feature vector of To calculate the function F j The initial feature representation is: in, is the function F j The context vector of .
4. According to claim 3, a method for detecting similarity of smart contracts based on a hierarchical graph neural network is characterized in that: The initial vector After the MPNN network, the final feature representation of the output node layer Specifically: Step S1: And initialize the number of iterations l = 1; Step S2: Calculate intermediate variables Among them, F msg is the message function, is the block node B after the l-1th iteration i′ The corresponding vector, is the block node B after the l-1th iteration i The corresponding vector, e ii′ It is the head block node B i With the tail block node B i′ The edge type between Yes ii′ The vector obtained by the BERT model, "||" represents connection, N B is the set of block nodes, and E is the set of edges; Step S3: According to the intermediate variable Update the tail block node B i′ The corresponding vector is: Among them, α and β are weight coefficients; Step S4: whether l=L is satisfied, where L is the maximum number of iterations; If satisfied, execute step S5; If not, set l=l+1 and return to step S2; Step S5: Aggregate the vectors corresponding to the nodes of each block after the Lth iteration through the average pooling layer to obtain the final feature representation of the node layer.
5. According to claim 4, a method for detecting similarity of smart contracts based on hierarchical graph neural network is characterized in that: The contract context vector and Compute the initial feature representation of the contract Specifically: in, is the contract context vector.
6. According to claim 5, a method for detecting similarity of smart contracts based on hierarchical graph neural network is characterized in that: The specific process of step S5 is as follows:
7. According to claim 6, a method for detecting similarity of smart contracts based on hierarchical graph neural network is characterized in that: The GCN is used to transform the function F j Internal block nodes {B1,B2,...B m′ } to aggregate and obtain structured function features Then and Add them together and pass the result through a multi-layer perceptron to get the function F j The updated initial feature representation of ; Specifically: Where m′ is the function F j The total number of internal block nodes, SET(F j ) is the function F j The internal block node collection, is the function F j The updated initial feature representation of .
8. According to claim 7, a method for detecting similarity of smart contracts based on hierarchical graph neural network is characterized in that: The updated initial feature representation of each function is passed through the MPNN network, and the final feature representation of the function layer is output through the MPNN network Specifically: Step A1: Initialize the number of iterations l = 1; Step A2: Calculate intermediate variables Among them, F msg is the message function, is the function F obtained after the l-1th iteration j The characteristic representation of is the function F obtained after the l-1th iteration j′ The feature representation of z jj′ is the header function F j With the tail function F j′ The edge between Yes jj′ The vector obtained by the BERT model, N F is a set of functions, and Z is a set of edges between functions; Step A3: Based on the intermediate variables Update function F j′ The characteristic representation of: Step A4: whether l=L is satisfied, where L is the maximum number of iterations; If satisfied, proceed to step A5; If not, set l=l+1 and return to step A2; Step A5: Aggregate the function feature representation obtained after the Lth iteration through the average pooling layer to obtain the final feature representation of the function layer.
9. According to claim 8, a method for detecting similarity of smart contracts based on hierarchical graph neural network is characterized in that: The specific process of step A5 is as follows:
10. A method for detecting similarity of smart contracts based on hierarchical graph neural network according to claim 9, characterized in that: The GCN is used to process the feature representation of each function obtained by the function layer to obtain the processed feature Then and Add, pass the addition result through a multi-layer perceptron, and use the output of the multi-layer perceptron as the final feature representation of the contract layer Specifically:
Citation Information
Patent Citations
Intelligent contract user permission anomaly vulnerability detection method based on graph neural network
CN115525905A
Intelligent contract vulnerability detection method
CN116010975A
Intelligent contract vulnerability detection method and system, storage medium and equipment
CN116361816A
Intelligent contract similarity detection method based on cross-modal pre-training model
CN116881668A
Vulnerability detection method and device for smart contract, and storage medium
US12093399B1