Short Paper: Formal Verification of Smart Contracts

Short Paper: Formal Verification of Smart Contracts

Short Paper: Formal Verification of Smart Contracts Karthikeyan Bhargavan2 Antoine Delignat-Lavaud1 Cedric´ Fournet1 Anitha Gollamudi3 Georges Gonthier1 Nadim Kobeissi2 Aseem Rastogi1 Thomas Sibut-Pinote2 Nikhil Swamy1 Santiago Zanella-Beguelin´ 1 1Microsoft Research 2Inria 3Harvard University fantdl,fournet,gonthier,aseemr,nswamy,[email protected] fkarthikeyan.bhargavan,nadim.kobeissi,[email protected] [email protected] Abstract ted too quickly, the difficulty increases, thus raising the com- Ethereum is a cryptocurrency framework that uses blockchain putational cost of mining. technology to provide an open distributed computing plat- Ethereum is similarly built on a blockchain based on form, called the Ethereum Virtual Machine (EVM). EVM proof-of-work; however, its ledger is considerably more ex- programs are written in bytecode which operates on a sim- pressive than that of Bitcoin’s: it stores Turing-complete ple stack machine. Programmers do not usually write EVM programs in the form of Ethereum Virtual Machine (EVM) code; instead, they can program in a JavaScript-like lan- bytecode, while transactions are construed as function calls guage called Solidity that compiles to bytecode. Since the and can carry additional data in the form of arguments. Fur- main application of EVM programs is as smart contracts that thermore, contracts may also use non-volatile storage and manage and transfer digital assets, security is of paramount log events, both of which are recorded in the ledger. importance. However, writing trustworthy smart contracts The initiator of a transaction pays a fee for its execution can be extremely difficult due to the intricate semantics of measured in units of gas. The miner who manages to ap- EVM and its openness: both programs and pseudonymous pend a block including the transaction gets to claim the fee users can call into the public methods of other programs. converted to Ether at a specified gas price. Some operations This problem is best illustrated by the recent attack on are more expensive than others: for instance, writing to stor- TheDAO contract, which allowed roughly $50M USD worth age and initiating a transaction is four orders of magnitude of Ether to be transferred into the control of an attacker. Re- more expensive than an arithmetic operation on stack val- covering the funds required a hard fork of the blockchain, ues. Therefore, Ethereum can be thought of as a distributed contrary to the code is law premise of the system. In this computing platform where anyone can run code by paying paper, we outline a framework to analyze and verify both for the associated gas charges. the runtime safety and the functional correctness of Solidity The integrity of the system relies on the honesty of a contracts in F?, a functional programming language aimed majority of miners: a miner may try to cheat by not running at program verification. the program, or running it incorrectly, but honest miners will reject the block and fork the chain. Since the longest chain is Categories and Subject Descriptors F.3 [F.3.1 Specifying the one that is considered valid, miners are incentivized not and Verifying and Reasoning about Programs] to cheat and to verify that others do as well, since their block reward may be lost unless malicious miners can supply the Keywords Ethereum, Solidity, EVM, smart contracts majority of new blocks to the network. While Ethereum’s adoption has led to smart contracts 1. Introduction managing millions of dollars in currency, the security of these contracts has become highly sensitive. For instance, The blockchain technology, pioneered by Bitcoin [7] pro- a variant of a well-documented reentrancy attack was re- vides a globally-consistent append-only ledger that does not cently exploited in TheDAO [2], a contract that implements rely on a central trusted authority. In Bitcoin, this ledger a decentralized autonomous venture capital fund, leading to records transactions of a virtual currency, which is created the theft of more than $50M worth of Ether, and raising the by a process called mining. In the proof-of-work mining question of whether similar bugs could be found by static scheme, each node of the network can earn the right to ap- analysis [6]. pend the next block of transactions to the ledger by finding In this paper, we outline a framework to analyze and a formatted value (which includes all transactions to appear formally verify Ethereum smart contracts using F? [9], a in the block) whose SHA256 digest is below some difficulty functional programming language aimed at program verifi- threshold. The system is designed to ensure that blocks are cation. Such contracts are generally written in Solidity [3], mined at a constant rate: when too many blocks are submit- 1 2016/8/11 a JavaScript-like language, and compiled down to bytecode hsolidityi ::=( hcontracti)* for the EVM. We consider the Solidity compiler as untrusted and develop a language-based approach for verifying smart hcontracti ::=‘ contract ’ @identifier ‘{’(hsti)*‘}’ contracts. Namely, we present two tools based on F?: hsti ::= htypedef i j hstatedef i j hmethodi Solidity? a tool to translate Solidity program to shallow- embedded F? programs (Section 2). htypedef i ::=‘ struct ’ @identifier ‘ {’(htypei @identifier ‘;’)* ‘}’ EVM? a decompiler for EVM bytecode that produces htypei ::=‘ uint’ j ‘address’ j ‘bool’ equivalent shallow-embedded F? programs that operate j ‘mapping (’ htypei ‘=>’ htypei ‘)’ j @identifier on a simpler machine without stack (Section 3). These tools enable three different forms of verification: hstatedef i ::= htypei @identifier 1. Given a Solidity program, we can use Solidity? to trans- hmethodi ::=‘ function’ (@identifier)?‘()’(hqualifieri)* ‘{’ ? (‘var’ (@identifier (‘=’ hexpressioni)? ‘,’)+)? late it to F and verify at the source level functional cor- (hstatementi ‘;’)* ‘}’ rectness specifications such as contract invariants, as well as safety with respect to runtime errors. hqualifieri ::=‘ private’ j ‘public’ j ‘internal’ j ‘returns (’ htypei (@identifier)? ‘)’ 2. Given an EVM bytecode, we can use EVM? to decompile it and analyze low-level properties, such as bounds on the hstatementi ::= " amount of gas consumed by calls. j htypei @identifier (‘=’ hexpressioni)? (*decl*) j ‘if(’ hexpressioni ‘)’ hstatementi 3. Given a Solidity program and allegedly functionally (‘else’ hstatementi)? equivalent EVM bytecode, we can verify their equiva- j ‘{’(hstatementi ‘;’)* ‘}’ ? j ‘return’(hexpressioni)? lence by translating each into F . Thus, we can check the j ‘throw’ correctness of the output of the Solidity compiler on a j hexpressioni case-by-case basis using relational reasoning [1]. hexpressioni ::= hliterali 1.1 Architecture of the Framework j hlhs expressioni ‘(’(hexpressioni ‘,’)* ‘)’ j hexpressioni hbinopi hexpressioni j hunopi hexpressioni j hlhs expressioni ‘=’ hexpressioni F* j hlhs expressioni Verified Translation Solidity Solidity* Verify Source Code Subset of F* hlhs expressioni ::= j @identifier ✅ Functional Correctness j hlhs expressioni ‘[’ hlhs expressioni‘]’ Equivalence j hlhs expressioni ‘.’ @identifier Proof ✅ Runtime Safety hliterali ::= hfunctioni j ‘{’ ( @identifier ‘:’ hexpressioni ‘,’)* ‘}’ Verified Decompilation EVM EVM* Verify j ‘[’(hexpressioni ‘,’)* ‘]’ Subset of F* Compiled Bytecode j @number j @address j @boolean hbinopi ::=‘ +’ j ‘-’ j ‘*’ j ‘/’ j ‘%’ j ‘&&’ j ‘||’ j ‘==’ j ‘!=’ j ‘>’ j ‘<’ j ‘>=’ j ‘<=’ Figure 1. Overview of the architecture of our framework hunopi ::=‘ +’ j ‘-’ j ‘!’ Our smart contract verification framework is a two- Figure 2. Syntax of the translated Solidity subset pronged approach (Figure 1) based on F?. F? comes with a type system that includes dependent types and monadic ? effects, which we apply to generate automated queries to 2. Translating Solidity to F statically verify properties on EVM bytecode and Solidity In the spirit of previous work on type-based analysis of sources. JavaScript programs [8], we advocate an approach where the While it is clearly favorable to obtain both the Solidity programmer can verify high-level goals of a contract using source code and EVM bytecode of a target smart contract, F?. In this section, we present a tool to translate Solidity to we design our architecture with the assumption that the veri- F?, and a simple automated analysis of extracted F? con- fier may only have the bytecode. At the moment of this writ- tracts. ing, only 396 out of 112,802 contracts have their source code Solidity programs consist of a number of contract decla- available on http://etherscan.io. Therefore we provide rations. Once compiled to EVM, contracts are installed us- separate tools for decompiling EVM bytecode (EVM?), and ing a special kind of account-creating transaction, which al- analyzing Solidity source code (Solidity?). locates an address to the contract. Unlike Bitcoin, where an 2 2016/8/11 address is the hash of the public key of an account, Ethereum 6. to translate assignments, we keep an environment of lo- addresses can refer indistinguishably to a contract or a user cal, state, and ambient global variable names: local vari- public key. Similarly, there is no distinction between trans- able declarations and assignments are translated to let actions and method calls: when sending Ether to a contract, bindings; globals are replaced with library calls; state it will implicitly call the fallback function (the unnamed properties are replaced with update on the state type; method of the Solidity contract). In fact, compiled

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us