Author

Topic: Looking for expert in Bitcoin hashing algorithms in PHP, C, Python, Javascript (Read 2921 times)

member
Activity: 72
Merit: 10
Thanks


you said:

Quote
SwapOrder operates on a set of bytes - just reverses their order.
LittleEndian takes a 32-bit integer and outputs 4 bytes in little-endian order.


In case of Bitcoin block header SwapOrder operates on a string of hex numbers, reversing their order
2 bytes-wise ( hex pairs)

example below
Quote
$rootHash = SwapOrder('2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');
    
The problem is with decimal number converted to little Endian
since
decimal value is converted to    Hexadecimal value
1222                                          4C6
Epoch Unix time (decimal)
1305998791                                 4DD7F5C7 (even number of hex symbols

Now I need to convert hexadecimal value to a string of hex digits ( 0,1,2.... A,C...F) -  hex (base 16) representation
and append "0" to "4C6" to get even number of hex digits and operate SwapOrder 2 bytes - wise


$time = littleEndian(1305998791);

from
https://en.bitcoin.it/wiki/Block_hashing_algorithm
Quote
The header is built from the six fields described above, concatenated together as little-endian values in hex notation:


(1305998791)10 = (4DD7F5C7)16

So in theory and practice, I am safe converting today Unix epoch time (decimal number) to hexadecimal value with even number of hex digits -  hex (base 16) representation

Hex2String conversion is not the right tool, converting
4DD7F5C7
to
M×õÇ  (ASCII string)

so the following code looks to work fine converting Unix epoch time value = 1305998791 to hex (base 16) string followed by SwapOrder(num.toString(16))

Quote




Click the button to display the formatted number.










if I am wrong, please correct me
legendary
Activity: 2053
Merit: 1356
aka tonikt
I have implemented SwapOrder (BytesSwap) in Javascript
Hex string 2 bits string
but I am not sure what makes the difference between  SwapOrder and LittleEndian
SwapOrder operates on a set of bytes - just reverses their order.
LittleEndian takes a 32-bit integer and outputs 4 bytes in little-endian order.

As for the rest of your questions - sorry, I don't have that much time Smiley
Maybe someone else will help you.
Though I can tell you that the Bitcoin header (the thing that you get to hash with sha256) is just 80 bytes in such a format:
https://en.bitcoin.it/wiki/Protocol_specification#Block_Headers
Build the 80 bytes with the values you need, hash them, then hash the result - and that's it.
member
Activity: 72
Merit: 10
Thank you.

Quote
SwapOrder is just converting big to little endian and you don't use it in the python code, because you already specify it as converted to little endian.

The issue is PHP code from example comes either with SwapOrder and LittleEndian

Quote
$rootHash = SwapOrder('2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');
    $time = littleEndian(1305998791);

I have implemented SwapOrder (BytesSwap) in Javascript
Hex string 2 bits string
but I am not sure what makes the difference between  SwapOrder and LittleEndian

since input data (6 parameters) for each example in Python, PHP is parsed from block header the other way
I don't need any parsing in example in plain C

http://pastebin.com/bW3fQA2a

Quote

You are exactly right, there is a number of sha256 implementations in Javascript
but what I was looking for was  Bitcoin block header hash implementation in Javascript ( still missing)

I need the way (API, database, datafile (csv) as 6 input parameters to run Block header hash off-line
to verify every block (nonce) from the past.

How to download and parse Bitcoin block header 6 input parameters and save to file as csv , any source, web link to such file ?

Ok, Javascript is slow but is nice for education or presentation on how Bitcoin block header hash works
step-by-step.

I tried to download Bitcoin block header 6 input parameters from Blockchain.info
unfortunately format is not smart ( a lot of parsing required)
 and time is not in Unix epoch format

btw
I don't see block header parsing (string made of 6 input parameters + BytesSwap, LittleEndian in plain C example from
http://pastebin.com/bW3fQA2a


I am really grateful for your help.
legendary
Activity: 2053
Merit: 1356
aka tonikt
You don't really need an expert - these is some pretty basic stuff.


Looking for working Bitcoin block header hashing algorithm in Javascript

Try google: https://www.google.com/search?q=sha256+javascript

"Bitcoin block header hashing algorithm" is just SHA256 on the 80 bytes of data and then SHA256 again on the 32-bytes output from the first hashing.


and exaplanation about LittleEndian and ByteSwap implementation in PHP

Quote
$version = littleEndian(1);
    $prevBlockHash = SwapOrder('00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81');
    $rootHash = SwapOrder('2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');
    $time = littleEndian(1305998791);
    $bits =littleEndian( 440711666);
    $nonce = littleEndian(2504433986);
   

vs. no LittleEndian implementation in Python

Quote
>>> header_hex = ("01000000" +
    "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
    "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
    "c7f5d74d" +
    "f2b9441a" +
     "42a14695")
  >>> header_bin = header_hex.decode('hex')

from
http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/

Quote
In PHP you might have to convert the endianness of a number. PHP does not provide any function for this even though it has function for almost everything.

All the hashes in bitcoin protocol are stored as little endian.
SwapOrder is just converting big to little endian and you don't use it in the python code, because you already specify it as converted to little endian.
In other words:
Code:
Big endian:    00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81
Little endian: 81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000
member
Activity: 72
Merit: 10
Looking for working Bitcoin block header hashing algorithm in Javascript

and exaplanation about LittleEndian and ByteSwap implementation in PHP

Quote
$version = littleEndian(1);
    $prevBlockHash = SwapOrder('00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81');
    $rootHash = SwapOrder('2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3');
    $time = littleEndian(1305998791);
    $bits =littleEndian( 440711666);
    $nonce = littleEndian(2504433986);
   

vs. no LittleEndian implementation in Python

Quote
>>> header_hex = ("01000000" +
    "81cd02ab7e569e8bcd9317e2fe99f2de44d49ab2b8851ba4a308000000000000" +
    "e320b6c2fffc8d750423db8b1eb942ae710e951ed797f7affc8892b0f1fc122b" +
    "c7f5d74d" +
    "f2b9441a" +
     "42a14695")
  >>> header_bin = header_hex.decode('hex')

from
http://shiplu.mokadd.im/95/convert-little-endian-to-big-endian-in-php-or-vice-versa/

Quote
In PHP you might have to convert the endianness of a number. PHP does not provide any function for this even though it has function for almost everything.


legendary
Activity: 2053
Merit: 1356
aka tonikt
Why are you looking for such an expert?
member
Activity: 72
Merit: 10
Hi,

I am looking for expert in Bitcoin block header hashing algorithms in PHP, C, Python, Javascript
to discuss Bitcoin hashing algorithms from bitcoin.it in PHP, Python, plain C,
developing Javascript
as below

https://en.bitcoin.it/wiki/Block_hashing_algorithm
Jump to: