public class MerkleTreePrimitives
extends java.lang.Object
All algorithms are expressed against the MerkleTreeHash operator,
which the caller supplies; there are no direct org.bouncycastle.crypto.*
or java.security.* dependencies in this class.
| Modifier and Type | Class and Description |
|---|---|
static class |
MerkleTreePrimitives.SubtreeInfo
Simple container for a subtree interval (start inclusive, end exclusive).
|
| Constructor and Description |
|---|
MerkleTreePrimitives() |
| Modifier and Type | Method and Description |
|---|---|
static byte[] |
computeMerkleTreeHash(java.util.List<byte[]> entryHashes,
long start,
long end,
MerkleTreeHash hash)
Computes the Merkle Tree Hash
MTH(D[start:end]) over a range of entries,
per RFC 9162 Section 2.1.1. |
static byte[] |
computeMerkleTreeHash(MerkleTreeNodeSource nodes,
long start,
long end,
MerkleTreeHash hash)
Computes the Merkle Tree Hash
MTH(D[start:end]) over a range of entries
from a storage-backed tree, per
RFC 9162 Section 2.1.1. |
static byte[] |
evaluateSubtreeInclusionProof(long index,
long start,
long end,
byte[] entryHash,
java.util.List<byte[]> proof,
MerkleTreeHash hash)
Evaluates a subtree inclusion proof, returning the expected subtree hash.
|
static java.util.List<long[]> |
findCoveringSubtrees(long start,
long end)
Selects the two subtrees that efficiently cover the interval [start, end), per
Section 4.5.1.
|
static java.util.List<byte[]> |
generateSubtreeConsistencyProof(long start,
long end,
long n,
java.util.List<byte[]> entryHashes,
MerkleTreeHash hash)
Generates a subtree consistency proof
SUBTREE_PROOF(start, end, D_n) showing
that the subtree [start, end) is contained in the tree of size n, per
Section 4.4.1. |
static java.util.List<byte[]> |
generateSubtreeConsistencyProof(long start,
long end,
long n,
MerkleTreeNodeSource nodes,
MerkleTreeHash hash)
Generates a subtree consistency proof
SUBTREE_PROOF(start, end, D_n) for the
subtree [start, end) in a storage-backed tree of size n; see
generateSubtreeConsistencyProof(long, long, long, List, MerkleTreeHash). |
static java.util.List<byte[]> |
generateSubtreeInclusionProof(long index,
long start,
long end,
java.util.List<byte[]> entryHashes,
MerkleTreeHash hash)
Generates a subtree inclusion proof for the entry at
index within the
subtree [start, end). |
static java.util.List<byte[]> |
generateSubtreeInclusionProof(long index,
long start,
long end,
MerkleTreeNodeSource nodes,
MerkleTreeHash hash)
Generates a subtree inclusion proof for the entry at
index within the
subtree [start, end) of a storage-backed tree; see
generateSubtreeInclusionProof(long, long, long, List, MerkleTreeHash). |
static boolean |
isFullSubtree(long start,
long end)
Whether
[start, end) is a full subtree: its size is a power of two and
start is a multiple of that size, so MTH(D[start:end]) is a single
node of the tree. |
static boolean |
isValidSubtree(long start,
long end)
Checks whether
[start, end) is a valid subtree interval per
Section 4.1:
0 <= start <= end, and start is a multiple of BIT_CEIL(end - start). |
static boolean |
verifySubtreeConsistencyProof(long start,
long end,
long n,
byte[] subtreeHash,
byte[] rootHash,
java.util.List<byte[]> proof,
MerkleTreeHash hash)
Verifies a subtree consistency proof.
|
static boolean |
verifySubtreeInclusionProof(long index,
long start,
long end,
byte[] entryHash,
byte[] subtreeHash,
java.util.List<byte[]> proof,
MerkleTreeHash hash)
Verifies a subtree inclusion proof by comparing the evaluated hash with the given subtree hash.
|
public static byte[] evaluateSubtreeInclusionProof(long index,
long start,
long end,
byte[] entryHash,
java.util.List<byte[]> proof,
MerkleTreeHash hash)
throws InvalidProofException
index - absolute index of the entry in the logstart - subtree start index (inclusive)end - subtree end index (exclusive)entryHash - hash of the entry (MTH({entry}))proof - list of node hashes forming the inclusion proofhash - the Merkle tree hash implementationInvalidProofException - if the proof is malformed or cannot be evaluatedpublic static boolean verifySubtreeInclusionProof(long index,
long start,
long end,
byte[] entryHash,
byte[] subtreeHash,
java.util.List<byte[]> proof,
MerkleTreeHash hash)
index - absolute index of the entrystart - subtree startend - subtree endentryHash - hash of the entrysubtreeHash - claimed subtree hashproof - inclusion proofhash - hash implementationpublic static boolean verifySubtreeConsistencyProof(long start,
long end,
long n,
byte[] subtreeHash,
byte[] rootHash,
java.util.List<byte[]> proof,
MerkleTreeHash hash)
start - subtree start indexend - subtree end index (exclusive)n - full tree size (number of entries)subtreeHash - hash of the subtree (MTH(D[start:end]))rootHash - hash of the full tree (MTH(D[0:n]))proof - list of node hashes forming the consistency proofhash - hash implementationpublic static byte[] computeMerkleTreeHash(java.util.List<byte[]> entryHashes,
long start,
long end,
MerkleTreeHash hash)
MTH(D[start:end]) over a range of entries,
per RFC 9162 Section 2.1.1.
The range need not be a subtree in the sense of Section 4.1; any
[start, end) within the list is accepted, which is what the proof
generators need for the intermediate node hashes they emit. The empty range
hashes to HASH(""), the hash of the empty string, as RFC 9162 defines
MTH({}).
This overload builds a fresh ListMerkleTreeNodeSource for the call, so it costs
O(n) hashes every time; to generate many proofs over one tree, construct one
ListMerkleTreeNodeSource and use the MerkleTreeNodeSource overload,
which reuses the nodes it has computed.
entryHashes - hashes of the tree's entries, entry i at position i
(each MTH({entry}), i.e. MerkleTreeHash.hashLeaf(byte[]))start - range start index (inclusive)end - range end index (exclusive)hash - the Merkle tree hash implementationMTH(D[start:end])java.lang.IllegalArgumentException - if the range is not covered by entryHashespublic static byte[] computeMerkleTreeHash(MerkleTreeNodeSource nodes, long start, long end, MerkleTreeHash hash)
MTH(D[start:end]) over a range of entries
from a storage-backed tree, per
RFC 9162 Section 2.1.1.
A range that is itself a full subtree is a single request to nodes; any other
range is combined from at most one full subtree per level of the tree.nodes - source of full-subtree hashes for the treestart - range start index (inclusive)end - range end index (exclusive)hash - the Merkle tree hash implementationMTH(D[start:end])java.lang.IllegalArgumentException - if the range is invalid, or nodes cannot supply a nodepublic static java.util.List<byte[]> generateSubtreeInclusionProof(long index,
long start,
long end,
java.util.List<byte[]> entryHashes,
MerkleTreeHash hash)
index within the
subtree [start, end). Per
Section 4.3
this is the Merkle inclusion proof PATH(index - start, D[start:end]) of
RFC 9162 Section 2.1.3.1,
computed over the subtree's entries. The result is accepted by
evaluateSubtreeInclusionProof(long, long, long, byte[], java.util.List<byte[]>, org.bouncycastle.cert.plants.MerkleTreeHash) and verifySubtreeInclusionProof(long, long, long, byte[], byte[], java.util.List<byte[]>, org.bouncycastle.cert.plants.MerkleTreeHash)
for the same index, start and end.
This overload builds a fresh ListMerkleTreeNodeSource for the call, so it costs
O(n) hashes every time; to generate many proofs over one tree, construct one
ListMerkleTreeNodeSource and use the MerkleTreeNodeSource overload,
which reuses the nodes it has computed.
index - absolute index of the entry in the logstart - subtree start index (inclusive)end - subtree end index (exclusive)entryHashes - hashes of the tree's entries, entry i at position i;
only positions [start, end) are readhash - the Merkle tree hash implementationjava.lang.IllegalArgumentException - if [start, end) is not a valid subtree (Section 4.1),
index lies outside it, or entryHashes does not cover itpublic static java.util.List<byte[]> generateSubtreeInclusionProof(long index,
long start,
long end,
MerkleTreeNodeSource nodes,
MerkleTreeHash hash)
index within the
subtree [start, end) of a storage-backed tree; see
generateSubtreeInclusionProof(long, long, long, List, MerkleTreeHash).index - absolute index of the entry in the logstart - subtree start index (inclusive)end - subtree end index (exclusive)nodes - source of full-subtree hashes for the tree; only nodes within
[start, end) are requestedhash - the Merkle tree hash implementationjava.lang.IllegalArgumentException - if [start, end) is not a valid subtree (Section 4.1),
index lies outside it, or nodes cannot supply a nodepublic static java.util.List<byte[]> generateSubtreeConsistencyProof(long start,
long end,
long n,
java.util.List<byte[]> entryHashes,
MerkleTreeHash hash)
SUBTREE_PROOF(start, end, D_n) showing
that the subtree [start, end) is contained in the tree of size n, per
Section 4.4.1.
The result is accepted by verifySubtreeConsistencyProof(long, long, long, byte[], byte[], java.util.List<byte[]>, org.bouncycastle.cert.plants.MerkleTreeHash) for the same
start, end and n. As the draft notes, with start == 0
this is the RFC 9162 consistency proof PROOF(end, D_n), and with
end == start + 1 it is the inclusion proof PATH(start, D_n).
This overload builds a fresh ListMerkleTreeNodeSource for the call, so it costs
O(n) hashes every time; to generate many proofs over one tree, construct one
ListMerkleTreeNodeSource and use the MerkleTreeNodeSource overload,
which reuses the nodes it has computed.
start - subtree start index (inclusive)end - subtree end index (exclusive)n - full tree size (number of entries)entryHashes - hashes of the tree's entries, entry i at position i;
positions [0, n) are readhash - the Merkle tree hash implementationjava.lang.IllegalArgumentException - if [start, end) is not a valid subtree (Section 4.1),
end > n, or entryHashes does not cover [0, n)public static java.util.List<byte[]> generateSubtreeConsistencyProof(long start,
long end,
long n,
MerkleTreeNodeSource nodes,
MerkleTreeHash hash)
SUBTREE_PROOF(start, end, D_n) for the
subtree [start, end) in a storage-backed tree of size n; see
generateSubtreeConsistencyProof(long, long, long, List, MerkleTreeHash).start - subtree start index (inclusive)end - subtree end index (exclusive)n - full tree size (number of entries)nodes - source of full-subtree hashes for the tree; only nodes within
[0, n) are requestedhash - the Merkle tree hash implementationjava.lang.IllegalArgumentException - if [start, end) is not a valid subtree (Section 4.1),
end > n, or nodes cannot supply a nodepublic static boolean isFullSubtree(long start,
long end)
[start, end) is a full subtree: its size is a power of two and
start is a multiple of that size, so MTH(D[start:end]) is a single
node of the tree.start - subtree start (inclusive)end - subtree end (exclusive)public static boolean isValidSubtree(long start,
long end)
[start, end) is a valid subtree interval per
Section 4.1:
0 <= start <= end, and start is a multiple of BIT_CEIL(end - start).
The empty interval [x, x) is a valid subtree for every x, since
BIT_CEIL(0) is 1 (draft-06 made empty subtrees valid), and [0, x)
is valid for every x.start - subtree start (inclusive)end - subtree end (exclusive)public static java.util.List<long[]> findCoveringSubtrees(long start,
long end)
{start, end} pairs, left then
right, with left.start <= start <= left.end == right.start <= end == right.end;
for an interval of at most one entry they are [start, end) and the
empty subtree [end, end) (draft-06 changed this from returning the
single subtree alone).start - start index of the interval (inclusive)end - end index of the interval (exclusive), at least start