import t from"to-arraybuffer";import{Buffer as r}from"buffer";import{BigInteger as e,SecureRandom as n}from"jsbn";var i=e.prototype.Barrett;function o(t,r){this.x=r,this.q=t}function u(t,r,n,i){this.curve=t,this.x=r,this.y=n,this.z=null==i?e.ONE:i,this.zinv=null}function s(t,r,e){this.q=t,this.a=this.fromBigInteger(r),this.b=this.fromBigInteger(e),this.infinity=new u(this,null,null),this.reducer=new i(this.q)}o.prototype.equals=function(t){return t==this||this.q.equals(t.q)&&this.x.equals(t.x)},o.prototype.toBigInteger=function(){return this.x},o.prototype.negate=function(){return new o(this.q,this.x.negate().mod(this.q))},o.prototype.add=function(t){return new o(this.q,this.x.add(t.toBigInteger()).mod(this.q))},o.prototype.subtract=function(t){return new o(this.q,this.x.subtract(t.toBigInteger()).mod(this.q))},o.prototype.multiply=function(t){return new o(this.q,this.x.multiply(t.toBigInteger()).mod(this.q))},o.prototype.square=function(){return new o(this.q,this.x.square().mod(this.q))},o.prototype.divide=function(t){return new o(this.q,this.x.multiply(t.toBigInteger().modInverse(this.q)).mod(this.q))},u.prototype.getX=function(){null==this.zinv&&(this.zinv=this.z.modInverse(this.curve.q));var t=this.x.toBigInteger().multiply(this.zinv);return this.curve.reduce(t),this.curve.fromBigInteger(t)},u.prototype.getY=function(){null==this.zinv&&(this.zinv=this.z.modInverse(this.curve.q));var t=this.y.toBigInteger().multiply(this.zinv);return this.curve.reduce(t),this.curve.fromBigInteger(t)},u.prototype.equals=function(t){return t==this||(this.isInfinity()?t.isInfinity():t.isInfinity()?this.isInfinity():!!t.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(e.ZERO)&&t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q).equals(e.ZERO))},u.prototype.isInfinity=function(){return null==this.x&&null==this.y||this.z.equals(e.ZERO)&&!this.y.toBigInteger().equals(e.ZERO)},u.prototype.negate=function(){return new u(this.curve,this.x,this.y.negate(),this.z)},u.prototype.add=function(t){if(this.isInfinity())return t;if(t.isInfinity())return this;var r=t.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(t.z)).mod(this.curve.q),n=t.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(t.z)).mod(this.curve.q);if(e.ZERO.equals(n))return e.ZERO.equals(r)?this.twice():this.curve.getInfinity();var i=new e("3"),o=this.x.toBigInteger(),s=this.y.toBigInteger(),f=(t.x.toBigInteger(),t.y.toBigInteger(),n.square()),a=f.multiply(n),h=o.multiply(f),l=r.square().multiply(this.z),c=l.subtract(h.shiftLeft(1)).multiply(t.z).subtract(a).multiply(n).mod(this.curve.q),g=h.multiply(i).multiply(r).subtract(s.multiply(a)).subtract(l.multiply(r)).multiply(t.z).add(r.multiply(a)).mod(this.curve.q),p=a.multiply(this.z).multiply(t.z).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(c),this.curve.fromBigInteger(g),p)},u.prototype.twice=function(){if(this.isInfinity())return this;if(0==this.y.toBigInteger().signum())return this.curve.getInfinity();var t=new e("3"),r=this.x.toBigInteger(),n=this.y.toBigInteger(),i=n.multiply(this.z),o=i.multiply(n).mod(this.curve.q),s=this.curve.a.toBigInteger(),f=r.square().multiply(t);e.ZERO.equals(s)||(f=f.add(this.z.square().multiply(s)));var a=(f=f.mod(this.curve.q)).square().subtract(r.shiftLeft(3).multiply(o)).shiftLeft(1).multiply(i).mod(this.curve.q),h=f.multiply(t).multiply(r).subtract(o.shiftLeft(1)).shiftLeft(2).multiply(o).subtract(f.square().multiply(f)).mod(this.curve.q),l=i.square().multiply(i).shiftLeft(3).mod(this.curve.q);return new u(this.curve,this.curve.fromBigInteger(a),this.curve.fromBigInteger(h),l)},u.prototype.multiply=function(t){if(this.isInfinity())return this;if(0==t.signum())return this.curve.getInfinity();var r,n=t,i=n.multiply(new e("3")),o=this.negate(),u=this;for(r=i.bitLength()-2;r>0;--r){u=u.twice();var s=i.testBit(r);s!=n.testBit(r)&&(u=u.add(s?this:o))}return u},u.prototype.multiplyTwo=function(t,r,e){var n;n=t.bitLength()>e.bitLength()?t.bitLength()-1:e.bitLength()-1;for(var i=this.curve.getInfinity(),o=this.add(r);n>=0;)i=i.twice(),t.testBit(n)?i=e.testBit(n)?i.add(o):i.add(this):e.testBit(n)&&(i=i.add(r)),--n;return i},s.prototype.getQ=function(){return this.q},s.prototype.getA=function(){return this.a},s.prototype.getB=function(){return this.b},s.prototype.equals=function(t){return t==this||this.q.equals(t.q)&&this.a.equals(t.a)&&this.b.equals(t.b)},s.prototype.getInfinity=function(){return this.infinity},s.prototype.fromBigInteger=function(t){return new o(this.q,t)},s.prototype.reduce=function(t){this.reducer.reduce(t)},s.prototype.decodePointHex=function(t){switch(parseInt(t.substr(0,2),16)){case 0:return this.infinity;case 2:case 3:return null;case 4:case 6:case 7:var r=(t.length-2)/2,n=t.substr(2,r),i=t.substr(r+2,r);return new u(this,this.fromBigInteger(new e(n,16)),this.fromBigInteger(new e(i,16)));default:return null}},s.prototype.encodePointHex=function(t){if(t.isInfinity())return"00";var r=t.getX().toBigInteger().toString(16),e=t.getY().toBigInteger().toString(16),n=this.getQ().toString(16).length;for(n%2!=0&&n++;r.length>>32-r},a=function(t,r){var e=r-t.length;return(e>0?"0".repeat(e):"")+t},h=function(t){return t<16?2043430169:2055708042},l=function(t,r,e,n){return n<16?t^r^e:t&r|t&e|r&e},c=function(t,r,e,n){return n<16?t^r^e:t&r|~t&e},g=function(t,r,e){for(var n,i,o,u,s,a=function(t){var r,e=new Array(132);t.forEach(function(t,r){e[r]=t});for(var n=16;n<68;n++)e[n]=(r=e[n-16]^e[n-9]^f(e[n-3],15))^f(r,15)^f(r,23)^f(e[n-13],7)^e[n-6];for(var i=0;i<64;i++)e[i+68]=e[i]^e[i+4];return e}(r),g=t[0],p=t[1],F=t[2],y=t[3],d=t[4],v=t[5],B=t[6],m=t[7],I=0;I<64;I++)i=(n=f(f(g,12)+d+f(h(I),I),7))^f(g,12),o=l(g,p,F,I)+y+i+a[I+68],u=c(d,v,B,I)+m+n+a[I],y=F,F=f(p,9),p=g,g=o,m=B,B=f(v,19),v=d,d=(s=u)^f(s,9)^f(s,17);return[g^t[0],p^t[1],F^t[2],y^t[3],d^t[4],v^t[5],B^t[6],m^t[7]]},p=function(e,n,i){if("string"==typeof e?e=r.from(e,n||"utf8"):e instanceof ArrayBuffer&&(e=r.from(e)),!r.isBuffer(e))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(e)+'"');var o,u,s,f,a,h;o=e,u=r.alloc(1,128),f=r.alloc(s=(s=o.length%64)>=56?64-s%56-1:56-s-1,0),a=r.alloc(8),h=8*o.length,a.writeUInt32BE(Math.floor(h/Math.pow(2,32)),0),a.writeUInt32BE(h%Math.pow(2,32),4);for(var l=(e=r.concat([o,u,f,a],o.length+1+s+8)).length/64,c=new Array(l),p=0;p>>28][(251658240&t)>>>24]<<24|w[(15728640&t)>>>20][(983040&t)>>>16]<<16|w[(61440&t)>>>12][(3840&t)>>>8]<<8|w[(240&t)>>>4][(15&t)>>>0]<<0},A=function(t){return(r=S(t))^f(r,2)^f(r,10)^f(r,18)^f(r,24);var r},z=function(t){var r,e=new Array(36);e[0]=t[0]^q[0],e[1]=t[1]^q[1],e[2]=t[2]^q[2],e[3]=t[3]^q[3];for(var n=new Array(32),i=0;i<32;i++)e[i+4]=e[i]^(r=S(e[i+1]^e[i+2]^e[i+3]^x[i]))^f(r,13)^f(r,23),n[i]=e[i+4];return n},C=function(t,r){for(var e=z(r),n=0;n<32;n++)t[n+4]=t[n]^A(t[n+1]^t[n+2]^t[n+3]^e[n]);return[t[35],t[34],t[33],t[32]]},O=function(t,r){for(var e=z(r).reverse(),n=0;n<32;n++)t[n+4]=t[n]^A(t[n+1]^t[n+2]^t[n+3]^e[n]);return[t[35],t[34],t[33],t[32]]},L=function(t){return[t.readInt32BE(0),t.readInt32BE(4),t.readInt32BE(8),t.readInt32BE(12)]},_=function(t){for(var e=r.alloc(16),n=0;n<4;n++)e.writeInt32BE(t[n],4*n);return e},D={__proto__:null,constants:{ECB:1,CBC:2},encrypt:function(e,n,i){var o=i||{},u=o.mode,s=o.iv,f=o.outputEncoding;if("string"==typeof e?e=r.from(e,o.inputEncoding||"utf8"):e instanceof ArrayBuffer&&(e=r.from(e)),!r.isBuffer(e))throw new TypeError('Expected "string" | "Buffer" | "ArrayBuffer" but received "'+Object.prototype.toString.call(e)+'"');if(!b.test(n))throw new TypeError("Invalid value of cipher `key`");if(n=r.from(n,"hex"),2===u&&!b.test(s))throw new TypeError("Invalid value of `iv` option");return function(e,n,i,o){i&&(i=L(i)),n=L(n);for(var u=[],s=(e=function(t){var e=16-t.length%16,n=r.alloc(e,e);return r.concat([t,n],t.length+e)}(e)).length/16,f=0;f=0;f--){var a,h=16*f;a=f>0?[e.readInt32BE(h-16),e.readInt32BE(h-16+4),e.readInt32BE(h-16+8),e.readInt32BE(h-16+12)]:i;var l=[e.readInt32BE(h),e.readInt32BE(h+4),e.readInt32BE(h+8),e.readInt32BE(h+12)],c=O(l,n);u.unshift(_([c[0]^a[0],c[1]^a[1],c[2]^a[2],c[3]^a[3]]))}else for(var g=0;g