Bitcoin Forum
May 08, 2024, 09:18:21 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Problems verifying merkle root from two child nodes  (Read 603 times)
tyrick (OP)
Newbie
*
Offline Offline

Activity: 9
Merit: 0


View Profile WWW
December 03, 2013, 07:02:15 PM
Last edit: December 03, 2013, 08:42:32 PM by tyrick
 #1

I am having issues with verifying the mrkl_root with the two children nodes.

Let's use the following block for a reference: http://blockexplorer.com/rawblock/00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d

According to my understanding (correct if wrong), I need to perform a byteSwap on each child, perform a hexToAscii, concatenate, then perform a double sha256. I am doing this in JAVA, an suspect that my hexToAscii method is off. I was hoping I could give some results to see if someone can verify where the problem is.

Code:
String childNode = "0d0eb1b4c4b49fd27d100e9cce555d4110594661b1b8ac05a4b8879c84959bd4";
byteSwap(childNode); //d49b95849c87b8a405acb8b161465910415d55ce9c0e107dd29fb4c4b4b10e0d
hexToAscii(byteSwap(childNode)); //Ô¸¤¬¸±aFYA]UÎ}Ò´Ä´±

And just to verify that my hexToAscii result is correct, can someone check the sha256 hash of the result? I feel that there are encoding issues with my implementation

Code:
sha256(hexToAscii(byteSwap(childNode)));
//e799dc13aaeaa1c2797596f5d4bf6c408b97a8595bf32c27c9186125454560d9

Would appreciate any advice if something seems off! Thanks!

I am convinced that there is a JAVA issue with my hexToAscii implementation.

Code:
	public String hexToBin(String hex) {
 StringBuilder sb = new StringBuilder();
 StringBuilder temp = new StringBuilder();
 //49204c6f7665204a617661 split into two characters 49, 20, 4c...
 for(int i = 0; i < hex.length() - 1; i += 2){
     //grab the hex in pairs
     String output = hex.substring(i, (i + 2));
     //convert hex to decimal
     int decimal = Integer.parseInt(output, 16);
     //convert the decimal to character
      sb.append((char)decimal);
     temp.append(decimal);
 }
 return sb.toString();
}


Some expertise would be appreciated here =/  Thanks

1715159901
Hero Member
*
Offline Offline

Posts: 1715159901

View Profile Personal Message (Offline)

Ignore
1715159901
Reply with quote  #2

1715159901
Report to moderator
Even if you use Bitcoin through Tor, the way transactions are handled by the network makes anonymity difficult to achieve. Do not expect your transactions to be anonymous unless you really know what you're doing.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715159901
Hero Member
*
Offline Offline

Posts: 1715159901

View Profile Personal Message (Offline)

Ignore
1715159901
Reply with quote  #2

1715159901
Report to moderator
1715159901
Hero Member
*
Offline Offline

Posts: 1715159901

View Profile Personal Message (Offline)

Ignore
1715159901
Reply with quote  #2

1715159901
Report to moderator
GoldenWings91
Full Member
***
Offline Offline

Activity: 141
Merit: 100


View Profile
December 03, 2013, 07:38:25 PM
Last edit: December 03, 2013, 08:11:47 PM by GoldenWings91
 #2

For reference

"0d0eb1b4c4b49fd27d100e9cce555d4110594661b1b8ac05a4b8879c84959bd4"

Endian swapped and double sha256:
"2b9f3fd09cf042ce9eb019b72ac356db63121c1f56ad6af6cbd401bdf0d51f16"

EDIT:

Shouldn't this be 1?
String output = hex.substring(i, (i + 2));



Support The Bitcoin Network By Running A Full Node
Node Stats     GPG Key-ID: 0x445DF2D8     Monetary Freedom Is A Basic Human Right
tyrick (OP)
Newbie
*
Offline Offline

Activity: 9
Merit: 0


View Profile WWW
December 03, 2013, 08:15:12 PM
 #3

Thanks for the reply! Actually, substring(i, (i + 2)) isn't inclusive of i + 2, so the result is just the two consecutive chars.

Also, you said

Quote
Endian swapped and double sha256:
"2b9f3fd09cf042ce9eb019b72ac356db63121c1f56ad6af6cbd401bdf0d51f16"

Are you sure that is all you did? I got the following:

Code:
String hash = "0d0eb1b4c4b49fd27d100e9cce555d4110594661b1b8ac05a4b8879c84959bd4"
flipHex(hash); //d49b95849c87b8a405acb8b161465910415d55ce9c0e107dd29fb4c4b4b10e0d
dsha256(flipHex(hash)); //fa17aefd43501ded4b8dba60fcd1f3f7254eeb0023894a543d0ca800998591bd

Completely different.  Now I am questioning my dsha256 method...
GoldenWings91
Full Member
***
Offline Offline

Activity: 141
Merit: 100


View Profile
December 03, 2013, 08:23:23 PM
 #4

Quote
Thanks for the reply! Actually, substring(i, (i + 2)) isn't inclusive of i + 2, so the result is just the two consecutive chars.

I'm no Java developer so that was just an incorrect guess  Tongue

Quote
Are you sure that is all you did? I got the following:

I'm 100% sure "2b9f3fd09cf042ce9eb019b72ac356db63121c1f56ad6af6cbd401bdf0d51f16" (little endian) is the correct double sha256 hash of "d49b95849c87b8a405acb8b161465910415d55ce9c0e107dd29fb4c4b4b10e0d" (little endian)

EDIT:
Code:
std::cout << functions::double_sha256( functions::hexTostring( functions::endian_swap( "0d0eb1b4c4b49fd27d100e9cce555d4110594661b1b8ac05a4b8879c84959bd4" ) ) ) << std::endl;


Support The Bitcoin Network By Running A Full Node
Node Stats     GPG Key-ID: 0x445DF2D8     Monetary Freedom Is A Basic Human Right
danneu
Newbie
*
Offline Offline

Activity: 32
Merit: 0



View Profile
December 03, 2013, 08:44:50 PM
 #5

Code:
(-> "0d0eb1b4c4b49fd27d100e9cce555d4110594661b1b8ac05a4b8879c84959bd4"
    hex->bytes
    reverse-bytes
    double-sha256
    bytes->hex)

;; "2b9f3fd09cf042ce9eb019b72ac356db63121c1f56ad6af6cbd401bdf0d51f16"

Here're my Java crypto:

Code:
(:import
 [java.security MessageDigest])

(defn sha256 [data]
  (-> (MessageDigest/getInstance "SHA-256" "BC")
      (.digest data)))

(defn double-sha256 [data]
  (sha256 (sha256 data)))

Are you sure you're working with the actual bytes that the hex represents and not, for example, the bytes of the chars that make up the hex string?
tyrick (OP)
Newbie
*
Offline Offline

Activity: 9
Merit: 0


View Profile WWW
December 03, 2013, 09:23:52 PM
Last edit: December 03, 2013, 09:42:02 PM by tyrick
 #6

AHHH THANKSSSSS.

danneu, that was exactly my problem!

wings, thanks for checking the values for 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!