Extracting Smart Contracts Tested and Verified in Coq
Total Page:16
File Type:pdf, Size:1020Kb
Extracting Smart Contracts Tested and Verified in Coq Danil Annenkov Mikkel Milo [email protected] [email protected] Concordium Blockchain Research Center, Aarhus Department of Computer Science, Aarhus University, University, Denmark Denmark Jakob Botsch Nielsen Bas Spitters [email protected] [email protected] Concordium Blockchain Research Center, Aarhus Concordium Blockchain Research Center, Aarhus University, Denmark University, Denmark Abstract Keywords: smart contracts, blockchain, Coq, proof assis- We implement extraction of Coq programs to functional tants, code extraction, formal verification, property-based languages based on MetaCoq’s certified erasure. As part of testing, certified programming, software correctness this, we implement an optimisation pass removing unused ACM Reference Format: arguments. We prove the pass correct wrt. a conventional Danil Annenkov, Mikkel Milo, Jakob Botsch Nielsen, and Bas Spit- call-by-value operational semantics of functional languages. ters. 2021. Extracting Smart Contracts Tested and Verified in Coq. We apply this to two functional smart contract languages, In Proceedings of the 10th ACM SIGPLAN International Conference Liquidity and Midlang, and to the functional language Elm. on Certified Programs and Proofs (CPP ’21), January 18ś19, 2021, Our development is done in the context of the ConCert frame- Virtual, Denmark. ACM, New York, NY, USA, 17 pages. https://doi. work that enables smart contract verification. We contribute org/10.1145/3437992.3439934 a verified boardroom voting smart contract featuring max- 1 Introduction imum voter privacy such that each vote is kept private ex- cept under collusion of all other parties. We also integrate Smart contracts are programs running on top of a blockchain. property-based testing into ConCert using QuickChick and They often control large amounts of cryptocurrency and can- our development is the first to support testing properties of not be changed after deployment. Unfortunately, many vul- interacting smart contracts. We test several complex con- nerabilities have been discovered in smart contracts and this tracts such as a DAO-like contract, an escrow contract, an has led to huge financial losses (e.g. TheDAO, Parity’s multi- implementation of a Decentralized Finance (DeFi) contract signature wallet). Therefore, smart contract verification is which includes a custom token standard (Tezos FA2), and crucially important. Functional smart contract languages are more. In total, this gives us a way to write dependent pro- becoming increasingly popular: e.g. Simplicity [36], Liquid- 1 grams in Coq, test them semi-automatically, verify, and then ity [9], Plutus [11], Scilla [40] and Midlang . A contract in extract to functional smart contract languages, while retain- such a language is just a function from a message type and ing a small trusted computing base of only MetaCoq and the a current state to a new state and a list of actions (trans- pretty-printers into these languages. fers, calls to other contracts), making smart contracts more amenable for formal verification. Functional smart contract CCS Concepts: · Theory of computation → Logic and languages, similarly to conventional functional languages, verification; Program verification; Type theory; Program are often based on a variants of System F allowing the type semantics; · Software and its engineering → Correct- checker to catch many errors. For errors that are not caught ness; Formal software verification; Software testing and by the type checker, a proof assistant, in particular Coq, can debugging. be used to ensure correctness. Permission to make digital or hard copies of all or part of this work for Formal verification is a complex and time-consuming ac- personal or classroom use is granted without fee provided that copies tivity, and much time may be wasted attempting to prove are not made or distributed for profit or commercial advantage and that false statements (e.g. if the implementation is incorrect). copies bear this notice and the full citation on the first page. Copyrights Property-based testing is an automated testing technique for components of this work owned by others than the author(s) must with high bug-discovering capability compared to e.g. unit be honored. Abstracting with credit is permitted. To copy otherwise, or republish, to post on servers or to redistribute to lists, requires prior specific testing. It can provide a preliminary, cost-efficient approach permission and/or a fee. Request permissions from [email protected]. to discover implementation bugs in the contract or mistakes CPP ’21, January 18ś19, 2021, Virtual, Denmark in the statement to be proven. © 2021 Copyright held by the owner/author(s). Publication rights licensed Once properties of contracts are tested and proved correct, to ACM. one would like to execute them on blockchains. One way ACM ISBN 978-1-4503-8299-1/21/01...$15.00 https://doi.org/10.1145/3437992.3439934 1https://developers.concordium.com/midlang 105 CPP ’21, January 18ś19, 2021, Virtual, Denmark Danil Annenkov, Mikkel Milo, Jakob Botsch Nielsen, and Bas Spitters Coq proof assistant Target languages Our work is the first development applying the property- ConCert MetaCoq optimise* Liquidity based testing techniques to smart contract execution traces Midlang (as opposed testing single-step executions), and extracting Smart contracts quote erase* + print CIC λ□ Elm the verified programs to smart contract languages. Testing ... Verification The pipeline covering the full process of starting with a extract types smart contract as a function in Coq and ending with extracted code in one of the target languages is shown in Figure 1. The Figure 1. The pipeline green items are contributions of this work and the items marked with ∗ are verified in Coq. The MetaCoq project [41] provides us with metaprogramming facilities (e.g. quoting Coq terms) and formalisation of the meta-theory of Coq (a of achieving this is to extract the executable code from the variant of the calculus of inductive constructions Ð CIC). By formalised development. Various verified developments rely λ□+, we mean the untyped calculus of extracted programs on the extraction feature of proof assistants extensively [13, (part of MetaCoq) enriched with data structures required for 14, 18, 25, 30]. However, currently, the standard extraction type extraction. feature in proof assistants supports conventional functional Our trusted computing base (TCB) includes Coq itself, the languages (Haskell, OCaml, Standard ML, Scheme, etc.) by quote functionality of MetaCoq and the pretty-printing to using unsafe features such as type casts, if required, and target languages. While the type extraction procedure is not this is not possible in many smart contracts languages. More verified, it does not affect the soundness of the pipeline (see importantly, the current implementation of extraction is discussion in Section 3.1). written in OCaml and is not verified. Our development is available as an accompanying arti- fact [3] and in the GitHub repository https://github.com/AU- Contributions. We build on the ConCert framework [4] COBRA/ConCert/tree/artifact-2020. for embedding smart contracts in Coq and the execution model introduced in [35]. We summarise the contributions of this work as the following. 2 The ConCert Framework The present work builds on and extends the ConCert smart • We provide a general framework for extraction from contract certification framework [4]. The ConCert frame- Coq to a typed functional language (Section 3). The work features an embedding of smart contracts into Coq framework is based on certified erasure [42], which along with the proof of soundness of the embedding using we extend with an erasure procedure for types and the MetaCoq project [41]. The embedded contracts are avail- inductive definitions. Moreover, we implement and able in the deep embedding (as ASTs) and in the shallow prove correct an optimisation procedure that removes embedding (as Coq functions). Having smart contracts as unused arguments and allows therefore for optimis- Coq functions facilitates the reasoning about their functional ing away some computationally irrelevant bits left correctness properties. Moreover, ConCert features an exe- after erasure. We develop the pretty-printers for print- cution model first introduced in [35]. The execution model ing code into two smart contract languages: Liquidity allows for reasoning on contract execution traces which (Tezos and Dune networks) and Midlang (Concordium makes it possible to state and prove temporal properties network). Since Midlang is a fork of the Elm functional of interacting smart contracts. The previous work on Con- language [17], our extraction also supports Elm as a Cert [4] mainly concerns with the following use-case: take a target language. smart contract, embed it into Coq and verify its properties. • We integrate contract verification and testing using This work explores how it is possible to verify a contract QuickChick, a property-based testing framework for as a Coq function and then extract it into a program in a Coq, by testing properties on generated execution functional smart contract language. traces. We show that this would have allowed us to detect several high profile exploits automatically. The testing frameworks itself uses the facilities of Coq for 3 Extraction giving stronger correctness guarantees for the testing The Coq proof assistant features a possibility of extracting infrastructure (Section 6). the executable