MerkleTree
Class: MerkleTree
A Merkle Tree is a binary tree in which every leaf is the cryptography hash of a piece of data, and every node is the hash of the concatenation of its two child nodes.
A Merkle Tree allows developers to easily and securely verify the integrity of large amounts of data.
Take a look at our documentation on how to use Merkle Trees in combination with zkApps and zero knowledge programming!
Levels are indexed from leaves (level 0) to root (level N - 1).
Table of contents
Constructors
Properties
Accessors
Methods
Constructors
constructor
• new MerkleTree(height
)
Creates a new, empty Merkle Tree.
Parameters
Name | Type | Description |
---|---|---|
height | number | The height of Merkle Tree. |
Defined in
Properties
height
• Readonly
height: number
The height of Merkle Tree.
Defined in
nodes
• Private
nodes: Record
\<number
, Record
\<string
, Field
>> = {}
Defined in
zeroes
• Private
zeroes: Field
[]
Defined in
Accessors
leafCount
• get
leafCount(): bigint
Returns the amount of leaf nodes.
Returns
bigint
Amount of leaf nodes.
Defined in
Methods
fill
▸ fill(leaves
): void
Fills all leaves of the tree.
Parameters
Name | Type | Description |
---|---|---|
leaves | Field [] | Values to fill the leaves with. |
Returns
void
Defined in
getNode
▸ getNode(level
, index
): Field
Returns a node which lives at a given index and level.
Parameters
Name | Type | Description |
---|---|---|
level | number | Level of the node. |
index | bigint | Index of the node. |
Returns
The data of the node.
Defined in
getRoot
▸ getRoot(): Field
Returns the root of the Merkle Tree.
Returns
The root of the Merkle Tree.
Defined in
getWitness
▸ getWitness(index
): Witness
Returns the witness (also known as Merkle Proof or Merkle Witness) for the leaf at the given index.
Parameters
Name | Type | Description |
---|---|---|
index | bigint | Position of the leaf node. |
Returns
Witness
The witness that belongs to the leaf.
Defined in
setLeaf
▸ setLeaf(index
, leaf
): void
Sets the value of a leaf node at a given index to a given value.
Parameters
Name | Type | Description |
---|---|---|
index | bigint | Position of the leaf node. |
leaf | Field | New value. |
Returns
void
Defined in
setNode
▸ Private
setNode(level
, index
, value
): void
Parameters
Name | Type |
---|---|
level | number |
index | bigint |
value | Field |
Returns
void
Defined in
validate
▸ validate(index
): boolean
Checks if the witness that belongs to the leaf at the given index is a valid witness.
Parameters
Name | Type | Description |
---|---|---|
index | bigint | Position of the leaf node. |
Returns
boolean
True if the witness for the leaf node is valid.