Class Tree

java.lang.Object
io.crums.util.mrkl.Tree
Direct Known Subclasses:
DeltaTree, FixedLeafTree, FreeLeafTree

public abstract class Tree extends Object
The Merkle tree. Instances are immutable. This class abstracts away the memory layout for the tree.
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final byte
     
    static final byte
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Tree(int leaves, String algo)
    Base constructor creates an index and locks down the algo.
    protected
    Tree(Tree copy)
    Copy constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected StringBuilder
     
    abstract byte[]
    data(int level, int index)
    Returns [a copy of] the data for the node at the specified coordinates.
    final String
    Returns the hashing algorithm that the tree's internal nodes use.
    final byte[]
    Returns the hash of the root of the tree.
    final int
    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.
    static byte[]
    hashInternals(byte[] left, byte[] right, MessageDigest digest)
     
    static byte[]
    Computes and returns the hash of the given merkle nodes.
    static byte[]
    hashLeaves(byte[] left, byte[] right, MessageDigest digest)
     
    static byte[]
    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 boolean
    Determines whether the width of the leaves is fixed.
    final boolean
    Determines whether the width of the leaves is the same as the byte width of the hashing algorithm.
    abstract int
    Returns the leaf width in bytes if fixed; -1, otherwise (variable).
    final Proof
    proof(int leafIndex)
    Returns a cryptographic path to the root of the Merkle tree.
    final Node
    Returns the root node of the tree.
    For debug use.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • Tree

      protected Tree(int leaves, String algo)
      Base constructor creates an index and locks down the algo.
      Parameters:
      leaves - the number of leaf nodes in the tree
      algo - the hash algo used for the trees internal nodes.
    • Tree

      protected Tree(Tree copy)
      Copy constructor.
  • Method Details

    • hashLeaves

      public static byte[] hashLeaves(byte[] left, byte[] right, MessageDigest digest)
    • hashLeaves

      public static byte[] hashLeaves(ByteBuffer left, ByteBuffer right, MessageDigest digest)
      Computes and returns the hash of the given leaf nodes. On return, the given buffers have no remaining bytes.
    • hashInternals

      public static byte[] hashInternals(byte[] left, byte[] right, MessageDigest digest)
    • hashInternals

      public static byte[] hashInternals(ByteBuffer left, ByteBuffer right, MessageDigest digest)
      Computes and returns the hash of the given merkle nodes. On return, the given buffers have no remaining bytes.
    • hashUncommon

      public static byte[] hashUncommon(byte[] leftInternal, byte[] rightLeaf, MessageDigest digest)
      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

      public final Node root()
      Returns the root node of the tree.
      See Also:
    • hash

      public final byte[] hash()
      Returns the hash of the root of the tree.
    • getHashAlgo

      public final String 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

      public final TreeIndex<Node> idx()
      Returns the random access index into tree structure.
    • 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:
      true iff 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

      public final Proof proof(int leafIndex)
      Returns a cryptographic path to the root of the Merkle tree.
    • toString

      public String toString()
      For debug use.
      Overrides:
      toString in class Object
    • appendToStringDetail

      protected StringBuilder appendToStringDetail(StringBuilder string)