0

I am using the merklelib library to generate a Merkle Tree from transactions. However, the Hashes turn out to be different even though I am using the same hashfunction.

I tried different styles of converting the string input, however the output is still different. I know that the output when using the MerkleTree function is wrong as the correct SHA256 hash of f.e 'a' == 'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb' and NOT '92535ffc19ffdd636384b9768137fafae6d776dce7316d0aba6b9b0dfba5ae68'.

Here is also a link to the merklelib repository: https://github.com/vpaliy/merklelib

1. Using the MerkleTree function:

from merklelib import MerkleTree, beautify
import hashlib
hashfunc = lambda x: hashlib.sha256(str(x).encode()).hexdigest()

mylist = ['a', 'b']

tree = MerkleTree(mylist, hashfunc)
beautify(tree)

Output:

439dc667cd474f56c40d47f90d2954194bb7884dec6190c489cc0b1cedcd7655
├── 92535ffc19ffdd636384b9768137fafae6d776dce7316d0aba6b9b0dfba5ae68
└── c8b1b17759d36012f990975f437dbd0f9791dd2aed7e0dc23ed90501ad4e113c

2. Using just the hashfunc function to get the correct hash:

print(hashfunc('a'))
print(hashfunc('b'))

Output:

ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
3e23e8160039594a33894f6564e1b1348bbd7a0088d42c4acb73eeaed59c009d
  • The tree isn't hashing your strings directly; there is a prefix byte added first, which differs between leaf and interior nodes of the tree (apparently this defends against a particular attack scenario). – jasonharper Oct 17 '22 at 13:51

0 Answers0