Blockchain Explained
The goal of blockchain
The goal of blockchain technology is the implementation of a Ledger, that means, an accounting repository where payment transactions between parties are performed and permanently stored, in a way that it doesn’t require a central coordinator. That means, the transactions can be agreed between the seller and the buyer without the intervention of banks or other financial institutions.
Nowadays the transaction process is done thanks to the banks, which guarantee the reliability of this process. International payments require several steps to complete a transaction. Let’s see an example.
I am in Spain and I want to make a money transfer to the US. The process would be as follows:
- I held an account in a spanish bank. I have money in this account, the spanish bank can guarantee that I have this money.
- A friend of mine lives in US and has an account with a US bank.
- I order a funds transfer from my Spanish bank account to the US bank account of my friend.
- The US bank and the Spanish bank are connected in some way. Usually, the spanish bank has a counterparty US bank, which has an agreement with the spanish bank to perform payments from Spain to the US. The money is transferred from the spanish bank to the counterparty US bank. I have sent an amount in EUR that needs to be converted in US dollar. There should be also an agreement on which FX rate should be applied.
- Finally the counterparty US bank transfers the US dollars to the bank of my US friend, and the bank of my US friend puts the money in his account.
This process is slow and expensive. Each bank has to process the request and has to be paid for that. It could take days until my US friend has its funds available.
With the internet revolution came other internet payment services, like Paypal. The processing time has progressed dramatically, international payments can be done in minutes. The management process remains centralized, in this case PayPal and the bank or financial institution serving the credit card or the account to be used by PayPal.
Blockchain allows direct payments in seconds and without intermediaries. To do this it is necessary to solve a fundamental problem: find out a mechanism that does what financial institutions do. Ensure that the person sending money really has this money and it belongs to him. And ensure that this money is transferred only once, that means, once a transaction is done the money doesn’t belong anymore to the sender (what is called the double spending problem).
It is also necessary to keep all the history of payments in the Ledger, that should be publicly accessed, that means, a full audit trail of all transactions.
Crytography
Cryptography is a mathematical field that provides very powerful tools to implement the requirements for Blockchain. It is used to guarantee the ownership of the digital currencies (digital signature) and to set up a mechanism that guarantees the consistency of the data stored in the blockchain and avoids double spending (consensus mechanisms).
To implement a digital signature, it is necessary first to create a pair Public Key (PK)/Secret or private Key (SK). These keys are usually a 32 bytes block, identifying a user of the blockchain services. The public key is shared with all other uses. The secret key must kept and not shown to anyone. Anyone knowing my secret key could spend my money.
Cryptography provides very useful functions:
- I can sign a message and create a signature with my secret key: sign (sk,message)=digital_signature. This message could be a transfer order.
- Anyone can verify that my digital signature is correct, by using my public key: verify(pk,digital_signature,message)=ok/ko. Every user can check that I am the owner of the money that the message (transfer order) contains, because only myself with my secret key can create a signed message that is validated with my public key.
- Hash function. A hash function converts a message with a huge amount of data into a block of a few bytes data, a hash. The hash function has the “collision free” property. That means, it is almost impossible that two different sets of data produce the same hash. The hash function is used to validate the blocks that are incorporated into the blockchain.
Bitcoin. The first blockchain
The original paper describing bitcoin published by Satoshi Nakamoto can be found at https://bitcoin.org/bitcoin.pdf.
Bitcoin uses digital signatures as explained in order to handle the ownership of the bitcoin.
In order to guarantee the reliability of the transactions and prevent the double spending problem, bitcoin uses the proof-of-work mechanism.
Each block contains a set of transactions, the hash of the previous block and a nonce.
Block(n) = [Hash(block(n-1)), TxSet, Nonce]
Certains nodes of the blockchain called miners create new blocks. To do this, they collect a set of transactions that were broadcasted into the network and have to use CPU power in order to find out a nonce. This nonce should lead to a SHA-256 hash of the block, having a number of leading zeroes defined by the bitcoin network. This nonce can’t be calculated, the miners use computers that generate aleatory numbers, and try that the resulting hash has the required number of zeroes.
Once a miner finds the nonce, the new block is broadcasted to the network and the other nodes validate it and incorporate it to the blockchain. One transaction of the block is a coinbase transaction that contains a brand new amount of bitcoins which is assigned to the miner, this is the reward for the mining work. At the time of writing this article, october 2022, it is 6,25 bitcoins. The bitcoins are created through those coinbase transactions, the first one was done by Satoshi Nakamoto in 2009.
The bitcoins are in fact stored as an output transaction, called unused output transaction of UTXO. A bitcoin transaction consists of a set of input and output transactions. All UTXOs are kept and only a UTXO can be used as input of a new transaction. And a given UTXO can be used only once, preventing double spending. If a malicious node sends two transactions with the same UTXO, only the first one (transactions have a timestamp) can be considered to be added to a block, the second one will be discarded.
The miners can also receive a fee for the transactions. The value of the output transactions of an order could be less than the input, and then this difference is given to the miner. That can be used as an incentive to process the transactions quickly, thus the miner will prefer to get transactions with reward to be incorporated into a block.
In fact, this transaction fee will be the only reward for miners in the future. The bitcoin supply has a defined maximum of 21 millions.Once this benchmark is reached, miners can only get a reward through the transaction fees.