View Source MSSMT (MSSMT v0.1.1)
Merkle Sum Sparse Merkle Tree (MSSMT) implementation.
Provides functions to create, insert, retrieve, delete, and verify elements in the MSSMT.
Summary
Functions
Deletes a key from the MSSMT.
Retrieves the value and sum associated with a key in the MSSMT.
Inserts a key-value pair with an associated sum into the MSSMT.
Generates a Merkle proof for a given key.
Creates a new empty MSSMT.
Returns the root hash of the MSSMT.
Returns the total sum of all nodes in the MSSMT.
Verifies a Merkle proof against a root hash.
Functions
@spec delete(MSSMT.Node.t() | nil, <<_::256>>) :: {:ok, MSSMT.Node.t() | nil} | {:error, :not_found}
Deletes a key from the MSSMT.
Parameters
tree
: The current tree.key
: The key to delete (256-bit binary).
Examples
iex> tree = MSSMT.new()
iex> key = :crypto.strong_rand_bytes(32)
iex> {:ok, tree} = MSSMT.insert(tree, key, "value", 100)
iex> {:ok, tree} = MSSMT.delete(tree, key)
iex> MSSMT.get(tree, key)
{:error, :not_found}
@spec get(MSSMT.Node.t() | nil, <<_::256>>) :: {:ok, binary(), non_neg_integer()} | {:error, :not_found}
Retrieves the value and sum associated with a key in the MSSMT.
Parameters
tree
: The current tree.key
: The key to retrieve (256-bit binary).
Examples
iex> tree = MSSMT.new()
iex> key = :crypto.strong_rand_bytes(32)
iex> {:ok, tree} = MSSMT.insert(tree, key, "value", 100)
iex> {:ok, value, sum} = MSSMT.get(tree, key)
iex> value
"value"
iex> sum
100
@spec insert(MSSMT.Node.t() | nil, <<_::256>>, binary(), non_neg_integer()) :: {:ok, MSSMT.Node.t()}
Inserts a key-value pair with an associated sum into the MSSMT.
Parameters
tree
: The current tree.key
: The key to insert (256-bit binary).value
: The value associated with the key.sum
: The sum associated with the key.
Examples
iex> tree = MSSMT.new()
iex> key = :crypto.strong_rand_bytes(32)
iex> {:ok, tree} = MSSMT.insert(tree, key, "value", 100)
iex> {:ok, value, sum} = MSSMT.get(tree, key)
iex> value
"value"
iex> sum
100
@spec merkle_proof(MSSMT.Node.t() | nil, <<_::256>>) :: [MSSMT.Node.t()]
Generates a Merkle proof for a given key.
Parameters
tree
: The current tree.key
: The key to generate the proof for (256-bit binary).
Examples
iex> tree = MSSMT.new()
iex> key = :crypto.strong_rand_bytes(32)
iex> {:ok, tree} = MSSMT.insert(tree, key, "value", 100)
iex> proof = MSSMT.merkle_proof(tree, key)
iex> is_list(proof)
true
@spec new() :: nil
Creates a new empty MSSMT.
Examples
iex> _tree = MSSMT.new()
nil
@spec root_hash(MSSMT.Node.t() | nil) :: MSSMT.NodeHash.t()
Returns the root hash of the MSSMT.
Examples
iex> tree = MSSMT.new()
iex> root_hash = MSSMT.root_hash(tree)
iex> root_hash == <<0::256>>
true
@spec total_sum(MSSMT.Node.t() | nil) :: non_neg_integer()
Returns the total sum of all nodes in the MSSMT.
Examples
iex> tree = MSSMT.new()
iex> key1 = :crypto.strong_rand_bytes(32)
iex> key2 = :crypto.strong_rand_bytes(32)
iex> {:ok, tree} = MSSMT.insert(tree, key1, "value1", 100)
iex> {:ok, tree} = MSSMT.insert(tree, key2, "value2", 200)
iex> MSSMT.total_sum(tree)
300
@spec verify_proof(MSSMT.NodeHash.t(), <<_::256>>, binary(), non_neg_integer(), [ MSSMT.Node.t() ]) :: boolean()
Verifies a Merkle proof against a root hash.
Parameters
root_hash
: The root hash of the MSSMT.key
: The key the proof is for (256-bit binary).value
: The value associated with the key.sum
: The sum associated with the key.proof
: The Merkle proof as a list of sibling nodes.
Examples
iex> tree = MSSMT.new()
iex> key = :crypto.strong_rand_bytes(32)
iex> {:ok, tree} = MSSMT.insert(tree, key, "value", 100)
iex> proof = MSSMT.merkle_proof(tree, key)
iex> root_hash = MSSMT.root_hash(tree)
iex> MSSMT.verify_proof(root_hash, key, "value", 100, proof)
true