crypto.js_c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var performance=performance||{};performance.now=function(){return performance.now||performance.mozNow||performance.msNow||performance.oNow||performance.webkitNow||Date.now}();function Benchmark(a,c,b,d,e,f,g,h,l){this.name=a;this.doWarmup=c;this.doDeterministic=b;this.deterministicIterations=d;this.run=e;this.Setup=f?f:function(){};this.TearDown=g?g:function(){};this.rmsResult=h?h:null;this.minIterations=l?l:32}function BenchmarkResult(a,c,b){this.benchmark=a;this.time=c;this.latency=b}
  2. BenchmarkResult.prototype.valueOf=function(){return this.time};function BenchmarkSuite(a,c,b){this.name=a;this.reference=c;this.benchmarks=b;BenchmarkSuite.suites.push(this)}BenchmarkSuite.suites=[];BenchmarkSuite.version="9";BenchmarkSuite.config={doWarmup:void 0,doDeterministic:void 0};alert=function(a){throw"Alert called with argument: "+a;};
  3. BenchmarkSuite.ResetRNG=function(){Math.random=function(){var a=49734321;return function(){a=a+2127912214+(a<<12)&4294967295;a=(a^3345072700^a>>>19)&4294967295;a=a+374761393+(a<<5)&4294967295;a=(a+3550635116^a<<9)&4294967295;a=a+4251993797+(a<<3)&4294967295;a=(a^3042594569^a>>>16)&4294967295;return(a&268435455)/268435456}}()};
  4. BenchmarkSuite.RunSuites=function(a,c){function b(){for(;d||g<f;){if(d)d=d();else{var h=e[g++];a.NotifyStart&&a.NotifyStart(h.name);-1<c.indexOf(h.name)?h.NotifySkipped(a):d=h.RunStep(a)}if(d&&"undefined"!=typeof window&&window.setTimeout){window.setTimeout(b,25);return}}a.NotifyScore&&(h=BenchmarkSuite.GeometricMean(BenchmarkSuite.scores),h=BenchmarkSuite.FormatScore(100*h),a.NotifyScore(h))}c="undefined"===typeof c?[]:c;var d=null,e=BenchmarkSuite.suites,f=e.length;BenchmarkSuite.scores=[];var g=
  5. 0;b()};BenchmarkSuite.CountBenchmarks=function(){for(var a=0,c=BenchmarkSuite.suites,b=0;b<c.length;b++)a+=c[b].benchmarks.length;return a};BenchmarkSuite.GeometricMean=function(a){for(var c=0,b=0;b<a.length;b++)c+=Math.log(a[b]);return Math.pow(Math.E,c/a.length)};BenchmarkSuite.GeometricMeanTime=function(a){for(var c=0,b=0;b<a.length;b++)c+=Math.log(a[b].time);return Math.pow(Math.E,c/a.length)};
  6. BenchmarkSuite.GeometricMeanLatency=function(a){for(var c=0,b=!1,d=0;d<a.length;d++)0!=a[d].latency&&(c+=Math.log(a[d].latency),b=!0);return b?Math.pow(Math.E,c/a.length):0};BenchmarkSuite.FormatScore=function(a){return 100<a?a.toFixed(0):a.toPrecision(3)};BenchmarkSuite.prototype.NotifyStep=function(a){this.results.push(a);this.runner.NotifyStep&&this.runner.NotifyStep(a.benchmark.name)};
  7. BenchmarkSuite.prototype.NotifyResult=function(){var a=BenchmarkSuite.GeometricMeanTime(this.results),a=this.reference[0]/a;BenchmarkSuite.scores.push(a);this.runner.NotifyResult&&(a=BenchmarkSuite.FormatScore(100*a),this.runner.NotifyResult(this.name,a));2==this.reference.length&&(a=BenchmarkSuite.GeometricMeanLatency(this.results),0!=a&&(a=this.reference[1]/a,BenchmarkSuite.scores.push(a),this.runner.NotifyResult&&(a=BenchmarkSuite.FormatScore(100*a),this.runner.NotifyResult(this.name+"Latency",
  8. a))))};BenchmarkSuite.prototype.NotifySkipped=function(a){BenchmarkSuite.scores.push(1);a.NotifyResult&&a.NotifyResult(this.name,"Skipped")};BenchmarkSuite.prototype.NotifyError=function(a){this.runner.NotifyError&&this.runner.NotifyError(this.name,a);this.runner.NotifyStep&&this.runner.NotifyStep(this.name)};
  9. BenchmarkSuite.prototype.RunSingleBenchmark=function(a,c){function b(b){for(var c=0,d=new Date,f=0;e?f<a.deterministicIterations:1E3>c;f++)a.run(),c=new Date-d;null!=b&&(b.runs+=f,b.elapsed+=c)}var d=BenchmarkSuite.config,e=void 0!==d.doDeterministic?d.doDeterministic:a.doDeterministic;(void 0!==d.doWarmup?d.doWarmup:a.doWarmup)||null!=c||(c={runs:0,elapsed:0});if(null==c)return b(null),{runs:0,elapsed:0};b(c);if(c.runs<a.minIterations)return c;var d=1E3*c.elapsed/c.runs,f=null!=a.rmsResult?a.rmsResult():
  10. 0;this.NotifyStep(new BenchmarkResult(a,d,f));return null};
  11. BenchmarkSuite.prototype.RunStep=function(a){function c(){if(f<e){try{g.benchmarks[f].Setup()}catch(a){return g.NotifyError(a),null}return b}g.NotifyResult();return null}function b(){try{h=g.RunSingleBenchmark(g.benchmarks[f],h)}catch(a){return g.NotifyError(a),null}return null==h?d:b()}function d(){try{g.benchmarks[f++].TearDown()}catch(a){return g.NotifyError(a),null}return c}BenchmarkSuite.ResetRNG();this.results=[];this.runner=a;var e=this.benchmarks.length,f=0,g=this,h;return c()};
  12. var Crypto=new BenchmarkSuite("Crypto",[266181],[new Benchmark("Encrypt",!0,!1,3900,encrypt),new Benchmark("Decrypt",!0,!1,220,decrypt)]),dbits,BI_DB,BI_DM,BI_DV,BI_FP,BI_FV,BI_F1,BI_F2,canary=0xdeadbeefcafe,j_lm=15715070==(canary&16777215);function BigInteger(a,c,b){this.array=[];null!=a&&("number"==typeof a?this.fromNumber(a,c,b):null==c&&"string"!=typeof a?this.fromString(a,256):this.fromString(a,c))}function nbi(){return new BigInteger(null)}
  13. function am1(a,c,b,d,e,f){var g=this.array;for(b=b.array;0<=--f;){var h=c*g[a++]+b[d]+e;e=Math.floor(h/67108864);b[d++]=h&67108863}return e}function am2(a,c,b,d,e,f){var g=this.array;b=b.array;var h=c&32767;for(c>>=15;0<=--f;){var l=g[a]&32767,m=g[a++]>>15,q=c*l+m*h,l=h*l+((q&32767)<<15)+b[d]+(e&1073741823);e=(l>>>30)+(q>>>15)+c*m+(e>>>30);b[d++]=l&1073741823}return e}
  14. function am3(a,c,b,d,e,f){var g=this.array;b=b.array;var h=c&16383;for(c>>=14;0<=--f;){var l=g[a]&16383,m=g[a++]>>14,q=c*l+m*h,l=h*l+((q&16383)<<14)+b[d]+e;e=(l>>28)+(q>>14)+c*m;b[d++]=l&268435455}return e}function am4(a,c,b,d,e,f){var g=this.array;b=b.array;var h=c&8191;for(c>>=13;0<=--f;){var l=g[a]&8191,m=g[a++]>>13,q=c*l+m*h,l=h*l+((q&8191)<<13)+b[d]+e;e=(l>>26)+(q>>13)+c*m;b[d++]=l&67108863}return e}
  15. setupEngine=function(a,c){BigInteger.prototype.am=a;BI_DB=dbits=c;BI_DM=(1<<dbits)-1;BI_DV=1<<dbits;BI_FP=52;BI_FV=Math.pow(2,BI_FP);BI_F1=BI_FP-dbits;BI_F2=2*dbits-BI_FP};var BI_RM="0123456789abcdefghijklmnopqrstuvwxyz",BI_RC=[],rr,vv;rr=48;for(vv=0;9>=vv;++vv)BI_RC[rr++]=vv;rr=97;for(vv=10;36>vv;++vv)BI_RC[rr++]=vv;rr=65;for(vv=10;36>vv;++vv)BI_RC[rr++]=vv;function int2char(a){return BI_RM.charAt(a)}function intAt(a,c){var b=BI_RC[a.charCodeAt(c)];return null==b?-1:b}
  16. function bnpCopyTo(a){for(var c=this.array,b=a.array,d=this.t-1;0<=d;--d)b[d]=c[d];a.t=this.t;a.s=this.s}function bnpFromInt(a){var c=this.array;this.t=1;this.s=0>a?-1:0;0<a?c[0]=a:-1>a?c[0]=a+DV:this.t=0}function nbv(a){var c=nbi();c.fromInt(a);return c}
  17. function bnpFromString(a,c){var b=this.array,d;if(16==c)d=4;else if(8==c)d=3;else if(256==c)d=8;else if(2==c)d=1;else if(32==c)d=5;else if(4==c)d=2;else{this.fromRadix(a,c);return}this.s=this.t=0;for(var e=a.length,f=!1,g=0;0<=--e;){var h=8==d?a[e]&255:intAt(a,e);0>h?"-"==a.charAt(e)&&(f=!0):(f=!1,0==g?b[this.t++]=h:g+d>BI_DB?(b[this.t-1]|=(h&(1<<BI_DB-g)-1)<<g,b[this.t++]=h>>BI_DB-g):b[this.t-1]|=h<<g,g+=d,g>=BI_DB&&(g-=BI_DB))}8==d&&0!=(a[0]&128)&&(this.s=-1,0<g&&(b[this.t-1]|=(1<<BI_DB-g)-1<<g));
  18. this.clamp();f&&BigInteger.ZERO.subTo(this,this)}function bnpClamp(){for(var a=this.array,c=this.s&BI_DM;0<this.t&&a[this.t-1]==c;)--this.t}
  19. function bnToString(a){var c=this.array;if(0>this.s)return"-"+this.negate().toString(a);if(16==a)a=4;else if(8==a)a=3;else if(2==a)a=1;else if(32==a)a=5;else if(4==a)a=2;else return this.toRadix(a);var b=(1<<a)-1,d,e=!1,f="",g=this.t,h=BI_DB-g*BI_DB%a;if(0<g--)for(h<BI_DB&&0<(d=c[g]>>h)&&(e=!0,f=int2char(d));0<=g;)h<a?(d=(c[g]&(1<<h)-1)<<a-h,d|=c[--g]>>(h+=BI_DB-a)):(d=c[g]>>(h-=a)&b,0>=h&&(h+=BI_DB,--g)),0<d&&(e=!0),e&&(f+=int2char(d));return e?f:"0"}
  20. function bnNegate(){var a=nbi();BigInteger.ZERO.subTo(this,a);return a}function bnAbs(){return 0>this.s?this.negate():this}function bnCompareTo(a){var c=this.array,b=a.array,d=this.s-a.s;if(0!=d)return d;var e=this.t,d=e-a.t;if(0!=d)return d;for(;0<=--e;)if(0!=(d=c[e]-b[e]))return d;return 0}function nbits(a){var c=1,b;0!=(b=a>>>16)&&(a=b,c+=16);0!=(b=a>>8)&&(a=b,c+=8);0!=(b=a>>4)&&(a=b,c+=4);0!=(b=a>>2)&&(a=b,c+=2);0!=a>>1&&(c+=1);return c}
  21. function bnBitLength(){return 0>=this.t?0:BI_DB*(this.t-1)+nbits(this.array[this.t-1]^this.s&BI_DM)}function bnpDLShiftTo(a,c){var b=this.array,d=c.array,e;for(e=this.t-1;0<=e;--e)d[e+a]=b[e];for(e=a-1;0<=e;--e)d[e]=0;c.t=this.t+a;c.s=this.s}function bnpDRShiftTo(a,c){for(var b=this.array,d=c.array,e=a;e<this.t;++e)d[e-a]=b[e];c.t=Math.max(this.t-a,0);c.s=this.s}
  22. function bnpLShiftTo(a,c){var b=this.array,d=c.array,e=a%BI_DB,f=BI_DB-e,g=(1<<f)-1,h=Math.floor(a/BI_DB),l=this.s<<e&BI_DM,m;for(m=this.t-1;0<=m;--m)d[m+h+1]=b[m]>>f|l,l=(b[m]&g)<<e;for(m=h-1;0<=m;--m)d[m]=0;d[h]=l;c.t=this.t+h+1;c.s=this.s;c.clamp()}
  23. function bnpRShiftTo(a,c){var b=this.array,d=c.array;c.s=this.s;var e=Math.floor(a/BI_DB);if(e>=this.t)c.t=0;else{var f=a%BI_DB,g=BI_DB-f,h=(1<<f)-1;d[0]=b[e]>>f;for(var l=e+1;l<this.t;++l)d[l-e-1]|=(b[l]&h)<<g,d[l-e]=b[l]>>f;0<f&&(d[this.t-e-1]|=(this.s&h)<<g);c.t=this.t-e;c.clamp()}}
  24. function bnpSubTo(a,c){for(var b=this.array,d=c.array,e=a.array,f=0,g=0,h=Math.min(a.t,this.t);f<h;)g+=b[f]-e[f],d[f++]=g&BI_DM,g>>=BI_DB;if(a.t<this.t){for(g-=a.s;f<this.t;)g+=b[f],d[f++]=g&BI_DM,g>>=BI_DB;g+=this.s}else{for(g+=this.s;f<a.t;)g-=e[f],d[f++]=g&BI_DM,g>>=BI_DB;g-=a.s}c.s=0>g?-1:0;-1>g?d[f++]=BI_DV+g:0<g&&(d[f++]=g);c.t=f;c.clamp()}
  25. function bnpMultiplyTo(a,c){var b=c.array,d=this.abs(),e=a.abs(),f=e.array,g=d.t;for(c.t=g+e.t;0<=--g;)b[g]=0;for(g=0;g<e.t;++g)b[g+d.t]=d.am(0,f[g],c,g,0,d.t);c.s=0;c.clamp();this.s!=a.s&&BigInteger.ZERO.subTo(c,c)}
  26. function bnpSquareTo(a){for(var c=this.abs(),b=c.array,d=a.array,e=a.t=2*c.t;0<=--e;)d[e]=0;for(e=0;e<c.t-1;++e){var f=c.am(e,b[e],a,2*e,0,1);(d[e+c.t]+=c.am(e+1,2*b[e],a,2*e+1,f,c.t-e-1))>=BI_DV&&(d[e+c.t]-=BI_DV,d[e+c.t+1]=1)}0<a.t&&(d[a.t-1]+=c.am(e,b[e],a,2*e,0,1));a.s=0;a.clamp()}
  27. function bnpDivRemTo(a,c,b){var d=a.abs();if(!(0>=d.t)){var e=this.abs();if(e.t<d.t)null!=c&&c.fromInt(0),null!=b&&this.copyTo(b);else{null==b&&(b=nbi());var f=nbi(),g=this.s;a=a.s;var h=BI_DB-nbits(d.array[d.t-1]);0<h?(d.lShiftTo(h,f),e.lShiftTo(h,b)):(d.copyTo(f),e.copyTo(b));var d=f.t,l=f.array,e=l[d-1];if(0!=e){var m=e*(1<<BI_F1)+(1<d?l[d-2]>>BI_F2:0),q=BI_FV/m,m=(1<<BI_F1)/m,p=1<<BI_F2,y=b.t,A=y-d,B=null==c?nbi():c;f.dlShiftTo(A,B);var K=b.array;0<=b.compareTo(B)&&(K[b.t++]=1,b.subTo(B,b));BigInteger.ONE.dlShiftTo(d,
  28. B);for(B.subTo(f,f);f.t<d;)l[f.t++]=0;for(;0<=--A;)if(l=K[--y]==e?BI_DM:Math.floor(K[y]*q+(K[y-1]+p)*m),(K[y]+=f.am(0,l,b,A,0,d))<l)for(f.dlShiftTo(A,B),b.subTo(B,b);K[y]<--l;)b.subTo(B,b);null!=c&&(b.drShiftTo(d,c),g!=a&&BigInteger.ZERO.subTo(c,c));b.t=d;b.clamp();0<h&&b.rShiftTo(h,b);0>g&&BigInteger.ZERO.subTo(b,b)}}}}function bnMod(a){var c=nbi();this.abs().divRemTo(a,null,c);0>this.s&&0<c.compareTo(BigInteger.ZERO)&&a.subTo(c,c);return c}function Classic(a){this.m=a}
  29. function cConvert(a){return 0>a.s||0<=a.compareTo(this.m)?a.mod(this.m):a}function cRevert(a){return a}function cReduce(a){a.divRemTo(this.m,null,a)}function cMulTo(a,c,b){a.multiplyTo(c,b);this.reduce(b)}function cSqrTo(a,c){a.squareTo(c);this.reduce(c)}Classic.prototype.convert=cConvert;Classic.prototype.revert=cRevert;Classic.prototype.reduce=cReduce;Classic.prototype.mulTo=cMulTo;Classic.prototype.sqrTo=cSqrTo;
  30. function bnpInvDigit(){if(1>this.t)return 0;var a=this.array[0];if(0==(a&1))return 0;var c=a&3,c=c*(2-(a&15)*c)&15,c=c*(2-(a&255)*c)&255,c=c*(2-((a&65535)*c&65535))&65535,c=c*(2-a*c%BI_DV)%BI_DV;return 0<c?BI_DV-c:-c}function Montgomery(a){this.m=a;this.mp=a.invDigit();this.mpl=this.mp&32767;this.mph=this.mp>>15;this.um=(1<<BI_DB-15)-1;this.mt2=2*a.t}
  31. function montConvert(a){var c=nbi();a.abs().dlShiftTo(this.m.t,c);c.divRemTo(this.m,null,c);0>a.s&&0<c.compareTo(BigInteger.ZERO)&&this.m.subTo(c,c);return c}function montRevert(a){var c=nbi();a.copyTo(c);this.reduce(c);return c}
  32. function montReduce(a){for(var c=a.array;a.t<=this.mt2;)c[a.t++]=0;for(var b=0;b<this.m.t;++b){var d=c[b]&32767,e=d*this.mpl+((d*this.mph+(c[b]>>15)*this.mpl&this.um)<<15)&BI_DM,d=b+this.m.t;for(c[d]+=this.m.am(0,e,a,b,0,this.m.t);c[d]>=BI_DV;)c[d]-=BI_DV,c[++d]++}a.clamp();a.drShiftTo(this.m.t,a);0<=a.compareTo(this.m)&&a.subTo(this.m,a)}function montSqrTo(a,c){a.squareTo(c);this.reduce(c)}function montMulTo(a,c,b){a.multiplyTo(c,b);this.reduce(b)}Montgomery.prototype.convert=montConvert;
  33. Montgomery.prototype.revert=montRevert;Montgomery.prototype.reduce=montReduce;Montgomery.prototype.mulTo=montMulTo;Montgomery.prototype.sqrTo=montSqrTo;function bnpIsEven(){var a=this.array;return 0==(0<this.t?a[0]&1:this.s)}function bnpExp(a,c){if(4294967295<a||1>a)return BigInteger.ONE;var b=nbi(),d=nbi(),e=c.convert(this),f=nbits(a)-1;for(e.copyTo(b);0<=--f;)if(c.sqrTo(b,d),0<(a&1<<f))c.mulTo(d,e,b);else var g=b,b=d,d=g;return c.revert(b)}
  34. function bnModPowInt(a,c){var b;b=256>a||c.isEven()?new Classic(c):new Montgomery(c);return this.exp(a,b)}BigInteger.prototype.copyTo=bnpCopyTo;BigInteger.prototype.fromInt=bnpFromInt;BigInteger.prototype.fromString=bnpFromString;BigInteger.prototype.clamp=bnpClamp;BigInteger.prototype.dlShiftTo=bnpDLShiftTo;BigInteger.prototype.drShiftTo=bnpDRShiftTo;BigInteger.prototype.lShiftTo=bnpLShiftTo;BigInteger.prototype.rShiftTo=bnpRShiftTo;BigInteger.prototype.subTo=bnpSubTo;
  35. BigInteger.prototype.multiplyTo=bnpMultiplyTo;BigInteger.prototype.squareTo=bnpSquareTo;BigInteger.prototype.divRemTo=bnpDivRemTo;BigInteger.prototype.invDigit=bnpInvDigit;BigInteger.prototype.isEven=bnpIsEven;BigInteger.prototype.exp=bnpExp;BigInteger.prototype.toString=bnToString;BigInteger.prototype.negate=bnNegate;BigInteger.prototype.abs=bnAbs;BigInteger.prototype.compareTo=bnCompareTo;BigInteger.prototype.bitLength=bnBitLength;BigInteger.prototype.mod=bnMod;BigInteger.prototype.modPowInt=bnModPowInt;
  36. BigInteger.ZERO=nbv(0);BigInteger.ONE=nbv(1);function bnClone(){var a=nbi();this.copyTo(a);return a}function bnIntValue(){var a=this.array;if(0>this.s){if(1==this.t)return a[0]-BI_DV;if(0==this.t)return-1}else{if(1==this.t)return a[0];if(0==this.t)return 0}return(a[1]&(1<<32-BI_DB)-1)<<BI_DB|a[0]}function bnByteValue(){var a=this.array;return 0==this.t?this.s:a[0]<<24>>24}function bnShortValue(){var a=this.array;return 0==this.t?this.s:a[0]<<16>>16}
  37. function bnpChunkSize(a){return Math.floor(Math.LN2*BI_DB/Math.log(a))}function bnSigNum(){var a=this.array;return 0>this.s?-1:0>=this.t||1==this.t&&0>=a[0]?0:1}function bnpToRadix(a){null==a&&(a=10);if(0==this.signum()||2>a||36<a)return"0";var c=this.chunkSize(a),c=Math.pow(a,c),b=nbv(c),d=nbi(),e=nbi(),f="";for(this.divRemTo(b,d,e);0<d.signum();)f=(c+e.intValue()).toString(a).substr(1)+f,d.divRemTo(b,d,e);return e.intValue().toString(a)+f}
  38. function bnpFromRadix(a,c){this.fromInt(0);null==c&&(c=10);for(var b=this.chunkSize(c),d=Math.pow(c,b),e=!1,f=0,g=0,h=0;h<a.length;++h){var l=intAt(a,h);0>l?"-"==a.charAt(h)&&0==this.signum()&&(e=!0):(g=c*g+l,++f>=b&&(this.dMultiply(d),this.dAddOffset(g,0),g=f=0))}0<f&&(this.dMultiply(Math.pow(c,f)),this.dAddOffset(g,0));e&&BigInteger.ZERO.subTo(this,this)}
  39. function bnpFromNumber(a,c,b){if("number"==typeof c)if(2>a)this.fromInt(1);else for(this.fromNumber(a,b),this.testBit(a-1)||this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this),this.isEven()&&this.dAddOffset(1,0);!this.isProbablePrime(c);)this.dAddOffset(2,0),this.bitLength()>a&&this.subTo(BigInteger.ONE.shiftLeft(a-1),this);else{b=[];var d=a&7;b.length=(a>>3)+1;c.nextBytes(b);b[0]=0<d?b[0]&(1<<d)-1:0;this.fromString(b,256)}}
  40. function bnToByteArray(){var a=this.array,c=this.t,b=[];b[0]=this.s;var d=BI_DB-c*BI_DB%8,e,f=0;if(0<c--)for(d<BI_DB&&(e=a[c]>>d)!=(this.s&BI_DM)>>d&&(b[f++]=e|this.s<<BI_DB-d);0<=c;)if(8>d?(e=(a[c]&(1<<d)-1)<<8-d,e|=a[--c]>>(d+=BI_DB-8)):(e=a[c]>>(d-=8)&255,0>=d&&(d+=BI_DB,--c)),0!=(e&128)&&(e|=-256),0==f&&(this.s&128)!=(e&128)&&++f,0<f||e!=this.s)b[f++]=e;return b}function bnEquals(a){return 0==this.compareTo(a)}function bnMin(a){return 0>this.compareTo(a)?this:a}
  41. function bnMax(a){return 0<this.compareTo(a)?this:a}function bnpBitwiseTo(a,c,b){var d=this.array,e=a.array,f=b.array,g,h,l=Math.min(a.t,this.t);for(g=0;g<l;++g)f[g]=c(d[g],e[g]);if(a.t<this.t){h=a.s&BI_DM;for(g=l;g<this.t;++g)f[g]=c(d[g],h);b.t=this.t}else{h=this.s&BI_DM;for(g=l;g<a.t;++g)f[g]=c(h,e[g]);b.t=a.t}b.s=c(this.s,a.s);b.clamp()}function op_and(a,c){return a&c}function bnAnd(a){var c=nbi();this.bitwiseTo(a,op_and,c);return c}function op_or(a,c){return a|c}
  42. function bnOr(a){var c=nbi();this.bitwiseTo(a,op_or,c);return c}function op_xor(a,c){return a^c}function bnXor(a){var c=nbi();this.bitwiseTo(a,op_xor,c);return c}function op_andnot(a,c){return a&~c}function bnAndNot(a){var c=nbi();this.bitwiseTo(a,op_andnot,c);return c}function bnNot(){for(var a=this.array,c=nbi(),b=c.array,d=0;d<this.t;++d)b[d]=BI_DM&~a[d];c.t=this.t;c.s=~this.s;return c}function bnShiftLeft(a){var c=nbi();0>a?this.rShiftTo(-a,c):this.lShiftTo(a,c);return c}
  43. function bnShiftRight(a){var c=nbi();0>a?this.lShiftTo(-a,c):this.rShiftTo(a,c);return c}function lbit(a){if(0==a)return-1;var c=0;0==(a&65535)&&(a>>=16,c+=16);0==(a&255)&&(a>>=8,c+=8);0==(a&15)&&(a>>=4,c+=4);0==(a&3)&&(a>>=2,c+=2);0==(a&1)&&++c;return c}function bnGetLowestSetBit(){for(var a=this.array,c=0;c<this.t;++c)if(0!=a[c])return c*BI_DB+lbit(a[c]);return 0>this.s?this.t*BI_DB:-1}function cbit(a){for(var c=0;0!=a;)a&=a-1,++c;return c}
  44. function bnBitCount(){for(var a=0,c=this.s&BI_DM,b=0;b<this.t;++b)a+=cbit(this_array[b]^c);return a}function bnTestBit(a){var c=Math.floor(a/BI_DB);return c>=this.t?0!=this.s:0!=(this.array[c]&1<<a%BI_DB)}function bnpChangeBit(a,c){var b=BigInteger.ONE.shiftLeft(a);this.bitwiseTo(b,c,b);return b}function bnSetBit(a){return this.changeBit(a,op_or)}function bnClearBit(a){return this.changeBit(a,op_andnot)}function bnFlipBit(a){return this.changeBit(a,op_xor)}
  45. function bnpAddTo(a,c){for(var b=this.array,d=a.array,e=c.array,f=0,g=0,h=Math.min(a.t,this.t);f<h;)g+=b[f]+d[f],e[f++]=g&BI_DM,g>>=BI_DB;if(a.t<this.t){for(g+=a.s;f<this.t;)g+=b[f],e[f++]=g&BI_DM,g>>=BI_DB;g+=this.s}else{for(g+=this.s;f<a.t;)g+=d[f],e[f++]=g&BI_DM,g>>=BI_DB;g+=a.s}c.s=0>g?-1:0;0<g?e[f++]=g:-1>g&&(e[f++]=BI_DV+g);c.t=f;c.clamp()}function bnAdd(a){var c=nbi();this.addTo(a,c);return c}function bnSubtract(a){var c=nbi();this.subTo(a,c);return c}
  46. function bnMultiply(a){var c=nbi();this.multiplyTo(a,c);return c}function bnDivide(a){var c=nbi();this.divRemTo(a,c,null);return c}function bnRemainder(a){var c=nbi();this.divRemTo(a,null,c);return c}function bnDivideAndRemainder(a){var c=nbi(),b=nbi();this.divRemTo(a,c,b);return[c,b]}function bnpDMultiply(a){this.array[this.t]=this.am(0,a-1,this,0,0,this.t);++this.t;this.clamp()}
  47. function bnpDAddOffset(a,c){for(var b=this.array;this.t<=c;)b[this.t++]=0;for(b[c]+=a;b[c]>=BI_DV;)b[c]-=BI_DV,++c>=this.t&&(b[this.t++]=0),++b[c]}function NullExp(){}function nNop(a){return a}function nMulTo(a,c,b){a.multiplyTo(c,b)}function nSqrTo(a,c){a.squareTo(c)}NullExp.prototype.convert=nNop;NullExp.prototype.revert=nNop;NullExp.prototype.mulTo=nMulTo;NullExp.prototype.sqrTo=nSqrTo;function bnPow(a){return this.exp(a,new NullExp)}
  48. function bnpMultiplyLowerTo(a,c,b){var d=b.array,e=a.array,f=Math.min(this.t+a.t,c);b.s=0;for(b.t=f;0<f;)d[--f]=0;var g;for(g=b.t-this.t;f<g;++f)d[f+this.t]=this.am(0,e[f],b,f,0,this.t);for(g=Math.min(a.t,c);f<g;++f)this.am(0,e[f],b,f,0,c-f);b.clamp()}function bnpMultiplyUpperTo(a,c,b){var d=b.array,e=a.array;--c;var f=b.t=this.t+a.t-c;for(b.s=0;0<=--f;)d[f]=0;for(f=Math.max(c-this.t,0);f<a.t;++f)d[this.t+f-c]=this.am(c-f,e[f],b,0,0,this.t+f-c);b.clamp();b.drShiftTo(1,b)}
  49. function Barrett(a){this.r2=nbi();this.q3=nbi();BigInteger.ONE.dlShiftTo(2*a.t,this.r2);this.mu=this.r2.divide(a);this.m=a}function barrettConvert(a){if(0>a.s||a.t>2*this.m.t)return a.mod(this.m);if(0>a.compareTo(this.m))return a;var c=nbi();a.copyTo(c);this.reduce(c);return c}function barrettRevert(a){return a}
  50. function barrettReduce(a){a.drShiftTo(this.m.t-1,this.r2);a.t>this.m.t+1&&(a.t=this.m.t+1,a.clamp());this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);for(this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);0>a.compareTo(this.r2);)a.dAddOffset(1,this.m.t+1);for(a.subTo(this.r2,a);0<=a.compareTo(this.m);)a.subTo(this.m,a)}function barrettSqrTo(a,c){a.squareTo(c);this.reduce(c)}function barrettMulTo(a,c,b){a.multiplyTo(c,b);this.reduce(b)}Barrett.prototype.convert=barrettConvert;
  51. Barrett.prototype.revert=barrettRevert;Barrett.prototype.reduce=barrettReduce;Barrett.prototype.mulTo=barrettMulTo;Barrett.prototype.sqrTo=barrettSqrTo;
  52. function bnModPow(a,c){var b=a.array,d=a.bitLength(),e,f=nbv(1),g;if(0>=d)return f;e=18>d?1:48>d?3:144>d?4:768>d?5:6;g=8>d?new Classic(c):c.isEven()?new Barrett(c):new Montgomery(c);var h=[],l=3,m=e-1,q=(1<<e)-1;h[1]=g.convert(this);if(1<e)for(d=nbi(),g.sqrTo(h[1],d);l<=q;)h[l]=nbi(),g.mulTo(d,h[l-2],h[l]),l+=2;for(var p=a.t-1,y,A=!0,B=nbi(),d=nbits(b[p])-1;0<=p;){d>=m?y=b[p]>>d-m&q:(y=(b[p]&(1<<d+1)-1)<<m-d,0<p&&(y|=b[p-1]>>BI_DB+d-m));for(l=e;0==(y&1);)y>>=1,--l;0>(d-=l)&&(d+=BI_DB,--p);if(A)h[y].copyTo(f),
  53. A=!1;else{for(;1<l;)g.sqrTo(f,B),g.sqrTo(B,f),l-=2;0<l?g.sqrTo(f,B):(l=f,f=B,B=l);g.mulTo(B,h[y],f)}for(;0<=p&&0==(b[p]&1<<d);)g.sqrTo(f,B),l=f,f=B,B=l,0>--d&&(d=BI_DB-1,--p)}return g.revert(f)}
  54. function bnGCD(a){var c=0>this.s?this.negate():this.clone();a=0>a.s?a.negate():a.clone();if(0>c.compareTo(a)){var b=c,c=a;a=b}var b=c.getLowestSetBit(),d=a.getLowestSetBit();if(0>d)return c;b<d&&(d=b);0<d&&(c.rShiftTo(d,c),a.rShiftTo(d,a));for(;0<c.signum();)0<(b=c.getLowestSetBit())&&c.rShiftTo(b,c),0<(b=a.getLowestSetBit())&&a.rShiftTo(b,a),0<=c.compareTo(a)?(c.subTo(a,c),c.rShiftTo(1,c)):(a.subTo(c,a),a.rShiftTo(1,a));0<d&&a.lShiftTo(d,a);return a}
  55. function bnpModInt(a){var c=this.array;if(0>=a)return 0;var b=BI_DV%a,d=0>this.s?a-1:0;if(0<this.t)if(0==b)d=c[0]%a;else for(var e=this.t-1;0<=e;--e)d=(b*d+c[e])%a;return d}
  56. function bnModInverse(a){var c=a.isEven();if(this.isEven()&&c||0==a.signum())return BigInteger.ZERO;for(var b=a.clone(),d=this.clone(),e=nbv(1),f=nbv(0),g=nbv(0),h=nbv(1);0!=b.signum();){for(;b.isEven();)b.rShiftTo(1,b),c?(e.isEven()&&f.isEven()||(e.addTo(this,e),f.subTo(a,f)),e.rShiftTo(1,e)):f.isEven()||f.subTo(a,f),f.rShiftTo(1,f);for(;d.isEven();)d.rShiftTo(1,d),c?(g.isEven()&&h.isEven()||(g.addTo(this,g),h.subTo(a,h)),g.rShiftTo(1,g)):h.isEven()||h.subTo(a,h),h.rShiftTo(1,h);0<=b.compareTo(d)?
  57. (b.subTo(d,b),c&&e.subTo(g,e),f.subTo(h,f)):(d.subTo(b,d),c&&g.subTo(e,g),h.subTo(f,h))}if(0!=d.compareTo(BigInteger.ONE))return BigInteger.ZERO;if(0<=h.compareTo(a))return h.subtract(a);if(0>h.signum())h.addTo(a,h);else return h;return 0>h.signum()?h.add(a):h}
  58. var lowprimes=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509],lplim=67108864/lowprimes[lowprimes.length-1];
  59. function bnIsProbablePrime(a){var c,b=this.abs(),d=b.array;if(1==b.t&&d[0]<=lowprimes[lowprimes.length-1]){for(c=0;c<lowprimes.length;++c)if(d[0]==lowprimes[c])return!0;return!1}if(b.isEven())return!1;for(c=1;c<lowprimes.length;){for(var d=lowprimes[c],e=c+1;e<lowprimes.length&&d<lplim;)d*=lowprimes[e++];for(d=b.modInt(d);c<e;)if(0==d%lowprimes[c++])return!1}return b.millerRabin(a)}
  60. function bnpMillerRabin(a){var c=this.subtract(BigInteger.ONE),b=c.getLowestSetBit();if(0>=b)return!1;var d=c.shiftRight(b);a=a+1>>1;a>lowprimes.length&&(a=lowprimes.length);for(var e=nbi(),f=0;f<a;++f){e.fromInt(lowprimes[f]);var g=e.modPow(d,this);if(0!=g.compareTo(BigInteger.ONE)&&0!=g.compareTo(c)){for(var h=1;h++<b&&0!=g.compareTo(c);)if(g=g.modPowInt(2,this),0==g.compareTo(BigInteger.ONE))return!1;if(0!=g.compareTo(c))return!1}}return!0}BigInteger.prototype.chunkSize=bnpChunkSize;
  61. BigInteger.prototype.toRadix=bnpToRadix;BigInteger.prototype.fromRadix=bnpFromRadix;BigInteger.prototype.fromNumber=bnpFromNumber;BigInteger.prototype.bitwiseTo=bnpBitwiseTo;BigInteger.prototype.changeBit=bnpChangeBit;BigInteger.prototype.addTo=bnpAddTo;BigInteger.prototype.dMultiply=bnpDMultiply;BigInteger.prototype.dAddOffset=bnpDAddOffset;BigInteger.prototype.multiplyLowerTo=bnpMultiplyLowerTo;BigInteger.prototype.multiplyUpperTo=bnpMultiplyUpperTo;BigInteger.prototype.modInt=bnpModInt;
  62. BigInteger.prototype.millerRabin=bnpMillerRabin;BigInteger.prototype.clone=bnClone;BigInteger.prototype.intValue=bnIntValue;BigInteger.prototype.byteValue=bnByteValue;BigInteger.prototype.shortValue=bnShortValue;BigInteger.prototype.signum=bnSigNum;BigInteger.prototype.toByteArray=bnToByteArray;BigInteger.prototype.equals=bnEquals;BigInteger.prototype.min=bnMin;BigInteger.prototype.max=bnMax;BigInteger.prototype.and=bnAnd;BigInteger.prototype.or=bnOr;BigInteger.prototype.xor=bnXor;
  63. BigInteger.prototype.andNot=bnAndNot;BigInteger.prototype.not=bnNot;BigInteger.prototype.shiftLeft=bnShiftLeft;BigInteger.prototype.shiftRight=bnShiftRight;BigInteger.prototype.getLowestSetBit=bnGetLowestSetBit;BigInteger.prototype.bitCount=bnBitCount;BigInteger.prototype.testBit=bnTestBit;BigInteger.prototype.setBit=bnSetBit;BigInteger.prototype.clearBit=bnClearBit;BigInteger.prototype.flipBit=bnFlipBit;BigInteger.prototype.add=bnAdd;BigInteger.prototype.subtract=bnSubtract;
  64. BigInteger.prototype.multiply=bnMultiply;BigInteger.prototype.divide=bnDivide;BigInteger.prototype.remainder=bnRemainder;BigInteger.prototype.divideAndRemainder=bnDivideAndRemainder;BigInteger.prototype.modPow=bnModPow;BigInteger.prototype.modInverse=bnModInverse;BigInteger.prototype.pow=bnPow;BigInteger.prototype.gcd=bnGCD;BigInteger.prototype.isProbablePrime=bnIsProbablePrime;function Arcfour(){this.j=this.i=0;this.S=[]}
  65. function ARC4init(a){var c,b,d;for(c=0;256>c;++c)this.S[c]=c;for(c=b=0;256>c;++c)b=b+this.S[c]+a[c%a.length]&255,d=this.S[c],this.S[c]=this.S[b],this.S[b]=d;this.j=this.i=0}function ARC4next(){var a;this.i=this.i+1&255;this.j=this.j+this.S[this.i]&255;a=this.S[this.i];this.S[this.i]=this.S[this.j];this.S[this.j]=a;return this.S[a+this.S[this.i]&255]}Arcfour.prototype.init=ARC4init;Arcfour.prototype.next=ARC4next;function prng_newstate(){return new Arcfour}var rng_psize=256,rng_state,rng_pool,rng_pptr;
  66. function rng_seed_int(a){rng_pool[rng_pptr++]^=a&255;rng_pool[rng_pptr++]^=a>>8&255;rng_pool[rng_pptr++]^=a>>16&255;rng_pool[rng_pptr++]^=a>>24&255;rng_pptr>=rng_psize&&(rng_pptr-=rng_psize)}function rng_seed_time(){rng_seed_int(1122926989487)}if(null==rng_pool){rng_pool=[];rng_pptr=0;for(var t;rng_pptr<rng_psize;)t=Math.floor(65536*Math.random()),rng_pool[rng_pptr++]=t>>>8,rng_pool[rng_pptr++]=t&255;rng_pptr=0;rng_seed_time()}
  67. function rng_get_byte(){if(null==rng_state){rng_seed_time();rng_state=prng_newstate();rng_state.init(rng_pool);for(rng_pptr=0;rng_pptr<rng_pool.length;++rng_pptr)rng_pool[rng_pptr]=0;rng_pptr=0}return rng_state.next()}function rng_get_bytes(a){var c;for(c=0;c<a.length;++c)a[c]=rng_get_byte()}function SecureRandom(){}SecureRandom.prototype.nextBytes=rng_get_bytes;function parseBigInt(a,c){return new BigInteger(a,c)}
  68. function linebrk(a,c){for(var b="",d=0;d+c<a.length;)b+=a.substring(d,d+c)+"\n",d+=c;return b+a.substring(d,a.length)}function byte2Hex(a){return 16>a?"0"+a.toString(16):a.toString(16)}function pkcs1pad2(a,c){if(c<a.length+11)return alert("Message too long for RSA"),null;for(var b=[],d=a.length-1;0<=d&&0<c;)b[--c]=a.charCodeAt(d--);b[--c]=0;for(var d=new SecureRandom,e=[];2<c;){for(e[0]=0;0==e[0];)d.nextBytes(e);b[--c]=e[0]}b[--c]=2;b[--c]=0;return new BigInteger(b)}
  69. function RSAKey(){this.n=null;this.e=0;this.coeff=this.dmq1=this.dmp1=this.q=this.p=this.d=null}function RSASetPublic(a,c){null!=a&&null!=c&&0<a.length&&0<c.length?(this.n=parseBigInt(a,16),this.e=parseInt(c,16)):alert("Invalid RSA public key")}function RSADoPublic(a){return a.modPowInt(this.e,this.n)}function RSAEncrypt(a){a=pkcs1pad2(a,this.n.bitLength()+7>>3);if(null==a)return null;a=this.doPublic(a);if(null==a)return null;a=a.toString(16);return 0==(a.length&1)?a:"0"+a}
  70. RSAKey.prototype.doPublic=RSADoPublic;RSAKey.prototype.setPublic=RSASetPublic;RSAKey.prototype.encrypt=RSAEncrypt;function pkcs1unpad2(a,c){for(var b=a.toByteArray(),d=0;d<b.length&&0==b[d];)++d;if(b.length-d!=c-1||2!=b[d])return null;for(++d;0!=b[d];)if(++d>=b.length)return null;for(var e="";++d<b.length;)e+=String.fromCharCode(b[d]);return e}
  71. function RSASetPrivate(a,c,b){null!=a&&null!=c&&0<a.length&&0<c.length?(this.n=parseBigInt(a,16),this.e=parseInt(c,16),this.d=parseBigInt(b,16)):alert("Invalid RSA private key")}
  72. function RSASetPrivateEx(a,c,b,d,e,f,g,h){null!=a&&null!=c&&0<a.length&&0<c.length?(this.n=parseBigInt(a,16),this.e=parseInt(c,16),this.d=parseBigInt(b,16),this.p=parseBigInt(d,16),this.q=parseBigInt(e,16),this.dmp1=parseBigInt(f,16),this.dmq1=parseBigInt(g,16),this.coeff=parseBigInt(h,16)):alert("Invalid RSA private key")}
  73. function RSAGenerate(a,c){var b=new SecureRandom,d=a>>1;this.e=parseInt(c,16);for(var e=new BigInteger(c,16);;){for(;this.p=new BigInteger(a-d,1,b),0!=this.p.subtract(BigInteger.ONE).gcd(e).compareTo(BigInteger.ONE)||!this.p.isProbablePrime(10););for(;this.q=new BigInteger(d,1,b),0!=this.q.subtract(BigInteger.ONE).gcd(e).compareTo(BigInteger.ONE)||!this.q.isProbablePrime(10););if(0>=this.p.compareTo(this.q)){var f=this.p;this.p=this.q;this.q=f}var f=this.p.subtract(BigInteger.ONE),g=this.q.subtract(BigInteger.ONE),
  74. h=f.multiply(g);if(0==h.gcd(e).compareTo(BigInteger.ONE)){this.n=this.p.multiply(this.q);this.d=e.modInverse(h);this.dmp1=this.d.mod(f);this.dmq1=this.d.mod(g);this.coeff=this.q.modInverse(this.p);break}}}function RSADoPrivate(a){if(null==this.p||null==this.q)return a.modPow(this.d,this.n);var c=a.mod(this.p).modPow(this.dmp1,this.p);for(a=a.mod(this.q).modPow(this.dmq1,this.q);0>c.compareTo(a);)c=c.add(this.p);return c.subtract(a).multiply(this.coeff).mod(this.p).multiply(this.q).add(a)}
  75. function RSADecrypt(a){a=parseBigInt(a,16);a=this.doPrivate(a);return null==a?null:pkcs1unpad2(a,this.n.bitLength()+7>>3)}RSAKey.prototype.doPrivate=RSADoPrivate;RSAKey.prototype.setPrivate=RSASetPrivate;RSAKey.prototype.setPrivateEx=RSASetPrivateEx;RSAKey.prototype.generate=RSAGenerate;RSAKey.prototype.decrypt=RSADecrypt;nValue="a5261939975948bb7a58dffe5ff54e65f0498f9175f5a09288810b8975871e99af3b5dd94057b0fc07535f5f97444504fa35169d461d0d30cf0192e307727c065168c788771c561a9400fb49175e9e6aa4e23fe11af69e9412dd23b0cb6684c4c2429bce139e848ab26d0829073351f4acd36074eafd036a5eb83359d2a698d3";
  76. eValue="10001";dValue="8e9912f6d3645894e8d38cb58c0db81ff516cf4c7e5a14c7f1eddb1459d2cded4d8d293fc97aee6aefb861859c8b6a3d1dfe710463e1f9ddc72048c09751971c4a580aa51eb523357a3cc48d31cfad1d4a165066ed92d4748fb6571211da5cb14bc11b6e2df7c1a559e6d5ac1cd5c94703a22891464fba23d0d965086277a161";pValue="d090ce58a92c75233a6486cb0a9209bf3583b64f540c76f5294bb97d285eed33aec220bde14b2417951178ac152ceab6da7090905b478195498b352048f15e7d";qValue="cab575dc652bb66df15a0359609d51d1db184750c00c6698b90ef3465c99655103edbf0d54c56aec0ce3c4d22592338092a126a0cc49f65a4a30d222b411e58f";
  77. dmp1Value="1a24bca8e273df2f0e47c199bbf678604e7df7215480c77c8db39f49b000ce2cf7500038acfff5433b7d582a01f1826e6f4d42e1c57f5e1fef7b12aabc59fd25";dmq1Value="3d06982efbbe47339e1f6d36b1216b8a741d410b0c662f54f7118b27b9a4ec9d914337eb39841d8666f3034408cf94f5b62f11c402fc994fe15a05493150d9fd";coeffValue="3a3e731acd8960b7ff9eb81a7ff93bd1cfa74cbd56987db58b4594fb09c09084db1734c8143f98b602b981aaa9243ca28deb69b5b280ee8dcee0fd2625e53250";setupEngine(am3,28);
  78. var TEXT="The quick brown fox jumped over the extremely lazy frog! Now is the time for all good men to come to the party.",encrypted;function encrypt(){var a=new RSAKey;a.setPublic(nValue,eValue);a.setPrivateEx(nValue,eValue,dValue,pValue,qValue,dmp1Value,dmq1Value,coeffValue);encrypted=a.encrypt(TEXT)}
  79. function decrypt(){var a=new RSAKey;a.setPublic(nValue,eValue);a.setPrivateEx(nValue,eValue,dValue,pValue,qValue,dmp1Value,dmq1Value,coeffValue);if(a.decrypt(encrypted)!=TEXT)throw Error("Crypto operation failed");};
  80. ////////////////////////////////////////////////////////////////////////////////
  81. // Runner
  82. ////////////////////////////////////////////////////////////////////////////////
  83. var success = true;
  84. function NotifyStart(name) {
  85. }
  86. function NotifyError(name, error) {
  87. WScript.Echo(name + " : ERROR : " + error.stack);
  88. success = false;
  89. }
  90. function NotifyResult(name, score) {
  91. if (success) {
  92. WScript.Echo("### SCORE:", score);
  93. }
  94. }
  95. function NotifyScore(score) {
  96. }
  97. BenchmarkSuite.RunSuites({
  98. NotifyStart: NotifyStart,
  99. NotifyError: NotifyError,
  100. NotifyResult: NotifyResult,
  101. NotifyScore: NotifyScore
  102. });