node-forge 1.3.2 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/jsbn.js CHANGED
@@ -124,10 +124,12 @@ if(typeof(navigator) === 'undefined')
124
124
  } else if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
125
125
  BigInteger.prototype.am = am2;
126
126
  dbits = 30;
127
- } else if(j_lm && (navigator.appName != "Netscape")) {
127
+ }
128
+ else if(j_lm && (navigator.appName != "Netscape")) {
128
129
  BigInteger.prototype.am = am1;
129
130
  dbits = 26;
130
- } else { // Mozilla/Netscape seems to prefer am3
131
+ }
132
+ else { // Mozilla/Netscape seems to prefer am3
131
133
  BigInteger.prototype.am = am3;
132
134
  dbits = 28;
133
135
  }
@@ -202,7 +204,8 @@ function bnpFromString(s,b) {
202
204
  else if(sh+k > this.DB) {
203
205
  this.data[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
204
206
  this.data[this.t++] = (x>>(this.DB-sh));
205
- } else
207
+ }
208
+ else
206
209
  this.data[this.t-1] |= x<<sh;
207
210
  sh += k;
208
211
  if(sh >= this.DB) sh -= this.DB;
@@ -239,7 +242,8 @@ function bnToString(b) {
239
242
  if(p < k) {
240
243
  d = (this.data[i]&((1<<p)-1))<<(k-p);
241
244
  d |= this.data[--i]>>(p+=this.DB-k);
242
- } else {
245
+ }
246
+ else {
243
247
  d = (this.data[i]>>(p-=k))&km;
244
248
  if(p <= 0) { p += this.DB; --i; }
245
249
  }
@@ -351,7 +355,8 @@ function bnpSubTo(a,r) {
351
355
  c >>= this.DB;
352
356
  }
353
357
  c += this.s;
354
- } else {
358
+ }
359
+ else {
355
360
  c += this.s;
356
361
  while(i < a.t) {
357
362
  c -= a.data[i];
@@ -411,7 +416,8 @@ function bnpDivRemTo(m,q,r) {
411
416
  if(r == null) r = nbi();
412
417
  var y = nbi(), ts = this.s, ms = m.s;
413
418
  var nsh = this.DB-nbits(pm.data[pm.t-1]); // normalize modulus
414
- if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); } else { pm.copyTo(y); pt.copyTo(r); }
419
+ if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
420
+ else { pm.copyTo(y); pt.copyTo(r); }
415
421
  var ys = y.t;
416
422
  var y0 = y.data[ys-1];
417
423
  if(y0 == 0) return;
@@ -608,326 +614,335 @@ BigInteger.ONE = nbv(1);
608
614
 
609
615
  // jsbn2 lib
610
616
 
611
- //Copyright (c) 2005-2009 Tom Wu
612
- //All Rights Reserved.
613
- //See "LICENSE" for details (See jsbn.js for LICENSE).
617
+ // Copyright (c) 2005-2009 Tom Wu
618
+ // All Rights Reserved.
619
+ // See "LICENSE" for details (See jsbn.js for LICENSE).
614
620
 
615
- //Extended JavaScript BN functions, required for RSA private ops.
621
+ // Extended JavaScript BN functions, required for RSA private ops.
616
622
 
617
- //Version 1.1: new BigInteger("0", 10) returns "proper" zero
623
+ // Version 1.1: new BigInteger("0", 10) returns "proper" zero
624
+ // Version 1.2: square() API, isProbablePrime fix
618
625
 
619
- //(public)
626
+ // (public)
620
627
  function bnClone() { var r = nbi(); this.copyTo(r); return r; }
621
628
 
622
- //(public) return value as integer
629
+ // (public) return value as integer
623
630
  function bnIntValue() {
624
- if(this.s < 0) {
625
- if(this.t == 1) return this.data[0]-this.DV;
626
- else if(this.t == 0) return -1;
627
- } else if(this.t == 1) return this.data[0];
628
- else if(this.t == 0) return 0;
629
- // assumes 16 < DB < 32
630
- return ((this.data[1]&((1<<(32-this.DB))-1))<<this.DB)|this.data[0];
631
+ if(this.s < 0) {
632
+ if(this.t == 1) return this.data[0]-this.DV;
633
+ else if(this.t == 0) return -1;
634
+ }
635
+ else if(this.t == 1) return this.data[0];
636
+ else if(this.t == 0) return 0;
637
+ // assumes 16 < DB < 32
638
+ return ((this.data[1]&((1<<(32-this.DB))-1))<<this.DB)|this.data[0];
631
639
  }
632
640
 
633
- //(public) return value as byte
641
+ // (public) return value as byte
634
642
  function bnByteValue() { return (this.t==0)?this.s:(this.data[0]<<24)>>24; }
635
643
 
636
- //(public) return value as short (assumes DB>=16)
644
+ // (public) return value as short (assumes DB>=16)
637
645
  function bnShortValue() { return (this.t==0)?this.s:(this.data[0]<<16)>>16; }
638
646
 
639
- //(protected) return x s.t. r^x < DV
647
+ // (protected) return x s.t. r^x < DV
640
648
  function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
641
649
 
642
- //(public) 0 if this == 0, 1 if this > 0
650
+ // (public) 0 if this == 0, 1 if this > 0
643
651
  function bnSigNum() {
644
- if(this.s < 0) return -1;
645
- else if(this.t <= 0 || (this.t == 1 && this.data[0] <= 0)) return 0;
646
- else return 1;
652
+ if(this.s < 0) return -1;
653
+ else if(this.t <= 0 || (this.t == 1 && this.data[0] <= 0)) return 0;
654
+ else return 1;
647
655
  }
648
656
 
649
- //(protected) convert to radix string
657
+ // (protected) convert to radix string
650
658
  function bnpToRadix(b) {
651
- if(b == null) b = 10;
652
- if(this.signum() == 0 || b < 2 || b > 36) return "0";
653
- var cs = this.chunkSize(b);
654
- var a = Math.pow(b,cs);
655
- var d = nbv(a), y = nbi(), z = nbi(), r = "";
656
- this.divRemTo(d,y,z);
657
- while(y.signum() > 0) {
658
- r = (a+z.intValue()).toString(b).substr(1) + r;
659
- y.divRemTo(d,y,z);
660
- }
661
- return z.intValue().toString(b) + r;
659
+ if(b == null) b = 10;
660
+ if(this.signum() == 0 || b < 2 || b > 36) return "0";
661
+ var cs = this.chunkSize(b);
662
+ var a = Math.pow(b,cs);
663
+ var d = nbv(a), y = nbi(), z = nbi(), r = "";
664
+ this.divRemTo(d,y,z);
665
+ while(y.signum() > 0) {
666
+ r = (a+z.intValue()).toString(b).substr(1) + r;
667
+ y.divRemTo(d,y,z);
668
+ }
669
+ return z.intValue().toString(b) + r;
662
670
  }
663
671
 
664
- //(protected) convert from radix string
672
+ // (protected) convert from radix string
665
673
  function bnpFromRadix(s,b) {
666
- this.fromInt(0);
667
- if(b == null) b = 10;
668
- var cs = this.chunkSize(b);
669
- var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
670
- for(var i = 0; i < s.length; ++i) {
671
- var x = intAt(s,i);
672
- if(x < 0) {
673
- if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
674
- continue;
675
- }
676
- w = b*w+x;
677
- if(++j >= cs) {
678
- this.dMultiply(d);
679
- this.dAddOffset(w,0);
680
- j = 0;
681
- w = 0;
682
- }
683
- }
684
- if(j > 0) {
685
- this.dMultiply(Math.pow(b,j));
686
- this.dAddOffset(w,0);
687
- }
688
- if(mi) BigInteger.ZERO.subTo(this,this);
689
- }
690
-
691
- //(protected) alternate constructor
674
+ this.fromInt(0);
675
+ if(b == null) b = 10;
676
+ var cs = this.chunkSize(b);
677
+ var d = Math.pow(b,cs), mi = false, j = 0, w = 0;
678
+ for(var i = 0; i < s.length; ++i) {
679
+ var x = intAt(s,i);
680
+ if(x < 0) {
681
+ if(s.charAt(i) == "-" && this.signum() == 0) mi = true;
682
+ continue;
683
+ }
684
+ w = b*w+x;
685
+ if(++j >= cs) {
686
+ this.dMultiply(d);
687
+ this.dAddOffset(w,0);
688
+ j = 0;
689
+ w = 0;
690
+ }
691
+ }
692
+ if(j > 0) {
693
+ this.dMultiply(Math.pow(b,j));
694
+ this.dAddOffset(w,0);
695
+ }
696
+ if(mi) BigInteger.ZERO.subTo(this,this);
697
+ }
698
+
699
+ // (protected) alternate constructor
692
700
  function bnpFromNumber(a,b,c) {
693
- if("number" == typeof b) {
694
- // new BigInteger(int,int,RNG)
695
- if(a < 2) this.fromInt(1);
696
- else {
697
- this.fromNumber(a,c);
698
- if(!this.testBit(a-1)) // force MSB set
699
- this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
700
- if(this.isEven()) this.dAddOffset(1,0); // force odd
701
- while(!this.isProbablePrime(b)) {
702
- this.dAddOffset(2,0);
703
- if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
704
- }
705
- }
706
- } else {
707
- // new BigInteger(int,RNG)
708
- var x = new Array(), t = a&7;
709
- x.length = (a>>3)+1;
710
- b.nextBytes(x);
711
- if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
712
- this.fromString(x,256);
713
- }
714
- }
715
-
716
- //(public) convert to bigendian byte array
701
+ if("number" == typeof b) {
702
+ // new BigInteger(int,int,RNG)
703
+ if(a < 2) this.fromInt(1);
704
+ else {
705
+ this.fromNumber(a,c);
706
+ if(!this.testBit(a-1)) // force MSB set
707
+ this.bitwiseTo(BigInteger.ONE.shiftLeft(a-1),op_or,this);
708
+ if(this.isEven()) this.dAddOffset(1,0); // force odd
709
+ while(!this.isProbablePrime(b)) {
710
+ this.dAddOffset(2,0);
711
+ if(this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a-1),this);
712
+ }
713
+ }
714
+ }
715
+ else {
716
+ // new BigInteger(int,RNG)
717
+ var x = new Array(), t = a&7;
718
+ x.length = (a>>3)+1;
719
+ b.nextBytes(x);
720
+ if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
721
+ this.fromString(x,256);
722
+ }
723
+ }
724
+
725
+ // (public) convert to bigendian byte array
717
726
  function bnToByteArray() {
718
- var i = this.t, r = new Array();
719
- r[0] = this.s;
720
- var p = this.DB-(i*this.DB)%8, d, k = 0;
721
- if(i-- > 0) {
722
- if(p < this.DB && (d = this.data[i]>>p) != (this.s&this.DM)>>p)
723
- r[k++] = d|(this.s<<(this.DB-p));
724
- while(i >= 0) {
725
- if(p < 8) {
726
- d = (this.data[i]&((1<<p)-1))<<(8-p);
727
- d |= this.data[--i]>>(p+=this.DB-8);
728
- } else {
729
- d = (this.data[i]>>(p-=8))&0xff;
730
- if(p <= 0) { p += this.DB; --i; }
731
- }
732
- if((d&0x80) != 0) d |= -256;
733
- if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
734
- if(k > 0 || d != this.s) r[k++] = d;
735
- }
736
- }
737
- return r;
727
+ var i = this.t, r = new Array();
728
+ r[0] = this.s;
729
+ var p = this.DB-(i*this.DB)%8, d, k = 0;
730
+ if(i-- > 0) {
731
+ if(p < this.DB && (d = this.data[i]>>p) != (this.s&this.DM)>>p)
732
+ r[k++] = d|(this.s<<(this.DB-p));
733
+ while(i >= 0) {
734
+ if(p < 8) {
735
+ d = (this.data[i]&((1<<p)-1))<<(8-p);
736
+ d |= this.data[--i]>>(p+=this.DB-8);
737
+ }
738
+ else {
739
+ d = (this.data[i]>>(p-=8))&0xff;
740
+ if(p <= 0) { p += this.DB; --i; }
741
+ }
742
+ if((d&0x80) != 0) d |= -256;
743
+ if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
744
+ if(k > 0 || d != this.s) r[k++] = d;
745
+ }
746
+ }
747
+ return r;
738
748
  }
739
749
 
740
750
  function bnEquals(a) { return(this.compareTo(a)==0); }
741
751
  function bnMin(a) { return(this.compareTo(a)<0)?this:a; }
742
752
  function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
743
753
 
744
- //(protected) r = this op a (bitwise)
754
+ // (protected) r = this op a (bitwise)
745
755
  function bnpBitwiseTo(a,op,r) {
746
- var i, f, m = Math.min(a.t,this.t);
747
- for(i = 0; i < m; ++i) r.data[i] = op(this.data[i],a.data[i]);
748
- if(a.t < this.t) {
749
- f = a.s&this.DM;
750
- for(i = m; i < this.t; ++i) r.data[i] = op(this.data[i],f);
751
- r.t = this.t;
752
- } else {
753
- f = this.s&this.DM;
754
- for(i = m; i < a.t; ++i) r.data[i] = op(f,a.data[i]);
755
- r.t = a.t;
756
- }
757
- r.s = op(this.s,a.s);
758
- r.clamp();
759
- }
760
-
761
- //(public) this & a
756
+ var i, f, m = Math.min(a.t,this.t);
757
+ for(i = 0; i < m; ++i) r.data[i] = op(this.data[i],a.data[i]);
758
+ if(a.t < this.t) {
759
+ f = a.s&this.DM;
760
+ for(i = m; i < this.t; ++i) r.data[i] = op(this.data[i],f);
761
+ r.t = this.t;
762
+ }
763
+ else {
764
+ f = this.s&this.DM;
765
+ for(i = m; i < a.t; ++i) r.data[i] = op(f,a.data[i]);
766
+ r.t = a.t;
767
+ }
768
+ r.s = op(this.s,a.s);
769
+ r.clamp();
770
+ }
771
+
772
+ // (public) this & a
762
773
  function op_and(x,y) { return x&y; }
763
774
  function bnAnd(a) { var r = nbi(); this.bitwiseTo(a,op_and,r); return r; }
764
775
 
765
- //(public) this | a
776
+ // (public) this | a
766
777
  function op_or(x,y) { return x|y; }
767
778
  function bnOr(a) { var r = nbi(); this.bitwiseTo(a,op_or,r); return r; }
768
779
 
769
- //(public) this ^ a
780
+ // (public) this ^ a
770
781
  function op_xor(x,y) { return x^y; }
771
782
  function bnXor(a) { var r = nbi(); this.bitwiseTo(a,op_xor,r); return r; }
772
783
 
773
- //(public) this & ~a
784
+ // (public) this & ~a
774
785
  function op_andnot(x,y) { return x&~y; }
775
786
  function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
776
787
 
777
- //(public) ~this
788
+ // (public) ~this
778
789
  function bnNot() {
779
- var r = nbi();
780
- for(var i = 0; i < this.t; ++i) r.data[i] = this.DM&~this.data[i];
781
- r.t = this.t;
782
- r.s = ~this.s;
783
- return r;
790
+ var r = nbi();
791
+ for(var i = 0; i < this.t; ++i) r.data[i] = this.DM&~this.data[i];
792
+ r.t = this.t;
793
+ r.s = ~this.s;
794
+ return r;
784
795
  }
785
796
 
786
- //(public) this << n
797
+ // (public) this << n
787
798
  function bnShiftLeft(n) {
788
- var r = nbi();
789
- if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
790
- return r;
799
+ var r = nbi();
800
+ if(n < 0) this.rShiftTo(-n,r); else this.lShiftTo(n,r);
801
+ return r;
791
802
  }
792
803
 
793
- //(public) this >> n
804
+ // (public) this >> n
794
805
  function bnShiftRight(n) {
795
- var r = nbi();
796
- if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
797
- return r;
806
+ var r = nbi();
807
+ if(n < 0) this.lShiftTo(-n,r); else this.rShiftTo(n,r);
808
+ return r;
798
809
  }
799
810
 
800
- //return index of lowest 1-bit in x, x < 2^31
811
+ // return index of lowest 1-bit in x, x < 2^31
801
812
  function lbit(x) {
802
- if(x == 0) return -1;
803
- var r = 0;
804
- if((x&0xffff) == 0) { x >>= 16; r += 16; }
805
- if((x&0xff) == 0) { x >>= 8; r += 8; }
806
- if((x&0xf) == 0) { x >>= 4; r += 4; }
807
- if((x&3) == 0) { x >>= 2; r += 2; }
808
- if((x&1) == 0) ++r;
809
- return r;
813
+ if(x == 0) return -1;
814
+ var r = 0;
815
+ if((x&0xffff) == 0) { x >>= 16; r += 16; }
816
+ if((x&0xff) == 0) { x >>= 8; r += 8; }
817
+ if((x&0xf) == 0) { x >>= 4; r += 4; }
818
+ if((x&3) == 0) { x >>= 2; r += 2; }
819
+ if((x&1) == 0) ++r;
820
+ return r;
810
821
  }
811
822
 
812
- //(public) returns index of lowest 1-bit (or -1 if none)
823
+ // (public) returns index of lowest 1-bit (or -1 if none)
813
824
  function bnGetLowestSetBit() {
814
- for(var i = 0; i < this.t; ++i)
815
- if(this.data[i] != 0) return i*this.DB+lbit(this.data[i]);
816
- if(this.s < 0) return this.t*this.DB;
817
- return -1;
825
+ for(var i = 0; i < this.t; ++i)
826
+ if(this.data[i] != 0) return i*this.DB+lbit(this.data[i]);
827
+ if(this.s < 0) return this.t*this.DB;
828
+ return -1;
818
829
  }
819
830
 
820
- //return number of 1 bits in x
831
+ // return number of 1 bits in x
821
832
  function cbit(x) {
822
- var r = 0;
823
- while(x != 0) { x &= x-1; ++r; }
824
- return r;
833
+ var r = 0;
834
+ while(x != 0) { x &= x-1; ++r; }
835
+ return r;
825
836
  }
826
837
 
827
- //(public) return number of set bits
838
+ // (public) return number of set bits
828
839
  function bnBitCount() {
829
- var r = 0, x = this.s&this.DM;
830
- for(var i = 0; i < this.t; ++i) r += cbit(this.data[i]^x);
831
- return r;
840
+ var r = 0, x = this.s&this.DM;
841
+ for(var i = 0; i < this.t; ++i) r += cbit(this.data[i]^x);
842
+ return r;
832
843
  }
833
844
 
834
- //(public) true iff nth bit is set
845
+ // (public) true iff nth bit is set
835
846
  function bnTestBit(n) {
836
- var j = Math.floor(n/this.DB);
837
- if(j >= this.t) return(this.s!=0);
838
- return((this.data[j]&(1<<(n%this.DB)))!=0);
847
+ var j = Math.floor(n/this.DB);
848
+ if(j >= this.t) return(this.s!=0);
849
+ return((this.data[j]&(1<<(n%this.DB)))!=0);
839
850
  }
840
851
 
841
- //(protected) this op (1<<n)
852
+ // (protected) this op (1<<n)
842
853
  function bnpChangeBit(n,op) {
843
- var r = BigInteger.ONE.shiftLeft(n);
844
- this.bitwiseTo(r,op,r);
845
- return r;
854
+ var r = BigInteger.ONE.shiftLeft(n);
855
+ this.bitwiseTo(r,op,r);
856
+ return r;
846
857
  }
847
858
 
848
- //(public) this | (1<<n)
859
+ // (public) this | (1<<n)
849
860
  function bnSetBit(n) { return this.changeBit(n,op_or); }
850
861
 
851
- //(public) this & ~(1<<n)
862
+ // (public) this & ~(1<<n)
852
863
  function bnClearBit(n) { return this.changeBit(n,op_andnot); }
853
864
 
854
- //(public) this ^ (1<<n)
865
+ // (public) this ^ (1<<n)
855
866
  function bnFlipBit(n) { return this.changeBit(n,op_xor); }
856
867
 
857
- //(protected) r = this + a
868
+ // (protected) r = this + a
858
869
  function bnpAddTo(a,r) {
859
- var i = 0, c = 0, m = Math.min(a.t,this.t);
860
- while(i < m) {
861
- c += this.data[i]+a.data[i];
862
- r.data[i++] = c&this.DM;
863
- c >>= this.DB;
864
- }
865
- if(a.t < this.t) {
866
- c += a.s;
867
- while(i < this.t) {
868
- c += this.data[i];
869
- r.data[i++] = c&this.DM;
870
- c >>= this.DB;
871
- }
872
- c += this.s;
873
- } else {
874
- c += this.s;
875
- while(i < a.t) {
876
- c += a.data[i];
877
- r.data[i++] = c&this.DM;
878
- c >>= this.DB;
879
- }
880
- c += a.s;
881
- }
882
- r.s = (c<0)?-1:0;
883
- if(c > 0) r.data[i++] = c;
884
- else if(c < -1) r.data[i++] = this.DV+c;
885
- r.t = i;
886
- r.clamp();
887
- }
888
-
889
- //(public) this + a
870
+ var i = 0, c = 0, m = Math.min(a.t,this.t);
871
+ while(i < m) {
872
+ c += this.data[i]+a.data[i];
873
+ r.data[i++] = c&this.DM;
874
+ c >>= this.DB;
875
+ }
876
+ if(a.t < this.t) {
877
+ c += a.s;
878
+ while(i < this.t) {
879
+ c += this.data[i];
880
+ r.data[i++] = c&this.DM;
881
+ c >>= this.DB;
882
+ }
883
+ c += this.s;
884
+ }
885
+ else {
886
+ c += this.s;
887
+ while(i < a.t) {
888
+ c += a.data[i];
889
+ r.data[i++] = c&this.DM;
890
+ c >>= this.DB;
891
+ }
892
+ c += a.s;
893
+ }
894
+ r.s = (c<0)?-1:0;
895
+ if(c > 0) r.data[i++] = c;
896
+ else if(c < -1) r.data[i++] = this.DV+c;
897
+ r.t = i;
898
+ r.clamp();
899
+ }
900
+
901
+ // (public) this + a
890
902
  function bnAdd(a) { var r = nbi(); this.addTo(a,r); return r; }
891
903
 
892
- //(public) this - a
904
+ // (public) this - a
893
905
  function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
894
906
 
895
- //(public) this * a
907
+ // (public) this * a
896
908
  function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
897
909
 
898
- //(public) this / a
910
+ // (public) this^2
911
+ function bnSquare() { var r = nbi(); this.squareTo(r); return r; }
912
+
913
+ // (public) this / a
899
914
  function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
900
915
 
901
- //(public) this % a
916
+ // (public) this % a
902
917
  function bnRemainder(a) { var r = nbi(); this.divRemTo(a,null,r); return r; }
903
918
 
904
- //(public) [this/a,this%a]
919
+ // (public) [this/a,this%a]
905
920
  function bnDivideAndRemainder(a) {
906
- var q = nbi(), r = nbi();
907
- this.divRemTo(a,q,r);
908
- return new Array(q,r);
921
+ var q = nbi(), r = nbi();
922
+ this.divRemTo(a,q,r);
923
+ return new Array(q,r);
909
924
  }
910
925
 
911
- //(protected) this *= n, this >= 0, 1 < n < DV
926
+ // (protected) this *= n, this >= 0, 1 < n < DV
912
927
  function bnpDMultiply(n) {
913
- this.data[this.t] = this.am(0,n-1,this,0,0,this.t);
914
- ++this.t;
915
- this.clamp();
928
+ this.data[this.t] = this.am(0,n-1,this,0,0,this.t);
929
+ ++this.t;
930
+ this.clamp();
916
931
  }
917
932
 
918
- //(protected) this += n << w words, this >= 0
933
+ // (protected) this += n << w words, this >= 0
919
934
  function bnpDAddOffset(n,w) {
920
- if(n == 0) return;
921
- while(this.t <= w) this.data[this.t++] = 0;
922
- this.data[w] += n;
923
- while(this.data[w] >= this.DV) {
924
- this.data[w] -= this.DV;
925
- if(++w >= this.t) this.data[this.t++] = 0;
926
- ++this.data[w];
927
- }
935
+ if(n == 0) return;
936
+ while(this.t <= w) this.data[this.t++] = 0;
937
+ this.data[w] += n;
938
+ while(this.data[w] >= this.DV) {
939
+ this.data[w] -= this.DV;
940
+ if(++w >= this.t) this.data[this.t++] = 0;
941
+ ++this.data[w];
942
+ }
928
943
  }
929
944
 
930
- //A "null" reducer
945
+ // A "null" reducer
931
946
  function NullExp() {}
932
947
  function nNop(x) { return x; }
933
948
  function nMulTo(x,y,r) { x.multiplyTo(y,r); }
@@ -938,68 +953,68 @@ NullExp.prototype.revert = nNop;
938
953
  NullExp.prototype.mulTo = nMulTo;
939
954
  NullExp.prototype.sqrTo = nSqrTo;
940
955
 
941
- //(public) this^e
956
+ // (public) this^e
942
957
  function bnPow(e) { return this.exp(e,new NullExp()); }
943
958
 
944
- //(protected) r = lower n words of "this * a", a.t <= n
945
- //"this" should be the larger one if appropriate.
959
+ // (protected) r = lower n words of "this * a", a.t <= n
960
+ // "this" should be the larger one if appropriate.
946
961
  function bnpMultiplyLowerTo(a,n,r) {
947
- var i = Math.min(this.t+a.t,n);
948
- r.s = 0; // assumes a,this >= 0
949
- r.t = i;
950
- while(i > 0) r.data[--i] = 0;
951
- var j;
952
- for(j = r.t-this.t; i < j; ++i) r.data[i+this.t] = this.am(0,a.data[i],r,i,0,this.t);
953
- for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a.data[i],r,i,0,n-i);
954
- r.clamp();
955
- }
956
-
957
- //(protected) r = "this * a" without lower n words, n > 0
958
- //"this" should be the larger one if appropriate.
962
+ var i = Math.min(this.t+a.t,n);
963
+ r.s = 0; // assumes a,this >= 0
964
+ r.t = i;
965
+ while(i > 0) r.data[--i] = 0;
966
+ var j;
967
+ for(j = r.t-this.t; i < j; ++i) r.data[i+this.t] = this.am(0,a.data[i],r,i,0,this.t);
968
+ for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a.data[i],r,i,0,n-i);
969
+ r.clamp();
970
+ }
971
+
972
+ // (protected) r = "this * a" without lower n words, n > 0
973
+ // "this" should be the larger one if appropriate.
959
974
  function bnpMultiplyUpperTo(a,n,r) {
960
- --n;
961
- var i = r.t = this.t+a.t-n;
962
- r.s = 0; // assumes a,this >= 0
963
- while(--i >= 0) r.data[i] = 0;
964
- for(i = Math.max(n-this.t,0); i < a.t; ++i)
965
- r.data[this.t+i-n] = this.am(n-i,a.data[i],r,0,0,this.t+i-n);
966
- r.clamp();
967
- r.drShiftTo(1,r);
975
+ --n;
976
+ var i = r.t = this.t+a.t-n;
977
+ r.s = 0; // assumes a,this >= 0
978
+ while(--i >= 0) r.data[i] = 0;
979
+ for(i = Math.max(n-this.t,0); i < a.t; ++i)
980
+ r.data[this.t+i-n] = this.am(n-i,a.data[i],r,0,0,this.t+i-n);
981
+ r.clamp();
982
+ r.drShiftTo(1,r);
968
983
  }
969
984
 
970
- //Barrett modular reduction
985
+ // Barrett modular reduction
971
986
  function Barrett(m) {
972
- // setup Barrett
973
- this.r2 = nbi();
974
- this.q3 = nbi();
975
- BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
976
- this.mu = this.r2.divide(m);
977
- this.m = m;
987
+ // setup Barrett
988
+ this.r2 = nbi();
989
+ this.q3 = nbi();
990
+ BigInteger.ONE.dlShiftTo(2*m.t,this.r2);
991
+ this.mu = this.r2.divide(m);
992
+ this.m = m;
978
993
  }
979
994
 
980
995
  function barrettConvert(x) {
981
- if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
982
- else if(x.compareTo(this.m) < 0) return x;
983
- else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
996
+ if(x.s < 0 || x.t > 2*this.m.t) return x.mod(this.m);
997
+ else if(x.compareTo(this.m) < 0) return x;
998
+ else { var r = nbi(); x.copyTo(r); this.reduce(r); return r; }
984
999
  }
985
1000
 
986
1001
  function barrettRevert(x) { return x; }
987
1002
 
988
- //x = x mod m (HAC 14.42)
1003
+ // x = x mod m (HAC 14.42)
989
1004
  function barrettReduce(x) {
990
- x.drShiftTo(this.m.t-1,this.r2);
991
- if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
992
- this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
993
- this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
994
- while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
995
- x.subTo(this.r2,x);
996
- while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
1005
+ x.drShiftTo(this.m.t-1,this.r2);
1006
+ if(x.t > this.m.t+1) { x.t = this.m.t+1; x.clamp(); }
1007
+ this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);
1008
+ this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);
1009
+ while(x.compareTo(this.r2) < 0) x.dAddOffset(1,this.m.t+1);
1010
+ x.subTo(this.r2,x);
1011
+ while(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
997
1012
  }
998
1013
 
999
- //r = x^2 mod m; x != r
1014
+ // r = x^2 mod m; x != r
1000
1015
  function barrettSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
1001
1016
 
1002
- //r = x*y mod m; x,y != r
1017
+ // r = x*y mod m; x,y != r
1003
1018
  function barrettMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
1004
1019
 
1005
1020
  Barrett.prototype.convert = barrettConvert;
@@ -1008,187 +1023,199 @@ Barrett.prototype.reduce = barrettReduce;
1008
1023
  Barrett.prototype.mulTo = barrettMulTo;
1009
1024
  Barrett.prototype.sqrTo = barrettSqrTo;
1010
1025
 
1011
- //(public) this^e % m (HAC 14.85)
1026
+ // (public) this^e % m (HAC 14.85)
1012
1027
  function bnModPow(e,m) {
1013
- var i = e.bitLength(), k, r = nbv(1), z;
1014
- if(i <= 0) return r;
1015
- else if(i < 18) k = 1;
1016
- else if(i < 48) k = 3;
1017
- else if(i < 144) k = 4;
1018
- else if(i < 768) k = 5;
1019
- else k = 6;
1020
- if(i < 8)
1021
- z = new Classic(m);
1022
- else if(m.isEven())
1023
- z = new Barrett(m);
1024
- else
1025
- z = new Montgomery(m);
1026
-
1027
- // precomputation
1028
- var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
1029
- g[1] = z.convert(this);
1030
- if(k > 1) {
1031
- var g2 = nbi();
1032
- z.sqrTo(g[1],g2);
1033
- while(n <= km) {
1034
- g[n] = nbi();
1035
- z.mulTo(g2,g[n-2],g[n]);
1036
- n += 2;
1037
- }
1038
- }
1039
-
1040
- var j = e.t-1, w, is1 = true, r2 = nbi(), t;
1041
- i = nbits(e.data[j])-1;
1042
- while(j >= 0) {
1043
- if(i >= k1) w = (e.data[j]>>(i-k1))&km;
1044
- else {
1045
- w = (e.data[j]&((1<<(i+1))-1))<<(k1-i);
1046
- if(j > 0) w |= e.data[j-1]>>(this.DB+i-k1);
1047
- }
1048
-
1049
- n = k;
1050
- while((w&1) == 0) { w >>= 1; --n; }
1051
- if((i -= n) < 0) { i += this.DB; --j; }
1052
- if(is1) { // ret == 1, don't bother squaring or multiplying it
1053
- g[w].copyTo(r);
1054
- is1 = false;
1055
- } else {
1056
- while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
1057
- if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
1058
- z.mulTo(r2,g[w],r);
1059
- }
1060
-
1061
- while(j >= 0 && (e.data[j]&(1<<i)) == 0) {
1062
- z.sqrTo(r,r2); t = r; r = r2; r2 = t;
1063
- if(--i < 0) { i = this.DB-1; --j; }
1064
- }
1065
- }
1066
- return z.revert(r);
1067
- }
1068
-
1069
- //(public) gcd(this,a) (HAC 14.54)
1028
+ var i = e.bitLength(), k, r = nbv(1), z;
1029
+ if(i <= 0) return r;
1030
+ else if(i < 18) k = 1;
1031
+ else if(i < 48) k = 3;
1032
+ else if(i < 144) k = 4;
1033
+ else if(i < 768) k = 5;
1034
+ else k = 6;
1035
+ if(i < 8)
1036
+ z = new Classic(m);
1037
+ else if(m.isEven())
1038
+ z = new Barrett(m);
1039
+ else
1040
+ z = new Montgomery(m);
1041
+
1042
+ // precomputation
1043
+ var g = new Array(), n = 3, k1 = k-1, km = (1<<k)-1;
1044
+ g[1] = z.convert(this);
1045
+ if(k > 1) {
1046
+ var g2 = nbi();
1047
+ z.sqrTo(g[1],g2);
1048
+ while(n <= km) {
1049
+ g[n] = nbi();
1050
+ z.mulTo(g2,g[n-2],g[n]);
1051
+ n += 2;
1052
+ }
1053
+ }
1054
+
1055
+ var j = e.t-1, w, is1 = true, r2 = nbi(), t;
1056
+ i = nbits(e.data[j])-1;
1057
+ while(j >= 0) {
1058
+ if(i >= k1) w = (e.data[j]>>(i-k1))&km;
1059
+ else {
1060
+ w = (e.data[j]&((1<<(i+1))-1))<<(k1-i);
1061
+ if(j > 0) w |= e.data[j-1]>>(this.DB+i-k1);
1062
+ }
1063
+
1064
+ n = k;
1065
+ while((w&1) == 0) { w >>= 1; --n; }
1066
+ if((i -= n) < 0) { i += this.DB; --j; }
1067
+ if(is1) { // ret == 1, don't bother squaring or multiplying it
1068
+ g[w].copyTo(r);
1069
+ is1 = false;
1070
+ }
1071
+ else {
1072
+ while(n > 1) { z.sqrTo(r,r2); z.sqrTo(r2,r); n -= 2; }
1073
+ if(n > 0) z.sqrTo(r,r2); else { t = r; r = r2; r2 = t; }
1074
+ z.mulTo(r2,g[w],r);
1075
+ }
1076
+
1077
+ while(j >= 0 && (e.data[j]&(1<<i)) == 0) {
1078
+ z.sqrTo(r,r2); t = r; r = r2; r2 = t;
1079
+ if(--i < 0) { i = this.DB-1; --j; }
1080
+ }
1081
+ }
1082
+ return z.revert(r);
1083
+ }
1084
+
1085
+ // (public) gcd(this,a) (HAC 14.54)
1070
1086
  function bnGCD(a) {
1071
- var x = (this.s<0)?this.negate():this.clone();
1072
- var y = (a.s<0)?a.negate():a.clone();
1073
- if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
1074
- var i = x.getLowestSetBit(), g = y.getLowestSetBit();
1075
- if(g < 0) return x;
1076
- if(i < g) g = i;
1077
- if(g > 0) {
1078
- x.rShiftTo(g,x);
1079
- y.rShiftTo(g,y);
1080
- }
1081
- while(x.signum() > 0) {
1082
- if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
1083
- if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
1084
- if(x.compareTo(y) >= 0) {
1085
- x.subTo(y,x);
1086
- x.rShiftTo(1,x);
1087
- } else {
1088
- y.subTo(x,y);
1089
- y.rShiftTo(1,y);
1090
- }
1091
- }
1092
- if(g > 0) y.lShiftTo(g,y);
1093
- return y;
1094
- }
1095
-
1096
- //(protected) this % n, n < 2^26
1087
+ var x = (this.s<0)?this.negate():this.clone();
1088
+ var y = (a.s<0)?a.negate():a.clone();
1089
+ if(x.compareTo(y) < 0) { var t = x; x = y; y = t; }
1090
+ var i = x.getLowestSetBit(), g = y.getLowestSetBit();
1091
+ if(g < 0) return x;
1092
+ if(i < g) g = i;
1093
+ if(g > 0) {
1094
+ x.rShiftTo(g,x);
1095
+ y.rShiftTo(g,y);
1096
+ }
1097
+ while(x.signum() > 0) {
1098
+ if((i = x.getLowestSetBit()) > 0) x.rShiftTo(i,x);
1099
+ if((i = y.getLowestSetBit()) > 0) y.rShiftTo(i,y);
1100
+ if(x.compareTo(y) >= 0) {
1101
+ x.subTo(y,x);
1102
+ x.rShiftTo(1,x);
1103
+ }
1104
+ else {
1105
+ y.subTo(x,y);
1106
+ y.rShiftTo(1,y);
1107
+ }
1108
+ }
1109
+ if(g > 0) y.lShiftTo(g,y);
1110
+ return y;
1111
+ }
1112
+
1113
+ // (protected) this % n, n < 2^26
1097
1114
  function bnpModInt(n) {
1098
- if(n <= 0) return 0;
1099
- var d = this.DV%n, r = (this.s<0)?n-1:0;
1100
- if(this.t > 0)
1101
- if(d == 0) r = this.data[0]%n;
1102
- else for(var i = this.t-1; i >= 0; --i) r = (d*r+this.data[i])%n;
1103
- return r;
1115
+ if(n <= 0) return 0;
1116
+ var d = this.DV%n, r = (this.s<0)?n-1:0;
1117
+ if(this.t > 0)
1118
+ if(d == 0) r = this.data[0]%n;
1119
+ else for(var i = this.t-1; i >= 0; --i) r = (d*r+this.data[i])%n;
1120
+ return r;
1104
1121
  }
1105
1122
 
1106
- //(public) 1/this % m (HAC 14.61)
1123
+ // (public) 1/this % m (HAC 14.61)
1107
1124
  function bnModInverse(m) {
1108
- var ac = m.isEven();
1109
- if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
1110
- var u = m.clone(), v = this.clone();
1111
- var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
1112
- while(u.signum() != 0) {
1113
- while(u.isEven()) {
1114
- u.rShiftTo(1,u);
1115
- if(ac) {
1116
- if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
1117
- a.rShiftTo(1,a);
1118
- } else if(!b.isEven()) b.subTo(m,b);
1119
- b.rShiftTo(1,b);
1120
- }
1121
- while(v.isEven()) {
1122
- v.rShiftTo(1,v);
1123
- if(ac) {
1124
- if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
1125
- c.rShiftTo(1,c);
1126
- } else if(!d.isEven()) d.subTo(m,d);
1127
- d.rShiftTo(1,d);
1128
- }
1129
- if(u.compareTo(v) >= 0) {
1130
- u.subTo(v,u);
1131
- if(ac) a.subTo(c,a);
1132
- b.subTo(d,b);
1133
- } else {
1134
- v.subTo(u,v);
1135
- if(ac) c.subTo(a,c);
1136
- d.subTo(b,d);
1137
- }
1138
- }
1139
- if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
1140
- if(d.compareTo(m) >= 0) return d.subtract(m);
1141
- if(d.signum() < 0) d.addTo(m,d); else return d;
1142
- if(d.signum() < 0) return d.add(m); else return d;
1143
- }
1144
-
1145
- 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];
1125
+ // FORGE: jsbn fix
1126
+ // avoid infinite loop
1127
+ if(this.signum() == 0) {
1128
+ // returning zero to align with similar behavior when no multiplicative
1129
+ // inverse module m is found.
1130
+ return BigInteger.ZERO;
1131
+ }
1132
+ var ac = m.isEven();
1133
+ if((this.isEven() && ac) || m.signum() == 0) return BigInteger.ZERO;
1134
+ var u = m.clone(), v = this.clone();
1135
+ var a = nbv(1), b = nbv(0), c = nbv(0), d = nbv(1);
1136
+ while(u.signum() != 0) {
1137
+ while(u.isEven()) {
1138
+ u.rShiftTo(1,u);
1139
+ if(ac) {
1140
+ if(!a.isEven() || !b.isEven()) { a.addTo(this,a); b.subTo(m,b); }
1141
+ a.rShiftTo(1,a);
1142
+ }
1143
+ else if(!b.isEven()) b.subTo(m,b);
1144
+ b.rShiftTo(1,b);
1145
+ }
1146
+ while(v.isEven()) {
1147
+ v.rShiftTo(1,v);
1148
+ if(ac) {
1149
+ if(!c.isEven() || !d.isEven()) { c.addTo(this,c); d.subTo(m,d); }
1150
+ c.rShiftTo(1,c);
1151
+ }
1152
+ else if(!d.isEven()) d.subTo(m,d);
1153
+ d.rShiftTo(1,d);
1154
+ }
1155
+ if(u.compareTo(v) >= 0) {
1156
+ u.subTo(v,u);
1157
+ if(ac) a.subTo(c,a);
1158
+ b.subTo(d,b);
1159
+ }
1160
+ else {
1161
+ v.subTo(u,v);
1162
+ if(ac) c.subTo(a,c);
1163
+ d.subTo(b,d);
1164
+ }
1165
+ }
1166
+ if(v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
1167
+ if(d.compareTo(m) >= 0) return d.subtract(m);
1168
+ if(d.signum() < 0) d.addTo(m,d); else return d;
1169
+ if(d.signum() < 0) return d.add(m); else return d;
1170
+ }
1171
+
1172
+ 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,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997];
1146
1173
  var lplim = (1<<26)/lowprimes[lowprimes.length-1];
1147
1174
 
1148
- //(public) test primality with certainty >= 1-.5^t
1175
+ // (public) test primality with certainty >= 1-.5^t
1149
1176
  function bnIsProbablePrime(t) {
1150
- var i, x = this.abs();
1151
- if(x.t == 1 && x.data[0] <= lowprimes[lowprimes.length-1]) {
1152
- for(i = 0; i < lowprimes.length; ++i)
1153
- if(x.data[0] == lowprimes[i]) return true;
1154
- return false;
1155
- }
1156
- if(x.isEven()) return false;
1157
- i = 1;
1158
- while(i < lowprimes.length) {
1159
- var m = lowprimes[i], j = i+1;
1160
- while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
1161
- m = x.modInt(m);
1162
- while(i < j) if(m%lowprimes[i++] == 0) return false;
1163
- }
1164
- return x.millerRabin(t);
1177
+ var i, x = this.abs();
1178
+ if(x.t == 1 && x.data[0] <= lowprimes[lowprimes.length-1]) {
1179
+ for(i = 0; i < lowprimes.length; ++i)
1180
+ if(x.data[0] == lowprimes[i]) return true;
1181
+ return false;
1182
+ }
1183
+ if(x.isEven()) return false;
1184
+ i = 1;
1185
+ while(i < lowprimes.length) {
1186
+ var m = lowprimes[i], j = i+1;
1187
+ while(j < lowprimes.length && m < lplim) m *= lowprimes[j++];
1188
+ m = x.modInt(m);
1189
+ while(i < j) if(m%lowprimes[i++] == 0) return false;
1190
+ }
1191
+ return x.millerRabin(t);
1165
1192
  }
1166
1193
 
1167
- //(protected) true if probably prime (HAC 4.24, Miller-Rabin)
1194
+ // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
1168
1195
  function bnpMillerRabin(t) {
1169
- var n1 = this.subtract(BigInteger.ONE);
1170
- var k = n1.getLowestSetBit();
1171
- if(k <= 0) return false;
1172
- var r = n1.shiftRight(k);
1173
- var prng = bnGetPrng();
1174
- var a;
1175
- for(var i = 0; i < t; ++i) {
1176
- // select witness 'a' at random from between 1 and n1
1177
- do {
1178
- a = new BigInteger(this.bitLength(), prng);
1179
- }
1180
- while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
1181
- var y = a.modPow(r,this);
1182
- if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
1183
- var j = 1;
1184
- while(j++ < k && y.compareTo(n1) != 0) {
1185
- y = y.modPowInt(2,this);
1186
- if(y.compareTo(BigInteger.ONE) == 0) return false;
1187
- }
1188
- if(y.compareTo(n1) != 0) return false;
1189
- }
1190
- }
1191
- return true;
1196
+ var n1 = this.subtract(BigInteger.ONE);
1197
+ var k = n1.getLowestSetBit();
1198
+ if(k <= 0) return false;
1199
+ var r = n1.shiftRight(k);
1200
+ var prng = bnGetPrng();
1201
+ var a;
1202
+ for(var i = 0; i < t; ++i) {
1203
+ // select witness 'a' at random from between 1 and n1
1204
+ do {
1205
+ a = new BigInteger(this.bitLength(), prng);
1206
+ }
1207
+ while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
1208
+ var y = a.modPow(r,this);
1209
+ if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
1210
+ var j = 1;
1211
+ while(j++ < k && y.compareTo(n1) != 0) {
1212
+ y = y.modPowInt(2,this);
1213
+ if(y.compareTo(BigInteger.ONE) == 0) return false;
1214
+ }
1215
+ if(y.compareTo(n1) != 0) return false;
1216
+ }
1217
+ }
1218
+ return true;
1192
1219
  }
1193
1220
 
1194
1221
  // get pseudo random number generator
@@ -1204,7 +1231,7 @@ function bnGetPrng() {
1204
1231
  };
1205
1232
  }
1206
1233
 
1207
- //protected
1234
+ // protected
1208
1235
  BigInteger.prototype.chunkSize = bnpChunkSize;
1209
1236
  BigInteger.prototype.toRadix = bnpToRadix;
1210
1237
  BigInteger.prototype.fromRadix = bnpFromRadix;
@@ -1219,7 +1246,7 @@ BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
1219
1246
  BigInteger.prototype.modInt = bnpModInt;
1220
1247
  BigInteger.prototype.millerRabin = bnpMillerRabin;
1221
1248
 
1222
- //public
1249
+ // public
1223
1250
  BigInteger.prototype.clone = bnClone;
1224
1251
  BigInteger.prototype.intValue = bnIntValue;
1225
1252
  BigInteger.prototype.byteValue = bnByteValue;
@@ -1254,11 +1281,14 @@ BigInteger.prototype.pow = bnPow;
1254
1281
  BigInteger.prototype.gcd = bnGCD;
1255
1282
  BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
1256
1283
 
1257
- //BigInteger interfaces not implemented in jsbn:
1284
+ // JSBN-specific extension
1285
+ BigInteger.prototype.square = bnSquare;
1286
+
1287
+ // BigInteger interfaces not implemented in jsbn:
1258
1288
 
1259
- //BigInteger(int signum, byte[] magnitude)
1260
- //double doubleValue()
1261
- //float floatValue()
1262
- //int hashCode()
1263
- //long longValue()
1264
- //static BigInteger valueOf(long val)
1289
+ // BigInteger(int signum, byte[] magnitude)
1290
+ // double doubleValue()
1291
+ // float floatValue()
1292
+ // int hashCode()
1293
+ // long longValue()
1294
+ // static BigInteger valueOf(long val)