Skip to main content

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).

Constructors

new MerkleTree()

new MerkleTree(height: number): MerkleTree

Creates a new, empty Merkle Tree.

Parameters

height: number

The height of Merkle Tree.

Returns

MerkleTree

A new MerkleTree

Source

lib/provable/merkle-tree.ts:37

Properties

height

readonly height: number;

The height of Merkle Tree.

Source

lib/provable/merkle-tree.ts:37


nodes

nodes: Record<number, Record<string, Field>> = {};

Source

lib/provable/merkle-tree.ts:29


zeroes

zeroes: Field[];

Source

lib/provable/merkle-tree.ts:30

Accessors

leafCount

get leafCount(): bigint

Returns the amount of leaf nodes.

Returns

bigint

Amount of leaf nodes.

Source

lib/provable/merkle-tree.ts:146

Methods

fill()

fill(leaves: Field[]): void

Fills all leaves of the tree.

Parameters

leaves: Field[]

Values to fill the leaves with.

Returns

void

Source

lib/provable/merkle-tree.ts:136


getNode()

getNode(level: number, index: bigint): Field

Returns a node which lives at a given index and level.

Parameters

level: number

Level of the node.

index: bigint

Index of the node.

Returns

Field

The data of the node.

Source

lib/provable/merkle-tree.ts:51


getRoot()

getRoot(): Field

Returns the root of the Merkle Tree.

Returns

Field

The root of the Merkle Tree.

Source

lib/provable/merkle-tree.ts:59


getWitness()

getWitness(index: bigint): Witness

Returns the witness (also known as Merkle Proof or Merkle Witness) for the leaf at the given index.

Parameters

index: bigint

Position of the leaf node.

Returns

Witness

The witness that belongs to the leaf.

Source

lib/provable/merkle-tree.ts:97


setLeaf()

setLeaf(index: bigint, leaf: Field): void

Sets the value of a leaf node at a given index to a given value.

Parameters

index: bigint

Position of the leaf node.

leaf: Field

New value.

Returns

void

Source

lib/provable/merkle-tree.ts:74


validate()

validate(index: bigint): boolean

Checks if the witness that belongs to the leaf at the given index is a valid witness.

Parameters

index: bigint

Position of the leaf node.

Returns

boolean

True if the witness for the leaf node is valid.

Source

lib/provable/merkle-tree.ts:119