Module merkerl

A module with a simple merkle tree implementation as described in https://en.wikipedia.org/wiki/Merkle_tree implementation.

Description

A module with a simple merkle tree implementation as described in https://en.wikipedia.org/wiki/Merkle_tree implementation. This module implements a immutable merkle tree. Once all the values are available, it can construct a balanced tree for those values which can then be used to access the leaf values, generate merkle proofs, and verify generated proofs.

Data Types

hash()

hash() = binary()

merkle()

abstract datatype: merkle()

proof()

abstract datatype: proof()

tree()

tree() = #leaf{hash = hash(), value = any()} | #empty{hash = hash()} | #node{hash = hash(), height = non_neg_integer(), left = tree(), right = tree()}

Function Index

count/1get the number of leaves int he merkle tree.
fold/3Fold over the leaves of the merkle tree.
gen_proof/2Generate a merkle proof for a given hash in the tree.
hash_value/1A commonly used hash value for merkle trees.
height/1Get the height of the given merkle tree.
leaves/1Get a list of values and their hashes from the tree.
new/2Construct a merkle tree given a list of values and a hash function.
proof_hashes/1Get just the hashes from a given proof.
root_hash/1Gets the root hash of the given merkle tree.
values/1Get the values of the merkle tree.
verify_proof/3Verifies that a given hash of a value is in a given merkle tree using the provided proof.

Function Details

count/1

count(Merkle::merkle()) -> non_neg_integer()

get the number of leaves int he merkle tree.

fold/3

fold(FoldFun, Acc, Merkle::tree() | merkle()) -> Acc

Fold over the leaves of the merkle tree. The given function will take the a {hash, value} tuple, and a given accumulator to fold over the merkle tree with and returns the resulting accumulator.

gen_proof/2

gen_proof(ValueHash::any(), Merkle::merkle()) -> proof() | not_found

Generate a merkle proof for a given hash in the tree. Note that the resulting proof does not include the value hash itself, which saves space in the proof.

hash_value/1

hash_value(Value) -> any()

A commonly used hash value for merkle trees. This function will SHA256 hash the given value when it is binary. A convenience form detects non-binary forms and uses term_to_binary/1 to convert other erlang terms to a binary form. It is not recommended to use the non-binary form if the resulting trees or proofs are to be sent over a network.

height/1

height(Merkle::merkle()) -> non_neg_integer()

Get the height of the given merkle tree. This is a fast operation since the hash was calculated on construction of the tree.

leaves/1

leaves(M::merkle()) -> [{hash(), term()}]

Get a list of values and their hashes from the tree. The resulting list retains the same order as the list that was passed in on merkle construction.

new/2

new(Values::[term()], HashFun::fun((term()) -> hash())) -> merkle()

Construct a merkle tree given a list of values and a hash function. The hash function is used to calculate the hash of the leaf value. A simple one hash_value/1 is provided in this module. The resulting merkle tree retains the order of the values but duplicate values (as determined by the result of the hash function) are skipped, so only the first found element counts.

proof_hashes/1

proof_hashes(Proof::proof()) -> [hash()]

Get just the hashes from a given proof.

root_hash/1

root_hash(Merkle::merkle()) -> hash()

Gets the root hash of the given merkle tree. This is a fast operation since the hash was calculated on construction of the tree.

values/1

values(M::merkle()) -> [term()]

Get the values of the merkle tree. This returns the values in the same order as the list that was passed in on merkle construction.

verify_proof/3

verify_proof(ValueHash::hash(), Merkle::merkle() | hash(), Proof::proof() | {error, any()}) -> ok | {error, Reason}

Verifies that a given hash of a value is in a given merkle tree using the provided proof.


Generated by EDoc