UNPKG

@zk-kit/incremental-merkle-tree

Version:
10 lines (9 loc) 4.18 kB
/** * @module @zk-kit/incremental-merkle-tree * @version 1.1.0 * @file Incremental Merkle tree implementation in TypeScript. * @copyright Cedoor 2023 * @license MIT * @see [Github]{@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/incremental-merkle-tree} */ var zkKitIncrementalMerkleTree=function(e){"use strict";function t(e,t){for(var r=[],o=2;o<arguments.length;o++)r[o-2]=arguments[o];if(void 0===e)throw new TypeError("Parameter '".concat(t,"' is not defined"));if(!r.includes(typeof e))throw new TypeError("Parameter '".concat(t,"' is none of these types: ").concat(r.join(", ")))}function r(e,r,o,n,i,s,h){if(t(e,"index","number"),e<0||e>=i[0].length)throw new Error("The leaf does not exist in this tree");for(var a=r,f=0;f<o;f+=1){var u=e-e%n,p=u+n,l=[];i[f][e]=a;for(var c=u;c<p;c+=1)c<i[f].length?l.push(i[f][c]):l.push(s[f]);a=h(l),e=Math.floor(e/n)}return a}var o=function(){function e(r,o,n,i,s){var h;if(void 0===i&&(i=2),void 0===s&&(s=[]),t(r,"hash","function"),t(o,"depth","number"),t(n,"zeroValue","number","string","bigint"),t(i,"arity","number"),t(s,"leaves","object"),o<1||o>e.maxDepth)throw new Error("The tree depth must be between 1 and 32");if(s.length>Math.pow(i,o))throw new Error("The tree cannot contain more than ".concat(Math.pow(i,o)," leaves"));this._hash=r,this._depth=o,this._zeroes=[],this._nodes=[],this._arity=i;for(var a=0;a<o;a+=1)this._zeroes.push(n),this._nodes[a]=[],n=r(Array(this._arity).fill(n));if(this._nodes[o]=[],s.length>0){this._nodes[0]=s;for(a=0;a<o;a+=1)for(var f=0;f<Math.ceil(this._nodes[a].length/i);f+=1){for(var u=f*i,p=[],l=0;l<i;l+=1)p.push(null!==(h=this._nodes[a][u+l])&&void 0!==h?h:this.zeroes[a]);this._nodes[a+1][f]=r(p)}}else this._nodes[o][0]=n;Object.freeze(this._zeroes),Object.freeze(this._nodes)}return Object.defineProperty(e.prototype,"root",{get:function(){return this._nodes[this.depth][0]},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"depth",{get:function(){return this._depth},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"leaves",{get:function(){return this._nodes[0].slice()},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"zeroes",{get:function(){return this._zeroes},enumerable:!1,configurable:!0}),Object.defineProperty(e.prototype,"arity",{get:function(){return this._arity},enumerable:!1,configurable:!0}),e.prototype.indexOf=function(e){return function(e,r){return t(e,"leaf","number","string","bigint"),r[0].indexOf(e)}(e,this._nodes)},e.prototype.insert=function(e){this._nodes[this.depth][0]=function(e,r,o,n,i,s){if(t(e,"leaf","number","string","bigint"),n[0].length>=Math.pow(o,r))throw new Error("The tree is full");for(var h=e,a=n[0].length,f=0;f<r;f+=1){var u=a-a%o,p=u+o,l=[];n[f][a]=h;for(var c=u;c<p;c+=1)c<n[f].length?l.push(n[f][c]):l.push(i[f]);h=s(l),a=Math.floor(a/o)}return h}(e,this.depth,this.arity,this._nodes,this.zeroes,this._hash)},e.prototype.delete=function(e){this._nodes[this.depth][0]=r(e,this.zeroes[0],this.depth,this.arity,this._nodes,this.zeroes,this._hash)},e.prototype.update=function(e,t){this._nodes[this.depth][0]=r(e,t,this.depth,this.arity,this._nodes,this.zeroes,this._hash)},e.prototype.createProof=function(e){return function(e,r,o,n,i,s){if(t(e,"index","number"),e<0||e>=n[0].length)throw new Error("The leaf does not exist in this tree");for(var h=[],a=[],f=e,u=0;u<r;u+=1){var p=e%o,l=e-p,c=l+o;a[u]=p,h[u]=[];for(var d=l;d<c;d+=1)d!==e&&(d<n[u].length?h[u].push(n[u][d]):h[u].push(i[u]));e=Math.floor(e/o)}return{root:s,leaf:n[0][f],pathIndices:a,siblings:h}}(e,this.depth,this.arity,this._nodes,this.zeroes,this.root)},e.prototype.verifyProof=function(e){return function(e,r){t(e,"proof","object"),t(e.root,"proof.root","number","string","bigint"),t(e.leaf,"proof.leaf","number","string","bigint"),t(e.siblings,"proof.siblings","object"),t(e.pathIndices,"proof.pathElements","object");for(var o=e.leaf,n=0;n<e.siblings.length;n+=1){var i=e.siblings[n].slice();i.splice(e.pathIndices[n],0,o),o=r(i)}return e.root===o}(e,this._hash)},e.maxDepth=32,e}();return e.IncrementalMerkleTree=o,Object.defineProperty(e,"__esModule",{value:!0}),e}({});