View Source MSSMT (MSSMT v0.1.0)
Implementation of a Merkle-Sum Sparse Merkle Tree (MS-SMT).
A MS-SMT is a data structure that combines the features of a Merkle tree and a sum tree, allowing for efficient proofs of inclusion and accumulation of values.
Summary
Functions
Deletes a key-value pair from the MS-SMT.
Generates a proof of inclusion for a given key.
Retrieves the value associated with a key from the MS-SMT.
Inserts a key-value pair into the MS-SMT.
Creates a new empty MS-SMT.
Calculates the root hash of the MS-SMT.
Calculates the total sum of all values in the MS-SMT.
Updates the value associated with a key in the MS-SMT.
Verifies a proof of inclusion for a given key and value.
Functions
Deletes a key-value pair from the MS-SMT.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> tree = MSSMT.delete(tree, "key1")
iex> MSSMT.get(tree, "key1")
nil
@spec generate_proof(map(), binary()) :: [MSSMT.Node.t()]
Generates a proof of inclusion for a given key.
The proof consists of the necessary sibling nodes to reconstruct the root hash.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> proof = MSSMT.generate_proof(tree, "key1")
iex> is_list(proof)
true
Retrieves the value associated with a key from the MS-SMT.
Returns nil
if the key does not exist.
Examples
iex> tree = MSSMT.new()
iex> MSSMT.get(tree, "nonexistent_key")
nil
Inserts a key-value pair into the MS-SMT.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> MSSMT.get(tree, "key1")
100
@spec new() :: map()
Creates a new empty MS-SMT.
Calculates the root hash of the MS-SMT.
The root hash is a cryptographic representation of the entire tree state.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> root_hash = MSSMT.root_hash(tree)
iex> byte_size(root_hash)
32
Calculates the total sum of all values in the MS-SMT.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> tree = MSSMT.insert(tree, "key2", 200)
iex> MSSMT.total_sum(tree)
300
Updates the value associated with a key in the MS-SMT.
If the key does not exist, the tree remains unchanged.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> tree = MSSMT.update(tree, "key1", 200)
iex> MSSMT.get(tree, "key1")
200
@spec verify_proof(binary() | nil, binary(), number(), [MSSMT.Node.t()]) :: boolean()
Verifies a proof of inclusion for a given key and value.
Returns true
if the proof is valid, false
otherwise.
Examples
iex> tree = MSSMT.new()
iex> tree = MSSMT.insert(tree, "key1", 100)
iex> root_hash = MSSMT.root_hash(tree)
iex> proof = MSSMT.generate_proof(tree, "key1")
iex> MSSMT.verify_proof(root_hash, "key1", 100, proof)
true