ZhangCheng-zh / blog

记录一些code
0 stars 0 forks source link

Web3 Blockchain Fundamentals MOOC #6

Open ZhangCheng-zh opened 2 years ago

ZhangCheng-zh commented 2 years ago

Properties of Money

FirstVirtual

SET Architecture

Digital Credit to Digital Cash

Cash needs to be boostrapped, but

Chaumian Ecash

The Double-Spend Issue

How can we prevent a 'double-spend'.

Anonymity

Scarcity

Computational Backing

"Bitcoin is Hashcash extended with inflation control" - Adam Back

Ledgers

Use Linked Timestamps

Efficiency of Linked Timestamps

Bitcion-style Blockciain

Bitcoin

Resolving the double-spend issue

ZhangCheng-zh commented 2 years ago

Cryptocurrency

Encryption

Symmetric-Key Encryption

Kerckhoff's Principle

Breaking Caesar

Symmetric-Key Encryption Weakness

Asymmetric-Key Encryption

Public-Key Encryption Fundamentals

Secure Communication Without Secure Channels

Public/Private Key Generation

Efficiency - one problem

Step 1: Establish a secure communications channel using asymmetric encryption Step 2: Share a symmetric encryption key Step 3: Use symmetric encryption for further communication

Public Key Infrastructure(PKI)

One-Way Functions

One-Way Function Example

ZhangCheng-zh commented 2 years ago

Introduction to Hashing

What is a hash function?

Bad Hash

Hash vs Cryptographic Hash

Cryptographic Hashes

Should have the following properties:

Collisions Always Exist

Finding Collisions

Using A Hash As a Message Digest

Hiding

Hiding: A Formal Definition

Bad Hash - Is it hiding?

Application of Hiding: Commitment

Commitment Scheme

com := commit(m, nonce)
verify(com, msg, nonce)

A nonce is random value that can be used only once. Nonces will come up very often in our exploration of Bitcoin.

Assumes the commit function is hiding and binding

Binding = collision - resistant Given two pairs m / nonce and m' / nonce', it is infeasible to find m != m' and commit(m, nonce) == commit(m', nonce')

com = commit(msg, nonce) Publish com publicly

Commitment Scheme Example

Puzzle Friendliness

Puzzle Friendliness Application

Is Bad Hash PuzzleFriendly ?

Hash Functions Used in Bitcoin

many fields

SHA-256

ZhangCheng-zh commented 2 years ago

Hashing In-Depth

Hash Pointers and Related Data Structures

What is a hash pointer?

What good are hash pointers?

Data Structures with Hash Pointers

Linked List with Hash Pointers

image

Tamper-Resistant

Hash pointer of previous data includes both data in node and hash of preceding node.

image

Binary Tree with Hash Pointers

Merkle Tree:

image

Proving Membership with a Merkle Tree

image

Why Merkle Trees?

Where Are Merkle Trees Used in Bitcoin?

And thus incentivizes miners to add more transactions, so they get transaction fee

Where Can't We Use Hash Pointers ?

Cyclic structures = no starting point for hashes

ZhangCheng-zh commented 2 years ago

Blockchain related data structures and concepts

More Hashing, Digital Signatures and Centralized Legers

Merkle-Damgard Transforms

Covert each character into a corresponding value, sum them up modulo size of output Variety of problems with this scheme

compression algorithm accepts two arguments: current block (size m) and previous result (size n) outputs result of size n (where n < m)

Example:

image

Digital Signatures

Characteristics of Digital Signatures

Digital Signature Scheme = Three Algorithms

(sk, pk) = generateKeys(keysize)

Given a key size keysize, return a 'keypair' - a public key used for verification and a secret key for signing

sig = sig(sk, message)

Given a secret key sk and a message, return a signature for that message

isValid = verify(pk, message, sig)

Given a public key pk, a message, and a signature, return a Boolean value indicating whether or not the message was properly signed

Key = Identity

GoofyCoin

Generating Money With GoofyCoin

Transferring Coins with GoofyCoin

The GoofyCoin Ecosystem

Creating Coin

image

Transfer Coin

image

Continue Transfer Coin

image image

Problem

image image

To solve this problem, ScroogeCoin is Here

image

Problems with ScroogeCoin

Centralization

ZhangCheng-zh commented 2 years ago

Decentralization

ScroogeCoin Recap

Decentralizing ScroogeCoin

Centralization vs Decentralization: The Very Idea

Bitcoin: Designed to Be Decentralized

"Abstract. A purely peer-to-peer version of electronic cash would allow online payments to be sent directly from one party to another without going through a financial institution, but the main benefits are lost if a trusted third party is still required to prevent double-spending"

How To Decentralize?

How Decentralized is Bitcoin?

Distributed Consensus

Transactions in Bitcoin

Alice want to pay Bob one bitcoin:

image

Possible Consensus Mechanism

image

Every Node(circle) has: 1 The blockchain up to that point in time 2 A list of outstanding transactions which have been broadcast but are not yet in the blockchain (the transaction pool or mempool)

image

Different nodes may have different transaction lists Every 10 minutes, nodes use some consensus mechanism (e.g. voting) to determine which transactions are put in blockchain. If a transaction is missed, no problem, wait for the next block!

Problem with Naive Consensus Protocol

Distributed Systems And Time

Sybil Attacks

image

Simplified Bitcoin Consensus Algorithm

image

Denial Of Service

Double-Spend w/ Simplified Consensus

image

Protecting Against Double-Spend with Simplified Consensus

What about Sybil Attacks?

ZhangCheng-zh commented 2 years ago

proof of work and mining

Recall Our Naive Consensus Protocol

Incentives

Incentives in Bitcoin

Block Reward

The Future of the Block Reward

Transaction Fees

UTXOs

Transactions Behind the Scenes

image

Splitting TXs - Sending 0.4 btc

image

Multiple TX Input / Output

image image

Transaction Fees

Proof of Work

As Long You Can Provide a Valid Block

Recall Puzzle-Friendliness of SHA-256 Hash

Hash Puzzle Properties

Difficult to Compute

Paramaterizable Cost

Impact of Puzzle-Friendliness

Bernoulli Trials

Poisson Process

image

Poisson Process - Mining Bitcoin

Poisson Process - Dice

How Long for a Given Miner To Find a Block?

mean time to my next block = 10 m / fraction of hash power I control

Trivial to Verify

Mining Overview

// Should I mine Bitcoin?
mining_reward = block_reward + tx_fees
mining_cost = hardware_cost + operating_costs
if (mining_reward > mining_cost)
    return true
else
    return false

51% Attack

What Can our Pseudo-Scrooge Do?

A Pale Yet Dangerous Shadow

ZhangCheng-zh commented 2 years ago

Mining In-Depth

Being A Miner

Finding a Valid Block

Generate Block Using Merkle Root of Tx Tree

example:https://blockchair.com/bitcoin/block/741014

image

Try Nonces, 0 through pow(2, 32) - 1, Only pow(2,32) nonces?

Keep Modifying Candidate Block Until H(block) < target

Once it does, broadcast it ASAP to the network

How Do I Know the Target?

SHA-256 and Double-SHA-256

How Does SHA-256 work?

SHA-256

CPU Mining

GPU Mining

The Brief Reign of FPGAs

ASICs: From 2012 to Now

Energy Consumption

Can We Increase Efficiency?

Is Bitcoin Mining Wasteful?

Can we make it less wasteful?

You still want to mine Bitcoin?

Mining Pools

Mining Shares

Mining Payout

What's to Stop You From Withholding A Valid Block?

What About Block Modification?

Can Pools Be Too Powerful?

Mining Pools: Good or Bad?

Mining Decisions

Attacks By Miners

Forking Attack

image

Forking Attack via Bribery

Block-Withholding

image

Blacklisting / Punitive Forking

image

Feather Forking

So What ?

ZhangCheng-zh commented 2 years ago

Mechanics of Cryptocurrency

Bitcoin Consensus

Account-Based Ledger

Efficiency Issues

image

Transaction-Based Model(Bitcoin)

Refresher: UTXOs

image

Verification Only Back Until Coin Creation

image

Joint Payment - 10 Bitcoin To Carol From Bob & Alice

image

A Look at a Raw Block

Anatomy of a Block

Block Metadata

image

Compare to Genesis Block

image

Transactions

Transaction Metadata

Transaction Inputs

image

Transaction Outputs

image

Programmable Money

Script - The Bitcoin Scripting Language

Stack-Based Programming

image

Why Not Turing-Complete?

Common Script Commands

Script Execution Walkthrough

Basic Bitcoin Script

image

Proof of Burn

P2PKH vs P2SH

ZhangCheng-zh commented 2 years ago

Network Concepts

Join the Bitcoin Network

image

Dynamic Random Topology

image

The Gossip Protocol

image

Valid Transactions at the Gossip Level

Transaction Propagation

Storage (as of 16 February 2020)

Fully Validating Nodes

SPV("Lightweight") Nodes

Why Run a Lightweight Node?

The Evolution of Bitcoin

Escrow Transactions

Remember: Bitcoin transactions are immutable once they are completed and on the blockchain! third-party arbiter: Judy buyer: Alice legitimate businessman: Bob need to be signatured by 2 of 3

image

Kinds of Forks

Changes to Consensus

Limitations of Bitcoin Network

ZhangCheng-zh commented 2 years ago

Achieving Consensus

Consensus

Consensus Relationships

image

Consensus in Value

How to Determine Value

Consensus About History

BIP(Bitcoin Improvement Proposal)

Who's In Charge?

Blockchain Hard Fork

image

Hard Fork and New Currency

Monero Network Upgrade

image

Love it or leave it

Major Bitcoin Fork

Bitcoin Cash Split

Bitcoin Core

Bitcoin Cash

User-Activated Soft Fork(UASF)

Bitcoin Scalability Debate

Small Blocker Arguments

Big Blocker Arguments

Satoshi's Thoughts

What is Bitcoin?

Ethereum / Ethereum Classic

Categories of Consensus

Practical Byzantine Fault Tolerance(pBFT)

Variations

ZhangCheng-zh commented 2 years ago

Using Cryptocurrency

Owning Bitcoin

Public Keys != Addresses

Base58Check Encoding

A Number Base58Check is still a Number

image

Bitcoin Address Generation From a Public Key

What If I want a specific address?

Vanity Address Generation

-Just like mining, we can just keep trying with different inputs For each character we want, there is a 1 / 58 chance we will get the correct one, ergo chances are 1 / pow(58,k) for a k-character pattern -Luckily, programs can do this for us... do this locally! keys have been stolen from "online vanity generations!" Also note that there are some possible efficiency optimizations sheer random guesses, see book for details

image

How do I store my cryptocurrency?

Hot vs Cold Storage

Wallet Software (Hot)

-Sofeware on your computer or phone which keeps track of your keys, coins, makes transactions, etc.

Paper Wallet(Cold)

Brain Wallet (Cold)

Hardware Wallet (hybrid hot / cold)

Why Address Re-Use is a bad idea

Hierarchical Deterministic Wallets

Splitting and Sharing Keys

Naive Splitting

Secret Sharing - K of N

Secret Sharing

What if we want K != N ?

2-of-n Secret Sharing

image

2-of-n Secret Sharing, One share

image

3-of-n Secret Sharing

you need have 3 or more points on the parabola

image

Arbitrary k-of-n Secret Sharing

Threshold Signatures

-Problem with previous schemes - they all generate the actual secret key, which is a point of weakness (key can be stolen after reconstruction and used on its own)

Online wallet

Online Exchange

Risk 1: Bank / Exchange Runs

Risk 2: counterparty Risk

Risk 3: Security Breach

ZhangCheng-zh commented 2 years ago

Cryptocurrency and Anonymity

What Do We Mean By "Anonymity"?

What about anonymity ?

Why do we even want anonymity?

The ethics of anonymity

On the other hand

Squaring the circle

Crypto-anarchy

Is pseudonymity enough?

So

Ideal world: unlinkability

Deanonymization via side-channels

Anonymity set

Taint Analysis

Linking - Change Address

image

0.08 BTC or 0.02 BTC Payment?

image

Avoid Address Re-Use

Idioms of Use

Real-world IDs: TXs/Addresses

Transaction Graph analysis

image

Network-level deanonymization

Nuts and bolts of network-level deanonymization

image

Avoiding network-level deanonymization

Offline / off-chain transfers

Mixers

Multi-mix

image

Should You Trust a Mixer?

CoinJoin

Problems with coinjoin

Privacy-focused altcoins

Other Privacy Enhancements