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.

Typedefs

typedef struct _Merkle *Merkle

A tree over size leaves.

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 i of out is the digest of digest i of every array in parts, concatenated in order.

A tree over out commits 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 count digests.

  • parts – [in] part_count arrays of count packed digests.

  • part_count – [in] Arrays.

  • count – [in] Digests per array.

Merkle merkle_new(uint64_t size)

Allocates a tree for size leaves, 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 size is 0.

void merkle_free(Merkle mk)

Releases a tree.

Parameters:

mk – [in] The tree, or NULL.

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] size packed 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_size sibling digests from the leaf’s level upwards, so out[i] is the sibling of the path node at height i.

Parameters:
  • out – [out] Receives log_size * BLAKE3_OUT_LEN bytes.

  • 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 at out + k * log_size * BLAKE3_OUT_LEN.

Parameters:
  • out – [out] Receives count * log_size * BLAKE3_OUT_LEN bytes.

  • mk – [in] A committed tree.

  • indices – [in] count leaves, each below 1 << 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_len packed 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 k sits at indices[k], its digest at leaf_digests + k * BLAKE3_OUT_LEN, and its opening is the path_len siblings at paths + k * path_len * BLAKE3_OUT_LEN. path_len is not checked against the tree’s depth, which the caller knows.

Parameters:
  • root – [in] The digest to match.

  • indices – [in] count leaf positions.

  • paths – [in] count openings, packed end to end.

  • path_len – [in] Siblings per opening.

  • leaf_digests – [in] count packed leaf digests.

  • count – [in] Leaves.

Returns:

-1 when every opening is valid, else the first k whose is not.

struct _Merkle
#include <merkle.h>

A tree over size leaves.

Public Members

uint8_t **levels

levels[i] holds the 2^i digests of level i.

uint64_t size

Leaves committed to, at most 1 << log_size.

uint64_t log_size

Height: the leaves sit at this level.