09 September 2021, ICTAC
Formal Design, Implementation and Verification of Blockchain Languages using K
Grigore Rosu
Professor of CS, University of Illinois at Urbana
President & CEO, Runtime Verification Inc.
What is ?
2
Ideal Language Framework Vision
3
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…
Current State-of-the-Art�- Sharp Contrast to Ideal Vision -
4
C
Java
JavaScript
Solidity
Ethereum VM
…
Interpreter
Symbolic Execution
Compiler
Model Checker
Deductive Verifier
…
Separate tools, by separate teams, little to no code shared
Current State-of-the-Art�- Sharp Contrast to Ideal Vision -
5
C
Java
JavaScript
Solidity
Ethereum VM
…
Interpreter
Symbolic Execution
Compiler
Model Checker
Deductive Verifier
…
The story of the PL/FM community.
Maintenance hell
(L * T systems).
Uneconomical.
Wasted talent!
L
T
How It Should Be
6
C
Java
JavaScript
Solidity
Ethereum VM
…
Interpreter
Symbolic Execution
Compiler
Model Checker
Deductive Verifier
…
Ideal Language Framework
L
T
K Framework�http://kframework.org
7
Complete K Definition of KernelC
8
Complete K Definition of KernelC
9
Syntax declared using annotated BNF
…
Complete K Definition of KernelC
10
Configuration given as a nested cell structure.
Leaves can be sets, multisets, lists, maps, or syntax
Complete K Definition of KernelC
11
Semantic rules given contextually
rule
<k> X = V => V …</k>
<env>… X |-> (_ => V) …</env>
Semantic rules given contextually
rule
k: X = V => V …
env: … X |-> (_ => V) …
K Scales
Several large languages were recently defined in K:
+ EVM[CSF’18], Solidity, IELE[FM’19], WASM, Michelson, …
K Configuration and Definition of C
13
120 Cells!
… plus ~5000 rules …
Ideal Language Framework Vision [K]
14
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…
K -> LLVM -> x86
1. Translate K def to LLVM�2. Compile LLVM to x86
Code (6-int-overflow.c)
RV-Match: Commercial tool
Concrete Execution Backend
RV-Match on Toyota ITC Benchmark�- Comparison with Other Analysis Tools -
Toyota Benchmark ISSRE ’15 (1276 tests) | RV-Match | GrammaTech CodeSonar | MathWorks Code Prover | MathWorks Bug Finder | GCC | Clang | ||||||||||||
| DR | FPR | PM | DR | FPR | PM | DR | FPR | PM | DR | FPR | PM | DR | FPR | PM | DR | FPR | PM |
Static memory | 100 | 100 | 100 | 100 | 100 | 100 | 97 | 100 | 98 | 97 | 100 | 98 | 0 | 100 | 0 | 15 | 100 | 39 |
Dynamic memory | 94 | 100 | 97 | 89 | 100 | 94 | 92 | 95 | 93 | 90 | 100 | 95 | 0 | 100 | 0 | 0 | 100 | 0 |
Stack-related | 100 | 100 | 100 | 0 | 100 | 0 | 60 | 70 | 65 | 15 | 85 | 36 | 0 | 100 | 0 | 0 | 100 | 0 |
Numerical | 96 | 100 | 98 | 48 | 100 | 69 | 55 | 99 | 74 | 41 | 100 | 64 | 12 | 100 | 35 | 11 | 100 | 33 |
Resource management | 93 | 100 | 96 | 61 | 100 | 78 | 20 | 90 | 42 | 55 | 100 | 74 | 6 | 100 | 25 | 3 | 100 | 18 |
Pointer-related | 98 | 100 | 99 | 52 | 96 | 71 | 69 | 93 | 80 | 69 | 100 | 83 | 9 | 100 | 30 | 13 | 100 | 36 |
Concurrency | 67 | 100 | 82 | 70 | 77 | 73 | 0 | 100 | 0 | 0 | 100 | 0 | 0 | 100 | 0 | 0 | 100 | 0 |
Inappropriate code | 0 | 100 | 0 | 46 | 99 | 67 | 1 | 97 | 10 | 28 | 94 | 51 | 2 | 100 | 13 | 0 | 100 | 0 |
Miscellaneous | 63 | 100 | 79 | 69 | 100 | 83 | 83 | 100 | 91 | 69 | 100 | 83 | 11 | 100 | 34 | 11 | 100 | 34 |
AVERAGE (Unweighted) | 79 | 100 | 89 | 59 | 97 | 76 | 53 | 94 | 71 | 52 | 98 | 71 | 4 | 100 | 20 | 6 | 100 | 24 |
AVERAGE (Weighted) | 82 | 100 | 91 | 68 | 98 | 82 | 53 | 95 | 71 | 62 | 99 | 78 | 5 | 100 | 22 | 7 | 100 | 26 |
[CAV’16]
From RV-Match to Blockchain
17
Ideal Language Framework Vision [K]
18
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…
State-of-the-Art
19
What We Want
language model/semantics!
20
Formal Language Definition
(Syntax and Semantics)
Deductive program verifier
Symbolic execution
Matching μ-Logic
[…, LICS’13, RTA’15, OOPSLA’16, FSCD’16, LMCS’17, LICS’19, ICFP’20]
16 proof rules only!
Simple proof checker (200 LOC, vs Coq’s 30000 LOC)!
Expressiveness of Matching μ-Logic
K = (Best Effort) Implementation of ML
23
EVM
Solidity
Michelson
WASM
…
for the Blockchain
24
K Framework Vision
25
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…
Blockchain Language Definition
KEVM: Semantics of the Ethereum Virtual Machine (EVM) in K
Complete semantics of EVM in K
26
[CSL’18]
1) Formal documentation: http://jellopaper.org
27
What Can We Do with KEVM?
What Can We Do with KEVM?
2) Generate and deploy correct-by-construction EVM client/simulator/emulator
Firefly tool: KEVM to run, analyze and monitor tests
Cardano testnet: mantis executing KEVM
28
What Can We Do with KEVM?
3) Formally verify Ethereum smart contracts
RV does that commercially. Won first Ethereum Security grant to verify Casper, then were hired to formalize Beacon Chain (Serenity) and verify ETH1 -> ETH2 deposit contract
29
[ FSE’18 ]
Smart Contract Verification Workflow
ERC20 Informal Business Logic
Formalize
Refine
1
rule
transfer(T, V) => true
caller: F
account:
id: F
balance: BF => BF - V
account:
id: T
balance: BT => BT + V
log: . => Transfer(F,T,V)
requires
0 <= V /\
V <= BF /\
BT + V <= MAXVALUE
ERC20-K formal executable high-level spec
2
[transfer]
callData: #abiCallData("transfer", #address(TO_ID), #uint256(VALUE))
gas: {GASCAP} => _
refund: _ => _
requires:
andBool 0 <=Int TO_ID andBool TO_ID <Int (2 ^Int 160)
andBool 0 <=Int VALUE andBool VALUE <Int (2 ^Int 256)
andBool 0 <=Int BAL_FROM andBool BAL_FROM <Int (2 ^Int 256)
andBool 0 <=Int BAL_TO andBool BAL_TO <Int (2 ^Int 256)
[transfer-success]
k: #execute => (RETURN RET_ADDR:Int 32 ~> _)
localMem: .Map => ( .Map[ RET_ADDR := #asByteStackInWidth(1, 32) ] _:Map )
log: _:List ( .List => ListItem(#abiEventLog(ACCT_ID, "Transfer", #indexed(#address(CALLER_ID
……….
………
…….
ERC20-EVM formal executable low-level spec that contains all EVM details
3
Notable Contracts� We’ve Verified
31
K Semantics of Other�Blockchain Languages
…
32
Modelling and Verification of�Blockchain Protocols
33
Taking K to the Next Level
34
Proof Object Generation
35
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…
Proof Object Generation
36
16 proof rules only!
Simple proof checker (<200 LOC)!
In contrast, Coq has about 45 proof rules, and its proof checker has 30000+ lines of OCAML
Proof Object Generation
37
Ultimate Goal����� a Universal Blockchain Technology
38
Goals
K-powered blockchain: blockchain of truth
39
K – A Universal Blockchain Language
40
K language semantics will be stored on blockchain. Fast LLVM backend of K can be used as execution engine / VM.
Vyper
V1.2
Solidity
V0.2.21
Plutus
V2.1
Semantics-Based Compilation
41
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…
Semantics-Based Compilation (SBC)
Goals
Program P in Language L
Semantics-Based Compilation
𝕂 Semantics of Language L
𝕂 Semantics of Language LP
K – A Universal Blockchain Language
43
Vyper
V1.2
Solidity
V0.2.21
Plutus
V2.1
1. Write contract P in any language, say L (unique address)
2. SBC[L] your P into LP ; verify P (or LP) with K prover
K – A Universal Blockchain Language
44
a Ultimate Smart Contract Language
45
K as a Smart Contract Language
K also implements transactions, directly!
K formal specifications are already executable!
46
Hm, then why not write my smart contracts directly and only as K executable specifications?
Example: ERC20 Token in Solidity�- Snippet -
47
Example: ERC20 Compiled to EVM�- Snippet -
48
Bytecode:
Opcodes:
K Specification of ERC20�- Snippet, Sugared -
49
rule transfer(To, V) => true
caller: From
account: id: From balance: BalanceFrom => BalanceFrom - V
account: id: To balance: BalanceTo => BalanceTo + V
log: . => Transfer(From, To, V)
requires 0 <= V <= BalanceFrom /\ BalanceTo + V <= MAXVALUE
(needed: gas model for K)
Blockchain of Truth
50
Blockchain of Truth (BoT)
51
mathematical proof
proof checker
Correct-By-Construction and Fast�is Possible!
52
Deductive program verifier
Parser
Interpreter
Compiler
(semantic) Debugger
Symbolic execution
Model checker
Formal Language Definition
(Syntax and Semantics)
…