<p>Read the answers to <a href="https://bitcoin.stackexchange.com/questions/148/what-exactly-is-mining">this question</a> and if you have any specific questions about the mining algorithm perhaps you can edit your question to ask them.</p>

<p>If all you want to do is run a mining program, there are lots available.  You can study the source and see what they're doing.</p>

<p>The official bitcoin client itself has a basic CPU miner built in.  You can <a href="https://github.com/bitcoin/bitcoin/blob/142e604184e3ab6dcbe02cebcbe08e5623182b81/src/main.cpp#L3352" rel="nofollow noreferrer">examine the code</a> to get an idea of how mining works.</p>

CC BY-SA 3.0
2012-03-01T10:53:25.593Z
2012-03-01T10:53:25.593Z
2017-04-13T12:47:33.283Z
Chris Moore
<p>The simplest miner would work like this:</p>

<ol>
<li>Request a new <a href="https://en.bitcoin.it/wiki/Getwork">Getwork</a> from a given pool/bitcoind with specified credentials</li>
<li>For nonce=0;nonce&lt;0xFFFFFFFF;nonce++
<ul>
<li>Set Getwork Data nonce</li>
<li>SHA-256 hash the Getwork Data (<a href="https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers">block header</a>)</li>
<li>Check if hash result is smaller than Getwork Target, if so, submit a share</li>
<li>If more than 1 second passed since you received Getwork, stop the loop</li>
</ul></li>
<li>GOTO: 1</li>
</ol>

<p>Now, if you want something that could rise up to par with currently used miners, you need to consider:</p>

<ul>
<li><a href="http://en.wikipedia.org/wiki/OpenCL">OpenCL</a> calculations, without this you will never get the high hashrates of graphic cards.</li>
<li>Midstate, or in other words hashing optimization. First half of Getwork Data doesn't change, so no point in calculating midstate for each hash.</li>
<li><a href="https://en.bitcoin.it/wiki/Getwork#longpoll">Longpolling</a>, makes you calculate less stale blocks.</li>
<li><a href="https://en.bitcoin.it/wiki/Getwork#rollntime">Rollntime</a>, minimizes communication with the pool.</li>
</ul>

CC BY-SA 3.0
2013-12-27T15:20:45.467Z
Could you please explain why we need to stop the loop if it took more than 1 second?
Rasoul
CC BY-SA 3.0
2013-12-28T07:49:09.220Z
@Rasoul The basic getwork requests (in bitcoind) time out after 1 second as far as I remember. This is why we have to stop the loop and get new work every second.
ThePiachu
CC BY-SA 3.0
2012-03-01T20:46:38.343Z
2012-03-01T20:46:38.343Z
ThePiachu
<p>I'm trying to figure out how does simple Bitcoin mining algorithm works in plain simple c or c#  or some pseudo language. I've found an example at <a href="http://pastebin.com/EXDsRbYH">http://pastebin.com/EXDsRbYH</a>, but unfortunately It isn't clear what it does. I was unable to run it.</p>

<p>Suppose I have only one input: a Bitcoin wallet "abc..." which I would like to be used for the Bitcoins to be mined. I need simple to understand algorithm that will do the bitcoin mining on one machine with one thread on one cpu [I know it will take ages to complete :)]</p>

<algorithms>
Simple bitcoin mining algorithm
Lu4
# Simple bitcoin mining algorithm

(Asked by: username_0 on 2012-03-01)

I'm trying to figure out how does simple Bitcoin mining algorithm works in plain simple c or c# or some pseudo language. I've found an example at <http://pastebin.com/EXDsRbYH>, but unfortunately It isn't clear what it does. I was unable to run it.

Suppose I have only one input: a Bitcoin wallet "abc..." which I would like to be used for the Bitcoins to be mined. I need simple to understand algorithm that will do the bitcoin mining on one machine with one thread on one cpu [I know it will take ages to complete :)]

**Tags:** algorithms


---

## 2 answers

### Answer by username_2 on 2012-03-01. Score: 20 | Accepted answer

The simplest miner would work like this:

1. Request a new [Getwork](https://en.bitcoin.it/wiki/Getwork) from a given pool/bitcoind with specified credentials
2. For nonce=0;nonce<0xFFFFFFFF;nonce++
   * Set Getwork Data nonce
   * SHA-256 hash the Getwork Data ([block header](https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers))
   * Check if hash result is smaller than Getwork Target, if so, submit a share
   * If more than 1 second passed since you received Getwork, stop the loop
3. GOTO: 1

Now, if you want something that could rise up to par with currently used miners, you need to consider:

* [OpenCL](http://en.wikipedia.org/wiki/OpenCL) calculations, without this you will never get the high hashrates of graphic cards.
* Midstate, or in other words hashing optimization. First half of Getwork Data doesn't change, so no point in calculating midstate for each hash.
* [Longpolling](https://en.bitcoin.it/wiki/Getwork#longpoll), makes you calculate less stale blocks.
* [Rollntime](https://en.bitcoin.it/wiki/Getwork#rollntime), minimizes communication with the pool.

> **Comments:**
> **username_3**: Could you please explain why we need to stop the loop if it took more than 1 second?  
> **username_2**: @username_3 The basic getwork requests (in bitcoind) time out after 1 second as far as I remember. This is why we have to stop the loop and get new work every second.  

---

### Answer by username_1 on 2012-03-01. Score: 2

Read the answers to [this question](https://bitcoin.stackexchange.com/questions/148/what-exactly-is-mining) and if you have any specific questions about the mining algorithm perhaps you can edit your question to ask them.

If all you want to do is run a mining program, there are lots available. You can study the source and see what they're doing.

The official bitcoin client itself has a basic CPU miner built in. You can [examine the code](https://github.com/bitcoin/bitcoin/blob/142e604184e3ab6dcbe02cebcbe08e5623182b81/src/main.cpp#L3352) to get an idea of how mining works.


---

