When computing the hash, do you need anything after the number of difficulty bits?


I'm going to screw up a lot of the terminology here. I'm trying to understand the Bitcoin algortihm and wanted to use this JS miner -- https://github.com/howardchung/jsminer . Specifically, I'm trying to understand how to build the data that someone attempts to hash. They attempt to do this in the JS miner by the following

    var header = version + prevhash + merkle_root + ntime + nbits + '00000000' + '000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000';
    curr_block.data = header;

I'm following most of this, using the Bitcoin wiki as a reference, but what I can't figure out at all is what are these last two numerical constants -- '00000000' and the bigger one beginning with "0000008". I have seen these come up in other examples but I can't figure out why these are needed or if these are hard-coded values in the JS miner that shouldn't be there in real life. Any help is appreciated.


Posted 2018-03-12T23:20:58.020

Reputation: 395



The first 00000000 is for the nonce. The nonce is changed in order to get a different block header without reselecting transactions. It is what is used primarily for finding a valid block hash.

The long string of bytes beginning with 0000008 is SHA-256 padding. It is not actually part of the block header and whatever is doing the SHA-256 hashing should figure out and append this padding for you. It is part of the pre-processing step for SHA-256.

Andrew Chow

Posted 2018-03-12T23:20:58.020

Reputation: 50 267

Yeah I pulled this fragment out of the section of the code where they're doing that pre-processing. Since the string beginning with "0000008" is padding, can it be anything? Does it have to be that specific sequence? – Dave – 2018-03-13T00:45:07.613

Yes. The padding is fixed and must be the same for all blocks. It should really be calculated dynamically by the SHA-256 function instead of something that you are hard coding. – Andrew Chow – 2018-03-13T01:47:24.693

To clarify your "Yes", do you mean "Yes it can be anything" or "yes it must be that specific sequence"? – Dave – 2018-03-13T13:51:14.003

Yes, it must be that specific sequence. – Andrew Chow – 2018-03-13T14:38:15.033