Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated jsbn to use proper array notation, as suggested by https://bu…

…gzilla.mozilla.org/show_bug.cgi?id=594655 and tweaked framework for support
  • Loading branch information...
commit fc42292f8888343b9f1d12014eb169aaaba2c13f 1 parent 523dd4e
@benadida authored
Showing with 304 additions and 311 deletions.
  1. +181 −181 bigint.js
  2. +71 −70 jsbn.js
  3. +52 −60 jsbn2.js
View
362 bigint.js
@@ -13,202 +13,202 @@
// A wrapper for java.math.BigInteger with some appropriate extra functions for JSON and
// generally being a nice JavaScript object.
-// check if java is enabled, if not launch the dummy version
-// for now, prevent MSIE from using the Java bigint, because it's having issues
-// TOFIX ASAP 2011-03-20
-if (navigator.appName == "Microsoft Internet Explorer" || !navigator.javaEnabled()) {
- /* var script = document.createElement('script');
- script.type = 'text/javascript';
- script.src = 'js/bigint.dummy.js';
- document.getElementsByTagName('head')[0].appendChild(script);
- */
- USE_SJCL = true;
-/* BigInt = {}
- BigInt.setup = function(callback, fail_callback) {
- if (fail_callback) {
- fail_callback();
- } else {
- alert('no java, and no way to recover');
- }
- } */
-} else {
- USE_SJCL = false;
-}
+// let's try always using SJCL
+var USE_SJCL = true;
+
+// let's make this much cleaner
+if (USE_SJCL) {
+ // why not?
+ var BigInt = BigInteger;
+ // ZERO AND ONE are already taken care of
+ BigInt.TWO = new BigInt("2",10);
-BigInt = Class.extend({
- init: function(value, radix) {
- if (value == null) {
- throw "null value!";
- }
-
- if (USE_SJCL) {
- this._java_bigint = new BigInteger(value, radix);
- } else if (BigInt.use_applet) {
- this._java_bigint = BigInt.APPLET.newBigInteger(value, radix);
- } else {
- try {
- this._java_bigint = new java.math.BigInteger(value, radix);
- } catch (e) {
- // alert("oy " + e.toString() + " value=" + value + " , radix=" + radix);
- throw TypeError
- }
+ BigInt.setup = function(callback, fail_callback) {
+ // nothing to do but go
+ callback();
}
- },
-
- toString: function() {
- return this._java_bigint.toString() + "";
- },
-
- toJSONObject: function() {
- return this.toString();
- },
-
- add: function(other) {
- return BigInt._from_java_object(this._java_bigint.add(other._java_bigint));
- },
-
- bitLength: function() {
- return this._java_bigint.bitLength();
- },
-
- mod: function(modulus) {
- return BigInt._from_java_object(this._java_bigint.mod(modulus._java_bigint));
- },
-
- equals: function(other) {
- return this._java_bigint.equals(other._java_bigint);
- },
-
- modPow: function(exp, modulus) {
- return BigInt._from_java_object(this._java_bigint.modPow(exp._java_bigint, modulus._java_bigint));
- },
-
- negate: function() {
- return BigInt._from_java_object(this._java_bigint.negate());
- },
-
- multiply: function(other) {
- return BigInt._from_java_object(this._java_bigint.multiply(other._java_bigint));
- },
-
- modInverse: function(modulus) {
- return BigInt._from_java_object(this._java_bigint.modInverse(modulus._java_bigint));
- }
-
-});
-
-BigInt.ready_p = false;
-
-//
-// Some Class Methods
-//
-BigInt._from_java_object = function(jo) {
- // bogus object
- var obj = new BigInt("0",10);
- obj._java_bigint = jo;
- return obj;
-};
-
-BigInt.fromJSONObject = function(s) {
- return new BigInt(s, 10);
-};
-
-BigInt.fromInt = function(i) {
- return BigInt.fromJSONObject("" + i);
-};
-
-//
-// do the applet check
-//
-function check_applet() {
- /* Is this Netscape 4.xx? */
- var is_ns4 = (navigator.appName == "Netscape" && navigator.appVersion < "5");
-
- /* Do we need the toString() workaround (requires applet)? */
- var str_workaround = (navigator.appName == "Opera");
- // stuff this in BigInt
- BigInt.is_ie = (navigator.appName == "Microsoft Internet Explorer");
+ BigInt.prototype.toJSONObject = function() {
+ return this.toString();
+ };
- /* Decide whether we need the helper applet or not */
- var use_applet = BigInt.is_ie || (!is_ns4 && navigator.platform.substr(0, 5) == "Linux") || str_workaround || typeof(java) == 'undefined';
-
- // add the applet
- if (use_applet) {
- var applet_base = JSCRYPTO_HOME;
+} else {
+ BigInt = Class.extend({
+ init: function(value, radix) {
+ if (value == null) {
+ throw "null value!";
+ }
+
+ if (USE_SJCL) {
+ this._java_bigint = new BigInteger(value, radix);
+ } else if (BigInt.use_applet) {
+ this._java_bigint = BigInt.APPLET.newBigInteger(value, radix);
+ } else {
+ try {
+ this._java_bigint = new java.math.BigInteger(value, radix);
+ } catch (e) {
+ // alert("oy " + e.toString() + " value=" + value + " , radix=" + radix);
+ throw TypeError
+ }
+ }
+ },
+
+ toString: function() {
+ return this._java_bigint.toString() + "";
+ },
+
+ toJSONObject: function() {
+ return this.toString();
+ },
+
+ add: function(other) {
+ return BigInt._from_java_object(this._java_bigint.add(other._java_bigint));
+ },
+
+ bitLength: function() {
+ return this._java_bigint.bitLength();
+ },
+
+ mod: function(modulus) {
+ return BigInt._from_java_object(this._java_bigint.mod(modulus._java_bigint));
+ },
+
+ equals: function(other) {
+ return this._java_bigint.equals(other._java_bigint);
+ },
+
+ modPow: function(exp, modulus) {
+ return BigInt._from_java_object(this._java_bigint.modPow(exp._java_bigint, modulus._java_bigint));
+ },
+
+ negate: function() {
+ return BigInt._from_java_object(this._java_bigint.negate());
+ },
+
+ multiply: function(other) {
+ return BigInt._from_java_object(this._java_bigint.multiply(other._java_bigint));
+ },
+
+ modInverse: function(modulus) {
+ return BigInt._from_java_object(this._java_bigint.modInverse(modulus._java_bigint));
+ }
+
+ });
- var applet_html = '<applet codebase="' + applet_base + '" mayscript name="bigint" code="bigint.class" width=1 height=1 id="bigint_applet"></applet>';
- // var applet_html = '<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" name="bigint" width="1" height="1" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_5_0-windows-i586.cab#Version=1,5,0,0"> <param name="code" value="bigint.class"> <param name="codebase" value="' + applet_base + '"> <param name="archive" value="myapplet.jar"> <param name="type" value="application/x-java-applet;version=1.5.0"> <param name="scriptable" value="true"> <param name="mayscript" value="false"> <comment> <embed code="bigint.class" name="bigint" java_codebase="' + applet_base + '" width="1" height="1" scriptable="true" mayscript="false" type="application/x-java-applet;version=1.5.0" pluginspage="http://java.sun.com/j2se/1.5.0/download.html"> <noembed>No Java Support.</noembed> </embed> </comment> </object>';
- $("#applet_div").html(applet_html);
- }
-
- return use_applet;
-};
-
-// Set up the pointer to the applet if necessary, and some
-// basic Big Ints that everyone needs (0, 1, 2, and 42)
-BigInt._setup = function() {
- if (BigInt.use_applet) {
- BigInt.APPLET = document.applets["bigint"];
- }
-
- try {
- BigInt.ZERO = new BigInt("0",10);
- BigInt.ONE = new BigInt("1",10);
- BigInt.TWO = new BigInt("2",10);
- BigInt.FORTY_TWO = new BigInt("42",10);
-
- BigInt.ready_p = true;
- } catch (e) {
- // not ready
- // count how many times we've tried
- if (this.num_invocations == null)
- this.num_invocations = 0;
-
- this.num_invocations += 1;
-
- if (this.num_invocations > 5) {
- // try SJCL
- if (!USE_SJCL) {
- USE_SJCL = true;
- this.num_invocations = 1;
- BigInt.use_applet = false;
- } else {
-
- if (BigInt.setup_interval)
- window.clearInterval(BigInt.setup_interval);
-
- if (BigInt.setup_fail) {
- BigInt.setup_fail();
- } else {
- alert('bigint failed!');
+ BigInt.ready_p = false;
+
+ //
+ // Some Class Methods
+ //
+ BigInt._from_java_object = function(jo) {
+ // bogus object
+ var obj = new BigInt("0",10);
+ obj._java_bigint = jo;
+ return obj;
+ };
+
+ //
+ // do the applet check
+ //
+ function check_applet() {
+ /* Is this Netscape 4.xx? */
+ var is_ns4 = (navigator.appName == "Netscape" && navigator.appVersion < "5");
+
+ /* Do we need the toString() workaround (requires applet)? */
+ var str_workaround = (navigator.appName == "Opera");
+
+ // stuff this in BigInt
+ BigInt.is_ie = (navigator.appName == "Microsoft Internet Explorer");
+
+ /* Decide whether we need the helper applet or not */
+ var use_applet = BigInt.is_ie || (!is_ns4 && navigator.platform.substr(0, 5) == "Linux") || str_workaround || typeof(java) == 'undefined';
+
+ // add the applet
+ if (use_applet) {
+ var applet_base = JSCRYPTO_HOME;
+
+ var applet_html = '<applet codebase="' + applet_base + '" mayscript name="bigint" code="bigint.class" width=1 height=1 id="bigint_applet"></applet>';
+ // var applet_html = '<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" name="bigint" width="1" height="1" codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_5_0-windows-i586.cab#Version=1,5,0,0"> <param name="code" value="bigint.class"> <param name="codebase" value="' + applet_base + '"> <param name="archive" value="myapplet.jar"> <param name="type" value="application/x-java-applet;version=1.5.0"> <param name="scriptable" value="true"> <param name="mayscript" value="false"> <comment> <embed code="bigint.class" name="bigint" java_codebase="' + applet_base + '" width="1" height="1" scriptable="true" mayscript="false" type="application/x-java-applet;version=1.5.0" pluginspage="http://java.sun.com/j2se/1.5.0/download.html"> <noembed>No Java Support.</noembed> </embed> </comment> </object>';
+ $("#applet_div").html(applet_html);
+ }
+
+ return use_applet;
+ };
+
+ // Set up the pointer to the applet if necessary, and some
+ // basic Big Ints that everyone needs (0, 1, 2, and 42)
+ BigInt._setup = function() {
+ if (BigInt.use_applet) {
+ BigInt.APPLET = document.applets["bigint"];
+ }
+
+ try {
+ BigInt.ZERO = new BigInt("0",10);
+ BigInt.ONE = new BigInt("1",10);
+ BigInt.TWO = new BigInt("2",10);
+ BigInt.FORTY_TWO = new BigInt("42",10);
+
+ BigInt.ready_p = true;
+ } catch (e) {
+ // not ready
+ // count how many times we've tried
+ if (this.num_invocations == null)
+ this.num_invocations = 0;
+
+ this.num_invocations += 1;
+
+ if (this.num_invocations > 5) {
+ // try SJCL
+ if (!USE_SJCL) {
+ USE_SJCL = true;
+ this.num_invocations = 1;
+ BigInt.use_applet = false;
+ } else {
+
+ if (BigInt.setup_interval)
+ window.clearInterval(BigInt.setup_interval);
+
+ if (BigInt.setup_fail) {
+ BigInt.setup_fail();
+ } else {
+ alert('bigint failed!');
+ }
+ }
}
+ return;
}
+
+ if (BigInt.setup_interval)
+ window.clearInterval(BigInt.setup_interval);
+
+ if (BigInt.setup_callback)
+ BigInt.setup_callback();
+ };
+
+ BigInt.setup = function(callback, fail_callback) {
+ if (callback)
+ BigInt.setup_callback = callback;
+
+ if (fail_callback)
+ BigInt.setup_fail = fail_callback;
+
+ BigInt.setup_interval = window.setInterval("BigInt._setup()", 1000);
}
- return;
- }
-
- if (BigInt.setup_interval)
- window.clearInterval(BigInt.setup_interval);
+}
- if (BigInt.setup_callback)
- BigInt.setup_callback();
+BigInt.fromJSONObject = function(s) {
+ return new BigInt(s, 10);
};
-BigInt.setup = function(callback, fail_callback) {
- if (callback)
- BigInt.setup_callback = callback;
-
- if (fail_callback)
- BigInt.setup_fail = fail_callback;
-
- BigInt.setup_interval = window.setInterval("BigInt._setup()", 1000);
-}
+BigInt.fromInt = function(i) {
+ return BigInt.fromJSONObject("" + i);
+};
// .onload instead of .ready, as I don't think the applet is ready until onload.
// FIXME: something wrong here in the first load
$(document).ready(function() {
- BigInt.use_applet = check_applet();
+ //BigInt.use_applet = check_applet();
+ BigInt.use_applet = false;
});
//}
View
141 jsbn.js
@@ -13,6 +13,7 @@ var j_lm = ((canary&0xffffff)==0xefcafe);
// (public) Constructor
function BigInteger(a,b,c) {
+ this.arr = new Array();
if(a != null)
if("number" == typeof a) this.fromNumber(a,b,c);
else if(b == null && "string" != typeof a) this.fromString(a,256);
@@ -32,9 +33,9 @@ function nbi() { return new BigInteger(null); }
// max internal value = 2*dvalue^2-2*dvalue (< 2^53)
function am1(i,x,w,j,c,n) {
while(--n >= 0) {
- var v = x*this[i++]+w[j]+c;
+ var v = x*this.arr[i++]+w.arr[j]+c;
c = Math.floor(v/0x4000000);
- w[j++] = v&0x3ffffff;
+ w.arr[j++] = v&0x3ffffff;
}
return c;
}
@@ -44,12 +45,12 @@ function am1(i,x,w,j,c,n) {
function am2(i,x,w,j,c,n) {
var xl = x&0x7fff, xh = x>>15;
while(--n >= 0) {
- var l = this[i]&0x7fff;
- var h = this[i++]>>15;
+ var l = this.arr[i]&0x7fff;
+ var h = this.arr[i++]>>15;
var m = xh*l+h*xl;
- l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
+ l = xl*l+((m&0x7fff)<<15)+w.arr[j]+(c&0x3fffffff);
c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
- w[j++] = l&0x3fffffff;
+ w.arr[j++] = l&0x3fffffff;
}
return c;
}
@@ -58,12 +59,12 @@ function am2(i,x,w,j,c,n) {
function am3(i,x,w,j,c,n) {
var xl = x&0x3fff, xh = x>>14;
while(--n >= 0) {
- var l = this[i]&0x3fff;
- var h = this[i++]>>14;
+ var l = this.arr[i]&0x3fff;
+ var h = this.arr[i++]>>14;
var m = xh*l+h*xl;
- l = xl*l+((m&0x3fff)<<14)+w[j]+c;
+ l = xl*l+((m&0x3fff)<<14)+w.arr[j]+c;
c = (l>>28)+(m>>14)+xh*h;
- w[j++] = l&0xfffffff;
+ w.arr[j++] = l&0xfffffff;
}
return c;
}
@@ -108,7 +109,7 @@ function intAt(s,i) {
// (protected) copy this to r
function bnpCopyTo(r) {
- for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
+ for(var i = this.t-1; i >= 0; --i) r.arr[i] = this.arr[i];
r.t = this.t;
r.s = this.s;
}
@@ -117,8 +118,8 @@ function bnpCopyTo(r) {
function bnpFromInt(x) {
this.t = 1;
this.s = (x<0)?-1:0;
- if(x > 0) this[0] = x;
- else if(x < -1) this[0] = x+DV;
+ if(x > 0) this.arr[0] = x;
+ else if(x < -1) this.arr[0] = x+DV;
else this.t = 0;
}
@@ -146,19 +147,19 @@ function bnpFromString(s,b) {
}
mi = false;
if(sh == 0)
- this[this.t++] = x;
+ this.arr[this.t++] = x;
else if(sh+k > this.DB) {
- this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
- this[this.t++] = (x>>(this.DB-sh));
+ this.arr[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
+ this.arr[this.t++] = (x>>(this.DB-sh));
}
else
- this[this.t-1] |= x<<sh;
+ this.arr[this.t-1] |= x<<sh;
sh += k;
if(sh >= this.DB) sh -= this.DB;
}
if(k == 8 && (s[0]&0x80) != 0) {
this.s = -1;
- if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
+ if(sh > 0) this.arr[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
}
this.clamp();
if(mi) BigInteger.ZERO.subTo(this,this);
@@ -167,7 +168,7 @@ function bnpFromString(s,b) {
// (protected) clamp off excess high words
function bnpClamp() {
var c = this.s&this.DM;
- while(this.t > 0 && this[this.t-1] == c) --this.t;
+ while(this.t > 0 && this.arr[this.t-1] == c) --this.t;
}
// (public) return string representation in given radix
@@ -183,14 +184,14 @@ function bnToString(b) {
var km = (1<<k)-1, d, m = false, r = "", i = this.t;
var p = this.DB-(i*this.DB)%k;
if(i-- > 0) {
- if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
+ if(p < this.DB && (d = this.arr[i]>>p) > 0) { m = true; r = int2char(d); }
while(i >= 0) {
if(p < k) {
- d = (this[i]&((1<<p)-1))<<(k-p);
- d |= this[--i]>>(p+=this.DB-k);
+ d = (this.arr[i]&((1<<p)-1))<<(k-p);
+ d |= this.arr[--i]>>(p+=this.DB-k);
}
else {
- d = (this[i]>>(p-=k))&km;
+ d = (this.arr[i]>>(p-=k))&km;
if(p <= 0) { p += this.DB; --i; }
}
if(d > 0) m = true;
@@ -213,7 +214,7 @@ function bnCompareTo(a) {
var i = this.t;
r = i-a.t;
if(r != 0) return r;
- while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
+ while(--i >= 0) if((r=this.arr[i]-a.arr[i]) != 0) return r;
return 0;
}
@@ -231,21 +232,21 @@ function nbits(x) {
// (public) return the number of bits in "this"
function bnBitLength() {
if(this.t <= 0) return 0;
- return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
+ return this.DB*(this.t-1)+nbits(this.arr[this.t-1]^(this.s&this.DM));
}
// (protected) r = this << n*DB
function bnpDLShiftTo(n,r) {
var i;
- for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
- for(i = n-1; i >= 0; --i) r[i] = 0;
+ for(i = this.t-1; i >= 0; --i) r.arr[i+n] = this.arr[i];
+ for(i = n-1; i >= 0; --i) r.arr[i] = 0;
r.t = this.t+n;
r.s = this.s;
}
// (protected) r = this >> n*DB
function bnpDRShiftTo(n,r) {
- for(var i = n; i < this.t; ++i) r[i-n] = this[i];
+ for(var i = n; i < this.t; ++i) r.arr[i-n] = this.arr[i];
r.t = Math.max(this.t-n,0);
r.s = this.s;
}
@@ -257,11 +258,11 @@ function bnpLShiftTo(n,r) {
var bm = (1<<cbs)-1;
var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
for(i = this.t-1; i >= 0; --i) {
- r[i+ds+1] = (this[i]>>cbs)|c;
- c = (this[i]&bm)<<bs;
+ r.arr[i+ds+1] = (this.arr[i]>>cbs)|c;
+ c = (this.arr[i]&bm)<<bs;
}
- for(i = ds-1; i >= 0; --i) r[i] = 0;
- r[ds] = c;
+ for(i = ds-1; i >= 0; --i) r.arr[i] = 0;
+ r.arr[ds] = c;
r.t = this.t+ds+1;
r.s = this.s;
r.clamp();
@@ -275,12 +276,12 @@ function bnpRShiftTo(n,r) {
var bs = n%this.DB;
var cbs = this.DB-bs;
var bm = (1<<bs)-1;
- r[0] = this[ds]>>bs;
+ r.arr[0] = this.arr[ds]>>bs;
for(var i = ds+1; i < this.t; ++i) {
- r[i-ds-1] |= (this[i]&bm)<<cbs;
- r[i-ds] = this[i]>>bs;
+ r.arr[i-ds-1] |= (this.arr[i]&bm)<<cbs;
+ r.arr[i-ds] = this.arr[i]>>bs;
}
- if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
+ if(bs > 0) r.arr[this.t-ds-1] |= (this.s&bm)<<cbs;
r.t = this.t-ds;
r.clamp();
}
@@ -289,15 +290,15 @@ function bnpRShiftTo(n,r) {
function bnpSubTo(a,r) {
var i = 0, c = 0, m = Math.min(a.t,this.t);
while(i < m) {
- c += this[i]-a[i];
- r[i++] = c&this.DM;
+ c += this.arr[i]-a.arr[i];
+ r.arr[i++] = c&this.DM;
c >>= this.DB;
}
if(a.t < this.t) {
c -= a.s;
while(i < this.t) {
- c += this[i];
- r[i++] = c&this.DM;
+ c += this.arr[i];
+ r.arr[i++] = c&this.DM;
c >>= this.DB;
}
c += this.s;
@@ -305,15 +306,15 @@ function bnpSubTo(a,r) {
else {
c += this.s;
while(i < a.t) {
- c -= a[i];
- r[i++] = c&this.DM;
+ c -= a.arr[i];
+ r.arr[i++] = c&this.DM;
c >>= this.DB;
}
c -= a.s;
}
r.s = (c<0)?-1:0;
- if(c < -1) r[i++] = this.DV+c;
- else if(c > 0) r[i++] = c;
+ if(c < -1) r.arr[i++] = this.DV+c;
+ else if(c > 0) r.arr[i++] = c;
r.t = i;
r.clamp();
}
@@ -324,8 +325,8 @@ function bnpMultiplyTo(a,r) {
var x = this.abs(), y = a.abs();
var i = x.t;
r.t = i+y.t;
- while(--i >= 0) r[i] = 0;
- for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
+ while(--i >= 0) r.arr[i] = 0;
+ for(i = 0; i < y.t; ++i) r.arr[i+x.t] = x.am(0,y.arr[i],r,i,0,x.t);
r.s = 0;
r.clamp();
if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
@@ -335,15 +336,15 @@ function bnpMultiplyTo(a,r) {
function bnpSquareTo(r) {
var x = this.abs();
var i = r.t = 2*x.t;
- while(--i >= 0) r[i] = 0;
+ while(--i >= 0) r.arr[i] = 0;
for(i = 0; i < x.t-1; ++i) {
- var c = x.am(i,x[i],r,2*i,0,1);
- if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
- r[i+x.t] -= x.DV;
- r[i+x.t+1] = 1;
+ var c = x.am(i,x.arr[i],r,2*i,0,1);
+ if((r.arr[i+x.t]+=x.am(i+1,2*x.arr[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
+ r.arr[i+x.t] -= x.DV;
+ r.arr[i+x.t+1] = 1;
}
}
- if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
+ if(r.t > 0) r.arr[r.t-1] += x.am(i,x.arr[i],r,2*i,0,1);
r.s = 0;
r.clamp();
}
@@ -361,30 +362,30 @@ function bnpDivRemTo(m,q,r) {
}
if(r == null) r = nbi();
var y = nbi(), ts = this.s, ms = m.s;
- var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus
+ var nsh = this.DB-nbits(pm.arr[pm.t-1]); // normalize modulus
if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
else { pm.copyTo(y); pt.copyTo(r); }
var ys = y.t;
- var y0 = y[ys-1];
+ var y0 = y.arr[ys-1];
if(y0 == 0) return;
- var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
+ var yt = y0*(1<<this.F1)+((ys>1)?y.arr[ys-2]>>this.F2:0);
var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
var i = r.t, j = i-ys, t = (q==null)?nbi():q;
y.dlShiftTo(j,t);
if(r.compareTo(t) >= 0) {
- r[r.t++] = 1;
+ r.arr[r.t++] = 1;
r.subTo(t,r);
}
BigInteger.ONE.dlShiftTo(ys,t);
t.subTo(y,y); // "negative" y so we can replace sub with am later
- while(y.t < ys) y[y.t++] = 0;
+ while(y.t < ys) y.arr[y.t++] = 0;
while(--j >= 0) {
// Estimate quotient digit
- var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
- if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
+ var qd = (r.arr[--i]==y0)?this.DM:Math.floor(r.arr[i]*d1+(r.arr[i-1]+e)*d2);
+ if((r.arr[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
y.dlShiftTo(j,t);
r.subTo(t,r);
- while(r[i] < --qd) r.subTo(t,r);
+ while(r.arr[i] < --qd) r.subTo(t,r);
}
}
if(q != null) {
@@ -427,14 +428,14 @@ Classic.prototype.sqrTo = cSqrTo;
// xy == 1 (mod m)
// xy = 1+km
// xy(2-xy) = (1+km)(1-km)
-// x[y(2-xy)] = 1-k^2m^2
-// x[y(2-xy)] == 1 (mod m^2)
+// x.arr[y(2-xy)] = 1-k^2m^2
+// x.arr[y(2-xy)] == 1 (mod m^2)
// if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
// should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
// JS multiply "overflows" differently from C/C++, so care is needed here.
function bnpInvDigit() {
if(this.t < 1) return 0;
- var x = this[0];
+ var x = this.arr[0];
if((x&1) == 0) return 0;
var y = x&3; // y == 1/x mod 2^2
y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
@@ -477,16 +478,16 @@ function montRevert(x) {
// x = x/R mod m (HAC 14.32)
function montReduce(x) {
while(x.t <= this.mt2) // pad x so am has enough room later
- x[x.t++] = 0;
+ x.arr[x.t++] = 0;
for(var i = 0; i < this.m.t; ++i) {
- // faster way of calculating u0 = x[i]*mp mod DV
- var j = x[i]&0x7fff;
- var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
+ // faster way of calculating u0 = x.arr[i]*mp mod DV
+ var j = x.arr[i]&0x7fff;
+ var u0 = (j*this.mpl+(((j*this.mph+(x.arr[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
// use am to combine the multiply-shift-add into one call
j = i+this.m.t;
- x[j] += this.m.am(0,u0,x,i,0,this.m.t);
+ x.arr[j] += this.m.am(0,u0,x,i,0,this.m.t);
// propagate carry
- while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
+ while(x.arr[j] >= x.DV) { x.arr[j] -= x.DV; x.arr[++j]++; }
}
x.clamp();
x.drShiftTo(this.m.t,x);
@@ -506,7 +507,7 @@ Montgomery.prototype.mulTo = montMulTo;
Montgomery.prototype.sqrTo = montSqrTo;
// (protected) true iff this is even
-function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
+function bnpIsEven() { return ((this.t>0)?(this.arr[0]&1):this.s) == 0; }
// (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
function bnpExp(e,z) {
View
112 jsbn2.js
@@ -5,7 +5,6 @@
// Extended JavaScript BN functions, required for RSA private ops.
// Version 1.1: new BigInteger("0", 10) returns "proper" zero
-// Version 1.2: square() API, isProbablePrime fix
// (public)
function bnClone() { var r = nbi(); this.copyTo(r); return r; }
@@ -13,20 +12,20 @@ function bnClone() { var r = nbi(); this.copyTo(r); return r; }
// (public) return value as integer
function bnIntValue() {
if(this.s < 0) {
- if(this.t == 1) return this[0]-this.DV;
+ if(this.t == 1) return this.arr[0]-this.DV;
else if(this.t == 0) return -1;
}
- else if(this.t == 1) return this[0];
+ else if(this.t == 1) return this.arr[0];
else if(this.t == 0) return 0;
// assumes 16 < DB < 32
- return ((this[1]&((1<<(32-this.DB))-1))<<this.DB)|this[0];
+ return ((this.arr[1]&((1<<(32-this.DB))-1))<<this.DB)|this.arr[0];
}
// (public) return value as byte
-function bnByteValue() { return (this.t==0)?this.s:(this[0]<<24)>>24; }
+function bnByteValue() { return (this.t==0)?this.s:(this.arr[0]<<24)>>24; }
// (public) return value as short (assumes DB>=16)
-function bnShortValue() { return (this.t==0)?this.s:(this[0]<<16)>>16; }
+function bnShortValue() { return (this.t==0)?this.s:(this.arr[0]<<16)>>16; }
// (protected) return x s.t. r^x < DV
function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
@@ -34,7 +33,7 @@ function bnpChunkSize(r) { return Math.floor(Math.LN2*this.DB/Math.log(r)); }
// (public) 0 if this == 0, 1 if this > 0
function bnSigNum() {
if(this.s < 0) return -1;
- else if(this.t <= 0 || (this.t == 1 && this[0] <= 0)) return 0;
+ else if(this.t <= 0 || (this.t == 1 && this.arr[0] <= 0)) return 0;
else return 1;
}
@@ -101,7 +100,7 @@ function bnpFromNumber(a,b,c) {
var x = new Array(), t = a&7;
x.length = (a>>3)+1;
b.nextBytes(x);
- if(t > 0) x[0] &= ((1<<t)-1); else x[0] = 0;
+ if(t > 0) x.arr[0] &= ((1<<t)-1); else x.arr[0] = 0;
this.fromString(x,256);
}
}
@@ -109,23 +108,23 @@ function bnpFromNumber(a,b,c) {
// (public) convert to bigendian byte array
function bnToByteArray() {
var i = this.t, r = new Array();
- r[0] = this.s;
+ r.arr[0] = this.s;
var p = this.DB-(i*this.DB)%8, d, k = 0;
if(i-- > 0) {
- if(p < this.DB && (d = this[i]>>p) != (this.s&this.DM)>>p)
- r[k++] = d|(this.s<<(this.DB-p));
+ if(p < this.DB && (d = this.arr[i]>>p) != (this.s&this.DM)>>p)
+ r.arr[k++] = d|(this.s<<(this.DB-p));
while(i >= 0) {
if(p < 8) {
- d = (this[i]&((1<<p)-1))<<(8-p);
- d |= this[--i]>>(p+=this.DB-8);
+ d = (this.arr[i]&((1<<p)-1))<<(8-p);
+ d |= this.arr[--i]>>(p+=this.DB-8);
}
else {
- d = (this[i]>>(p-=8))&0xff;
+ d = (this.arr[i]>>(p-=8))&0xff;
if(p <= 0) { p += this.DB; --i; }
}
if((d&0x80) != 0) d |= -256;
if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
- if(k > 0 || d != this.s) r[k++] = d;
+ if(k > 0 || d != this.s) r.arr[k++] = d;
}
}
return r;
@@ -138,15 +137,15 @@ function bnMax(a) { return(this.compareTo(a)>0)?this:a; }
// (protected) r = this op a (bitwise)
function bnpBitwiseTo(a,op,r) {
var i, f, m = Math.min(a.t,this.t);
- for(i = 0; i < m; ++i) r[i] = op(this[i],a[i]);
+ for(i = 0; i < m; ++i) r.arr[i] = op(this.arr[i],a.arr[i]);
if(a.t < this.t) {
f = a.s&this.DM;
- for(i = m; i < this.t; ++i) r[i] = op(this[i],f);
+ for(i = m; i < this.t; ++i) r.arr[i] = op(this.arr[i],f);
r.t = this.t;
}
else {
f = this.s&this.DM;
- for(i = m; i < a.t; ++i) r[i] = op(f,a[i]);
+ for(i = m; i < a.t; ++i) r.arr[i] = op(f,a.arr[i]);
r.t = a.t;
}
r.s = op(this.s,a.s);
@@ -172,7 +171,7 @@ function bnAndNot(a) { var r = nbi(); this.bitwiseTo(a,op_andnot,r); return r; }
// (public) ~this
function bnNot() {
var r = nbi();
- for(var i = 0; i < this.t; ++i) r[i] = this.DM&~this[i];
+ for(var i = 0; i < this.t; ++i) r.arr[i] = this.DM&~this.arr[i];
r.t = this.t;
r.s = ~this.s;
return r;
@@ -207,7 +206,7 @@ function lbit(x) {
// (public) returns index of lowest 1-bit (or -1 if none)
function bnGetLowestSetBit() {
for(var i = 0; i < this.t; ++i)
- if(this[i] != 0) return i*this.DB+lbit(this[i]);
+ if(this.arr[i] != 0) return i*this.DB+lbit(this.arr[i]);
if(this.s < 0) return this.t*this.DB;
return -1;
}
@@ -222,7 +221,7 @@ function cbit(x) {
// (public) return number of set bits
function bnBitCount() {
var r = 0, x = this.s&this.DM;
- for(var i = 0; i < this.t; ++i) r += cbit(this[i]^x);
+ for(var i = 0; i < this.t; ++i) r += cbit(this.arr[i]^x);
return r;
}
@@ -230,7 +229,7 @@ function bnBitCount() {
function bnTestBit(n) {
var j = Math.floor(n/this.DB);
if(j >= this.t) return(this.s!=0);
- return((this[j]&(1<<(n%this.DB)))!=0);
+ return((this.arr[j]&(1<<(n%this.DB)))!=0);
}
// (protected) this op (1<<n)
@@ -253,15 +252,15 @@ function bnFlipBit(n) { return this.changeBit(n,op_xor); }
function bnpAddTo(a,r) {
var i = 0, c = 0, m = Math.min(a.t,this.t);
while(i < m) {
- c += this[i]+a[i];
- r[i++] = c&this.DM;
+ c += this.arr[i]+a.arr[i];
+ r.arr[i++] = c&this.DM;
c >>= this.DB;
}
if(a.t < this.t) {
c += a.s;
while(i < this.t) {
- c += this[i];
- r[i++] = c&this.DM;
+ c += this.arr[i];
+ r.arr[i++] = c&this.DM;
c >>= this.DB;
}
c += this.s;
@@ -269,15 +268,15 @@ function bnpAddTo(a,r) {
else {
c += this.s;
while(i < a.t) {
- c += a[i];
- r[i++] = c&this.DM;
+ c += a.arr[i];
+ r.arr[i++] = c&this.DM;
c >>= this.DB;
}
c += a.s;
}
r.s = (c<0)?-1:0;
- if(c > 0) r[i++] = c;
- else if(c < -1) r[i++] = this.DV+c;
+ if(c > 0) r.arr[i++] = c;
+ else if(c < -1) r.arr[i++] = this.DV+c;
r.t = i;
r.clamp();
}
@@ -291,9 +290,6 @@ function bnSubtract(a) { var r = nbi(); this.subTo(a,r); return r; }
// (public) this * a
function bnMultiply(a) { var r = nbi(); this.multiplyTo(a,r); return r; }
-// (public) this^2
-function bnSquare() { var r = nbi(); this.squareTo(r); return r; }
-
// (public) this / a
function bnDivide(a) { var r = nbi(); this.divRemTo(a,r,null); return r; }
@@ -309,7 +305,7 @@ function bnDivideAndRemainder(a) {
// (protected) this *= n, this >= 0, 1 < n < DV
function bnpDMultiply(n) {
- this[this.t] = this.am(0,n-1,this,0,0,this.t);
+ this.arr[this.t] = this.am(0,n-1,this,0,0,this.t);
++this.t;
this.clamp();
}
@@ -317,12 +313,12 @@ function bnpDMultiply(n) {
// (protected) this += n << w words, this >= 0
function bnpDAddOffset(n,w) {
if(n == 0) return;
- while(this.t <= w) this[this.t++] = 0;
- this[w] += n;
- while(this[w] >= this.DV) {
- this[w] -= this.DV;
- if(++w >= this.t) this[this.t++] = 0;
- ++this[w];
+ while(this.t <= w) this.arr[this.t++] = 0;
+ this.arr[w] += n;
+ while(this.arr[w] >= this.DV) {
+ this.arr[w] -= this.DV;
+ if(++w >= this.t) this.arr[this.t++] = 0;
+ ++this.arr[w];
}
}
@@ -346,10 +342,10 @@ function bnpMultiplyLowerTo(a,n,r) {
var i = Math.min(this.t+a.t,n);
r.s = 0; // assumes a,this >= 0
r.t = i;
- while(i > 0) r[--i] = 0;
+ while(i > 0) r.arr[--i] = 0;
var j;
- for(j = r.t-this.t; i < j; ++i) r[i+this.t] = this.am(0,a[i],r,i,0,this.t);
- for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a[i],r,i,0,n-i);
+ for(j = r.t-this.t; i < j; ++i) r.arr[i+this.t] = this.am(0,a.arr[i],r,i,0,this.t);
+ for(j = Math.min(a.t,n); i < j; ++i) this.am(0,a.arr[i],r,i,0,n-i);
r.clamp();
}
@@ -359,9 +355,9 @@ function bnpMultiplyUpperTo(a,n,r) {
--n;
var i = r.t = this.t+a.t-n;
r.s = 0; // assumes a,this >= 0
- while(--i >= 0) r[i] = 0;
+ while(--i >= 0) r.arr[i] = 0;
for(i = Math.max(n-this.t,0); i < a.t; ++i)
- r[this.t+i-n] = this.am(n-i,a[i],r,0,0,this.t+i-n);
+ r.arr[this.t+i-n] = this.am(n-i,a.arr[i],r,0,0,this.t+i-n);
r.clamp();
r.drShiftTo(1,r);
}
@@ -437,12 +433,12 @@ function bnModPow(e,m) {
}
var j = e.t-1, w, is1 = true, r2 = nbi(), t;
- i = nbits(e[j])-1;
+ i = nbits(e.arr[j])-1;
while(j >= 0) {
- if(i >= k1) w = (e[j]>>(i-k1))&km;
+ if(i >= k1) w = (e.arr[j]>>(i-k1))&km;
else {
- w = (e[j]&((1<<(i+1))-1))<<(k1-i);
- if(j > 0) w |= e[j-1]>>(this.DB+i-k1);
+ w = (e.arr[j]&((1<<(i+1))-1))<<(k1-i);
+ if(j > 0) w |= e.arr[j-1]>>(this.DB+i-k1);
}
n = k;
@@ -458,7 +454,7 @@ function bnModPow(e,m) {
z.mulTo(r2,g[w],r);
}
- while(j >= 0 && (e[j]&(1<<i)) == 0) {
+ while(j >= 0 && (e.arr[j]&(1<<i)) == 0) {
z.sqrTo(r,r2); t = r; r = r2; r2 = t;
if(--i < 0) { i = this.DB-1; --j; }
}
@@ -499,8 +495,8 @@ function bnpModInt(n) {
if(n <= 0) return 0;
var d = this.DV%n, r = (this.s<0)?n-1:0;
if(this.t > 0)
- if(d == 0) r = this[0]%n;
- else for(var i = this.t-1; i >= 0; --i) r = (d*r+this[i])%n;
+ if(d == 0) r = this.arr[0]%n;
+ else for(var i = this.t-1; i >= 0; --i) r = (d*r+this.arr[i])%n;
return r;
}
@@ -546,15 +542,15 @@ function bnModInverse(m) {
if(d.signum() < 0) return d.add(m); else return d;
}
-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];
+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];
var lplim = (1<<26)/lowprimes[lowprimes.length-1];
// (public) test primality with certainty >= 1-.5^t
function bnIsProbablePrime(t) {
var i, x = this.abs();
- if(x.t == 1 && x[0] <= lowprimes[lowprimes.length-1]) {
+ if(x.t == 1 && x.arr[0] <= lowprimes[lowprimes.length-1]) {
for(i = 0; i < lowprimes.length; ++i)
- if(x[0] == lowprimes[i]) return true;
+ if(x.arr[0] == lowprimes[i]) return true;
return false;
}
if(x.isEven()) return false;
@@ -578,8 +574,7 @@ function bnpMillerRabin(t) {
if(t > lowprimes.length) t = lowprimes.length;
var a = nbi();
for(var i = 0; i < t; ++i) {
- //Pick bases at random, instead of starting at 2
- a.fromInt(lowprimes[Math.floor(Math.random()*lowprimes.length)]);
+ a.fromInt(lowprimes[i]);
var y = a.modPow(r,this);
if(y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
var j = 1;
@@ -643,9 +638,6 @@ BigInteger.prototype.pow = bnPow;
BigInteger.prototype.gcd = bnGCD;
BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
-// JSBN-specific extension
-BigInteger.prototype.square = bnSquare;
-
// BigInteger interfaces not implemented in jsbn:
// BigInteger(int signum, byte[] magnitude)
Please sign in to comment.
Something went wrong with that request. Please try again.