How cryptocurrencies actually work

· 10min · Dmitry Scherbakov
cover

Introduction

Today, we would be discussing what are cryptocurrencies, how they work and what are advantages and disadvantages of using them over standart banking system. Well known and everybody's loved Bitcoin would be used as a main example here.

Basic knowledge

Cryptocurrency is essentially a type of digital currency, which does not exists physically, so there is no way to actually touch or "feel" it. The most important advantage of cryptocurrencies over bank digital currencies (like USD, RUB) is that it is decentralized, meaning that it is NOT controlled by a single entity. Banks can do all kinds of stuff they want with your account such as cancelling your money transfers, blocking and even deleting your accouts etc. Nothing of mentioned happens with cryptocurrencies. And, of course, all your money information, such as when, where and how much you actually spent is stored and can be used against you. Cryptocurrencies can also help you to get away with this, but it is not about all cryptocurrencies. We will discuss this problem later.

The first cryptocurrency

As everybody knows, Bitcoin (labeld BTC on the stock market) was the first digital currency. But it is not fully correct. Bitcoin was the first currency that truly went viral, but there were a few attempts to distribute digital currencies in late 90s, which did not end well, first of all, due to lack of computing power and secondly, due to lack of peopled to actually make use of it.

Key components: intro

To understand how cryptocurrencies work, we need to understand some fundamental math and cryptography concepts such as hashing and encryption. I will try to cover each one in depth.

Key components: encryption

The first important concept to understand is encryption. We all have some "basic" level understanding of what encryption is. You have the data, you have the encryption key and a function, which can either encrypt or decrypt the data using the encryption key. This is known as a symmetric encryption. Why symmetric ? Because you can both encrypt and decrypt content when you know the encryption key. There is also another encryption type, called asymmetric encryption, which works quite differently. First and the most important aspect to understand is that you no longer have single key to both encrypt and decrypt data. There are two key types, which are called public and private. You can use public key to encrypt your data and private key to decrypt it. There is no other way to decrypt encrypted data, other than using private key. Asymmetric encryption can also be used to digitally sign the data. You can sign the payload with the private key and verify the signature using the public key. And, of course, there is no way to verify the signature other than using appropriate public key, which was generated alongside with the private key, which was used for generating a signature.

Asymmetric encryption and digital signature technologies are so good and well developed, so they are used pretty much everywhere: for HTTPS protocol, for DNS record validation, SSL certificates, secure communications and for a lot of other things. And, as you might have guessed, it is widely used in cryptocurrencies, specifically, digital signatures are widely used in cryptocurrencies.

When you create a wallet in ANY cryptocurrency, you are given public and private keys. With private key you can basically spend money, because private key is used to sign the transaction data and your public key would be used to verify the signature. Public key is also used to actually receive money. It is important to understand, that "private" and "public" keys are not named like this just for fun. As the name implies, you should keep your private key, well, private, otherwise anybody who knows it would be able to make use of all your money. Public key does not have such restrictions, you can literally store your public keys on a Times Square billboard if you want to.

Key components: hash function

The second important concept to understand is hashing. Lets illustarate hashing by taking a look at small problem. Suppose you have a bunch of texts, like 1000 or more, with each text containing at least a million symbols. And for each text, you need to discover how many of other text are equal to it. The most obvious solution that should come to mind at this point is to go through all texts and compare them one by one. This can last very long, since you have to make 1 million letter comparisons in the worst case. What if we knew how to uniquely identify single text ? And here comes the concept of hashing. Essentially, "hashing" means taking an object and converting its representation into a number with a fixed bit length that uniquely identifies this object. Obviously, bit length of this number should be very high, otherwise, different objects might end up having same hashes. Well known hashing algorithms are SHA-256, which converts object into number with bit length of 256, effectively meaning that there are 2^256 or approximately 10^78 different hashes that can be produced by this function, SHA-384 with bit length of 384 (approximately 10^116), SHA-512 with bit length of 512 (approximately 10^155) and MD5. MD5 has bit length of 128, meaning 2^128 or approximately 10^39 possible hashes that can be produced. This is still way more than total amount of grains of send on earth (approximately 10^19) but MD5 is well known for producing collisions. You can lookup those collisions on the enternet, there are not really uncommon. SHA-256 on the other hand does not have this problem and thus is used more common in production environments and cryptocurrencies as well. Returning back to that 1000 texts problem, what you can do is to calculate SHA-256 hash for each text and compare just them. Since SHA-256 hashes are just 256 bit integers, comparing them would be way faster, than comparing original texts.

Transaction, block and blockchain

Since now we know what are asymmetric encryption and hashing concepts are, lets talk about key structure or ANY cryptocurrency. Those being transaction, block and blockchain. Transaction is a structure, which contains receiver's public key (we have discussed that receiving money is possible via public key only), sender's public key (for digital signature verification as well as to make sure that sender has enough money to spend) timestamp (optional; for example, Bitcoin does not store timestamp in transaction), the amount of currency to transmit and, at it was mentioned already, it contains digital signature of this transaction itself signed by sender's private key. Some cryptocurrencies's transactions can contain multiple receiver wallet addresses, which means that you can send money to multiple people at once. It is important to understand, that the structure of the transaction we have defined is simplified a lot. Real world currencies include more metadata to simplify work for the miners. The transaction does not contain sender's private key (otherwise, everybody would be able to know it).

Block is essentially a list of transactions (in programming terms, data structure name is array), which are packed together. Block also contains hash of the previous block and special integer value, called "nonce". We would discuss what is nonce and why it is used later.

Blockchain is essentially just a chain of blocks. Of course, since blockchain stores all blocks, hence essentially stores all transactions made, it can get quite heavy in size. For example, current Bitcoin blockchain size is estimated to be around 600GiB in size.

Each blockchain contains so-called "genesis" block or initial block or block 0. Genesis block has only 1 transaction, transferring some fixed amount of currency to the first user (50 BTC for Bitcoin).

The purpose of miners

You may ask a question: we have a few transactions, packed in one single block, but what is the deal with blockchain ? Who would maintain it, who would place blocks in it ? Well, here come the miners. In a few words, miners are entities, which keep the cryptocurrency going. They are responsible for placing blocks on the blockchain, receiving and validating transactions (double spending prevention). Each miner has a copy of the blockchain. Miners compete with each other to get the right to place new block on the blockchain. Remembered that nonce integer, that was in the block structure ? It is used to change the block hash. Miners adjust this value to get block hash value, which is less than certain number, which is called threshold. Sometimes, this number is also mentioned as a difficulty, and there are some differences, but we would not discuss it right now, since we do not wont to get into complicated stuff. The miner who first manages to find this nonce value, which makes block hash value be less than the threshold, gets the right to put this block on the blockchain. Other miners would copy this block from the winner miner and continue on the next block. The "winner" (as we call) it miner also gets rewarded with some currency, equal to the total fee of all transaction in the new block plus the block reward (for Bitcoin, as of october 2024, this is equal to 6.25 BTC).

Disadvantages

  • Since each transaction contains sender's public key and receiver's public key, it is not hard to track where the money goes. You just have to analyze the entire blockchain (unlikely) and you would know such things like how many currency a person or entity has or had, where and when approximately the currency was transferred and to whom. This is one of the core disadvantages of cryptocurrencies with so-called "transparent" blockchains (Bitcoin is the one). Fortunately for us (of for people doing illegal activities) there are cryptocurrencies which are untraceable. The one I personally use and recommend is "Monero" or XMR.
  • Each transactions comes with some default minimum fee, which is up to you to set. Obviously, you can set the transaction fee to zero if you want to, but doing this lower the chance of your transaction being included in the blockchain basically to zero. Miners prioritize transactions based on the fee and since they pretty much always have a lot of transactions to work with, your transaction would just become discarded after some time. Some miners have minimum transaction fee threshold which you have to comply with in order to even have a chance to be included in the block. You might specify some higher than average fee, so miners can prioritize your transaction over others. Transaction fee itself is than collected by miners as a reward for the block.
  • 51% precent problem. All cryptocurrencies, including, of course, our lovely Bitcoin are vulnerable to this issue. Basically, this means that if more than 51% of the network of miners are controlled by single entity, this entity would be able to perform some malicious operations such as double spending the currency, censoring some transaction based on sender's or receiver's public key. Not only this kind of attack can cause some malicious transactions to spread over the blockchain, it also causes cryptocurrency to lose its price against other currencies and, most importantly, it causes other miners to give up what they are doing, since they are not able to mine blocks (because the entity who has 51% does this faster than other) and, hence not able to actually profit from mining, resulting in potential takeover of cryptocurrency.

Final words

For better understanding of cryptocurrencies working principles you can take a look these (inside single zip file) python scripts I have written specifically for this article:

example
├── entity.py
├── miner.py
└── requirements.txt