# How Cryptocurrencies Work (Technical Guide)

## #9 — Blockchain, Cryptography and Concensuses explained.

Cryptocurrencies are quite complex. If you wonder what happens when someone does a transaction, what is a blockchain, what is the purpose of mining or the limitation of cryptocurrencies, this article is for you!. It will explain with some technical details the different components of cryptocurrencies 😃.

This article is part of my Learning challenge where I learn about one topic each month. As you can imagine, this month, I was learning about blockchain & cryptocurrencies. I based this studies on a Coursera MOOC, a few articles, some whitepapers and the building of my own Proof of Stake Cryptocurrency and Coin Generator. Click here, if you want to know more about my methodology.

If you don’t know what is a cryptocurrency start with the following post:

Cryptocurrencies are made from for components:

• The data structure (most of the time a blockchain)
• Cryptography
• Rewards for running a nodes
• Decentralized (Peer-to-peer) consensus

### Cryptographic Hash function

Before starting with cryptocurrencies, let’s see the definition of one of the core element of Cryptocurrencies: the cryptographic hash function. They are used in many ways in cryptocurrencies, such as identifier but also to verify that data are not modified.

It is a mathematical algorithm that maps data of arbitrary size to a bit string of a fixed size (a hash) and is designed to be a one-way function, that is, a function which is infeasible to invert. — Source: https://en.wikipedia.org/wiki/Cryptographic_hash_function

There are many cryptographic hash functions, such as SHA-256 or Scrypt, X11 (using 11 different hashing functions).

You can test SHA-256 here: https://jsfiddle.net/sandoche/e0jmqLok/

### Data structure

The data structure is the skeleton of every cryptocurrency. Let’s talk about the most famous one: the blockchain.

#### Blocks

Most of the cryptocurrencies are using the blockchain data structure. As its name sounds, it is a chain of blocks containing data. Easy right? 😬😬

Find below an example of a simple block’s structure:

• previousHash → the hash of the previous block
• timetamp → the date time code
• data → in the case of cryptocurrencies it will contain the transactions
• nonce → an integer (it’s used for mining we will explain it later)
• hash → the hash of the current block calculated like this:
`const hash = H(previousHash + timestamp + data.toString() + nonce)// with H(x) the hashing function// and + to concatenate`

Note that some cryptocurrencies uses some preffix to their hashes, or addresses to differentiate between each others.

#### Blockchain

Now that you have seen the block structure it gets easier. The blockchain would look like this:

The power of this data structure is that, if you change the data of one block, you will have to re-calculate the hash of the block which will invalidate the value previousHash of the next block. You can see it in the figure below.

Note that there is one very special block, the first block, it doesn’t have any previousHash, it’s called the GenesisBlock.

The blockchain data structure is used by most of the coins such as Bitcoin, Litecoin or Ethereum. We will see in the consensus part, other cryptocurrencies that use different data structures along with others consensus.

Thanks to this structure coupled with a good decentralized consensus, it’s almost impossible to change data from the blockchain. Any data stored in the blockchain will stay there forever 😮. It can’t hardly be removed.

Moreover, any kind of data can be stored using this data structure. If like me you are wondering what technology is used to save this data structure, anything can be used. Since blocks are identified by their hash, the best solution is a key-value storage. Bitcoin uses LevelDB, a key-value storage library.

By adding transactions to the blocks of a blockchain you can turn it into a ledger.

### Transactions and cryptography

Now we know what is a blockchain, let’s talk about the transactions and how they are processed.

#### Transactions

Here is the structure of a transaction before it’s made:

• transactionInputs[] → Array of transactionInput
• transactionOutputs[] → Array of transactionOutput
• id → Hash calculated from the content of transactionInputs[] and transactionOutputs[]

Now the structure of transactionOutput and transactionInput.

transactionOutput:

• amount — Amount of the cryptocurrency to be sent to the address

transactionInput:

• transactionOutputId → id / hash of a previous transaction where the output is taken and use as an input
• transactionOutputIndex → index of the output to find the right output from the transactionOutputs[] array

The rule is the following in every transaction the sum of inputs should be equal to the sum of outputs! So what should be done is: if, let’s say Alice, has received 30 and she wants to send 10, she has to send also 20 to herself! This is how bitcoin works in order to verify transactions faster. It avoids checking the full history every time a transaction is issued.

Let’s see what happen in an example:

1. Let’s say there are 3 people, Alice with the address 0xA, John with the address 0xJ, Bob with the address 0xB, these addresses are theirs public keys
2. Alice has 30 COINS (she previously received it from John in the transaction with the id 0x5645464) and Bob has 0 COINS
3. Alice sends 10 COINS to Bob
4. Alice will create a transaction like the following
`// a first transaction output to bobconst txOut = {  amount: 10,  address: 0xB //bob's address}`
`// a second transaction output to alice herselfconst txOut1 = {  amount: 20,  address: 0xA //Alice's address}`
`// a transaction input// this transactionOutputId is the id of the transaction 0x5645464 where John sent 30 COINS to Alice previouslyconst txIn = {  transactionOutputId: 0x5645464,   transactionOutputIndex: 0}`
`// We can now create a transactionconst tx = {  transactionOutputs: [txOut1, txOut2]  transactionInputs: [txIn],}`
`// Then we need to calculate a hash of the transactions// If you want to know more about this you can check this good tutorial that explains the full process: https://lhartikk.github.io/`

Actually, you never have to do this yourself, your wallet software does it for you, luckily!

Once the transaction is created it has to be signed before being broadcast to the nodes to be part of a block.

#### Cryptography

This is where cryptography comes in.

Bitcoin as many other cryptocurrencies is using the Elliptic Curve Digital Signature Algorithm (ECDSA) to secure the transactions but also to create public keys and private keys.

Every user of bitcoin needs two keys: the private key (secret key) and the public key that is used as an address also to receive money.

The private key is generated randomly and the public key is gotten with the ECDSA algorithm. If you want to know how precisely the public key is made read this: https://bitcoin.stackexchange.com/questions/25024/how-do-you-get-a-bitcoin-public-key-from-a-private-key#29880 (it’s very technical).

Also, note that this algorithm works only one way, you can get a public key from the private key but not a private key from a public key.

These two keys are used to sign the transaction (that we previously created). A signature can be verified with another function that takes the transaction data and the public key which was used to sign, and will return if it is valid or not. If you want the technical explanation you can find it here: https://en.wikipedia.org/wiki/Elliptic_Curve_Digital_Signature_Algorithm#Signature_generation_algorithm

Once the transaction is signed it can be broadcasted to the network.

When a node receives transactions, it first checks if it is valid or not: no double spend, valid account balance, transaction amounts input equals transaction amount output and valid signature. Then it adds it to his transaction pool that it’s also shared with other nodes. Then it adds them to blocks.

### Accounts balance

In order to calculate the balance of each accounts bitcoin and some other cryptocurrencies are using the UTxO: Unspent Transaction Output.

To simplify, bitcoin stores outside of the blockchain the transactions that are received by each address and not spent. By doing the sum of the unspent transaction of an account you can know the balance of an account. This value is used to ensure that someone is not spending more that what he can.

This UTxO is stored in a database that is managed and updated locally by every node in order to process the transactions and validate them faster without checking the full blockchain.

### Proof of Work, Mining, and Puzzles

Since cryptocurrencies are decentralized in a peer to peer network, nodes, which are computers who are running the cryptocurrency software, have to decide which transactions should be included in the next block and agree on that.

There are different consensus to define the rules of agreement. The very first one used in bitcoin is Proof of Work and it is also the most used.

Before explaining how Proof of Work works, let’s give an example of what would happen if there was no proof of work.

Let’s imagine a very small cryptocurrency called SmallCoin, with 3 nodes (X, Y, Z), each block can take up to 3 transactions maximum, and then add one block to the blockchain every hour. Alice broadcasts 3 transactions, and Bob broadcasts 3 other transactions and all of them are valid. The node X, decides to put the 3 transactions of Alice, the node Y decide to put 3 transactions of Bob, and Z, 2 transaction of Alice and one of Bob.

Who is right? 🤔 how can all the node agree on which block will be the next one on the blockchain? One solution could be to choose every time one node randomly like a lottery. The problem is the following: if one of the nodes, let’s say X, opens 97 other nodes (by running 97 instances of the software for example), he will have the power, and will be able to blacklist some users and for example never accept the transactions of Bob! This is called a Sybil attack.

In reallity, bitcoin blockchain adds one block to the blockchain every 10 minutes, and there is not any limit of 3 transactions per block, but a size limit of 1 megabyte per block, and today (13th April of 2018), 10050 bitcoin nodes are running (see the last number here: https://bitnodes.earn.com/). Bitcoin fight against Sybil attack using the Proof of Work consensus.

#### Proof of Work consensus

Instead of choosing randomly the block of a node, nodes have to resolve a computational puzzle. The first who resolves it will be the chosen one, its block will be added to the blockchain.

Here is how the puzzle works. Remember the block structure? it contained a timestamp, data, a nonce and a hash. Mining consists of incrementing the nonce and calculate the hash until the it gives a value that starts with a specific number of 0 called difficulty.

`const hash = H(previousHash + timestamp + data.toString() + nonce)// Here is the hashing function, in bitcoin the hashing function is SHA-256, a hashing function created by the NSA!`

I make this little JS fiddle for you to understand of the mining works: https://jsfiddle.net/sandoche/784kdgnp/

You can change the data passed to the hash function, but also the mining difficulty.

So once a node finds the nonce that gives the right number of 0, it has to broadcast its block to the network as it will be chosen as the next block of the blockchain. Also, the one who finds the block gets a reward, to motivate people to make the network more secure by running miners.

Also since the blocks contain the hash, the transaction, the nonce and the timestamp, other nodes can easily verify that the block is valid and the proof of work has been done. There is no way to cheat than trying all the nonce.

If two blocks are sent as the same time, there will be two branches, it’s called a fork, but the rule is to add blocks to the longest branch, so at some point one branch will die.

Basically, instead of having a random lottery, the more computational power a node has the more chance it has to validate the next block. Which makes harder to control the blockchain, as you need 51% of the computational power of the world to be able to block some transaction and blacklist them. That’s the purpose of proof of work. To make double spending or rewriting the history hard to happen. Because in order to have computational power you need to invest in mining hardware.

Bitcoin was initially designed to be mined with CPU power, so a lot of computers all around the world were mining it. But since the value of bitcoin increased people started to invest more in mining. They started to mine with GPU, then FPGA (Field-Programmable Gate Array, which are some boards very good with SHA 256 calculation), and now Bitcoin miners are using ASIC (Application-specific integrated circuit) which are devices built for mining Bitcoin 😱 (and some others) but their only purpose is mining. Mining is costly in hardware (buying it), and it also consumes quite a lot of electricity for running and for cooling. We will talk later about the waste of energy. But that’s the deal. Proof of Work is a way to convert energy and work into coins and security.

Since it can take years even with the latest ASIC to find your first block, miners usually work together in pools. Pools will make the miners try to find the nonce together. They give rewards based on the number of shares found, a share is a hash which starts with a number of 0 which is a bit inferior to the difficulty level.

Here an example for a difficulty of 6:

`// Invalid hash18ea285983df355f3024e412fb46ad6cbd98a7ffe6872e26612e35f38aa39c41`
`// Invalid hash0146415487f165835c00d441602290f7aa494f91b93fd9ec2c5d280f17f999c0`
`// Invalid hash considers as a share because it starts with 5 zeros00000eeff9839efeb3e528ade5789d90b8721c34c49718eeb9626f5a29b1350b`
`// Valid hash000000b8e1ae46744ccd94bdae212ed43f4a586df68a6348e39fa7fd13e9db5c`

One important detail is no matter how many devices are mining, the time between two blocks will stay around 10 min for bitcoin, by adjusting the difficulty ever 2 weeks. You can see here an example of how to write the code that adjusts the difficulty: https://lhartikk.github.io/jekyll/update/2017/07/13/chapter2.html

You can see in the following graph, the hashrate and the difficulty of bitcoin.

One last thing, it’s always possible to run a bitcoin node without mining, the only purpose is when you make a transaction having a node you don’t rely on any intermediary that can save your IP for example. But nodes that are not mining don’t get rewards 😔.

### Rewards & transaction fees

As explained above, the reward is an incentive to make users become miners, in order to secure the network. Without miners, the network cannot be secured, and without rewards 💵, no one will want to waste its energy, therefore their money.

Every currency has it’s own rewards rules that you can find in the source code. For example, Dogecoin (the meme coin) was giving random rewards in its first months of existence.

In bitcoin every node that validates a block receive currently 12.5 BTC, this amount is halved every 4 years. It’s a way for bitcoin creators to pre-define an inflation rate! No one can change it. You can see here the date of the next halving: http://www.bitcoinblockhalf.com/

In order to get this reward, the miner adds a transaction called Coinbase transaction to the block before doing the mining puzzle. This Coinbase transaction doesn’t contain any input and contains one output with the reward’s amount and the miner’s address as a recipient. So once the block is added in the blockchain the miner will keep this reward.

Also when you do a transaction you have to set a transfer fee. This fee is collected by miners. If there are too many transactions happening, the miners will take in priority the transactions with more fees. So you will have to pay higher fees to see your transaction happening fast.

Some cryptocurrencies are not giving any rewards, they only give transaction fees to the node, and some others don’t even give transaction fees. In those casea, all the coins are created in the genesis block and then distributed or sold to the users.

### Confirmations & branches

One last and important thing to understand is what is the number of confirmations of a transaction. Confirmations are the number of blocks in the blockchain that are after the block containing the transaction.

As explained before, it happens sometimes that the blockchain has branches, and the miners tend to add blocks to the longest chain (the one with more mining power used to build it actually). If you make a transaction that is added to the blockchain and another branch becomes more important, the smaller branch will become orphan and your transaction will not be part of the ledger.

In bitcoin, if a transaction gets more than 6 confirmations it’s considered as secured, and it’s unlikely that it will get orphaned. So if you sell goods or services with bitcoin be sure to get the 6 confirmation before giving their due.

### How the whole process works in 3 minutes

Now that the most important concepts are covered I recommend you to watch this video that will give a good overview and summary of the full process.

Also if you want to check how blocks look like here is the content of one from bitcoin: https://blockchain.info/fr/block/0000000000000000004f6b895db5e2dd336ccdecb42f3c829109096938882c8e

### Distributed consensus & alternatives to mining

Until now we were mostly focused on Bitcoin, the blockchain data structure and the Proof of Work consensus; but since bitcoin was created, many alternatives has been appearing 😃. The goal of the consensus is to find a way for the nodes to agree on the transactions to consider as valid and also to avoid attacks, such as double spending attacks, DDoS attack, or blacklisting. I put them in the following article.

### Challenges faced by cryptocurrencies

Here are the most important challenges that the cryptocurrencies are facing.

#### Scalability

The first and most important one is the scalability. It’s linked to all the following ones. The cryptocurrency should be stable no matter what is the number of users. Also note that every nodes needs to have the full copy of the blockchain which can become very big, the size of the bitcoin blockchain is currently around 160 GB!

#### Speed

Should be able to compete with Visa that can handle 54000 transactions/s in peak time and in average 2000 transactions/s.

Bitcoin, for example, can handle 3 transactions/s, Ethereum 20 transactions/s and Nano (Raiblocks) around 7000 transactions/s

#### Cost

The transactions fees should be as low as possible, that’s the only way that it can compete with other centralized money transfer or payment solutions. For example, Paypal charges 2.9% of the price + \$0.30

Bitcoin in high transactions fees, in a day it can reach \$50 of fees, but it is very volatile, check this chart: https://bitinfocharts.com/comparison/bitcoin-transactionfees.html

Some other cryptocurrencies such as Nano (Raiblocks) or Iota have 0 fees!

#### Centralisation

The centralization can be an issue and can make the network unsafe and unreliable. Also, some consensus tend to push nodes to be more centralized. For example with Proof of Work, miners tend to work together and make pools. They then share the rewards with each other.

#### Energy waste

Proof of Work consensus consumes a lot of electricity. The current electricity consumption to mine bitcoin is 60TWh/year. It equals to the annual production of 10 Nuclear plants.

You can think it’s a lot to run a cryptocurrency. But you cannot judge it by itself alone, you should compare it with the electric consumption of the global banking system in the world, that includes: people working, full offices using electricity, IT systems running all the time, ATM, trucks filling up the ATMs, …

See more numbers here: https://digiconomist.net/bitcoin-energy-consumption

Also, as we saw previously, there are some consensuses that are a lot more efficient than Proof of Work, as we saw above, it can even work with the sun power🌞!