What exactly is cryptocurrency mining? A short introduction…
Bitcoin: distributed account keeping
Bitcoin is software. What differentiates it from most other software applications, however, is the fact that it is decentralized. This means that a copy of the software is found on each computer that is part of the Bitcoin network. These copies talk to each other. The Bitcoin program is modified on a consistent basis as bitcoins are sent and received over the network. Each computer (node) works with other nodes to approach a consensus — an agreement over new transactions taking place on the network. What makes Bitcoin especially popular, is the value that is attached to each bitcoin. Most importantly, this value may be traded for other currencies such as the USD or the Euro, thus making Bitcoin currency in its own regard.
Unlike fiat currencies which are printed, bitcoins need to be mined. Miners use processing power to create new bitcoins. Creating new bitcoins is only secondary, however, as the real utility of mining lies in validating transactions occurring on the network. Each miner on the decentralized Bitcoin network plays an essential role as mining is the only way new transactions may be verified. This is the only way to keep the network secure. The process of verification ensures only a majority may decide the state of the blockchain, essentially just a list of transactions — a digital book of accounts that holds data on who has how much. Transactions on the blockchain are grouped into blocks that form a chain.
The greatest utility of blockchain is the security it provides to its contents. Mined transactions are immutable — unless an attacker can produce more than 51% of processing power on the network (a rather difficult maneuver). Transactions are grouped into blocks. Each block contains one megabyte (1 MB) of transactions. A new block is created roughly every 10 minutes. Once it is created, it may be described as ‘mined’.
A closer look at the blockchain reveals greater complexity and the ingenuity that gives Bitcoin and other cryptocurrencies their true breadth and scope but there are still many challenges that are part and parcel of blockchains. At the heart is verifying the legitimacy of transactions.
Finding new solutions to old problems
One of the greatest drawbacks of using software as currency is its ability to be easily copy-pasted. Anyone may make copies of a digital dollar and use it repeatedly — a process known as “double spending”. This would defeat its utility as a currency since anything that can be created by anyone without effort out of thin air quickly loses its value.
Double-spending is not a problem with traditional fiat such as the USD or the Euro. As soon as someone makes a purchase with their money, that money is spent. A person handing over money to the cashier at a store may no longer use that money next door. Even electronic transactions handled by banks and other financial institutions leave a trail of records in a central database as well as paper receipts. Even carefully crafted counterfeit notes can be detected with the right technology. Thus, traditional fiat is very difficult, if not impossible to double-spend.
Digital currencies, before the development of Bitcoin, were not able to overcome this problem. Satoshi Nakamoto’s whitepaper on Bitcoin describes a process that verifies whether a transaction on the Bitcoin network is indeed legitimate (and involves no double-spending). In a nutshell, this process involves the use of computer processing power to solve difficult problems with solutions that are easy to verify. We call this mining.
Since Bitcoin is decentralized, a copy of its blockchain is found on every computer (or node) on the Bitcoin network. As mentioned earlier, since there is no central authority to verify the legitimacy of transactions on the network, that task is left to miners. For example, on a network that accepts transactions entirely without verification, anyone could easily edit their copy of the ledger to increase their holdings. The network would have no way of distinguishing between honest and corrupt nodes.
The miner’s task is to ensure all nodes on the network have a consistent copy of the Bitcoin blockchain. This is achieved by creating (mining) a new block from outstanding transactions. In other words, miners group new transactions together which are not yet part of the blockchain. These transactions are then checked for inconsistency. If the block is found to be valid and the network, reaches a consensus, that block is added to the blockchain, and each node updates its copy of the blockchain with the new block announced to the network.
One way of determining whether a transaction is legitimate is to check every other transaction in the group for a matching hash (a unique string of digits created by the miner). Hashing is a cryptographic technique widely utilized by cryptocurrencies. There are many different types of hashing functions available, but they all offer the same utility — providing an output without revealing the input.
For example, we can calculate a 256-bit hash of the word, ‘cat’, and arrive at the following string of digits:
More than keeping each string to a predetermined length, hashing is used for it’s ability to keep each output, for a particular input, completely unique. For example, a 256-bit hash of the word, ‘bat’, gives use the following string:
Note the difference between the hash of ‘cat’ and ‘bat’ is significant. In other words, it is not easy to guess the word simply by looking at the string. Hashing hides the input while revealing the output. This is known as one-way hashing.
Hashing functions take any input and provide an output that is a fixed-sized string. Different types of hashing functions are widely employed but they all display four special properties that make them useful. These properties enable these functions to be:
- Deterministic: for any input, the output will always be the same.
- Efficient: finding the output does not require a lot of computing power
- Unique: no two inputs return the same output
- Irreversible: the input cannot be determined from the output
From bits to blocks
When Alice wants to send Bob some bitcoins, she asks Bob for his wallet address. However, this address works differently than an email or a residential address. When Alice sends Bob her bitcoins, instead of directing them to an actual address, she broadcasts the transaction on the Bitcoin network.
The new transaction contains information that includes the addresses for the sender (Alice) and the recipient (Bob). In short, New entries added to the a block consist of the following information:
- the origin and destination of the transaction
- the date and time of the transaction
- a hash — the unique code that distinguishes transactions from one another
The origin and destination of each transaction contains addresses which are public keys, a hash generated from private keys available only to the owners of the respective wallets — in this case, Alice and Bob. Public keys act as addresses and can be safely shared and used as part of information stored on the blockchain.
The transaction created by Alice is signed with her private key as proof that only she and no one else made the transaction. These new transactions, including the amount sent by Alice to Bob, are then validated by miners before being added to the blockchain, a process which generally takes about 10 minutes. This is the approximate duration that miners require to collect new transactions, mine them into a new block and receive confirmation from other miners on the Bitcoin network that the block is legitimate. An approximately consistent duration of time spent mining each block is by design, as we’ll see in the next part.
The code for Bitcoin program automatically sets a difficulty for mining. This is done so that mining a new block intentionally takes some work to accomplish. The difficulty in mining deters malicious use of the blockchain by requiring a substantial but feasible amount of effort.
The amount of difficulty is set by a target hash. In order for a particular hash to be regarded as valid and the block to be mined, the hash must be less than the target hash.
For example, the here is the hash of block number 635440:
The above hash had to be less than or equal to the value of its target in order to be validated as a block by the network. As a simpler example, let’s suppose the target is 000536. Any calculated hash with four zeroes will work (000089 < 000536) but some hashes with three zeroes will work (000456 < 000536) but 000789 won’t work.
Since hashing is deterministic, guessing the correct hash with a single input (a megabyte of transactions) will only lead to the same output. In most cases, this will not be an acceptable outcome. To overcome this, a number is added to the hashed input, which when rehashed falls within the difficulty limit. This number is called a nonce (a “number only used once”). It is a 32-bit number that miners look for when mining a block. In other words, the ‘math problem’ is essentially just guess work for the computer targeted towards finding the correct number to hash the input with.
Finding the nonce is extremely difficult and is more akin to winning the lottery. Having greater computational power gives the miner a better chance at being the first to discover this number. Once a nonce is found which can generate a hash that is less than or equal to the target hash, the discoverer of the nonce earns the reward for their discovery and the nonce is announced to the network before the block can be mined.
To ensure that each block is mined in approximately 10 minutes, the mining difficulty self-adjusts every 2016 blocks depending on the total network hash rate measured in terms of megahashes (a million hashes per second), gigahashes (billion per second), and terahashes (trillion per second).
The average time to generate a new block can be calculated using the following formula:
time = difficulty * 2**32 / hashrate
Approximately 1 in 232 or 1 in 4.2 billion hashes succeeds. An individual attempting to mine using a CPU at 12 million hashes/sec would take millions of years before they succeeded. The difficulty and the work required in mining ensures that the blockchain may only be modified through consensus. In other words, the blockchain may only be changed through collective effort. This is what keeps Bitcoin secure.
Without a set difficulty, coming up with a target hash for a new block would be trivial for a modern computer. Requiring a significant amount of work to be performed by a miner significantly impedes any malicious activity on the blockchain. This requisite work is known as proof-of-work (or PoW), a concept developed for digital currencies by Hal Finney (who was also the recipient of the first Bitcoin transaction).
Proof of Work
The blockchain is immutable. Mined blocks cannot be altered. This immutability exists due to cryptographic mechanisms in place such as hashing functions, as well the significant time and resources required for mining valid transactions.
Each block on the blockchain contains information regarding its contents: each transaction contained within the block, a timestamp, as well as a hash of the previous block in the blockchain. A malicious user attempting to change the contents of an already mined block would have to re-mine that block in order to find a valid hash. This is because changing even a single bit of data in a block by, for example, increasing the amount of bitcoins sent to a particular wallet, would result in a completely different hash.
Not only would this take a user with limited computational power an enormous amount of time to, it would also make no difference even if the attempt succeeded. This is because the block needs to be accepted by the majority of nodes on the network. In other words, it would be extremely difficult, if not impossible.
For the blockchain, hashing helps mitigate the problem with double-spending. Conflicting transactions, or transactions that display the same hash are automatically discarded from the new block.
When a chunk of valid transactions is found, it is then structured into a merkle tree. In simple terms, a merkle tree can be thought of as an upside-down tree with the roots on top and the leaves below. Transactions form the leaves at the bottom of the tree.
Merkle trees help identity whether or not a particular transaction is part of a block. This is achieved by continuously hashing pairs of transactions until only a single hash or the root hash or merkle root remains. In case an odd number of transactions is collected, the last transaction is duplicated to create an even number of leaves. Since any input of a hash function leads to a single and deterministic output, the merkle root is always the same as long as the transactions within are left unchanged. The merkle root is also unique and acts as an identifier for all of the transactions within a block.
Merkle trees are essential to the blockchain as they allow miners to create a hash of the content of each block. As discussed earlier, changing a single bit of data in a block completely changes the hash of the block. Thus, malicious attacks on the network can be prevented simply by comparing the root of a modified block’s merkle tree to that of a valid block’s merkle tree.
The block header
Each block on the Bitcoin blockchain contains six important pieces of data in a section known as the block header. This is an 80-byte string that is further hashed for use in the next block.
- Version number — The version number describes the block validation rules followed by the block. Different block versions have been used throughout the course of the development of Bitcoin. 4-bytes.
- Previous bock hash — The hash of the previous block header which acts as a pointer. Changing this value requires changing every previous block on the blockchain. This is the key to the blockchain’s immutability. 32-bytes.
- Merkle root — Hash of the blockchain’s merkle root, a derivation of the hashes of all transactions contained within this block. 32-bytes.
- Timestamp — The number of seconds elapsed since January 1970. This is measured in Unix epoch time. The timestamp is recorded at the exact instance the miner began hashing the header. 4-bytes.
- Difficulty target (bits) — A hashed value that describes the maximum value that the hash of the block header must fall under. 4-bytes.
- Nonce — The number that modifies the header hash such that the product hash is less than or equal to the target difficulty threshold. 4-bytes.
In practice, each successive field is layered on top of the one before. The code for the block headers looks like this:
Not all blockchains are built equally. There are numerous different algorithms for verifying transactions over each blockchain. These are just the most popular as of today.
Short for Secure Hash Algorithm, the SHA-256 algorithm is a popular hash function used by early cryptocurrencies such as Bitcoin. The algorithm produces a message digest or a string of digits from an input. The algorithm cannot produce two different outputs for any input, hence it is used to check the integrity of an input (such as a file) as any change to the contents will result in a different output.
ASIC miners are specially designed to work with the SHA-256 algorithm.
Scrypt was developed by Colin Percival to make large-scale custom hardware attacks difficult to perform. The algorithm makes use of large amounts of memory and offsets the efficiency of logic gates used by ASICs. Various cryptocurrencies such as Litecoin use a simplified version of the scrypt algorithm as a proof-of-work in order to mitigate the centralization of computing power occurring in the Bitcoin community due to mining companies employing large-scale ASIC hardware.
Unfortunately, the simplification in the scrypt algorithm that was originally meant to keep only CPU mining viable, ultimately led to unforeseen consequences. Today, there are scrypt-based ASICs available, decrying the endeavour to keep centralization at bay as a failure.
Similar to scrypt, CryptoNight was developed with an egalitarian vision in mind. The algorithm makes use of memory, specifically, the L3 cache of the CPU making it inefficient for GPUs and ASICs which lack cache memory. It emphasises random access speeds and dependence on memory latency by invoking a ‘scratchpad’ — 2 MB of memory allocated towards storing working data. In other words, the use of memory keeps the algorithm from being used by ASICs and substantially lowers the efficiency of GPUs.
Despite the algorithm being targeted by specialized ASICs developed by several manufacturers, the developers of CryptoNight have managed to repeatedly increase the memory constraints of the algorithm.
CryptoNight is used by many popular cryptocurrencies such as Monero and Bytecoin.
Ethash was developed as a proof-of-work algorithm for the Ethereum blockchain. It is an updated version of the Dagger-Hashimoto algorithm developed by Vitalik Buterin and the Ethereum team. Much like other SHA-256 alternatives, Ethash was designed to offer resistance against ASICs.
Ethash can be utilized by CPUs as well as GPUs. While CPU mining using this algorithm is no longer profitable, it can still adequately power smart contracts or facilitate cryptocurrency transactions over the network. CPU mining can be performed using an Ethereum client called ‘geth’.
GPU mining using Ethash is far more efficient but requires at least 1–2 GB of RAM per GPU. AMD GPUs also fare better than Nvidia. GPU mining can be done using Ethminer, a Ethash mining software.
While ASIC mining using Ethash is possible, it is highly discouraged by the Ethereum community as well as the developers.
The X11 algorithm was developed by Evan Duffield in 2014. It is named for its implementation of eleven different hashing algorithms for proof-of-work. These are: blake, bmw, groestl, jh, keccak, skein, luffa, cubehash, shavite, simd, and echo.
X11 is known for its energy-efficiency compared to other mining algorithms. It is approximately 30% more efficient than SHA-256.
While this is in no means comprehensive, I hope you can now leave with just a little more understanding of mining and the blockchain.
- Top 6 Softwares for Managing Bitcoin Mining
- A Simple Guide to Mining Pools
- The Best Crypto Trading Bots
- The Best Bitcoin Hardware wallet
- The Best Crypto Tax Software
- Best Crypto Trading Platforms
- Best Crypto Lending Platforms
- The Idiots Guide to Margin Trading on Bitmex
- The Definitive Guide to Crypto Swing Trading
- Bitmex Advanced Margin Trading Guide
- Best Crypto APIs for Developers
- Crypto arbitrage guide: How to make money as a beginner
- Top Bitcoin Node Providers
- Best Crypto Charting Tool