To understand the complicated mining process, you need to know how does it works.
First let’s say what is SHA-256:
SHA-256 (Secure Hash Algorithm 256-bit) is a cryptographic hash function that is widely used in various applications, including blockchain technology and cryptocurrency mining (like Bitcoin). It is part of the SHA-2 family of hash functions, which were designed by the National Security Agency (NSA) to provide a higher level of security than its predecessor, SHA-1.
Here's what makes it special:
- 256-bit Output:
- It produces a fixed-length output of 256 bits (or 64 hexadecimal characters). This means the output is always 256 bits long, no matter how large or small the input is. For example, the hash of the word "hello" will always be 256 bits, just like the hash of an entire book or a long file.
One-Way Function:
- SHA-256 is irreversible. You cannot get the original input back from the hash. This is a crucial feature for ensuring security.
Deterministic:
- For a given input, SHA-256 will always produce the same output (hash). This consistency is vital in applications like blockchain, where each block’s hash is crucial to the integrity of the entire chain.
Collision-Resistant:
- A good cryptographic hash function, like SHA-256, should not produce the same hash for two different inputs (a phenomenon called a collision). In the case of SHA-256, it is extremely unlikely (though theoretically possible) for two different inputs to result in the same hash.
How SHA-256 Works (Basic Explanation)
- Input: You provide a piece of data (could be anything—text, file, transaction data, etc.).
Example: “Hello, world!”
- Processing: The SHA-256 algorithm processes the input data through multiple rounds of mathematical operations (including bit wise operations, logical functions, and padding).
- Output: It produces a fixed-size output of 256 bits (64 characters when represented in hexadecimal format).
Example SHA-256 hash for the text "Hello, world!":
6cd3556deb0da54bca060b4c39479839a9c5b4b1f4c6f8703d7e779be1501f1d
A SHA-256 hash is a 64-character long string that is typically represented using hexadecimal format (base 16). Hexadecimal includes the digits 0–9 and the letters a–f, where:
- 0-9 represent values 0 to 9.
- a-f represent values 10 to 15.
Now let’s continue…
- Prepare the Data (Block Header)
Before you begin searching for the correct hash, a miner must first create a block header. This block header includes critical information:
- Merkle Root: A hash of all transactions in the block.
- Previous Block Hash: The hash of the previous block in the blockchain, linking the blocks together.
- Timestamp: The time at which the block is being mined.
- Difficulty Target: The current network difficulty, specifying how hard it is to find a valid hash (i.e., the number of leading zeros required in the hash).
- Nonce: A random number that miners can adjust to try different hashes.
Example Block Header (simplified):
Block Header:
- Merkle Root: abc1234567890
- Previous Block Hash: 0000000000000abcdef...
- Timestamp: 1633039200
- Difficulty Target: 0000000000000000000000000000000000000000000000000000000000000000
- Nonce: 0 (initially set to 0)
The nonce is the only part of the block header that can be changed during the hash search process. This will be incremented with each new hash calculation.
- Apply SHA-256 to the Block Header
The miner applies the SHA-256 algorithm to the block header, which includes the Merkle Root, the previous block hash, the timestamp, the difficulty target, and the nonce.
- Input: The block header (which includes the nonce).
- Output: A 256-bit hash (64 characters in hexadecimal) is generated by applying the SHA-256 hash function.
Example of an output hash:
9e0b145fdd7c8b201ed43b2e925f4104047e946d7f42f4b3a5b423e5be9f7f74
This hash is the result of applying the SHA-256 function to the block header data.
- Compare the Generated Hash to the Difficulty Target
The next step is to check if the generated hash meets the network's difficulty target. The difficulty target is a number that defines how hard it is to mine a block. It specifies the number of leading zeros the hash must have (in binary form) for the hash to be considered valid.
For example, the difficulty target might look like this (in hexadecimal):
0000000000000000000000000000000000000000000000000000000000000000
This means the hash must be numerically smaller than this target value. More precisely, the hash must have a certain number of leading zeros in its binary form.
- Check: Does the generated hash have enough leading zeros (or is it smaller than the difficulty target)? If yes, the hash is valid, and the miner can submit the block to the network.
- If No: The hash does not meet the target, so the miner must adjust the nonce and try again.
- Increment the Nonce and Repeat
If the generated hash does not meet the difficulty target, the miner will increment the nonce by 1 and repeat the process:
- Increment Nonce: Change the nonce to a new value (e.g., nonce = 1, 2, 3, ...).
- Recalculate SHA-256: Apply the SHA-256 hash function again using the new nonce value and re-generate a new hash.
- Repeat: This process continues as long as the hash does not meet the difficulty target.
For example:
- Nonce = 1: Apply SHA-256 to the new block header with nonce = 1.
- Nonce = 2: Apply SHA-256 to the new block header with nonce = 2.
- Nonce = 3: Apply SHA-256 to the new block header with nonce = 3.
- Check the New Hash
Each time the nonce is incremented and a new hash is generated, the miner checks whether the new hash meets the difficulty target. This is a process of trial and error, and miners will typically try billions of different nonce values before finding the correct hash.
- Finding the Correct Hash
Eventually, one of the generated hashes will meet the required difficulty target, meaning the hash will be smaller than the network's target or have the correct number of leading zeros. For example:
- If the difficulty target is 0000000000000000000000000000000000000000000000000000000000000000, the generated hash might look like:
0000000000000000000000000000000000000000000000000000000000009f34
This hash starts with enough leading zeros, making it valid according to the network's rules.
Summary of the Process:
- Prepare the block header (containing the Merkle root, previous block hash, timestamp, difficulty target, and nonce).
- Apply SHA-256 to the block header.
- Compare the hash to the difficulty target (check if it meets the required condition, such as leading zeros).
- If the hash is invalid, increment the nonce and try again (brute force).
- Repeat the process until a valid hash is found.
- Broadcast the block with the valid hash to the network.
- Receive the block reward and continue mining the next block.
This process is what makes Proof of Work in cryptocurrencies like Bitcoin both secure and resource-intensive, as it requires significant computational power to find valid hashes.
Why You Can’t Just Bomb the Pool with Multiple Tries
If you are familiar with brute force (we already spoke about that), you would probably ask this question. If I am trying to find some hash, why I can’t just send multiple hashes and try to see what will work, not to send one by one.
- Sequential Search: Mining requires sequential attempts (nonce values) to generate hashes, and you can’t bypass this with parallel random guesses.
- Work Distribution: Mining pools distribute work to participants, and random guesses outside the assigned work will not contribute to the solution.
- Energy and Resource Intensive: The system is designed to be computationally expensive, and simply trying random hashes would not help in meeting the difficulty target.
- Block Consensus Rules: A valid hash must adhere to the blockchain’s consensus rules (correct Merkle root, previous block hash, etc.), which random guesses are unlikely to satisfy.
- Difficulty Target: The difficulty target adjusts to ensure mining remains challenging and consistent, making brute force attempts inefficient.
In short, bombing the pool with random guesses won't give you any advantage because the mining process requires valid, sequential hashing attempts, and any hash that doesn't meet the difficulty target is discarded. The system is built to prevent such shortcuts and ensure that miners put in real computational effort to secure the network.
What are actually those expensive systems and what is the main difference between expensive and cheap?
Most of you already know that people are watching the electrical power and think that expensive ones are spending more power, but why is that?
As we already spoke there is SHA-256 that we are looking for. It is a 64-character hash that we need to find. It means that you need every new hash to be created again and again.
In your case, you're searching for a 64-character code where:
- The first character is 0 (fixed).
- The second character is not 0, but can be any of the digits from 1-9 or letters from a-f (this gives 15 possible options).
- The remaining characters can each be any of 0-9 or a-f, i.e., 16 possible characters (0-9 and a-f).
Steps to calculate the total number of possibilities:
- First character is fixed as 0 → 1 possibility.
- Second character can be any of 1-9 or a-f, giving us 15 possibilities.
- For each of the remaining 62 characters, there are 16 possibilities (from 0-9 and a-f).
Total number of possibilities:
Total possibilities=1×15×1662
Where:
- 1 is the fixed possibility for the first character,
- 15 is the number of choices for the second character,
- 1662 is the number of possibilities for the remaining 62 characters.
So, the total number of possible combinations is:
Total possibilities =15×1662
This is an extremely large number, and it’s difficult to fully represent without scientific notation. The key point is that 1662 is a huge number, so the total possibilities are astronomically high.
Would you like me to calculate the exact value of 1662 for better understanding?
Now, this is only calculating for the hash if the 0 is the first character. What would happen if you are looking for hash that has first six characters 0?
Well, here is the thing that we spoke about those super fast expensive systems. You need to know how many hashes can your CPU or GPU produce per second (depends of what are you mining).
If you want to get more things about crypto, follow my next posts. I will also explain the ways mining, the black hole in system, the security that is necessarily and other useful things that you need to know if you are into this. Also, if you have some special requests I am here to help.