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
119
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.1.7 to 0.2.0

56

lib/bn.js

@@ -894,2 +894,28 @@ var assert = require('assert');

BN.prototype._fastSqrtm = function _fastSqrtm(odd, num) {
assert(this.mont);
return r;
};
// Square root over p
BN.prototype.montSqrt = function montSqrt() {
assert(!this.sign, 'mont works only with positives');
assert(this.mont, 'montInvm works only with mont numbers');
assert(this.cmp(0) !== 0);
var mont = this.mont;
// Fast case
if (mont.m.andl(3) === 3) {
var pow = mont.m.add(1).ishr(2);
var r = this.montPow(pow);
return r;
}
// TODO(indutny): Tonelli-Shanks algorithm
throw new Error('Not implemented yet, for p % 4 !== 3');
};
BN.prototype.montInvm = function montInvm() {

@@ -915,2 +941,32 @@ assert(!this.sign, 'mont works only with positives');

BN.prototype.montPow = function montPow(num, base) {
if (!(num instanceof BN))
num = new BN(num, base);
assert(this.mont && !num.mont, 'montPow(montNum, normalNum)');
var w = [];
var q = num.clone();
while (q.cmp(0) !== 0) {
w.push(q.andl(1));
q.ishr(1);
}
// Skip leading zeroes
var res = this;
for (var i = 0; i < w.length; i++, res = res.montSqr()) {
if (w[i] !== 0)
break;
}
if (++i < w.length) {
for (var q = res.montSqr(); i < w.length; i++, q = q.montSqr()) {
if (w[i] === 0)
continue;
res = res.montMul(q);
}
}
return res;
};
BN.mont = function mont(num, base) {

@@ -917,0 +973,0 @@ return new Mont(num, base);

2

package.json
{
"name": "bn.js",
"version": "0.1.7",
"version": "0.2.0",
"description": "Big number implementation in pure javascript",

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

@@ -178,3 +178,29 @@ var assert = require('assert');

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);
});
it('should sqrtm numbers', function() {
var p = bn(263);
var m = bn.mont(p);
var q = bn(11).toMont(m);
var qr = q.montSqrt(true, p);
assert.equal(qr.montSqr().cmp(q), 0);
var qr = q.montSqrt(false, p);
assert.equal(qr.montSqr().cmp(q), 0);
var p = bn(
'fffffffffffffffffffffffffffffffeffffffffffffffff',
16);
var m = bn.mont(p);
var q = bn(13).toMont(m);
var qr = q.montSqrt(true, p);
assert.equal(qr.montSqr().cmp(q), 0);
var qr = q.montSqrt(false, p);
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