Intelligent contract security analysis method and system based on contrast learning
By constructing a semantic equivalent contract converter and a contrastive learning method, a variety of equivalent semantic contract variants are generated, which solves the problem of low accuracy in smart contract detection in existing technologies and achieves more efficient contract security analysis.
Patent Information
- Application Number
- CN202511598442.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-04
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2045-11-04
AI Technical Summary
Existing technologies for smart contract security testing suffer from several problems, including difficulty in obtaining equivalent contracts from the real world and an inability to capture the fine-grained differences between contracts, resulting in low detection accuracy.
We employ a contrastive learning-based smart contract security analysis method. By constructing a semantic equivalent contract transformer for data augmentation, we generate diverse equivalent semantic contract variants. We then train a Transformer Encoder using contrastive learning cross-entropy loss and combine it with a multilayer perceptron (MLP) for contract security analysis.
It improves the robustness and generalization ability of smart contract detection, can capture fine-grained features between contracts, significantly improves the accuracy and identification ability of vulnerability detection, and reduces the occurrence of false positives and false negatives.
Smart Images

Figure CN121051763B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to deep learning and smart contract detection technology, specifically to a smart contract security analysis method and system based on comparative learning. Background Technology
[0002] The Transformer model is a self-attention-based architecture and one of the representative algorithms of deep learning. It utilizes self-attention, avoiding the sequential structure of RNNs, allowing for parallel training and the acquisition of global information. The Transformer consists of an Encoder and a Decoder. The Encoder transforms input data (such as words, characters, or other discrete units) into continuous vector representations, while the Decoder generates the model's output sequence. Due to its excellent performance and flexibility, the Transformer model has been widely applied in various fields of Natural Language Processing (NLP).
[0003] Contrastive learning is a key method in the field of self-supervised learning. In recent years, its superior performance on image, text, and cross-modal tasks has been remarkable. Its core objective is to enable the model to learn more robust feature representations by comparing similar and dissimilar samples. Contrastive learning, through data augmentation operations, can improve model performance, allowing it to achieve good classification results even when faced with new and unseen data. The supervised contrastive learning used in this invention is a branch of contrastive learning that utilizes labeled data to explicitly train the model to distinguish between similar and dissimilar instances. In supervised contrastive learning, the model is trained on pairs of data points and their labels, indicating whether the data points are similar or dissimilar. The goal is to learn a representation space where similar instances cluster closer together, while dissimilar instances are pushed apart.
[0004] Data augmentation in contrastive learning is a common technique in machine learning and deep learning. Its core purpose is to increase the size and diversity of the dataset by transforming existing data or generating new data, thereby improving the model's generalization ability and reducing the risk of overfitting. Data augmentation typically relies on artificially increasing the amount of data by generating new data points from existing data. This includes perturbing the data in different directions or using deep learning models to generate new data points in the latent space of the original data to artificially expand the dataset.
[0005] Several studies on smart contract security detection have been conducted. For example, Chinese patent application 2024111100878 discloses a smart contract vulnerability detection method and system based on constraint-guided fuzzing. This scheme sets the testing order and priority of functions in the fuzzer according to the function call sequence, enabling timely vulnerability detection. Chinese patent application 202411247118.4 discloses an intelligent detection method for reentrancy vulnerabilities based on cross-contract reentrancy semantic graphs. This scheme analyzes the smart contract program to obtain the semantic graph of each contract, analyzes the call relationship between the calling and called contracts to obtain a cross-contract semantic graph, then obtains a cross-contract reentrancy semantic graph based on the fallback mechanism that triggers reentrancy vulnerabilities, and finally vectorizes the cross-contract reentrancy semantic graph and inputs it into a sequence model to extract features for intelligent vulnerability detection. Chinese patent application 2020100885960 discloses a smart contract vulnerability detection method based on transfer learning. This method trains a model by inputting a Java sample set, reuses a pre-trained Java model for downstream tasks of detecting smart contract code, and fine-tunes it before using it for smart contract detection.
[0006] However, the aforementioned existing technologies still have some technical problems, such as the difficulty in obtaining equivalent contracts from the real world, the inability to capture the fine granularity between various contracts, and the resulting low detection accuracy. Summary of the Invention
[0007] Purpose of the invention: The purpose of this invention is to address the shortcomings of existing technologies and provide a smart contract security analysis method and system based on comparative learning.
[0008] Technical solution: The present invention provides a smart contract security analysis method based on contrastive learning, comprising the following steps:
[0009] Step 1: Analyze the semantic transformation operations and construct a semantic equivalent contract converter. The resulting semantic equivalent contract converter includes three types of semantic equivalent operations: renaming operations, equivalent swap operations, and dead code operations.
[0010] Step 2: Perform data augmentation on the semantic equivalent contract converter obtained in Step 1. Input the vulnerability contracts in the dataset into the semantic equivalent contract converter to obtain the semantic equivalent variants of the vulnerability contracts, i.e., the augmented vulnerability contracts.
[0011] Step 3: Construct sample pairs, that is, construct the vulnerable contract and its semantically equivalent variant (i.e., the enhanced vulnerable contract) as positive sample pairs P-P', and construct the non-vulnerable contract and the vulnerable contract as negative sample pairs PN;
[0012] Step 4: After word embedding operations are performed on the contract samples (including vulnerable contracts, non-vulnerable contracts, and enhanced vulnerable contracts), contrastive learning cross-entropy loss is used to increase the correlation between positive samples and reduce the correlation between positive and negative samples, thereby training and optimizing the Transformer Encoder to obtain a good representation of the contract samples.
[0013] Step 5: Input the good representation of the obtained contract sample into the structure classifier to generate the contract security analysis results. Here, the analysis results are divided into binary classification results of vulnerable and non-vulnerable. Each hidden layer is connected to the ReLU activation function. The structure classifier is implemented by a multilayer perceptron (MLP) and contains multiple hidden layers (Dense) and a softmax function.
[0014] This invention first performs data augmentation on the sample data and proposes a semantic equivalence transformation method, which solves the problem of the difficulty in obtaining an equivalent contract from the real world and improves robustness and generalization ability. The entire vulnerability detection module combines a contrastive learning method to capture fine-grained differences between contracts and improve the accuracy of vulnerability detection. At the same time, a hard negative sample mining strategy is used during the training process, which greatly improves the generalization ability of the model.
[0015] Furthermore, the specific details of the three types of semantic equivalence operations in step 1 are as follows:
[0016] Renaming operations include contract renaming, function renaming, and variable renaming; equivalence exchange operations include equivalent substitution of loop and conditional statements, equivalent substitution of call statements, and code compression and expansion; dead code operations include dead code insertion, dead code deletion, and the addition and deletion of comments. Semantic-preserving transformation operations are introduced to automatically generate semantically equivalent smart contracts. This enables the efficient synthesis of structurally diverse but semantically consistent contract variants without requiring extensive manual annotation of data, while ensuring that the generated smart contracts are functionally consistent with the original versions.
[0017] Furthermore, the specific steps of data augmentation in step 2 are as follows:
[0018] Step 2.1: Initialize the storage-enhanced vulnerability contract set POS for semantically equivalent contracts;
[0019] Step 2.2: Create an equivalent contract converter and initialize the conversion function. ... And the probability corresponding to each transformation function is initialized. ... And the number of variants, n, to be generated;
[0020] in, For any semantically equivalent operation, the transformation function is... Then it is the corresponding probability. ;
[0021] Step 2.3: For each transformation function According to probability Sample a binomial random variable ,if =1, then perform the conversion function. Convert the contract;
[0022] Step 2.4: When applying the transformation function, the abstract syntax tree and source code are transformed according to the function transformation requirements;
[0023] Repeat steps 2.1 to 2.4 above until all transformation functions have been traversed and the number of variants generated is n;
[0024] Finally, the output is a set of POS containing all semantically equivalent variants, including all enhanced exploit contracts.
[0025] Furthermore, the specific process of constructing samples in step 3 is as follows:
[0026] First, for negative sample pairs PN, to enhance the diversity of negative sample pairs, it is represented as... , Here, 'k' refers to a non-vulnerable contract, and 'k' refers to a vulnerable contract. A hard negative mining strategy is used to select the non-vulnerable contract 'k'. The method is as follows:
[0027] A dynamically updated queue of non-vulnerable contract negative samples, `neg`, with a fixed size of N, is defined. In each training batch, a subset of non-vulnerable contract samples are randomly selected from the dataset and added to this queue, while the earliest added samples are removed to maintain the queue size. Then, the similarity between all samples in queue `neg` and the current vulnerable contract is calculated. Negative samples with smaller differences are selected to construct difficult negative sample pairs, avoiding a large number of simple negative samples dominating the gradient. The formula for calculating the similarity is as follows:
[0028] ;
[0029] ;
[0030] in, The feature representation obtained from the Transformer encoder, Vulnerable contracts in the negative sample queue;
[0031] Then, for the positive sample pair P-P', it is represented as , This refers to the semantic equivalent contract of the vulnerability contract, P' ( This is an enhanced vulnerability contract;
[0032] Next, the vulnerable contract, the non-vulnerable contract, and the enhanced vulnerable contract are input together into the embed layer of the contrastive learning feature extraction module, and the parameters are fine-tuned based on the training results, wherein the Encoder parameters of the three samples are consistent; the specific structure of the contrastive learning feature extraction module is as follows:
[0033] Using a transformer as the base model, the first step is the word embedding layer, which combines the multilayer perceptron (MHA) and pool layers with the base model MLP.
[0034] Further, in step 4, the contract sample is input into the contrastive learning feature extraction module for feature extraction. After passing through the word embedding layer and the transformer layer, the contract sample yields a feature vector. The feature vector is used to calculate the distance between the contract sample and the anchor sample and the positive and negative samples, and the module is trained using a loss function. The specific method of the contrastive learning feature extraction module is as follows:
[0035] Step 4.1: Perform word embedding operations on the samples;
[0036] Perform vocabulary generation operations, using EncodeAsIds to map each token in the sample to a unique integer ID, and generate a vector. T is the sequence length, for vectors Perform position encoding and output vector , It is expressed as follows:
[0037] ;
[0038] in, It is the input sample vector. These are the embedding layer weights. It refers to the model dimension, PE(t).
[0039] It is a positional encoding;
[0040] Step 4.2: Input the previous layer position encoding vector into the transformer encoder for encoding, and obtain the feature vector F, as follows:
[0041] ReLU( + ;
[0042] in, It is the output of the previous layer, with an initial value of , , , , These are the learnable parameters of the transformer layer;
[0043] The formula for calculating the Multi-Head Attention (MHA) mechanism is as follows:
[0044] ;
[0045] ;
[0046] in , , It is the self-attention weight matrix;
[0047] Step 4.3: Calculate the similarity between the positive and negative sample pairs and the anchor sample. The similarity is defined as follows:
[0048] ;
[0049] ;
[0050] In the above formula, s It is the anchor sample feature vector. It is a positive sample feature vector. It is the negative sample feature vector, where U represents the number of samples and C represents the vector dimension;
[0051] Step 4.4: Using the loss function Update the transformer layer parameters during model training. The loss function is as follows:
[0052] .
[0053] Furthermore, step 5 uses a Multilayer Perceptron (MLP) as the structural classifier. This three-layer MLP classifier includes three fully connected layers and a nonlinear activation function to enhance the model's expressive power. The specific expression of the three-layer MLP classifier is as follows:
[0054] ;
[0055] The softmax function will output the final binary classification result z:
[0056] ;
[0057] in, For feature vectors, It is the weight matrix of the classifier. It is a bias term. It is an activation function used to transform the output into a probability distribution. It is the pre-activation value of the hidden layer. Output layer score;
[0058] Through backpropagation during the training phase, the model continuously adjusts its parameters to minimize the difference between the predicted values and the true labels, thereby improving classification performance. The loss function is as follows:
[0059] ;
[0060] in It is the first The probability that a sample is predicted to be of the positive class. It is the first The true label of each sample.
[0061] The present invention also discloses a system for implementing the above-mentioned smart contract security analysis method based on contrastive learning, including a data augmentation module, a contrastive learning feature extraction module, and a vulnerability classification module;
[0062] The data augmentation module is used to simulate the situation where the same contract function in reality has multiple forms due to syntax changes and other reasons, and is used as positive sample pairs for feature learning in the future.
[0063] The contrastive learning feature extraction module performs similarity enhancement on positive sample pairs composed of vulnerability functions and variant functions, and similarity reduction on negative sample pairs, to obtain a better contract representation.
[0064] The vulnerability classification module extracts the features from the contrastive learning feature extraction module and uses an MLP model to train and analyze the target samples, thereby achieving binary classification of vulnerability functions and non-vulnerability functions.
[0065] Beneficial effects: This invention augments smart contracts through equivalent semantic contract transformation operations, generating diverse variants of equivalent semantic contracts. Then, by constructing sample pairs and combining them with contrastive learning methods, an encoder is trained to obtain good feature representations. Compared with existing technologies, this invention has the following advantages:
[0066] 1. This invention constructs a semantic equivalence converter to perform three types of semantic equivalence operations on contracts. By learning the similarity between the constructed positive and negative sample pairs, the encoder is continuously optimized. Finally, the obtained features are used to generate security analysis results of smart contracts through MLP.
[0067] 2. This invention can solve the problems of high cost of obtaining semantically equivalent contracts, difficulty in verification, and uncertainty in real-world smart contract scenarios.
[0068] 3. This invention can capture fine-grained features between semantically equivalent smart contracts through a contrastive learning mechanism, thereby generating vector representations with consistent semantic representations. This breaks through the limitations of traditional analysis that relies on the surface structure of code, and effectively improves the model's ability to identify semantically equivalent contracts with significant differences in syntax structure.
[0069] 4. This invention maintains high accuracy in judging diverse code structures through its semantic awareness mechanism, significantly reducing false positives and false negatives caused by differences in feature representations. It has good robustness in identifying malicious contracts and is suitable for smart contract security analysis scenarios such as automatic vulnerability detection and security analysis. Attached Figure Description
[0070] Figure 1 This is a schematic diagram of the experimental method and model structure of the present invention;
[0071] Figure 2 This is a schematic diagram of the contrastive learning feature extraction module in the embodiment;
[0072] Figure 3 This is a schematic diagram of the MLP classifier in the embodiment. Detailed Implementation
[0073] The technical solution of the present invention will be described in detail below, but the scope of protection of the present invention is not limited to the embodiments described.
[0074] like Figure 1 As shown, the present invention provides a smart contract security analysis method based on contrastive learning, comprising the following steps:
[0075] Step 1: Analyze the semantic transformation operations and construct a semantic equivalent contract converter. The resulting semantic equivalent contract converter includes three types of semantic equivalent operations: renaming operations, equivalent swap operations, and dead code operations.
[0076] Step 2: Perform data augmentation on the semantic equivalent contract converter obtained in Step 1. Input the vulnerable contracts in the dataset into the semantic equivalent contract converter to obtain semantic equivalent variants of the vulnerable contracts.
[0077] Step 3: Construct sample pairs, that is, construct a positive sample pair P-P' between the vulnerable contract and its semantically equivalent variant, i.e., the enhanced vulnerable contract, and construct a negative sample pair PN between the non-vulnerable contract and the vulnerable contract;
[0078] Step 4: After word embedding of the contract samples, contrastive learning cross-entropy loss is used to increase the correlation between positive samples and reduce the correlation between positive and negative samples, thereby training and optimizing the Transformer Encoder to obtain a good representation of the contract samples.
[0079] Step 5: Input the good representation of the obtained contract sample into the structure classifier to generate the contract security analysis results. Here, the analysis results are divided into binary classification results of vulnerable and non-vulnerable, and each hidden layer is connected to the ReLU activation function.
[0080] The structure classifier is implemented using a multilayer perceptron (MLP), which contains multiple hidden layers (Dense) and a softmax function.
[0081] This invention can capture fine-grained features between semantically equivalent smart contracts through a contrastive learning mechanism, thereby generating vector representations with consistent semantic representations. This overcomes the limitations of traditional analysis that relies on the surface structure of code, effectively improving the model's ability to identify semantically equivalent contracts with significant differences in syntactic structure. Through this semantic awareness mechanism, the system can maintain high-precision judgment when faced with diverse code structures, significantly reducing false positives and false negatives caused by differences in feature representations. It has good robustness in identifying malicious contracts and is suitable for smart contract security analysis scenarios such as automatic vulnerability detection and security analysis.
[0082] The specific details of the three types of semantic equivalence operations in step 1 of this embodiment are as follows:
[0083] Renaming operations include contract renaming, function renaming, and variable renaming; equivalent substitution operations include equivalent substitution of loop statements and conditional statements, equivalent substitution of call statements, and code compression and expansion; dead code operations include dead code insertion, dead code deletion, and the addition and deletion of comments, as shown in Table 1.
[0084] Table 1 Summary of Semantic Equivalence Operations
[0085] Renaming Operation Equivalence Transformation Dead Code Operations Contract renaming Equivalent substitution of loop statements and conditional statements Dead code insertion Function renaming Equivalent replacement of call statement Dead code removal Variable renaming Code compression and expansion Adding or deleting comments
[0086] In the real world, obtaining semantically equivalent smart contracts is both costly and time-consuming, and the equivalence verification process is subject to significant uncertainty. To address this challenge, this invention introduces a set of semantically-preserving transformation operations to automatically generate semantically equivalent smart contracts. The code snippets generated by the operations in the table exhibit syntactic diversity but maintain semantic consistency. In this way, this invention can efficiently synthesize structurally diverse but semantically consistent contract variants without requiring extensive manual annotation of data, while ensuring that the generated smart contracts are functionally consistent with the original versions.
[0087] The specific steps for data augmentation in step 2 of this embodiment are as follows:
[0088] Step 2.1: Initialize the POS set for semantic equivalence contracts;
[0089] Step 2.2: Create an equivalent contract converter and initialize the conversion function. ... And the probability corresponding to each transformation function is initialized. ... And the number of variants, n, to be generated;
[0090] in, For any semantically equivalent operation, the transformation function is... Then it is the corresponding probability. ;
[0091] Step 2.3: For each transformation function According to probability Sample a binomial random variable ,if =1, then perform the conversion function. Convert the contract;
[0092] Step 2.4: When applying the transformation function, the abstract syntax tree and source code are transformed according to the function transformation requirements;
[0093] Repeat steps 2.1 to 2.4 above until all transformation functions have been traversed and the number of variants generated is n;
[0094] Finally, the output is a set POS containing all variants (semantic equivalent variants).
[0095] The specific process of constructing the sample in step 3 of this embodiment is as follows:
[0096] First, for the negative sample pair PN, it is represented as , "k" refers to a non-vulnerable contract, and "k" refers to a vulnerable contract. Here, a hard negative mining strategy is used to select negative samples "k". The method is as follows:
[0097] A dynamically updated queue of non-vulnerable contract negative samples, `neg`, with a fixed size of N, is defined. In each training batch, a subset of non-vulnerable contract samples are randomly selected from the dataset and added to this queue, while the earliest added samples are removed to maintain the queue size. Then, the similarity between all samples in queue `neg` and the current vulnerable contract is calculated. Negative samples with smaller differences are selected to construct difficult negative sample pairs, avoiding a large number of simple negative samples dominating the gradient. The formula for calculating the similarity is as follows:
[0098] ;
[0099] ;
[0100] in, The feature representation obtained from the Transformer encoder, represents the vulnerable contracts in the negative sample queue, and k represents the non-vulnerable contracts;
[0101] Then, for the positive sample pair P-P', it is represented as , P' refers to the semantic equivalent contract of the vulnerability contract, where P' is the enhanced vulnerability contract.
[0102] Next, the sample, positive sample, and negative sample are input together into the embed layer, and the parameters are fine-tuned based on the model's training results, wherein the encoder parameters of the three samples are consistent; the specific structure of the feature model is as follows:
[0103] Using a transformer as the base model, the first step is the word embedding layer, which combines the multilayer perceptron (MHA) and pool layers with the base model MLP.
[0104] like Figure 2 As shown, step 4 inputs the contract sample into the contrastive learning feature extraction module for feature extraction. After passing through the word embedding layer and transformer layer, the contract sample yields a feature vector. The feature vector is used to calculate the distance between the contract sample and the anchor sample and the positive and negative samples, and the module is trained using a loss function. The specific method of the contrastive learning feature extraction module is as follows:
[0105] Step 4.1: Perform word embedding operations on the samples;
[0106] Perform vocabulary generation operations, using EncodeAsIds to map each token in the sample to a unique integer ID, and generate a vector. T is the sequence length, for vectors Perform position encoding and output vector , It is expressed as follows:
[0107] ;
[0108] in, It is the input sample vector. These are the embedding layer weights. It refers to the model dimension, PE(t).
[0109] It is a positional encoding;
[0110] Step 4.2: Input the vector into the transformer encoder for encoding to obtain the feature vector F, as shown below:
[0111] ReLU( + ;
[0112] in, It is the output of the previous layer, with an initial value of , , , , These are the learnable parameters of the transformer layer; the formula for calculating the multi-head attention mechanism (MHA) is:
[0113] ;
[0114] ;
[0115] in , , It is the self-attention weight matrix;
[0116] Step 4.3: Calculate the similarity between positive and negative sample pairs. The similarity is defined as follows:
[0117] ;
[0118] ;
[0119] In the above formula, s It is the anchor sample feature vector. It is a positive sample feature vector. It is the negative sample feature vector, where U represents the number of samples and C represents the vector dimension;
[0120] Step 4.4: Using the loss function Update the transformer layer parameters during model training. The loss function is as follows:
[0121] .
[0122] like Figure 3 Step 5, as shown, uses a Multilayer Perceptron (MLP) as the structural classifier. This three-layer MLP classifier includes three fully connected layers and a non-linear activation function to enhance the model's expressive power. The specific expression of the three-layer MLP classifier is as follows:
[0123] ;
[0124] The softmax function will output the final binary classification result z:
[0125] ;
[0126] in, For feature vectors, It is the weight matrix of the classifier. It is a bias term. It is an activation function used to transform the output into a probability distribution. It is the pre-activation value of the hidden layer. Output layer score;
[0127] Through backpropagation during the training phase, the model continuously adjusts its parameters to minimize the difference between the predicted values and the true labels, thereby improving classification performance. The loss function is as follows:
[0128] ;
[0129] in It is the first The probability that a sample is predicted to be of the positive class. It is the first The true label of each sample.
[0130] The present invention also discloses a system for implementing the above-mentioned smart contract security analysis method based on contrastive learning, including a data augmentation module, a contrastive learning feature extraction module, and a vulnerability classification module;
[0131] The data augmentation module is used to simulate the situation where the same contract function in reality has multiple forms due to syntax changes and other reasons, and is used as positive sample pairs for feature learning in the future.
[0132] The contrastive learning feature extraction module performs similarity enhancement on positive sample pairs composed of vulnerability functions and variant functions, and similarity reduction on negative sample pairs, to obtain a better contract representation.
[0133] The vulnerability classification module extracts the features from the contrastive learning feature extraction module and uses an MLP model to train and analyze the target samples, thereby achieving binary classification of vulnerability functions and non-vulnerability functions.
[0134] To verify the performance of the technical solution of this invention, this embodiment was tested and compared with the ten most advanced vulnerability detection schemes under the same dataset. The experimental results are shown in Table 2.
[0135] This invention is significantly superior to existing methods. More specifically, the detection model proposed in this invention achieves an accuracy of 86.46%, which is 4.11% higher than the current best method. In terms of accuracy and recall, it is 2.23% and 8.12% higher than the highest values of existing technologies, respectively. Furthermore, the F1-score of the model in this invention is 6.24% higher than the highest value of existing technologies.
[0136] Table 2. Performance data comparison between the present invention and prior art.
[0137] Methods Accuracy (%) Accuracy (%) Recall rate (%) F1(%) Securify1 17.71 14.20 16.03 15.06 Securify2 27.66 22.71 25.60 24.07 Sailfish 26.93 20.24 24.57 22.20 Smartian 42.06 24.31 38.06 29.67 Mythril 44.28 34.60 40.80 37.45 Peculiar 85.20 79.34 72.38 75.70 LSTM 83.65 78.03 66.96 72.07 BiLSTM 84.12 79.44 67.39 72.92 GraBit 85.78 79.72 74.66 77.11 ReVulDL 86.44 82.35 71.69 76.65 This invention 88.67 86.46 79.81 82.89
[0138] In summary, this invention captures fine-grained relationships between semantically equivalent contracts through contrastive learning, reducing false positives and false negatives caused by over-reliance on semantics. Furthermore, this invention innovatively proposes a semantically equivalent contract converter, addressing the high cost and time consumption of obtaining semantically equivalent smart contracts in the real world, as well as the significant uncertainty in equivalence verification. This invention not only effectively identifies known vulnerabilities but also discovers potential unknown vulnerability patterns through a contrastive learning mechanism, thereby improving the comprehensiveness and accuracy of detection. In addition, this method considers the complex interaction scenarios of smart contracts in practical applications, better adapting to the vulnerability detection needs in multi-contract environments.
Claims
1. A smart contract security analysis method based on contrastive learning, characterized in that, Includes the following steps: Step 1: Analyze the semantic transformation operations and construct a semantic equivalent contract converter. The resulting semantic equivalent contract converter includes three types of semantic equivalent operations: renaming operations, equivalent swap operations, and dead code operations. Step 2: Perform data augmentation on the semantic equivalent contract converter obtained in Step 1. Input the vulnerable contracts in the dataset into the semantic equivalent contract converter to obtain semantic equivalent variants of the vulnerable contracts, i.e., the augmented vulnerable contracts. The specific steps of data augmentation are as follows: Step 2.1: Initialize the storage-enhanced vulnerability contract set POS for semantically equivalent contracts; Step 2.2: Create an equivalent contract converter and initialize the conversion function. ... And the probability corresponding to each transformation function is initialized. ... And the number of variants, n, to be generated; in, For any semantically equivalent operation, the transformation function is... Then it is the corresponding probability. ; Step 2.3: For each transformation function According to probability Sample a binomial random variable ,if =1, then perform the conversion function. Transform smart contracts; Step 2.4: When applying the transformation function, perform the transformation of the abstract syntax tree and source code according to the function transformation; Repeat steps 2.1 to 2.4 above until all transformation functions have been traversed and the number of variants generated is n; Finally, the output is a set of POS containing all semantically equivalent variants, including all enhanced exploit contracts. Step 3: Construct sample pairs, that is, construct a positive sample pair P-P' between the vulnerable contract and the enhanced vulnerable contract, and construct a negative sample pair PN between the non-vulnerable contract and the vulnerable contract; Step 4: After word embedding of the contract samples, contrastive learning cross-entropy loss is used to increase the correlation between positive samples and reduce the correlation between positive and negative samples, thereby training and optimizing the Transformer model encoder to obtain a good representation of the contract samples. Step 5: Input the good representation of the obtained contract sample into the structure classifier to generate contract security analysis results. The analysis results here include two classification results: contracts with vulnerabilities and contracts without vulnerabilities. Each hidden layer is connected to the ReLU activation function. The structure classifier is implemented by a multilayer perceptron (MLP).
2. The smart contract security analysis method based on contrastive learning according to claim 1, characterized in that, The specific details of the three types of semantic equivalence operations in step 1 are as follows: Renaming operations include contract renaming, function renaming, and variable renaming; equivalence exchange operations include equivalent substitution of loop statements and conditional statements, equivalent substitution of call statements, and code compression and expansion; dead code operations include dead code insertion, dead code deletion, and the addition and deletion of comments.
3. The smart contract security analysis method based on contrastive learning according to claim 1, characterized in that, The specific process of constructing samples in step 3 is as follows: First, for the negative sample pair PN, it is represented as 'k' refers to the vulnerability contract. This refers to non-vulnerable contracts. A hard negative sample mining strategy is used here to select non-vulnerable contracts. The method is as follows: A dynamically updated queue of non-vulnerable contract negative samples, `neg`, is defined with a fixed size of N. In each training batch, a subset of non-vulnerable contract samples are randomly selected from the dataset and added to this queue, while the earliest added samples are removed to maintain the queue size. Then, the similarity between all samples in queue `neg` and the current vulnerable contract is calculated. Negative samples with smaller differences are selected to construct hard negative sample pairs. The formula for calculating the similarity is as follows: ; ; in, The feature representation obtained from the Transformer encoder, Vulnerable contracts in the negative sample queue; Then, for the positive sample pair P-P', it is represented as , To enhance the post-vulnerability contract; Next, the vulnerable contract, the non-vulnerable contract, and the enhanced vulnerable contract are fed into the embed layer, and the parameters are fine-tuned based on the training results, with the Encoder parameters of the three samples being consistent.
4. The smart contract security analysis method based on contrastive learning according to claim 1, characterized in that, Step 4 involves inputting the contract sample into the contrastive learning feature extraction module for feature extraction. This involves passing the contract sample through a word embedding layer and a transformer encoder to obtain a feature vector. The feature vector is then used to calculate the distance between the contract sample and the anchor sample and the positive and negative samples, and the loss function is used for training. The specific method of the contrastive learning feature extraction module is as follows: Step 4.1: Perform word embedding operations on the contract sample; Perform vocabulary generation operations, using EncodeAsIds to map each token in the sample to a unique integer ID, and generate a vector. T is the sequence length, for vectors Perform position encoding and output vector , It is expressed as follows: ; in, It is the input sample vector. These are the embedding layer weights. It refers to the model dimension, PE(t). It is a positional encoding; Step 4.2: Input the previous layer position encoding vector into the transformer encoder for encoding, and obtain the feature vector F, as follows: ReLU( + ; in, It is the output of the previous layer, with an initial value of , , , , These are the learnable parameters of the transformer layer; The formula for calculating the Multi-Head Attention (MHA) mechanism is as follows: ; ; in , , It is the self-attention weight matrix; Step 4.3: Calculate the similarity between the positive and negative sample pairs and the anchor sample. The similarity is defined as follows: ; ; In the above formula, s It is the anchor sample feature vector. It is a positive sample feature vector. It is the negative sample feature vector, where U represents the number of samples and C represents the vector dimension; Step 4.4: Using the loss function Update the transformer layer parameters during model training. The loss function is as follows: 。 5. The smart contract security analysis method based on contrastive learning according to claim 1, characterized in that, Step 5 uses a Multilayer Perceptron (MLP) as the structural classifier. This three-layer MLP classifier includes three fully connected layers and a non-linear activation function to enhance the model's expressive power. The specific expression of the three-layer MLP classifier is as follows: ; The softmax function will output the final binary classification result z: ; in, For feature vectors, It is the weight matrix of the classifier. It is a bias term. It is an activation function used to transform the output into a probability distribution. It is the pre-activation value of the hidden layer. Output layer score; Through backpropagation during the training phase, the model continuously adjusts its parameters to minimize the difference between the predicted values and the true labels, thereby improving classification performance. The loss function... as follows: ; in It is the first The probability that a sample is predicted to be of the positive class. It is the first The true label of each sample.
6. A system for implementing the smart contract security analysis method based on contrastive learning as described in any one of claims 1 to 5, characterized in that, It includes a data augmentation module, a contrastive learning feature extraction module, and a vulnerability classification module; The data augmentation module simulates the multiple variations of the same contract function in reality due to syntax changes, and is used as positive sample pairs for feature learning in the future. The contrastive learning feature extraction module performs similarity enhancement on positive sample pairs composed of vulnerability functions and variant functions, and performs similarity reduction on negative sample pairs. The vulnerability classification module extracts the features from the contrastive learning feature extraction module and uses an MLP model to train and analyze the target samples, thereby achieving binary classification of vulnerability functions and non-vulnerability functions.
Citation Information
Patent Citations
Intelligent re-entry vulnerability detection method based on cross-contract re-entry semantic graph
CN119180036A
Intelligent contract vulnerability detection method based on code graph fusion analysis and graph matching network
CN119885196A
Vulnerability detection method and system based on semantic sensitive contrast learning and graph representation
CN120541852A