user1114: 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 :)]

user659: Read the answers to this question 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 to get an idea of how mining works.

user323: The simplest miner would work like this:

Request a new Getwork from a given pool/bitcoind with specified credentials
For nonce=0;nonce<0xFFFFFFFF;nonce++

Set Getwork Data nonce
SHA-256 hash the Getwork Data (block header)
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

GOTO: 1

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

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, makes you calculate less stale blocks.
Rollntime, minimizes communication with the pool.


1114: <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>
 659: <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>
 323: <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>
