Class Tree
java.lang.Object
io.crums.util.mrkl.Tree
- Direct Known Subclasses:
DeltaTree,FixedLeafTree,FreeLeafTree
The Merkle tree. Instances are immutable. This class abstracts away the
memory layout for the tree.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected StringBuilderappendToStringDetail(StringBuilder string) abstract byte[]data(int level, int index) Returns [a copy of] the data for the node at the specified coordinates.final StringReturns the hashing algorithm that the tree's internal nodes use.final byte[]hash()Returns the hash of the root of the tree.final intReturns the byte width of the hashing algorithm by observing the width of the node at coordinates (1, 0), which is the first internal node constructed in a tree.static byte[]hashInternals(byte[] left, byte[] right, MessageDigest digest) static byte[]hashInternals(ByteBuffer left, ByteBuffer right, MessageDigest digest) Computes and returns the hash of the given merkle nodes.static byte[]hashLeaves(byte[] left, byte[] right, MessageDigest digest) static byte[]hashLeaves(ByteBuffer left, ByteBuffer right, MessageDigest digest) Computes and returns the hash of the given leaf nodes.static byte[]hashUncommon(byte[] leftInternal, byte[] rightLeaf, MessageDigest digest) Computes and returns the hash of the given merkle nodes.static byte[]hashUncommon(ByteBuffer leftInternal, ByteBuffer rightLeaf, MessageDigest digest) Computes and returns the hash of the given merkle nodes.idx()Returns the random access index into tree structure.final booleanDetermines whether the width of the leaves is fixed.final booleanDetermines whether the width of the leaves is the same as the byte width of the hashing algorithm.abstract intReturns the leaf width in bytes if fixed; -1, otherwise (variable).final Proofproof(int leafIndex) Returns a cryptographic path to the root of the Merkle tree.final Noderoot()Returns the root node of the tree.toString()For debug use.
-
Field Details
-
LEAF_PAD
public static final byte LEAF_PAD- See Also:
-
BRANCH_PAD
public static final byte BRANCH_PAD- See Also:
-
-
Constructor Details
-
Tree
Base constructor creates an index and locks down the algo.- Parameters:
leaves- the number of leaf nodes in the treealgo- the hash algo used for the trees internal nodes.
-
Tree
Copy constructor.
-
-
Method Details
-
hashLeaves
-
hashLeaves
Computes and returns the hash of the given leaf nodes. On return, the given buffers have no remaining bytes. -
hashInternals
-
hashInternals
Computes and returns the hash of the given merkle nodes. On return, the given buffers have no remaining bytes. -
hashUncommon
Computes and returns the hash of the given merkle nodes. -
hashUncommon
public static byte[] hashUncommon(ByteBuffer leftInternal, ByteBuffer rightLeaf, MessageDigest digest) Computes and returns the hash of the given merkle nodes. On return, the given buffers have no remaining bytes. -
root
-
hash
public final byte[] hash()Returns the hash of the root of the tree. -
getHashAlgo
Returns the hashing algorithm that the tree's internal nodes use. The data for internal nodes (level > 0) is a hash whose value is computed using this algorithm. (The data for leaf nodes can be anything and is independent of this algorithm.)- Returns:
- a name compatible with MessageDigest.getInstance(String)
-
idx
-
data
public abstract byte[] data(int level, int index) Returns [a copy of] the data for the node at the specified coordinates. For internal nodes, this is just the node's hash, which is computed from the hash of its child nodes; for leaf nodes, the node's data may generally be anything, but more often than not it's the hash of a source object.- See Also:
-
leafWidth
public abstract int leafWidth()Returns the leaf width in bytes if fixed; -1, otherwise (variable). -
isLeafWidthFixed
public final boolean isLeafWidthFixed()Determines whether the width of the leaves is fixed.- Returns:
trueiff the leaf width is not variable
-
isOmniWidth
public final boolean isOmniWidth()Determines whether the width of the leaves is the same as the byte width of the hashing algorithm. -
hashAlgoWidth
public final int hashAlgoWidth()Returns the byte width of the hashing algorithm by observing the width of the node at coordinates (1, 0), which is the first internal node constructed in a tree. -
proof
Returns a cryptographic path to the root of the Merkle tree. -
toString
-
appendToStringDetail
-