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.3.1 to 0.4.0

160

lib/bn.js

@@ -12,8 +12,9 @@ function assert(val, msg) {

function BN(number, base) {
if (number instanceof BN)
// May be `new BN(bn)` ?
if (number !== null &&
typeof number === 'object' &&
Array.isArray(number.words)) {
return number;
}
if (!(this instanceof BN))
return new BN(number, base);
this.sign = false;

@@ -31,2 +32,4 @@ this.words = null;

BN.BN = BN;
BN.prototype._init = function init(number, base) {

@@ -195,3 +198,3 @@ if (typeof number === 'number') {

c.sign = false;
while (c.cmp(0) !== 0) {
while (c.cmpn(0) !== 0) {
var tmp = c._div(div10);

@@ -203,3 +206,3 @@ var r = tmp.mod;

r = r.length === 2 ? (r.words[0] + r.words[1] * 0x1000000) : r.words[0];
if (c.cmp(0) !== 0)
if (c.cmpn(0) !== 0)
out = zero14(r + '') + out;

@@ -209,3 +212,3 @@ else

}
if (this.cmp(0) === 0)
if (this.cmpn(0) === 0)
out = '0' + out;

@@ -230,4 +233,4 @@ if (this.sign)

var q = this.clone();
for (var i = 0; q.cmp(0) !== 0; i++) {
var b = q.andl(0xff);
for (var i = 0; q.cmpn(0) !== 0; i++) {
var b = q.andln(0xff);
q.ishrn(8);

@@ -281,5 +284,2 @@

BN.prototype.iadd = function iadd(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
// negative + positive

@@ -338,5 +338,2 @@ if (this.sign && !num.sign) {

BN.prototype.add = function add(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
if (num.sign && !this.sign)

@@ -355,5 +352,2 @@ return this.sub(num.neg());

BN.prototype.isub = function isub(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
// this - (-num) = this + num

@@ -435,9 +429,6 @@ if (num.sign) {

BN.prototype.mul = function mul(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
if (this === num)
return this.sqr();
if (this.cmp(0) === 0 || num.cmp(0) === 0)
if (this.cmpn(0) === 0 || num.cmpn(0) === 0)
return new BN(0);

@@ -486,3 +477,3 @@

BN.prototype.sqr = function sqr() {
if (this.cmp(0) === 0)
if (this.cmpn(0) === 0)
return new BN(0);

@@ -657,3 +648,3 @@

assert(typeof num === 'number');
assert(this.cmp(num) >= 0, 'Sign change is not supported in isubn');
assert(this.cmpn(num) >= 0, 'Sign change is not supported in isubn');
if (num < 0)

@@ -688,3 +679,3 @@ return this.iaddn(-num);

c.isub(max);
r.binc(shift);
r.bincn(shift);
}

@@ -701,5 +692,3 @@ var delta = Math.max(1, maxLen - c.bitLength());

BN.prototype._div = function _div(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
assert(num.cmp(0) !== 0);
assert(num.cmpn(0) !== 0);

@@ -710,3 +699,3 @@ if (this.sign && !num.sign) {

div: res.div.neg(),
mod: res.mod.cmp(0) === 0 ? res.mod : num.sub(res.mod)
mod: res.mod.cmpn(0) === 0 ? res.mod : num.sub(res.mod)
};

@@ -743,3 +732,3 @@ } else if (!this.sign && num.sign) {

assert(!p.sign);
assert(p.cmp(0) !== 0);
assert(p.cmpn(0) !== 0);

@@ -753,7 +742,7 @@ var a = this;

a = a.clone();
assert(a.cmp(0) !== 0);
assert(a.cmpn(0) !== 0);
x1 = x1.clone();
var x2 = new BN(0);
while (a.cmp(1) !== 0 && b.cmp(1) !== 0) {
while (a.cmpn(1) !== 0 && b.cmpn(1) !== 0) {
while (a.isEven()) {

@@ -781,3 +770,3 @@ a.ishrn(1);

}
if (a.cmp(1) === 0)
if (a.cmpn(1) === 0)
return x1.mod(p);

@@ -790,4 +779,2 @@ else

BN.prototype.invm = function invm(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
return this._egcd(new BN(1), num);

@@ -805,3 +792,3 @@ };

// And first word and num
BN.prototype.andl = function andl(num) {
BN.prototype.andln = function andln(num) {
return this.words[0] & num;

@@ -811,3 +798,3 @@ };

// Increment at the bit position in-line
BN.prototype.binc = function binc(bit) {
BN.prototype.bincn = function bincn(bit) {
assert(typeof bit === 'number');

@@ -842,2 +829,27 @@ var r = bit % 24;

BN.prototype.cmpn = function cmpn(num) {
var sign = num < 0;
if (sign)
num = -num;
if (this.sign && !sign)
return -1;
else if (!this.sign && sign)
return 1;
num &= 0xffffff;
this.strip();
var res;
if (this.length > 1) {
res = 1;
} else {
var w = this.words[0];
res = w === num ? 0 : w < num ? -1 : 1;
}
if (this.sign)
res = -res;
return res;
};
// Compare two numbers and return:

@@ -848,33 +860,2 @@ // 1 - if `this` > `num`

BN.prototype.cmp = function cmp(num, base) {
// Fast number checks
if (typeof num === 'number') {
var sign = num < 0;
if (sign)
num = -num;
num &= 0xffffff;
this.strip();
if (this.sign && !sign)
return -1;
else if (!this.sign && sign)
return 1;
var res;
if (this.length > 1) {
res = 1;
} else {
var w = this.words[0];
res = w === num ? 0 : w < num ? -1 : 1;
}
if (this.sign)
res = -res;
return res;
}
if (!(num instanceof BN))
num = new BN(num, base);
this.strip();
num.strip();
if (this.sign && !num.sign)

@@ -885,2 +866,5 @@ return -1;

this.strip();
num.strip();
// At this point both numbers have the same sign

@@ -938,4 +922,2 @@ if (this.length > num.length)

BN.prototype.montAdd = function montAdd(num, base) {
if (!(num instanceof BN))
num = new BN(num, base).toMont(this.mont);
this._montVerify(num);

@@ -952,4 +934,2 @@

BN.prototype.montIAdd = function montIAdd(num, base) {
if (!(num instanceof BN))
num = new BN(num, base).toMont(this.mont);
this._montVerify(num);

@@ -966,4 +946,2 @@

BN.prototype.montSub = function montSub(num, base) {
if (!(num instanceof BN))
num = new BN(num, base).toMont(this.mont);
this._montVerify(num);

@@ -973,3 +951,3 @@

var res = this.sub(num);
if (res.cmp(0) < 0)
if (res.cmpn(0) < 0)
res.iadd(mont.m);

@@ -982,4 +960,2 @@

BN.prototype.montISub = function montISub(num, base) {
if (!(num instanceof BN))
num = new BN(num, base).toMont(this.mont);
this._montVerify(num);

@@ -989,3 +965,3 @@

var res = this.isub(num);
if (res.cmp(0) < 0)
if (res.cmpn(0) < 0)
res.iadd(mont.m);

@@ -1008,8 +984,6 @@ res.mont = mont;

BN.prototype.montMul = function montMul(num, base) {
if (!(num instanceof BN))
num = new BN(num, base).toMont(this.mont);
this._montVerify(num);
var mont = this.mont;
if (this.cmp(0) === 0 || num.cmp(0) === 0) {
if (this.cmpn(0) === 0 || num.cmpn(0) === 0) {
var res = new BN(0);

@@ -1026,3 +1000,3 @@ res.mont = mont;

res = u.isub(mont.m);
else if (u.cmp(0) < 0)
else if (u.cmpn(0) < 0)
res = u.iadd(mont.m);

@@ -1042,3 +1016,3 @@

assert(this.mont, 'montInvm works only with mont numbers');
assert(this.cmp(0) !== 0);
assert(this.cmpn(0) !== 0);

@@ -1048,4 +1022,4 @@ var mont = this.mont;

// Fast case
if (mont.m.andl(3) === 3) {
var pow = mont.m.add(1).ishrn(2);
if (mont.m.andln(3) === 3) {
var pow = mont.m.add(new BN(1)).ishrn(2);
var r = this.montPow(pow);

@@ -1080,4 +1054,2 @@ return r;

BN.prototype.montPow = function montPow(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
assert(this.mont && !num.mont, 'montPow(montNum, normalNum)');

@@ -1087,4 +1059,4 @@

var q = num.clone();
while (q.cmp(0) !== 0) {
w.push(q.andl(1));
while (q.cmpn(0) !== 0) {
w.push(q.andln(1));
q.ishrn(1);

@@ -1115,4 +1087,4 @@ }

function Mont(num, base) {
this.m = new BN(num, base);
function Mont(m) {
this.m = m;
this.shift = this.m.bitLength();

@@ -1126,3 +1098,7 @@ if (this.shift % 24 !== 0)

// TODO(indutny): simplify it
this.minv = this.rinv.mul(this.r).sub(1).div(this.m).neg().mod(this.r);
this.minv = this.rinv.mul(this.r)
.sub(new BN(1))
.div(this.m)
.neg()
.mod(this.r);
}
{
"name": "bn.js",
"version": "0.3.1",
"version": "0.4.0",
"description": "Big number implementation in pure javascript",

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

var assert = require('assert');
var bn = require('../');
var BN = require('../').BN;
describe('BN', function() {
it('should work with Number input', function() {
assert.equal(bn(12345).toString(16), '3039');
assert.equal(new BN(12345).toString(16), '3039');
});
it('should work with String input', function() {
assert.equal(bn('29048849665247').toString(16),
assert.equal(new BN('29048849665247').toString(16),
'1a6b765d8cdf');
assert.equal(bn('-29048849665247').toString(16),
assert.equal(new BN('-29048849665247').toString(16),
'-1a6b765d8cdf');
assert.equal(bn('1A6B765D8CDF', 16).toString(16),
assert.equal(new BN('1A6B765D8CDF', 16).toString(16),
'1a6b765d8cdf');
assert.equal(bn('FF', 16).toString(), '255');
assert.equal(bn('1A6B765D8CDF', 16).toString(),
assert.equal(new BN('FF', 16).toString(), '255');
assert.equal(new BN('1A6B765D8CDF', 16).toString(),
'29048849665247');
assert.equal(bn('a89c e5af8724 c0a23e0e 0ff77500', 16).toString(16),
assert.equal(new BN('a89c e5af8724 c0a23e0e 0ff77500', 16).toString(16),
'a89ce5af8724c0a23e0e0ff77500');
assert.equal(bn('10654321').toString(), '10654321');
assert.equal(new BN('10654321').toString(), '10654321');
});
it('should import/export big endian', function() {
assert.equal(bn([1,2,3]).toString(16), '10203');
assert.equal(bn([1,2,3,4]).toString(16), '1020304');
assert.equal(bn([1,2,3,4,5]).toString(16), '102030405');
assert.equal(bn([1,2,3,4]).toArray().join(','), '1,2,3,4');
assert.equal(new BN([1,2,3]).toString(16), '10203');
assert.equal(new BN([1,2,3,4]).toString(16), '1020304');
assert.equal(new BN([1,2,3,4,5]).toString(16), '102030405');
assert.equal(new BN([1,2,3,4]).toArray().join(','), '1,2,3,4');
});
it('should return proper bitLength', function() {
assert.equal(bn(0).bitLength(), 0);
assert.equal(bn(0x1).bitLength(), 1);
assert.equal(bn(0x2).bitLength(), 2);
assert.equal(bn(0x3).bitLength(), 2);
assert.equal(bn(0x4).bitLength(), 3);
assert.equal(bn(0x8).bitLength(), 4);
assert.equal(bn(0x10).bitLength(), 5);
assert.equal(bn(0x100).bitLength(), 9);
assert.equal(bn(0x123456).bitLength(), 21);
assert.equal(bn('123456789', 16).bitLength(), 33);
assert.equal(bn('8023456789', 16).bitLength(), 40);
assert.equal(new BN(0).bitLength(), 0);
assert.equal(new BN(0x1).bitLength(), 1);
assert.equal(new BN(0x2).bitLength(), 2);
assert.equal(new BN(0x3).bitLength(), 2);
assert.equal(new BN(0x4).bitLength(), 3);
assert.equal(new BN(0x8).bitLength(), 4);
assert.equal(new BN(0x10).bitLength(), 5);
assert.equal(new BN(0x100).bitLength(), 9);
assert.equal(new BN(0x123456).bitLength(), 21);
assert.equal(new BN('123456789', 16).bitLength(), 33);
assert.equal(new BN('8023456789', 16).bitLength(), 40);
});
it('should add numbers', function() {
assert.equal(bn(14).add(26).toString(16), '28');
var k = bn(0x1234);
assert.equal(new BN(14).add(new BN(26)).toString(16), '28');
var k = new BN(0x1234);
var r = k;

@@ -53,4 +53,4 @@ for (var i = 0; i < 257; i++)

var k = bn('abcdefabcdefabcdef', 16);
var r = bn('deadbeef', 16);
var k = new BN('abcdefabcdefabcdef', 16);
var r = new BN('deadbeef', 16);
for (var i = 0; i < 257; i++)

@@ -62,15 +62,15 @@ r.iadd(k);

it('should subtract numbers', function() {
assert.equal(bn(14).sub(26).toString(16), '-c');
assert.equal(bn(26).sub(14).toString(16), 'c');
assert.equal(bn(26).sub(26).toString(16), '0');
assert.equal(bn(-26).sub(26).toString(16), '-34');
assert.equal(new BN(14).sub(new BN(26)).toString(16), '-c');
assert.equal(new BN(26).sub(new BN(14)).toString(16), 'c');
assert.equal(new BN(26).sub(new BN(26)).toString(16), '0');
assert.equal(new BN(-26).sub(new BN(26)).toString(16), '-34');
var a = new bn(
var a = new BN(
'31ff3c61db2db84b9823d320907a573f6ad37c437abe458b1802cda041d6384' +
'a7d8daef41395491e2',
16);
var b = new bn(
var b = new BN(
'6f0e4d9f1d6071c183677f601af9305721c91d31b0bbbae8fb790000',
16);
var r = new bn(
var r = new BN(
'31ff3c61db2db84b9823d3208989726578fd75276287cd9516533a9acfb9a67' +

@@ -84,9 +84,12 @@ '76281f34583ddb91e2',

var r = b.sub(new BN(14));
assert.equal(b.clone().isubn(14).cmp(r), 0);
// Carry and copy
var a = new bn('12345', 16);
var b = new bn('1000000000000', 16);
var a = new BN('12345', 16);
var b = new BN('1000000000000', 16);
assert.equal(a.isub(b).toString(16), '-fffffffedcbb');
var a = new bn('12345', 16);
var b = new bn('1000000000000', 16);
var a = new BN('12345', 16);
var b = new BN('1000000000000', 16);
assert.equal(b.isub(a).toString(16), 'fffffffedcbb');

@@ -96,9 +99,9 @@ });

it('should mul numbers', function() {
assert.equal(bn(0x1001).mul(0x1234).toString(16),
assert.equal(new BN(0x1001).mul(new BN(0x1234)).toString(16),
'1235234');
assert.equal(bn(-0x1001).mul(0x1234).toString(16),
assert.equal(new BN(-0x1001).mul(new BN(0x1234)).toString(16),
'-1235234');
assert.equal(bn(-0x1001).mul(-0x1234).toString(16),
assert.equal(new BN(-0x1001).mul(new BN(-0x1234)).toString(16),
'1235234');
var n = bn(0x1001);
var n = new BN(0x1001);
var r = n;

@@ -110,3 +113,3 @@ for (var i = 0; i < 4; i++)

var n = bn(
var n = new BN(
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',

@@ -127,10 +130,10 @@ 16

it('should div numbers', function() {
assert.equal(bn('10').div('256').toString(16),
assert.equal(new BN('10').div(new BN(256)).toString(16),
'0');
assert.equal(bn('69527932928').div('16974594').toString(16),
assert.equal(new BN('69527932928').div(new BN('16974594')).toString(16),
'fff');
assert.equal(bn('-69527932928').div('16974594').toString(16),
assert.equal(new BN('-69527932928').div(new BN('16974594')).toString(16),
'-fff');
var b = bn(
var b = new BN(
'39e58a8055b6fb264b75ec8c646509784204ac15a8c24e05babc9729ab9' +

@@ -140,3 +143,3 @@ 'b055c3a9458e4ce3289560a38e08ba8175a9446ce14e608245ab3a9' +

16);
var n = bn(
var n = new BN(
'79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',

@@ -149,7 +152,7 @@ 16

it('should mod numbers', function() {
assert.equal(bn('10').mod('256').toString(16),
assert.equal(new BN('10').mod(new BN(256)).toString(16),
'a');
assert.equal(bn('69527932928').mod('16974594').toString(16),
assert.equal(new BN('69527932928').mod(new BN('16974594')).toString(16),
'102f302');
assert.equal(bn('-69527932928').mod('16974594').toString(16),
assert.equal(new BN('-69527932928').mod(new BN('16974594')).toString(16),
'1000');

@@ -159,5 +162,5 @@ });

it('should shl numbers', function() {
assert.equal(bn('69527932928').shln(13).toString(16),
assert.equal(new BN('69527932928').shln(13).toString(16),
'2060602000000');
assert.equal(bn('69527932928').shln(45).toString(16),
assert.equal(new BN('69527932928').shln(45).toString(16),
'206060200000000000000');

@@ -167,5 +170,5 @@ });

it('should shr numbers', function() {
assert.equal(bn('69527932928').shrn(13).toString(16),
assert.equal(new BN('69527932928').shrn(13).toString(16),
'818180');
assert.equal(bn('69527932928').shrn(17).toString(16),
assert.equal(new BN('69527932928').shrn(17).toString(16),
'81818');

@@ -175,11 +178,11 @@ });

it('should invm numbers', function() {
var p = bn(257);
var a = bn(3);
var p = new BN(257);
var a = new BN(3);
var b = a.invm(p);
assert.equal(a.mul(b).mod(p).toString(16), '1');
var p192 = bn(
var p192 = new BN(
'fffffffffffffffffffffffffffffffeffffffffffffffff',
16);
var a = bn('deadbeef', 16);
var a = new BN('deadbeef', 16);
var b = a.invm(p192);

@@ -189,39 +192,42 @@ assert.equal(a.mul(b).mod(p192).toString(16), '1');

it('should support binc', function() {
assert.equal(bn(0).binc(1).toString(16), '2');
assert.equal(bn(2).binc(1).toString(16), '4');
assert.equal(bn(2).binc(1).binc(1).toString(16),
bn(2).binc(2).toString(16));
assert.equal(bn(0xffffff).binc(1).toString(16), '1000001');
it('should support bincn', function() {
assert.equal(new BN(0).bincn(1).toString(16), '2');
assert.equal(new BN(2).bincn(1).toString(16), '4');
assert.equal(new BN(2).bincn(1).bincn(1).toString(16),
new BN(2).bincn(2).toString(16));
assert.equal(new BN(0xffffff).bincn(1).toString(16), '1000001');
});
it('should support imaskn', function() {
assert.equal(bn(0).imaskn(1).toString(16), '0');
assert.equal(bn(3).imaskn(1).toString(16), '1');
assert.equal(bn('123456789', 16).imaskn(4).toString(16), '9');
assert.equal(bn('123456789', 16).imaskn(16).toString(16), '6789');
assert.equal(bn('123456789', 16).imaskn(28).toString(16), '3456789');
assert.equal(new BN(0).imaskn(1).toString(16), '0');
assert.equal(new BN(3).imaskn(1).toString(16), '1');
assert.equal(new BN('123456789', 16).imaskn(4).toString(16), '9');
assert.equal(new BN('123456789', 16).imaskn(16).toString(16), '6789');
assert.equal(new BN('123456789', 16).imaskn(28).toString(16), '3456789');
});
it('should support montgomery operations', function() {
var p192 = bn(
var p192 = new BN(
'fffffffffffffffffffffffffffffffeffffffffffffffff',
16);
var m = bn.mont(p192);
var a = bn(123);
var b = bn(231);
var m = BN.mont(p192);
var a = new BN(123);
var b = new BN(231);
var c = a.toMont(m).montMul(b.toMont(m)).fromMont();
assert(c.cmp(a.mul(b).mod(p192)) === 0);
assert.equal(a.toMont(m).montPow(3).fromMont().cmp(a.sqr().mul(a)), 0);
assert.equal(a.toMont(m).montPow(4).fromMont().cmp(a.sqr().sqr()), 0);
assert.equal(a.toMont(m).montPow(8).fromMont().cmp(a.sqr().sqr().sqr()), 0);
assert.equal(a.toMont(m).montPow(9).fromMont()
.cmp(a.sqr().sqr().sqr().mul(a)), 0);
assert.equal(a.toMont(m).montPow(new BN(3)).fromMont()
.cmp(a.sqr().mul(a)), 0);
assert.equal(a.toMont(m).montPow(new BN(4)).fromMont()
.cmp(a.sqr().sqr()), 0);
assert.equal(a.toMont(m).montPow(new BN(8)).fromMont()
.cmp(a.sqr().sqr().sqr()), 0);
assert.equal(a.toMont(m).montPow(new BN(9)).fromMont()
.cmp(a.sqr().sqr().sqr().mul(a)), 0);
});
it('should sqrtm numbers', function() {
var p = bn(263);
var m = bn.mont(p);
var q = bn(11).toMont(m);
var p = new BN(263);
var m = BN.mont(p);
var q = new BN(11).toMont(m);
var qr = q.montSqrt(true, p);

@@ -232,7 +238,7 @@ assert.equal(qr.montSqr().cmp(q), 0);

var p = bn(
var p = new BN(
'fffffffffffffffffffffffffffffffeffffffffffffffff',
16);
var m = bn.mont(p);
var q = bn(13).toMont(m);
var m = BN.mont(p);
var q = new BN(13).toMont(m);
var qr = q.montSqrt(true, p);

@@ -239,0 +245,0 @@ assert.equal(qr.montSqr().cmp(q), 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