Optimization method under Solidiity smart contract security arithmetic mechanism

By extracting and merging the secure numerical bytecode of Solidity smart contract and bytecode without secure arithmetic function, the problem of high cost of deployment and calling smart contracts in the existing technology is solved, and more efficient optimization results are achieved.

CN119960754APending Publication Date: 2025-05-09NANJING UNIV OF SCI & TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311502533.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-11-09
Publication Date
2025-05-09

AI Technical Summary

Technical Problem

The existing Solidity smart contract optimization methods are mainly concentrated at the source code or bytecode level, and there is a lack of optimization methods based on compiler characteristics, resulting in high cost of deployment and calling smart contracts.

Method used

By extracting the secure calculation digital bytecode of the Solidity smart contract and using the unchecked keyword to wrap the source code for compilation, it generates bytecode without the secure calculation function, and finally merges the two bytecodes to optimize the smart contract.

Benefits of technology

Without complex program analysis, this method significantly reduces the deployment and call costs of smart contracts, and is suitable for a wide range of optimization scenarios and is highly applicable.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure HDA0004544715780000011
    Figure HDA0004544715780000011
  • Figure HDA0004544715780000012
    Figure HDA0004544715780000012
  • Figure HDA0004544715780000013
    Figure HDA0004544715780000013
Patent Text Reader

Abstract

The invention discloses an optimization method under a Solidiity smart contract security arithmetic mechanism, which comprises the following steps of: compiling an input smart contract source code into a byte code by using a Solidiity compiler, and extracting a security arithmetic number node code from the byte code; the method comprises the following steps of: using each function in an intelligent contract source code input by an uncheck keyword package, and compiling the source code packaged by the uncheck keyword to obtain a byte code without a security arithmetic function; and combining the previously obtained byte codes to obtain an optimized byte code. The method provided by the invention has the advantages of effectiveness and high efficiency, and the deployment and calling cost of the smart contract can be effectively reduced with relatively low compiling overhead.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of program optimization, and specifically relates to an optimization method under the secure arithmetic mechanism of Solidity smart contracts. Background Art

[0002] With the rapid development of blockchain technology, smart contracts have been widely used on blockchain platforms. Due to their security, reliability, fairness and efficiency, smart contracts have been widely used in many fields such as finance, medical care, copyright protection, supply chain management, etc.

[0003] Currently, Solidity is one of the most widely used high-level programming languages ​​for writing smart contracts, and Ethereum is the largest blockchain platform supporting smart contract transactions. Because Solidity is a Turing-complete programming language, to prevent users from misusing computing resources by deploying malicious smart contracts to Ethereum nodes, Ethereum has introduced a gas mechanism: users pay a fee when deploying and calling smart contracts. The contract deployment fee is determined by the length of the smart contract's bytecode, while the contract call fee is determined by the specific bytecode instructions executed by the Ethereum Virtual Machine during the call.

[0004] Although many research works have proposed optimization methods for Solidity smart contracts, most of them look for some anti-patterns that can be optimized at the source code or bytecode level and replace them with equivalent patterns with lower gas consumption. There are few methods that specifically optimize based on compiler characteristics. Summary of the Invention

[0005] The purpose of this invention is to provide an effective optimization method under the Solidity smart contract security arithmetic mechanism to reduce the deployment and calling costs of smart contracts.

[0006] The technical solution to achieve the purpose of this invention is: an optimization method under the secure arithmetic mechanism of Solidity smart contracts, which takes Solidity smart contract source code as input and produces optimized bytecode as output, and the steps are as follows:

[0007] Step 1: Use the Solidity compiler to compile the input smart contract source code into bytecode and extract the secure arithmetic bytecode from the bytecode;

[0008] Step 2: Use the unchecked keyword to wrap each function in the input smart contract source code, and compile the source code wrapped with unchecked to obtain bytecode without the safe arithmetic function;

[0009] Step 3: merge the bytecodes obtained in step 1 and step 2 to obtain the optimized bytecode.

[0010] Compared with the existing technology, the present invention has the following significant advantages: (1) The present invention does not require complex program analysis, but only needs to traverse the assembly code a constant number of times, resulting in high optimization efficiency. (2) The present invention does not optimize for a specific anti-pattern, and has strong applicability. BRIEF DESCRIPTION OF THE DRAWINGS

[0011] Figure 1 This is a flowchart of the optimization method under the Solidity smart contract security arithmetic mechanism provided by the present invention.

[0012] Figure 2 This is an example of a Solidity smart contract source code with commonly used inputs.

[0013] Figure 3 Is compiled Figure 2 An example of the basic block jump graph of the resulting bytecode.

[0014] Figure 4 This is an example of extracting secure arithmetic byte codes from byte codes.

[0015] Figure 5 This is an example of preprocessing source code using the unchecked keyword.

[0016] Figure 6 is an example of merged bytecode.

[0017] Figure 7 is an example of the optimized bytecode output. DETAILED DESCRIPTION

[0018] This invention discloses an effective optimization method under the secure arithmetic mechanism of Solidity smart contract, which takes Solidity smart contract source code as input and outputs optimized bytecode. Figure 1 shown.

[0019] The specific implementation is as follows:

[0020] Step 1: Use the Solidity compiler to compile the input smart contract source code into bytecode, and extract the secure arithmetic bytecode from the bytecode. The specific steps are as follows:

[0021] Step 1-1: Use the Solidity compiler to compile the input smart contract source code to obtain the bytecode and the mapping of the instructions in the bytecode to the source code abstract syntax tree nodes;

[0022] Step 1-2: Split the bytecode into a set of basic blocks and traverse each instruction in each basic block. When there is an instruction PUSH[tag]B in basic block A that pushes the entry address of basic block B onto the stack, create a directed edge from A to B. After traversing all basic blocks, the basic block jump graph of the bytecode is obtained.

[0023] Step 1-3: Based on the mapping from bytecode instructions to source code obtained in step 1-1, traverse each instruction in the bytecode. When the instruction satisfies:

[0024] (1) This instruction is PUSH[tag]X, which pushes the entry address of basic block X into the stack.

[0025] (2) The next instruction of this instruction is the unconditional jump instruction JUMP;

[0026] (3) The abstract syntax tree node corresponding to this instruction is a binary arithmetic expression, whose type is a triple<op,l,r> , where op is an arithmetic operator, l and r are the data types of the left and right arithmetic operators respectively; starting from basic block X, a breadth-first search is performed on the basic block jump graph obtained by steps 1-2 to obtain the calculation type of<op,l,r> The set of basic blocks for safe arithmetic of arithmetic expressions and the entry basic blocks for safe arithmetic corresponding to the expression type.

[0027] Step 2: Use the unchecked keyword to wrap each function in the input source code, and compile the source code wrapped with unchecked to generate bytecode without the safe arithmetic function. The specific steps are as follows:

[0028] Step 2-1, use regular expression matching to delete all comments in the source code;

[0029] Step 2-2, traverse the source code string and replace the existing unchecked keyword with a comment symbol;

[0030] Step 2-3, traverse the source code string again. When the keyword function is encountered, search rightward for the nearest left curly brace '{' and the matching '}' symbol. Insert the unchecked keyword between these two symbols to wrap the entire function.

[0031] Steps 2-4 compile the source code wrapped with the unchecked keyword to obtain optimized bytecode that does not contain safe arithmetic functions.

[0032] Step 3: Merge the bytecodes obtained in step 1 and step 2 to get the optimized bytecode. The specific steps are as follows:

[0033] Step 3-1: copy the secure arithmetic bytecode obtained in step 1 to the optimized bytecode without the secure arithmetic function obtained in step 2;

[0034] Step 3-2: traverse each basic block in the bytecode that does not contain safe arithmetic functions, and denote this basic block as B. When encountering arithmetic operation instructions ADD, MUL, and SUB, and the arithmetic operation instruction corresponds to an abstract syntax tree node of a binary arithmetic operation and the node is not surrounded by a comment symbol in the source code, split B into B' and B" with the arithmetic operation instruction as the split point;

[0035] Step 3-3: Use the constructed instruction sequence pattern to replace the arithmetic operation instructions. The specific replacement rules are as follows:

[0036] (3) ADD and MUL instructions use the instruction sequence {PUSH[tag]B",SWAP2,PUSH[tag]

[0037] SA, JUMP} replacement;

[0038] (4) The SUB instruction uses the instruction sequence {PUSH[tag]B",SWAP2,SWAP1,PUSH[tag]

[0039] SA, JUMP} replacement;

[0040] like<op,l,r> is the arithmetic expression type of the abstract syntax tree node corresponding to the current arithmetic operation instruction, then SA is<op,l,r> The corresponding entry basic block of the secure arithmetic has been obtained in step 1. After traversing all basic blocks and completing the replacement, the final optimized bytecode is obtained.

[0041] Example

[0042] The present invention is an optimization method for the secure arithmetic mechanism of Solidity smart contracts. Based on the idea of ​​bytecode extraction and merging, the present invention compiles a Solidity smart contract source code twice, extracts the secure arithmetic bytecode from the bytecode obtained in the first compilation, and then uses the unchecked keyword to wrap the source code for a second compilation to generate bytecode without secure arithmetic function and optimize it using the compiler. Finally, the two bytecodes are merged to achieve the purpose of reducing the cost of contract deployment and calling while retaining the secure arithmetic function. The specific workflow is as follows: Figure 1As shown in the figure, first, the input Solidity source code is compiled to generate bytecode and the safe arithmetic bytecode is extracted from the bytecode based on the mapping from bytecode to the source code abstract syntax tree nodes; then, the source code is preprocessed using the unchecked keyword and the preprocessed source code is compiled to obtain the bytecode without safe arithmetic; finally, the safe arithmetic bytecode and the bytecode without safe arithmetic are merged.

[0043] In conjunction with the example, the method includes:

[0044] Step 1: Use the Solidity compiler to compile the input smart contract source code into bytecode, and extract the secure arithmetic bytecode from the bytecode. The specific steps are as follows:

[0045] Step 1-1, use the Solidity compiler to compile the Solidity source code into bytecode, Figure 2 For input source code instance;

[0046] Step 1-2: Split the bytecode into a set of basic blocks, traverse each instruction in each basic block, and when there is an instruction PUSH[tag]B in basic block A that pushes the entry address of basic block B onto the stack, create a directed edge from A to B. After traversing all basic blocks, the basic block jump graph of the bytecode is obtained. Figure 3 Shows compilation Figure 2 The basic block jump diagram obtained after the source code.

[0047] Step 1-3: Based on the mapping from bytecode instructions to source code obtained in step 1-1, traverse each instruction in the bytecode. When the instruction satisfies:

[0048] (1) This instruction is PUSH[tag]X, which pushes the entry address of basic block X into the stack.

[0049] (2) The next instruction of this instruction is the unconditional jump instruction JUMP;

[0050] (3) The abstract syntax tree node corresponding to this instruction is a binary arithmetic expression, whose type is a triple<op,l,r> , where op is the arithmetic operator, l and r are the data types of the left and right arithmetic respectively;

[0051] Starting from basic block X, a breadth-first search is performed on the basic block jump graph obtained from steps 1-2 to obtain the calculation type of<op,l,r> The set of basic blocks for safe arithmetic of arithmetic expressions and the entry basic blocks for safe arithmetic corresponding to the expression type. Figure 4The following figure shows the situation when traversing to basic block tag 7. The instruction PUSH[tag]12 corresponds to the arithmetic expression balance[y]+x, which is a triplet of type <+,uint256,uint256>. Tag 12 is the entry basic block of the arithmetic expression of type <+,uint256,uint256>. Figure 3 A breadth-first search on the basic block jump graph shown can traverse to basic blocks tag 12 and tag 18.

[0052] Step 2: Use the unchecked keyword to wrap each function in the input source code, and compile the source code wrapped with unchecked to generate bytecode without the safe arithmetic function. The specific steps are as follows:

[0053] Step 2-1, use regular expression matching to delete all comments in the source code;

[0054] Step 2-2, traverse the source code string and replace the existing unchecked keyword with a comment symbol;

[0055] Step 2-3, traverse the source code string again. When the keyword function is encountered, search rightward for the nearest left curly brace '{' and the matching '}' symbol. Insert the unchecked keyword between these two symbols to wrap the entire function. The final source code is as follows Figure 5 As shown;

[0056] Step 2-4, compile Figure 5 The source code in is optimized to obtain bytecode that does not contain safe arithmetic functions.

[0057] Step 3: Merge the bytecodes obtained in step 1 and step 2 to obtain the optimized bytecode. The specific steps are as follows:

[0058] Step 3-1, copy the safe arithmetic byte code obtained in step 1 to the optimized byte code without safe arithmetic function obtained in step 2, and copy the basic blocks 12 and 18 obtained in step 1 to the compiled Figure 5 The bytecode is obtained later.

[0059] Step 3-2: traverse each basic block in the bytecode that does not contain safe arithmetic functions. Let this basic block be B. When encountering arithmetic operation instructions ADD, MUL, and SUB, and the arithmetic operation instruction corresponds to an abstract syntax tree node for a binary arithmetic operation and the node is not surrounded by a comment symbol in the source code, split B into B' and B". Figure 6The ADD instruction that meets the conditions in basic block tag 5 is displayed, and tag 5 is split into two basic blocks tag5 and tag 6.

[0060] Step 3-3: Use the constructed instruction sequence pattern to replace the arithmetic operation instructions. The specific replacement rules are as follows:

[0061] (5) ADD and MUL instructions use the instruction sequence {PUSH[tag]B",SWAP2,PUSH[tag]

[0062] SA, JUMP} replacement;

[0063] (6) The SUB instruction uses the instruction sequence {PUSH[tag]B",SWAP2,SWAP1,PUSH[tag]

[0064] SA, JUMP} replacement;

[0065] like<op,l,r> is the arithmetic expression type of the abstract syntax tree node corresponding to the current arithmetic operation instruction, then SA is<op,l,r> The corresponding secure arithmetic entry basic block, obtained in step 1, is obtained by traversing all basic blocks and completing the replacement to obtain the final optimized bytecode. For this example, the ADD instruction in tag 6 is replaced with the instruction sequence {PUSH[tag]6,SWAP2,PUSH[tag]12,JUMP}.

[0066] Figure 7 Shows the final optimized bytecode output.

[0067] The present invention takes the Solidity smart contract source code as input and takes the optimized bytecode as output. Solidity's safe arithmetic mechanism means that the Solidity compiler will generate corresponding arithmetic operation bytecode with overflow checking function for the arithmetic operation in the source code. However, this code generation method will cause the same arithmetic expression to be scattered in different basic blocks, so that the compiler will miss some optimization opportunities, resulting in increased contract deployment and call costs. In order to reduce the deployment and execution costs of smart contracts under the safe arithmetic mechanism, the present invention is based on the idea of ​​bytecode extraction and merging, compiles a Solidity smart contract source code twice, extracts the safe arithmetic bytecode from the bytecode obtained by the first compilation, and then uses the unchecked keyword to preprocess the source code for a second compilation to generate bytecode without safe arithmetic function and uses the compiler to optimize it. Finally, the two bytecodes are merged to achieve the purpose of reducing the contract deployment and call costs while retaining the safe arithmetic function.

Claims

1. An optimization method under the secure arithmetic mechanism of Solidity smart contract, characterized in that: Taking the Solidity smart contract source code as input and the optimized bytecode as output, the specific steps are as follows: Step 1: Use the Solidity compiler to compile the input smart contract source code into bytecode, and extract the secure arithmetic bytecode from the bytecode; Step 2: Use the unchecked keyword to wrap each function in the input smart contract source code, and compile the source code wrapped by unchecked to obtain bytecode without secure arithmetic functions; Step 3: merge the bytecodes obtained in step 1 and step 2 to obtain optimized bytecodes.

2. The optimization method under the Solidity smart contract security arithmetic mechanism according to claim 1, characterized in that: In step 1, use the Solidity compiler to compile the input smart contract source code into bytecode, and extract the secure arithmetic byte code from the bytecode. The specific steps are as follows: Step 1-1, use the Solidity compiler to compile the input smart contract source code to obtain the bytecode and the mapping of the instructions in the bytecode to the source code abstract syntax tree nodes; Step 1-2, split the bytecode into a set of basic blocks, traverse each instruction in each basic block, and when there is an instruction PUSH[tag]B in any basic block A that pushes the entry address of another basic block B onto the stack, establish a directed edge from basic block A to basic block B, and after traversing all basic blocks, obtain the basic block jump graph of the bytecode; Step 1-3, according to the mapping of bytecode instructions to source code obtained in step 1-1, traverse each instruction in the bytecode, when the instruction satisfies both: (1) This instruction is PUSH[tag]X, which pushes the entry address of basic block X into the stack; (2) The next instruction of this instruction is an unconditional jump instruction JUMP; (3) The abstract syntax tree node corresponding to this instruction is a binary arithmetic expression, whose type is a triple<op,l,r> , where op is an arithmetic operator, l and r are the data types of the left and right arithmetic operators respectively; starting from basic block X, a breadth-first search is performed on the basic block jump graph obtained by step 1-2 to obtain the calculation type<op,l,r> The set of basic blocks for safe arithmetic of arithmetic expressions and the entry basic blocks for safe arithmetic corresponding to the expression type.

3. The optimization method under the Solidity smart contract security arithmetic mechanism according to claim 1 is characterized in that: In step 2, the unchecked keyword is used to wrap each function in the input source code. Compiling the source code wrapped by unchecked includes the following steps: Step 2-1, use regular expression matching to delete all comments in the source code; Step 2-2, traverse the source code string and replace the existing unchecked keyword with a comment symbol; Step 2-3, traverse the source code string again, and when encountering the keyword function, look to the right for the nearest left curly brace '{' and the matching '}' symbol, and insert the unchecked keyword between these two symbols to wrap the entire function; Step 2-4, compile the source code wrapped with the unchecked keyword to obtain optimized bytecode that does not contain safe arithmetic functions.

4. The optimization method under the Solidity smart contract security arithmetic mechanism according to claim 1, characterized in that: In step 3, the bytecodes obtained by merging steps 1 and 2 are combined to obtain bytecodes that can be deployed and called. The specific steps are as follows: Step 3-1, copy the secure arithmetic byte code obtained in step 1 to the optimized byte code without secure arithmetic function obtained in step 2; Step 3-2, traverse each basic block in the bytecode that does not contain the safe arithmetic function, and let the basic block be B. When encountering arithmetic operation instructions ADD, MUL and SUB, and the arithmetic operation instruction corresponds to an abstract syntax tree node of a binary arithmetic operation and the node is not wrapped by a comment symbol in the source code, B is split into B' and B" with the arithmetic operation instruction as the split point; Step 3-3, use the constructed instruction sequence pattern to replace the arithmetic operation instructions. The specific replacement rules are as follows: (1) ADD and MUL instructions are replaced by the instruction sequence {PUSH[tag]B”, SWAP2, PUSH[tag]SA, JUMP}; (2) The SUB instruction is replaced by the instruction sequence {PUSH[tag]B”,SWAP2,SWAP1,PUSH[tag]SA,JUMP}; like<op,l,r> is the arithmetic expression type of the abstract syntax tree node corresponding to the current arithmetic operation instruction, then SA is<op,l,r> The corresponding entry basic block of secure arithmetic has been obtained in step 1. After traversing all basic blocks and completing the replacement, the final optimized bytecode is obtained.