Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: gigabytecoin on March 16, 2011, 01:16:23 AM



Title: Pseudo Code For Generating Bitcoins?
Post by: gigabytecoin on March 16, 2011, 01:16:23 AM
I am interested in trying to write my own open sourced miner. But have no idea where to start.

I have read through the forum quite a bit and asked a similar question before, but to no avail.

Can anybody provide some pseudo code for generating bitcoins?

I assume it goes something like...

Code:
$hashed_string_to_crack = grab_new_hash();
while($hashed_string_unknown === true) {
  //try to crack hash?
 
  if($hash_cracked) {
    $hashed_string_unkown = false;
  }
}
submit_cracked_hash_to_bitcoin_network($hash_cracked);
repeat;

Or am I better off just to examine the poclbm source code?


Title: Re: Pseudo Code For Generating Bitcoins?
Post by: grondilu on March 16, 2011, 01:51:11 AM
I think you could use the code I wrote in my thread about a full shell script implementation of bitcoin:

Code:
bigEndianHex2littleEndianHex() {
local s=''
while read -n 2 char
do s=$char$s
done
echo $s
}

bitcoinHash() {
bigEndianHex2littleEndianHex |
xxd -p -r |
openssl dgst -sha256 -binary |
openssl dgst -sha256 -binary |
xxd -p -c 80 |
bigEndianHex2littleEndianHex
}

And then with the following data from the current block:

ver=1
prev_block=0000000000000000000000000000000000000000000000000000000000000000
mrkl_root=4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
bits=486604799

(this is the genesis "data")

You can create a next block by searching a nonce like this:

Code:
nonce=0

while
        time=$(date +%s)
        printf "%08x%08x%08x%064s%064s%08x" $nonce $bits $time $mrkl_root $prev_block $ver |
        bitcoinHash |
        grep "some test I don't know exactly"
do nonce=$(bc <<< "nonce + 1")
done

of course a lot is missing, especially the network part.



Title: Re: Pseudo Code For Generating Bitcoins?
Post by: gigabytecoin on March 16, 2011, 02:22:53 AM
Thanks for that grondilu, do you have the link to your original post on hand?

Is the "networking part" that's missing simply grabbing the next block "todo" from the network?

That is the part I am having trouble with mentally, is where/whom do I contact to download the block chain?


Title: Re: Pseudo Code For Generating Bitcoins?
Post by: Cryptoman on March 16, 2011, 02:25:09 AM
A full shell script implementation of bitcoin: http://bitcointalk.org/index.php?topic=2461.0


Title: Re: Pseudo Code For Generating Bitcoins?
Post by: gigabytecoin on March 16, 2011, 04:24:37 AM
Thanks Cryptoman! You've been quite a help to me today  :o


Title: Re: Pseudo Code For Generating Bitcoins?
Post by: Viceroy on April 25, 2013, 06:55:29 PM
Same question for litecoin.  

Has Anyone written pseudocode for mining litecoin?


Or willing to sell me an RTL for the same?    :-)

Here:
https://bitcointalk.org/index.php?topic=187667.0 (https://bitcointalk.org/index.php?topic=187667.0)


Title: Re: Pseudo Code For Generating Bitcoins?
Post by: Remember remember the 5th of November on April 25, 2013, 07:04:50 PM
Hashing for blocks is done in two ways. Either learning the SHA256 algorithm and make use of the midstate to speed up computation, or something along those lines OR read the article on the block hashing algorithm which is basically doublesha256 of the block header, nothing too complex.

You can see the relevant code here from the genesis block generator I wrote https://bitcointalk.org/index.php?topic=181981.0