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