CN116149671A - Method and device for translating intelligent contract language and electronic equipment - Google Patents

Method and device for translating intelligent contract language and electronic equipment Download PDF

Info

Publication number
CN116149671A
CN116149671A CN202310437229.0A CN202310437229A CN116149671A CN 116149671 A CN116149671 A CN 116149671A CN 202310437229 A CN202310437229 A CN 202310437229A CN 116149671 A CN116149671 A CN 116149671A
Authority
CN
China
Prior art keywords
function
syntax tree
abstract syntax
data type
nodes
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202310437229.0A
Other languages
Chinese (zh)
Other versions
CN116149671B (en
Inventor
许颖
康跃馨
陈�胜
夏清
方明哲
吴志林
李玉成
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Institute of Software of CAS
Original Assignee
Institute of Software of CAS
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Institute of Software of CAS filed Critical Institute of Software of CAS
Priority to CN202310437229.0A priority Critical patent/CN116149671B/en
Publication of CN116149671A publication Critical patent/CN116149671A/en
Application granted granted Critical
Publication of CN116149671B publication Critical patent/CN116149671B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/42Syntactic analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F21/00Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
    • G06F21/50Monitoring users, programs or devices to maintain the integrity of platforms, e.g. of processors, firmware or operating systems
    • G06F21/55Detecting local intrusion or implementing counter-measures
    • G06F21/56Computer malware detection or handling, e.g. anti-virus arrangements
    • G06F21/562Static detection
    • G06F21/563Static detection by source code analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F21/00Security arrangements for protecting computers, components thereof, programs or data against unauthorised activity
    • G06F21/50Monitoring users, programs or devices to maintain the integrity of platforms, e.g. of processors, firmware or operating systems
    • G06F21/57Certifying or maintaining trusted computer platforms, e.g. secure boots or power-downs, version controls, system software checks, secure updates or assessing vulnerabilities
    • G06F21/577Assessing vulnerabilities and evaluating computer system security
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F40/00Handling natural language data
    • G06F40/40Processing or translation of natural language
    • G06F40/58Use of machine translation, e.g. for multi-lingual retrieval, for server-side translation for client devices or for real-time translation
    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computer Security & Cryptography (AREA)
  • Computer Hardware Design (AREA)
  • Software Systems (AREA)
  • General Physics & Mathematics (AREA)
  • Physics & Mathematics (AREA)
  • Health & Medical Sciences (AREA)
  • General Health & Medical Sciences (AREA)
  • Virology (AREA)
  • Computing Systems (AREA)
  • Artificial Intelligence (AREA)
  • Audiology, Speech & Language Pathology (AREA)
  • Computational Linguistics (AREA)
  • Devices For Executing Special Programs (AREA)

Abstract

A method and apparatus, an electronic device, a computer-readable storage medium for translating a smart contract language, the method comprising generating a normalized abstract syntax tree in response to a source code written in the smart contract language being loaded; translating the composite data type expression node in the abstract syntax tree according to the type definition of the intelligent contract language; translating function nodes in the abstract syntax tree according to the function definition of the intelligent contract language; and translating other nodes in the abstract syntax tree according to the source language specification of the trusted compiling tool, wherein the other nodes comprise nodes except the composite data type expression node and the function node in the abstract syntax tree. According to the embodiment of the application, the translation of the program written in the intelligent contract language supporting the composite data type and the multi-return value function is realized, and formal verification of the composite data type and the function translation process is realized.

Description

Method and device for translating intelligent contract language and electronic equipment
Technical Field
The present application relates to the field of blockchain technology, and in particular, to a method and apparatus for translating intelligent contract languages, an electronic device, and a computer readable storage medium.
Background
The intelligent contract is program code running on the blockchain system, and the intelligent contract is high in modification cost after being uplinked due to the non-tamperable characteristic of the blockchain, and once the contract has security holes, serious economic loss can be caused. Therefore, ensuring the security of smart contracts is critical.
The inventor finds that the existing technical scheme generally uses the existing formal verification tool to prove whether the contract meets the expected security attribute, including whether overflow problems, reentrant loopholes and the like exist or not, but the compiling process of the intelligent contract code is lack of formal verification, so that the possibility of incorrect compiling exists, and the correctness of the target program cannot be completely ensured. The problem can be solved by using an theorem proving tool to prove the correctness of the translation process and interfacing with a trusted compiling tool.
In order to effectively apply intelligent contracts in a blockchain network, besides basic data types and function call functions, some common grammatical features enable users to write contract programs more conveniently, such as compound data types and built-in functions thereof, and function support multiple return values. In addition to this, programming of the smart contract language also requires state variables to permanently record the state of the current contract.
Therefore, the composite data type expression node, function node and other nodes in the source code written in the intelligent contract language supporting the grammar characteristics need to be translated and proved so as to generate the program conforming to the source language specification of the trusted compiling tool, thereby realizing the trusted compiling chain of the intelligent contract language and effectively guaranteeing the safety of the intelligent contract.
Disclosure of Invention
The application aims at providing a method and device for translating intelligent contract language, electronic equipment and a computer readable storage medium, translating programs written in the intelligent contract language supporting composite data types and multi-return value functions, and formally verifying the translation process.
According to an aspect of the present application, there is provided a method for translating a smart contract language, comprising: generating a normalized abstract syntax tree in response to the loaded source code written in the smart contract language; translating the composite data type expression node in the abstract syntax tree according to the type definition of the intelligent contract language; translating function nodes in the abstract syntax tree according to the function definition of the intelligent contract language; and translating other nodes in the abstract syntax tree according to the source language specification of the trusted compiling tool, wherein the other nodes comprise nodes except the composite data type expression node and the function node in the abstract syntax tree.
According to some embodiments, translating the compound data type expression node in the abstract syntax tree according to a type definition of the smart contract language comprises: traversing the abstract syntax tree to obtain the composite data type of the composite data type expression node; generating a built-in function of the composite data type; translating the built-in method statement of the composite data type into a function call statement for calling the built-in function; the composite data type is translated into a data type that complies with the source language specification of the trusted compilation tool.
According to some embodiments, translating function nodes in the abstract syntax tree according to a function definition of the smart contract language comprises: merging the output parameter expression in the function call statement in the function into the input parameter expression; the output parameters in the structural definition of the function are incorporated into the input parameters.
According to some embodiments, the method further comprises: and carrying out formal verification on the translation process of the source code written in the intelligent contract language.
According to some embodiments, formalized verification of a translation process of source code written in the smart contract language includes: performing formal verification on the obtained composite data type; performing formal verification on the built-in function for generating the composite data type; translating the built-in method statement of the composite data type into a function call statement for calling the built-in function for formal verification; formalized verification is carried out on the composite data type translated into the data type conforming to the source language specification of the trusted compiling tool; combining the output parameter expressions in the function call statement in the function into the input parameter expressions for formal verification; combining the output parameters in the structural definition of the function into the input parameters for formal verification; and/or performing formal verification on other nodes in the abstract syntax tree, wherein the other nodes comprise nodes except function nodes and expression nodes in the abstract syntax tree.
According to some embodiments, generating a normalized abstract syntax tree in response to the loaded source code written in the smart contract language comprises: performing lexical analysis on the source codes according to word forming rules of the intelligent contract language to generate lexical unit sequences; combining the lexical unit sequences into a grammar phrase according to the grammar rules of the intelligent contract language, thereby generating the abstract grammar tree; normalizing the abstract syntax tree, thereby generating the normalized abstract syntax tree.
According to some embodiments, normalizing the abstract syntax tree, thereby generating the normalized abstract syntax tree comprises: preprocessing the abstract syntax tree; performing static type checking on nodes in the abstract syntax tree; normalizing compound data type expression nodes, function nodes, and other nodes of the abstract syntax tree to generate the normalized abstract syntax tree.
According to an aspect of the present application, there is provided an apparatus for translating a smart contract language, including: an abstract syntax tree generating unit for generating a normalized abstract syntax tree in response to the loaded source code written in the intelligent contract language; the composite data type expression translation unit translates the composite data type expression nodes in the abstract syntax tree according to the type definition of the intelligent contract language; a function translation unit, which translates function nodes in the abstract syntax tree according to the function definition of the intelligent contract language; and the abstract syntax tree node specification unit is used for translating other nodes in the abstract syntax tree according to the source language specification of the trusted compiling tool, wherein the other nodes comprise nodes except the composite data type expression node and the function node in the abstract syntax tree.
According to some embodiments, the apparatus further comprises: and the formal verification unit is used for formally verifying the translation process in the composite data type expression translation unit, the function translation unit and/or the abstract syntax tree node specification unit.
According to an aspect of the present application, there is provided an electronic device, including: a memory, a processor; wherein the memory stores one or more computer instructions that when executed by the processor implement a method as described in any preceding claim.
According to an aspect of the present application, a computer-readable storage medium is presented, characterized in that a computer program is stored thereon, which, when executed, implements the method of any of the preceding claims.
According to some embodiments of the present application, translation of a program written in an intelligent contract language supporting a composite data type and multiple return value functions is achieved by finding the composite data type from the intelligent contract code and generating a built-in function of the composite data type, and translating built-in method statements of the composite data type into function call statements that call the built-in function, and by incorporating output parameter expressions in the function call statements into input parameter expressions and output parameters in the structural definition of the function into the input parameters.
According to other embodiments, by formal verification of the composite data type and function translation process, trusted compiling of the intelligent contract language is achieved, and safety of compiling of the intelligent contract language is guaranteed.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the application.
Drawings
In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings that are required to be used in the description of the embodiments will be briefly described below. The above and other objects, features and advantages of the present application will become more apparent by describing in detail exemplary embodiments thereof with reference to the attached drawings.
FIG. 1 illustrates a flow chart of a method for translating the smart contract language according to an example embodiment of the present application.
FIG. 2 illustrates a flow chart of a method of generating a normalized abstract syntax tree according to an example embodiment of the present application.
FIG. 3 illustrates a method flow diagram for formally validating a translation process of the smart contract language, according to an example embodiment of the present application.
FIG. 4 illustrates a block diagram of an apparatus for translating the smart contract language, according to an example embodiment of the present application.
FIG. 5 illustrates another apparatus block diagram for translating the smart contract language, according to an example embodiment of the present application.
Fig. 6 illustrates an electronic device according to an exemplary embodiment of the present application.
Detailed Description
Example embodiments will now be described more fully with reference to the accompanying drawings. However, the exemplary embodiments can be embodied in many forms and should not be construed as limited to the embodiments set forth herein; rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the concept of the example embodiments to those skilled in the art. The same reference numerals in the drawings denote the same or similar parts, and thus a repetitive description thereof will be omitted.
The described features, structures, or characteristics may be combined in any suitable manner in one or more embodiments. In the following description, numerous specific details are provided to give a thorough understanding of embodiments of the disclosure. One skilled in the relevant art will recognize, however, that the disclosed aspects may be practiced without one or more of the specific details, or with other methods, components, materials, devices, operations, etc. In these instances, well-known structures, methods, devices, implementations, materials, or operations are not shown or described in detail.
The flow diagrams depicted in the figures are exemplary only, and do not necessarily include all of the elements and operations/steps, nor must they be performed in the order described. For example, some operations/steps may be decomposed, and some operations/steps may be combined or partially combined, so that the order of actual execution may be changed according to actual situations.
The terms first, second and the like in the description and in the claims of the present application and in the above-described figures, are used for distinguishing between different objects and not for describing a particular sequential order. Furthermore, the terms "comprise" and "have," as well as any variations thereof, are intended to cover a non-exclusive inclusion. For example, a process, method, system, article, or apparatus that comprises a list of steps or elements is not limited to only those listed steps or elements but may include other steps or elements not listed or inherent to such process, method, article, or apparatus.
Intelligent contracts are program code that runs on top of a blockchain system that can automatically and trustfully execute business logic without relying on third party trust. With the widespread use of blockchain technology in numerous fields, smart contracts bear a great deal of value. In order to effectively apply intelligent contracts in a blockchain network, some common grammatical features are needed, such as compound data types and built-in functions thereof, and function support multiple return values, so that a user can write contract programs more conveniently.
Formal verification techniques rely on the tightness of mathematical logic reasoning, an effective means of ensuring the security of intelligent contracts. Theorem proving techniques in formalizing methods are capable of describing and validating formalized specifications of a computer system or software through logical languages and proving sequences to ensure that programs meet the specifications. The intelligent contract compiling process is verified by adopting the interactive theorem proving technology, so that contract codes can be guaranteed not to be wrongly compiled, the semantics of the source program are kept to the target program, the safety of the translating process is guaranteed, and trusted compiling is realized.
The inventors have found that existing solutions typically use existing formal verification tools to verify the functionality of the smart contract, to verify whether the smart contract meets the expected security attributes, e.g., whether there is an overflow problem, a reentrant vulnerability, etc. For example, intelligent contract code written in the solubility language is translated into Boogie intermediate language code, and then the Boogie code is automatically verified by a verifier. The technical scheme has the defects that the translation process from the intelligent contract code to the intermediate language code is not subjected to formal verification, the possibility of incorrect compiling exists, and the correctness of the target program cannot be completely ensured.
According to some embodiments of the present application, translation of a program written in an intelligent contract language supporting a composite data type and multiple return value functions is achieved by finding the composite data type from the intelligent contract code and generating a built-in function of the composite data type, and translating built-in method statements of the composite data type into function call statements that call the built-in function, and by incorporating output parameter expressions in the function call statements into input parameter expressions and output parameters in the structural definition of the function into the input parameters.
According to other embodiments, by formal verification of the composite data type and function translation process, trusted compiling of the intelligent contract language is achieved, and safety of compiling of the intelligent contract language is guaranteed.
Before describing embodiments of the present application, terms appearing in the present application are first explained.
Composite data type: as opposed to the base data type, refers to a type defined based on other types. In this application, composite data types include strings, options, lists, mappings, constructs.
Grammar: grammar refers to the structural rules of a language.
Semantics: semantics refer to the actual meaning attached to the language structure.
Operation semantics: is the semantics of a program defined by describing the effects of the program grammar construction on the machine.
Semantic execution: the process by which the program described by the operation semantics runs on an abstract machine, with the state of the machine (e.g., memory) changing.
Semantic evaluation: during semantic execution, the value of the expression of the program at the current machine state is calculated.
Specific embodiments according to the present application will be described in detail below with reference to the accompanying drawings.
Fig. 1 shows a flowchart of a method for translating a smart contract language according to an exemplary embodiment of the present application, and a method for translating a smart contract language according to an exemplary embodiment of the present application will be described in detail below with reference to fig. 1 as an example.
As shown in fig. 1, in step S101, a normalized abstract syntax tree is generated in response to the source code written in the smart contract language being loaded.
According to the embodiment of the application, at step S101, lexical and grammatical analysis is performed on the source code written according to the loaded smart contract language, so as to convert the smart contract code into an abstract syntax tree. Wherein each node of the abstract syntax tree represents a structure, e.g., a function, a statement, or an expression, in the smart contract code.
According to other embodiments, after the abstract syntax tree is obtained in step S101, normalization processing is further required for the abstract syntax tree, including preprocessing the abstract syntax tree, static type checking, and normalization processing for the composite data type and function, so as to obtain a normalized abstract syntax tree.
In step S103, the compound data type expression node in the abstract syntax tree is translated according to the type definition of the smart contract language.
And in step S103, according to the type definition of the intelligent contract language, translating the expression in the abstract syntax tree to translate the composite data type in the source code into the data type conforming to the source language specification of the trusted compiling tool.
According to an embodiment of the present application, for an intelligent contract comprising composite data types, step S103 is required to translate the composite data types in the source code into data types that conform to the source language specification of the trusted compilation tool.
In a specific implementation, the translation of the composite data type may be accomplished by the following steps.
First, in step S1031, the abstract syntax tree obtained in step S101 is traversed to obtain a composite data type of the composite data type expression node.
For example, all global variables, functions, statements, and expression nodes in the abstract syntax tree are traversed to find the composite data type therein. Wherein the found composite data type also includes composite data types nested within one composite data type.
Then, a built-in function of the composite data type is generated at step S1033.
For example, for all the compound data type expressions found in step S1031, the built-in functions required for each compound data type are generated. The built-in functions comprise an initialization function of the composite data type, a valuation function, a function of judging whether character strings are equal, a function of adding, acquiring and modifying list elements and/or a function of acquiring mapping corresponding elements.
Then, in step S1035, the built-in method statement of the composite data type is translated into a function call statement for calling the built-in function.
For example, the built-in method statement of the composite data type is translated into a call to the built-in function generated in step S1035.
Finally, in step S1037, the composite data type is translated into a data type that complies with the source language specification of the trusted compilation tool.
For example, translate the composite data type into a Struct type in a subset of the C language supported by a trusted compilation tool (CompCert), add a composite environment to the global environment that contains a mapping of composite data type definitions.
In step S105, the function nodes in the abstract syntax tree are translated according to the function definition of the smart contract language.
In step S105, according to the function definition of the smart contract language, the functions in the abstract syntax tree are translated to translate the multi-return value function structure in the source code into a single-return value function structure conforming to the source language specification of the trusted compilation tool.
In a specific implementation, in step S105, the translation of the function node may be implemented as follows.
Step S1051: and merging the output parameter expression in the function call statement in the function into the input parameter expression.
For example, when translating a function call statement, an input parameter expression and an output parameter expression originally recorded in the statement of the function are connected by using a list connection symbol, and a list in which the types of the input parameter expression and the output parameter expression are recorded is connected by using a type list connection symbol as an input parameter expression of a new function call statement, and the list is used as a parameter type of the new function call statement.
Step S1053: the output parameters in the structural definition of the function are incorporated into the input parameters.
For example, in translating a function, the list of input parameters and the list of output parameters are concatenated using list concatenation symbols, and the return value is taken as the input parameter of the function.
According to some embodiments, steps S1051 and S1053 need to be implemented in the same translation stage.
In step S107, other nodes in the abstract syntax tree are translated according to the source language specification of the trusted compilation tool. Wherein the other nodes include nodes in the abstract syntax tree except for complex data type expression nodes and function nodes.
For example, the memory allocation statement is translated into a statement that invokes an external malloc function, and an external function statement is generated and inserted into the abstract syntax tree, ultimately completing the translation of the intelligent contract language program to the source language program of the trusted compilation tool.
According to the embodiment shown in fig. 1, the translation of a program written in an intelligent contract language supporting a composite data type and a multi-return value function is achieved by finding the composite data type from the intelligent contract code and generating a built-in function of the composite data type, and translating a built-in method statement of the composite data type into a function call statement that calls the built-in function, and by incorporating an output parameter expression in the function call statement into an input parameter expression and an output parameter in a structural definition of the function into the input parameter.
Fig. 2 shows a flowchart of a method for generating a normalized abstract syntax tree according to an exemplary embodiment of the present application, and in step S201, lexical analysis is performed on the source code according to word forming rules of the smart contract language to generate a lexical unit sequence, as shown in fig. 2.
For example, a lexical unit sequence is generated for the smart contract code by scanning and recognizing legal morphemes in the smart contract code according to the word forming rule.
In step S203, the lexical unit sequences are combined into a grammar phrase according to the grammar rules of the smart contract language, thereby generating the abstract syntax tree.
For example, based on lexical analysis, sequences of lexical elements are combined into grammatical phrases according to grammatical rules, thereby being converted into abstract grammatical trees. Each node of the abstract syntax tree represents a structure in the source program, including programs, functions, statements, expressions, etc.
It should be noted that, in the following translation stage of the smart contract code, on the basis of traversing the abstract syntax tree, nodes or structures of the abstract syntax tree are modified to generate a new abstract syntax tree.
In step S205, the abstract syntax tree is normalized, thereby generating the normalized abstract syntax tree.
In a specific embodiment, step S205 includes implementation by the following steps.
First, in step S2051, the abstract syntax tree is preprocessed.
For example, assignment checking of constants, syntax sugar translation, invariance checking of data structures at loop time, and/or function loop recursive call checking are prohibited.
Then, in step S2053, a static type check is performed on the nodes in the abstract syntax tree.
For example, type derivation, checking and labeling are performed on expressions that have not yet been labeled with types, and it is checked whether the type of each expression meets the rules for type matching. For example, the element types of List type expressions must be the same, and errors are reported if they do not match.
Finally, at step S2055, composite data type expressions and functions of the abstract syntax tree are normalized to generate the normalized abstract syntax tree.
For example, if the expression relates to a function call of a compound data type, a corresponding built-in method statement is generated and inserted into the abstract syntax tree in logical order to facilitate the conversion of the built-in function afterwards.
For another example, an initialization statement is generated for a local variable in a function and inserted into an abstract syntax tree.
For another example, string constants are translated into elements that are global variables, e.g., string constants are translated into elements that are global variables of the type of an array of Char types in the C language.
For another example, a state variable is translated into a global variable, and its data type is still the corresponding type when the state variable is declared.
For another example, a parameter expression in a function call statement is translated, if it is a simple data type, without changing the expression, otherwise the expression is translated into an expression of its pointer.
According to some embodiments, when generating a normalized abstract syntax tree according to the method shown in fig. 2, an initialization statement is generated for a local variable in a function and inserted into the abstract syntax tree and a state variable is translated into a global variable as an operation that must be performed.
According to the embodiment shown in fig. 2, lexical and grammatical analysis is performed using smart contract codes to generate an abstract syntax tree. And further, the abstract syntax tree is subjected to standardization processing to obtain a standardized abstract syntax tree, so that the abstract syntax tree can be conveniently and subsequently translated.
According to other embodiments of the present application, in order to implement trusted compiling of the smart contract language, security of the compiling of the smart contract language is ensured, and formal verification of the composite data type and function translation process is also required. As shown in fig. 3, a flowchart of a method for formally verifying a translation process of an intelligent contract according to an exemplary embodiment of the present application, according to some embodiments, each of the verification stages shown in fig. 3 starts from a semantic retention theorem of program execution, where the theorem may be described as that, in the same memory state, the semantic execution of any one function is consistent with a semantic environment (including a global environment and a local environment) and a change of the memory state before and after the translation. The proving process is naturally proving inwards layer by layer according to the translation function and the definition of the program, the function and the statement to the expression. The translation function is a tool for translating one grammar tree into another grammar tree, and comprises a program translation function, a function translation function and a sentence translation function.
A method for formal verification of a translation process of an intelligent contract according to an exemplary embodiment of the present application is described below with reference to fig. 3.
As shown in fig. 3, in step S301, formal verification is performed on the obtained composite data type.
This stage needs to prove if all the compound data types present in the program are correctly found. This stage saves only the definitions of the found compound data types and does not change the semantics before and after the translation stage.
Step S301 is a proof of the foregoing step S1031, and the proof idea of step S301 is that: the correct properties for traversal from program to expression are defined similar to step S301. In addition, since there may be multiple compound types in a compound type of an expression, the correct nature of finding the compound data type is defined, and generalized definition is performed, similar to a multi-way tree, in which non-leaf nodes are compound types and leaf nodes are simple types. Since the abstract syntax tree is normalized, the expression definition itself is typed, so that it is stated that all compound types must be found as long as the correct nature of traversal and the correct nature of parsing compound types are satisfied.
According to some embodiments, the program translation function is developed at step S301, and the classification proof and the induction proof are performed according to the property definition.
In step S303, a formal verification is performed on the built-in function that generated the composite data type.
Step S303 is a proof of the foregoing step S1033.
Because no function call statement is generated before and after the translation stage to call the just-generated built-in function. Therefore, the generation of the built-in function does not affect the semantic execution and is easy to directly obtain the evidence.
According to some embodiments, this stage may result in a secondary lemma: for all compound data types found in the translation process, and any built-in method statements supported by the smart contract on this type, there must be a corresponding correct function definition in memory after the translation is completed in step S1035. That is, this stage generates all the expected built-in functions. The demonstration of this auxiliary quotation is simple, as any complex type is a known type and the built-in methods are naturally known, and therefore, a discussion is classified on each type and each method, thereby proving.
In step S305, the built-in method statement of the composite data type is translated into a function call statement for calling the built-in function for formal verification.
Step S305 is the certification of step S1035 described above, and requires the translation of the built-in method statement into the function call statement.
The built-in method statement is one of the statements, and is translated into a corresponding function call statement, which is aimed at the built-in function generated in step S1033, through step S1035.
According to some embodiments, in step S305, it is necessary to prove that, in the environment corresponding to the program translation, executing any built-in function of composite data type is identical to the modification of the environment and the memory state when executing the function call statement that calls the corresponding built-in function generated in step S1033. The definition of the semantic rules of the built-in method sentences is consistent with the operation of the built-in functions on the expression by sentence.
According to some embodiments, in step S305, the auxiliary quotation described in step S303 is applied to obtain a correct definition of the built-in function, and then the execution operation of the semantic rule is proved to be the same every time the environment is changed by using the statement of the built-in function.
In step S307, formal verification is performed on the translation of the composite data type into a data type that complies with the source language specification of the trusted compilation tool.
Step S307 is a proof of the previous step S1037 that requires a translation of the proof composite data type.
According to some embodiments, the method shown in fig. 2 is to translate the smart contract code into the C language supported by the CompCert compiler, where before translation, sizeof and alignnof functions have a certain size according to the composition of the composite data type, after translation, the global environment is augmented with a composite environment, and the CompCert defined sizeof and alignnof functions require the use of a data structure of the composite data type definition composition in the composite environment. Therefore, in step S307, it is necessary to prove the equivalence of the two functions before and after the addition of the composite environment.
The proof thinking is that firstly, the proof is generalized on the types, each type is discussed according to the general correctness of the static check in step S2053, and then the theorem build_composition_env_feature of CompCert is applied to obtain the corresponding composition of the currently discussed type in the composite environment.
Then, the two conditions of success or failure of the construction environment are discussed on the translated construction composite environment function build_composition_env by using an eq_refl and case strategy, and the contradiction of the failure condition is easy to be obtained. In case of success, a specific construction of the composite environment is obtained.
Thus, the relationship between the compound of any compound type and the compound environment meets the definition of "consistency" of CompCert, and the specific alignment value of the type can be calculated on the compound, which proves to be equivalent to the fixed alignment before translation.
In step S309, formal verification is performed on the combination of the output parameter expressions in the function call statement in the function into the input parameter expressions.
Step S309 is a proof of the aforementioned step S1051.
When the semantic maintainability of the function call statement is proved, semantic evaluation needs to be carried out on the input parameter expression and the output parameter expression respectively, and the semantic evaluation is directly carried out on the input parameter expression after translation. Therefore, only the same semantic evaluation function is required to be proved, the semantic evaluation of the expression is respectively carried out, and the semantic evaluation is carried out after the expression is spliced, so that the obtained result is the same.
In step S311, formal verification is performed on the combination of the output parameters in the structural definition of the function into the input parameters.
Step S311 is a proof of the aforementioned step S1053.
When the semantic maintainability of a function call statement is proved, the function types before and after translation are required to be proved to be the same, and the function types comprise parameter types. Therefore, the types of the input and output parameter lists before translation are proved, and the types of the input and output parameter lists after translation are the same, so that the verification is easy due to the correctness of list connection.
In step S313, formal verification is performed on translating other nodes in the abstract syntax tree, where the other nodes include nodes in the abstract syntax tree except function nodes and expression nodes.
Step S311 is to perform formal verification on translation of other nodes in the abstract syntax tree according to the source language specification of the trusted compiling tool.
Step S313 is a proof of the previous step S107.
For example, for the memory allocation statement and the external malloc function in step S107, the semantic rule is to evaluate the input parameter semantically, then obtain the memory size value to be allocated, and call the external function allocated by the memory with this value as the parameter, which is easy to be verified.
According to the embodiment shown in fig. 3, through formal verification of the composite data type and the function translation process, the trusted compiling of the intelligent contract language is realized, and the compiling safety of the intelligent contract language is ensured.
It should be noted that the embodiment shown in fig. 3 is directed to formal verification of the composite data type and function translation process shown in fig. 1. In an embodiment, all steps from step S301 to step S311 are not necessarily performed, and may be selected according to an embodiment.
FIG. 4 illustrates a block diagram of an apparatus for translating an intelligent contract language, according to an example embodiment of the present application, the apparatus shown in FIG. 4 includes an abstract syntax tree generation unit 401, a composite data type expression translation unit 403, a function translation unit 405, and an abstract syntax tree node specification Fan Shanyuan, wherein the abstract syntax tree generation unit 401 generates a normalized abstract syntax tree in response to a source code written in the intelligent contract language being loaded; the compound data type expression translation unit 403 translates compound data type expression nodes in the abstract syntax tree according to the type definition of the intelligent contract language; a function translation unit 405 translates function nodes in the abstract syntax tree according to the function definition of the intelligent contract language; the abstract syntax tree node specification unit 407 is configured to translate other nodes in the abstract syntax tree according to a source language specification of a trusted compiling tool, where the other nodes include nodes in the abstract syntax tree except for a compound data type expression node and a function node.
According to further embodiments, the apparatus shown in fig. 4 further comprises a formal verification unit 409 for formal verification of the translation process in the compound data type expression translation unit, the function translation unit and/or the abstract syntax tree node specification unit, as shown in fig. 5.
Fig. 6 illustrates an electronic device according to an exemplary embodiment of the present application. An electronic device 200 according to this embodiment of the present application is described below with reference to fig. 6. The electronic device 200 shown in fig. 6 is only an example and should not be construed as limiting the functionality and scope of use of the embodiments herein.
As shown in fig. 6, the electronic device 200 is in the form of a general purpose computing device. The components of the electronic device 200 may include, but are not limited to: at least one processing unit 210, at least one memory unit 220, a bus 230 connecting the different system components (including the memory unit 220 and the processing unit 210), a display unit 240, and the like.
Wherein the storage unit stores program code that can be executed by the processing unit 210, such that the processing unit 210 performs the methods described herein according to various exemplary embodiments of the present application. For example, the processing unit 210 may perform the method as shown in fig. 1.
The storage unit 220 may include readable media in the form of volatile storage units, such as Random Access Memory (RAM) 2201 and/or cache memory 2202, and may further include Read Only Memory (ROM) 2203.
The storage unit 220 may also include a program/utility 2204 having a set (at least one) of program modules 2205, such program modules 2205 including, but not limited to: an operating system, one or more application programs, other program modules, and program data, each or some combination of which may include an implementation of a network environment.
Bus 230 may be a bus representing one or more of several types of bus structures including a memory unit bus or memory unit controller, a peripheral bus, an accelerated graphics port, a processing unit, or a local bus using any of a variety of bus architectures.
The electronic device 200 may also communicate with one or more external devices 300 (e.g., keyboard, pointing device, bluetooth device, etc.), one or more devices that enable a user to interact with the electronic device 200, and/or any device (e.g., router, modem, etc.) that enables the electronic device 200 to communicate with one or more other computing devices. Such communication may occur through an input/output (I/O) interface 250. Also, the electronic device 200 may communicate with one or more networks such as a Local Area Network (LAN), a Wide Area Network (WAN), and/or a public network, such as the Internet, through a network adapter 260. Network adapter 260 may communicate with other modules of electronic device 200 via bus 230. It should be appreciated that although not shown, other hardware and/or software modules may be used in connection with electronic device 200, including, but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, data backup storage systems, and the like.
From the above description of embodiments, those skilled in the art will readily appreciate that the example embodiments described herein may be implemented in software, or may be implemented in software in combination with the necessary hardware. The technical solution according to the embodiments of the present application may be embodied in the form of a software product, which may be stored in a non-volatile storage medium (may be a CD-ROM, a usb disk, a mobile hard disk, etc.) or on a network, and includes several instructions to cause a computing device (may be a personal computer, a server, or a network device, etc.) to perform the above-described method according to the embodiments of the present application.
The software product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium can be, for example, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or a combination of any of the foregoing. More specific examples (a non-exhaustive list) of the readable storage medium would include the following: an electrical connection having one or more wires, a portable disk, a hard disk, random Access Memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
The computer readable storage medium may include a data signal propagated in baseband or as part of a carrier wave, with readable program code embodied therein. Such a propagated data signal may take any of a variety of forms, including, but not limited to, electro-magnetic, optical, or any suitable combination of the foregoing. A readable storage medium may also be any readable medium that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a readable storage medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
Program code for carrying out operations of the present application may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C++ or the like and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computing device, partly on the user's device, as a stand-alone software package, partly on the user's computing device, partly on a remote computing device, or entirely on the remote computing device or server. In the case of remote computing devices, the remote computing device may be connected to the user computing device through any kind of network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or may be connected to an external computing device (e.g., connected via the Internet using an Internet service provider).
The computer-readable medium carries one or more programs which, when executed by one of the devices, cause the computer-readable medium to perform the aforementioned functions.
Those skilled in the art will appreciate that the modules may be distributed throughout several devices as described in the embodiments, and that corresponding variations may be implemented in one or more devices that are unique to the embodiments. The modules of the above embodiments may be combined into one module, or may be further split into a plurality of sub-modules.
According to an embodiment of the present application, a computer program is presented, comprising a computer program or instructions which, when executed by a processor, can perform the method described above.
The foregoing has outlined rather broadly the more detailed description of embodiments of the present application, wherein specific examples have been provided herein to illustrate the principles and embodiments of the present application, and wherein the above examples are provided to assist in the understanding of the methods and concepts of the present application. Meanwhile, based on the ideas of the present application, those skilled in the art can make changes or modifications on the specific embodiments and application scope of the present application, which belong to the scope of the protection of the present application. In view of the foregoing, this description should not be construed as limiting the application.

Claims (11)

1. A method for translating a smart contract language, comprising:
generating a normalized abstract syntax tree in response to the loaded source code written in the smart contract language;
translating the composite data type expression node in the abstract syntax tree according to the type definition of the intelligent contract language;
translating function nodes in the abstract syntax tree according to the function definition of the intelligent contract language;
and translating other nodes in the abstract syntax tree according to the source language specification of the trusted compiling tool, wherein the other nodes comprise nodes except the composite data type expression node and the function node in the abstract syntax tree.
2. The method of claim 1, wherein translating the composite data type expression node in the abstract syntax tree according to the type definition of the smart contract language comprises:
traversing the abstract syntax tree to obtain the composite data type of the composite data type expression node;
generating a built-in function of the composite data type;
translating the built-in method statement of the composite data type into a function call statement for calling the built-in function;
The composite data type is translated into a data type that complies with the source language specification of the trusted compilation tool.
3. The method of claim 2, wherein translating function nodes in the abstract syntax tree according to a function definition of the smart contract language comprises:
merging the output parameter expression in the function call statement in the function into the input parameter expression;
the output parameters in the structural definition of the function are incorporated into the input parameters.
4. A method according to claim 3, characterized in that the method further comprises:
and carrying out formal verification on the translation process of the source code written in the intelligent contract language.
5. The method of claim 4, wherein formalizing the translation of the source code written in the smart contract language comprises:
performing formal verification on the obtained composite data type;
performing formal verification on the built-in function for generating the composite data type;
translating the built-in method statement of the composite data type into a function call statement for calling the built-in function for formal verification;
Formalized verification is carried out on the composite data type translated into the data type conforming to the source language specification of the trusted compiling tool;
combining the output parameter expressions in the function call statement in the function into the input parameter expressions for formal verification;
combining the output parameters in the structural definition of the function into the input parameters for formal verification; and/or
And performing formal verification on other nodes in the abstract syntax tree, wherein the other nodes comprise nodes except function nodes and expression nodes in the abstract syntax tree.
6. The method of claim 3, wherein generating a normalized abstract syntax tree in response to the loaded source code written in the smart contract language comprises:
performing lexical analysis on the source codes according to word forming rules of the intelligent contract language to generate lexical unit sequences;
combining the lexical unit sequences into a grammar phrase according to the grammar rules of the intelligent contract language, thereby generating the abstract grammar tree;
normalizing the abstract syntax tree, thereby generating the normalized abstract syntax tree.
7. The method of claim 6, wherein normalizing the abstract syntax tree, thereby generating the normalized abstract syntax tree comprises:
preprocessing the abstract syntax tree;
performing static type checking on nodes in the abstract syntax tree;
normalizing compound data type expression nodes, function nodes, and other nodes of the abstract syntax tree to generate the normalized abstract syntax tree.
8. An apparatus for translating a smart contract language, comprising:
an abstract syntax tree generating unit for generating a normalized abstract syntax tree in response to the loaded source code written in the intelligent contract language;
the composite data type expression translation unit translates the composite data type expression nodes in the abstract syntax tree according to the type definition of the intelligent contract language;
a function translation unit, which translates function nodes in the abstract syntax tree according to the function definition of the intelligent contract language;
and the abstract syntax tree node specification unit is used for translating other nodes in the abstract syntax tree according to the source language specification of the trusted compiling tool, wherein the other nodes comprise nodes except the composite data type expression node and the function node in the abstract syntax tree.
9. The apparatus of claim 8, wherein the apparatus further comprises:
and the formal verification unit is used for formally verifying the translation process in the composite data type expression translation unit, the function translation unit and/or the abstract syntax tree node specification unit.
10. An electronic device, comprising: a memory, a processor; wherein the memory stores one or more computer instructions that, when executed by the processor, implement the method of any of claims 1-7.
11. A computer readable storage medium, characterized in that a computer program is stored thereon, which, when executed, implements the method according to any of claims 1-7.
CN202310437229.0A 2023-04-23 2023-04-23 Method and device for translating intelligent contract language and electronic equipment Active CN116149671B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202310437229.0A CN116149671B (en) 2023-04-23 2023-04-23 Method and device for translating intelligent contract language and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202310437229.0A CN116149671B (en) 2023-04-23 2023-04-23 Method and device for translating intelligent contract language and electronic equipment

Publications (2)

Publication Number Publication Date
CN116149671A true CN116149671A (en) 2023-05-23
CN116149671B CN116149671B (en) 2023-07-04

Family

ID=86352893

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202310437229.0A Active CN116149671B (en) 2023-04-23 2023-04-23 Method and device for translating intelligent contract language and electronic equipment

Country Status (1)

Country Link
CN (1) CN116149671B (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116501303A (en) * 2023-06-25 2023-07-28 北京长亭科技有限公司 Automatic API code generation method and device

Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108459860A (en) * 2018-03-28 2018-08-28 成都链安科技有限公司 Block chain intelligence forms of contract chemical examination card code converter and conversion method
CN110865814A (en) * 2019-10-30 2020-03-06 南京天数智芯科技有限公司 Compiler implementation method and system supporting heterogeneous computing core architecture
CN111124422A (en) * 2019-12-25 2020-05-08 成都互诚在线科技有限公司 EOS intelligent contract language conversion method based on abstract syntax tree
US20200201838A1 (en) * 2018-12-20 2020-06-25 Sri International Middleware to automatically verify smart contracts on blockchains
US20200364037A1 (en) * 2018-02-08 2020-11-19 Valid Network Ltd Code conversion method and system
CN112346722A (en) * 2020-11-11 2021-02-09 苏州大学 Method for realizing compiling embedded Python
CN113535184A (en) * 2020-04-14 2021-10-22 华为技术有限公司 Cross-platform code conversion method and device
CN114047928A (en) * 2022-01-12 2022-02-15 北京大学 Intelligent contract formal verification method and system suitable for combined transaction
CN114153435A (en) * 2021-11-29 2022-03-08 北京航空航天大学 EB2S system for intelligent contract code design and generation and use method

Patent Citations (9)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20200364037A1 (en) * 2018-02-08 2020-11-19 Valid Network Ltd Code conversion method and system
CN108459860A (en) * 2018-03-28 2018-08-28 成都链安科技有限公司 Block chain intelligence forms of contract chemical examination card code converter and conversion method
US20200201838A1 (en) * 2018-12-20 2020-06-25 Sri International Middleware to automatically verify smart contracts on blockchains
CN110865814A (en) * 2019-10-30 2020-03-06 南京天数智芯科技有限公司 Compiler implementation method and system supporting heterogeneous computing core architecture
CN111124422A (en) * 2019-12-25 2020-05-08 成都互诚在线科技有限公司 EOS intelligent contract language conversion method based on abstract syntax tree
CN113535184A (en) * 2020-04-14 2021-10-22 华为技术有限公司 Cross-platform code conversion method and device
CN112346722A (en) * 2020-11-11 2021-02-09 苏州大学 Method for realizing compiling embedded Python
CN114153435A (en) * 2021-11-29 2022-03-08 北京航空航天大学 EB2S system for intelligent contract code design and generation and use method
CN114047928A (en) * 2022-01-12 2022-02-15 北京大学 Intelligent contract formal verification method and system suitable for combined transaction

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116501303A (en) * 2023-06-25 2023-07-28 北京长亭科技有限公司 Automatic API code generation method and device
CN116501303B (en) * 2023-06-25 2023-11-14 北京长亭科技有限公司 Automatic API code generation method and device

Also Published As

Publication number Publication date
CN116149671B (en) 2023-07-04

Similar Documents

Publication Publication Date Title
CN108459860B (en) Block chain intelligent contract formal verification code converter and conversion method
US11334692B2 (en) Extracting a knowledge graph from program source code
US7219338B2 (en) Multi-language compilation
US8365280B2 (en) System, method, and program for determining validity of string
US9122540B2 (en) Transformation of computer programs and eliminating errors
CN109375899A (en) A kind of method of formal verification Solidity intelligence contract
US9378195B2 (en) Logical contingency analysis for domain-specific languages
CN101261579A (en) Method for accomplishing VBScript language trans-platform uses
CN116149671B (en) Method and device for translating intelligent contract language and electronic equipment
Ren et al. Making smart contract development more secure and easier
Zhang et al. Characterizing and detecting webassembly runtime bugs
CN113885876A (en) Parameter checking method, device, storage medium and computer system
Zhao et al. Pattern-based design evolution using graph transformation
US20170168787A1 (en) Optimized compiling of a template function
US10606569B2 (en) Declarative configuration elements
CN112988163A (en) Intelligent programming language adaptation method and device, electronic equipment and medium
US20230072988A1 (en) System and a method for automatic generation of smart contracts across blockchain platforms
CN115145575A (en) High-level language parsing method, equipment and computer readable storage medium
CN114510414B (en) Method and system for formalized verification of intelligent contract function attribute based on symbol execution
CN113849781B (en) Go language source code confusion method, system, terminal and storage medium
CN115237419A (en) Heterogeneous target code generation method, device and system
CN110727428B (en) Method and device for converting service logic layer codes and electronic equipment
US10929106B1 (en) Semantic analyzer with grammatical-number enforcement within a namespace
CN110737431A (en) Software development method, development platform, terminal device and storage medium
CN112860233A (en) Target syntax tree generation method and related equipment

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant