merkle.h¶
A binary Merkle tree over BLAKE3 digests.
The caller supplies the leaf digests; an internal node is BLAKE3(left || right). Levels are numbered from the root: level 0 holds the root, level log_size the leaves, so level i holds 2^i digests. A leaf count that is not a power of two is padded with zero digests, which makes the root well defined for any size; the leaf count is public protocol data, so the padding needs no domain separation. Digest buffers never alias between input and output.
Note
Thread-safe between trees; one tree belongs to one thread at a time.
Functions
-
void merkle_join_digests(uint8_t *out, const uint8_t *const *parts, uint64_t part_count, uint64_t count)¶
Writes joined leaf digests: digest
iofoutis the digest of digestiof every array inparts, concatenated in order.A tree over
outcommits to every array’s leaves at once, one leaf per index. A large join runs on the library’s threads, with the same result at any number of them.- Parameters:
out – [out] Receives
countdigests.parts – [in]
part_countarrays ofcountpacked digests.part_count – [in] Arrays.
count – [in] Digests per array.
-
Merkle merkle_new(uint64_t size)¶
Allocates a tree for
sizeleaves, every level at once, so the same tree can be committed to repeatedly.Warning
Exits the process if an allocation is refused.
- Parameters:
size – [in] Leaves, at least 1.
- Returns:
A tree to release with merkle_free(); NULL when
sizeis 0.
-
void merkle_commit(Merkle mk, const uint8_t *leaf_digests)¶
Fills the leaf level and rebuilds every internal level, a large level on the library’s threads, the same tree at any number of them.
- Parameters:
mk – [inout] The tree.
leaf_digests – [in]
sizepacked digests.
-
void merkle_get_root(uint8_t *out, Merkle mk)¶
Writes the root of the last commit.
- Parameters:
out – [out] Receives the digest.
mk – [in] A committed tree.
-
void merkle_open(uint8_t *out, Merkle mk, uint64_t index)¶
Writes the opening of a leaf: the
log_sizesibling digests from the leaf’s level upwards, soout[i]is the sibling of the path node at heighti.- Parameters:
out – [out] Receives
log_size * BLAKE3_OUT_LENbytes.mk – [in] A committed tree.
index – [in] The leaf, below
1 << log_size.
-
void merkle_open_many(uint8_t *out, Merkle mk, const uint64_t *indices, uint64_t count)¶
Writes the openings of several leaves, each as merkle_open() writes it, end to end: the opening of
indices[k]starts atout + k * log_size * BLAKE3_OUT_LEN.- Parameters:
out – [out] Receives
count * log_size * BLAKE3_OUT_LENbytes.mk – [in] A committed tree.
indices – [in]
countleaves, each below1 << log_size.count – [in] Leaves.
-
bool merkle_verify(const uint8_t *root, uint64_t index, const uint8_t *path, uint64_t path_len, const uint8_t *leaf_digest)¶
Recomputes a root from a leaf digest and its opening and compares it with
root.- Parameters:
root – [in] The digest to match.
index – [in] The leaf position the opening belongs to.
path – [in]
path_lenpacked sibling digests, as merkle_open() writes them.path_len – [in] Siblings.
leaf_digest – [in] The leaf’s digest.
- Returns:
true when the opening is valid.
-
int64_t merkle_verify_many(const uint8_t *root, const uint64_t *indices, const uint8_t *paths, uint64_t path_len, const uint8_t *leaf_digests, uint64_t count)¶
Checks the openings of several leaves of one tree, each as merkle_verify() does.
Leaf
ksits atindices[k], its digest atleaf_digests + k * BLAKE3_OUT_LEN, and its opening is thepath_lensiblings atpaths + k * path_len * BLAKE3_OUT_LEN.path_lenis not checked against the tree’s depth, which the caller knows.- Parameters:
root – [in] The digest to match.
indices – [in]
countleaf positions.paths – [in]
countopenings, packed end to end.path_len – [in] Siblings per opening.
leaf_digests – [in]
countpacked leaf digests.count – [in] Leaves.
- Returns:
-1 when every opening is valid, else the first
kwhose is not.
-
struct _Merkle¶
- #include <merkle.h>
A tree over
sizeleaves.