> For the complete documentation index, see [llms.txt](https://www.notbank.com/learn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.notbank.com/learn/academy/en/bitcoin/mining.md).

# Mining

Mining, Proof of Work and Difficulty Adjustment

***

> “Proof of Work is the soul of Bitcoin: it turns energy into security.”\
> — *Cypherpunk principle*

***

## 1. Introduction

The **mining** is the mechanism that secures the Bitcoin network by enabling:

* Issuance of new bitcoins
* Protection against attacks
* Temporal ordering of transactions
* Probabilistic consensus
* Cumulative immutability
* Defense against tampering

The tool that makes this possible is **Proof of Work (PoW)**.

This chapter explains:

* How PoW works
* Why it makes the network secure
* The critical role of difficulty adjustment
* Economic incentives of miners
* Mathematical security models
* Potential attacks (and why they are impractical)

***

## 2. What is mining in Bitcoin?

Mining is the process by which specialized nodes:

1. group valid transactions,
2. compete to find a valid hash for the header,
3. propagate the winning block to the network,
4. receive reward (subsidy + fees).

***

### 2.1. Formal definition of PoW

A miner must find:

$$H(\text{header}) < \text{Target}$$

where:

* $$H = SHA256(SHA256(x))$$
* *Target* is a number that defines the difficulty
* the probability of success is inversely proportional to the `hashrate` global

***

## 3. Structure of the header used in PoW

The **80-byte header** is the only data subjected to PoW:

| Field             | Bytes | Purpose                   |
| ----------------- | ----- | ------------------------- |
| version           | 4     | Consensus rules           |
| prev\_block\_hash | 32    | Chaining                  |
| merkle\_root      | 32    | Transactions              |
| timestamp         | 4     | Timestamp                 |
| bits              | 4     | Target compression        |
| nonce             | 4     | Value that is incremented |

***

## 4. The “nonce” and the search space

The miner tests billions of nonces per second.

The nonce is 32 bits → 4 billion possible values.\
But if it is exhausted, the miner modifies:

* extraNonce (in coinbase)
* merkle\_root
* timestamp

which creates a practically infinite space for PoW.

***

## 5. Difficulty: heart of economic security

Bitcoin adjusts the difficulty every **2016 blocks** (\~2 weeks) to maintain an average interval of:

$$10 \text{ minutos por bloque}$$

If blocks are mined faster:

* hashrate ↑
* difficulty ↑

If they are mined slower:

* hashrate ↓
* difficulty ↓

***

### 5.1. Difficulty adjustment formula

The new difficulty is calculated as:

$$D\_{new} = D\_{old} \cdot \frac{t\_{real}}{t\_{esperado}}$$

where:

* $$t\_{esperado} = 2016 \cdot 600 = 1,209,600 s$$ (2 weeks)
* $$t\_{real}$$ = actual time it took for the last 2016 blocks

Limits:

* maximum adjustment ±4×
* prevents exaggerated manipulation

***

## 6. The target: numerical threshold

Difficulty is an abstract value.\
The target is the concrete number the hash must be less than.

$$\text{Target} = \frac{2^{224} - 1}{Dificultad}$$

Simplified example:

HASH (in hex): 0000000af3c1... TARGET: 0000000fffff...

Since HASH < TARGET → valid block

***

## 7. Pseudocode example of PoW

```python
while True:
    header = build_header(block, nonce)
    hash_val = sha256(sha256(header))
    if hash_val < target:
        return header
    nonce += 1
```

## 8. Why is PoW secure?

Because it physically costs energy to generate hashes, while verifying them is trivial:

<p align="center"><span class="math">Costo \ de \ minar \gg Costo \ de \ verificar</span></p>

Costs for the attacker:

* specialized hardware (ASICs)
* electricity
* infrastructure
* lost economic opportunity
* financial risk
* social coordination

Costs for the verifier:

* a single hash per node
* less than 1 ms per block

### 8.1. Accumulated security

PoW creates a “cryptographic wall”:

<p align="center"><span class="math">Seguridad \ de \ la \ cadena \propto PoW \ acumulado</span></p>

No matter how many nodes exist: **security comes from the total work buried under each block**.

## 9. Economic incentives: game theory applied

We could define mining as a strategic interaction between economic agents.

### 9.1. Block reward

It is composed of:

<p align="center"><span class="math">Recompensa = Subsidio + Fees</span></p>

Subsidy:

<p align="center"><span class="math">50 \times 2^{-n}</span></p>

where $$n = número \ de \ halvings$$.

### 9.2. Aligned incentives

* Miners → maximize profits
* Nodes → verify rules
* Users → want security
* Developers → improve efficiency

Incentives are set up to favor:

* Honesty > Attack

### 9.3. Why attacking the network is irrational

To rewrite old blocks you must redo all the accumulated PoW competing against all honest miners at a cost higher than the benefit.

Even an attacker with 51%:

* cannot steal funds
* cannot modify rules
* cannot create inflation
* can only reverse their own payments

And at a ruinous cost.

## 10. Global hashrate: security metric

<p align="center"><span class="math">Hashrate \ global \thickapprox TH/s</span></p>

<p align="center">(terahashes/second)</p>

The higher it is, the more expensive it is to attack the network.

### 10.1. Conceptual example

If the network has:

<p align="center"><span class="math">300 EH/s</span></p>

<p align="center">(300 quintillion hashes/s)</p>

An actor would have to match that power **just to compete**.

## 11. Mining pools

Miners usually group into pools.

Reasons:

* reduce variance
* stable income
* share work

The pool:

Miners → send shares → Pool aggregates PoW → Pool produces block → Distributes reward

### 11.1. Does this increase centralization risk?

Yes, but not as much as it seems:

* changing pools is trivial
* pools do not control private keys
* they only coordinate work
* if they abuse, miners migrate

## 12. Types of hardware in the history of mining

| Era          | Hardware | Efficiency | Duration |
| ------------ | -------- | ---------- | -------- |
| 2009         | CPU      | Very low   | Months   |
| 2010         | GPU      | 50× CPU    | \~1 year |
| 2012         | FPGA     | 10× GPU    | Short    |
| 2013-present | ASIC     | 1,000× GPU | Dominant |

ASIC = `Application-Specific Integrated Circuit`

## 13. Mathematical models of block time

Mining follows a **Poisson process**.

Expected time per block:

<p align="center"><span class="math">E[T]= \dfrac{1}{λ}</span></p>

where:

<p align="center"><span class="math">λ =\dfrac{dificultad}{hashrate}</span></p>

Variability: some blocks take seconds, others hours. But the long-run mean converges to **\~10 minutes**.

## 14. Attacks on the PoW system

### 14.1. 51% attack

Allows:

* reversing own payments
* censoring transactions (temporarily)

Does not allow:

* creating bitcoins
* stealing funds
* breaking signatures
* changing consensus rules

### 14.2. Selfish mining

Theory: miner withholds blocks to gain advantage.

Reality:

* requires threshold > 33%
* not very profitable
* easy to detect
* partially solved with `BIP32`, P2P improvements and topologies

### 14.3. Timestamp attacks

Manipulate time to adjust difficulty. Mitigation:

* strict limits
* allowed window ± 2h
* nodes reject impossible timestamps

## 15. Why PoW is superior to PoS (according to original design)

Cypherpunk / engineering perspective:

* **Based on physics, not on wealth**
* **Objective cost of attack**
* **History protected with irreversible work**
* **Australian Fairness → everyone can compete**
* **Proven dynamics since 2009 without failures**

PoW is a mechanism **neutral, resilient, verifiable and politically independent**.

## 16. Chapter conclusion

Mining and Proof of Work are:

* The physical security layer of Bitcoin
* The source of its immutability
* The engine of its issuance
* The foundation of its consensus
* The economic barrier against attackers

PoW turns electricity into digital security. Difficulty adjustment turns fluctuations into stability. Incentives turn competition into collective protection.

> Mining is not “wasting energy”.\
> It is **spending energy to prevent fraud**, a military function without violence.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.notbank.com/learn/academy/en/bitcoin/mining.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
