Class Proof

java.lang.Object
io.crums.util.mrkl.Proof

public class Proof
extends java.lang.Object
A cryptographic path from an item (expressed in bytes) to the root of a Merkle tree. Note although instances are immutable, a reference to one does not imply a verified proof. For such a guarantee, considering defining a subclass.
See Also:
hashChain(), verify(MessageDigest)
  • Constructor Summary

    Constructors
    Modifier Constructor Description
    protected Proof​(Proof copy)
    Copy constructor.
      Proof​(Tree tree, int leafIndex)  
      Proof​(java.lang.String algo, int leafCount, int leafIndex, byte[][] chain)  
      Proof​(java.lang.String algo, int leafCount, int leafIndex, byte[][] chain, boolean copy)  
  • Method Summary

    Modifier and Type Method Description
    static int chainLength​(int leafCount, int leafIndex)  
    boolean equals​(java.lang.Object o)  
    java.lang.String getHashAlgo()  
    java.util.List<byte[]> hashChain()
    Returns the hash chain.
    int hashCode()  
    byte[] item()
    Returns [a copy of] the item proven.
    int leafCount()
    Returns the total number of leaves in the tree from which this proof was constructed.
    int leafIndex()
    The [leaf] index of the item proven.
    byte[] rootHash()
    Returns[a copy of] the hash at the root of the Merkle tree.
    boolean verify​(java.security.MessageDigest digest)
    Verifies this proof and returns the result.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Proof

      public Proof​(Tree tree, int leafIndex) throws java.lang.IndexOutOfBoundsException
      Throws:
      java.lang.IndexOutOfBoundsException
    • Proof

      public Proof​(java.lang.String algo, int leafCount, int leafIndex, byte[][] chain)
    • Proof

      public Proof​(java.lang.String algo, int leafCount, int leafIndex, byte[][] chain, boolean copy)
    • Proof

      protected Proof​(Proof copy)
      Copy constructor.
  • Method Details

    • verify

      public final boolean verify​(java.security.MessageDigest digest)
      Verifies this proof and returns the result.
    • chainLength

      public static int chainLength​(int leafCount, int leafIndex)
    • equals

      public final boolean equals​(java.lang.Object o)
      Overrides:
      equals in class java.lang.Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class java.lang.Object
    • leafIndex

      public final int leafIndex()
      The [leaf] index of the item proven.
    • leafCount

      public final int leafCount()
      Returns the total number of leaves in the tree from which this proof was constructed. The number of leaves determines the structure of the tree, which in turn governs the validity of the proof.
    • getHashAlgo

      public final java.lang.String getHashAlgo()
    • hashChain

      public final java.util.List<byte[]> hashChain()
      Returns the hash chain. The returned list is immutable (both structurally and contents-wise). The first element in the list is the item, the last element is root of the Merkle tree, and the elements in between are siblings on the path to root.

      So the element at index 1 is the first element's (the item's) sibling, the element at index 2 is the sibling of the (implied and calculable) parent of the first 2 elements, the element at index 3 the sibling of the parent of the last element, and so on, until the last child of root.

      Note the returned list does not contain information on its own about the handedness of the nodes (whether they join their siblings from the left or the right); that is established the leaf index and leaf count.

    • rootHash

      public final byte[] rootHash()
      Returns[a copy of] the hash at the root of the Merkle tree.
    • item

      public final byte[] item()
      Returns [a copy of] the item proven.