Bytecode function retrieval method, device and computer equipment for an ethereum virtual machine

By processing the Ethereum Virtual Machine's bytecode library and combining a pre-trained bytecode retriever with a specific retriever, similarity is calculated to retrieve target bytecode functions. This solves the problem of low retrieval accuracy in existing technologies and achieves efficient bytecode function localization.

CN121350320BActive Publication Date: 2026-03-17HANGZHOU HIGH-TECH ZONE (BINJIANG) INSTITUTE OF BLOCKCHAIN & DATA SECURITY
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-12-18
Publication Date
2026-03-17

AI Technical Summary

Technical Problem

In existing technologies, the accuracy of bytecode function retrieval in the Ethereum Virtual Machine is low, making it difficult to quickly locate specific bytecode functions in large-scale smart contracts.

Method used

By processing the Ethereum Virtual Machine's bytecode library, a pre-trained bytecode retrieval tool is used to calculate the first similarity between the natural language query content and the bytecode function. This is combined with a large model to generate code language functions. A second similarity is then calculated using a fine-tuned, specific retrieval tool. Finally, the target bytecode function is retrieved based on the similarity scores.

Benefits of technology

It improves the accuracy and recall of bytecode function retrieval, enabling fast and accurate location of target bytecode functions in the Ethereum Virtual Machine.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121350320B_ABST
    Figure CN121350320B_ABST
Patent Text Reader

Abstract

This application relates to a method, apparatus, and computer device for retrieving bytecode functions in the Ethereum Virtual Machine (EVM). The method includes processing all bytecode in the EVM's bytecode library to obtain all bytecode functions; calculating a first similarity between a natural language query and all bytecode functions based on a pre-trained bytecode retrieval tool; inputting the natural language query into a pre-defined large model to generate a code language function corresponding to a smart contract function; calculating a second similarity between the code language function and all bytecode functions based on a pre-trained specific retrieval tool; and retrieving the target bytecode function corresponding to the natural language query based on the first and second similarities. This application solves the problem of low retrieval accuracy in related technologies by retrieving accurate target bytecode functions based on the first and second similarities, thereby enhancing retrieval recall and accuracy.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of bytecode function retrieval technology, and in particular to bytecode function retrieval methods, apparatus and computer devices for the Ethereum Virtual Machine. Background Technology

[0002] The Ethereum Virtual Machine (EVM) is the core execution environment of the Ethereum network. It is a sandboxed, isolated, and deterministic virtual machine. The EVM's responsibility is to execute smart contract code. Bytecode functions are sequences of EVM opcodes formed by compiling high-level language functions; they are the smallest executable unit of the EVM.

[0003] In practical applications, some Ethereum smart contracts only have bytecode and no source code, so analysis can only be performed on bytecode functions. However, when smart contract projects are large-scale, the current approach is to use natural language descriptions to quickly locate and retrieve specific bytecode functions, followed by in-depth analysis. For example, users reviewing smart contract bytecode can use "functions with access control specificity" as a description to directly retrieve and locate the bytecode functions most likely to have access control specificity in the project. The drawback of this approach is that the direct retrieval method results in low accuracy.

[0004] There is currently no effective solution to the problem of low retrieval accuracy in related technologies. Summary of the Invention

[0005] This embodiment provides a method, apparatus, and computer device for retrieving bytecode functions of the Ethereum Virtual Machine to solve the problem of low retrieval accuracy in related technologies.

[0006] Firstly, this embodiment provides a method for retrieving bytecode functions of the Ethereum Virtual Machine, including:

[0007] Process all the bytecode in the Ethereum Virtual Machine's bytecode library to obtain all the bytecode functions;

[0008] Based on a pre-trained bytecode retrieval tool, the first similarity between the natural language query content to be retrieved and all the bytecode functions is calculated.

[0009] The natural language query content is input into a preset large model to generate the code language function of the corresponding smart contract function;

[0010] Based on a pre-trained specific retrieval tool, a second similarity is calculated between the code language function and all bytecode functions; the specific retrieval tool is obtained by fine-tuning the bytecode retrieval tool.

[0011] Based on the first similarity and the second similarity, the target bytecode function corresponding to the natural language query content is retrieved.

[0012] In some embodiments, based on the first similarity and the second similarity, a target bytecode function corresponding to the natural language query content is retrieved, including:

[0013] The first similarity and the second similarity corresponding to each bytecode function are calculated to obtain the third similarity corresponding to each bytecode function;

[0014] Based on the preset similarity evaluation criteria and the third similarity, the target bytecode function corresponding to the natural language query content is determined; the similarity evaluation criteria is the maximum and minimum similarity scores.

[0015] In some embodiments, the method further includes:

[0016] Based on the initial natural language query sub-retrieval unit and the initial bytecode query sub-retrieval unit, an initial bytecode retrieval unit and an initial specific retrieval unit are constructed respectively; the initial natural language query sub-retrieval unit and the initial bytecode query sub-retrieval unit have the same architecture;

[0017] Based on intra-batch comparative learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval are trained to obtain the pre-trained bytecode retrieval.

[0018] Based on intra-batch comparative learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial specific retrieval are trained to obtain the pre-trained specific retrieval.

[0019] In some embodiments, based on in-batch contrastive learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval are trained to obtain the pre-trained bytecode retrieval, including:

[0020] Based on the preset first training data and the preset contrast loss function, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval are trained by batch contrast learning to obtain the pre-trained bytecode retrieval.

[0021] The first training data includes natural language query samples, code query samples, and decompiled bytecode samples; the pre-trained bytecode retrieval device includes a first natural language query sub-retrieval device and a first bytecode query sub-retrieval device.

[0022] In some embodiments, based on intra-batch contrastive learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial specific retrieval are trained to obtain the pre-trained specific retrieval, including:

[0023] Based on the preset second training data and the preset contrast loss function, the initial natural language query sub-retrieval in the initial specific retrieval is subjected to meta-learning training to obtain the second natural language query sub-retrieval in the pre-trained meta-retrieval; the second training data includes natural language query samples and decompiled bytecode samples;

[0024] Based on the preset small sample data and the contrast loss function, the second natural language query sub-retrieval is trained by batch contrast learning to obtain the third natural language query sub-retrieval in the pre-trained specific retrieval.

[0025] In some embodiments, the method further includes:

[0026] The first bytecode query sub-retrieval is used as the bytecode query sub-retrieval in the pre-trained meta-retrieval;

[0027] The first bytecode query sub-retrieval is used as the bytecode query sub-retrieval in the pre-trained specific retrieval.

[0028] In some embodiments, both the initial natural language query sub-retrieval unit and the initial bytecode query sub-retrieval unit include two encoders with identical architectures.

[0029] In some embodiments, based on preset second training data and a preset contrastive loss function, the initial natural language query sub-retrieval in the initial specific retrieval device is subjected to meta-learning training to obtain a pre-trained second natural language query sub-retrieval in the meta-retrieval device, including:

[0030] The second training data is divided into multiple second sub-training data; each second sub-training data corresponds to a batch; and the second sub-training data is further divided into a first subset and a second subset.

[0031] A copy of the initial natural language query sub-retrieval is created based on the first subset of the dataset to obtain the copy sub-retrieval;

[0032] The parameter gradient of the updated replica sub-retrieval is obtained by training based on the second subset of the dataset and the replica sub-retrieval.

[0033] The initial natural language query sub-retrieval is trained based on the contrastive loss function and the parameter gradient to obtain the second natural language query sub-retrieval in the pre-trained meta-retrieval.

[0034] Secondly, this embodiment provides a bytecode function retrieval device for the Ethereum Virtual Machine, including: a processing module, a first calculation module, a generation module, a second calculation module, and a retrieval module;

[0035] The processing module is used to process all the bytecode in the Ethereum Virtual Machine's bytecode library to obtain all the bytecode functions;

[0036] The first calculation module is used to calculate the first similarity between the natural language query content to be retrieved and all the bytecode functions based on a pre-trained bytecode retrieval machine;

[0037] The generation module is used to input the natural language query content into a preset large model to generate the code language function of the corresponding smart contract function;

[0038] The second calculation module is used to calculate a second similarity between the code language function and all bytecode functions based on a pre-trained specific retrieval tool; the specific retrieval tool is obtained by fine-tuning the bytecode retrieval tool.

[0039] The retrieval module is used to retrieve the target bytecode function corresponding to the natural language query content based on the first similarity and the second similarity.

[0040] Thirdly, this embodiment provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements the bytecode function retrieval method for the Ethereum Virtual Machine described in the first aspect above.

[0041] Fourthly, this embodiment provides a storage medium storing a computer program that, when executed by a processor, implements the bytecode function retrieval method of the Ethereum Virtual Machine described in the first aspect above.

[0042] Compared with related technologies, the Ethereum Virtual Machine (EVM) bytecode function retrieval method, apparatus, and computer device provided in this embodiment obtain all bytecode functions by processing all bytecode in the EVM bytecode library; calculating the first similarity between the natural language query content to be retrieved and all bytecode functions based on a pre-trained bytecode retrieval tool; inputting the natural language query content into a preset large model to generate the corresponding smart contract function's code language function; calculating the second similarity between the code language function and all bytecode functions based on a pre-trained specific retrieval tool; the specific retrieval tool is obtained by fine-tuning the bytecode retrieval tool; and retrieving the target bytecode function corresponding to the natural language query content based on the first and second similarities. This solves the problem of low retrieval accuracy in related technologies by first processing the bytecode library to adapt to the EVM and completing the initial first similarity calculation with the pre-trained bytecode retrieval tool, then using a large model in conjunction with the pre-trained specific retrieval tool to fully understand the language information of the source code to obtain an enhanced second similarity; and finally, retrieving accurate target bytecode functions based on the first and second similarities, thereby enhancing the retrieval recall and accuracy.

[0043] Details of one or more embodiments of this application are set forth in the following drawings and description to make other features, objects and advantages of this application more readily apparent. Attached Figure Description

[0044] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:

[0045] Figure 1 This is a hardware structure block diagram of a terminal device for a bytecode function retrieval method for the Ethereum Virtual Machine provided in an embodiment of this application;

[0046] Figure 2 This is a flowchart of a bytecode function retrieval method for the Ethereum Virtual Machine provided in an embodiment of this application;

[0047] Figure 3 This is a flowchart of step S250;

[0048] Figure 4 This is a flowchart of the training process of a bytecode retriever and a specific retriever provided in an embodiment of this application;

[0049] Figure 5 This is a schematic flowchart illustrating the training process of a bytecode retriever and a specific retriever provided in an embodiment of this application;

[0050] Figure 6This is a flowchart of a preferred embodiment of the Ethereum Virtual Machine bytecode function retrieval method provided in this application;

[0051] Figure 7 This is a structural block diagram of a bytecode function retrieval device for the Ethereum Virtual Machine provided in one embodiment of this application.

[0052] In the diagram: 102, processor; 104, memory; 106, transmission device; 108, input / output device; 210, processing module; 220, first calculation module; 230, generation module; 240, second calculation module; 250, retrieval module. Detailed Implementation

[0053] To better understand the purpose, technical solution, and advantages of this application, the application is described and illustrated below in conjunction with the accompanying drawings and embodiments.

[0054] Unless otherwise defined, the technical or scientific terms used in this application shall have the general meaning understood by a person skilled in the art to which this application pertains. Words such as “a,” “an,” “an,” “the,” “the,” and “these” used in this application do not indicate quantitative limitation and may be singular or plural. The terms “comprising,” “including,” “having,” and any variations thereof used in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or device that comprises a series of steps or modules (units) is not limited to the listed steps or modules (units) but may include steps or modules (units) not listed, or may include other steps or modules (units) inherent to these processes, methods, products, or devices. Words such as “connected,” “linked,” and “coupled” used in this application are not limited to physical or mechanical connections but may include electrical connections, whether direct or indirect. “Multiple” used in this application refers to two or more. “And / or” describes the relationship between related objects, indicating that three relationships may exist; for example, “A and / or B” can represent: A alone, A and B simultaneously, or B alone. Normally, the character " / " indicates that the objects before and after it are in an "or" relationship. The terms "first," "second," "third," etc., used in this application are merely to distinguish similar objects and do not represent a specific order of objects.

[0055] The method embodiments provided in this example can be executed on a terminal, computer, or similar computing device. For example, it can run on a terminal. Figure 1 This is a hardware structure block diagram of the terminal for the Ethereum Virtual Machine bytecode function retrieval method in this embodiment. For example... Figure 1 As shown, a terminal may include one or more ( Figure 1Only one is shown in the diagram. A processor 102 and a memory 104 for storing data are also included. The processor 102 may be, but is not limited to, a microprocessor (MCU) or a programmable logic device (FPGA). The terminal may also include a transmission device 106 for communication functions and an input / output device 108. Those skilled in the art will understand that… Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the terminal described above. For example, the terminal may also include components that are larger than... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown are illustrated.

[0056] The memory 104 can be used to store computer programs, such as application software programs and modules, like the computer program corresponding to the bytecode function retrieval method of the Ethereum Virtual Machine in this embodiment. The processor 102 executes various functional applications and data processing by running the computer programs stored in the memory 104, thereby implementing the above-described method. The memory 104 may include high-speed random access memory and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.

[0057] The transmission device 106 is used to receive or send data via a network. This network includes a wireless network provided by the terminal's communication provider. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 can be a Radio Frequency (RF) module used for wireless communication with the Internet.

[0058] This embodiment provides a method for retrieving bytecode functions from the Ethereum Virtual Machine. Figure 2 This is a flowchart of the bytecode function retrieval method of the Ethereum Virtual Machine in this embodiment, as follows: Figure 2 As shown, the process includes the following steps:

[0059] Step S210: Process all bytecode in the Ethereum Virtual Machine's bytecode library to obtain all bytecode functions;

[0060] Step S220: Based on the pre-trained bytecode retrieval machine, calculate the first similarity between the natural language query content to be retrieved and all bytecode functions;

[0061] Step S230: Input the natural language query content into the preset large model to generate the code language function of the corresponding smart contract function;

[0062] Step S240: Based on a pre-trained specific retrieval tool, calculate the second similarity between the code language function and all bytecode functions; the specific retrieval tool is obtained by fine-tuning the bytecode retrieval tool.

[0063] Step S250: Based on the first similarity and the second similarity, retrieve the target bytecode function corresponding to the natural language query content.

[0064] Specifically, the Ethereum Virtual Machine (EVM) bytecode library is a set of instructions generated after smart contracts are compiled, consisting of executable opcodes and their operation data. By decompiling, extracting functions, or using neural network models, all the bytecode in the EVM bytecode library is processed to obtain the corresponding bytecode functions, allowing subsequent similarity calculations to be directly adapted to the EVM.

[0065] The bytecode retriever and the specific retriever have the same model network architecture, both including two structurally identical sub-retrievalrs, which can be trained using methods such as intra-batch contrastive learning and multi-task joint training. The specific retriever is obtained by fine-tuning the bytecode retriever. Fine-tuning refers to adjusting the first training data of the bytecode retriever according to a specific task in a specific scenario, or further adjusting it by combining meta-learning.

[0066] Specifically, the bytecode retrieval engine is trained using a pre-defined first training data and a pre-defined contrastive loss function. This enables the bytecode retrieval engine to retrieve natural language queries and accurately and comprehensively calculate the first similarity between the natural language query and all bytecode functions. The specific retrieval engine is trained by fine-tuning the training samples and methods based on the pre-trained bytecode retrieval engine. This allows the specific retrieval engine, combined with the larger model, to fully understand the linguistic information of the source code, resulting in an enhanced, specifically targeted second similarity. The larger model can be a deep learning model with a large number of parameters used for answering questions; there are no restrictions on this. Furthermore, the training samples for the specific retrieval engine can be directly matched to specific retrieval needs for fine-tuning; for example, if locating vulnerable code, samples for the vulnerability task are fine-tuned; if locating malicious code, samples for the malicious task are fine-tuned.

[0067] Finally, by combining the comprehensive first similarity score with a specific second similarity score, the target bytecode function corresponding to the natural language query content is accurately retrieved. Specifically, further operations (such as arithmetic operations, comparison operations, etc.) can be performed on the first and second similarities to obtain a similarity score, which is then used to filter out the bytecode functions that meet the requirements as the target bytecode functions corresponding to the natural language query content. The filtering requirements are related to the operations performed; for example, if the operation is summation, then the filtering requirement is the maximum similarity score, and there are no restrictions on this.

[0068] In related technologies, because some smart contracts on Ethereum only have bytecode and not source code, the analysis can only be performed on bytecode functions. However, when the scale of smart contract projects is very large, the approach of using natural language descriptions to quickly locate and retrieve specific bytecode functions before conducting in-depth analysis suffers from low retrieval accuracy. In this embodiment, all bytecode in the Ethereum Virtual Machine's bytecode library is processed to obtain all bytecode functions. Based on a pre-trained bytecode retrieval tool, a first similarity is calculated between the natural language query content to be retrieved and all bytecode functions. The natural language query content is input into a pre-set large model to generate the corresponding smart contract function's code language function. Based on a pre-trained specific retrieval tool, a second similarity is calculated between the code language function and all bytecode functions. The bytecode retrieval tool and the specific retrieval tool are trained separately based on intra-batch contrastive learning. The specific retrieval tool is obtained by fine-tuning the bytecode retrieval tool. Based on the first and second similarities, the target bytecode function corresponding to the natural language query content is retrieved. This solves the problem of low retrieval accuracy in related technologies. First, the bytecode library is processed to adapt to the Ethereum Virtual Machine, and a preliminary first similarity calculation is completed with the pre-trained bytecode retrieval tool. Then, a large model is used in conjunction with the pre-trained specific retrieval tool to fully understand the language information of the source code to obtain an enhanced second similarity. Finally, based on the first and second similarities, the accurate target bytecode function is retrieved, thereby improving the retrieval recall and accuracy.

[0069] The steps described above are explained in detail below:

[0070] In some of these embodiments, such as Figure 3 As shown, step S250, which retrieves the target bytecode function corresponding to the natural language query content based on the first similarity and the second similarity, includes the following steps:

[0071] Step S251: Calculate the first similarity and the second similarity corresponding to each bytecode function to obtain the third similarity corresponding to each bytecode function;

[0072] Step S252: Determine the target bytecode function corresponding to the natural language query content based on the preset similarity evaluation criteria and the third similarity; the similarity evaluation criteria is the maximum and minimum similarity scores.

[0073] In this embodiment, the first and second similarities corresponding to each bytecode function can be summed to obtain the third similarity corresponding to each bytecode function. If the similarity evaluation criterion is the maximum similarity score, the third similarities are sorted, and the third similarity with the highest similarity score (maximum similarity score) is selected from the sorting results. The bytecode function corresponding to this third similarity is then determined as the target bytecode function corresponding to the natural language query content. In other embodiments, the third similarities can also be compared to select the third similarity with the highest similarity score; this is not a limitation.

[0074] This embodiment enables rapid retrieval and reduces retrieval costs.

[0075] In some of these embodiments, such as Figure 4 As shown, the Ethereum Virtual Machine's bytecode function retrieval method also includes the following steps:

[0076] Step S410: Based on the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval, construct the initial bytecode retrieval and the initial specific retrieval respectively; the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval have the same architecture;

[0077] Step S420: Based on intra-batch contrastive learning, train the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval to obtain a pre-trained bytecode retrieval.

[0078] Step S430: Based on intra-batch contrastive learning, train the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial specific retrieval to obtain the pre-trained specific retrieval.

[0079] Specifically, the initial bytecode retriever and the initial specific retriever refer to the retrievers that have not been trained. After training, they become the pre-trained bytecode retriever and the pre-trained specific retriever, respectively.

[0080] The initial bytecode retriever and the initial specific retriever share the same architecture, each consisting of two encoders. For example, both the query encoder and the bytecode encoder can adopt the CodeT5 model architecture to improve adaptability. In other embodiments, the specific form of the encoder is not limited.

[0081] The training of the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval can be carried out by: constructing positive and negative sample pairs using only samples in the current training batch of training data, optimizing the model parameters of the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval by comparing the loss, and completing the training when the loss reaches a preset loss threshold or the training rounds reach a preset round threshold, thus obtaining the pre-trained bytecode retrieval.

[0082] The training of the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial specific retrieval is also carried out in a similar process to the training of the initial bytecode retrieval. The difference is that the training data is processed according to a specific task to obtain small sample data corresponding to the specific task.

[0083] This embodiment maximizes the use of in-batch samples to construct comparison tasks, thereby simplifying the training process and achieving efficient self-supervised training.

[0084] The following is combined with Figure 5 The training process will be explained.

[0085] In some embodiments, based on in-batch contrastive learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval are trained to obtain a pre-trained bytecode retrieval, including the following steps:

[0086] Based on the preset first training data and the preset contrastive loss function, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval are trained by batch contrastive learning to obtain the pre-trained bytecode retrieval.

[0087] The first training data includes natural language query samples, code query samples, and decompiled bytecode samples; the pre-trained bytecode retrieval system includes a first natural language query sub-retrieval system and a first bytecode query sub-retrieval system.

[0088] Specifically, the pre-trained bytecode retrieval engine can be considered a general-purpose retrieval engine, used for query localization in general scenarios. The first training data is a pre-set general training dataset for this bytecode retrieval engine, which includes natural language query samples, code query samples, and decompiled bytecode samples. The natural language query samples are the natural language of some queries; the code query samples are the code of some queries; and the decompiled bytecode samples are the bytecode after decompiling some bytecode functions.

[0089] The initial bytecode retrieval unit and the initial specific retrieval unit have the same architecture, each consisting of two encoders (e.g., a query encoder and a bytecode encoder; the query encoder encodes the input query (which can be natural language or code); the bytecode encoder encodes the decompiled bytecode). The training process will be explained using the CodeT5 model architecture as an example.

[0090] Initialize the Encoder parameters and architecture of the CodeT5 model, i.e., initialize the encoder; use the first vector generated by the encoder as the feature representation of the query and bytecode. After encoding, a normalized vector containing 768 floating-point numbers is generated, and the similarity between the query and bytecode is obtained by calculating the inner product of the two vectors.

[0091] Specifically, the formula for calculating similarity is as follows:

[0092] ;

[0093] in, The query vector is obtained by encoding the i-th query q (which can be natural language or code); The bytecode vector obtained by encoding the i-th bytecode b; To obtain the first query vector in the query vector results; To obtain the first bytecode vector in the bytecode vector result.

[0094] The first training data is divided into multiple batches (each batch contains a set of queries and a set of bytecode). Positive and negative sample pairs are constructed using samples within the current batch. The encoder is then optimized using a pre-defined contrastive loss function. Intra-batch contrastive learning training is completed when the training loss (Lnce) decreases below a pre-defined threshold for 10 consecutive epochs, or when the target number of training epochs is reached. After all encoders have completed training, the first natural language query sub-retrieval unit and the first bytecode query sub-retrieval unit are obtained, which constitute the pre-trained bytecode retrieval unit. Figure 5 The process of 'a'.

[0095] The contrastive loss function is expressed as:

[0096] ;

[0097] Where Q represents the set of queries within a batch, |Q| represents the size of a batch, and |B| represents the set of bytecode within the batch. To represent a positive sample, use Denotes negative sample cases, where This indicates that they are in the same batch.

[0098] In some embodiments, based on intra-batch contrastive learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial specific retrieval are trained to obtain a pre-trained specific retrieval, including the following steps:

[0099] Based on the preset second training data and the preset contrastive loss function, meta-learning training is performed on the initial natural language query sub-retrieval in the initial specific retrieval to obtain the second natural language query sub-retrieval in the pre-trained meta-retrieval; the second training data includes natural language query samples and bytecode samples;

[0100] Based on the preset small sample data and contrastive loss function, the second natural language query sub-retrieval is trained by in-batch contrastive learning to obtain the third natural language query sub-retrieval in the pre-trained specific retrieval.

[0101] Specifically, a pre-trained specific retrieval tool can be considered as a query localization tool used in a specific scenario, such as a vulnerability retrieval tool in a vulnerability query scenario. The training of a specific retrieval tool can be divided into two parts: the first part is the training of an initial natural language query sub-retrieval tool; the second part is the training of an initial bytecode query sub-retrieval tool.

[0102] For the first part: it has two phases (meta-learning training phase and fine-tuning phase).

[0103] The meta-learning training phase involves: pre-setting natural language query samples and decompiled bytecode samples; setting corresponding contrastive loss functions; and performing meta-learning training on the initial natural language query sub-retrieval in the initial specific retrieval system based on the second training data and the contrastive loss function Lnce, thereby obtaining the second natural language query sub-retrieval (meta-natural language query retrieval) in the pre-trained meta-retrieval system. Figure 5 The process of b-1. The specific process is as follows:

[0104] 1. Divide the second training data (natural language query samples and decompiled bytecode samples) D into k second sub-training data. Each sub-training set corresponds to a batch, and each sub-training set contains Where Q is a natural language query sample and B is the corresponding decompiled bytecode sample;

[0105] 2. Transfer the second sub-training data D i Divided into two subsets ;

[0106] 3. During training, the first subset of the dataset is used first. The parameters of the initial natural language query sub-retrieval are updated, but this update creates a copy of the sub-retrieval parameters, resulting in a copied sub-retrieval, instead of directly updating the initial natural language query sub-retrieval. Then, the second subset of the dataset is used. Train the replica sub-retrieval tool and record the parameter gradients that the replica sub-retrieval tool needs to update. The gradient of the updated parameters is... Figure 5 Local contrast loss in;

[0107] Here, θ represents the parameters of the initial natural language query sub-retrieval unit, which are normally trained as follows: The training here is ; ;Record ;

[0108] 4. Under the contrastive loss function Lnce, after a fixed number of training iterations, the gradient sum is updated based on the recorded parameters (i.e., ... The parameters of the initial natural language query sub-retrieval unit are updated to complete cloud learning and obtain the second natural language query sub-retrieval unit (meta-natural language query retrieval unit). After the update, the gradients of these records are no longer needed.

[0109] Through the above process, the second natural language query sub-retrieval unit (meta-natural language query retrieval unit) can be trained on multiple different tasks. The process learns a good parameter that allows the second natural language query sub-retrieval unit to quickly adapt to new tasks with few samples. Furthermore, the contrastive loss function used during training is Lnce, which is unaffected by the size of the dataset.

[0110] Since this second natural language query sub-retrieval tool (meta-natural language query retrieval tool) cannot be used directly, it requires further few-shot training on a smaller amount of sample data, i.e., a fine-tuning phase. Figure 5 b-2 in the middle.

[0111] The training dataset for the fine-tuning phase consists of small sample data; it could be a small sample of data for a specific task (such as vulnerability data) with only a hundred or so entries. The contrastive loss function can also be Lnce. Based on the small sample data and the contrastive loss function, the second natural language query sub-retrieval module is trained using in-batch contrastive learning to obtain the third natural language query sub-retrieval module from the pre-trained specific retrieval module. This training process is as follows:

[0112] The second natural language query sub-retrieval unit consists of two encoders trained through meta-learning. These encoders are initialized, and the first vector generated by each encoder is used as the feature representation of the query and bytecode. A normalized vector containing 768 floating-point numbers is generated after encoding. The similarity between the query and bytecode is obtained by calculating the inner product of the two vectors.

[0113] Specifically, the formula for calculating similarity is as follows:

[0114] ;

[0115] in, The query vector is obtained by encoding the i-th query q (which can be natural language or code); The bytecode vector obtained by encoding the i-th bytecode b; To obtain the first query vector in the query vector results; To obtain the first bytecode vector in the bytecode vector result.

[0116] The second training data is divided into multiple batches (each batch contains a set of queries and a set of bytecode). Positive and negative sample pairs are constructed using samples within the current batch, and the encoder is optimized using a contrastive loss function. Intra-batch contrastive learning training is completed when the training loss decreases less than a preset threshold for 10 consecutive epochs, or when the training epochs reach the target number of training epochs. After all encoders have completed training, a third natural language query sub-retrieval device is obtained from the specific retrieval device.

[0117] The contrastive loss function is expressed as:

[0118] ;

[0119] Where Q represents the set of queries within a batch, Indicates the size of a batch. Represents a set of bytecode within a batch, using To represent a positive sample, use Denotes negative sample cases, where This indicates that they are in the same batch.

[0120] This embodiment uses specific small sample data for further training, maximizing the use of in-batch samples to construct a comparison task, thereby simplifying the training process, achieving efficient self-supervised training, and improving the accuracy of a specific retrieval device.

[0121] The second part can be accomplished using the contrastive learning method described above; alternatively, it can be achieved by directly inheriting the first bytecode query sub-retrieval from the pre-trained bytecode retrieval system. Specifically, the first bytecode query sub-retrieval is used as the bytecode query sub-retrieval in the pre-trained meta-retrieval system; or the first bytecode query sub-retrieval is used as the bytecode query sub-retrieval in the pre-trained specific retrieval system.

[0122] This process directly saves time in training the bytecode query sub-retrieval tool, greatly speeds up the training of the bytecode retrieval tool, and maintains the accuracy of the retrieval.

[0123] In this embodiment, the meta-learning combined with intra-batch contrastive learning ensures that the sub-retrieval unit can still perform well in specific scenarios where only a very small number of samples are available for learning, thereby expanding the applicable scenarios.

[0124] The present embodiment will now be described and illustrated through preferred embodiments.

[0125] like Figure 6 As shown, in the wallet contract, the user needs to locate a certain transfer function through a query. The natural language query used by the user is "function to transfer deposits to other addresses".

[0126] Based on a pre-trained bytecode retrieval machine, calculate the first similarity between the function that migrates deposits to other addresses and all bytecode functions;

[0127] Input the function that migrates deposits to other addresses into the preset large model to generate the corresponding smart contract function (Solidity function) code language function;

[0128] Based on a pre-trained specific retrieval tool, the second similarity between code language functions and all bytecode functions is calculated;

[0129] The first and second similarity scores are added together to obtain the total score, which is then used as the third similarity score. The scores are then sorted, and if the highest score in the sort is migrateTo, migrateTo is returned as the target bytecode function corresponding to the function that migrates deposits to other addresses.

[0130] This preferred embodiment enables accurate retrieval of natural language query content within the Ethereum Virtual Machine, enhancing retrieval recall and accuracy.

[0131] It should be noted that the steps shown in the above process or in the flowchart of the accompanying figures can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.

[0132] This embodiment also provides a bytecode function retrieval device for the Ethereum Virtual Machine, which is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. The terms "module," "unit," "subunit," etc., used below can refer to combinations of software and / or hardware that implement a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.

[0133] Figure 7 This is a structural block diagram of the bytecode function retrieval device of the Ethereum Virtual Machine in this embodiment, as shown below. Figure 7 As shown, the device includes: a processing module 210, a first calculation module 220, a generation module 230, a second calculation module 240, and a retrieval module 250;

[0134] Processing module 210 is used to process all bytecode in the Ethereum Virtual Machine's bytecode library to obtain all bytecode functions;

[0135] The first computing module 220 is used to calculate the first similarity between the natural language query content to be retrieved and all bytecode functions based on the pre-trained bytecode retrieval machine.

[0136] The generation module 230 is used to input natural language query content into a preset large model to generate the code language function of the corresponding smart contract function;

[0137] The second computation module 240 is used to calculate the second similarity between code language functions and all bytecode functions based on a pre-trained specific retrieval module; the bytecode retrieval module and the specific retrieval module are trained separately based on intra-batch contrastive learning; the specific retrieval module is obtained by fine-tuning the bytecode retrieval module.

[0138] The retrieval module 250 is used to retrieve the target bytecode function corresponding to the natural language query content based on the first similarity and the second similarity.

[0139] The above-mentioned device solves the problem of low retrieval accuracy in related technologies. First, it uses bytecode library processing to adapt to the Ethereum Virtual Machine and a pre-trained bytecode retrieval tool to complete the initial first similarity calculation. Then, it uses a large model in conjunction with a pre-trained specific retrieval tool to fully understand the language information of the source code to obtain an enhanced second similarity. Finally, based on the first and second similarities, it retrieves the accurate target bytecode function, thereby enhancing the retrieval recall and accuracy.

[0140] In some embodiments, the retrieval module 250 is further configured to calculate the first similarity and the second similarity corresponding to each bytecode function to obtain the third similarity corresponding to each bytecode function;

[0141] Based on the preset similarity evaluation criteria and the third similarity, the target bytecode function corresponding to the natural language query content is determined; the similarity evaluation criteria is the maximum and minimum similarity scores.

[0142] All third-order similarity scores are sorted, and the bytecode function with the highest similarity score is selected from the sorted results as the target bytecode function corresponding to the natural language query content.

[0143] In some embodiments, the Ethereum Virtual Machine's bytecode function retrieval device further includes a pre-training module;

[0144] The pre-training module is used to build an initial bytecode search engine and an initial specific search engine based on the initial natural language query sub-search engine and the initial bytecode query sub-search engine, respectively; the initial natural language query sub-search engine and the initial bytecode query sub-search engine have the same architecture;

[0145] Based on in-batch comparison learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval are trained to obtain a pre-trained bytecode retrieval.

[0146] Based on intra-batch contrastive learning, the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial specific retrieval are trained to obtain the pre-trained specific retrieval.

[0147] In some embodiments, the pre-training module is further configured to perform intra-batch contrastive learning training on the initial natural language query sub-retrieval and the initial bytecode query sub-retrieval in the initial bytecode retrieval based on preset first training data and preset contrastive loss function, so as to obtain a pre-trained bytecode retrieval.

[0148] The first training data includes natural language query samples, code query samples, and decompiled bytecode samples; the pre-trained bytecode retrieval system includes a first natural language query sub-retrieval system and a first bytecode query sub-retrieval system.

[0149] In some embodiments, the pre-training module is further configured to perform meta-learning training on the initial natural language query sub-retrieval in the initial specific retrieval based on preset second training data and preset contrast loss function, so as to obtain the second natural language query sub-retrieval in the pre-trained meta-retrieval; the second training data includes natural language query samples and decompiled bytecode samples.

[0150] Based on the preset small sample data and contrastive loss function, the second natural language query sub-retrieval is trained by in-batch contrastive learning to obtain the third natural language query sub-retrieval in the pre-trained specific retrieval.

[0151] In some embodiments, the pre-training module is further configured to use the first bytecode query sub-retrieval as a bytecode query sub-retrieval in the pre-trained meta-retrieval;

[0152] Use the first bytecode query sub-retrieval as a bytecode query sub-retrieval in the pre-trained specific retrieval.

[0153] In some embodiments, the pre-training module is further configured to divide the second training data into multiple second sub-training data; each second sub-training data corresponds to a batch; and divide the second sub-training data into a first subset and a second subset.

[0154] A copy of the initial natural language query sub-retrieval is created based on the first subset of the dataset, resulting in a copy sub-retrieval.

[0155] The parameter gradient of the duplicate sub-retrieval is obtained by training based on the second subset of the dataset and the duplicate sub-retrieval.

[0156] The initial natural language query sub-retrieval is trained based on the contrastive loss function and parameter gradients to obtain the second natural language query sub-retrieval in the pre-trained meta-retrieval.

[0157] It should be noted that the above modules can be functional modules or program modules, and can be implemented through software or hardware. For modules implemented through hardware, the above modules can reside in the same processor; or the above modules can be located in different processors in any combination.

[0158] This embodiment also provides a computer device, including a memory and a processor, wherein the memory stores a computer program and the processor is configured to run the computer program to perform the steps in any of the above method embodiments.

[0159] Optionally, the computer device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor and the input / output device is connected to the processor.

[0160] Optionally, in this embodiment, the processor can be configured to perform the following steps via a computer program:

[0161] S1 processes all the bytecode in the Ethereum Virtual Machine's bytecode library to obtain all the bytecode functions;

[0162] S2, based on a pre-trained bytecode retrieval machine, calculates the first similarity between the natural language query content to be retrieved and all bytecode functions;

[0163] S3, input the natural language query content into the preset large model to generate the code language function of the corresponding smart contract function;

[0164] S4, based on a pre-trained specific retrieval engine, calculates the second similarity between code language functions and all bytecode functions; the bytecode retrieval engine and the specific retrieval engine are trained separately based on intra-batch contrastive learning; the specific retrieval engine is obtained by fine-tuning the bytecode retrieval engine.

[0165] S5. Based on the first similarity and the second similarity, retrieve the target bytecode function corresponding to the natural language query content.

[0166] It should be noted that the specific examples in this embodiment can refer to the examples described in the above embodiments and optional implementations, and will not be repeated in this embodiment.

[0167] Furthermore, in conjunction with the Ethereum Virtual Machine bytecode function retrieval method provided in the above embodiments, this embodiment can also provide a storage medium for implementation. This storage medium stores a computer program; when executed by a processor, this computer program implements any of the Ethereum Virtual Machine bytecode function retrieval methods described in the above embodiments.

[0168] It should be noted that all information and data involved in this application are authorized by the user or fully authorized by all parties and will be used legally.

[0169] It should be understood that the specific embodiments described herein are merely illustrative of the application and not intended to limit it. All other embodiments derived by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application.

[0170] Obviously, the accompanying drawings are merely some examples or embodiments of this application. Those skilled in the art can apply this application to other similar situations based on these drawings without any creative effort. Furthermore, it is understood that although the work done in this development process may be complex and lengthy, for those skilled in the art, certain design, manufacturing, or production modifications made based on the technical content disclosed in this application are merely conventional technical means and should not be considered as insufficient disclosure of this application.

[0171] The term "embodiment" in this application refers to a specific feature, structure, or characteristic described in connection with an embodiment that may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily imply the same embodiment, nor does it imply that it is mutually exclusive with or independent of other embodiments. It will be clearly or implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.

[0172] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of patent protection. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the appended claims.

Claims

1. A method for retrieving a bytecode function of an Ethereum virtual machine, characterized by, The method comprises: processing all bytecodes in a bytecode library of an Ethereum virtual machine to obtain all bytecode functions; calculating a first similarity between a natural language query content to be retrieved and all the bytecode functions based on a pre-trained bytecode retriever; inputting the natural language query content into a preset large model to generate a code language function corresponding to a smart contract function; calculating a second similarity between the code language function and all the bytecode functions based on a pre-trained specific retriever, wherein the specific retriever is fine-tuned from the bytecode retriever; retrieving a target bytecode function corresponding to the natural language query content according to the first similarity and the second similarity. 2.The method of retrieving bytecode functions of an Ethereum virtual machine according to claim 1, wherein, The method of retrieving a target bytecode function corresponding to the natural language query content according to the first similarity and the second similarity comprises: operating the first similarity and the second similarity corresponding to each bytecode function to obtain a third similarity corresponding to each bytecode function; determining a target bytecode function corresponding to the natural language query content according to a preset similarity evaluation basis and the third similarity, wherein the similarity evaluation basis is a maximum similarity score. 3.The method of claim 1, wherein, The method further comprises: constructing an initial bytecode retriever and an initial specific retriever based on an initial natural language query sub-retriever and an initial bytecode query sub-retriever, wherein the initial natural language query sub-retriever and the initial bytecode query sub-retriever have the same architecture; training the initial natural language query sub-retriever and the initial bytecode query sub-retriever in the initial bytecode retriever based on in-batch contrast learning to obtain the pre-trained bytecode retriever; training the initial natural language query sub-retriever and the initial bytecode query sub-retriever in the initial specific retriever based on in-batch contrast learning to obtain the pre-trained specific retriever.

4. The bytecode function retrieval method of an Ethereum virtual machine according to claim 3, characterized in that, The method of training the initial natural language query sub-retriever and the initial bytecode query sub-retriever in the initial bytecode retriever based on in-batch contrast learning to obtain the pre-trained bytecode retriever comprises: training the initial natural language query sub-retriever and the initial bytecode query sub-retriever in the initial bytecode retriever based on in-batch contrast learning according to preset first training data and a preset contrast loss function to obtain the pre-trained bytecode retriever; the first training data comprises natural language query samples, code query samples, and decompiled bytecode samples; and the pre-trained bytecode retriever comprises a first natural language query sub-retriever and a first bytecode query sub-retriever. 5.The method of claim 4, wherein, The method of training the initial natural language query sub-retriever and the initial bytecode query sub-retriever in the initial specific retriever based on in-batch contrast learning to obtain the pre-trained specific retriever comprises: According to the preset second training data and the preset contrast loss function, the initial natural language query sub-retriever in the initial specific retriever is subjected to meta-learning training to obtain a second natural language query sub-retriever in a pre-trained meta-retriever; the second training data includes natural language query samples and decompiled bytecode samples; According to the preset small sample data and the contrast loss function, the second natural language query sub-retriever is subjected to in-batch contrast learning training to obtain a third natural language query sub-retriever in the pre-trained specific retriever. 6.The method of retrieving bytecode functions of an Ethereum virtual machine according to claim 5, wherein, The method further comprises: The first bytecode query sub-retriever is used as a bytecode query sub-retriever in the pre-trained meta-retriever; The first bytecode query sub-retriever is used as a bytecode query sub-retriever in the pre-trained specific retriever. 7.The method of claim 5, wherein, According to the preset second training data and the preset contrast loss function, the initial natural language query sub-retriever in the initial specific retriever is subjected to meta-learning training to obtain a second natural language query sub-retriever in a pre-trained meta-retriever, comprising: The second training data is divided into a plurality of second sub-training data; each second sub-training data corresponds to a batch; and the second sub-training data is divided into a first sub-data set and a second sub-data set; Based on the first sub-data set, a copy of the initial natural language query sub-retriever is created to obtain a copy sub-retriever; According to the second sub-data set and the copy sub-retriever, the copy sub-retriever is trained to obtain an updated parameter gradient of the copy sub-retriever; According to the contrast loss function and the parameter gradient, the initial natural language query sub-retriever is trained to obtain a second natural language query sub-retriever in the pre-trained meta-retriever.

8. A bytecode function retrieval device for the Ethereum Virtual Machine, characterized in that, Comprise: a processing module, a first calculation module, a generation module, a second calculation module, and a retrieval module; The processing module is configured to process all bytecodes in a bytecode library of an Ethereum virtual machine to obtain all bytecode functions; The first calculation module is configured to calculate a first similarity between a natural language query content to be retrieved and all the bytecode functions based on a pre-trained bytecode retriever; The generation module is configured to input the natural language query content into a preset large model to generate a code language function corresponding to a smart contract function; The second calculation module is configured to calculate a second similarity between the code language function and all the bytecode functions based on a pre-trained specific retriever; the specific retriever is obtained by fine-tuning the bytecode retriever; The retrieval module is configured to retrieve a target bytecode function corresponding to the natural language query content according to the first similarity and the second similarity. 9.A computer device, comprising a memory and a processor, and characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to execute the steps of the bytecode function retrieval method of the Ethereum virtual machine according to any one of claims 1 to 7.

10. A computer-readable storage medium having stored thereon a computer program, characterized in that, The computer program is executed by the processor to implement the steps of the bytecode function retrieval method of the Ethereum virtual machine according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Digital object retrieval method and device based on retrieval enhancement generation under intelligent contract mechanism

    CN118410241A

  • KR20240012184A