How Bitcoin Mining Works (Advanced)?
Introduction
Bitcoin mining is the process through which new blocks are added to the blockchain and new Bitcoin are issued. At an advanced level, mining involves complex mathematics, dynamic difficulty adjustment, competition between miners, and sophisticated consensus mechanisms. This guide will explain the deep technical fundamentals of how mining really works.
Important: This is an advanced level guide. We assume basic knowledge of blockchain, hashing (SHA-256), and Bitcoin concepts. If you're a beginner, we recommend first reading about basic mining before advancing to this deep technical content.
By the end of this guide, you'll understand how difficulty works mathematically, how nonce is used, how retarget adjusts difficulty, how competition between miners works, and the formulas that govern the entire mining process.
Mining Fundamentals
What Is Mining?
Mining is the process of finding a hash that meets difficulty criterion, adding block to blockchain and earning reward.
Objective:
- Create new block on blockchain
- Validate pending transactions
- Add block to longest chain
- Earn block reward (new Bitcoin + fees)
Basic process:
- Collect pending transactions
- Create candidate block header
- Try to find hash that meets difficulty
- If found, mine block and earn reward
Block Header
Block header contains:
- Version (4 bytes)
- Previous block hash (32 bytes)
- Merkle Root (32 bytes)
- Timestamp (4 bytes)
- Difficulty bits (4 bytes)
- Nonce (4 bytes)
Total: 80 bytes
What miner needs to do:
- Keep most fields constant
- Vary nonce (and other fields if necessary)
- Try to find valid hash
Difficulty: The Heart of Mining
What Is Difficulty?
Difficulty is a number representing how difficult it is to find valid hash compared to easiest case.
Basic formula:
Difficulty = Maximum_Target_Difficulty / Current_Target_Difficulty
Target:
- Number that hash must be less than
- Represented in "bits" in header
- Smaller target = higher difficulty
Target formula:
Target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000 × (2^(8 × (0x1d - 3)))
Simplified:
Target ≈ 2^(256 - difficulty_bits)
How Difficulty Works
Bits representation:
- Difficulty is represented as "bits" in header
- Bits encode target in compact form
- Conversion formula:
target = mantissa × 2^(8 × (exponent - 3))
Simplified example:
- Bits:
0x1d00ffff - Exponent:
0x1d = 29 - Mantissa:
0x00ffff - Target =
0x00ffff × 2^(8 × (29 - 3)) - Target =
0x00ffff × 2^208
Interpretation:
- Block hash must be less than target
- Smaller target = higher difficulty
- More leading zeros needed in hash
Valid Hash
Hash is valid if:
SHA-256d(header) < Target
What this means:
- Hash must be numerically less than target
- Target is very large number
- Chance of valid hash is extremely low
Probability:
- Chance of valid hash ≈ Target / 2^256
- For current difficulty: ~1 in 20 trillion (approximately)
- That's why needs lots of computational power
Difficulty Adjustment
Difficulty adjusts every 2016 blocks:
- Ideal time: 2016 blocks × 10 minutes = 20160 minutes = 2 weeks
- If blocks were mined faster: difficulty increases
- If blocks were mined slower: difficulty decreases
Adjustment formula:
New_Difficulty = Current_Difficulty × (20160 minutes / Actual_time_of_last_2016_blocks)
Example:
- Last 2016 blocks mined in 1 week (half the time)
- New difficulty = Current_Difficulty × (2 weeks / 1 week)
- New difficulty = Current_Difficulty × 2
- Difficulty doubles to compensate for higher speed
Limits:
- Maximum adjustment per time: 4x (increase or decrease)
- Prevents too drastic changes
- Stabilizes network
Nonce: The Variable Field
What Is Nonce?
Nonce (Number Used Once) is 32-bit field in block header that miners vary to try finding valid hash.
Characteristics:
- 32 bits (4 bytes)
- Value from 0 to 2^32 - 1 (0 to 4,294,967,295)
- Can be any value
- Miner varies to try different hashes
How it works:
- Miner starts with nonce = 0
- Calculates header hash
- If not valid, increments nonce
- Repeats until finds valid hash or exhausts nonce
Nonce Limit
Problem:
- Nonce only has 32 bits
- Only 4.3 billion possible values
- With current computational power, nonce exhausts quickly
Solution - Extra Fields: When nonce exhausts, miner varies other fields:
1. ExtraNonce (in coinbase):
- Variable in coinbase transaction
- Allows more attempts
- Practically unlimited
2. Timestamp:
- Can vary slightly (±2 hours)
- Allows additional attempts
- Limited by protocol
3. Version:
- Can use different version
- More possible attempts
- Limited
Total attempts:
- Nonce: 2^32 possibilities
- With ExtraNonce: practically unlimited
- Miners never "run out" of attempts
Mining Process with Nonce
Step by step:
1. Prepare Block:
- Collect transactions
- Create Merkle root
- Build header with nonce = 0
2. Calculate Hash:
hash = SHA-256d(header)
3. Verify Hash:
If hash < Target:
✅ Valid block! Mine and earn reward
Else:
❌ Invalid hash, keep trying
4. Increment Nonce:
- Nonce = Nonce + 1
- Go back to step 2
- Repeat until find valid hash
5. If Nonce Exhausts:
- Change ExtraNonce in coinbase
- Reset nonce to 0
- Continue trying
Retarget: Dynamic Adjustment
What Is Retarget?
Retarget (readjustment) is process that adjusts mining difficulty every 2016 blocks to keep block time at ~10 minutes.
Objective:
- Keep average block time at 10 minutes
- Compensate for changes in computational power
- Stabilize Bitcoin emission
How Retarget Works
Frequency:
- Every 2016 blocks
- Approximately every 2 weeks
- Block where retarget happens is special
Calculation:
1. Calculate Actual Time:
Actual_Time = Current_Block_Timestamp - Block_Timestamp_2016_blocks_ago
2. Calculate New Difficulty:
New_Difficulty = Current_Difficulty × (20160 minutes / Actual_Time_minutes)
3. Apply Limits:
If New_Difficulty > Current_Difficulty × 4:
New_Difficulty = Current_Difficulty × 4
If New_Difficulty < Current_Difficulty / 4:
New_Difficulty = Current_Difficulty / 4
Practical example:
Scenario 1: Blocks Too Fast:
- Last 2016 blocks mined in: 1 week (10080 minutes)
- Ideal time: 20160 minutes
- New difficulty = Current × (20160 / 10080)
- New difficulty = Current × 2
- Difficulty doubles to slow down mining
Scenario 2: Blocks Too Slow:
- Last 2016 blocks mined in: 4 weeks (40320 minutes)
- Ideal time: 20160 minutes
- New difficulty = Current × (20160 / 40320)
- New difficulty = Current × 0.5
- Difficulty halves to speed up mining
Scenario 3: Perfect Time:
- Last 2016 blocks mined in: exactly 2 weeks (20160 minutes)
- New difficulty = Current × (20160 / 20160)
- New difficulty = Current × 1
- Difficulty stays same
Why 2016 Blocks?
Reasons:
- Sufficient time: 2 weeks is reasonable time to calculate average
- Not too frequent: Avoids too constant adjustments
- Statistically significant: Large enough sample to calculate reliable average
- Balanced: Not too fast, not too slow
Compromise:
- More frequent: more accurate, but more complex
- Less frequent: simpler, but less responsive
- 2016 blocks is good compromise
Retarget Impact
If computational power increases:
- Blocks are mined faster
- Retarget increases difficulty
- Block time returns to ~10 minutes
- Reward continues to be ~6.25 BTC (current)
If computational power decreases:
- Blocks are mined slower
- Retarget decreases difficulty
- Block time returns to ~10 minutes
- Network continues functioning
Result:
- Block time remains stable (~10 minutes)
- Independent of total computational power
- Predictable Bitcoin emission
- Stable and reliable network
Competition: The Miner Race
How Competition Works
All miners compete simultaneously:
- All work on same block
- All try to find valid hash first
- First to find: wins reward
- Others: lose work and start new block
Analogy:
- Like race where everyone runs simultaneously
- First to cross line: wins
- Others win nothing
- Everyone starts new race on next block
Competitive Advantages
1. Computational Power (Hash Rate):
- Higher hash rate = more attempts per second
- More chances to find valid hash first
- Proportional to winning chance
Simplified formula:
Winning_Chance ≈ Your_Hash_Rate / Total_Network_Hash_Rate
Example:
- Total hash rate: 500 EH/s (exa-hash per second)
- Your hash rate: 10 PH/s (0.01 EH/s)
- Chance ≈ 0.01 / 500 = 0.00002 = 0.002%
- Very low for small individual miner
2. Energy Efficiency:
- Lower cost per hash = more profitable
- Can mine with lower profit margin
- Competitive advantage
3. Mining Pool:
- Miners join pools
- Share rewards proportionally
- Reduces variance, increases consistent income
4. Access to Cheap Electricity:
- Electricity is largest cost
- Access to cheap energy = competitive advantage
- Geographic location matters
Probability and Expectation
Probability of mining block:
For individual miner:
Probability = Your_Hash_Rate / Total_Hash_Rate × Current_Difficulty
Simplified:
Probability ≈ Your_Hash_Rate / Total_Hash_Rate
Expected time:
Expected_Blocks = Total_Hash_Rate / Your_Hash_Rate
Expected_Minutes = Expected_Blocks × 10
Example:
- Total hash rate: 500 EH/s
- Your hash rate: 100 TH/s (0.0001 EH/s)
- Expected blocks = 500 / 0.0001 = 5,000,000 blocks
- Expected time ≈ 5,000,000 × 10 minutes ≈ 95 years!
- That's why pools are necessary for small miners
Mining Pools
How pools work:
1. Pool coordinates miners:
- Pool distributes work among miners
- Miners try to find valid hash
- If any miner finds, pool wins
2. Shared reward:
- Block reward is divided
- Based on hash rate contribution
- Miners receive proportionally
Simplified formula:
Your_Reward = Block_Reward × (Your_Hash_Rate / Pool_Hash_Rate)
3. Advantages:
- ✅ More consistent income (less variance)
- ✅ Regular payments even without mining block alone
- ✅ Access to mining for small miners
Disadvantages:
- ❌ Lower reward (divided)
- ❌ Pool dependence
- ❌ Potential centralization
Formulas and Calculations
Hash Rate and Difficulty
Relationship between hash rate and difficulty:
Hash rate needed for average time T:
Hash_Rate_Needed = Difficulty × 2^32 / Target_Time
For 10 minute time (600 seconds):
Hash_Rate_Needed = Difficulty × 2^32 / 600
Example with current difficulty:
- Difficulty ≈ 80 trillion (approximately)
- Hash rate needed ≈ 80 trillion × 4.3 billion / 600
- Hash rate needed ≈ 570 EH/s
- Approximately matches real network hash rate!
Success Probability
Probability of finding valid hash in 1 attempt:
P = Target / 2^256
Simplified:
P ≈ 1 / (2^256 / Target)
P ≈ 1 / (Difficulty × 2^32)
Expected number of attempts:
Expected_Attempts = 1 / P
Expected_Attempts ≈ Difficulty × 2^32
Example:
- Difficulty ≈ 80 trillion
- Expected attempts ≈ 80 trillion × 4.3 billion
- Expected attempts ≈ 3.4 × 10^23 attempts!
- That's why needs lots of computational power
Profitability Calculation
Expected revenue:
Expected_Revenue = (Your_Hash_Rate / Total_Hash_Rate) × Block_Reward × Blocks_Per_Hour
Blocks per hour:
Blocks_Per_Hour = 60 minutes / Average_Block_Time
Blocks_Per_Hour ≈ 60 / 10 = 6 blocks/hour
Cost:
Cost = Hash_Rate × Energy_Consumption × Electricity_Price
Profit:
Profit = Expected_Revenue - Cost
Simplified example:
- Hash rate: 100 TH/s
- Total hash rate: 500 EH/s
- Reward: 6.25 BTC + 1 BTC fees = 7.25 BTC
- Blocks/hour: 6
- Revenue/hour ≈ (0.0001 / 500) × 7.25 × 6 ≈ 0.0000087 BTC/hour
- Very low for small individual miner!
Complete Mining Process
Mining Cycle
1. Transaction Collection:
- Miner selects transactions from mempool
- Prioritizes by fees (fee rate)
- Fills block up to size limit
2. Header Creation:
- Builds header with selected transactions
- Calculates Merkle root
- Sets timestamp
- Starts with nonce = 0
3. Hash Loop:
While not found valid hash:
hash = SHA-256d(header)
If hash < Target:
Stop (valid block found)
Else:
Increment nonce
If nonce exhausts:
Change ExtraNonce
Reset nonce
4. Block Mining:
- When valid hash found
- Miner broadcasts block to network
- Other nodes validate
- If valid, block added to blockchain
5. Reward Earned:
- Miner receives block reward (coinbase)
- Miner receives transaction fees
- Total added to miner's wallet
Timestamp and Validation
Timestamp rules:
- Timestamp must be greater than median timestamp of last 11 blocks
- Timestamp can be up to 2 hours in future
- Nodes reject blocks with invalid timestamp
Why it matters:
- Prevents miners manipulating time
- Keeps blockchain chronological
- Used in retarget calculation
Validation formula:
Current_Timestamp > Median(Timestamps_last_11_blocks)
Current_Timestamp <= Current_Time + 2_hours
Efficiency and Optimizations
Hardware Optimizations
ASIC (Application-Specific Integrated Circuit):
- Hardware specialized for SHA-256
- Much more efficient than CPU/GPU
- Only viable hardware type today
- High cost, but maximum efficiency
Efficiency measured in:
- Hash per second (H/s, KH/s, MH/s, GH/s, TH/s, PH/s, EH/s)
- Watts per hash (W/H)
- Joules per hash (J/H)
Comparison:
- CPU: ~10 MH/s, ~100 W → 10 W/MH
- GPU: ~100 MH/s, ~300 W → 3 W/MH
- ASIC: ~100 TH/s, ~3000 W → 0.03 W/TH
- ASIC is ~100,000x more efficient!
Software Optimizations
1. Hash Optimization:
- Optimized SHA-256 implementation
- Use of specialized instructions
- Processing pipeline
2. Transaction Optimization:
- Intelligent transaction selection
- Maximize fees per byte
- Optimize Merkle tree
3. Pool Strategies:
- Efficient coordination
- Overhead reduction
- Payment optimization
Statistics and Metrics
Important Metrics
Hash Rate:
- Total network computational power
- Measured in hash per second
- Current: ~500-600 EH/s (exa-hash per second)
Difficulty:
- Number representing mining difficulty
- Adjusts every 2016 blocks
- Current: ~80 trillion (approximately)
Block Time:
- Average time between blocks
- Target: 10 minutes
- Actual: usually close to 10 minutes
Block Reward:
- New Bitcoin created per block
- Currently: 6.25 BTC
- Halves every halving (~4 years)
Transaction Fees:
- Fees paid for included transactions
- Vary with congestion
- Added to block reward
Historical Trends
Hash rate evolution:
- 2009: ~MH/s (mega-hash)
- 2013: ~TH/s (tera-hash)
- 2017: ~EH/s (exa-hash)
- 2024: ~500-600 EH/s
- Exponential growth
Difficulty evolution:
- Follows hash rate
- Increases with computational power
- Keeps block time stable
Mining cost:
- Increases with difficulty
- Depends on electricity price
- Varies geographically
Frequently Asked Questions
Why does difficulty adjust?
To keep block time at ~10 minutes. If computational power increases, blocks would be too fast without adjustment. Difficulty compensates to keep time stable.
What happens if all miners stop?
If hash rate drops drastically, difficulty adjusts down at next retarget (after 2016 blocks). Blocks would continue to be mined, just slower temporarily.
Can I mine with CPU or GPU?
Technically yes, but not viable. ASICs are ~100,000x more efficient. You would spend more on electricity than earn in rewards.
How long does it take to mine a block?
Expected time depends on your hash rate relative to network. For small individual miner, can take years or decades. That's why pools are used.
What happens if two miners mine block simultaneously?
Both blocks are valid. Network accepts both temporarily. Next block decides which chain continues (usually longest or with most accumulated work).
Can nonce exhaust?
Yes, but not a problem. When nonce exhausts (after 2^32 attempts), miner changes ExtraNonce in coinbase transaction and continues. Practically unlimited.
Conclusion
Bitcoin mining is complex technical process involving mathematics, competition, and dynamic adjustment. Understanding difficulty, nonce, retarget, and competition is fundamental to understand how Bitcoin maintains security and stability.
The main points you need to understand are:
- Difficulty adjusts dynamically - Keeps block time at ~10 minutes regardless of computational power
- Nonce is main variable field - Miners vary nonce to try different hashes
- Retarget happens every 2016 blocks - Adjusts difficulty based on actual time vs target time
- Competition is constant - All miners compete to find valid hash first
- Hash rate determines probability - Higher hash rate = better chance of winning reward
- Formulas govern everything - Mathematics behind ensures stable and predictable operation
Mining is process that keeps Bitcoin secure and functioning. Through dynamic difficulty, competition between miners, and constant adjustment, Bitcoin maintains stable block time, predictable emission, and maximum security.
The formulas and calculations behind mining may seem complex, but they are fundamental to understand how Bitcoin works. Difficulty, nonce, retarget, and competition work together to create robust, stable, and secure system.
If you want to deeply understand how Bitcoin works, understanding mining at advanced level is essential. It's technical knowledge that opens doors to understand network security, mining economics, and Bitcoin protocol evolution.