Bitcoin Forum
May 08, 2024, 09:57:36 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Looking for expert in Bitcoin hashing algorithms in PHP, C, Python, Javascript  (Read 2895 times)
darius2020 (OP)
Member
**
Offline Offline

Activity: 72
Merit: 10


View Profile
January 20, 2014, 04:27:54 PM
 #1

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
1715162256
Hero Member
*
Offline Offline

Posts: 1715162256

View Profile Personal Message (Offline)

Ignore
1715162256
Reply with quote  #2

1715162256
Report to moderator
1715162256
Hero Member
*
Offline Offline

Posts: 1715162256

View Profile Personal Message (Offline)

Ignore
1715162256
Reply with quote  #2

1715162256
Report to moderator
BitcoinCleanup.com: Learn why Bitcoin isn't bad for the environment
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715162256
Hero Member
*
Offline Offline

Posts: 1715162256

View Profile Personal Message (Offline)

Ignore
1715162256
Reply with quote  #2

1715162256
Report to moderator
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
January 20, 2014, 07:23:02 PM
 #2

Why are you looking for such an expert?

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
darius2020 (OP)
Member
**
Offline Offline

Activity: 72
Merit: 10


View Profile
January 21, 2014, 03:22:49 PM
 #3

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.


piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
January 21, 2014, 03:40:29 PM
 #4

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

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
darius2020 (OP)
Member
**
Offline Offline

Activity: 72
Merit: 10


View Profile
January 21, 2014, 04:02:49 PM
 #5

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.
piotr_n
Legendary
*
Offline Offline

Activity: 2053
Merit: 1354


aka tonikt


View Profile WWW
January 21, 2014, 04:28:33 PM
 #6

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.

Check out gocoin - my original project of full bitcoin node & cold wallet written in Go.
PGP fingerprint: AB9E A551 E262 A87A 13BB  9059 1BE7 B545 CDF3 FD0E
darius2020 (OP)
Member
**
Offline Offline

Activity: 72
Merit: 10


View Profile
January 22, 2014, 11:36:10 AM
Last edit: January 22, 2014, 11:52:57 AM by darius2020
 #7

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
<!DOCTYPE html>
<html>
<body>

<p id="demo">Click the button to display the formatted number.</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction()
{
var num = 1305998791;
var x = document.getElementById("demo");
x.innerHTML=num.toString(16);
}
</script>

</body>
</html>

if I am wrong, please correct me
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!