# Toward an Extensible Blockchain
Martin Becze github.com/wanderer
Alex Beregszaszi github.com/axic
[comment]: # (this talk will give an overview of ewasm)
---
## Evolution of the web
1. HTML as a markup<!-- .element: class="fragment" data-fragment-index="0" -->
2. Scripting languages: Javascript with high level methods<!-- .element: class="fragment" data-fragment-index="1" -->
3. Browser plugins: for CPU intensive operations, such as Java and Flash<!-- .element: class="fragment" data-fragment-index="2" -->
4. HTML5: implementing what browser plugins do natively in the browser (video/audio input/output, etc.)<!-- .element: class="fragment" data-fragment-index="3" -->
5. ?<!-- .element: class="fragment" data-fragment-index="4" -->
---
## The Extensible Web Manifesto
- See extensiblewebmanifesto.org<!-- .element: class="fragment" data-fragment-index="0" -->
- Focus on adding new low-level capabilities to the web platform that are secure and efficient.<!-- .element: class="fragment" data-fragment-index="1" -->
- Expose low-level capabilities that explain existing features<!-- .element: class="fragment" data-fragment-index="2" -->
- Develop, describe and test new high-level features in terms of the low-level capabilites <!-- .element: class="fragment" data-fragment-index="3" -->
---
## The Extensible Web Manifesto
- Contain new security surface area.<!-- .element: class="fragment" data-fragment-index="0" -->
- Reduce the rate of growth in complexity, and therefore bugs, in implementations.<!-- .element: class="fragment" data-fragment-index="1" -->
- Allow browser vendors and library authors to iterate on libraries that provide developer-friendly, high-level APIs.<!-- .element: class="fragment" data-fragment-index="2" -->
---
## The Extensible Web Manifesto
- WebAssembly (wasm)
- Canvas
- WebGL
- simd.js
---
## Useful Generalities
A generality in software development is axiom or low level feature that can be used to implement two or more desired high level features<!-- .element: class="fragment" data-fragment-index="0" -->
You know you have a useful generality when axiom or set of axiom not only encompasses the desired high level features but also reduces implementation complexity <!-- .element: class="fragment" data-fragment-index="1" -->
---
## Evolution of the blockchain
1. Bitcoin (scripting restricted early on) <!-- .element: class="fragment" data-fragment-index="0" -->
2. Namecoin and OP_RETURN usage <!-- .element: class="fragment" data-fragment-index="1" -->
3. Ethereum <!-- .element: class="fragment" data-fragment-index="2" -->
4. Ethereum Precompiles <!-- .element: class="fragment" data-fragment-index="3" -->
5. ? <!-- .element: class="fragment" data-fragment-index="4" -->
---
## The Extensible Blockchain?
- New capabilities require hardforks <!-- .element: class="fragment" data-fragment-index="0" -->
- Strong incentive against expanding the trusted computing base <!-- .element: class="fragment" data-fragment-index="1" -->
- Hard to iterate on features <!-- .element: class="fragment" data-fragment-index="2" -->
---
## What we face today
![](https://docs.google.com/drawings/d/1zUNIkJgooMP0jVbasWVUGKXgA-BRi7F5ANuNC3AKP_U/pub?w=787&h=359)
---
## What we face today - Precompiles
- Potential new precompiles in Metropolis (hard fork): <!-- .element: class="fragment" data-fragment-index="0" -->
- Elliptic curve arithmetic <!-- .element: class="fragment" data-fragment-index="0" -->
- Big number arithmetic (modexp) <!-- .element: class="fragment" data-fragment-index="0" -->
- Elliptic curve pairing functions <!-- .element: class="fragment" data-fragment-index="0" -->
- Later hard forks: <!-- .element: class="fragment" data-fragment-index="1" -->
- Blake2 <!-- .element: class="fragment" data-fragment-index="1" -->
- SHA1 <!-- .element: class="fragment" data-fragment-index="1" -->
- Also SHA3 as an instruction and not as a precompile <!-- .element: class="fragment" data-fragment-index="2" -->
---
## What we face today - IPC
- Current IPC methods:
- CALL
- CALLDELEGATE
- CALLCODE
- Upcoming new IPC methods:
- CALL_STATIC
- CALL_PURE
---
## What we face today - Inextensible VM
- State & environment operations:
- BLOCKHASH
- SELFDESTRUCT
- BALANCE
- ORIGIN
- CALLER
- CALLVALUE
- CALLDATALOAD
- CALLDATASIZE
- etc.
---
## What we face today - Calculations
- Current arithmetics: <!-- .element: class="fragment" data-fragment-index="0" -->
- only 256 bit wide operations <!-- .element: class="fragment" data-fragment-index="0" -->
- no shift operations <!-- .element: class="fragment" data-fragment-index="0" -->
- Upcoming arithmetics: <!-- .element: class="fragment" data-fragment-index="1" -->
- additional 64 bit wide operations <!-- .element: class="fragment" data-fragment-index="1" -->
- shift operations <!-- .element: class="fragment" data-fragment-index="1" -->
- additional SIMD operations <!-- .element: class="fragment" data-fragment-index="1" -->
---
## What we face today - Interpolarity
- Incompatible with the ecosystem outside of Ethereum <!-- .element: class="fragment" data-fragment-index="0" -->
- It is a new compilation target for programming languages <!-- .element: class="fragment" data-fragment-index="1" -->
---
## What we face today
Big Trusted Computing bases are really bad for symmetric computation platform
---
## Ze future
![](https://docs.google.com/drawings/d/1AOV5J2tKJjh_KXnkozmv7eZbpbwM68-kYC6fYbWryCw/pub?w=960&h=720)
---
## Waist Thin
![](https://docs.google.com/drawings/d/1sXkSbMx-f2OYKEHXJFLSCQPXapZvN4U6qpxrXwZM9ug/pub?w=951&h=591)
---
## Risk of becoming a Big Ball of Mud
![](https://docs.google.com/drawings/d/178bK7ooePlnMP1U8mEnIh1yz_bWJmkFAsbrsMaqUIic/pub?w=653&h=544)
Footnote: Interesting read "Big Ball of Mud" on Wikipedia.
---
## Modular Ethereum
![](https://docs.google.com/drawings/d/1f87beiJ7VakT9ififzN0f_NMO6UAOTdmDPSBioVXato/pub?w=653&h=544)
---
## Modular Ethereum
![](https://docs.google.com/drawings/d/1yquesWIOFG_sTMYypVMZsO2GbfhQy4uKRRCJD6as9EY/pub?w=653&h=544)
---
## Low Level Primitives
- RISC VM (like WebAssembly) <!-- .element: class="fragment" data-fragment-index="0" -->
- Non-atomic messaging <!-- .element: class="fragment" data-fragment-index="1" -->
- Filesystem like model for I/O (and create State Trie on top of it)<!-- .element: class="fragment" data-fragment-index="2" -->
- Microkernel for IPC management <!-- .element: class="fragment" data-fragment-index="3" -->
---
## Why do we need it?
- We don't need C, you can do it all Assembly! <!-- .element: class="fragment" data-fragment-index="0" -->
- We don't need Java/Python/Ruby, you can do it all in C! <!-- .element: class="fragment" data-fragment-index="1" -->
- 640K is more memory than anyone will ever need - Bill Gates <!-- .element: class="fragment" data-fragment-index="2" -->
- Give me any ethereum use-case & I'll tell you how to do it with bitcoin. - Paul Sztorc <!-- .element: class="fragment" data-fragment-index="3" -->
---
## Why do we need it?
![](https://upload.wikimedia.org/wikipedia/commons/0/00/Transistor_Count_and_Moore%27s_Law_-_2011.svg)
---
## Can there be a better way?
---
## What is WebAssembly?
> WebAssembly or wasm is a new, portable, size- and load-time-efficient binary format that aims to execute at native speed by taking advantage of common hardware capabilities available on a wide range of platforms.
---
## What is WebAssembly?
- Fast & Efficient <!-- .element: class="fragment" data-fragment-index="1" -->
- Secure Sandbox <!-- .element: class="fragment" data-fragment-index="2" -->
- Toolchain Compatibility <!-- .element: class="fragment" data-fragment-index="3" -->
- Extensible <!-- .element: class="fragment" data-fragment-index="4" -->
---
### W3C Working Group
www.w3.org/community/webassembly
- Participants (660) <!-- .element: class="fragment" data-fragment-index="1" -->
- Mozilla <!-- .element: class="fragment" data-fragment-index="2" -->
- Google <!-- .element: class="fragment" data-fragment-index="3" -->
- Microsoft <!-- .element: class="fragment" data-fragment-index="4" -->
- Apple <!-- .element: class="fragment" data-fragment-index="5" -->
---
### Instruction Set
- RISC like <!-- .element: class="fragment" data-fragment-index="1" -->
- Integer and floating point operations <!-- .element: class="fragment" data-fragment-index="2" -->
- Defined as an annotated stack machine <!-- .element: class="fragment" data-fragment-index="3" -->
- can be easily processed
- can be easily transformed
- can be interpreted with a Stack Machine
- or JITed
---
### Assembly represented in S-Expressions
```clojure=
(module
(import $useGas "ethereum" "useGas"
(param i32))
(memory 1)
(export "memory" memory)
(export "main" $main)
(func $main
(local $sp i32)
(local $jump_dest i32)
(set_local $sp
(i32.const -32))
(set_local $jump_dest
(i32.const -1))
(loop $done $loop
(block $1
(block $0
(br_table $0
(if
(i32.eq
(get_local $jump_dest)
(i32.const -1))
(then
(i32.const 0))
(else
(unreachable)))))
(call_import $useGas
(i32.const 6))
(if
(i32.gt_s
(get_local $sp)
(i32.const 32672))
(then
(unreachable)))
(call $PUSH
(i64.const 0)
(i64.const 0)
(i64.const 0)
(i64.const 96)
(get_local $sp))
(set_local $sp
(i32.add
(get_local $sp)
(i32.const 32)))
(call $PUSH
(i64.const 0)
(i64.const 0)
(i64.const 0)
(i64.const 0)
(get_local $sp))
(set_local $sp
(i32.add
(get_local $sp)
(i32.const 32)))))))
```
---
### Semantics
* Types<!-- .element: class="fragment" data-fragment-index="1" -->
* i32 (i32.<instr> <args..>)
* i64 (i64.<instr> <args..>)
* <!-- .element: class="fragment" data-fragment-index="2" --> (i64.const 42)
* <!-- .element: class="fragment" data-fragment-index="3" --> (local $a)
* Memory Access <!-- .element: class="fragment" data-fragment-index="4" -->
* (i64.load (i64.const 4))
* (i64.store (get_local $a) (i64.const 4))
* Arithmetics<!-- .element: class="fragment" data-fragment-index="5" -->
* Conditionals <!-- .element: class="fragment" data-fragment-index="6" -->
* <!-- .element: class="fragment" data-fragment-index="7" -->Calls
* (call_import $useGas (i64.const 33))
---
### Binary Encoding
```
0061 736d 0b00 0000 0474 7970 6500 0140 0201 0101 0108
6675 6e63 7469 6f6e 0001 0002 0665 7870 6f72 7400 0100
0206 6164 6454 776f 0463 6f64 6500 0100 0014
```
- Fast decoding <!-- .element: class="fragment" data-fragment-index="1" -->
- Single pass validation <!-- .element: class="fragment" data-fragment-index="2" -->
- Extensible <!-- .element: class="fragment" data-fragment-index="3" -->
---
### Binary Dissected
```python=
0000000: 0061 736d ; WASM_BINARY_MAGIC
0000004: 0b00 0000 ; WASM_BINARY_VERSION
; section "type"
0000008: 04 ; string length
0000009: 7479 7065 ; section id: "type"
000000d: 00 ; section size (guess)
000000e: 01 ; num types
; type 0
000000f: 40 ; function form
0000010: 02 ; num params
0000011: 01 ; param type
0000012: 01 ; param type
0000013: 01 ; num results
0000014: 01 ; result_type
; section "function"
0000015: 08 ; string length
0000016: 6675 6e63 7469 6f6e ; section id: "function"
000001e: 00 ; section size (guess)
000001f: 01 ; num functions
0000020: 00 ; function 0 signature index
000001e: 02 ; FIXUP section size
; section "export"
0000021: 06 ; string length
0000022: 6578 706f 7274 ; section id: "export"
0000028: 00 ; section size (guess)
0000029: 01 ; num exports
000002a: 00 ; export func index
000002b: 06 ; string length
000002c: 6164 6454 776f ; export name
; section "code"
0000032: 04 ; string length
0000033: 636f 6465 ; section id: "code"
0000037: 00 ; section size (guess)
0000038: 01 ; num functions
; function body 0
0000039: 00 ; func body size (guess)
000003a: 00 ; local decl count
000003b: 14 ; OPCODE_GET_LOCAL
```
---
### Availability of VMs
- <!-- .element: class="fragment" data-fragment-index="1" -->Chrome (chrome://flags - enable-webassembly)
- <!-- .element: class="fragment" data-fragment-index="2" -->Firefox Nightly (about:config and set javascript.options.wasm to true)
- <!-- .element: class="fragment" data-fragment-index="3" -->Microsoft Edge
- <!-- .element: class="fragment" data-fragment-index="4" -->node.js 7.0 (--enable-wasm)
- <!-- .element: class="fragment" data-fragment-index="5" -->wasm-jit-prototype (github.com/WebAssembly/wasm-jit-prototype)
- <!-- .element: class="fragment" data-fragment-index="6" -->Ocaml "the spec" (github.com/WebAssembly/spec)
- interpreters<!-- .element: class="fragment" data-fragment-index="7" -->
- wabt (github.com/WebAssembly/wabt)
- binaryen (github.com/WebAssembly/binaryen)
---
## Ethereum flavored WebAssembly `e^WASM`
- A proposed replacement for EVM<!-- .element: class="fragment" data-fragment-index="1" -->
- A restricted subset of WebAssembly<!-- .element: class="fragment" data-fragment-index="2" -->
- No floating point operations
- Defines Backwards Compatibility<!-- .element: class="fragment" data-fragment-index="3" -->
- Defines Metering<!-- .element: class="fragment" data-fragment-index="4" -->
- Defines an interface for interacting with the Ethereum Blockchain<!-- .element: class="fragment" data-fragment-index="5" -->
---
### EVM2WASM
- Transcompiles EVM code to WebAssembly<!-- .element: class="fragment" data-fragment-index="1" -->
- Can be run as a contract<!-- .element: class="fragment" data-fragment-index="2" -->
- Compliant with the offical EVM tests<!-- .element: class="fragment" data-fragment-index="3" -->
- Produces very fast EVM code<!-- .element: class="fragment" data-fragment-index="4" -->
- ~5000 line of wasm<!-- .element: class="fragment" data-fragment-index="5" -->
- ~400 line of js<!-- .element: class="fragment" data-fragment-index="6" -->
---
### EVM2WASM
![](https://i.imgur.com/KOsU7mB.png)
Visit: https://ewasm.github.io/evm2wasm-frontend/dist/
---
### Metering Injection
Metering is decoupled from the VM
```flow
st=>start: raw wasm
e=>end: run
op=>operation: Validate
op2=>operation: Injection Metering
cond=>condition: Yes or No?
st->op->op2->e
```
---
### Metering By Branch
![](https://i.imgur.com/axIXk25.jpg)
---
### Metering By Branch
![](https://i.imgur.com/dngglBx.jpg)
---
## Gas Cost Calculation
1) Gas cost is based on the cycle count of the instructions.<!-- .element: class="fragment" data-fragment-index="1" -->
2) "Gas cost oracle": The gas cost table is stored in a contract.<!-- .element: class="fragment" data-fragment-index="2" -->
3) SHA256 performance (of 8kb data):<!-- .element: class="fragment" data-fragment-index="3" -->
- precompile cost: ~3 100<!-- .element: class="fragment" data-fragment-index="3" -->
- eWASM cost: ~6 500<!-- .element: class="fragment" data-fragment-index="3" -->
- EVM (Solidity) cost: ~32 000 000<!-- .element: class="fragment" data-fragment-index="3" -->
- EVM (Solidity) cost (with shifts): ~1 000 000<!-- .element: class="fragment" data-fragment-index="3" -->
---
## Interfaces
<!-- Transition: by now you all are experts in WebAssembly and S-expressions. Let's glue all of this together. -->
*Glueing all this together*
---
### eWASM Ethereum Interface (EEI)
Defines the Ethereum blockchain methods for contracts:
- getCallDataSize ~ CALLDATASIZE
- callDataCopy ~ CALLDATACOPY
- storageLoad ~ SLOAD
- storageStore ~ SSTORE
- call ~ CALL
- delegateCall ~ DELEGATECALL
- return ~ RETURN
- ...
A total of 32 methods.
---
### Example: storageLoad
```clojure
(call_import $storageLoad (i32.const 0) (i32.const 32))
;; ^ key offset in memory
;; ^ result offset in memory
```
Simples.
---
<!-- Transition: now it is time to wrap all these calls into an actual contract. -->
### eWASM Contract Interface (ECI)
Defines the format of contracts:
- WebAssembly binary encoding version 0xd
- Single entry point: *main*
- Sets other limitations.
---
### Example contract
Drumroll:
```clojure
(module
(memory 1
(segment 0 "Hello World!")
)
(import $return "ethereum" "return" (param i32 i32))
(export "main" $main)
(func $main
(call_import $return (i32.const 0) (i32.const 12))
)
)
```
---
<!-- Transition: if we can write contract, we can also write system contracts -->
### System Contracts
1) Sentinel
- ECI validation
- metering injection
- wrap into deployer
2) evm2wasm! <!-- .element: class="fragment" data-fragment-index="3" -->
3) EVM1 precompiled contracts <!-- .element: class="fragment" data-fragment-index="4" -->
---
<!-- Transition: having system contracts simplifies the client implementation a lot -->
## VM Semantics
Only 3 new rules for a client:
1) Check contract code for eWASM signature <!-- .element: class="fragment" data-fragment-index="1" -->
2) Use evm2wasm to transcompile if needed <!-- .element: class="fragment" data-fragment-index="2" -->
3) Use sentinel contract during contract creation <!-- .element: class="fragment" data-fragment-index="3" -->
<!-- Note: transcompiled code is not stored in the state. -->
---
## ewasm-kernel and Hera
1) ewasm-kernel
- Javascript
- runs in browsers and node.js
- < 1000 lines for the interface and VM core
2) Hera
- C++
- *EVM-C compatible*
- works with cpp-ethereum (eth --vm hera)
- and pyethereum \o/
- ... and hopefully go-ethereum + parity in the future
---
<!-- Transition: don't be afraid, no need to write S-expressions. Contracts can be written in C and C++. -->
## Programming in C and the precompiles
*clang & ewasm-libc*
---
## Example: sha256
```c
#include "ethereum.h"
#define BASE_FEE 60
#define WORD_FEE 12
void main() {
size_t datasize = ethereum_calldatasize();
unsigned char data[datasize];
unsigned char ret[32] = { 0 };
ethereum_calldatacopy(data, 0, datasize);
ethereum_usegas(BASE_FEE + (((datasize + 31) / 32) * WORD_FEE));
mbedtls_sha256(data, datasize, ret, 0);
ethereum_return(ret, 32);
}
```
---
<!-- Transition: afraid of C? Solidity! -->
## Solidity for eWASM
Input:
```js
contract A {
function a() {
throw;
}
function b() payable returns (uint64) {
uint64 x = 42;
return x;
}
}
```
---
## Solidity for eWASM
Output:
```clojure
;; Contract: A
(module
(import $callDataCopy "ethereum" "callDataCopy" (param i32 i32 i32))
(import $getCallValue "ethereum" "getCallValue" (param i32))
(import $return "ethereum" "return" (param i32))
(memory 1 1)
(export "memory" memory)
(export "main" $main)
(func $main
(call_import $callDataCopy (i32.const 0) (i32.const 0) (i32.const 4))
(call $dispatcher (i32.load (i32.const 0)))
(unreachable)
)
(func $ensureNotPayable
(call_import $getCallValue (i32.const 0))
(if (i64.ne (i64.const 0) (i64.load (i32.const 0)) (unreachable))
(if (i64.ne (i64.const 0) (i64.load (i32.const 8)) (unreachable))
)
(func $dispatcher
(param $sig i32)
(if (i32.eq (get_local $sig) (i32.const 0x0dbe671f)) (then (call $__0dbe671f)))
(if (i32.eq (get_local $sig) (i32.const 0x4df7e3d0)) (then (call $__4df7e3d0)))
(unreachable)
)
;; function: a()
(func $__0dbe671f
(call $ensureNotPayable)
(block
(unreachable)
)
)
;; function: b() payable
(func $__4df7e3d0
(block
(local $x (param i64))
(set_local $x (i64.const 42))
(i64.store (i32.const 0) (i64.const 0))
(i64.store (i32.const 8) (i64.const 0))
(i64.store (i32.const 16) (i64.const 0))
(i64.store (i32.const 24) (get_local $x))
(call_import $return (i32.const 0) (i32.const 32))
)
)
)
```
---
## (call_import $ethereumReturn)
See more awesome details at https://github.com/ewasm
>:::danger
>:fire: Error: Out-Of-Gas! :fire:
>:::
<!-- .element: class="fragment" data-fragment-index="2" -->
{"metaMigratedAt":"2023-06-14T12:22:22.508Z","metaMigratedFrom":"Content","title":"Toward an Extensible Blockchain","breaks":true,"contributors":"[]"}