vfhe.crypto.merkle¶
The vector commitment: Merkle, a binary tree over packed BLAKE3 leaf digests, and join_digests.
A tree commits to leaf digests packed end to end, DIGEST_LEN bytes each; an
opening is the sibling digests along the leaf’s path, packed the same way,
and a verifier checks it against the root alone. join_digests hashes
several packed arrays together index by index, so one tree commits to all of
them.
>>> from vfhe.crypto.hash import digest
>>> from vfhe.crypto.merkle import Merkle
>>> digests = b"".join(digest(leaf) for leaf in (b"a", b"b", b"c"))
>>> tree = Merkle(digests)
>>> Merkle.verify(tree.root, 1, tree.open(1), digest(b"b"))
True
- class vfhe.crypto.merkle.Merkle(digests: bytes | bytearray | memoryview)¶
A binary Merkle tree over BLAKE3 leaf digests:
root,open,open_many, and the staticverifyandverify_many.The tree is built on construction, in C, and holds every level, so openings cost no rebuild. An internal node is the digest of its left child followed by its right. A leaf count that is not a power of two is padded with zero digests up to the next one, which makes the root well defined for any size; the leaf count is public protocol data, so the padding needs no domain separation. An opening is the sibling digests along the leaf’s path, from the leaf’s level up to the root’s children, packed in one
bytes; the leaf index is no part of it, since a verifier checks the position it queried itself. A tree belongs to one thread at a time.Builds the tree.
- Parameters:
digests – The leaf digests, one or more
DIGEST_LEN-byte digests packed end to end, in leaf order. Read once.- Raises:
ValueError –
digestsis empty or its length is no multiple ofDIGEST_LEN.
- root¶
the
DIGEST_LEN-byte commitment to every leaf.- Type:
The root
- open(index: int) bytes¶
Opens one leaf.
- Parameters:
index – The leaf, 0 to
len(self) - 1.- Returns:
ceil(log2(len(self)))sibling digests packed end to end, the leaf’s sibling first and the root’s child last.- Return type:
The opening
- Raises:
IndexError –
indexis negative or past the last leaf.
- open_many(indices: Sequence[int]) bytes¶
Opens several leaves in one call.
- Parameters:
indices – The leaves, each 0 to
len(self) - 1; a leaf may repeat.- Returns:
The openings of
indicespacked end to end, each asopenreturns it, so the opening of leafkis thek-th oflen(indices)equal parts; empty for no index or a tree of one leaf. Siblings shared by several paths are sent once per index.- Raises:
IndexError – An index is negative or past the last leaf.
- static verify_many(root: Bytes, indices: Sequence[int], paths: Bytes, digests: Bytes) int | None¶
Checks the openings of several leaves of one tree in one call.
- Parameters:
root – The
DIGEST_LEN-byte root the leaves are claimed under.indices – The leaf positions the verifier queried.
paths – The openings packed end to end in the same order, as
open_manyreturns them; the number of siblings per opening is read from the length.digests – The
DIGEST_LEN-byte digest of each leaf, packed in the same order.
- Returns:
None when every opening authenticates, else the first
kwhose opening does not.- Raises:
ValueError –
digestsis not one digest per index, orpathsdoes not split intolen(indices)openings of whole digests.
- static verify(root: bytes | bytearray | memoryview, index: int, path: bytes | bytearray | memoryview, digest: bytes | bytearray | memoryview) bool¶
Checks an opening against a root, with no tree at hand.
Recomputes the root from the leaf digest and its opening, pairing each sibling on the side the bits of
indexsay, and compares. A local check: no challenges, no rounds. Thread-safe, no state.- Parameters:
root – The
DIGEST_LEN-byte root the leaf is claimed under.index – The leaf position the verifier queried.
path – The opening, as
openreturns it: whole digests, as many as the tree is high.digest – The
DIGEST_LEN-byte digest of the leaf.
- Returns:
True when the leaf with
digestsits atindexunderroot.- Raises:
ValueError –
rootordigestis notDIGEST_LENbytes, or the length ofpathis no multiple ofDIGEST_LEN.
- vfhe.crypto.merkle.join_digests(parts: Sequence[Bytes]) bytes¶
Joins several packed digest arrays into one, index by index.
Digest
iof the result is the digest of digestiof every array inparts, concatenated in the order ofparts. A tree over the result commits to leafiof every array at once, so several codewords of one length sit under one tree and one opening. Thread-safe, no state.- Parameters:
parts – One or more arrays, each the same whole number of
DIGEST_LEN-byte digests packed end to end.- Returns:
As many packed digests as each array holds.
- Raises:
ValueError –
partsis empty, an array’s length is no multiple ofDIGEST_LEN, or the arrays differ in length.