#4dev - Smart Contract Security
#4dev - Smart Contract Security
Smart Contract
Security
Richard Liu and Max Fang
Blockchain at Berkeley
Therefore, distributed currencies and platforms are the most adversarial environment
● Unfortunately, designing simple contracts is very non-trivial
● Even a Rock-Paper-Scissors contract is highly non-trivial
Platform Recap
Mistakes to Avoid
1 - Unexpected Behavior
● Examine player_input
○ msg.value != 1000
○ Player 3 joins game
● Both lose money!
○ Money from inputs should be
refunded
2 - Dishonest Individuals
● Player choices sent in plaintext
● Cheating player can peek
○ Always pick winning option
○ Game theory:
■ Anonymous system
● no reputation
■ Goal: maximize financial gain
Solution: Cryptography
● Hash functions:
○ binding - “second-preimage resistant”
○ hiding - “preimage resistant”
Hashing Review
* k
We define a cryptographic hash function: H : {0,1} ⟼ {0,1}
The function is deterministic; the same input always yields the same output.
0xd9014c4624844aa5bac314773d6b689ad467fa4e1d1a50a1b8a99d5a95f72ff5
Preimage Resistance:
*
Let x = {0,1} = message
y = H(x)
-1
x = H (y) → should be computationally difficult
Given message x
Collision Resistance:
The probability that their hashes are equal, P(H(x1) = H(x2)), should be “very
small”
1/2
The upper bound to find a collision is at most O(N ) (called a Birthday Attack)
+ Merkle Trees
+ Proof-of-Work (Bitcoin, Litecoin, Dogecoin, etc…)
+ Transactions, Blocks, Addresses all referenced by hash value
Also
Solution: Cryptography
● Hash functions:
○ binding - “second-preimage resistant”
○ hiding - “preimage resistant”
3 - Misaligned Incentives
● Commitments solve “peeking”
● Second player sees loss
○ Should be indifferent
○ Negative incentive to open commitment
■ Costs gas
○ Smart contract never pays out
■ Loser saves gas cost
Realigning Incentives
● Construct contract carefully
○ Users are ALWAYS incentivized to cooperate
● Rock-Paper-Scissors
○ Solution: set deadline for reveal after 1st player
■ If 2nd player doesn’t reveal, 1st player wins
■ Include security deposit
● Forfeited if player chooses not to reveal
Call-Stack Depth
● Ethereum recursion stack: 1024 calls high
● What happens at depth 1025?
○ Nothing.
○ Literally nothing.
● Consider code to the right
○ Only goes 2 calls deep
○ Contract calling it may go deeper
● Payouts messed up:
○ Users miss out on send entirely
○ Sending money/gas to (buggy) contracts
■ Default behavior: send all gas
blockchain.berkeley.edu 16 | October 2016
Blockchain at Berkeley
Example: Etherpot
● Ethereum-based lottery
○ Random value: hashes of (future) blocks
Blockhash Bug
● Problem: winner had to cash out fast
● block.prevhash supports 256 most recent
○ Within 256-block limit
blocks
○ Around 85 minutes
○ Intended to help efficiency
○ Proposed fix: “blockhash service”
■ Contract to retrieve block hashes
beyond 256
Example: Etherpot
● Ethereum-based lottery
○ Random value: hashes of (future) blocks
○ Oh no! Does not win ○ Lottery prize lower than block reward
In Summary
Presentation based on
"Step by Step Towards Creating a Safe
Smart Contract: Lessons and Insights from
a Cryptocurrency Lab"
Like us on Facebook:
@Berkeleyblockchain
https://www.facebook.com/berkeleyblockchain/