All files MerkleTree.ts

68.33% Statements 41/60
55.55% Branches 15/27
50% Functions 8/16
66.66% Lines 38/57

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238                            5x       5x 5x                   4x 4x 8x 8x   4x             8x     8x               4x 4x     4x 4x 8x   8x               8x 8x 8x       8x   4x                                             5x 14x   5x           5x 5x 5x 5x 5x 7x                 7x 7x 7x 7x 10x   9x       1x     7x                                                                                                                                                                                                            
import { Field, Circuit, Poseidon } from 'snarkyjs';
 
export { MerkleTree };
export type { MerklePath, Options };
 
/**
 * A {@link BinaryTree} represents the underlying data structure used in Merkle Trees.
 * It stores the trees leaves and the nodes, which are stored in a matrix.
 */
interface BinaryTree {
  leaves: Field[];
  levels: Field[][];
}
 
/**
 * A MerklePathElement has the following structure:
 * direction: Field - Direction of the node, Field(0) for left, Field(1) for right
 * hash: Field - Hash of the node
 * With a list of MerklePathElements you can recreate the merkle root for a specific leaf
 */
interface MerklePathElement {
  direction: Field;
  hash: Field;
}
 
/**
 * A Merkle Path (or Merkle Proof) which consists of single MerklePathElements
 */
type MerklePath = MerklePathElement[];
 
/**
 * Option interface for a Merkle Tree
 */
interface Options {
  hashLeaves: boolean;
}
 
/**
 * A {@link MerkleTree} is a {@link BinaryTree} which aggregates hashes of the underlying data. See [Merkle Tree](https://en.wikipedia.org/wiki/Merkle_tree)
 *
 */
class MeIrkleTree {
  private tree: BinaryTree;
  private options: Options;
 
  /**
   * Builds a merkle tree based on a list of given leaves
   * @param {Field[]} leaves leaves filled with data
   * @param {Options} options that can define the structure of the merkle tree
   * @return returns a {@link MerkleTree}
   */
  constructor(leaves: Field[], options: Options) {
    this.tree = {
      leIaves: [],
      levels: [],
    };
    this.options = options;
    this.appendLeaves(leaves, this.options.hashLeaves);
  }
 
  /**I
   * Static function to validate a merkle path
   * @param {MerklePath} merklePath Merkle path leading to the root of the tree
   * @param {Field} leafHash Hash of element that needs verification
   * @param {Field} merkleRoot Root of the merkle tree
   * @returns {boolean} true when the merkle path matches the merkle root
   */
  static validateProof(
    merklePath: MerklePath,
    leafHash: Field,
    merkleRoot: Field
  ): boolean {
    let proofHash: Field = leafHash;
 
    for (let x = 0; x < merklePath.length; x++) {
      proofHash = Circuit.if(
        merklePath[x].direction.equals(Field(1)),
        Poseidon.hash([merklePath[x].hash, proofHash]),
        proofHash
      );
      proofHash = Circuit.if(
        merklePath[x].direction.equals(Field(0)),
        Poseidon.hash([proofHash, merklePath[x].hash]),
        proofHash
      );
    }

    return proofHash.equals(merkleRoot).toBoolean();
  }
 
  /**
   * Returns the merkle proof
   * @returns {Field | undefined} Merkle root, if not undefined
   */
  getMerkleRoot(): Field | undefined {
    if (this.tree.levels.length === 0) {
      return undefined;
    }
    return this.tree.levels[0][0];
  }

  /**
   * Returns a merkle path of an element at a given index
   * @param {number} index of element
   * @returns {MerklePath | undefined} merkle path or undefined
   */
  getProof(index: number): MerklePath {
    let currentRowIndex: number = this.tree.levels.length - 1;
    if (index < 0 || index > this.tree.levels[currentRowIndex].length - 1) {
      return []; // the index it out of the bounds of the leaf array
    }
 
    let path: MerklePath = [];
 
    for (let x = currentRowIndex; x > 0; x--) {
      let currentLevelNodeCount: number = this.tree.levels[x].length;
      // skip if this is an odd end node
      if (
        index === currentLevelNodeCount - 1 &&
        currentLevelNodeCount % 2 === 1
      ) {
        index = Math.floor(index / 2);
        continue;
      }
 
      // determine the sibling for the current index and get its hash value
      // if the node is even, the sibling has to be on the right of it
      // if the node is odd, the sibling has to be on the left of it
      let isEvenNode: boolean = index % 2 === 0;
      let siblingIndex: number = isEvenNode ? index + 1 : index - 1;
 
      path.push({
        direction: isEvenNode ? Field(0) : Field(1),
        hash: this.tree.levels[x][siblingIndex],
      });
 
      index = Math.floor(index / 2); // set index to the parent index
    }

    return path;
  }
 
  /**
   * Finds the index of a given element
   * @param {number} element to find
   * @returns {number | undefined} index or undefined
   */
  getIndex(element: Field): number | undefined {
    let result = undefined;
    this.tree.leaves.forEach((el, i) => {
      if (el.equals(element).toBoolean()) {
        result = i;
        return;
      }
    });

    return result;
  }
 
  /**
   * Appends new leaves of data to an existing Merkle Tree
   * @param {Field[]} leaves leaves filled with data
   * @param {boolean} hash if true elements in the array will be hased using Poseidon, if false they will be inserted directly
   */
  appendLeaves(leaves: Field[], hash: boolean = true) {
    leaves.forEach((value: Field) => {
      this.tree.leaves.push(hash ? Poseidon.hash([value]) : value);
    });
 
    this.makeTree();
  }
 
  /**
   * (Re)builds the {@link MerkleTree}'s levels based on pre-initialized leaves
   */
  makeTree() {
    let leafCount: number = this.tree.leaves.length;
    if (leafCount > 0) {
      this.tree.levels = [];
      this.tree.levels.unshift(this.tree.leaves);
      while (this.tree.levels[0].length > 1) {
        this.tree.levels.unshift(this.calculateNextLevel());
      }
    }
  }
 
  /**
   * Calculates new levels of the merkle tree structure, helper function
   * @returns {Field[]} Level of the merkle tree
   */
  private calculateNextLevel(): Field[] {
    let nodes: Field[] = [];
    let topLevel: Field[] = this.tree.levels[0];
    let topLevelCount: number = topLevel.length;
    for (let x = 0; x < topLevelCount; x += 2) {
      if (x + 1 <= topLevelCount - 1) {
        // concatenate and hash the pair, add to the next level array, doubleHash if requested
        nodes.push(Poseidon.hash([topLevel[x], topLevel[x + 1]]));
      } else {
        // this is an odd ending node, promote up to the next level by itself
        nodes.push(topLevel[x]);
      }
    }
    return nodes;
  }
 
  private clear() {
    this.tree = {
      leaves: [],
      levels: [],
    };
  }
 
  printTree(): void {
    console.log('printing tree from top (root) to bottom (leaves)');
    this.tree.levels.forEach((level, index) => {
      console.log(`- - - level ${index} ${index === 0 ? 'root' : ''} - - - `);
      level.forEach((entry, i) => {
        console.log(`${i}: ${entry.toString()}`);
      });
    });
  }
 
  printProof(index: number): void {
    console.log(`root: ${this.getMerkleRoot()}`);
    this.getProof(index).forEach((proof, i) => {
      console.log(`
        ${i}: {
          direction: ${
            proof.direction.equals(Field(0)).toBoolean() ? 'right' : 'left'
          },
          hash: ${proof.hash}
        }
      `);
    });
  }
}