Comparing version 3.2.0 to 3.3.0
@@ -558,2 +558,15 @@ (function (module, exports) { | ||
function toBitArray(num) { | ||
var w = new Array(num.bitLength()); | ||
for (var bit = 0; bit < w.length; bit++) { | ||
var off = (bit / 26) | 0; | ||
var wbit = bit % 26; | ||
w[bit] = (num.words[off] & (1 << wbit)) >>> wbit; | ||
} | ||
return w; | ||
} | ||
// Number of trailing zero bits | ||
@@ -1084,2 +1097,25 @@ BN.prototype.zeroBits = function zeroBits() { | ||
// Math.pow(`this`, `num`) | ||
BN.prototype.pow = function pow(num) { | ||
var w = toBitArray(num); | ||
if (w.length === 0) | ||
return new BN(1); | ||
// Skip leading zeroes | ||
var res = this; | ||
for (var i = 0; i < w.length; i++, res = res.sqr()) | ||
if (w[i] !== 0) | ||
break; | ||
if (++i < w.length) { | ||
for (var q = res.sqr(); i < w.length; i++, q = q.sqr()) { | ||
if (w[i] === 0) | ||
continue; | ||
res = res.mul(q); | ||
} | ||
} | ||
return res; | ||
}; | ||
// Shift-left in-place | ||
@@ -2303,14 +2339,6 @@ BN.prototype.iushln = function iushln(bits) { | ||
Red.prototype.pow = function pow(a, num) { | ||
var w = []; | ||
if (num.cmpn(0) === 0) | ||
var w = toBitArray(num); | ||
if (w.length === 0) | ||
return new BN(1); | ||
var q = num.clone(); | ||
while (q.cmpn(0) !== 0) { | ||
w.push(q.andln(1)); | ||
q.iushrn(1); | ||
} | ||
// Skip leading zeroes | ||
@@ -2317,0 +2345,0 @@ var res = a; |
{ | ||
"name": "bn.js", | ||
"version": "3.2.0", | ||
"version": "3.3.0", | ||
"description": "Big number implementation in pure javascript", | ||
@@ -5,0 +5,0 @@ "main": "lib/bn.js", |
@@ -77,2 +77,3 @@ # <img src="./logo.png" alt="bn.js" width="160" height="160" /> | ||
* `a.sqr()` - square (`i`) | ||
* `a.pow(b)` - raise `a` to the power of `b` | ||
* `a.div(b)` - divide (`divn`, `idivn`) | ||
@@ -79,0 +80,0 @@ * `a.mod(b)` - reduct (`u`, `n`) |
@@ -229,2 +229,12 @@ var assert = require('assert'); | ||
describe('.pow()', function() { | ||
it('should raise number to the power', function() { | ||
var a = new BN('ab', 16); | ||
var b = new BN('13', 10); | ||
var c = a.pow(b); | ||
assert.equal(c.toString(16), '15963da06977df51909c9ba5b'); | ||
}); | ||
}); | ||
describe('.div()', function() { | ||
@@ -231,0 +241,0 @@ it('should divide numbers', function() { |
@@ -43,2 +43,6 @@ var assert = require('assert'); | ||
.cmp(a.sqr().sqr().sqr().sqr().mul(a)), 0); | ||
assert.equal( | ||
a.toRed(m).redPow(new BN('deadbeefabbadead', 16)).fromRed() | ||
.toString(16), | ||
'3aa0e7e304e320b68ef61592bcb00341866d6fa66e11a4d6'); | ||
}); | ||
@@ -45,0 +49,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
120745
3251
202