Skip to main content

IndexedMerkleMap

function IndexedMerkleMap(height: number): typeof IndexedMerkleMapBase

Class factory for an Indexed Merkle Map with a given height.

class MerkleMap extends IndexedMerkleMap(33) {}

let map = new MerkleMap();

map.insert(2n, 14n);
map.insert(1n, 13n);

let x = map.get(2n); // 14

Indexed Merkle maps can be used directly in provable code:

ZkProgram({
methods: {
test: {
privateInputs: [MerkleMap.provable, Field],

method(map: MerkleMap, key: Field) {
// get the value associated with `key`
let value = map.getOption(key).orElse(0n);

// increment the value by 1
map.set(key, value.add(1));
}
}
}
})

Initially, every IndexedMerkleMap is populated by a single key-value pair: (0, 0). The value for key 0 can be updated like any other. When keys and values are hash outputs, (0, 0) can serve as a convenient way to represent a dummy update to the tree, since 0 is not effciently computable as a hash image, and this update doesn't affect the Merkle root.

Parameters

height: number

Returns

typeof IndexedMerkleMapBase

Source

index.ts:156