README.md

merkle-tree

This library implements a merkle-tree data structure, for representing a list of
hashed data as a binary tree of pairs of hashes converging to a single hash, the
merkle root. SHA3_256 is used as the hashing algorithm.

Description

A Merkle tree is a tamper-resistant data structure that allows a large amount of
data to be compressed into a single hash and can be queried for the presence
of specific elements in the data with a proof constructed in logarithmic space.

Construction

A Merkle tree is a binary tree of hashes, in which all the leaf nodes are the
individual data elements in the block. To construct a merkle tree, the initial
data elements are first hashed using the merkle tree hash function to generate
the leaf nodes of the tree. The resulting hashed data are subsequently hashed
together in pairs to create the parent nodes of the leaf nodes. This process
continues until it results in a single hash known as the merkle root.

--| Constructs a Merkle Tree from a list of ByteStringsmkMerkleTree:: [ByteString] ->MerkleTreeByteString--| Generates the hash of a piece of data existing as a leaf node in the TreemkLeafRootHash::ByteString->MerkleRootByteString

Merkle Inclusion Proof

Perhaps the most important reason to use a merkle tree to represent the block
data is the ability to construct a merkle proof (O(n)) that verifies
the inclusion of a transaction t in a specific block b (O(log(n))).

Merkle proofs are an important part in creating decentralized block chain
networks as nodes or client programs (aka "light-weight nodes") that do not
store blocks in memory or on disk regularly need to verify that a
transaction has been included on the chain or not. This can be accomplished
by the lightweight node supplying the transaction hash and a block index,
querying a full node for a merkle inclusion proof of the transaction's inclusion
in the block with the index supplied. If the full node finds the transaction in
that block and responds with the inclusion proof and the block's merkle root.
With this information, it takes O(log(n)) for the lightweight node to validate
the inclusion proof.

--| Constructs a Merkle Inclusion Proof
merkleProof
::MerkleTreea--^ Tree to which data may belong ->MerkleRoota--^ Leaf root hash, data to query inclusion of ->MerkleProofa--^ A list of hashes to verify data inclusion --| Validates a Merkle Inclusion Proof
validateMerkleProof
::MerkleProofa--^ Inclusion proof constructed by the prover ->MerkleRoota--^ Root of the merkle tree from which the proof was constructed->MerkleRoota--^ Leaf root hash for which the proof was constructed ->Bool--^ Leaf root inclusion