Nomos : Resource-Aware Session Types for Programming Digital - - PowerPoint PPT Presentation

nomos resource aware session types for programming
SMART_READER_LITE
LIVE PREVIEW

Nomos : Resource-Aware Session Types for Programming Digital - - PowerPoint PPT Presentation

Programming language developed at Carnegie Mellon Nomos : Resource-Aware Session Types for Programming Digital Contracts Stephanie Balzer, Ankush Das, Jan Ho ff mann, and Frank Pfenning With some slides from Ankush. ETH 2019 Digital


slide-1
SLIDE 1

Stephanie Balzer, Ankush Das, Jan Hoffmann, and Frank Pfenning

Nomos: Resource-Aware Session Types for
 Programming Digital Contracts

ETH 2019

Programming language developed at Carnegie Mellon With some slides from Ankush.

slide-2
SLIDE 2

Digital Contracts (or Smart Contracts)

Smart contracts (Ethereum): programs stored on a blockchain

  • Carry out (financial) transactions between (untrusted) agents
  • Cannot be modified but have state
  • Community needs to reach consensus on the result of execution
  • Users need to pay for the execution cost upfront
slide-3
SLIDE 3

Digital Contracts (or Smart Contracts)

Smart contracts (Ethereum): programs stored on a blockchain

  • Carry out (financial) transactions between (untrusted) agents
  • Cannot be modified but have state
  • Community needs to reach consensus on the result of execution
  • Users need to pay for the execution cost upfront

smart contract

user miner

money (estimated gas cost) transaction new block sufficient gas? remaining gas

slide-4
SLIDE 4

Bugs in Digital Contracts are Expensive

  • Bugs result in financial disasters (DAO, Parity Wallet, King of Ether, …)
  • Bugs are difficult to fix because they alter the contract
slide-5
SLIDE 5

Can Programming Languages Prevent Bugs?

slide-6
SLIDE 6

Can Programming Languages Prevent Bugs?

Yes!

slide-7
SLIDE 7

Can Programming Languages Prevent Bugs?

Example: memory safety

  • Most security vulnerabilities are based on memory safety issues


(Microsoft: 70% over past in the past 12 years in MS products)

  • Why stick with unsafe languages?


Legacy code, developers (training, social factors, …) Yes!

slide-8
SLIDE 8

Can Programming Languages Prevent Bugs?

Example: memory safety

  • Most security vulnerabilities are based on memory safety issues


(Microsoft: 70% over past in the past 12 years in MS products)

  • Why stick with unsafe languages?


Legacy code, developers (training, social factors, …) Yes! Languages for Digital Contracts

  • Great opportunity to start from a clean slate
  • Correctness and readability of contracts are priorities
slide-9
SLIDE 9

Can Programming Languages Prevent Bugs?

Example: memory safety

  • Most security vulnerabilities are based on memory safety issues


(Microsoft: 70% over past in the past 12 years in MS products)

  • Why stick with unsafe languages?


Legacy code, developers (training, social factors, …) Yes! Languages for Digital Contracts

  • Great opportunity to start from a clean slate
  • Correctness and readability of contracts are priorities

Nomos

  • Build on state-of-the art: statically-typed, strict, functional language
  • Address domain-specific issues
slide-10
SLIDE 10

Domain Specific Bugs: Auction Contract

status: running

slide-11
SLIDE 11

Domain Specific Bugs: Auction Contract

Bidder 1 Bidder 3 Bidder 2 Bid 1 Bid 2 Bid 3 status: running

slide-12
SLIDE 12

Domain Specific Bugs: Auction Contract

Bidder 1 Bidder 3 Bidder 2 Bid 1 Bid 2 Bid 3 status: running

slide-13
SLIDE 13

Domain Specific Bugs: Auction Contract

Bidder 1 Bidder 3 Bidder 2 Bid 1 Bid 2 Bid 3 status: ended

slide-14
SLIDE 14

Domain Specific Bugs: Auction Contract

Bidder 1 Bidder 3 Bidder 2 Bid 1 Bid 2 Bid 3 status: ended

slide-15
SLIDE 15

Domain Specific Bugs: Auction Contract

Bidder 1 Bidder 3 Bidder 2 Bid 1 Bid 2 Bid 3 status: ended

slide-16
SLIDE 16

Auction Contract in Solidity

slide-17
SLIDE 17

Auction in Solidity

slide-18
SLIDE 18

Auction in Solidity

slide-19
SLIDE 19

Auction in Solidity

What happens if collect is called when auction is running?

slide-20
SLIDE 20

Auction in Solidity

What happens if collect is called when auction is running? add require (status == ended);

slide-21
SLIDE 21

Auction in Solidity

What happens if collect is called when auction is running? add require (status == ended);

Protocol is not statically enforced!

slide-22
SLIDE 22

Auction in Solidity

slide-23
SLIDE 23

Auction in Solidity

slide-24
SLIDE 24

Auction in Solidity

What happens if collect is called twice?

slide-25
SLIDE 25

Auction in Solidity

What happens if collect is called twice? set pendingReturns[msg.sender] = 0

slide-26
SLIDE 26

Auction in Solidity

What happens if collect is called twice? set pendingReturns[msg.sender] = 0

Linearity is not enforced!

slide-27
SLIDE 27

Auction in Solidity

slide-28
SLIDE 28

Auction in Solidity

slide-29
SLIDE 29

Auction in Solidity

Method ‘send’ potentially transfers control to other contract.

slide-30
SLIDE 30

Auction in Solidity

Method ‘send’ potentially transfers control to other contract. ‘send’ should be the last instruction.

slide-31
SLIDE 31

Auction in Solidity

Method ‘send’ potentially transfers control to other contract. ‘send’ should be the last instruction.

Re-entrancy attack

slide-32
SLIDE 32

Auction in Solidity

slide-33
SLIDE 33

Auction in Solidity

slide-34
SLIDE 34

Auction in Solidity

Method ‘send’ potentially transfers control to other contract.

slide-35
SLIDE 35

Auction in Solidity

Method ‘send’ potentially transfers control to other contract. Need to check return value of ‘send’.

slide-36
SLIDE 36

Auction in Solidity

Method ‘send’ potentially transfers control to other contract. Need to check return value of ‘send’.

Out-of-gas exception.

slide-37
SLIDE 37

Domain-Specific Issues with Digital Contracts

  • 1. Resource consumption (gas cost)
  • Participants have to agree on the result of a computation

➡ Denial of service attacks ➡ Would like to have static gas bounds

slide-38
SLIDE 38

Domain-Specific Issues with Digital Contracts

  • 1. Resource consumption (gas cost)
  • Participants have to agree on the result of a computation

➡ Denial of service attacks ➡ Would like to have static gas bounds

  • 2. Contract protocols and interfaces
  • Contract protocols should be described and enforced

➡ Prevent issues like reentrancy bugs (DAO)

slide-39
SLIDE 39

Domain-Specific Issues with Digital Contracts

  • 1. Resource consumption (gas cost)
  • Participants have to agree on the result of a computation

➡ Denial of service attacks ➡ Would like to have static gas bounds

  • 2. Contract protocols and interfaces
  • Contract protocols should be described and enforced

➡ Prevent issues like reentrancy bugs (DAO)

  • 3. Keeping track of assets (crypto coins)
  • Assets should not be duplicated
  • Assets should not be lost
slide-40
SLIDE 40

Nomos: A Type-Based Approach

A statically-typed, strict, functional language

  • Functional fragment of ML

Additional features for domain-specific requirements

Language feature Expertise Gas bounds Automatic amortized resource analysis Jan Hoffmann Tracking assets Linear type system Frank Pfenning Contract interfaces Shared binary session types Stephanie Balzer
 Frank Pfenning Lead developer: Ankush Das

slide-41
SLIDE 41

Nomos: A Type-Based Approach

A statically-typed, strict, functional language

  • Functional fragment of ML

Additional features for domain-specific requirements

Language feature Expertise Gas bounds Automatic amortized resource analysis Jan Hoffmann Tracking assets Linear type system Frank Pfenning Contract interfaces Shared binary session types Stephanie Balzer
 Frank Pfenning Lead developer: Ankush Das

Based on a linear type system

slide-42
SLIDE 42
  • 1. Automatic amortized resource analysis (AARA)
slide-43
SLIDE 43

Resource Bound Analysis

Given: A (functional) program P Question: What is the (worst-case) resource consumption of P as a function of the size of its inputs?

slide-44
SLIDE 44

Resource Bound Analysis

Given: A (functional) program P Question: What is the (worst-case) resource consumption of P as a function of the size of its inputs? Clock cycles, heap space, gas, ...

slide-45
SLIDE 45

Resource Bound Analysis

Given: A (functional) program P Question: What is the (worst-case) resource consumption of P as a function of the size of its inputs? Clock cycles, heap space, gas, ... Not only asymptotic bounds but concrete constant factors.

slide-46
SLIDE 46

Resource Bound Analysis

Given: A (functional) program P Question: What is the (worst-case) resource consumption of P as a function of the size of its inputs? Clock cycles, heap space, gas, ... Not only asymptotic bounds but concrete constant factors.

Automatic

slide-47
SLIDE 47

Resource Bound Analysis

Given: A (functional) program P Question: What is the (worst-case) resource consumption of P as a function of the size of its inputs? Clock cycles, heap space, gas, ... Not only asymptotic bounds but concrete constant factors.

Automatic

Goal: produce proofs (easily checkable)

slide-48
SLIDE 48
  • Assign potential functions to data structures

➡ States are mapped to non-negative numbers

  • Potential pays the resource consumption and

the potential at the following program point

  • Initial potential is an upper bound

AARA: Use Potential Method

Φ(state) ≥ 0 Φ(before) ≥ Φ(after) + cost Φ(initial state) ≥ P cost

telescoping

slide-49
SLIDE 49
  • Assign potential functions to data structures

➡ States are mapped to non-negative numbers

  • Potential pays the resource consumption and

the potential at the following program point

  • Initial potential is an upper bound

AARA: Use Potential Method

Φ(state) ≥ 0 Φ(before) ≥ Φ(after) + cost Φ(initial state) ≥ P cost Type systems for automatic analysis

  • Fix a format of potential functions (basis like in linear algebra)
  • Type rules introduce linear constraint on coefficients

telescoping

slide-50
SLIDE 50
  • Assign potential functions to data structures

➡ States are mapped to non-negative numbers

  • Potential pays the resource consumption and

the potential at the following program point

  • Initial potential is an upper bound

AARA: Use Potential Method

Φ(state) ≥ 0 Φ(before) ≥ Φ(after) + cost Φ(initial state) ≥ P cost Type systems for automatic analysis

  • Fix a format of potential functions (basis like in linear algebra)
  • Type rules introduce linear constraint on coefficients

telescoping

Clear soundness theorem.

  • Compositional. Efficient inference.
slide-51
SLIDE 51

Example: Append for Persistent Lists

Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer)

append(x,y)

slide-52
SLIDE 52

Example: Append for Persistent Lists

Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-53
SLIDE 53

Example: Append for Persistent Lists

c b

x

a

y

d e

Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-54
SLIDE 54

Example: Append for Persistent Lists

c b

x

a

y

d e c

Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-55
SLIDE 55

Example: Append for Persistent Lists

c b

x

a

y

d e c b

Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-56
SLIDE 56

Example: Append for Persistent Lists

c b

x

a

y

d e c b a

Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-57
SLIDE 57

Example: Append for Persistent Lists

c b

x

a

y

d e c b a

append(x,y) Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-58
SLIDE 58

Example: Append for Persistent Lists

c b

x

a

y

d e c b a

append(x,y) Heap usage: 2*n = 2*3 = 6 Heap-space usage is 2n if

  • n is the length of list x
  • One list element requires two heap cells


(data and pointer) Example evaluation:

append(x,y)

slide-59
SLIDE 59

Example: Composing Calls of Append

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-60
SLIDE 60

Example: Composing Calls of Append

c b

x

a

y

d e

Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 4 4 4 2 2 z

f

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-61
SLIDE 61

Example: Composing Calls of Append

c b

x

a

y

d e

Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 4 4 4 append(x,y) 2 2 z

f

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-62
SLIDE 62

Example: Composing Calls of Append

c b

x

a

y

d e

Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 4 4 append(x,y) 2 2 z

f c 2

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-63
SLIDE 63

Example: Composing Calls of Append

c b

x

a

y

d e

Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 4 append(x,y) 2 2 z

f c 2 b

2 Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-64
SLIDE 64

Example: Composing Calls of Append

c b

x

a

y

d e

Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 append(x,y) 2 2 z

f c 2 b

2

a2

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-65
SLIDE 65

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 2 2 z

f c 2 b

2

a2

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-66
SLIDE 66

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 2 2 z

f c 2 b

2

a2

append(t,z) Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-67
SLIDE 67

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 2 z

f c 2 b

2

a2 e

append(t,z) Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-68
SLIDE 68

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 z

f c 2 b

2

a2 e d

append(t,z) Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-69
SLIDE 69

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 z

f c b

2

a2 e d c

append(t,z) Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-70
SLIDE 70

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 z

f c b a2 e d c b

append(t,z) Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-71
SLIDE 71

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 z

f c b a e d c b a

append(t,z) Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-72
SLIDE 72

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 z

f c b a

append(t,z)

e d c b a

Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-73
SLIDE 73

Example: Composing Calls of Append

c b

x

a

y

d e

t Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16 z

f c b a

append(t,z)

e d c b a

Implicit reasoning about size-changes. Heap usage of f(x,y,z) is 2n + 2(n+m) if

  • n is the length of list x
  • m is the length of list y

f(x,y,z) = let t = append(x,y) in append(t,z)

slide-74
SLIDE 74

Example: Composing Calls of Append

The most general type of append is specialized at call-sites:

f(x,y,z) = { let t = append(x,y) in append(t,z) } append: (L (int),L (int)) ----> L (int) 2 0/0 append: (L (int),L (int)) ----> L (int) 4 2 0/0 2 append: (L (int),L (int)) ----> L (int) | Φ q r s/t p

Linear constraints.

slide-75
SLIDE 75

Polynomial Potential Functions

User-defined resource metrics
 (i.e., by tick(q) in the code) Naturally compositional: tracks size changes, types are specifications Bound inference by reduction to efficient LP solving Type derivations prove bounds with respect to the cost semantics Linear Potential Functions

✓ ✓ ✓ ✓

slide-76
SLIDE 76

Polynomial Potential Functions

User-defined resource metrics
 (i.e., by tick(q) in the code) Naturally compositional: tracks size changes, types are specifications Bound inference by reduction to efficient LP solving Type derivations prove bounds with respect to the cost semantics Linear Potential Functions

✓ ✓ ✓ ✓

Strong soundness theorem.

slide-77
SLIDE 77

Polynomial Potential Functions

User-defined resource metrics
 (i.e., by tick(q) in the code) Naturally compositional: tracks size changes, types are specifications Bound inference by reduction to efficient LP solving Type derivations prove bounds with respect to the cost semantics Linear Potential Functions

✓ ✓ ✓ ✓

Multivariate Polynomial Potential Functions

✓ ✓ ✓ ✓

Strong soundness theorem.

slide-78
SLIDE 78

Polynomial Potential Functions

User-defined resource metrics
 (i.e., by tick(q) in the code) Naturally compositional: tracks size changes, types are specifications Bound inference by reduction to efficient LP solving Type derivations prove bounds with respect to the cost semantics Linear Potential Functions

✓ ✓ ✓ ✓

Multivariate Polynomial Potential Functions

✓ ✓ ✓ ✓

For example m*n2. Strong soundness theorem.

slide-79
SLIDE 79

Implementations: RaML and Absynth

Resource Aware ML (RaML)

  • Based on Inria’s OCaml compiler
  • Polymorphic and higher-order functions
  • User-defined data types
  • Side effects (arrays and references)

Absynth

  • Based on control-flow graph IR
  • Different front ends
  • Bounds are integer expressions
  • Supports probabilistic programs

http://raml.co

slide-80
SLIDE 80

Micro Benchmarks

Evaluation-Step Bounds

Computed Bound Actual Behavior Analysis
 Runtime Constraints

Sorting A-nodes (asort) 11+22kn +13k2nv+13m +15n O(k2n+m) 0.14 s 5656 Quick sort (lists of lists) 3 -7.5nm +7.5nm2 +19.5m +16.5m2 O(nm2) 0.27 s 8712 Merge sort (list.ml) 43 + 30.5n + 8.5n2 O(n log n) 0.11 s 3066 Split and sort 11 + 47n + 29n2 O(n2) 0.69 s 3793 Longest common subsequence 23 + 10n + 52nm + 25m O(nm) 0.16 s 901 Matrix multiplication 3 + 2nm +18m + 22mxy +16my O(mxy) 1.11 s 3901 Evaluator for boolean expressions (tutorial) 10+11n+16m+16mx+16my+20x+20y O(mx+my) 0.33 s 1864 Dijkstra’s shortest-path algorithm 46 + 33n +111n2 O(n2) 0.11 s 2808 Echelon form 8 + 43m2n + 59m + 63m2 O(nm2) 1.81 s 8838 Binary multiplication (CompCert) 2+17kr+10ks+25k +8l+2+7r+8 O(kr+ks) 14.04 s 89,507 Square root (CompCert) 13+66m+16mn +4m2 +59n +4n2 O(n2) 18.25 s 135,529

slide-81
SLIDE 81

Quick Sort for Integers

Evaluation-step bound vs. measured behavior

slide-82
SLIDE 82

Longest Common Subsequence

Evaluation-step bound vs. measured behavior

slide-83
SLIDE 83

Longest Common Subsequence

Evaluation-step bound vs. measured behavior First automatically derived bound for LCS.

slide-84
SLIDE 84

Automatic Amortized Resource Analysis (AARA)

Type system for deriving symbolic resource bounds

  • Compositional: Integrated with type systems or program logics
  • Expressive: Bounds are multivariate resource polynomials
  • Reliable: Formal soundness proof wrt. cost semantics
  • Verifiable: Produces easily-checkable certificates
  • Automatic: No user interaction required

Applicable in practice

  • Implemented: Resource Aware ML and Absynth
  • Effective: Works for many typical programs
  • Efficient: Inference via linear programming
slide-85
SLIDE 85

Automatic Amortized Resource Analysis (AARA)

Type system for deriving symbolic resource bounds

  • Compositional: Integrated with type systems or program logics
  • Expressive: Bounds are multivariate resource polynomials
  • Reliable: Formal soundness proof wrt. cost semantics
  • Verifiable: Produces easily-checkable certificates
  • Automatic: No user interaction required

Applicable in practice

  • Implemented: Resource Aware ML and Absynth
  • Effective: Works for many typical programs
  • Efficient: Inference via linear programming

Type checking in linear time!

slide-86
SLIDE 86
  • 2. Shared (resource-aware) binary session types
slide-87
SLIDE 87

Binary Session Types

  • Implement message-passing concurrent programs
  • Communication via typed bidirectional channels
  • Curry-Howard correspondence with intuitionistic linear logic
  • Client and provider have dual types

Example type:

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-88
SLIDE 88

Binary Session Types

  • Implement message-passing concurrent programs
  • Communication via typed bidirectional channels
  • Curry-Howard correspondence with intuitionistic linear logic
  • Client and provider have dual types

Example type:

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

Internal choice External choice

slide-89
SLIDE 89

Binary Session Types

  • Implement message-passing concurrent programs
  • Communication via typed bidirectional channels
  • Curry-Howard correspondence with intuitionistic linear logic
  • Client and provider have dual types

Example type:

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

Receive msg of type A Send msg of type A

slide-90
SLIDE 90

Binary Session Types

  • Implement message-passing concurrent programs
  • Communication via typed bidirectional channels
  • Curry-Howard correspondence with intuitionistic linear logic
  • Client and provider have dual types

Example type:

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

Type soundness (progress and preservation) implies deadlock freedom Receive msg of type A Send msg of type A

slide-91
SLIDE 91

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-92
SLIDE 92

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

recv ‘ins’ and y

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-93
SLIDE 93

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

recv ‘ins’ and y send ‘ins’ and y

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-94
SLIDE 94

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

recv ‘ins’ and y send ‘ins’ and y recurse

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-95
SLIDE 95

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

recv ‘ins’ and y send ‘ins’ and y recurse send ‘some’, x

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-96
SLIDE 96

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

recv ‘ins’ and y send ‘ins’ and y recurse send ‘some’, x terminate

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

slide-97
SLIDE 97

Example: Queue

elemx x : A

s : queueA t : queueA

tail of queue head of queue (element stored) empty elemz

recv ‘ins’ and y send ‘ins’ and y recurse send ‘some’, x terminate

queueA = &{ins : A ( queueA, del : ⊕{none : 1, some : A ⊗ queueA}}

Type checking in linear time!

slide-98
SLIDE 98

Example: Auction

slide-99
SLIDE 99

Example: Auction

sends status

  • f auction
slide-100
SLIDE 100

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding
slide-101
SLIDE 101

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding

receive id and money

slide-102
SLIDE 102

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding

receive id and money recurse

slide-103
SLIDE 103

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding

receive id and money recurse

  • ffers choice

to collect

slide-104
SLIDE 104

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding

receive id and money recurse

  • ffers choice

to collect sends result

  • f bidding
slide-105
SLIDE 105

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding

receive id and money recurse

  • ffers choice

to collect sends result

  • f bidding

send Mona Lisa

slide-106
SLIDE 106

Example: Auction

sends status

  • f auction
  • ffers choice
  • f bidding

receive id and money recurse

  • ffers choice

to collect sends result

  • f bidding

send Mona Lisa send back money

slide-107
SLIDE 107

Resource-Aware Session Types

  • Each process stores potential in functional data
  • Potential can be transferred via messages
  • Potential is used to pay for performed work
slide-108
SLIDE 108

Resource-Aware Session Types

  • Each process stores potential in functional data
  • Potential can be transferred via messages
  • Potential is used to pay for performed work

Potential transfer

  • nly at the type level,

not at runtime.

slide-109
SLIDE 109

Resource-Aware Session Types

  • Each process stores potential in functional data
  • Potential can be transferred via messages
  • Potential is used to pay for performed work

Potential transfer

  • nly at the type level,

not at runtime. User-defined cost metric.

slide-110
SLIDE 110

Resource-Aware Session Types

  • Each process stores potential in functional data
  • Potential can be transferred via messages
  • Potential is used to pay for performed work

Potential transfer

  • nly at the type level,

not at runtime. User-defined cost metric.

  • Message potential is a function of (functional) payload

A, B, C ::= τ ⊃ A input value of type τ and continue as A | τ ∧ A

  • utput value of type τ and continue as A

(

L (int) 2

t

b

2

a2

slide-111
SLIDE 111

Resource-Aware Session Types

  • Each process stores potential in functional data
  • Potential can be transferred via messages
  • Potential is used to pay for performed work

Potential transfer

  • nly at the type level,

not at runtime. User-defined cost metric.

A ::= . . . | .rA | /rA the provider to pay units of potential which ` ; ; `

q get xm {r} ; P ::

( ) `

m : r ) ` q pay xm {r} ; P ::

  • Only in intermediate language:
  • Syntactic sugar (no payload)
  • Message potential is a function of (functional) payload

A, B, C ::= τ ⊃ A input value of type τ and continue as A | τ ∧ A

  • utput value of type τ and continue as A

(

L (int) 2

t

b

2

a2

slide-112
SLIDE 112

Resource-Aware Session Types

  • Each process stores potential in functional data
  • Potential can be transferred via messages
  • Potential is used to pay for performed work

Potential transfer

  • nly at the type level,

not at runtime. User-defined cost metric. Efficient type inference via LP solving

A ::= . . . | .rA | /rA the provider to pay units of potential which ` ; ; `

q get xm {r} ; P ::

( ) `

m : r ) ` q pay xm {r} ; P ::

  • Only in intermediate language:
  • Syntactic sugar (no payload)
  • Message potential is a function of (functional) payload

A, B, C ::= τ ⊃ A input value of type τ and continue as A | τ ∧ A

  • utput value of type τ and continue as A

(

L (int) 2

t

b

2

a2

slide-113
SLIDE 113

Example: Type of an Auction Contract

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-114
SLIDE 114

Example: Type of an Auction Contract

Sharing: Need to acquire contract before use.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-115
SLIDE 115

Example: Type of an Auction Contract

Sharing: Need to acquire contract before use. Equi-synchronizing: Release contract at the same type.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-116
SLIDE 116

Example: Type of an Auction Contract

Action can be open (running) or closed (ended).

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-117
SLIDE 117

Example: Type of an Auction Contract

Action can be open (running) or closed (ended). Sending a functional value.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-118
SLIDE 118

Example: Type of an Auction Contract

Action can be open (running) or closed (ended). Sending a functional value. Sending a
 linear value.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-119
SLIDE 119

Example: Type of an Auction Contract

Action can be open (running) or closed (ended). Sending a functional value. Sending a
 linear value. Can collect lot or reclaim your bid.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-120
SLIDE 120

Example: Type of an Auction Contract

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-121
SLIDE 121

Example: Type of an Auction Contract

At the beginning, you have to pay 11 units to cover the worst-case gas cost.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-122
SLIDE 122

Example: Type of an Auction Contract

At the beginning, you have to pay 11 units to cover the worst-case gas cost.

Gas cost is given by a cost semantics and the type system ensures 11 is the worst-case.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-123
SLIDE 123

Example: Type of an Auction Contract

At the beginning, you have to pay 11 units to cover the worst-case gas cost.

Gas cost is given by a cost semantics and the type system ensures 11 is the worst-case. If the worst-case path is not taken then the leftover is returned.

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-124
SLIDE 124

Example: Type of an Auction Contract

At the beginning, you have to pay 11 units to cover the worst-case gas cost.

Gas cost is given by a cost semantics and the type system ensures 11 is the worst-case. If the worst-case path is not taken then the leftover is returned. This is the worst- case path

controlled functional type sys- the integra- (Section 4) status in the d session ↑S

L /11 ⊕ {running : N{bid : id ⊃ money ( .1 ↓S L auction,

cancel : .8 ↓S

L auction},

ended : N{collect : id ⊃ ⊕{won : lot ⊗ .3 ↓S

L auction,

lost : money⊗ ↓S

L auction},

cancel : .8 ↓S

L auction}}

Since there exist multiple bidders in an auction, we use a ↓L lost : money⊗ ↓S

L auction}

=

slide-125
SLIDE 125

Implementation of a Running Auction

slide-126
SLIDE 126

Implementation of a Running Auction

accept ‘acquire’ (↑S

L )

slide-127
SLIDE 127

Implementation of a Running Auction

accept ‘acquire’ (↑S

L )

send status ‘running’

slide-128
SLIDE 128

Implementation of a Running Auction

accept ‘acquire’ (↑S

L )

send status ‘running’ recv ‘id’ and ‘money’

slide-129
SLIDE 129

Implementation of a Running Auction

accept ‘acquire’ (↑S

L )

send status ‘running’ recv ‘id’ and ‘money’ detach from client (↓S

L)

slide-130
SLIDE 130

Implementation of a Running Auction

accept ‘acquire’ (↑S

L )

send status ‘running’ recv ‘id’ and ‘money’ detach from client (↓S

L)

add bid and money

slide-131
SLIDE 131

Implementation of a Running Auction

accept ‘acquire’ (↑S

L )

send status ‘running’ recv ‘id’ and ‘money’ detach from client (↓S

L)

add bid and money no work constructs!

slide-132
SLIDE 132

How to Use the Potential

Payment schemes (amortized cost)

  • Ensure constant gas cost in the presence of costly operations
  • Overcharge for cheap operations and store gas in contract
  • Similar to storing ether in memory in EVM but part of contract

Explicit gas bounds

  • Add an additional argument that carries potential
  • User arg N ~ maximal number of players => gas bound is 81*N + 28

Enforce constant gas cost

  • Simply disable potential in contract state
  • Require messages to only carry constant potential
slide-133
SLIDE 133

Computation on a Blockchain

Blockchain state: shared processes waiting to be acquired

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

slide-134
SLIDE 134

Computation on a Blockchain

Blockchain state: shared processes waiting to be acquired

Contracts store functional and linear data.

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

slide-135
SLIDE 135

Computation on a Blockchain

Blockchain state: shared processes waiting to be acquired

Contracts store functional and linear data. Channel name = address

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

slide-136
SLIDE 136

Computation on a Blockchain

Blockchain state: shared processes waiting to be acquired

Contracts store functional and linear data. Channel name = address

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

Transaction: client submits code of a linear process

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

client

slide-137
SLIDE 137

Computation on a Blockchain

  • Client process can acquire existing contracts
  • Client process can spawn new (shared) processes -> new contracts
  • Client process needs to terminates in a new valid state

Blockchain state: shared processes waiting to be acquired

Contracts store functional and linear data. Channel name = address

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

Transaction: client submits code of a linear process

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

client

slide-138
SLIDE 138

Computation on a Blockchain

  • Client process can acquire existing contracts
  • Client process can spawn new (shared) processes -> new contracts
  • Client process needs to terminates in a new valid state

Blockchain state: shared processes waiting to be acquired

Contracts store functional and linear data. Channel name = address

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

Transaction: client submits code of a linear process

contr1(~ u1,~ v1)

c1 c2 cn

contrn(~ un,~ vn) contr2(~ u2,~ v2)

client

Contract should have default clients.

slide-139
SLIDE 139

Blockchain, Type Checking, and Verification

Type checking is part of the attack surface

  • Contract code can checked at publication time
  • User code needs to be checked for each transaction
  • Denial of service attacks are possible
  • Nomos type checking is linear in the size of the program

Verification of Nomos program is possible

  • Dynamic semantics specifies runtime behavior
  • Directly applicable to verification in Coq
  • Nomos’ type system guaranties some important properties
slide-140
SLIDE 140

Nomos

References A statically-typed, strict, functional language for digital contracts

  • Automatic amortized resource analysis for static gas bounds
  • Shared binary session types for transparent & safe contract interfaces
  • Linear type system for accurately reflecting assets

Ongoing work: implementation

  • Parser
  • Type checker
  • Interpreter
  • Compiler

✓ ✓

  • POPL ’17: AARA for OCaml (RaML)
  • LICS ’18: Resource-Aware Session Types
  • arXiv ’19: Nomos
slide-141
SLIDE 141

Nomos

References A statically-typed, strict, functional language for digital contracts

  • Automatic amortized resource analysis for static gas bounds
  • Shared binary session types for transparent & safe contract interfaces
  • Linear type system for accurately reflecting assets

Ongoing work: implementation

  • Parser
  • Type checker
  • Interpreter
  • Compiler

✓ ✓

  • POPL ’17: AARA for OCaml (RaML)
  • LICS ’18: Resource-Aware Session Types
  • arXiv ’19: Nomos

Collaborators: Stephanie Balzer, Ankush Das, and Frank Pfenning