![Page 1: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/1.jpg)
Ivy: A Declarative Predicate Language for Smart ContractsDan Robinson Product Architect, Chain
J A N U A R Y 2 0 1 7
![Page 2: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/2.jpg)
Introduction: Two Blockchain Models
2
Mutable State (Accounts and Contracts) Immutable State (UTXOs)
Examples Ethereum Bitcoin, Chain, Corda
State Stored in accounts and “contract” objects Stored in “unspent transaction outputs” (UTXOs)
Transactions Trigger messages between accounts and contracts
Consume and create new UTXOS
Programs Associated with contract objects
Alter state and send messages
Associated with UTXOs
Either succeed (authorizing spending of the UTXO) or fail
![Page 3: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/3.jpg)
Smart contract development in Ethereum
3
EVM Assembly Solidity
![Page 4: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/4.jpg)
Smart contract development in Bitcoin
4
???
Bitcoin Script
![Page 5: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/5.jpg)
Smart contract development in Bitcoin
5
IvyBitcoin Script
![Page 6: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/6.jpg)
Ivy
6
• Predicate language (Δ0) — programs either succeed or fail
• Programs guard value in a UTXO
• Designed for writing programs for the Chain VM
• A (limited) dialect compiles to Bitcoin Script
![Page 7: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/7.jpg)
Ivy — Benefits
7
• Abstracts away stack manipulation (OP_DUP, OP_SWAP) in favor of
named variables
• Avoids unfamiliar postfix notation (1 1 OP_ADD 2 OP_EQUAL OP_VERIFY)
• Easier to write, read, and teach
![Page 8: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/8.jpg)
Ivy Examples
8
![Page 9: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/9.jpg)
PayToPubKey
9
program p2pk(pubKey: PublicKey) { path spend(sig: Signature) { verify checkSig(pubKey, sig) } }
<pubKey> OP_CHECKSIG <sig>
Arguments (“ScriptSig”) Program (“ScriptPubKey”)
![Page 10: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/10.jpg)
PayToPubKey
10
program p2pk(pubKey: PublicKey) { path spend(sig: Signature) { verify checkSig(pubKey, sig) } }
<pubKey> OP_CHECKSIG <sig>
Arguments (“ScriptSig”) Program (“ScriptPubKey”)
![Page 11: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/11.jpg)
PayToPubKey
11
program p2pk(pubKey: PublicKey) { path spend(sig: Signature) { verify checkSig(pubKey, sig) } }
Arguments (“ScriptSig”) Program (“ScriptPubKey”)
<pubKey> OP_CHECKSIG <sig>
![Page 12: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/12.jpg)
PayToPubKey
12
program p2pk(pubKey: PublicKey) { path spend(sig: Signature) { verify checkSig(pubKey, sig) } }
<pubKey> OP_CHECKSIG <sig>
Arguments (“ScriptSig”) Program (“ScriptPubKey”)
![Page 13: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/13.jpg)
PayToPubKeyHash
13
program p2pkh(pkHash: Hash) { path spend(pubKey: PublicKey, sig: Signature) { verify hash160(pubKey) == pkHash verify checkSig(pubKey, sig) } }
<sig> <pubKey>
OP_DUP OP_HASH160 <pkHash> OP_EQUALVERIFY OP_CHECKSIG
![Page 14: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/14.jpg)
PayToPubKeyHash
14
program p2pkh(pkHash: Hash) { path spend(pubKey: PublicKey, sig: Signature) { verify hash160(pubKey) == pkHash verify checkSig(pubKey, sig) } }
<sig> <pubKey>
OP_DUP OP_HASH160 <pkHash> OP_EQUALVERIFY OP_CHECKSIG
![Page 15: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/15.jpg)
PayToPubKeyHash
15
program p2pkh(pkHash: Hash) { path spend(pubKey: PublicKey, sig: Signature) { verify hash160(pubKey) == pkHash verify checkSig(pubKey, sig) } }
<sig> <pubKey>
OP_DUP OP_HASH160 <pkHash> OP_EQUALVERIFY OP_CHECKSIG
![Page 16: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/16.jpg)
PayToPubKeyHash
16
program p2pkh(pkHash: Hash) { path spend(pubKey: PublicKey, sig: Signature) { verify hash160(pubKey) == pkHash verify checkSig(pubKey, sig) } }
<sig> <pubKey>
OP_DUP OP_HASH160 <pkHash> OP_EQUALVERIFY OP_CHECKSIG
![Page 17: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/17.jpg)
PayToPubKeyHash
17
program p2pkh(pkHash: Hash) { path spend(pubKey: PublicKey, sig: Signature) { verify hash160(pubKey) == pkHash verify checkSig(pubKey, sig) } }
<sig> <pubKey>
OP_DUP OP_HASH160 <pkHash> OP_EQUALVERIFY OP_CHECKSIG
![Page 18: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/18.jpg)
Escrow with timeout
18
program Escrow(sender: PublicKey, recipient: PublicKey, agent: PublicKey, timeout: Time) {
path approve(sig1: Signature, sig2: Signature) { verify checkMultiSig(3, sender, recipient, agent, 2, sig1, sig2) } path cancel(sig: Signature) { verify checkLockTime(timeout) verify checkSig(recipient, sig) } }
![Page 19: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/19.jpg)
Escrow with timeout
19
program Escrow(sender: PublicKey, recipient: PublicKey, agent: PublicKey, timeout: Time) {
path approve(sig1: Signature, sig2: Signature) { verify checkMultiSig(3, sender, recipient, agent, 2, sig1, sig2) } path cancel(sig: Signature) { verify checkLockTime(timeout) verify checkSig(recipient, sig) } }
![Page 20: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/20.jpg)
Escrow with timeout
20
program Escrow(sender: PublicKey, recipient: PublicKey, agent: PublicKey, timeout: Time) {
path approve(sig1: Signature, sig2: Signature) { verify checkMultiSig(3, sender, recipient, agent, 2, sig1, sig2) } path cancel(sig: Signature) { verify checkLockTime(timeout) verify checkSig(recipient, sig) } }
![Page 21: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/21.jpg)
Covenants
21
<0x0100000001> OP_SWAP OP_SIZE 36 OP_NUMEQUALVERIFY OP_CAT <0x00> OP_CAT OP_SWAP OP_SIZE 32 OP_NUMEQUALVERIFY OP_CAT <0x00005f> OP_CAT 2 OP_PICK OP_SIZE 95 OP_NUMEQUALVERIFY OP_CAT <0xffffffff0100> OP_CAT OP_SWAP OP_SIZE 32 OP_NUMEQUALVERIFY OP_CAT <0x0000> OP_HASH256 OP_CAT <0x17a914> OP_CAT OP_SWAP OP_HASH160 OP_CAT <0x870000000001000000> OP_CAT OP_SHA256 1 OP_DUP OP_CAT OP_DUP OP_CAT OP_DUP OP_CAT OP_DUP OP_CAT OP_DUP OP_CAT OP_DUP OP_CAT OP_DUP 2 OP_ROLL 3 OP_PICK OP_CHECKSIGFROMSTACKVERIFY 1 OP_CAT OP_SWAP OP_CHECKSIG
![Page 22: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/22.jpg)
Covenants
22
program QuineCovenant(fixedSignature: Signature) { path spend(outPoint: String, valueIn: String, valueOut: String, script: String, recoveredPubKey: PublicKey) { verify size(outPoint) == 36 verify size(valueIn) == 32 verify size(script) == 95 verify size(valueOut) == 32 let tx = (0x0100000001 ++ outPoint ++ 0x00 ++ valueIn ++ 0x00005f ++ script ++ 0xffffffff0100 ++ valueOut ++ hash256(0x0000) ++ 0x17a914 ++ hash160(script) ++ 0x870000000001000000) let txSigData = hash256(tx) verify checkSigFromStack(recoveredPubKey, txSigData, fixedSignature) verify checkSig(recoveredPubKey, fixedSignature ++ 0x01) } }
![Page 23: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/23.jpg)
program Offer(seller: PublicKey, price: Number, currency: Asset) { path lift() { verify tx.hasOutput(price, currency, Account(sellerKey)) } path cancel(signature: Signature) { verify checkSig(sellerKey, signature) } }
Offer
23
![Page 24: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/24.jpg)
program Offer(seller: PublicKey, price: Number, currency: Asset) { path lift() { verify tx.hasOutput(price, currency, Account(sellerKey)) } path cancel(signature: Signature) { verify checkSig(sellerKey, signature) } }
Offer
24
![Page 25: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/25.jpg)
program Offer(seller: PublicKey, price: Number, currency: Asset) { path lift() { verify tx.hasOutput(price, currency, Account(sellerKey)) } path cancel(signature: Signature) { verify checkSig(sellerKey, signature) } }
Offer
25
![Page 26: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/26.jpg)
program Offer(seller: PublicKey, price: Number, currency: Asset) { path lift() { verify tx.hasOutput(price, currency, Account(sellerKey)) } path cancel(signature: Signature) { verify checkSig(sellerKey, signature) } }
Offer
26
![Page 27: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/27.jpg)
Potential applications and compilation targets
27
• Chain VM
• Bitcoin Script and extensions (Lightning Network, lotteries, merkelized scripts, Elements Alpha, covenants…)
• Crypto-Conditions
• zk-SNARKs
![Page 28: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/28.jpg)
Learn more
CHAIN CORE Get the open-source Chain Core Developer Edition at chain.com.
Protocol whitepaper, VM specification, and Ivy tutorial at
chain.com/docs.
Slack: slack.chain.comTwitter: @chain Support forums: support.chain.com
CHAIN PROTOCOL
GET IN TOUCH
28
![Page 29: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/29.jpg)
IC3 Open House: February 23 in SF
WHAT & WHERE Initiative for CryptoCurrencies and Contracts (IC3) Winter Retreat
Hosted by Chain
Employees of companies interested in joining IC3
Students interested in pursuing graduate studies at IC3
initc3.org/events.html
WHO CAN APPLY
AGENDA & REGISTRATION
29
![Page 30: Ivy: A Declarative Predicate Language for Smart Contracts · 2017-02-04 · Introduction: Two Blockchain Models 2 Mutable State (Accounts and Contracts) Immutable State (UTXOs) Examples](https://reader033.vdocuments.mx/reader033/viewer/2022041721/5e4ef471c4007465ad2bac42/html5/thumbnails/30.jpg)
30
@oleganzaOleg Andreev
@danrobinsonDan Robinson
@basculeTony Arcieri