How Crypto Works. Bitcoin.

1 day ago 42

Betty Kirkman

The Capital

Since 2010, banks have begun introducing increased bureaucratic barriers when opening accounts for citizens, making it more difficult to withdraw personal funds from the country and emigrate in general. Banks must comply with compliance standards that depend on local laws. Otherwise, they can be forced to pay large fines or even have their banking license revoked. Risks associated with insufficient background checks and customer reliability can include prosecution for terrorist financing, money laundering, state corruption, and secondary sanctions when servicing Russian clients.

Cryptocurrencies have become a popular way to bypass these restrictions. In this article, I will analyze the principles of their operation using Bitcoin and Ethereum as examples. Here, you will learn:

  • What is a blockchain, and why is it structured as a chain of linked blocks?
  • What is mining, and why does it consume so many computing resources?
  • What is a monetary transaction, and why are blockchains so slow and expensive?
  • How do “second-level” systems work in blockchain protocol networks, and how do they make transactions fast and cheap?
  • What is a smart contract?
  • How do stablecoins like USDT work?
  • Is there a way to make your monetary transactions truly anonymous, and is it possible to escape the control of all-powerful financial regulators with the help of cryptocurrencies

The article is designed for beginners and does not require any special knowledge. It clearly explains everything required to understand cryptocurrencies, down to such basic concepts as hash and asymmetric encryption.

The Bitcoin blockchain is a chain of linked blocks. A block is a store of transactions. A transaction is an electronic record of a money transfer. I’ll explain later why transactions are stored in blocks and why those blocks need to be linked, but for now, let’s imagine that the blockchain is simply a history of all the transactions that have ever happened on the Bitcoin network and that it’s stored in some abstract place that everyone on the network can access. Any participant can spend their bitcoins by adding new transactions to the blockchain, but they can’t erase or overwrite existing transactions.

Transaction’s role in Bitcoin can be a bit of a mind-bender. That’s because money on the blockchain isn’t stored in Bitcoin wallets like it is in a bank account balance. All the money on the Bitcoin network is stored in transactions, and to make a money transfer, you have to create a new transaction by moving bitcoins from some existing transaction into it. The balance of your Bitcoin wallet is stored in the transactions you have access to, and you have access to the transactions that are “opened” by your secret cryptographic key. The main function of a Bitcoin wallet is to store this secret key.

Let’s say your neighbor bought a jar of cucumbers from you for 1 bitcoin and, in a separate transaction, bought a jar of premium cucumbers from you for 2 bitcoins. A few years pass, and you spend 2.5 bitcoins buying 5 brand-new Audi Q5.

You create a new transaction, give it 2 inputs (1 and 2 bitcoins), and output 2 outputs — 2.5 bitcoins to the Audi Q5 seller and 0.4 bitcoins to yourself, and leave 0.1 bitcoin as a (very generous) commission to the miner who will process this transaction. Any transaction can have any number of inputs and any number of outputs. Moreover, the total amount of bitcoins at the inputs must equal the number at the outputs. Remember how I said money for new transactions is taken from existing transactions? To be more specific, money is taken from the outputs of existing transactions, and if you use such an output when creating a new transaction, this output will be spent in full. An unspent output is called a UTXO (unspent transaction output). A data structure called a list of unspent outputs (UTXO pool or UTXO set) is stored next to the blockchain. Outputs for creating new transactions are taken from this pool, and when a transaction is confirmed, they are removed from it.

Your unspent outputs are accessed by a private encryption key stored in your Bitcoin wallet. To understand how this works, you must familiarize yourself with asymmetric encryption. Unlike symmetric encryption, in which a message is encrypted with one cryptographic key and decrypted with the same key, asymmetric encryption uses two keys. A message encrypted with the first key can only be decrypted with the second key. One of these keys is public — it can be freely published in open sources and openly distributed over the network. The second key is private and should be stored in a safe place and not shown to anyone. An electronic digital signature works on this principle. If a server sends you an encrypted document, and you can decrypt it with the server’s public key, you can be sure that the document was issued and signed by that server because no one else could encrypt it that way. Bitcoin transactions use slightly different mathematical key processing, but they are based on the same cryptographic algorithms.

To get a Bitcoin wallet, you need to generate a private asymmetric encryption key and a public key based on this private key. If someone wants to transfer bitcoins to your wallet, then (roughly speaking) they will transfer them to your public key.

Each transaction output contains a mathematical problem based on the public key associated with the wallet to which the transfer is made. Only the owner of the private key can solve this problem. This means that only the owner of the target wallet will be able to form a correct, valid transaction using this output.

If you look at the structure of a transaction, the cryptographic task to unlock the UTXO is stored in one section, and important parameters (such as the amount of the money transfer) are stored in another. Let’s imagine a situation where you agreed to send someone 5 bitcoins and decided to use a UTXO worth 10 bitcoins for this. This means you create a transaction with 2 outputs — 5 bitcoins to the recipient and 5 to yourself (let’s forget about the miner’s commission for now). Let’s say that the recipient intercepted your transaction before it entered the network and replaced the values in its sections with 10 and 0, respectively, thus ensuring that 10 bitcoins go to his account and 0 to yours. To make such attacks impossible, verifying the digital signature of all data in the new transaction, including the funds transferred, is added to the cryptographic task blocking the transaction output. An attacker won’t be able to create a valid transaction with other amounts because they won’t be able to sign it with your private key.

Your Bitcoin wallet balance is the sum of all the UTXOs in the blockchain that you can spend using your private key. If someone steals this key, they can access all your bitcoins. If you lose your private key beyond recovery, all your bitcoins are lost to humanity — no one will ever be able to spend them.

Let’s return to the “abstract place” where the blockchain is stored. The Bitcoin network is powered by thousands of computers called full nodes. Let’s assume that each stores a copy of the entire blockchain. That is, the history of all transactions in the Bitcoin network is stored and distributed across many computers, making it virtually impossible for any state to paralyze the network. Nodes constantly exchange data on newly confirmed transactions, and the current state of the blockchain is constantly synchronized between nodes. This makes it impossible for the owner of a single node to replace the transaction history.

In addition to the blockchain, nodes require a UTXO pool to operate. This pool is not synchronized between nodes but is formed independently by each node when analyzing the blockchain. After installing a full node on a computer, it goes through the entire blockchain and adds all transaction outputs that have not yet been used in this blockchain to the UTXO pool. Another data structure that full nodes work with is a queue of unconfirmed transactions called the Mempool. Unconfirmed transactions are distributed across the network between full nodes, but mandatory and complete synchronization with all nodes is not required here, unlike the blockchain.

In fact, not all full nodes store all 600 GB of blockchain history. To maintain the network and verify transactions in the Mempool, it is enough to store a certain number of recent blocks and the current UTXO pool. In addition, nodes do not communicate or synchronize data directly with absolutely all full nodes in the Bitcoin network — but they form a single decentralized network in which the current state of the blockchain from one node always comes to all other nodes.

In addition to storing the blockchain itself, the UTXO pool, and the Mempool, nodes ensure that new transactions are verified before they are added to the Mempool and that all blocks are verified before they are added to the blockchain. A block is a special container that contains, on average, several thousand transactions. A blockchain, as the name suggests, is a chain of blocks. Before I explain a block, I should explain what a hash is.

A hash is a number that a hash function generates from an arbitrary input data sequence. Bitcoin uses the SHA256 hashing algorithm. The output of the hash function of this algorithm is a 256-bit number. The input to the hash function can be data of any shape and size. It could be a collection of your favorite movies, technical documentation, a recipe for garlic jam, or just the number 2. In any case, the output is a gibberish of 256 bits. Moreover, different data gives different hash at the output, and even by changing just one letter in the input data, when hashing, you will get a completely different set of bits that do not resemble the previous one. Since the number of variations of arbitrary input data is infinite, and the number of variations of a 256-bit hash is 2256, it is impossible to assign a unique hash to each possible set of data, so there will inevitably be different sets of data for which the hashing algorithm will give the same result. This phenomenon is called a collision. Mathematically, hash functions are designed so that the probability of collisions is vanishingly small, and finding collisions is practically impossible.

The most obvious application of hash functions is to check the integrity of data. For example, in Internet connections, outgoing packets contain a hash of the transmitted data. When the receiving party receives a packet, it independently calculates the hash of this data and compares it with the hash specified in the packet. If these hashes do not match, then it is considered that the data was damaged during delivery, and the packet is discarded. Another useful property of the SHA256 hash (and many other hashing algorithms) is that the hash of the data is calculated in a fraction of a second while finding data that produces a given hash at the hash function’s output takes millions and billions of years. To simplify the above, the hash function is calculated quickly in one direction and slowly in the other. This is useful, for example, in a client authorization system, when the server stores not user passwords but hashes of these passwords. The server memory usually stores a table containing a login, password hash, email, and other data for each client. During authentication, the client sends the server a password, the server calculates the hash of this password, and if it matches the hash specified in the table for this client, the server provides it with an access token. Even if attackers steal the database with the authentication table, they will not receive the passwords themselves, only their hashes. Using hashes, they will not be able to log in to the server and gain control over user profiles since they will first have to restore the password from the hash for each user, spending billions of years on this.

Now, let’s get back to the Bitcoin blockchain. Each block (except the very first block in the entire blockchain, called the genesis block) contains a hash of the previous block. This makes it impossible to change a block in the middle of the blockchain without changing all the blocks following it. This is an example of using hash functions to check data integrity. Without the previous block’s hash, the blockchain will lose its coherence and turn from an ordered chain into a set of disparate blocks. Why the blockchain needs to remain a chain will become clear later. In addition to the previous block’s hash and the list of transactions, each block contains several more information fields, including nonce and difficulty target.

These 2 parameters are introduced to turn the formation of a block into a long and computationally complex process. Calculating the previous block’s hash and the Merkle tree’s root from all transactions, writing a list of all transactions, and adding a timestamp (date and time of block formation) are quick operations that do not require large computing resources. Thanks to the nonce and difficulty target, this simple work becomes a task for a mining farm — a specialized supercomputer assembled from either video cards or ASIC processors designed specifically for mining purposes. These enterprises worldwide spend an inadequate amount of electricity, bring global warming closer, and periodically arrange an explosive increase in video card prices, causing gamers frustration. If aliens fly to us in the coming years, they will certainly be interested in what kind of complex and incredibly important task this is, on which humanity spends so much energy and computing resources. When they realize that all these racks of specialized computers are heating the atmosphere and making noise with fans to find hashes smaller than a given target, they will start looking at us like we are weirdos.

The miner’s task is to take the required number of transactions from the Mempool, assemble a block from them, including an arbitrary number (the same nonce) in it, calculate the hash from all this data, calculate the hash from the resulting hash and compare it with the difficulty target. If the hash exceeds the target, another nonce is taken, and the operation is repeated. The miner goes through different nonce values ​​​​until the hash is less than the difficulty target. How long it takes depends on luck and the available computing power. Such a parameter as hash rate measures the computing power of a mining device — this is the number of hashes that the device calculates in 1 second. The total hash rate of the entire Bitcoin network as of January 2025 is 753.5 quintillion. Independent mining is not a very promising idea for owners of mining farms. Most of them prefer to unite with other miners in a mining pool and work together to calculate the same block and share the profit proportionally to the invested computing power.

It might seem that the speed of calculating a block in the Bitcoin network should be determined by the computing power of the largest mining pool since it calculates blocks faster than all other participants. After it publishes another block, the work of the other participants will be in vain, and they will be forced to stop and start calculating the next block — which, again, with the highest probability, will be calculated by the largest mining pool. Miners consistently select different nonce values within one pool, and different pools mine blocks with different contents, and the probability that two miners will calculate the same hash is vanishingly small. Thus, it is not that two mining pools compete in terms of speed in performing the same work. Rather, they are doing a common job of finding the right nonce, and the speed of doing this work is determined by the hash rate of the entire Bitcoin network and not by the hash rate of the largest mining pool. The success of finding each specific block is determined by luck, but over a long period, according to the laws of statistics, blocks are distributed proportionally to the shares in the Bitcoin computing network. We can say that if Foundry USA owns 30% of all capacities and AntPool — 25%, then Foundry USA finds 30% of new blocks and AntPool — 25%.

A new block is added to the blockchain every 10 minutes. If the computing resources of miners increase, then Bitcoin algorithms lower the difficulty target, thereby complicating the mining task so that the rate of block appearance remains at the level of “once every 10 minutes”. The target is revised every 2016 block (approximately once every two weeks).

The reward for the miner’s work, in addition to the user commission, is the emission of a fixed number of bitcoins taken out of thin air. The emission size is called the block reward and decreases by 2 times every 210,000 blocks. In April 2024, the block reward decreased to 3.125 BTC; in 2028, it will decrease to 1.5625 BTC. Due to the regular halving of the reward built into the Bitcoin algorithms, the total number of Bitcoins will never exceed the 21 million limit. Bitcoin deflation was originally built into its protocol at the design stage.

The miner transfers the block reward and the sum of all fees to himself, including a special type of transaction called “Coinbase” in the block. It does not spend any outputs from the UTXO pool and is located at the beginning of the list of transactions in each block.

Let’s assume that one miner has calculated a valid block and started distributing it to the nearest nodes. But before this block had time to spread across the network, another miner on the other side of the planet calculated another block and started distributing it to full nodes. The 2 new blocks in the network refer to the same previous block and store its hash. The blockchain has split into two branches. In this case, one part of the full node stores the first branch, and the other — the second. When the full node notices that one of the branches has become longer, it discards the shorter branch and switches to the longer one. The rule in the Bitcoin network is that the longer branch is true. Transactions from the short branch are returned to the Mempool, and the miners who formed its blocks lose their rewards and fees.

Due to the complexity of calculating a block, branching in the blockchain network is relatively rare. The probability of two miners simultaneously forming a valid block is quite low, and the probability of this happening twice in a row and two equal branches of two blocks in length appearing in the network is even lower. The probability of this happening a third time is even lower. In the end, a new block will inevitably appear only in one branch, with the subsequent displacement of the other branch being shorter.

Due to branching, it is recommended to consider your transaction completed only after receiving 6 confirmations, i.e., after the transaction gets into a block, and after this block, 5 more blocks are added. If you assign a fairly generous commission, and the miner designs to include it in the next block, you will have to wait about 50 minutes, and with a certain amount of luck, you can consider your transaction completed with a clear conscience. In the sphere of regular bank transfers, such speed of operations is not considered acceptable, but such is the price for anonymity of payments. The Bitcoin network has a “second layer” technology called Lightning Network, which provides an acceptable transaction speed and low fees, but we will talk about this later.

Miners must spend large amounts of computational resources to form blocks, a consensus mechanism called Proof of Work (PoW). Consensus is the process by which all participants in the network confirm the correctness of transactions and agree on which transaction history is considered authentic. Now that we have a global understanding of the system, we can ask why we need block coherence and PoW.

What happens if you abandon the connectivity of blocks, i.e., remove the link to the previous block (in the form of its hash) from the block? The N chain of blocks will become a regular list of unrelated objects. In this case, how will the system detect repeated spending of already spent bitcoins? Given two transactions with the same coins at the input, you must determine which is correct and which should be deleted. Naturally, the transaction that occurred earlier is considered correct, so you need to sort the list of blocks by the time they were added, i.e., by the timestamp specified in the block. But how will you check the correctness of the timestamp? All full nodes in the network are equal, and if one node thinks that it is 6 am UTC and another that it is 10 pm, then which of these times should be considered true? Given that a node owner might try to trick the block-adding time system into pulling off some kind of fraudulent scheme, these kinds of time discrepancies are bound to arise. If you add blocks with the timestamp specified by the block creator, keeping the order of blocks in ascending time, then the creator will simply be able to specify minus an hour from the current time in the timestamp and insert his block somewhere in the middle of the list, before many already added blocks. In this way, he can buy something with his bitcoins, receive the goods, and then add a block in the middle of the list, sending the same bitcoins to himself, invalidating the transaction for the goods, and the seller will lose money.

In addition to these problems, the computational complexity of transaction verification will increase. You will no longer be able to rely on a data structure such as a UTXO pool. When a new block appears in the middle of the list, you will have to go through many previous and subsequent transactions to determine which transactions have become invalid after that.

Returning to a simple chain of blocks linked by a hash makes sense. Let’s try to save electricity and remove Proof of Work from this system. In this case, the network will lose the ability to control the rate of release of new blocks (every 10 minutes) and thus control the emission (the same “block rewards” that are taken out of thin air). The appearance of new blocks will become too short compared to the synchronization of the blockchain between nodes so that branching will occur constantly, and the branches will be much longer than now. An attacker who owns a large number of nodes will be able to organize a “51% attack”, i.e., pay for and receive the goods, and then distribute a chain of blocks across the network, which will begin before the transaction he made, and which will be longer than all the chains containing this transaction. Thus, the goods will be received, but their payment will be canceled.

Miners waste a huge amount of electricity on seemingly meaningless calculations, but they are a necessary evil for the Bitcoin network. Since a transaction is considered confirmed after adding 6 blocks, a “51% attack” involves calculating a parallel branch of 6 blocks faster than the rest of the network calculates the same branch. This requires at least concentrating more than half of the Bitcoin network hash rate in your hands, which is almost impossible and so expensive that this event is unlikely to pay off even in the case of a successful attack. Since the “winner” in the race to add a block is determined randomly, a “51% attack” can fail even if the attacker has the necessary computing power. The attack assumes that the attacker will spend a lot of bitcoins and then get his money back by rewriting the history of the blockchain. But he won’t know when he should spend these bitcoins because he doesn’t know when his computing network will calculate 6 blocks faster than the rest. Moreover, if such huge capacities switch to calculating a parallel branch, this will lead to a drop in the Bitcoin network hash rate by more than half, which will be immediately noticeable to all other participants. To do this secretly, you will have to create a separate mining network, the capacity of which should exceed not half but the entire capacity of the existing network.

Unlike mining, running a full node does not bring direct financial benefit. So, what makes people run these nodes on their machines? Many do it to support the Bitcoin network, either out of pure altruism or because this network is a necessary element in the infrastructure of their business. Running a full node is quite cheap. It can be handled by an ordinary computer connected to the Internet at an ordinary unlimited tariff. By creating an additional node in the Bitcoin network, you increase the degree of decentralization, the security of this network, and its resistance to attacks.

It is believed that your full node increases the security and privacy of your operations. Using someone else’s node, you work through an intermediary, and depending on the implementation of a particular service, a third party can record your activity, your transactions, IP addresses, etc., and provide you with incomplete or distorted information about the state of the blockchain.

For cryptocurrency investors, running a node allows them to collect advanced statistics on Bitcoin transactions to predict rate fluctuations. To connect to the Bitcoin network, you need to know the IP addresses of at least a few nodes. Information about nodes is stored on DNS seeders, such as bitseed.xf2.org, dnsseed.bluematt.me, seed.bitcoin.sipa.be, dnsseed.bitcoin.dashjr.org, seed.bitcoinstats.com. Due to the limited number of seeders, the Bitcoin network cannot be called completely decentralized, but this kind of centralization does not create an additional threat of attacks.

The genesis block of Bitcoin contains one coinbase transaction that sends 50 BTC to an unknown address. No one can spend these 50 BTC in principle. All other bitcoins were obtained due to emission when creating new blocks, and the first blocks did not contain any transactions at all, except coinbase, since the genesis block does not contain UTXO that could be spent. The mysterious creator of Bitcoin, Satoshi Nakamoto, chose to remain anonymous and disappear after publishing the genesis block, so now no one knows where he is or who he is. Perhaps now he sighs sadly, looking at modern quotes. On the other hand, it could be some visionary from the drug business who should not count such pennies as 50 bitcoins.

Read Entire Article