UNPKG

@toruslabs/session-manager

Version:
2 lines 181 kB
/*! For license information please see sessionManager.umd.min.js.LICENSE.txt */ !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.SessionManager=e():t.SessionManager=e()}(self,(()=>(()=>{var t={181:(t,e,r)=>{"use strict";r.d(e,{X:()=>l});var i=r(705);function n(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,i)}return r}function f(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?n(Object(r),!0).forEach((function(e){(0,i.A)(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):n(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var o=r(744),a=r.n(o),s=r(65);const d=r.n(s)().getLogger("http-helpers");d.setLevel(s.levels.INFO);let c="torus-default";async function h(t,e){let r=null;try{r=new URL(t)}catch(t){}return fetch(t,e)}function u(){const t={};return c&&(t["x-api-key"]=c),t}function b(t){d.info(`Response: ${t.status} ${t.statusText}`),d.info(`Url: ${t.url}`)}class l{constructor(){(0,i.A)(this,"sessionId",void 0)}checkSessionParams(){if(!this.sessionId)throw new Error("Session id is required");this.sessionId=this.sessionId.padStart(64,"0")}request({method:t="GET",url:e,data:r={},headers:i={}}){const n={headers:i};switch(t){case"GET":return(async(t,e={},r={})=>{const i={mode:"cors",headers:{}};r.useAPIKey&&(i.headers=f(f({},i.headers),u())),e.method="GET";const n=a()(i,e),o=await h(t,n);if(o.ok){const t=o.headers.get("content-type");return null!=t&&t.includes("application/json")?o.json():o.text()}throw b(o),o})(e,n);case"POST":return((t,e={},r={},i={})=>{const n={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};i.useAPIKey&&(n.headers=f(f({},n.headers),u())),r.method="POST";const o=a()(n,r);return i.isUrlEncodedData?(o.body=e,"application/json; charset=utf-8"===o.headers["Content-Type"]&&delete o.headers["Content-Type"]):o.body=JSON.stringify(e),(async(t,e)=>{let r=null;try{const i=new Promise(((e,i)=>{r=setTimeout((()=>{i(new Error(`Timed out in ${t}ms`))}),t)})),n=await Promise.race([e,i]);return null!=r&&clearTimeout(r),n}catch(t){throw null!=r&&clearTimeout(r),t}})(i.timeout||6e4,h(t,o).then((t=>{if(i.logTracingHeader&&function(t){const e=t.headers.get("x-web3-correlation-id");e&&d.info(`Request tracing with traceID = ${e}`)}(t),t.ok){const e=t.headers.get("content-type");return null!=e&&e.includes("application/json")?t.json():t.text()}throw b(t),t})))})(e,r,n);case"PUT":return(async(t,e={},r={},i={})=>{const n={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};i.useAPIKey&&(n.headers=f(f({},n.headers),u())),r.method="PUT";const o=a()(n,r);i.isUrlEncodedData?(o.body=e,"application/json; charset=utf-8"===o.headers["Content-Type"]&&delete o.headers["Content-Type"]):o.body=JSON.stringify(e);const s=await h(t,o);if(s.ok){const t=s.headers.get("content-type");return null!=t&&t.includes("application/json")?s.json():s.text()}throw b(s),s})(e,r,n);case"PATCH":return(async(t,e={},r={},i={})=>{const n={mode:"cors",headers:{"Content-Type":"application/json; charset=utf-8"}};i.useAPIKey&&(n.headers=f(f({},n.headers),u())),r.method="PATCH";const o=a()(n,r);i.isUrlEncodedData?(o.body=e,"application/json; charset=utf-8"===o.headers["Content-Type"]&&delete o.headers["Content-Type"]):o.body=JSON.stringify(e);const s=await h(t,o);if(s.ok){const t=s.headers.get("content-type");return null!=t&&t.includes("application/json")?s.json():s.text()}throw b(s),s})(e,r,n)}throw new Error("Invalid method type")}}},278:()=>{},267:(t,e,r)=>{"use strict";r.d(e,{C:()=>ut});var i=r(705),n=r(729),f=r(668).hp;const o=new n.ec("secp256k1"),a=globalThis.crypto||globalThis.msCrypto||{},s=a.subtle||a.webkitSubtle,d=f.from("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141","hex"),c=f.alloc(32,0);function h(t,e){if(!t)throw new Error(e||"Assertion failed")}function u(t){return e=t,!(!f.isBuffer(e)||32!==e.length)&&t.compare(c)>0&&t.compare(d)<0;var e}function b(t){if(void 0===a.getRandomValues)return f.from(a.randomBytes(t));const e=new Uint8Array(t);return a.getRandomValues(e),f.from(e)}async function l(t){if(!a.createHash){const e=await s.digest("SHA-512",t);return new Uint8Array(e)}const e=a.createHash("sha512").update(t).digest();return new Uint8Array(e)}function p(t){return async function(e,r,i){if(s&&s[t]&&s.importKey){const n={name:"AES-CBC"},o=await s.importKey("raw",r,n,!1,[t]),a={name:"AES-CBC",iv:e},d=await s[t](a,o,i);return f.from(new Uint8Array(d))}if("encrypt"===t&&a.createCipheriv){const t=a.createCipheriv("aes-256-cbc",r,e),n=t.update(i),o=t.final();return f.concat([n,o])}if("decrypt"===t&&a.createDecipheriv){const t=a.createDecipheriv("aes-256-cbc",r,e),n=t.update(i),o=t.final();return f.concat([n,o])}throw new Error(`Unsupported operation: ${t}`)}}const m=p("encrypt"),y=p("decrypt");async function g(t,e){if(!a.createHmac){const r={name:"HMAC",hash:{name:"SHA-256"}},i=await s.importKey("raw",new Uint8Array(t),r,!1,["sign","verify"]),n=await s.sign("HMAC",i,e);return f.from(new Uint8Array(n))}const r=a.createHmac("sha256",f.from(t));return r.update(e),r.digest()}const v=function(t){return h(32===t.length,"Bad private key"),h(u(t),"Bad private key"),f.from(o.keyFromPrivate(t).getPublic("array"))},w=async function(t,e){return h(32===t.length,"Bad private key"),h(u(t),"Bad private key"),h(e.length>0,"Message should not be empty"),h(e.length<=32,"Message is too long"),f.from(o.sign(e,t,{canonical:!0}).toDER())},M=async function(t,e){h(f.isBuffer(t),"Bad private key"),h(f.isBuffer(e),"Bad public key"),h(32===t.length,"Bad private key"),h(u(t),"Bad private key"),h(65===e.length||33===e.length,"Bad public key"),65===e.length&&h(4===e[0],"Bad public key"),33===e.length&&h(2===e[0]||3===e[0],"Bad public key");const r=o.keyFromPrivate(t),i=o.keyFromPublic(e),n=r.derive(i.getPublic());return f.from(n.toArray())},S=async function(t,e){h(f.isBuffer(t),"Bad private key"),h(f.isBuffer(e),"Bad public key"),h(32===t.length,"Bad private key"),h(u(t),"Bad private key"),h(65===e.length||33===e.length,"Bad public key"),65===e.length&&h(4===e[0],"Bad public key"),33===e.length&&h(2===e[0]||3===e[0],"Bad public key");const r=o.keyFromPrivate(t),i=o.keyFromPublic(e),n=r.derive(i.getPublic());return f.from(n.toString(16,64),"hex")},A=async function(t,e,r){const i=null!=r&&r,n=i?S:M,o=await n(t,e.ephemPublicKey),a=await l(o),s=a.slice(0,32),d=a.slice(32),c=f.concat([e.iv,e.ephemPublicKey,e.ciphertext]),h=await async function(t,e,r){return function(t,e){if(t.length!==e.length)return!1;let r=0;for(let i=0;i<t.length;i++)r|=t[i]^e[i];return 0===r}(await g(t,e),r)}(f.from(d),c,e.mac);if(!h&&!1===i)return A(t,e,!0);if(!h&&!0===i)throw new Error("bad MAC after trying padded");const u=await y(e.iv,f.from(s),e.ciphertext);return f.from(new Uint8Array(u))};function _(t){if(!Number.isSafeInteger(t)||t<0)throw new Error(`positive integer expected, not ${t}`)}function I(t,...e){if(!((r=t)instanceof Uint8Array||null!=r&&"object"==typeof r&&"Uint8Array"===r.constructor.name))throw new Error("Uint8Array expected");var r;if(e.length>0&&!e.includes(t.length))throw new Error(`Uint8Array expected of length ${e}, not of length=${t.length}`)}function x(t,e=!0){if(t.destroyed)throw new Error("Hash instance has been destroyed");if(e&&t.finished)throw new Error("Hash#digest() has already been called")}function E(t,e){I(t);const r=e.outputLen;if(t.length<r)throw new Error(`digestInto() expects output buffer of length at least ${r}`)}const B={number:_,bool:function(t){if("boolean"!=typeof t)throw new Error(`boolean expected, not ${t}`)},bytes:I,hash:function(t){if("function"!=typeof t||"function"!=typeof t.create)throw new Error("Hash should be wrapped by utils.wrapConstructor");_(t.outputLen),_(t.blockLen)},exists:x,output:E},R=BigInt(2**32-1),k=BigInt(32);function P(t,e=!1){return e?{h:Number(t&R),l:Number(t>>k&R)}:{h:0|Number(t>>k&R),l:0|Number(t&R)}}function O(t,e=!1){let r=new Uint32Array(t.length),i=new Uint32Array(t.length);for(let n=0;n<t.length;n++){const{h:f,l:o}=P(t[n],e);[r[n],i[n]]=[f,o]}return[r,i]}const z=68===new Uint8Array(new Uint32Array([287454020]).buffer)[0];function L(t){for(let r=0;r<t.length;r++)t[r]=(e=t[r])<<24&4278190080|e<<8&16711680|e>>>8&65280|e>>>24&255;var e}function q(t){return"string"==typeof t&&(t=function(t){if("string"!=typeof t)throw new Error("utf8ToBytes expected string, got "+typeof t);return new Uint8Array((new TextEncoder).encode(t))}(t)),I(t),t}class U{clone(){return this._cloneInto()}}const T=[],N=[],j=[],C=BigInt(0),F=BigInt(1),$=BigInt(2),D=BigInt(7),H=BigInt(256),K=BigInt(113);for(let t=0,e=F,r=1,i=0;t<24;t++){[r,i]=[i,(2*r+3*i)%5],T.push(2*(5*i+r)),N.push((t+1)*(t+2)/2%64);let n=C;for(let t=0;t<7;t++)e=(e<<F^(e>>D)*K)%H,e&$&&(n^=F<<(F<<BigInt(t))-F);j.push(n)}const[J,X]=O(j,!0),Z=(t,e,r)=>r>32?((t,e,r)=>e<<r-32|t>>>64-r)(t,e,r):((t,e,r)=>t<<r|e>>>32-r)(t,e,r),V=(t,e,r)=>r>32?((t,e,r)=>t<<r-32|e>>>64-r)(t,e,r):((t,e,r)=>e<<r|t>>>32-r)(t,e,r);class W extends U{constructor(t,e,r,i=!1,n=24){if(super(),this.blockLen=t,this.suffix=e,this.outputLen=r,this.enableXOF=i,this.rounds=n,this.pos=0,this.posOut=0,this.finished=!1,this.destroyed=!1,_(r),0>=this.blockLen||this.blockLen>=200)throw new Error("Sha3 supports only keccak-f1600 function");var f;this.state=new Uint8Array(200),this.state32=(f=this.state,new Uint32Array(f.buffer,f.byteOffset,Math.floor(f.byteLength/4)))}keccak(){z||L(this.state32),function(t,e=24){const r=new Uint32Array(10);for(let i=24-e;i<24;i++){for(let e=0;e<10;e++)r[e]=t[e]^t[e+10]^t[e+20]^t[e+30]^t[e+40];for(let e=0;e<10;e+=2){const i=(e+8)%10,n=(e+2)%10,f=r[n],o=r[n+1],a=Z(f,o,1)^r[i],s=V(f,o,1)^r[i+1];for(let r=0;r<50;r+=10)t[e+r]^=a,t[e+r+1]^=s}let e=t[2],n=t[3];for(let r=0;r<24;r++){const i=N[r],f=Z(e,n,i),o=V(e,n,i),a=T[r];e=t[a],n=t[a+1],t[a]=f,t[a+1]=o}for(let e=0;e<50;e+=10){for(let i=0;i<10;i++)r[i]=t[e+i];for(let i=0;i<10;i++)t[e+i]^=~r[(i+2)%10]&r[(i+4)%10]}t[0]^=J[i],t[1]^=X[i]}r.fill(0)}(this.state32,this.rounds),z||L(this.state32),this.posOut=0,this.pos=0}update(t){x(this);const{blockLen:e,state:r}=this,i=(t=q(t)).length;for(let n=0;n<i;){const f=Math.min(e-this.pos,i-n);for(let e=0;e<f;e++)r[this.pos++]^=t[n++];this.pos===e&&this.keccak()}return this}finish(){if(this.finished)return;this.finished=!0;const{state:t,suffix:e,pos:r,blockLen:i}=this;t[r]^=e,128&e&&r===i-1&&this.keccak(),t[i-1]^=128,this.keccak()}writeInto(t){x(this,!1),I(t),this.finish();const e=this.state,{blockLen:r}=this;for(let i=0,n=t.length;i<n;){this.posOut>=r&&this.keccak();const f=Math.min(r-this.posOut,n-i);t.set(e.subarray(this.posOut,this.posOut+f),i),this.posOut+=f,i+=f}return t}xofInto(t){if(!this.enableXOF)throw new Error("XOF is not possible for this instance");return this.writeInto(t)}xof(t){return _(t),this.xofInto(new Uint8Array(t))}digestInto(t){if(E(t,this),this.finished)throw new Error("digest() was already called");return this.writeInto(t),this.destroy(),t}digest(){return this.digestInto(new Uint8Array(this.outputLen))}destroy(){this.destroyed=!0,this.state.fill(0)}_cloneInto(t){const{blockLen:e,suffix:r,outputLen:i,rounds:n,enableXOF:f}=this;return t||(t=new W(e,r,i,f,n)),t.state32.set(this.state32),t.pos=this.pos,t.posOut=this.posOut,t.finished=this.finished,t.rounds=n,t.suffix=r,t.outputLen=i,t.enableXOF=f,t.destroyed=this.destroyed,t}}const Y=(t,e,r)=>function(t){const e=e=>t().update(q(e)).digest(),r=t();return e.outputLen=r.outputLen,e.blockLen=r.blockLen,e.create=()=>t(),e}((()=>new W(e,t,r))),G=Y(1,144,28),Q=Y(1,136,32),tt=Y(1,104,48),et=Y(1,72,64);function rt(t){return e=>(B.bytes(e),t(e))}B.bool,B.bytes,(()=>{const t="object"==typeof globalThis&&"crypto"in globalThis?globalThis.crypto:void 0,e="undefined"!=typeof module&&"function"==typeof module.require&&module.require.bind(module);e&&!t&&e("crypto")})(),rt(G);const it=(()=>{const t=rt(Q);return t.create=Q.create,t})();rt(tt),rt(et);var nt=r(668).hp;function ft(t){return nt.from(it(t))}const ot=new n.ec("secp256k1");var at=r(668).hp;async function st(t,e){const r=JSON.stringify(e),i=at.from(r,"utf-8"),n=(o=await async function(t,e,r){let i=(r=r||{}).ephemPrivateKey||b(32);for(;!u(i);)i=r.ephemPrivateKey||b(32);const n=v(i),o=await M(i,t),a=await l(o),s=r.iv||b(16),d=a.slice(0,32),c=a.slice(32),h=await m(s,f.from(d),e),p=f.concat([s,n,h]);return{iv:s,ephemPublicKey:n,ciphertext:h,mac:await g(f.from(c),p)}}(v(at.from(t,"hex")),i),{iv:at.from(o.iv).toString("hex"),ephemPublicKey:at.from(o.ephemPublicKey).toString("hex"),ciphertext:at.from(o.ciphertext).toString("hex"),mac:at.from(o.mac).toString("hex")});var o;return JSON.stringify(n)}async function dt(t,e){const r=(f=JSON.parse(e),{iv:at.from(f.iv,"hex"),ephemPublicKey:at.from(f.ephemPublicKey,"hex"),ciphertext:at.from(f.ciphertext,"hex"),mac:at.from(f.mac,"hex")}),i=ot.keyFromPrivate(t),n=(await A(at.from(i.getPrivate().toString("hex",64),"hex"),r)).toString("utf-8");var f;return JSON.parse(n)}var ct=r(181),ht=r(668).hp;class ut extends ct.X{constructor({sessionServerBaseUrl:t,sessionNamespace:e,sessionTime:r,sessionId:n}={}){super(),(0,i.A)(this,"sessionServerBaseUrl","https://session.web3auth.io"),(0,i.A)(this,"sessionNamespace",void 0),(0,i.A)(this,"sessionTime",86400),(0,i.A)(this,"sessionId",""),t&&(this.sessionServerBaseUrl=t),e&&(this.sessionNamespace=e),r&&(this.sessionTime=r),n&&(this.sessionId=n.padStart(64,"0"))}static generateRandomSessionKey(){return function(){let t=b(32);for(;!u(t);)t=b(32);return t}().toString("hex").padStart(64,"0")}async createSession(t){super.checkSessionParams();const e=ht.from(this.sessionId,"hex"),r=v(e).toString("hex"),i=await st(this.sessionId,t),n={key:r,data:i,signature:(await w(e,ft(ht.from(i,"utf8")))).toString("hex"),namespace:this.sessionNamespace,timeout:this.sessionTime};return await super.request({method:"POST",url:`${this.sessionServerBaseUrl}/store/set`,data:n}),this.sessionId}async authorizeSession(){super.checkSessionParams();const t=v(ht.from(this.sessionId,"hex")).toString("hex"),e=new URL(`${this.sessionServerBaseUrl}/store/get`);e.searchParams.append("key",t),this.sessionNamespace&&e.searchParams.append("namespace",this.sessionNamespace);const r=await super.request({url:e.toString()});if(!r.message)throw new Error("Session Expired or Invalid public key");const i=await dt(this.sessionId,r.message);if(i.error)throw new Error("There was an error decrypting data.");return i}async updateSession(t){super.checkSessionParams();const e=ht.from(this.sessionId,"hex"),r=v(e).toString("hex"),i=await st(this.sessionId,t),n={key:r,data:i,signature:(await w(e,ft(ht.from(i,"utf8")))).toString("hex"),namespace:this.sessionNamespace};await super.request({method:"PUT",url:`${this.sessionServerBaseUrl}/store/update`,data:n})}async invalidateSession(){super.checkSessionParams();const t=ht.from(this.sessionId,"hex"),e=v(t).toString("hex"),r=await st(this.sessionId,{}),i={key:e,data:r,signature:(await w(t,ft(ht.from(r,"utf8")))).toString("hex"),namespace:this.sessionNamespace,timeout:1};return await super.request({method:"POST",url:`${this.sessionServerBaseUrl}/store/set`,data:i}),this.sessionId="",!0}}},526:(t,e)=>{"use strict";e.byteLength=function(t){var e=a(t),r=e[0],i=e[1];return 3*(r+i)/4-i},e.toByteArray=function(t){var e,r,f=a(t),o=f[0],s=f[1],d=new n(function(t,e,r){return 3*(e+r)/4-r}(0,o,s)),c=0,h=s>0?o-4:o;for(r=0;r<h;r+=4)e=i[t.charCodeAt(r)]<<18|i[t.charCodeAt(r+1)]<<12|i[t.charCodeAt(r+2)]<<6|i[t.charCodeAt(r+3)],d[c++]=e>>16&255,d[c++]=e>>8&255,d[c++]=255&e;return 2===s&&(e=i[t.charCodeAt(r)]<<2|i[t.charCodeAt(r+1)]>>4,d[c++]=255&e),1===s&&(e=i[t.charCodeAt(r)]<<10|i[t.charCodeAt(r+1)]<<4|i[t.charCodeAt(r+2)]>>2,d[c++]=e>>8&255,d[c++]=255&e),d},e.fromByteArray=function(t){for(var e,i=t.length,n=i%3,f=[],o=16383,a=0,d=i-n;a<d;a+=o)f.push(s(t,a,a+o>d?d:a+o));return 1===n?(e=t[i-1],f.push(r[e>>2]+r[e<<4&63]+"==")):2===n&&(e=(t[i-2]<<8)+t[i-1],f.push(r[e>>10]+r[e>>4&63]+r[e<<2&63]+"=")),f.join("")};for(var r=[],i=[],n="undefined"!=typeof Uint8Array?Uint8Array:Array,f="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",o=0;o<64;++o)r[o]=f[o],i[f.charCodeAt(o)]=o;function a(t){var e=t.length;if(e%4>0)throw new Error("Invalid string. Length must be a multiple of 4");var r=t.indexOf("=");return-1===r&&(r=e),[r,r===e?0:4-r%4]}function s(t,e,i){for(var n,f,o=[],a=e;a<i;a+=3)n=(t[a]<<16&16711680)+(t[a+1]<<8&65280)+(255&t[a+2]),o.push(r[(f=n)>>18&63]+r[f>>12&63]+r[f>>6&63]+r[63&f]);return o.join("")}i["-".charCodeAt(0)]=62,i["_".charCodeAt(0)]=63},404:function(t,e,r){!function(t,e){"use strict";function i(t,e){if(!t)throw new Error(e||"Assertion failed")}function n(t,e){t.super_=e;var r=function(){};r.prototype=e.prototype,t.prototype=new r,t.prototype.constructor=t}function f(t,e,r){if(f.isBN(t))return t;this.negative=0,this.words=null,this.length=0,this.red=null,null!==t&&("le"!==e&&"be"!==e||(r=e,e=10),this._init(t||0,e||10,r||"be"))}var o;"object"==typeof t?t.exports=f:e.BN=f,f.BN=f,f.wordSize=26;try{o="undefined"!=typeof window&&void 0!==window.Buffer?window.Buffer:r(790).Buffer}catch(t){}function a(t,e){var r=t.charCodeAt(e);return r>=48&&r<=57?r-48:r>=65&&r<=70?r-55:r>=97&&r<=102?r-87:void i(!1,"Invalid character in "+t)}function s(t,e,r){var i=a(t,r);return r-1>=e&&(i|=a(t,r-1)<<4),i}function d(t,e,r,n){for(var f=0,o=0,a=Math.min(t.length,r),s=e;s<a;s++){var d=t.charCodeAt(s)-48;f*=n,o=d>=49?d-49+10:d>=17?d-17+10:d,i(d>=0&&o<n,"Invalid character"),f+=o}return f}function c(t,e){t.words=e.words,t.length=e.length,t.negative=e.negative,t.red=e.red}if(f.isBN=function(t){return t instanceof f||null!==t&&"object"==typeof t&&t.constructor.wordSize===f.wordSize&&Array.isArray(t.words)},f.max=function(t,e){return t.cmp(e)>0?t:e},f.min=function(t,e){return t.cmp(e)<0?t:e},f.prototype._init=function(t,e,r){if("number"==typeof t)return this._initNumber(t,e,r);if("object"==typeof t)return this._initArray(t,e,r);"hex"===e&&(e=16),i(e===(0|e)&&e>=2&&e<=36);var n=0;"-"===(t=t.toString().replace(/\s+/g,""))[0]&&(n++,this.negative=1),n<t.length&&(16===e?this._parseHex(t,n,r):(this._parseBase(t,e,n),"le"===r&&this._initArray(this.toArray(),e,r)))},f.prototype._initNumber=function(t,e,r){t<0&&(this.negative=1,t=-t),t<67108864?(this.words=[67108863&t],this.length=1):t<4503599627370496?(this.words=[67108863&t,t/67108864&67108863],this.length=2):(i(t<9007199254740992),this.words=[67108863&t,t/67108864&67108863,1],this.length=3),"le"===r&&this._initArray(this.toArray(),e,r)},f.prototype._initArray=function(t,e,r){if(i("number"==typeof t.length),t.length<=0)return this.words=[0],this.length=1,this;this.length=Math.ceil(t.length/3),this.words=new Array(this.length);for(var n=0;n<this.length;n++)this.words[n]=0;var f,o,a=0;if("be"===r)for(n=t.length-1,f=0;n>=0;n-=3)o=t[n]|t[n-1]<<8|t[n-2]<<16,this.words[f]|=o<<a&67108863,this.words[f+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,f++);else if("le"===r)for(n=0,f=0;n<t.length;n+=3)o=t[n]|t[n+1]<<8|t[n+2]<<16,this.words[f]|=o<<a&67108863,this.words[f+1]=o>>>26-a&67108863,(a+=24)>=26&&(a-=26,f++);return this._strip()},f.prototype._parseHex=function(t,e,r){this.length=Math.ceil((t.length-e)/6),this.words=new Array(this.length);for(var i=0;i<this.length;i++)this.words[i]=0;var n,f=0,o=0;if("be"===r)for(i=t.length-1;i>=e;i-=2)n=s(t,e,i)<<f,this.words[o]|=67108863&n,f>=18?(f-=18,o+=1,this.words[o]|=n>>>26):f+=8;else for(i=(t.length-e)%2==0?e+1:e;i<t.length;i+=2)n=s(t,e,i)<<f,this.words[o]|=67108863&n,f>=18?(f-=18,o+=1,this.words[o]|=n>>>26):f+=8;this._strip()},f.prototype._parseBase=function(t,e,r){this.words=[0],this.length=1;for(var i=0,n=1;n<=67108863;n*=e)i++;i--,n=n/e|0;for(var f=t.length-r,o=f%i,a=Math.min(f,f-o)+r,s=0,c=r;c<a;c+=i)s=d(t,c,c+i,e),this.imuln(n),this.words[0]+s<67108864?this.words[0]+=s:this._iaddn(s);if(0!==o){var h=1;for(s=d(t,c,t.length,e),c=0;c<o;c++)h*=e;this.imuln(h),this.words[0]+s<67108864?this.words[0]+=s:this._iaddn(s)}this._strip()},f.prototype.copy=function(t){t.words=new Array(this.length);for(var e=0;e<this.length;e++)t.words[e]=this.words[e];t.length=this.length,t.negative=this.negative,t.red=this.red},f.prototype._move=function(t){c(t,this)},f.prototype.clone=function(){var t=new f(null);return this.copy(t),t},f.prototype._expand=function(t){for(;this.length<t;)this.words[this.length++]=0;return this},f.prototype._strip=function(){for(;this.length>1&&0===this.words[this.length-1];)this.length--;return this._normSign()},f.prototype._normSign=function(){return 1===this.length&&0===this.words[0]&&(this.negative=0),this},"undefined"!=typeof Symbol&&"function"==typeof Symbol.for)try{f.prototype[Symbol.for("nodejs.util.inspect.custom")]=h}catch(t){f.prototype.inspect=h}else f.prototype.inspect=h;function h(){return(this.red?"<BN-R: ":"<BN: ")+this.toString(16)+">"}var u=["","0","00","000","0000","00000","000000","0000000","00000000","000000000","0000000000","00000000000","000000000000","0000000000000","00000000000000","000000000000000","0000000000000000","00000000000000000","000000000000000000","0000000000000000000","00000000000000000000","000000000000000000000","0000000000000000000000","00000000000000000000000","000000000000000000000000","0000000000000000000000000"],b=[0,0,25,16,12,11,10,9,8,8,7,7,7,7,6,6,6,6,6,6,6,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5],l=[0,0,33554432,43046721,16777216,48828125,60466176,40353607,16777216,43046721,1e7,19487171,35831808,62748517,7529536,11390625,16777216,24137569,34012224,47045881,64e6,4084101,5153632,6436343,7962624,9765625,11881376,14348907,17210368,20511149,243e5,28629151,33554432,39135393,45435424,52521875,60466176];function p(t,e,r){r.negative=e.negative^t.negative;var i=t.length+e.length|0;r.length=i,i=i-1|0;var n=0|t.words[0],f=0|e.words[0],o=n*f,a=67108863&o,s=o/67108864|0;r.words[0]=a;for(var d=1;d<i;d++){for(var c=s>>>26,h=67108863&s,u=Math.min(d,e.length-1),b=Math.max(0,d-t.length+1);b<=u;b++){var l=d-b|0;c+=(o=(n=0|t.words[l])*(f=0|e.words[b])+h)/67108864|0,h=67108863&o}r.words[d]=0|h,s=0|c}return 0!==s?r.words[d]=0|s:r.length--,r._strip()}f.prototype.toString=function(t,e){var r;if(e=0|e||1,16===(t=t||10)||"hex"===t){r="";for(var n=0,f=0,o=0;o<this.length;o++){var a=this.words[o],s=(16777215&(a<<n|f)).toString(16);f=a>>>24-n&16777215,(n+=2)>=26&&(n-=26,o--),r=0!==f||o!==this.length-1?u[6-s.length]+s+r:s+r}for(0!==f&&(r=f.toString(16)+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}if(t===(0|t)&&t>=2&&t<=36){var d=b[t],c=l[t];r="";var h=this.clone();for(h.negative=0;!h.isZero();){var p=h.modrn(c).toString(t);r=(h=h.idivn(c)).isZero()?p+r:u[d-p.length]+p+r}for(this.isZero()&&(r="0"+r);r.length%e!=0;)r="0"+r;return 0!==this.negative&&(r="-"+r),r}i(!1,"Base should be between 2 and 36")},f.prototype.toNumber=function(){var t=this.words[0];return 2===this.length?t+=67108864*this.words[1]:3===this.length&&1===this.words[2]?t+=4503599627370496+67108864*this.words[1]:this.length>2&&i(!1,"Number can only safely store up to 53 bits"),0!==this.negative?-t:t},f.prototype.toJSON=function(){return this.toString(16,2)},o&&(f.prototype.toBuffer=function(t,e){return this.toArrayLike(o,t,e)}),f.prototype.toArray=function(t,e){return this.toArrayLike(Array,t,e)},f.prototype.toArrayLike=function(t,e,r){this._strip();var n=this.byteLength(),f=r||Math.max(1,n);i(n<=f,"byte array longer than desired length"),i(f>0,"Requested array length <= 0");var o=function(t,e){return t.allocUnsafe?t.allocUnsafe(e):new t(e)}(t,f);return this["_toArrayLike"+("le"===e?"LE":"BE")](o,n),o},f.prototype._toArrayLikeLE=function(t,e){for(var r=0,i=0,n=0,f=0;n<this.length;n++){var o=this.words[n]<<f|i;t[r++]=255&o,r<t.length&&(t[r++]=o>>8&255),r<t.length&&(t[r++]=o>>16&255),6===f?(r<t.length&&(t[r++]=o>>24&255),i=0,f=0):(i=o>>>24,f+=2)}if(r<t.length)for(t[r++]=i;r<t.length;)t[r++]=0},f.prototype._toArrayLikeBE=function(t,e){for(var r=t.length-1,i=0,n=0,f=0;n<this.length;n++){var o=this.words[n]<<f|i;t[r--]=255&o,r>=0&&(t[r--]=o>>8&255),r>=0&&(t[r--]=o>>16&255),6===f?(r>=0&&(t[r--]=o>>24&255),i=0,f=0):(i=o>>>24,f+=2)}if(r>=0)for(t[r--]=i;r>=0;)t[r--]=0},Math.clz32?f.prototype._countBits=function(t){return 32-Math.clz32(t)}:f.prototype._countBits=function(t){var e=t,r=0;return e>=4096&&(r+=13,e>>>=13),e>=64&&(r+=7,e>>>=7),e>=8&&(r+=4,e>>>=4),e>=2&&(r+=2,e>>>=2),r+e},f.prototype._zeroBits=function(t){if(0===t)return 26;var e=t,r=0;return 8191&e||(r+=13,e>>>=13),127&e||(r+=7,e>>>=7),15&e||(r+=4,e>>>=4),3&e||(r+=2,e>>>=2),1&e||r++,r},f.prototype.bitLength=function(){var t=this.words[this.length-1],e=this._countBits(t);return 26*(this.length-1)+e},f.prototype.zeroBits=function(){if(this.isZero())return 0;for(var t=0,e=0;e<this.length;e++){var r=this._zeroBits(this.words[e]);if(t+=r,26!==r)break}return t},f.prototype.byteLength=function(){return Math.ceil(this.bitLength()/8)},f.prototype.toTwos=function(t){return 0!==this.negative?this.abs().inotn(t).iaddn(1):this.clone()},f.prototype.fromTwos=function(t){return this.testn(t-1)?this.notn(t).iaddn(1).ineg():this.clone()},f.prototype.isNeg=function(){return 0!==this.negative},f.prototype.neg=function(){return this.clone().ineg()},f.prototype.ineg=function(){return this.isZero()||(this.negative^=1),this},f.prototype.iuor=function(t){for(;this.length<t.length;)this.words[this.length++]=0;for(var e=0;e<t.length;e++)this.words[e]=this.words[e]|t.words[e];return this._strip()},f.prototype.ior=function(t){return i(!(this.negative|t.negative)),this.iuor(t)},f.prototype.or=function(t){return this.length>t.length?this.clone().ior(t):t.clone().ior(this)},f.prototype.uor=function(t){return this.length>t.length?this.clone().iuor(t):t.clone().iuor(this)},f.prototype.iuand=function(t){var e;e=this.length>t.length?t:this;for(var r=0;r<e.length;r++)this.words[r]=this.words[r]&t.words[r];return this.length=e.length,this._strip()},f.prototype.iand=function(t){return i(!(this.negative|t.negative)),this.iuand(t)},f.prototype.and=function(t){return this.length>t.length?this.clone().iand(t):t.clone().iand(this)},f.prototype.uand=function(t){return this.length>t.length?this.clone().iuand(t):t.clone().iuand(this)},f.prototype.iuxor=function(t){var e,r;this.length>t.length?(e=this,r=t):(e=t,r=this);for(var i=0;i<r.length;i++)this.words[i]=e.words[i]^r.words[i];if(this!==e)for(;i<e.length;i++)this.words[i]=e.words[i];return this.length=e.length,this._strip()},f.prototype.ixor=function(t){return i(!(this.negative|t.negative)),this.iuxor(t)},f.prototype.xor=function(t){return this.length>t.length?this.clone().ixor(t):t.clone().ixor(this)},f.prototype.uxor=function(t){return this.length>t.length?this.clone().iuxor(t):t.clone().iuxor(this)},f.prototype.inotn=function(t){i("number"==typeof t&&t>=0);var e=0|Math.ceil(t/26),r=t%26;this._expand(e),r>0&&e--;for(var n=0;n<e;n++)this.words[n]=67108863&~this.words[n];return r>0&&(this.words[n]=~this.words[n]&67108863>>26-r),this._strip()},f.prototype.notn=function(t){return this.clone().inotn(t)},f.prototype.setn=function(t,e){i("number"==typeof t&&t>=0);var r=t/26|0,n=t%26;return this._expand(r+1),this.words[r]=e?this.words[r]|1<<n:this.words[r]&~(1<<n),this._strip()},f.prototype.iadd=function(t){var e,r,i;if(0!==this.negative&&0===t.negative)return this.negative=0,e=this.isub(t),this.negative^=1,this._normSign();if(0===this.negative&&0!==t.negative)return t.negative=0,e=this.isub(t),t.negative=1,e._normSign();this.length>t.length?(r=this,i=t):(r=t,i=this);for(var n=0,f=0;f<i.length;f++)e=(0|r.words[f])+(0|i.words[f])+n,this.words[f]=67108863&e,n=e>>>26;for(;0!==n&&f<r.length;f++)e=(0|r.words[f])+n,this.words[f]=67108863&e,n=e>>>26;if(this.length=r.length,0!==n)this.words[this.length]=n,this.length++;else if(r!==this)for(;f<r.length;f++)this.words[f]=r.words[f];return this},f.prototype.add=function(t){var e;return 0!==t.negative&&0===this.negative?(t.negative=0,e=this.sub(t),t.negative^=1,e):0===t.negative&&0!==this.negative?(this.negative=0,e=t.sub(this),this.negative=1,e):this.length>t.length?this.clone().iadd(t):t.clone().iadd(this)},f.prototype.isub=function(t){if(0!==t.negative){t.negative=0;var e=this.iadd(t);return t.negative=1,e._normSign()}if(0!==this.negative)return this.negative=0,this.iadd(t),this.negative=1,this._normSign();var r,i,n=this.cmp(t);if(0===n)return this.negative=0,this.length=1,this.words[0]=0,this;n>0?(r=this,i=t):(r=t,i=this);for(var f=0,o=0;o<i.length;o++)f=(e=(0|r.words[o])-(0|i.words[o])+f)>>26,this.words[o]=67108863&e;for(;0!==f&&o<r.length;o++)f=(e=(0|r.words[o])+f)>>26,this.words[o]=67108863&e;if(0===f&&o<r.length&&r!==this)for(;o<r.length;o++)this.words[o]=r.words[o];return this.length=Math.max(this.length,o),r!==this&&(this.negative=1),this._strip()},f.prototype.sub=function(t){return this.clone().isub(t)};var m=function(t,e,r){var i,n,f,o=t.words,a=e.words,s=r.words,d=0,c=0|o[0],h=8191&c,u=c>>>13,b=0|o[1],l=8191&b,p=b>>>13,m=0|o[2],y=8191&m,g=m>>>13,v=0|o[3],w=8191&v,M=v>>>13,S=0|o[4],A=8191&S,_=S>>>13,I=0|o[5],x=8191&I,E=I>>>13,B=0|o[6],R=8191&B,k=B>>>13,P=0|o[7],O=8191&P,z=P>>>13,L=0|o[8],q=8191&L,U=L>>>13,T=0|o[9],N=8191&T,j=T>>>13,C=0|a[0],F=8191&C,$=C>>>13,D=0|a[1],H=8191&D,K=D>>>13,J=0|a[2],X=8191&J,Z=J>>>13,V=0|a[3],W=8191&V,Y=V>>>13,G=0|a[4],Q=8191&G,tt=G>>>13,et=0|a[5],rt=8191&et,it=et>>>13,nt=0|a[6],ft=8191&nt,ot=nt>>>13,at=0|a[7],st=8191&at,dt=at>>>13,ct=0|a[8],ht=8191&ct,ut=ct>>>13,bt=0|a[9],lt=8191&bt,pt=bt>>>13;r.negative=t.negative^e.negative,r.length=19;var mt=(d+(i=Math.imul(h,F))|0)+((8191&(n=(n=Math.imul(h,$))+Math.imul(u,F)|0))<<13)|0;d=((f=Math.imul(u,$))+(n>>>13)|0)+(mt>>>26)|0,mt&=67108863,i=Math.imul(l,F),n=(n=Math.imul(l,$))+Math.imul(p,F)|0,f=Math.imul(p,$);var yt=(d+(i=i+Math.imul(h,H)|0)|0)+((8191&(n=(n=n+Math.imul(h,K)|0)+Math.imul(u,H)|0))<<13)|0;d=((f=f+Math.imul(u,K)|0)+(n>>>13)|0)+(yt>>>26)|0,yt&=67108863,i=Math.imul(y,F),n=(n=Math.imul(y,$))+Math.imul(g,F)|0,f=Math.imul(g,$),i=i+Math.imul(l,H)|0,n=(n=n+Math.imul(l,K)|0)+Math.imul(p,H)|0,f=f+Math.imul(p,K)|0;var gt=(d+(i=i+Math.imul(h,X)|0)|0)+((8191&(n=(n=n+Math.imul(h,Z)|0)+Math.imul(u,X)|0))<<13)|0;d=((f=f+Math.imul(u,Z)|0)+(n>>>13)|0)+(gt>>>26)|0,gt&=67108863,i=Math.imul(w,F),n=(n=Math.imul(w,$))+Math.imul(M,F)|0,f=Math.imul(M,$),i=i+Math.imul(y,H)|0,n=(n=n+Math.imul(y,K)|0)+Math.imul(g,H)|0,f=f+Math.imul(g,K)|0,i=i+Math.imul(l,X)|0,n=(n=n+Math.imul(l,Z)|0)+Math.imul(p,X)|0,f=f+Math.imul(p,Z)|0;var vt=(d+(i=i+Math.imul(h,W)|0)|0)+((8191&(n=(n=n+Math.imul(h,Y)|0)+Math.imul(u,W)|0))<<13)|0;d=((f=f+Math.imul(u,Y)|0)+(n>>>13)|0)+(vt>>>26)|0,vt&=67108863,i=Math.imul(A,F),n=(n=Math.imul(A,$))+Math.imul(_,F)|0,f=Math.imul(_,$),i=i+Math.imul(w,H)|0,n=(n=n+Math.imul(w,K)|0)+Math.imul(M,H)|0,f=f+Math.imul(M,K)|0,i=i+Math.imul(y,X)|0,n=(n=n+Math.imul(y,Z)|0)+Math.imul(g,X)|0,f=f+Math.imul(g,Z)|0,i=i+Math.imul(l,W)|0,n=(n=n+Math.imul(l,Y)|0)+Math.imul(p,W)|0,f=f+Math.imul(p,Y)|0;var wt=(d+(i=i+Math.imul(h,Q)|0)|0)+((8191&(n=(n=n+Math.imul(h,tt)|0)+Math.imul(u,Q)|0))<<13)|0;d=((f=f+Math.imul(u,tt)|0)+(n>>>13)|0)+(wt>>>26)|0,wt&=67108863,i=Math.imul(x,F),n=(n=Math.imul(x,$))+Math.imul(E,F)|0,f=Math.imul(E,$),i=i+Math.imul(A,H)|0,n=(n=n+Math.imul(A,K)|0)+Math.imul(_,H)|0,f=f+Math.imul(_,K)|0,i=i+Math.imul(w,X)|0,n=(n=n+Math.imul(w,Z)|0)+Math.imul(M,X)|0,f=f+Math.imul(M,Z)|0,i=i+Math.imul(y,W)|0,n=(n=n+Math.imul(y,Y)|0)+Math.imul(g,W)|0,f=f+Math.imul(g,Y)|0,i=i+Math.imul(l,Q)|0,n=(n=n+Math.imul(l,tt)|0)+Math.imul(p,Q)|0,f=f+Math.imul(p,tt)|0;var Mt=(d+(i=i+Math.imul(h,rt)|0)|0)+((8191&(n=(n=n+Math.imul(h,it)|0)+Math.imul(u,rt)|0))<<13)|0;d=((f=f+Math.imul(u,it)|0)+(n>>>13)|0)+(Mt>>>26)|0,Mt&=67108863,i=Math.imul(R,F),n=(n=Math.imul(R,$))+Math.imul(k,F)|0,f=Math.imul(k,$),i=i+Math.imul(x,H)|0,n=(n=n+Math.imul(x,K)|0)+Math.imul(E,H)|0,f=f+Math.imul(E,K)|0,i=i+Math.imul(A,X)|0,n=(n=n+Math.imul(A,Z)|0)+Math.imul(_,X)|0,f=f+Math.imul(_,Z)|0,i=i+Math.imul(w,W)|0,n=(n=n+Math.imul(w,Y)|0)+Math.imul(M,W)|0,f=f+Math.imul(M,Y)|0,i=i+Math.imul(y,Q)|0,n=(n=n+Math.imul(y,tt)|0)+Math.imul(g,Q)|0,f=f+Math.imul(g,tt)|0,i=i+Math.imul(l,rt)|0,n=(n=n+Math.imul(l,it)|0)+Math.imul(p,rt)|0,f=f+Math.imul(p,it)|0;var St=(d+(i=i+Math.imul(h,ft)|0)|0)+((8191&(n=(n=n+Math.imul(h,ot)|0)+Math.imul(u,ft)|0))<<13)|0;d=((f=f+Math.imul(u,ot)|0)+(n>>>13)|0)+(St>>>26)|0,St&=67108863,i=Math.imul(O,F),n=(n=Math.imul(O,$))+Math.imul(z,F)|0,f=Math.imul(z,$),i=i+Math.imul(R,H)|0,n=(n=n+Math.imul(R,K)|0)+Math.imul(k,H)|0,f=f+Math.imul(k,K)|0,i=i+Math.imul(x,X)|0,n=(n=n+Math.imul(x,Z)|0)+Math.imul(E,X)|0,f=f+Math.imul(E,Z)|0,i=i+Math.imul(A,W)|0,n=(n=n+Math.imul(A,Y)|0)+Math.imul(_,W)|0,f=f+Math.imul(_,Y)|0,i=i+Math.imul(w,Q)|0,n=(n=n+Math.imul(w,tt)|0)+Math.imul(M,Q)|0,f=f+Math.imul(M,tt)|0,i=i+Math.imul(y,rt)|0,n=(n=n+Math.imul(y,it)|0)+Math.imul(g,rt)|0,f=f+Math.imul(g,it)|0,i=i+Math.imul(l,ft)|0,n=(n=n+Math.imul(l,ot)|0)+Math.imul(p,ft)|0,f=f+Math.imul(p,ot)|0;var At=(d+(i=i+Math.imul(h,st)|0)|0)+((8191&(n=(n=n+Math.imul(h,dt)|0)+Math.imul(u,st)|0))<<13)|0;d=((f=f+Math.imul(u,dt)|0)+(n>>>13)|0)+(At>>>26)|0,At&=67108863,i=Math.imul(q,F),n=(n=Math.imul(q,$))+Math.imul(U,F)|0,f=Math.imul(U,$),i=i+Math.imul(O,H)|0,n=(n=n+Math.imul(O,K)|0)+Math.imul(z,H)|0,f=f+Math.imul(z,K)|0,i=i+Math.imul(R,X)|0,n=(n=n+Math.imul(R,Z)|0)+Math.imul(k,X)|0,f=f+Math.imul(k,Z)|0,i=i+Math.imul(x,W)|0,n=(n=n+Math.imul(x,Y)|0)+Math.imul(E,W)|0,f=f+Math.imul(E,Y)|0,i=i+Math.imul(A,Q)|0,n=(n=n+Math.imul(A,tt)|0)+Math.imul(_,Q)|0,f=f+Math.imul(_,tt)|0,i=i+Math.imul(w,rt)|0,n=(n=n+Math.imul(w,it)|0)+Math.imul(M,rt)|0,f=f+Math.imul(M,it)|0,i=i+Math.imul(y,ft)|0,n=(n=n+Math.imul(y,ot)|0)+Math.imul(g,ft)|0,f=f+Math.imul(g,ot)|0,i=i+Math.imul(l,st)|0,n=(n=n+Math.imul(l,dt)|0)+Math.imul(p,st)|0,f=f+Math.imul(p,dt)|0;var _t=(d+(i=i+Math.imul(h,ht)|0)|0)+((8191&(n=(n=n+Math.imul(h,ut)|0)+Math.imul(u,ht)|0))<<13)|0;d=((f=f+Math.imul(u,ut)|0)+(n>>>13)|0)+(_t>>>26)|0,_t&=67108863,i=Math.imul(N,F),n=(n=Math.imul(N,$))+Math.imul(j,F)|0,f=Math.imul(j,$),i=i+Math.imul(q,H)|0,n=(n=n+Math.imul(q,K)|0)+Math.imul(U,H)|0,f=f+Math.imul(U,K)|0,i=i+Math.imul(O,X)|0,n=(n=n+Math.imul(O,Z)|0)+Math.imul(z,X)|0,f=f+Math.imul(z,Z)|0,i=i+Math.imul(R,W)|0,n=(n=n+Math.imul(R,Y)|0)+Math.imul(k,W)|0,f=f+Math.imul(k,Y)|0,i=i+Math.imul(x,Q)|0,n=(n=n+Math.imul(x,tt)|0)+Math.imul(E,Q)|0,f=f+Math.imul(E,tt)|0,i=i+Math.imul(A,rt)|0,n=(n=n+Math.imul(A,it)|0)+Math.imul(_,rt)|0,f=f+Math.imul(_,it)|0,i=i+Math.imul(w,ft)|0,n=(n=n+Math.imul(w,ot)|0)+Math.imul(M,ft)|0,f=f+Math.imul(M,ot)|0,i=i+Math.imul(y,st)|0,n=(n=n+Math.imul(y,dt)|0)+Math.imul(g,st)|0,f=f+Math.imul(g,dt)|0,i=i+Math.imul(l,ht)|0,n=(n=n+Math.imul(l,ut)|0)+Math.imul(p,ht)|0,f=f+Math.imul(p,ut)|0;var It=(d+(i=i+Math.imul(h,lt)|0)|0)+((8191&(n=(n=n+Math.imul(h,pt)|0)+Math.imul(u,lt)|0))<<13)|0;d=((f=f+Math.imul(u,pt)|0)+(n>>>13)|0)+(It>>>26)|0,It&=67108863,i=Math.imul(N,H),n=(n=Math.imul(N,K))+Math.imul(j,H)|0,f=Math.imul(j,K),i=i+Math.imul(q,X)|0,n=(n=n+Math.imul(q,Z)|0)+Math.imul(U,X)|0,f=f+Math.imul(U,Z)|0,i=i+Math.imul(O,W)|0,n=(n=n+Math.imul(O,Y)|0)+Math.imul(z,W)|0,f=f+Math.imul(z,Y)|0,i=i+Math.imul(R,Q)|0,n=(n=n+Math.imul(R,tt)|0)+Math.imul(k,Q)|0,f=f+Math.imul(k,tt)|0,i=i+Math.imul(x,rt)|0,n=(n=n+Math.imul(x,it)|0)+Math.imul(E,rt)|0,f=f+Math.imul(E,it)|0,i=i+Math.imul(A,ft)|0,n=(n=n+Math.imul(A,ot)|0)+Math.imul(_,ft)|0,f=f+Math.imul(_,ot)|0,i=i+Math.imul(w,st)|0,n=(n=n+Math.imul(w,dt)|0)+Math.imul(M,st)|0,f=f+Math.imul(M,dt)|0,i=i+Math.imul(y,ht)|0,n=(n=n+Math.imul(y,ut)|0)+Math.imul(g,ht)|0,f=f+Math.imul(g,ut)|0;var xt=(d+(i=i+Math.imul(l,lt)|0)|0)+((8191&(n=(n=n+Math.imul(l,pt)|0)+Math.imul(p,lt)|0))<<13)|0;d=((f=f+Math.imul(p,pt)|0)+(n>>>13)|0)+(xt>>>26)|0,xt&=67108863,i=Math.imul(N,X),n=(n=Math.imul(N,Z))+Math.imul(j,X)|0,f=Math.imul(j,Z),i=i+Math.imul(q,W)|0,n=(n=n+Math.imul(q,Y)|0)+Math.imul(U,W)|0,f=f+Math.imul(U,Y)|0,i=i+Math.imul(O,Q)|0,n=(n=n+Math.imul(O,tt)|0)+Math.imul(z,Q)|0,f=f+Math.imul(z,tt)|0,i=i+Math.imul(R,rt)|0,n=(n=n+Math.imul(R,it)|0)+Math.imul(k,rt)|0,f=f+Math.imul(k,it)|0,i=i+Math.imul(x,ft)|0,n=(n=n+Math.imul(x,ot)|0)+Math.imul(E,ft)|0,f=f+Math.imul(E,ot)|0,i=i+Math.imul(A,st)|0,n=(n=n+Math.imul(A,dt)|0)+Math.imul(_,st)|0,f=f+Math.imul(_,dt)|0,i=i+Math.imul(w,ht)|0,n=(n=n+Math.imul(w,ut)|0)+Math.imul(M,ht)|0,f=f+Math.imul(M,ut)|0;var Et=(d+(i=i+Math.imul(y,lt)|0)|0)+((8191&(n=(n=n+Math.imul(y,pt)|0)+Math.imul(g,lt)|0))<<13)|0;d=((f=f+Math.imul(g,pt)|0)+(n>>>13)|0)+(Et>>>26)|0,Et&=67108863,i=Math.imul(N,W),n=(n=Math.imul(N,Y))+Math.imul(j,W)|0,f=Math.imul(j,Y),i=i+Math.imul(q,Q)|0,n=(n=n+Math.imul(q,tt)|0)+Math.imul(U,Q)|0,f=f+Math.imul(U,tt)|0,i=i+Math.imul(O,rt)|0,n=(n=n+Math.imul(O,it)|0)+Math.imul(z,rt)|0,f=f+Math.imul(z,it)|0,i=i+Math.imul(R,ft)|0,n=(n=n+Math.imul(R,ot)|0)+Math.imul(k,ft)|0,f=f+Math.imul(k,ot)|0,i=i+Math.imul(x,st)|0,n=(n=n+Math.imul(x,dt)|0)+Math.imul(E,st)|0,f=f+Math.imul(E,dt)|0,i=i+Math.imul(A,ht)|0,n=(n=n+Math.imul(A,ut)|0)+Math.imul(_,ht)|0,f=f+Math.imul(_,ut)|0;var Bt=(d+(i=i+Math.imul(w,lt)|0)|0)+((8191&(n=(n=n+Math.imul(w,pt)|0)+Math.imul(M,lt)|0))<<13)|0;d=((f=f+Math.imul(M,pt)|0)+(n>>>13)|0)+(Bt>>>26)|0,Bt&=67108863,i=Math.imul(N,Q),n=(n=Math.imul(N,tt))+Math.imul(j,Q)|0,f=Math.imul(j,tt),i=i+Math.imul(q,rt)|0,n=(n=n+Math.imul(q,it)|0)+Math.imul(U,rt)|0,f=f+Math.imul(U,it)|0,i=i+Math.imul(O,ft)|0,n=(n=n+Math.imul(O,ot)|0)+Math.imul(z,ft)|0,f=f+Math.imul(z,ot)|0,i=i+Math.imul(R,st)|0,n=(n=n+Math.imul(R,dt)|0)+Math.imul(k,st)|0,f=f+Math.imul(k,dt)|0,i=i+Math.imul(x,ht)|0,n=(n=n+Math.imul(x,ut)|0)+Math.imul(E,ht)|0,f=f+Math.imul(E,ut)|0;var Rt=(d+(i=i+Math.imul(A,lt)|0)|0)+((8191&(n=(n=n+Math.imul(A,pt)|0)+Math.imul(_,lt)|0))<<13)|0;d=((f=f+Math.imul(_,pt)|0)+(n>>>13)|0)+(Rt>>>26)|0,Rt&=67108863,i=Math.imul(N,rt),n=(n=Math.imul(N,it))+Math.imul(j,rt)|0,f=Math.imul(j,it),i=i+Math.imul(q,ft)|0,n=(n=n+Math.imul(q,ot)|0)+Math.imul(U,ft)|0,f=f+Math.imul(U,ot)|0,i=i+Math.imul(O,st)|0,n=(n=n+Math.imul(O,dt)|0)+Math.imul(z,st)|0,f=f+Math.imul(z,dt)|0,i=i+Math.imul(R,ht)|0,n=(n=n+Math.imul(R,ut)|0)+Math.imul(k,ht)|0,f=f+Math.imul(k,ut)|0;var kt=(d+(i=i+Math.imul(x,lt)|0)|0)+((8191&(n=(n=n+Math.imul(x,pt)|0)+Math.imul(E,lt)|0))<<13)|0;d=((f=f+Math.imul(E,pt)|0)+(n>>>13)|0)+(kt>>>26)|0,kt&=67108863,i=Math.imul(N,ft),n=(n=Math.imul(N,ot))+Math.imul(j,ft)|0,f=Math.imul(j,ot),i=i+Math.imul(q,st)|0,n=(n=n+Math.imul(q,dt)|0)+Math.imul(U,st)|0,f=f+Math.imul(U,dt)|0,i=i+Math.imul(O,ht)|0,n=(n=n+Math.imul(O,ut)|0)+Math.imul(z,ht)|0,f=f+Math.imul(z,ut)|0;var Pt=(d+(i=i+Math.imul(R,lt)|0)|0)+((8191&(n=(n=n+Math.imul(R,pt)|0)+Math.imul(k,lt)|0))<<13)|0;d=((f=f+Math.imul(k,pt)|0)+(n>>>13)|0)+(Pt>>>26)|0,Pt&=67108863,i=Math.imul(N,st),n=(n=Math.imul(N,dt))+Math.imul(j,st)|0,f=Math.imul(j,dt),i=i+Math.imul(q,ht)|0,n=(n=n+Math.imul(q,ut)|0)+Math.imul(U,ht)|0,f=f+Math.imul(U,ut)|0;var Ot=(d+(i=i+Math.imul(O,lt)|0)|0)+((8191&(n=(n=n+Math.imul(O,pt)|0)+Math.imul(z,lt)|0))<<13)|0;d=((f=f+Math.imul(z,pt)|0)+(n>>>13)|0)+(Ot>>>26)|0,Ot&=67108863,i=Math.imul(N,ht),n=(n=Math.imul(N,ut))+Math.imul(j,ht)|0,f=Math.imul(j,ut);var zt=(d+(i=i+Math.imul(q,lt)|0)|0)+((8191&(n=(n=n+Math.imul(q,pt)|0)+Math.imul(U,lt)|0))<<13)|0;d=((f=f+Math.imul(U,pt)|0)+(n>>>13)|0)+(zt>>>26)|0,zt&=67108863;var Lt=(d+(i=Math.imul(N,lt))|0)+((8191&(n=(n=Math.imul(N,pt))+Math.imul(j,lt)|0))<<13)|0;return d=((f=Math.imul(j,pt))+(n>>>13)|0)+(Lt>>>26)|0,Lt&=67108863,s[0]=mt,s[1]=yt,s[2]=gt,s[3]=vt,s[4]=wt,s[5]=Mt,s[6]=St,s[7]=At,s[8]=_t,s[9]=It,s[10]=xt,s[11]=Et,s[12]=Bt,s[13]=Rt,s[14]=kt,s[15]=Pt,s[16]=Ot,s[17]=zt,s[18]=Lt,0!==d&&(s[19]=d,r.length++),r};function y(t,e,r){r.negative=e.negative^t.negative,r.length=t.length+e.length;for(var i=0,n=0,f=0;f<r.length-1;f++){var o=n;n=0;for(var a=67108863&i,s=Math.min(f,e.length-1),d=Math.max(0,f-t.length+1);d<=s;d++){var c=f-d,h=(0|t.words[c])*(0|e.words[d]),u=67108863&h;a=67108863&(u=u+a|0),n+=(o=(o=o+(h/67108864|0)|0)+(u>>>26)|0)>>>26,o&=67108863}r.words[f]=a,i=o,o=n}return 0!==i?r.words[f]=i:r.length--,r._strip()}function g(t,e,r){return y(t,e,r)}function v(t,e){this.x=t,this.y=e}Math.imul||(m=p),f.prototype.mulTo=function(t,e){var r=this.length+t.length;return 10===this.length&&10===t.length?m(this,t,e):r<63?p(this,t,e):r<1024?y(this,t,e):g(this,t,e)},v.prototype.makeRBT=function(t){for(var e=new Array(t),r=f.prototype._countBits(t)-1,i=0;i<t;i++)e[i]=this.revBin(i,r,t);return e},v.prototype.revBin=function(t,e,r){if(0===t||t===r-1)return t;for(var i=0,n=0;n<e;n++)i|=(1&t)<<e-n-1,t>>=1;return i},v.prototype.permute=function(t,e,r,i,n,f){for(var o=0;o<f;o++)i[o]=e[t[o]],n[o]=r[t[o]]},v.prototype.transform=function(t,e,r,i,n,f){this.permute(f,t,e,r,i,n);for(var o=1;o<n;o<<=1)for(var a=o<<1,s=Math.cos(2*Math.PI/a),d=Math.sin(2*Math.PI/a),c=0;c<n;c+=a)for(var h=s,u=d,b=0;b<o;b++){var l=r[c+b],p=i[c+b],m=r[c+b+o],y=i[c+b+o],g=h*m-u*y;y=h*y+u*m,m=g,r[c+b]=l+m,i[c+b]=p+y,r[c+b+o]=l-m,i[c+b+o]=p-y,b!==a&&(g=s*h-d*u,u=s*u+d*h,h=g)}},v.prototype.guessLen13b=function(t,e){var r=1|Math.max(e,t),i=1&r,n=0;for(r=r/2|0;r;r>>>=1)n++;return 1<<n+1+i},v.prototype.conjugate=function(t,e,r){if(!(r<=1))for(var i=0;i<r/2;i++){var n=t[i];t[i]=t[r-i-1],t[r-i-1]=n,n=e[i],e[i]=-e[r-i-1],e[r-i-1]=-n}},v.prototype.normalize13b=function(t,e){for(var r=0,i=0;i<e/2;i++){var n=8192*Math.round(t[2*i+1]/e)+Math.round(t[2*i]/e)+r;t[i]=67108863&n,r=n<67108864?0:n/67108864|0}return t},v.prototype.convert13b=function(t,e,r,n){for(var f=0,o=0;o<e;o++)f+=0|t[o],r[2*o]=8191&f,f>>>=13,r[2*o+1]=8191&f,f>>>=13;for(o=2*e;o<n;++o)r[o]=0;i(0===f),i(!(-8192&f))},v.prototype.stub=function(t){for(var e=new Array(t),r=0;r<t;r++)e[r]=0;return e},v.prototype.mulp=function(t,e,r){var i=2*this.guessLen13b(t.length,e.length),n=this.makeRBT(i),f=this.stub(i),o=new Array(i),a=new Array(i),s=new Array(i),d=new Array(i),c=new Array(i),h=new Array(i),u=r.words;u.length=i,this.convert13b(t.words,t.length,o,i),this.convert13b(e.words,e.length,d,i),this.transform(o,f,a,s,i,n),this.transform(d,f,c,h,i,n);for(var b=0;b<i;b++){var l=a[b]*c[b]-s[b]*h[b];s[b]=a[b]*h[b]+s[b]*c[b],a[b]=l}return this.conjugate(a,s,i),this.transform(a,s,u,f,i,n),this.conjugate(u,f,i),this.normalize13b(u,i),r.negative=t.negative^e.negative,r.length=t.length+e.length,r._strip()},f.prototype.mul=function(t){var e=new f(null);return e.words=new Array(this.length+t.length),this.mulTo(t,e)},f.prototype.mulf=function(t){var e=new f(null);return e.words=new Array(this.length+t.length),g(this,t,e)},f.prototype.imul=function(t){return this.clone().mulTo(t,this)},f.prototype.imuln=function(t){var e=t<0;e&&(t=-t),i("number"==typeof t),i(t<67108864);for(var r=0,n=0;n<this.length;n++){var f=(0|this.words[n])*t,o=(67108863&f)+(67108863&r);r>>=26,r+=f/67108864|0,r+=o>>>26,this.words[n]=67108863&o}return 0!==r&&(this.words[n]=r,this.length++),e?this.ineg():this},f.prototype.muln=function(t){return this.clone().imuln(t)},f.prototype.sqr=function(){return this.mul(this)},f.prototype.isqr=function(){return this.imul(this.clone())},f.prototype.pow=function(t){var e=function(t){for(var e=new Array(t.bitLength()),r=0;r<e.length;r++){var i=r/26|0,n=r%26;e[r]=t.words[i]>>>n&1}return e}(t);if(0===e.length)return new f(1);for(var r=this,i=0;i<e.length&&0===e[i];i++,r=r.sqr());if(++i<e.length)for(var n=r.sqr();i<e.length;i++,n=n.sqr())0!==e[i]&&(r=r.mul(n));return r},f.prototype.iushln=function(t){i("number"==typeof t&&t>=0);var e,r=t%26,n=(t-r)/26,f=67108863>>>26-r<<26-r;if(0!==r){var o=0;for(e=0;e<this.length;e++){var a=this.words[e]&f,s=(0|this.words[e])-a<<r;this.words[e]=s|o,o=a>>>26-r}o&&(this.words[e]=o,this.length++)}if(0!==n){for(e=this.length-1;e>=0;e--)this.words[e+n]=this.words[e];for(e=0;e<n;e++)this.words[e]=0;this.length+=n}return this._strip()},f.prototype.ishln=function(t){return i(0===this.negative),this.iushln(t)},f.prototype.iushrn=function(t,e,r){var n;i("number"==typeof t&&t>=0),n=e?(e-e%26)/26:0;var f=t%26,o=Math.min((t-f)/26,this.length),a=67108863^67108863>>>f<<f,s=r;if(n-=o,n=Math.max(0,n),s){for(var d=0;d<o;d++)s.words[d]=this.words[d];s.length=o}if(0===o);else if(this.length>o)for(this.length-=o,d=0;d<this.length;d++)this.words[d]=this.words[d+o];else this.words[0]=0,this.length=1;var c=0;for(d=this.length-1;d>=0&&(0!==c||d>=n);d--){var h=0|this.words[d];this.words[d]=c<<26-f|h>>>f,c=h&a}return s&&0!==c&&(s.words[s.length++]=c),0===this.length&&(this.words[0]=0,this.length=1),this._strip()},f.prototype.ishrn=function(t,e,r){return i(0===this.negative),this.iushrn(t,e,r)},f.prototype.shln=function(t){return this.clone().ishln(t)},f.prototype.ushln=function(t){return this.clone().iushln(t)},f.prototype.shrn=function(t){return this.clone().ishrn(t)},f.prototype.ushrn=function(t){return this.clone().iushrn(t)},f.prototype.testn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26,n=1<<e;return!(this.length<=r||!(this.words[r]&n))},f.prototype.imaskn=function(t){i("number"==typeof t&&t>=0);var e=t%26,r=(t-e)/26;if(i(0===this.negative,"imaskn works only with positive numbers"),this.length<=r)return this;if(0!==e&&r++,this.length=Math.min(r,this.length),0!==e){var n=67108863^67108863>>>e<<e;this.words[this.length-1]&=n}return this._strip()},f.prototype.maskn=function(t){return this.clone().imaskn(t)},f.prototype.iaddn=function(t){return i("number"==typeof t),i(t<67108864),t<0?this.isubn(-t):0!==this.negative?1===this.length&&(0|this.words[0])<=t?(this.words[0]=t-(0|this.words[0]),this.negative=0,this):(this.negative=0,this.isubn(t),this.negative=1,this):this._iaddn(t)},f.prototype._iaddn=function(t){this.words[0]+=t;for(var e=0;e<this.length&&this.words[e]>=67108864;e++)this.words[e]-=67108864,e===this.length-1?this.words[e+1]=1:this.words[e+1]++;return this.length=Math.max(this.length,e+1),this},f.prototype.isubn=function(t){if(i("number"==typeof t),i(t<67108864),t<0)return this.iaddn(-t);if(0!==this.negative)return this.negative=0,this.iaddn(t),this.negative=1,this;if(this.words[0]-=t,1===this.length&&this.words[0]<0)this.words[0]=-this.words[0],this.negative=1;else for(var e=0;e<this.length&&this.words[e]<0;e++)this.words[e]+=67108864,this.words[e+1]-=1;return this._strip()},f.prototype.addn=function(t){return this.clone().iaddn(t)},f.prototype.subn=function(t){return this.clone().isubn(t)},f.prototype.iabs=function(){return this.negative=0,this},f.prototype.abs=function(){return this.clone().iabs()},f.prototype._ishlnsubmul=function(t,e,r){var n,f,o=t.length+r;this._expand(o);var a=0;for(n=0;n<t.length;n++){f=(0|this.words[n+r])+a;var s=(0|t.words[n])*e;a=((f-=67108863&s)>>26)-(s/67108864|0),this.words[n+r]=67108863&f}for(;n<this.length-r;n++)a=(f=(0|this.words[n+r])+a)>>26,this.words[n+r]=67108863&f;if(0===a)return this._strip();for(i(-1===a),a=0,n=0;n<this.length;n++)a=(f=-(0|this.words[n])+a)>>26,this.words[n]=67108863&f;return this.negative=1,this._strip()},f.prototype._wordDiv=function(t,e){var r=(this.length,t.length),i=this.clone(),n=t,o=0|n.words[n.length-1];0!=(r=26-this._countBits(o))&&(n=n.ushln(r),i.iushln(r),o=0|n.words[n.length-1]);var a,s=i.length-n.length;if("mod"!==e){(a=new f(null)).length=s+1,a.words=new Array(a.length);for(var d=0;d<a.length;d++)a.words[d]=0}var c=i.clone()._ishlnsubmul(n,1,s);0===c.negative&&(i=c,a&&(a.words[s]=1));for(var h=s-1;h>=0;h--){var u=67108864*(0|i.words[n.length+h])+(0|i.words[n.length+h-1]);for(u=Math.min(u/o|0,67108863),i._ishlnsubmul(n,u,h);0!==i.negative;)u--,i.negative=0,i._ishlnsubmul(n,1,h),i.isZero()||(i.negative^=1);a&&(a.words[h]=u)}return a&&a._strip(),i._strip(),"div"!==e&&0!==r&&i.iushrn(r),{div:a||null,mod:i}},f.prototype.divmod=function(t,e,r){return i(!t.isZero()),this.isZero()?{div:new f(0),mod:new f(0)}:0!==this.negative&&0===t.negative?(a=this.neg().divmod(t,e),"mod"!==e&&(n=a.div.neg()),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.iadd(t)),{div:n,mod:o}):0===this.negative&&0!==t.negative?(a=this.divmod(t.neg(),e),"mod"!==e&&(n=a.div.neg()),{div:n,mod:a.mod}):this.negative&t.negative?(a=this.neg().divmod(t.neg(),e),"div"!==e&&(o=a.mod.neg(),r&&0!==o.negative&&o.isub(t)),{div:a.div,mod:o}):t.length>this.length||this.cmp(t)<0?{div:new f(0),mod:this}:1===t.length?"div"===e?{div:this.divn(t.words[0]),mod:null}:"mod"===e?{div:null,mod:new f(this.modrn(t.words[0]))}:{div:this.divn(t.words[0]),mod:new f(this.modrn(t.words[0]))}:this._wordDiv(t,e);var n,o,a},f.prototype.div=function(t){return this.divmod(t,"div",!1).div},f.prototype.mod=function(t){return this.divmod(t,"mod",!1).mod},f.prototype.umod=function(t){return this.divmod(t,"mod",!0).mod},f.prototype.divRound=function(t){var e=this.divmod(t);if(e.mod.isZero())return e.div;var r=0!==e.div.negative?e.mod.isub(t):e.mod,i=t.ushrn(1),n=t.andln(1),f=r.cmp(i);return f<0||1===n&&0===f?e.div:0!==e.div.negative?e.div.isubn(1):e.div.iaddn(1)},f.prototype.modrn=function(t){var e=t<0;e&&(t=-t),i(t<=67108863);for(var r=(1<<26)%t,n=0,f=this.length-1;f>=0;f--)n=(r*n+(0|this.words[f]))%t;return e?-n:n},f.prototype.modn=function(t){return this.modrn(t)},f.prototype.idivn=function(t){var e=t<0;e&&(t=-t),i(t<=67108863);for(var r=0,n=this.length-1;n>=0;n--){var f=(0|this.words[n])+67108864*r;this.words[n]=f/t|0,r=f%t}return this._strip(),e?this.ineg():this},f.prototype.divn=function(t){return this.clone().idivn(t)},f.prototype.egcd=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==e.negative?e.umod(t):e.clone();for(var n=new f(1),o=new f(0),a=new f(0),s=new f(1),d=0;e.isEven()&&r.isEven();)e.iushrn(1),r.iushrn(1),++d;for(var c=r.clone(),h=e.clone();!e.isZero();){for(var u=0,b=1;!(e.words[0]&b)&&u<26;++u,b<<=1);if(u>0)for(e.iushrn(u);u-- >0;)(n.isOdd()||o.isOdd())&&(n.iadd(c),o.isub(h)),n.iushrn(1),o.iushrn(1);for(var l=0,p=1;!(r.words[0]&p)&&l<26;++l,p<<=1);if(l>0)for(r.iushrn(l);l-- >0;)(a.isOdd()||s.isOdd())&&(a.iadd(c),s.isub(h)),a.iushrn(1),s.iushrn(1);e.cmp(r)>=0?(e.isub(r),n.isub(a),o.isub(s)):(r.isub(e),a.isub(n),s.isub(o))}return{a,b:s,gcd:r.iushln(d)}},f.prototype._invmp=function(t){i(0===t.negative),i(!t.isZero());var e=this,r=t.clone();e=0!==