An IMT (aka Incremental Merkle Tree) is a type of data structure used in cryptography and computer science for efficiently verifying the integrity of a large set of data, especially in situations where new data is added over time. It is based on the concept of a Merkle tree, and its key feature is its ability to efficiently update the tree when new data is added or existing data is modified. In this implementation, the tree is constructed using a fixed IMT#depth value, and a list of IMT#zeroes (one for each level) is used to compute the hash of a node when not all of its children are defined. The number of children for each node can also be specified with the IMT#arity parameter.

Constructors

  • It initializes the tree with an hash function, the depth, the zero value to use for zeroes and the arity (i.e. the number of children for each node). It also takes an optional parameter to initialize the tree with a list of leaves.

    Parameters

    • hash: IMTHashFunction

      The hash function used to create nodes.

    • depth: number

      The tree depth.

    • zeroValue: any

      The zero value used to create zeroes.

    • arity: number = 2

      The number of children for each node.

    • leaves: any[] = []

      The list of initial leaves.

    Returns IMT

Properties

_arity: number

The number of children per node.

_depth: number

The depth of the tree, which is the number of edges from the node to the tree's root node.

The hash function used to compute the tree nodes.

_nodes: any[][]

The matrix where all the tree nodes are stored. The first index indicates the level of the tree, while the second index represents the node's position within that specific level.

_zeroes: any[]

A list of zero values calculated during the initialization of the tree. The list contains one value for each level of the tree, and the value for a given level is equal to the hash of the previous level's value. The first value is the zero hash provided by the user. These values are used to calculate the hash of a node in case some of its children are missing.

Accessors

  • get arity(): number
  • The number of children per node.

    Returns number

    The number of children per node.

  • get depth(): number
  • The depth of the tree, which equals the number of levels - 1.

    Returns number

    The depth of the tree.

  • get leaves(): any[]
  • The leaves of the tree. They can be retrieved from the first level of the tree using IMT#_nodes. The returned value is a copy of the array and not the original object.

    Returns any[]

    The list of tree leaves.

  • get root(): any
  • The root of the tree. This value doesn't need to be stored as it is always the first and unique element of the last level of the tree. Its value can be retrieved in IMT#_nodes.

    Returns any

    The root hash of the tree.

  • get zeroes(): any[]
  • The list of zero values calculated during the initialization of the tree.

    Returns any[]

    The list of pre-computed zeroes.

Methods

  • It creates a IMTMerkleProof for a leaf of the tree. That proof can be verified by this tree using the same hash function.

    Parameters

    • index: number

      The index of the leaf for which a Merkle proof will be generated.

    Returns IMTMerkleProof

    The Merkle proof of the leaf.

  • It deletes a leaf from the tree. It does not remove the leaf from the data structure, but rather it sets the leaf to be deleted to the zero value.

    Parameters

    • index: number

      The index of the leaf to be deleted.

    Returns void

  • It returns the index of a leaf. If the leaf does not exist it returns -1.

    Parameters

    • leaf: any

      A leaf of the tree.

    Returns number

    The index of the leaf.

  • The leaves are inserted incrementally. If 'i' is the index of the last leaf, the new one will be inserted at position 'i + 1'. Every time a new leaf is inserted, the nodes that separate the new leaf from the root of the tree are created or updated if they already exist, from bottom to top. When a node has only one child (the left one), its value is the hash of that node and the zero value of that level. Otherwise, the hash of the children is calculated.

    Parameters

    • leaf: any

      The new leaf to be inserted in the tree.

    Returns void

  • It updates a leaf in the tree. It's very similar to the IMT#insert function.

    Parameters

    • index: number

      The index of the leaf to be updated.

    • newLeaf: any

      The new leaf to be inserted.

    Returns void

  • It verifies a IMTMerkleProof to confirm that a leaf indeed belongs to a tree. Does not verify that the node belongs to this tree in particular. Equivalent to IMT.verifyProof(proof, this._hash).

    Parameters

    Returns boolean

    True if the leaf is part of the tree, and false otherwise.

  • It verifies a IMTMerkleProof to confirm that a leaf indeed belongs to a tree.

    Parameters

    Returns boolean

    True if the leaf is part of the tree, and false otherwise.