Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bn.js

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bn.js - npm Package Compare versions

Comparing version 0.4.4 to 0.5.0

225

lib/bn.js

@@ -39,3 +39,3 @@ function assert(val, msg) {

}
this.words = [ number & 0xffffff ];
this.words = [ number & 0x3ffffff ];
this.length = 1;

@@ -48,14 +48,19 @@ return;

this.words = new Array(this.length);
for (var i = 0; i < this.length; i++)
this.words[i] = 0;
// Assume big-endian
var delta = 3 - number.length % 3;
if (delta === 3)
delta = 0;
for (var i = 0; i < this.length; i++) {
var off = i * 3 - delta;
var w = number[off + 2] | (number[off + 1] << 8) | (number[off] << 16);
this.words[this.length - i - 1] = w;
var off = 0;
for (var i = number.length - 1, j = 0; i >= 0; i -= 3) {
var w = number[i] | (number[i - 1] << 8) | (number[i - 2] << 16);
this.words[j] |= (w << off) & 0x3ffffff;
this.words[j + 1] = (w >> (26 - off)) & 0x3ffffff;
off += 24;
if (off >= 26) {
off -= 26;
j++;
}
}
return;
return this.strip();
}

@@ -83,10 +88,26 @@ if (base === 'hex')

BN.prototype._parseHex = function parseHex(number, start) {
// Scan 3-byte chunks
this.length = Math.ceil((number.length - start) / 7);
// Create possibly bigger array to ensure that it fits the number
this.length = Math.ceil((number.length - start) / 6);
this.words = new Array(this.length);
for (var i = number.length - 6, j = 0; i >= start; i -= 6, j++)
this.words[j] = parseInt(number.slice(i, i + 6), 16);
if (i + 6 !== start)
this.words[j++] = parseInt(number.slice(start, i + 6), 16);
this.length = this.words.length;
for (var i = 0; i < this.words.length; i++)
this.words[i] = 0;
// Scan 24-bit chunks and add them to the number
var off = 0;
for (var i = number.length - 6, j = 0; i >= start; i -= 6) {
var w = parseInt(number.slice(i, i + 6), 16);
this.words[j] |= (w << off) & 0x3ffffff;
this.words[j + 1] |= w >> (26 - off) & 0x3fffff;
off += 24;
if (off >= 26) {
off -= 26;
j++;
}
}
if (i + 6 !== start) {
var w = parseInt(number.slice(start, i + 6), 16);
this.words[j] |= (w << off) & 0x3ffffff;
this.words[j + 1] |= w >> (26 - off) & 0x3fffff;
}
this.strip();
};

@@ -118,3 +139,3 @@

if (q > 0xfffff) {
assert(q <= 0xffffff);
assert(q <= 0x3ffffff);
if (!bigQ)

@@ -216,10 +237,23 @@ bigQ = new BN(q);

if (base === 16 || base === 'hex') {
var out = this.sign ? '-' : '';
for (var i = this.length - 1; i >= 0; i--) {
var word = this.words[i].toString(16);
if (i !== this.length - 1)
out += zero6(word);
var out = '';
var off = 0;
var carry = 0;
for (var i = 0; i < this.length; i++) {
var w = this.words[i];
var word = (((w << off) | carry) & 0xffffff).toString(16);
carry = (w >> (24 - off)) & 0xffffff;
if (carry !== 0 || i !== this.length - 1)
out = zero6(word) + out;
else
out += word;
out = word + out;
off += 2;
if (off >= 26) {
off -= 26;
i--;
}
}
if (carry !== 0)
out = carry.toString(16) + out;
if (this.sign)
out = '-' + out;
return out;

@@ -236,3 +270,3 @@ } else if (base === 10) {

assert(r.length <= 2);
r = r.length === 2 ? (r.words[0] + r.words[1] * 0x1000000) : r.words[0];
r = r.length === 2 ? (r.words[0] + r.words[1] * 0x4000000) : r.words[0];
if (c.cmpn(0) !== 0)

@@ -283,6 +317,6 @@ out = zero14(r + '') + out;

return new Function('w', 'return ' + arr.join(':\n') + ':\n0;');
return new Function('w', 'return ' + arr.join(' :\n') + ' :\n0;');
};
BN.prototype._countBits = genCountBits(24);
BN.prototype._countBits = genCountBits(26);
*/

@@ -292,26 +326,28 @@

BN.prototype._countBits = function _countBits(w) {
return w >= 0x800000 ? 24:
w >= 0x400000 ? 23:
w >= 0x200000 ? 22:
w >= 0x100000 ? 21:
w >= 0x80000 ? 20:
w >= 0x40000 ? 19:
w >= 0x20000 ? 18:
w >= 0x10000 ? 17:
w >= 0x8000 ? 16:
w >= 0x4000 ? 15:
w >= 0x2000 ? 14:
w >= 0x1000 ? 13:
w >= 0x800 ? 12:
w >= 0x400 ? 11:
w >= 0x200 ? 10:
w >= 0x100 ? 9:
w >= 0x80 ? 8:
w >= 0x40 ? 7:
w >= 0x20 ? 6:
w >= 0x10 ? 5:
w >= 0x8 ? 4:
w >= 0x4 ? 3:
w >= 0x2 ? 2:
w >= 0x1 ? 1:
return w >= 0x2000000 ? 26 :
w >= 0x1000000 ? 25 :
w >= 0x800000 ? 24 :
w >= 0x400000 ? 23 :
w >= 0x200000 ? 22 :
w >= 0x100000 ? 21 :
w >= 0x80000 ? 20 :
w >= 0x40000 ? 19 :
w >= 0x20000 ? 18 :
w >= 0x10000 ? 17 :
w >= 0x8000 ? 16 :
w >= 0x4000 ? 15 :
w >= 0x2000 ? 14 :
w >= 0x1000 ? 13 :
w >= 0x800 ? 12 :
w >= 0x400 ? 11 :
w >= 0x200 ? 10 :
w >= 0x100 ? 9 :
w >= 0x80 ? 8 :
w >= 0x40 ? 7 :
w >= 0x20 ? 6 :
w >= 0x10 ? 5 :
w >= 0x8 ? 4 :
w >= 0x4 ? 3 :
w >= 0x2 ? 2 :
w >= 0x1 ? 1 :
0;

@@ -326,3 +362,3 @@ }

var hi = this._countBits(w);
return (this.length - 1) * 24 + hi;
return (this.length - 1) * 26 + hi;
};

@@ -334,4 +370,3 @@

var w = this.words[this.length - 1];
var bytes = w <= 0xff ? 1 : w <= 0xffff ? 2 : 3;
return (this.length - 1) * 3 + bytes;
return Math.ceil(this.bitLength() / 8);
};

@@ -380,9 +415,9 @@

var r = a.words[i] + b.words[i] + carry;
this.words[i] = r & 0xffffff;
carry = r >> 24;
this.words[i] = r & 0x3ffffff;
carry = r >> 26;
}
for (; carry !== 0 && i < a.length; i++) {
var r = a.words[i] + carry;
this.words[i] = r & 0xffffff;
carry = r >> 24;
this.words[i] = r & 0x3ffffff;
carry = r >> 26;
}

@@ -457,3 +492,3 @@

if (r < 0) {
r += 0x1000000;
r += 0x4000000;
carry = 1;

@@ -468,3 +503,3 @@ } else {

if (r < 0) {
r += 0x1000000;
r += 0x4000000;
carry = 1;

@@ -515,7 +550,7 @@ } else {

var lo = r & 0xffffff;
var carry = (r - lo) / 0x1000000;
var lo = r & 0x3ffffff;
var carry = (r - lo) / 0x4000000;
lo = result.words[k] + lo;
result.words[k] = lo & 0xffffff;
carry += lo >> 24;
result.words[k] = lo & 0x3ffffff;
carry += lo >> 26;
}

@@ -525,8 +560,8 @@

k++;
assert(carry <= 0xffffff);
assert(carry <= 0x3ffffff);
for (; carry !== 0; k++) {
if (result.words[k]) {
carry += result.words[k];
result.words[k] = carry & 0xffffff;
carry >>= 24;
result.words[k] = carry & 0x3ffffff;
carry >>= 26;
} else {

@@ -560,7 +595,7 @@ result.words[k] = carry;

var lo = r & 0xffffff;
var carry = (r - lo) / 0x1000000;
var lo = r & 0x3ffffff;
var carry = (r - lo) / 0x4000000;
lo = res.words[k] + lo;
res.words[k] = lo & 0xffffff;
carry += lo >> 24;
res.words[k] = lo & 0x3ffffff;
carry += lo >> 26;
}

@@ -570,8 +605,8 @@

k++;
assert(carry <= 0xffffff);
assert(carry <= 0x3ffffff);
for (; carry !== 0; k++) {
if (res.words[k]) {
carry += res.words[k];
res.words[k] = carry & 0xffffff;
carry >>= 24;
res.words[k] = carry & 0x3ffffff;
carry >>= 26;
} else {

@@ -590,5 +625,5 @@ res.words[k] = carry;

assert(typeof bits === 'number' && bits >= 0);
var r = bits % 24;
var s = (bits - r) / 24;
var carryMask = (0xffffff >> (24 - r)) << (24 - r);
var r = bits % 26;
var s = (bits - r) / 26;
var carryMask = (0x3ffffff >> (26 - r)) << (26 - r);

@@ -601,3 +636,3 @@ if (r !== 0) {

this.words[i] = c | carry;
carry = newCarry >> (24 - r);
carry = newCarry >> (26 - r);
}

@@ -628,7 +663,7 @@ if (carry) {

else
hint = (hint - (hint % 24)) / 24;
hint = (hint - (hint % 26)) / 26;
var r = bits % 24;
var s = (bits - r) / 24;
var mask = 0xffffff ^ ((0xffffff >> r) << r);
var r = bits % 26;
var s = (bits - r) / 26;
var mask = 0x3ffffff ^ ((0x3ffffff >> r) << r);

@@ -648,3 +683,3 @@ if (s !== 0) {

var word = this.words[i];
this.words[i] = (carry << (24 - r)) | (this.words[i] >> r);
this.words[i] = (carry << (26 - r)) | (this.words[i] >> r);
carry = word & mask;

@@ -675,4 +710,4 @@ }

assert(typeof bits === 'number' && bits >= 0);
var r = bits % 24;
var s = (bits - r) / 24;
var r = bits % 26;
var s = (bits - r) / 26;

@@ -687,3 +722,3 @@ assert(!this.sign, 'imaskn works only with positive numbers');

if (r !== 0) {
var mask = 0xffffff ^ ((0xffffff >> r) << r);
var mask = 0x3ffffff ^ ((0x3ffffff >> r) << r);
this.words[this.words.length - 1] &= mask;

@@ -708,4 +743,4 @@ }

// Carry
for (var i = 0; i < this.length && this.words[i] >= 0x1000000; i++) {
this.words[i] -= 0x1000000;
for (var i = 0; i < this.length && this.words[i] >= 0x4000000; i++) {
this.words[i] -= 0x4000000;
if (i == this.length - 1)

@@ -731,3 +766,3 @@ this.words[i + 1] = 1;

for (var i = 0; i < this.length && this.words[i] < 0; i++) {
this.words[i] += 0x1000000;
this.words[i] += 0x4000000;
this.words[i + 1] -= 1;

@@ -890,4 +925,4 @@ }

assert(typeof bit === 'number');
var r = bit % 24;
var s = (bit - r) / 24;
var r = bit % 26;
var s = (bit - r) / 26;
var q = 1 << r;

@@ -909,4 +944,4 @@

w += carry;
carry = w >> 24;
w &= 0xffffff;
carry = w >> 26;
w &= 0x3ffffff;
this.words[i] = w;

@@ -930,3 +965,3 @@ }

num &= 0xffffff;
num &= 0x3ffffff;
this.strip();

@@ -1167,4 +1202,4 @@

this.shift = this.m.bitLength();
if (this.shift % 24 !== 0)
this.shift += 24 - (this.shift % 24);
if (this.shift % 26 !== 0)
this.shift += 26 - (this.shift % 26);
this.r = new BN(1).ishln(this.shift);

@@ -1171,0 +1206,0 @@ this.r2 = this.r.sqr().mod(this.m);

{
"name": "bn.js",
"version": "0.4.4",
"version": "0.5.0",
"description": "Big number implementation in pure javascript",

@@ -5,0 +5,0 @@ "main": "lib/bn.js",

@@ -21,2 +21,5 @@ var assert = require('assert');

'a89ce5af8724c0a23e0e0ff77500');
assert.equal(new BN('123456789abcdef123456789abcdef123456789abcdef',
16).toString(16),
'123456789abcdef123456789abcdef123456789abcdef');
assert.equal(new BN('10654321').toString(), '10654321');

@@ -29,3 +32,6 @@ });

assert.equal(new BN([1,2,3,4,5]).toString(16), '102030405');
assert.equal(new BN([1,2,3,4,5,6,7,8]).toString(16), '102030405060708');
assert.equal(new BN([1,2,3,4]).toArray().join(','), '1,2,3,4');
assert.equal(new BN([1,2,3,4,5,6,7,8]).toArray().join(','),
'1,2,3,4,5,6,7,8');
});

@@ -32,0 +38,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc