UNPKG

merkle-heap-snarkyjs

Version:

Implementation of a Merkle Heap for SnarkyJS, a framework to develop ZK-snarks on Mina Protocol.

80 lines (79 loc) 2.81 kB
import { Field } from "snarkyjs"; import { MerkleTree } from 'snarkyjs'; export declare class MerkleHeap extends MerkleTree { private nextIndexToAdd; private numberOfNodes; constructor(height: number); private getFatherIndexOfChild; getChildIndexesOfFather(fatherIndex: bigint): { left: null; right: null; } | { left: bigint; right: bigint; }; private getSmallerChildIndexOfFather; private getHeapRoot; private findElementIndex; private downHeap; private upHeap; getMerkleTreeLeaf(index: bigint | null): Field | null; /** * Insert an element into the heap, mantaining the Heap * Property and recalculating the hashes of the Merkle Tree. * @param value that is going to be inserted */ insert(value: Field): void; /** * Delete an arbitrary element of the queue at a given index. * @param index where the element to delete is going to be located * @returns the value that was deleted from the queue. */ deleteElementAtIndex(index: bigint): Field | null; /** * Delete an arbitrary element of the queue with a given value. * @param value that is going to be searched and deleted if it is found. * @returns the value deleted from the queue. */ deleteElement(value: Field): Field | null; /** * Delete the minimum element in the queue. * @returns the min value deleted from the queue. */ deleteMin(): Field | null; /** * Delete the min element of the heap and then insert another element. * It is more efficient than executing a deleteMin and an insert independently. * @param insertValue * @returns the value that was inserted after the deleteMin */ deleteMinThenInsert(insertValue: Field): Field; /** * Insert an element into the heap and then extract the root of the tree. * It is more efficient than executing an insert and a deleteMin independently. * @param insertValue * @returns the min value deleted from the queue. */ insertThenDeleteMin(insertValue: Field): Field; /** * Search if a value is part of the queue * @param value to search. * @returns true if the value is in the queue or false otherwise. */ inQueue(value: Field): boolean; /** * @returns the min element of the queue without deleting it. */ findMin(): Field | null; /** * Find an arbitrary element in the heap without deleting it. * @param valueToFind * @returns the element found or null if it doesn't exist. */ findElement(valueToFind: Field): Field | null; /** * // TODO: We are going to implement this? * @returns the max element of the queue without deleting it. */ findMax(): Field; }