merkle_tree v2.0.0 MerkleTree

A hash tree or Merkle tree is a tree in which every non-leaf node is labelled with the hash of the labels or values (in case of leaves) of its child nodes. Hash trees are useful because they allow efficient and secure verification of the contents of large data structures.

## Usage Example

iex> MerkleTree.new ["a", "b", "c", "d"] %MerkleTree{

blocks: ["a", "b", "c", "d"],
hash_function: &MerkleTree.Crypto.sha256/1,
root: %MerkleTree.Node{
  children: [
    %MerkleTree.Node{
      children: [
        %MerkleTree.Node{
          children: [],
          height: 0,
          value: "022a6979e6dab7aa5ae4c3e5e45f7e977112a7e63593820dbec1ec738a24f93c"
        },
        %MerkleTree.Node{
          children: [],
          height: 0,
          value: "57eb35615d47f34ec714cacdf5fd74608a5e8e102724e80b24b287c0c27b6a31"
        }
      ],
      height: 1,
      value: "4c64254e6636add7f281ff49278beceb26378bd0021d1809974994e6e233ec35"
    },
    %MerkleTree.Node{
      children: [
        %MerkleTree.Node{
          children: [],
          height: 0,
          value: "597fcb31282d34654c200d3418fca5705c648ebf326ec73d8ddef11841f876d8"
        },
        %MerkleTree.Node{
          children: [],
          height: 0,
          value: "d070dc5b8da9aea7dc0f5ad4c29d89965200059c9a0ceca3abd5da2492dcb71d"
        }
      ],
      height: 1,
      value: "40e2511a6323177e537acb2e90886e0da1f84656fd6334b89f60d742a3967f09"
    }
  ],
  height: 2,
  value: "9dc1674ae1ee61c90ba50b6261e8f9a47f7ea07d92612158edfe3c2a37c6d74c"
}

}

Link to this section Summary

Functions

Builds a root MerkleTree.Node structure of a merkle tree

Calculates the root of the merkle tree without building the entire tree explicitly,

Creates a new merkle tree, given a blocks and hash function or opts. available options

Link to this section Types

Link to this type

blocks()

blocks() :: [String.t(), ...]
Link to this type

hash_function()

hash_function() :: (String.t() -> String.t())
Link to this type

t()

t() :: %MerkleTree{
  blocks: blocks(),
  hash_function: hash_function(),
  root: root()
}

Link to this section Functions

Link to this function

build(blocks, hash_function_or_opts \\ [])

build(blocks(), hash_function() | Keyword.t()) :: root()

Builds a root MerkleTree.Node structure of a merkle tree

See new/2 for a rundown of options

Link to this function

fast_root(blocks, opts \\ [])

fast_root(blocks(), Keyword.t()) :: MerkleTree.Node.hash()

Calculates the root of the merkle tree without building the entire tree explicitly,

See new/2 for a rundown of options

Link to this function

new(blocks, hash_function_or_opts \\ [])

new(blocks(), hash_function() | Keyword.t()) :: t()

Creates a new merkle tree, given a blocks and hash function or opts. available options:

:hash_function - used hash in mercle tree default :sha256 from :cryto
:height - allows to construct tree of provided height,
    empty leaves data will be taken from `:default_data_block` parameter
:default_data_block - this data will be used to supply empty
    leaves in case where there isn't enough blocks provided

Check out MerkleTree.Crypto for other available cryptographic hashes. Alternatively, you can supply your own hash function that has the spec (String.t -> String.t).