A method and system for optimizing decompiled smart contract code

By constructing a contract dependency graph and a large language model to optimize smart contract decompilation code, the problems of method boundary identification, variable type recovery, and contract attribute inference are solved, achieving more comprehensive code optimization, improving the accuracy and reliability of decompilation output, and supporting more effective program understanding and vulnerability detection.

CN120029630BActive Publication Date: 2025-11-14SUN YAT SEN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510255846.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Priority Date
2025-01-09
Filing Date
2025-03-05
Publication Date
2025-11-14
Estimated Expiration
2045-03-05

AI Technical Summary

Technical Problem

Existing smart contract decompilation code optimization methods cannot provide more comprehensive optimization solutions, especially in terms of method boundary identification, variable type recovery, and contract attribute inference. This results in insufficient accuracy and reliability of the decompiled output, affecting vulnerability detection and program understanding.

Method used

By constructing a contract dependency graph, generating variable method code contexts, optimizing the code using a large language model and pre-built variable type inference candidate sets and contract attribute inference candidate sets, and using pre-built program analysis and verification techniques for consistency checks, the optimized code maintains behavioral consistency.

Benefits of technology

It provides a more comprehensive optimization scheme, improves the accuracy and reliability of decompiled output, enhances code readability and usability, and supports more effective program understanding and vulnerability detection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120029630B_ABST
    Figure CN120029630B_ABST
Patent Text Reader

Abstract

This invention discloses a method and system for optimizing decompiled smart contract code, addressing the technical problem that existing smart contract decompilation code optimization methods cannot provide more comprehensive optimization solutions. The method includes: acquiring the decompiled code of the smart contract to be optimized; constructing a contract dependency graph based on the decompiled code; extracting fragments from the contract dependency graph to generate variable and method code contexts; generating multiple thought chain hints based on the contract dependency graph; using a large language model to optimize the decompiled smart contract code based on the variable and method code contexts, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, outputting intermediate decompiled smart contract code; and using pre-set program analysis and verification technology to perform a consistency check on the intermediate decompiled smart contract code based on the decompiled code of the smart contract to be optimized, determining the target smart contract decompiled code.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] This application claims priority to Chinese patent application No. 202510036616.2, filed on January 9, 2025, the entire contents of which are incorporated herein by reference. Technical Field

[0002] This invention relates to the field of information security technology, and in particular to a method and system for optimizing smart contract decompilation code. Background Technology

[0003] Smart contracts are automatically executing programs that run on a blockchain. Due to the transparency of the blockchain, the bytecode of smart contracts is publicly visible, but their source code is usually not disclosed. Statistics show that over 99% of smart contracts only disclose their bytecode and not their source code; therefore, decompilation has become a key technology for understanding and analyzing smart contracts.

[0004] Decompilation is the process of converting low-level machine code (such as EVM bytecode, Ethereum Virtual Machine Bytecode) back into a high-level representation (such as Solidity code). It plays an important role in program analysis, especially in program understanding and vulnerability detection. Decompilation involves recovering abstractions of variables, functions, and control flow through various program analysis methods, and then using heuristic rules to synthesize these abstractions to reconstruct a high-level code representation.

[0005] Most existing smart contract decompilation code optimization methods are based on smart contract decompilation frameworks that convert EVM bytecode into high-level code similar to Solidity. They identify method boundaries by analyzing patterns of repeated calls, thereby optimizing the smart contract decompilation code. However, this approach only focuses on a single problem and cannot provide a more comprehensive optimization solution. Summary of the Invention

[0006] This invention provides a method and system for optimizing smart contract decompiled code, which addresses the technical problem that existing smart contract decompiled code optimization methods cannot provide more comprehensive optimization solutions.

[0007] The first aspect of this invention provides a method for optimizing smart contract decompiled code, comprising:

[0008] Obtain the decompiled code of the smart contract to be optimized, and construct a contract dependency graph based on the decompiled code of the smart contract to be optimized;

[0009] Fragment extraction is performed on the contract dependency graph to generate variable method code context;

[0010] Based on the contract dependency graph, multiple thought chain prompts are generated;

[0011] A large language model is used to optimize the decompiled code of the smart contract to be optimized based on the variable method code context, multiple thought chain hints, a pre-set variable type inference candidate set and a pre-set contract attribute inference candidate set, and output intermediate smart contract decompiled code.

[0012] The pre-programmed analysis and verification technology is used to perform a consistency check on the intermediate smart contract decompiled code based on the smart contract decompiled code to be optimized, and to determine the target smart contract decompiled code.

[0013] Optionally, the step of constructing a contract dependency graph based on the decompiled code of the smart contract to be optimized includes:

[0014] Type dependency identification is performed on the decompiled code of the smart contract to be optimized, and multiple contract dependency data corresponding to the decompiled code of the smart contract to be optimized are output; the multiple contract dependency data include type dependency, state dependency and control flow dependency.

[0015] Construct a contract dependency graph based on the type dependency, the state dependency, and the control flow dependency.

[0016] Optionally, the variable method code context includes variable code snippets and method code snippets; the step of extracting snippets from the contract dependency graph to generate the variable method code context includes:

[0017] The contract dependency graph is sliced ​​to generate variable code snippets;

[0018] The call chain analysis is performed on the contract dependency graph to generate method code snippets.

[0019] Optionally, the step of using a large language model to optimize the decompiled smart contract code based on the variable method code context, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, and outputting intermediate smart contract decompiled code, includes:

[0020] Data extraction is performed on the variable code fragment and the method code fragment, and the structural features and semantic information corresponding to the variable code fragment and the method code fragment are output.

[0021] Using the structural features and semantic information corresponding to the variable code fragments and the structural features and semantic information corresponding to the method code fragments as constraints, large-scale language model reasoning is performed using the pre-set variable type reasoning candidate set and the pre-set contract attribute reasoning candidate set to generate code optimization suggestions;

[0022] The decompiled code of the smart contract to be optimized is optimized using multiple thought chain hints and code optimization suggestions, and intermediate smart contract decompiled code is output.

[0023] Optionally, the pre-built program analysis and verification technology includes formal verification methods and violation of rejection rules; the step of using the pre-built program analysis and verification technology to perform a consistency check on the intermediate smart contract decompiled code based on the smart contract decompiled code to be optimized, and determining the target smart contract decompiled code, includes:

[0024] Symbolic execution is performed on the decompiled code of the smart contract to be optimized and the decompiled code of the intermediate smart contract respectively, generating symbol digests corresponding to the decompiled code of the smart contract to be optimized and the symbol digests corresponding to the decompiled code of the intermediate smart contract;

[0025] Formal verification methods are used to determine whether the symbol digest corresponding to the decompiled code of the smart contract to be optimized and the symbol digest corresponding to the decompiled code of the intermediate smart contract are equivalent;

[0026] If so, determine whether the decompiled code of the intermediate smart contract violates the rejection rule;

[0027] If the conditions are met, the intermediate smart contract decompiled code will be used as the target smart contract decompiled code.

[0028] Optionally, the violation of the rejection rule specifically refers to:

[0029] ;

[0030] Where π is the intermediate smart contract decompiled code, representing the context containing a list of variable types assigned to expression patterns; e is the expression; and θ is the variable type.

[0031] A second aspect of the present invention provides a smart contract decompilation code optimization system, comprising:

[0032] The acquisition module is used to acquire the decompiled code of the smart contract to be optimized, and to construct a contract dependency graph based on the decompiled code of the smart contract to be optimized;

[0033] The extraction module is used to extract fragments from the contract dependency graph and generate variable method code context;

[0034] The generation module is used to generate multiple thought chain prompts based on the contract dependency graph;

[0035] The optimization module is used to optimize the decompiled code of the smart contract to be optimized by using a large language model based on the variable method code context, multiple thought chain hints, a pre-set variable type inference candidate set and a pre-set contract attribute inference candidate set, and output intermediate smart contract decompiled code;

[0036] The inspection module is used to perform a consistency check on the intermediate smart contract decompiled code based on the decompiled code of the smart contract to be optimized using a pre-set program analysis and verification technology, and to determine the target smart contract decompiled code.

[0037] A computer device provided in a third aspect of the present invention includes a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor performs the steps of the smart contract decompilation code optimization method as described in any of the preceding claims.

[0038] The fourth aspect of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed, it implements the steps of the smart contract decompilation code optimization method as described in any of the preceding claims.

[0039] The fifth aspect of the present invention provides a computer program product comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program comprising program instructions, wherein, when the program instructions are executed by a computer, the computer performs the steps of the smart contract decompilation code optimization method as described in any of the preceding claims.

[0040] As can be seen from the above technical solutions, the present invention has the following advantages:

[0041] The above-mentioned technical solution of the present invention provides a method for optimizing decompiled smart contract code. First, the decompiled code of the smart contract to be optimized is obtained, and a contract dependency graph is constructed based on the decompiled code. Next, fragments are extracted from the contract dependency graph to generate variable and method code contexts. Based on the contract dependency graph, multiple thought chain hints are generated. A large-scale language model is used to optimize the decompiled smart contract code based on the variable and method code contexts, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, outputting intermediate decompiled smart contract code. Finally, a pre-set program analysis and verification technique is used to perform a consistency check on the intermediate decompiled smart contract code based on the decompiled smart contract code to be optimized, determining the target smart contract decompiled code. Based on the above solution, the process of combining the variable and method code contexts, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, inputting them into a large-scale language model for code optimization, and then performing a consistency check based on the pre-set program analysis and verification technique to determine the target smart contract decompiled code, simultaneously considers three key issues: method boundary identification, variable type recovery, and contract attribute inference, providing a more comprehensive optimization solution. Attached Figure Description

[0042] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0043] Figure 1 This is a flowchart illustrating the steps of a smart contract decompilation code optimization method provided in Embodiment 1 of the present invention.

[0044] Figure 2 This is an overall framework diagram for optimizing smart contract decompiled code provided in Embodiment 2 of the present invention;

[0045] Figure 3 This is a structural block diagram of a smart contract decompilation code optimization system provided in Embodiment 3 of the present invention. Detailed Implementation

[0046] This invention provides a method and system for optimizing smart contract decompiled code, which addresses the technical problem that existing smart contract decompiled code optimization methods cannot provide more comprehensive optimization solutions.

[0047] To make the objectives, features, and advantages of this invention more apparent and understandable, the technical solutions of the embodiments of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the embodiments described below are only some embodiments of this invention, and not all embodiments. Based on the embodiments of this invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this invention.

[0048] Terminology Explanation:

[0049] Smart Contract: An automatically executing program that runs on a blockchain and is used to automatically execute contract terms when predetermined conditions are met.

[0050] Decompiler: A tool that converts low-level machine code (such as bytecode) back into high-level programming language code.

[0051] EVM (Ethereum Virtual Machine): The Ethereum Virtual Machine is the runtime environment for smart contracts, executing smart contract code on the Ethereum network.

[0052] Solidity: The most commonly used smart contract programming language, specifically designed for writing Ethereum smart contracts.

[0053] Bytecode: The low-level representation of a smart contract after compilation; it is the code form that the EVM executes directly.

[0054] Large Language Model (LLM): A natural language processing model based on deep learning, such as the GPT series, that can understand and generate human language.

[0055] Static analysis: A technique for analyzing the structure and semantics of program code without actually running the program.

[0056] Dependency Graph: A graph structure that represents the dependencies between elements (such as variables and functions) in a program.

[0057] Symbolic execution: A technique that uses symbolic values ​​instead of concrete values ​​to analyze the program execution path.

[0058] SMT Solver: A tool for solving problems in Satisfiability Modulo Theories, such as Z3.

[0059] Method Boundary: Defines the start and end points of a method (function) in the code.

[0060] Variable Type Recovery: The process of inferring and recovering the data type of a variable during decompilation.

[0061] Contract Attribute: Attributes that describe the characteristics or functions of a smart contract, such as assets, identity, router, etc.

[0062] Chain-of-Thought Prompt: A series of intermediate reasoning steps provided for large language models, guiding the model to perform more complex reasoning.

[0063] Correctness verification: The process of ensuring that the optimized code is functionally equivalent to the original code.

[0064] Program slicing: A technique for extracting parts of a program that are relevant to a specific computation.

[0065] Abstract Syntax Tree (AST): A tree-like data structure that represents the structure of a program's source code.

[0066] Control Flow Graph (CFG): A graph that represents all possible execution paths of a program.

[0067] Formal verification: a technique that uses mathematical methods to prove or refute the correctness of a software system.

[0068] Fuzzing: An automated software testing technique that discovers software vulnerabilities by providing unexpected or random data as input.

[0069] Please see Figure 1 , Figure 1 The flowchart illustrates the steps of a smart contract decompilation code optimization method provided in Embodiment 1 of the present invention.

[0070] This invention provides a method for optimizing smart contract decompiled code, comprising:

[0071] Step 101: Obtain the decompiled code of the smart contract to be optimized, and construct a contract dependency graph based on the decompiled code of the smart contract to be optimized.

[0072] Specifically, the process of constructing a contract dependency graph based on the decompiled code of the smart contract to be optimized can be achieved by executing the following sub-steps S11 to S12:

[0073] Step S11: Identify type dependencies in the decompiled code of the smart contract to be optimized, and output multiple contract dependency data corresponding to the decompiled code of the smart contract to be optimized; the multiple contract dependency data include type dependency, state dependency and control flow dependency;

[0074] Step S12: Construct a contract dependency graph based on type dependency, state dependency, and control flow dependency.

[0075] It should be noted that the first step is to extract key semantic information from the decompiled code of the smart contract to be optimized, namely, to identify three types of dependencies: type dependency, state dependency, and control flow dependency. Type dependency refers to the dependency relationship between the type of variable va and another variable vb or a specific expression eb. The system then applies this information according to the expression syntax rules of the Solidity language (e.g., ...). Figure 2 (As shown) Identify type dependencies; State dependencies: refer to the dependencies between different state variables or between state variables and expressions, such as read-write dependencies on state variables. The system uses the SmartState analyzer to identify state dependencies; Control flow dependencies: refer to the dependencies in the program execution flow, such as conditional statements, loops, etc.; Among them, the rules for identifying type dependencies can be expressed as:

[0076]

[0077] In a smart contract, an expression (e) can be any of the following forms: a single variable (v), such as: x, balance, totalSupply; a constant value (c), such as: 5, "hello", true; two expressions connected by a Boolean operator (e blop e), such as: isActive && isOwner; two expressions connected by a mathematical operator (e numop e), such as: a + b, price * quantity; two expressions connected by a comparison operator (e cmpope), such as: balance > 0; two expressions connected by a bitwise operator (e bitop e), such as: flags & mask; a tuple of multiple expressions ((e, ..., e)), such as: (x, y, z); an array of multiple expressions ([e, ..., e]), such as: [1, 2, 3]; a function call expression (e(e, ..., ..., ...)). e)), such as: transfer(address, amount); access arrays or maps using indices (e[e]), such as: balances[userAddress]; slice arrays (e[e: e]), such as: array[1:4]; access object properties (ev), such as: msg.sender.

[0078] Furthermore, based on the identified dependencies, the system constructs a dependency graph (DG), namely, a contract dependency graph. The DG can be represented as a triple Gc = (Nc, Ec, Xe), where: Nc: the set of nodes in the graph, including variable nodes Nv and expression nodes Ne; Ec: the set of edges in the graph, including control flow dependency edges Ed, state dependency edges Es, and type dependency edges Et; and Xe: a labeling function that maps edges to their corresponding dependency types.

[0079] It is worth mentioning that a control flow graph is generated first, and then a complete dependency graph (DG) is constructed by incrementally adding state dependency edges and type dependency edges.

[0080] Step 102: Extract fragments from the contract dependency graph to generate variable method code context.

[0081] Further, step 102 may include the following sub-steps S21-S22:

[0082] Step S21: Slice the contract dependency graph into code to generate variable code snippets;

[0083] Step S22: Perform call chain analysis on the contract dependency graph to generate method code snippets.

[0084] It should be noted that relevant code snippets are generated for variables and methods as code context; specifically, for variables: code slicing is performed based on the dependency graph (DG) to extract code snippets related to the target variable, resulting in variable code snippets; for methods: the DG is searched to find the call chain of the target method, and relevant methods are combined to generate code snippets, resulting in method code snippets.

[0085] Step 103: Generate multiple thought chain prompts based on the contract dependency graph.

[0086] It should be noted that, based on the contract dependency graph, a series of intermediate inference steps are generated as thought chain hints to guide the LLM model (Large Language Model) in inference. Based on the templates in Table 1, corresponding thought chain hints are generated for different optimization tasks.

[0087] Table 1: Mind Chain Hint Template

[0088]

[0089] Step 104: Using a large language model, optimize the decompiled code of the smart contract to be optimized based on the variable method code context, multiple thought chain hints, pre-set variable type inference candidate set, and pre-set contract attribute inference candidate set, and output intermediate smart contract decompiled code.

[0090] It should be noted that before performing code optimization, candidate sets of variable types and contract attributes are first provided for LLM, namely, a pre-defined variable type inference candidate set and a pre-defined contract attribute inference candidate set; the pre-defined variable type inference candidate set is collected from the Solidity documentation based on built-in types, and the pre-defined contract attribute inference candidate set is summarized by analyzing representative smart contract datasets to obtain attribute categories such as [Limit, Fee, Flag, Address, Asset, Router, Others].

[0091] Further, step 104 may include the following sub-steps S41-S43:

[0092] Step S41: Extract data from variable code snippets and method code snippets, and output the structural features and semantic information corresponding to variable code snippets and method code snippets;

[0093] Step S42: Using the structural features and semantic information corresponding to the variable code fragments and the structural features and semantic information corresponding to the method code fragments as constraints, large-scale language model reasoning is performed using a pre-set variable type reasoning candidate set and a pre-set contract attribute reasoning candidate set to generate code optimization suggestions;

[0094] Step S43: Optimize the decompiled code of the smart contract to be optimized using multiple thought chain hints and code optimization suggestions, and output the intermediate smart contract decompiled code.

[0095] It's important to note that LLM optimization is the core step in the entire semantic enrichment process. In this step, SmartHalo combines the three types of hints generated earlier (code context, inference candidates, and thought chains) into a complete hint input to the LLM. The specific organization of the hints is as follows: First, the code context section contains code snippets related to the optimization target (e.g., the variable `setStorage`). Second, the inference candidate section lists possible variable types or contract attributes. Finally, the thought chain section describes the dependency analysis steps in the inference process.

[0096] Specifically, firstly, in the code context analysis phase, SmartHalo receives input from the dependency graph and processes variable and method code snippets separately. For variables, the system uses code slicing technology to locate dependencies related to the target variable; for methods, it identifies the relationships between methods by analyzing the call chain. The purpose of this phase is to extract the structural features and semantic information of the code, that is, to extract data from variable and method code snippets separately and output the corresponding structural features and semantic information of the variable and method code snippets.

[0097] Secondly, in the candidate solution generation phase, a predefined set of variable type candidate sets (from the Solidity documentation) and contract attribute candidate sets are used to guide the LLM reasoning process. These candidate sets help the LLM limit the scope of reasoning within a reasonable space, improving the accuracy of the reasoning. The output of this phase is constrained optimization suggestions, i.e., LLM reasoning is performed based on the structural features and semantic information corresponding to variable code snippets and method code snippets (as constraints), the predefined set of variable type candidate sets, and the set of contract attribute candidate sets to generate constrained optimization suggestions (code optimization suggestions).

[0098] Finally, in the optimization execution phase, the system uses thought chain hints to guide the LLM to optimize according to the reasoning steps of static analysis. The thought chain hints serve as a guideline for reasoning, ensuring that the LLM's reasoning process conforms to the logic of static analysis. This phase directly outputs the optimized code, i.e., based on the thought chain hints and constrained optimization suggestions, the decompiled code of the smart contract to be optimized is optimized according to the reasoning steps of static analysis, and the optimized code (intermediate smart contract decompiled code) is output.

[0099] For example, LLM optimization is the core step in the entire semantic enrichment process. In this step, SmartHalo integrates three important types of hints into a complete input for the LLM to process. This is like providing the LLM with a working guide containing all the necessary information to help it complete the code optimization task. As shown in Table 2, for the example in Table 2, the complete hints include: 1. Code context: showing the code snippet related to the variable setStorage; 2. Type candidates: [bool, bytes32, ...uint256]; 3. Thought chain: analyzing the derivation process from the return type of the keccak256 function to the type setStorage. Specifically, suppose we need to optimize the variable setStorage in a smart contract code and determine its correct type. SmartHalo will organize the information as follows: First, it provides code context: this is like showing the LLM a complete scenario diagram so that it understands how this variable is used. For example:

[0100]

[0101] Then provide possible options: This is equivalent to giving the LLM a list of choices, telling it that "the type of the variable can only be one of these options". For example, it can be a simple type: such as number (uint256), text (string), yes / no (bool), etc.; it can also be a complex type: such as address (address), byte data (bytes32), etc.; or a mapping type: a data structure used to store key-value pairs.

[0102] Finally, provide guidance on thinking: This is like giving the LLM a problem-solving approach, telling it how to analyze step by step. For example: first look at the function keccak256, which always returns a bytes32 type of data; then see that this data is passed to the v0 variable; finally find that setStorage uses v0 as an index, which indicates that it should be a mapping type. Through such clear information organization, the LLM can accurately understand the purpose and structure of the code, make choices within a reasonable range, analyze according to logical steps, and finally give the correct optimization suggestions.

[0103] This process is like guiding a smart assistant to complete code analysis. By providing complete background information, alternative solutions, and analysis methods, it ensures that the assistant can draw accurate conclusions. This is also an important reason why SmartHalo can achieve good results in code optimization.

[0104] Table 2 Examples of Optimization Tips

[0105]

[0106] Step 105: Using pre-built program analysis and verification technology, perform consistency checks on the intermediate smart contract decompiled code based on the decompiled code of the smart contract to be optimized, and determine the target smart contract decompiled code.

[0107] Pre-defined procedure analysis and verification techniques include formal verification and violation of rejection rules.

[0108] Further, step 105 may include the following sub-steps S51-S54:

[0109] Step S51: Perform symbolic execution on the decompiled code of the smart contract to be optimized and the decompiled code of the intermediate smart contract respectively, and generate symbol digests corresponding to the decompiled code of the smart contract to be optimized and the symbol digests corresponding to the decompiled code of the intermediate smart contract.

[0110] Step S52: Use formal verification to determine whether the symbol digest corresponding to the decompiled code of the smart contract to be optimized and the symbol digest corresponding to the decompiled code of the intermediate smart contract are equivalent;

[0111] Step S53: If yes, then determine whether the decompiled code of the intermediate smart contract violates the rejection rule;

[0112] Step S54: If the conditions are met, the intermediate smart contract decompiled code is used as the target smart contract decompiled code.

[0113] It should be noted that after the code optimization is completed, it is also necessary to ensure that the optimized code maintains behavioral consistency and conforms to static rules. This includes two checks: checking the equivalence of program behavior based on symbolic execution and formal verification methods, that is, using symbolic execution and formal verification methods (formal verification methods) to verify the behavioral equivalence of the code before and after optimization. Specifically: (1) Perform symbolic execution on the original method (the decompiled code of the smart contract to be optimized) m and the optimized method (the decompiled code of the intermediate smart contract) m' to generate symbolic digests s and s', that is, generate the symbolic digest s corresponding to the decompiled code of the smart contract to be optimized and the symbolic digest s' corresponding to the decompiled code of the intermediate smart contract. (2) Construct equivalence assertions. (3) Use an SMT solver (such as Z3) to analyze whether assertion Φ is satisfied. If Φ is satisfied, it means that methods m and m' are not equivalent in terms of program behavior; otherwise, they are equivalent.

[0114] Furthermore, static rule violation checks are performed based on the violation rejection rules: SmartHalo traverses the optimized code and then uses the violation rejection rules to identify and reject erroneous variable types predicted by the LLM. Specifically, the SmartHalo integration of the proposed invention includes violation rejection rules. Each violation rejection rule consists of two parts: a specific premise and a conclusion. They are organized in the following form:

[0115] ;

[0116] In this context, π represents the intermediate smart contract decompiled code, indicating a context containing a list of variable types assigned to expression patterns. In this form, e refers to an expression, and this invention uses e1, ..., en to represent different expressions; θ represents variable types, and this invention uses θ1, ..., θn to represent different variable types. The rules in this form are called judgments or assignments, and the goal of this invention is to obtain the context π that assigns variable types to all variables in the code.

[0117] It is worth mentioning that if the symbol digest corresponding to the decompiled code of the smart contract to be optimized is not equivalent to the symbol digest corresponding to the decompiled code of the intermediate smart contract, or if the decompiled code of the intermediate smart contract does not meet the rejection rule, it indicates that an error has been found in the verification process. The error information and verification suggestions are then re-inputted into the LLM model for iterative optimization until all optimization errors are corrected or the maximum number of iterations is reached.

[0118] For comparison of technical effects, existing technologies can be referenced. The inheritance mechanism of smart contracts differs from that of traditional languages ​​(such as C++ and Java). Smart contract inheritance involves directly embedding the code blocks of all base sub-contracts (B1, B2, ..., Bn) into the inherited contract A, without retaining explicit call information. This makes traditional method boundary identification techniques based on call sites ineffective in smart contracts. During compilation, much type information is irreversibly optimized away. For example, the return type of predefined functions and information about complex data structures are missing at the bytecode level. State variables in smart contracts are often used to record key attributes (such as assets, identity, etc.). These attributes are clearly expressed in the source code through variable names and comments, but are completely lost at the bytecode level.

[0119] Existing code optimization solutions include Gigahorse, SigRec, DeepInfer, SmartDagger, and DIRTY. Gigahorse is a smart contract decompilation framework that converts EVM bytecode into high-level code similar to Solidity. This solution uses declarative rules and data flow analysis to recover program structure, such as loops and functions. Gigahorse primarily identifies method boundaries by analyzing patterns of repeated calls. SigRec is specifically designed to recover the signatures of public functions in smart contracts. It infers the number and types of function parameters using predefined rules by analyzing opcode sequences and stack operations in the bytecode. SigRec's core idea is to identify common parameter types based on pattern matching. DeepInfer uses deep learning methods to infer function signatures. It converts bytecode into an intermediate representation and then uses graph neural networks and sequence models to predict the parameter types of functions. This approach transforms the bytecode analysis problem into a machine learning task. This approach transforms bytecode analysis into a machine learning task; SmartDagger is a cross-contract vulnerability detection framework that includes a contract attribute recovery component. This component uses static analysis to extract usage patterns of state variables and then predicts the attributes of these variables (such as assets, addresses, etc.) through a neural network model. A key feature of SmartDagger is its use of a large amount of real-world contract data for model training; DIRTY is a decompiler output enhancement framework that primarily focuses on recovering variable types and names. It uses a Transformer-based model to analyze the abstract syntax tree of decompiled code, predicting the type and name of each variable. DIRTY's innovation lies in applying natural language processing techniques to code analysis.

[0120] The aforementioned approaches represent different technical routes in the field of smart contract decompilation, including static analysis, heuristic rules, machine learning, and other methods. Each method provides solutions to specific problems in the decompilation process, but none fully addresses key issues such as method boundary identification, variable type recovery, and contract attribute inference. These methods suffer from several shortcomings, such as inaccurate method boundary identification: existing decompilers (such as Gigahorse) have limitations in identifying smart contract method boundaries, especially for inherited methods. This leads to the omission of important methods or misidentification of method scope, increasing the complexity of downstream tasks (such as method-level similarity comparison). Furthermore, variable type recovery is inaccurate and incomplete: the variable types generated by decompilers often differ from static domain rules, such as ignoring predefined types. While tools like SigRec and DeepInfer can infer function parameter types, they cannot handle broader variable type recovery issues. While DIRTY attempts to recover variable types, its lack of consideration for static domain knowledge easily leads to type errors inconsistent with smart contract language rules; it lacks contract attributes: decompilers cannot recover key contract attributes (such as assets, identity, etc.) explicitly declared in the source code, which are crucial for downstream tasks such as vulnerability detection; it has limited contract attribute inference capabilities: tools like SmartDagger have made some progress in inferring contract attributes, but their performance is heavily dependent on the training dataset, and their effectiveness drops significantly when faced with emerging or rare contract types; it cannot be recompiled: the code output by decompilers is usually not directly recompilable, limiting its usability. These shortcomings severely hinder reverse engineers from understanding program logic and adversely affect downstream tasks such as vulnerability detection and component analysis; it lacks a comprehensive optimization method: existing technologies often only provide solutions for single problems (such as function signature recovery or variable type inference), lacking a comprehensive method that can fully optimize decompiler output; and it suffers from a disconnect between static analysis and machine learning methods: existing methods either rely on static analysis and heuristics (such as Gigahorse and SigRec) or rely entirely on machine learning models (such as DeepInfer and DIRTY). There is a lack of a solution that can effectively combine the advantages of both methods.

[0121] To address the aforementioned problems, this invention provides a method for optimizing smart contract decompiled code. This method simultaneously solves key issues such as problem boundary identification, variable type recovery, and contract attribute inference. It combines the precision of static analysis with the flexibility of large-scale language models to improve the quality and reliability of the decompiled output. Furthermore, it designs a mechanism to effectively extract and utilize static domain knowledge of smart contracts to guide large-scale language models in more accurate semantic recovery. A rigorous correctness verification mechanism is also developed to ensure that the optimized code maintains behavioral consistency with the original decompiled output and eliminates reasoning errors that violate static domain rules. In addition, it improves the readability and usability of the decompiled code, making it easier for analysts to understand and utilize, thereby supporting more effective downstream tasks such as program understanding, vulnerability detection, and component analysis. By achieving these objectives, this invention significantly improves the quality of smart contract decompilation, providing a more reliable foundation for the security analysis and understanding of smart contracts.

[0122] In this embodiment of the invention, a method for optimizing decompiled smart contract code is provided. First, the decompiled code of the smart contract to be optimized is obtained, and a contract dependency graph is constructed based on this code. Next, fragments are extracted from the contract dependency graph to generate variable and method code contexts. Based on the contract dependency graph, multiple thought chain hints are generated. A large-scale language model is used to optimize the decompiled smart contract code based on the variable and method code contexts, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, outputting intermediate decompiled smart contract code. Finally, a pre-set program analysis and verification technique is used to perform a consistency check on the intermediate decompiled smart contract code based on the decompiled smart contract code to be optimized, determining the target smart contract decompiled code. Based on the above scheme, by combining the variable and method code contexts, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, and inputting them into a large-scale language model for code optimization, followed by a consistency check based on the pre-set program analysis and verification technique to determine the target smart contract decompiled code, this invention simultaneously considers three key issues: method boundary identification, variable type recovery, and contract attribute inference, providing a more comprehensive optimization solution.

[0123] For better explanation, refer to Figure 2 This diagram illustrates the overall framework of the smart contract decompilation code optimization method provided in Embodiment 2 of the present invention. It should be noted that this embodiment only provides a brief description of the general flow of the smart contract decompilation code optimization method. The specific implementation process of each step can be understood by referring to the relevant content in the foregoing embodiments, and will not be elaborated upon here. It is understood that the present invention does not impose any limitations on this.

[0124] The overall framework for optimizing smart contract decompilation code proposed in this invention is as follows: Figure 2 As shown, named SmartHalo, SmartHalo mainly comprises three core components: a dependency-based semantic extraction module, an LLM-based semantic enrichment module, and a correctness verification module. By combining the advantages of static analysis (SA) and large-scale language models (LLM), SmartHalo comprehensively solves key problems such as method boundary identification, variable type recovery, and contract attribute inference. The following is a detailed technical solution for SmartHalo:

[0125] In the dependency-based semantic extraction module, the module is responsible for extracting key semantic information from the decompiled code, integrating three types of dependency relationships: type dependency, state dependency, and control flow dependency. The dependency graph is represented by a triple Gc = (Nc, Ec, Xe), and the complete dependency graph is constructed incrementally.

[0126] In the LLM-based semantic enrichment module, the module uses a large language model (LLM) to perform semantic optimization on the decompiled code, performs code slicing of variables and extraction of method call chains based on dependency graphs, designs templates based on dependency types, and transforms static analysis steps into an LLM-understandable reasoning process. Thus, the dependency information extracted by static analysis guides LLM reasoning, and the output of LLM is combined with static rules for verification, outputting optimized decompiled code.

[0127] In the correctness verification module, this module ensures that the optimized code maintains consistent behavior and conforms to static rules, based on symbolic execution and formal verification for program behavior equivalence checks, as well as type violation checks based on static rules; and sets an iterative optimization strategy, if the optimized code does not meet any of the checks, the verification result is fed back to LLM for further optimization, until all optimization errors are corrected or the maximum number of iterations is reached.

[0128] In this embodiment of the invention, SmartHalo solves three key problems simultaneously—method boundary identification, variable type recovery, and contract attribute inference—by combining static analysis and a large-scale language model. Compared to methods like Gigahorse and SigRec, which focus on only a single problem, SmartHalo offers a more comprehensive optimization solution. For example, in method boundary identification, SmartHalo utilizes a combination of dependency graphs and LLM to more accurately identify complex inheritance methods, which is difficult to achieve with methods like Gigahorse that rely on simple heuristic rules. SmartHalo's hybrid approach fully leverages the precision of static analysis and the flexibility of LLM. By extracting static domain knowledge through dependency graphs and guiding LLM with thought chain hints, SmartHalo can better handle emerging or rare contract patterns. This solves the performance degradation problem of methods like SmartDagger, which heavily rely on training data, when faced with novel contracts. Simultaneously, SmartHalo's correctness verification mechanism effectively prevents the illusions generated by LLM, improving the reliability of optimization results—an advantage not possessed by pure machine learning methods like DIRTY. Furthermore, SmartHalo not only improves the quality and reliability of decompiled output, but also enhances its adaptability to diverse and complex smart contracts, providing a more reliable foundation for downstream tasks such as vulnerability detection and component analysis.

[0129] Please see Figure 3 , Figure 3 This is a structural block diagram of a smart contract decompilation code optimization system provided in Embodiment 3 of the present invention.

[0130] This invention provides a smart contract decompilation code optimization system, comprising:

[0131] The acquisition module 301 is used to acquire the decompiled code of the smart contract to be optimized, and to construct a contract dependency graph based on the decompiled code of the smart contract to be optimized.

[0132] Extraction module 302 is used to extract fragments from the contract dependency graph and generate variable method code context;

[0133] Module 303 is used to generate multiple thought chain hints based on the contract dependency graph;

[0134] Optimization module 304 is used to optimize the decompiled code of the smart contract to be optimized using a large language model based on the variable method code context, multiple thought chain hints, pre-set variable type inference candidate set and pre-set contract attribute inference candidate set, and output intermediate smart contract decompiled code;

[0135] The inspection module 305 is used to perform consistency checks on the intermediate smart contract decompiled code based on the decompiled code of the smart contract to be optimized using pre-set program analysis and verification technology, and to determine the target smart contract decompiled code.

[0136] Furthermore, module 301 is specifically used for:

[0137] The decompiled code of the smart contract to be optimized is subjected to type dependency identification, and multiple contract dependency data corresponding to the decompiled code of the smart contract to be optimized are output; the multiple contract dependency data include type dependency, state dependency and control flow dependency.

[0138] Construct a contract dependency graph based on type dependency, state dependency, and control flow dependency.

[0139] Furthermore, the extraction module 302 is specifically used for:

[0140] Slice the contract dependency graph into code to generate variable code snippets;

[0141] Perform call chain analysis on the contract dependency graph to generate method code snippets.

[0142] Furthermore, module 304 is optimized specifically for:

[0143] Data extraction is performed on variable code snippets and method code snippets, and the structural features and semantic information corresponding to the variable code snippets and the method code snippets are output.

[0144] Using the structural features and semantic information corresponding to variable code fragments and method code fragments as constraints, a large-scale language model inference is performed using a pre-set variable type inference candidate set and a pre-set contract attribute inference candidate set to generate code optimization suggestions;

[0145] The decompiled code of the smart contract to be optimized is optimized using multiple thought chain hints and code optimization suggestions, and the intermediate smart contract decompiled code is output.

[0146] Furthermore, the pre-defined procedure analysis and verification techniques include formal verification and violation of rejection rules; the inspection module 305 is specifically used for:

[0147] Symbolic execution is performed on the decompiled code of the smart contract to be optimized and the decompiled code of the intermediate smart contract, respectively, to generate symbol digests for the decompiled code of the smart contract to be optimized and the symbol digests for the decompiled code of the intermediate smart contract.

[0148] Formal verification is used to determine whether the symbol digests corresponding to the decompiled code of the smart contract to be optimized and the symbol digests corresponding to the decompiled code of the intermediate smart contract are equivalent;

[0149] If so, determine whether the decompiled code of the intermediate smart contract violates the rejection rule;

[0150] If the conditions are met, the decompiled code of the intermediate smart contract will be used as the decompiled code of the target smart contract.

[0151] Furthermore, the denial rule is violated, specifically as follows:

[0152] ;

[0153] Where π is the intermediate smart contract decompiled code, representing the context containing a list of variable types assigned to expression patterns; e is the expression; and θ is the variable type.

[0154] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working process of the system and modules described above can be referred to the corresponding process in the foregoing method embodiments, and will not be repeated here.

[0155] This invention also provides a computer device, including a memory and a processor, wherein the memory stores a computer program; when the computer program is executed by the processor, the processor performs the steps of the smart contract decompilation code optimization method as described in any of the above embodiments.

[0156] This invention also provides a computer-readable storage medium storing a computer program / instructions thereon, which, when executed by a processor, implements the steps of the smart contract decompilation code optimization method as described in any of the above embodiments.

[0157] This invention also provides a computer program product, including a computer program / instructions, wherein when the computer program / instructions are executed by a processor, they implement the steps of the smart contract decompilation code optimization method as described in any of the above embodiments.

[0158] In the several embodiments provided in this application, it should be understood that the disclosed systems and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.

[0159] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0160] The above-described embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for optimizing decompiled smart contract code, characterized in that, include: Obtain the decompiled code of the smart contract to be optimized, and construct a contract dependency graph based on the decompiled code of the smart contract to be optimized; Fragment extraction is performed on the contract dependency graph to generate variable method code context; Based on the contract dependency graph, multiple thought chain prompts are generated; A large language model is used to optimize the decompiled code of the smart contract to be optimized based on the variable method code context, multiple thought chain hints, a pre-set variable type inference candidate set and a pre-set contract attribute inference candidate set, and output intermediate smart contract decompiled code. Pre-set program analysis and verification technology is used to perform a consistency check on the intermediate smart contract decompiled code based on the smart contract decompiled code to be optimized, and to determine the target smart contract decompiled code. The variable method code context includes variable code snippets and method code snippets; The step of extracting fragments from the contract dependency graph and generating variable method code context includes: The contract dependency graph is sliced ​​to generate variable code snippets; Perform call chain analysis on the contract dependency graph to generate method code snippets; The process employs a large language model to optimize the decompiled smart contract code based on the variable method code context, multiple thought chain hints, a pre-set variable type inference candidate set, and a pre-set contract attribute inference candidate set, outputting intermediate smart contract decompiled code, including: Data extraction is performed on the variable code fragment and the method code fragment, and the structural features and semantic information corresponding to the variable code fragment and the method code fragment are output. Using the structural features and semantic information corresponding to the variable code fragments and the structural features and semantic information corresponding to the method code fragments as constraints, large-scale language model reasoning is performed using the pre-set variable type reasoning candidate set and the pre-set contract attribute reasoning candidate set to generate code optimization suggestions; The decompiled code of the smart contract to be optimized is optimized using multiple thought chain hints and code optimization suggestions, and intermediate smart contract decompiled code is output.

2. The smart contract decompilation code optimization method according to claim 1, characterized in that, The step of constructing a contract dependency graph based on the decompiled code of the smart contract to be optimized includes: Type dependency identification is performed on the decompiled code of the smart contract to be optimized, and multiple contract dependency data corresponding to the decompiled code of the smart contract to be optimized are output; the multiple contract dependency data include type dependency, state dependency and control flow dependency. Construct a contract dependency graph based on the type dependency, the state dependency, and the control flow dependency.

3. The smart contract decompilation code optimization method according to claim 1, characterized in that, The pre-built program analysis and verification technology includes formal verification and violation of rejection rules; the use of the pre-built program analysis and verification technology to perform consistency checks on the intermediate smart contract decompiled code based on the smart contract decompiled code to be optimized, and to determine the target smart contract decompiled code, includes: Symbolic execution is performed on the decompiled code of the smart contract to be optimized and the decompiled code of the intermediate smart contract respectively, generating symbol digests corresponding to the decompiled code of the smart contract to be optimized and the symbol digests corresponding to the decompiled code of the intermediate smart contract; Formal verification methods are used to determine whether the symbol digest corresponding to the decompiled code of the smart contract to be optimized and the symbol digest corresponding to the decompiled code of the intermediate smart contract are equivalent; If so, determine whether the decompiled code of the intermediate smart contract violates the rejection rule; If the conditions are met, the intermediate smart contract decompiled code will be used as the target smart contract decompiled code.

4. The smart contract decompilation code optimization method according to claim 3, characterized in that, The violation of the denial rule specifically refers to: ; Where π is the intermediate smart contract decompiled code, representing the context containing a list of variable types assigned to expression patterns; e is the expression; and θ is the variable type.

5. A smart contract decompilation code optimization system, applied to the smart contract decompilation code optimization method of claim 1, characterized in that, include: The acquisition module is used to acquire the decompiled code of the smart contract to be optimized, and to construct a contract dependency graph based on the decompiled code of the smart contract to be optimized; The extraction module is used to extract fragments from the contract dependency graph and generate variable method code context; The generation module is used to generate multiple thought chain prompts based on the contract dependency graph; The optimization module is used to optimize the decompiled code of the smart contract to be optimized by using a large language model based on the variable method code context, multiple thought chain hints, a pre-set variable type inference candidate set and a pre-set contract attribute inference candidate set, and output intermediate smart contract decompiled code; The inspection module is used to perform a consistency check on the intermediate smart contract decompiled code based on the decompiled code of the smart contract to be optimized using a pre-set program analysis and verification technology, and to determine the target smart contract decompiled code.

6. A computer device, characterized in that, The system includes a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the processor causes the processor to perform the steps of the smart contract decompilation code optimization method as described in any one of claims 1-4.

7. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed, it implements the smart contract decompilation code optimization method as described in any one of claims 1-4.

8. A computer program product, characterized in that, The computer program product includes a computer program stored on a non-transitory computer-readable storage medium, the computer program including program instructions, wherein when the program instructions are executed by a computer, the computer performs the smart contract decompilation code optimization method as described in any one of claims 1-4.

Citation Information

Patent Citations

  • Intelligent contract optimization method and device based on source codes

    CN112631656A

  • Smart contract optimization method and device and readable storage medium

    CN115657945A