比特币与区块链

I learned the basic ideas of bitcoin and blockchain in the computer security class last week, so I just write a summary here.

##1.Ledger
Firstly, let’s talk about the ledger. Why use a ledger? If you exchange money with some people very frequently, using a ledger is better than cash. So you guys can create a ledger and record all the transactions. For example, Alice, Bob, Charlie and you. You can see some records in the ledger as below:

Alice pays Bob $20

Bob pays Charlie $40

Charle pays you $30

You pay Alice $10

This ledger is public, everybody could add new records in it. In the end of every moth, people will look at the list of records and settle up.
As above description, the protocol is pretty simple:

  • Anyone could add records to the Ledger;
  • Settle up with US dollars every moth;

However, there is a problem of the protocol. Anyone can add a new transaction if he/she want. For example, Alice can add a record that Bob pays Alice $200 without telling Bob. How to solve this problem?

##2.Digital Signature
In the real world, we can sign our name at the end of every transaction. The signature could prove we already have seen it and approve it. In the computer world, we should use digital signature.

The digital signature leverages hash function. There is an example as below:

Transaction Signature
Alice pays Bob $20 Signature1=hash(“Alice pays Bob $20”)
Bob pays Charlie $40 Signature2=hash(“Bob pays Charlie $40”)
Bob pays Charlie $30 Signature3=hash(“Bob pays Charlie $30”)

There is a crucial problem. As the handwritten signature, a digital signature could be forged. Actually, the digital signature is a string of bits, people could copy it. So it cannot prevent forgeries. How to prove the signature’s owner?

##3.Public-key Crypto
What is Public-key cryptography? You can click the hyperlink to see the details. To solve the problem as above, the basic idea is:

  1. Get the hash value by hash fuction (SHA-256);HashValue=SHA-256(OriginalRecord)
  2. Alice use her own private key to encrypt the hash value generated before;Signature=Sign(HashValue,PrivateKey)
  3. Anyone could use Alice’s public key to decrypt the signature and compare the result with original record. This process is called signature verify, the result is true or false;Result(T/F)=Verify(OriginalRecord,Signature,PublicKey)

If the result is true, it means:

  1. This signature belongs to this record;
  2. This signature belongs to Alice;
  3. This record is not modified by others;

However, there is another problem: even though other people cannot forge Alice’s signature, but they can copy a whole record to a new line. Luckily, there is a solution: add an unique sequence number to every record. Until now, the protocol looks like below.

  • Anyone could add records to the Ledger;
  • Settle up with US dollars every moth;
  • Only signed transactions are valid;

It seems perfect, but there must be some people break the rules. So we must rely on some honor system. In the paper “Bitcoin: A Peer-to-Peer Electronic Cash System”, the author proposed “peer-to-peer”. It means that it shouldn’t be a central system in the protocol. If Bob owes a lot of money and runs away, how to handle this problem? The protocol should be changed as below:

  • Anyone could add records to the Ledger;
  • No overspending;
  • Only signed transactions are valid;

In theory, if all people in the world was using this ledger, people could live their whole life just sending and receiving money on this ledger without real US dollars. So the ledger should be placed in a public place, such as a website where anyone can add new records. But who host the website?

As I described above, we cannot use a central system, but we can have everybody hold their own copy of ledger. In the network, if someone makes a transaction what he/she does it broadcast that out into the whole network. According to this approach, how could you get everyone to agree on what the right ledger is? how to make everyone received and believes that transaction? how to add the transactions in the ledger if you received them together?

##4.Bitcoin&Blockchain
For a hash function like SHA-256, whatever you put into it, the result is a 256 bits string. What the miners do is that they find a number add into the ledger, and then use SHA-256(ledger+number) to get a result. The result first 30 bits are “0”! But find the number is very very hard. The only way to find the special number is guessing and checking. So the miner has to go through about a billion different numbers before finding the special one. But the verify process is much easier. The process that people find the number is called “proof of work”.

Let’s go back the distributed ledger situations. In the whole network, there are many copies of ledger, every copy is a block. And in each block, it contains a list of transactions(records) with a proof of work(special number). A block is only considered valid if it has a proof of work. But how the next transactions know the previous transactions that use to make sure someone has enough money to pay? We make a block has to contain the hash of the previous block at its header, it means every transaction should base on the previous transactions. It looks like a blockchain.

In the blockchain, anyone could be a block creater. The creator is going to listen for transactions being broadcast, collect into some block, and then computes the special number that makes the hash of that block start with N zeros. Once he/she find it, just broadcast out the block he/she found. To reward a bloack creator for all this work, when the creator puts together a block, we allow he/she to include a very special transaction at the top of it, it is called block reward. It means the total number of bitcoin in our economy increases with each block. The process of creating blocks is called “mining”, the creator called “miner”. what the miner did is listening for transactions, creating blocks, broadcasting those blocks, and getting rewarded with new money for doing so. The miner’s computer more powerful, the high possibility they have.

However, if someone receive two blocks at the same, what should he does? The solution is that he just makes a branch and keep both of them, and waits for the next block. It is hardly to receive two blocks at the same time. If receive, just wait for the third block. After that, he/she just selects the longer branch and discard the other one.

##5.Techniques

  1. Leverage SHA-256 hash function and public-key crypto to generate digital signature
  2. Save the transactions by blocks of blockchain
  3. Protect the bitcoin network and control the average block time by setting extra works
  4. Incent miner to increase the network
  5. Transactions of bitcoin don’t rely on bank or other honor system

##6.Tips

  1. The Average Block Time is about 10 minuetes
  2. The total number of bitcoin is less than 21 million
  3. GPU mining is faster than CPU mining.
  4. Block Reward: 12.5 BTC (2017)
  5. Transaction Fee: average 2 BTC (2017)
  6. Average income of one block: 14.5 BTC = 260 thousand USD = 1.7 million USD
  7. To maintain the average block time, the bitcoin network upgrade the degree of difficulty every two weeks
  8. Bitcoin is saved in your bitcoin client

##7.Reference

  1. Ever wonder how Bitcoin (and other cryptocurrencies) actually work?
  2. 比特币的原理