Comparing version 4.6.1 to 4.6.2
{ | ||
"name": "bn.js", | ||
"version": "4.6.1", | ||
"version": "4.6.2", | ||
"description": "Big number implementation in pure javascript", | ||
"main": "lib/bn.js", | ||
"scripts": { | ||
"lint": "jscs lib/*.js test/*.js && jshint lib/*.js", | ||
"test": "mocha --reporter=spec test/*-test.js" | ||
"lint": "semistandard", | ||
"unit": "mocha --reporter=spec test/*-test.js", | ||
"test": "npm run lint && npm run unit" | ||
}, | ||
@@ -29,6 +30,5 @@ "repository": { | ||
"istanbul": "^0.3.5", | ||
"jscs": "^1.11.1", | ||
"jshint": "^2.6.0", | ||
"mocha": "^2.1.0" | ||
"mocha": "^2.1.0", | ||
"semistandard": "^7.0.4" | ||
} | ||
} |
@@ -76,9 +76,9 @@ # <img src="./logo.png" alt="bn.js" width="160" height="160" /> | ||
* `a.abs()` - absolute value (`i`) | ||
* `a.add(b)` - addition (`i`, `n`) | ||
* `a.sub(b)` - subtraction (`i`, `n`) | ||
* `a.mul(b)` - multiply (`i`, `n`) | ||
* `a.add(b)` - addition (`i`, `n`, `in`) | ||
* `a.sub(b)` - subtraction (`i`, `n`, `in`) | ||
* `a.mul(b)` - multiply (`i`, `n`, `in`) | ||
* `a.sqr()` - square (`i`) | ||
* `a.pow(b)` - raise `a` to the power of `b` | ||
* `a.div(b)` - divide (`divn`, `idivn`) | ||
* `a.mod(b)` - reduct (`u`, `n`) | ||
* `a.mod(b)` - reduct (`u`, `n`) (but no `umodn`) | ||
* `a.divRound(b)` - rounded division | ||
@@ -88,9 +88,9 @@ | ||
* `a.or(b)` - or (`i`, `u`) | ||
* `a.and(b)` - and (`i`, `u`, `andln`) (NOTE: `andln` is going to be replaced | ||
* `a.or(b)` - or (`i`, `u`, `iu`) | ||
* `a.and(b)` - and (`i`, `u`, `iu`, `andln`) (NOTE: `andln` is going to be replaced | ||
with `andn` in future) | ||
* `a.xor(b)` - xor (`i`, `u`) | ||
* `a.xor(b)` - xor (`i`, `u`, `iu`) | ||
* `a.setn(b)` - set specified bit to `1` | ||
* `a.shln(b)` - shift left (`i`, `u`) | ||
* `a.shrn(b)` - shift right (`i`, `u`) | ||
* `a.shln(b)` - shift left (`i`, `u`, `iu`) | ||
* `a.shrn(b)` - shift right (`i`, `u`, `iu`) | ||
* `a.testn(b)` - test if specified bit is set | ||
@@ -97,0 +97,0 @@ * `a.maskn(b)` - clear bits with indexes higher or equal to `b` (`i`) |
@@ -0,1 +1,3 @@ | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
@@ -5,22 +7,28 @@ var BN = require('../').BN; | ||
describe('BN.js/Arithmetic', function() { | ||
describe('.add()', function() { | ||
it('should add numbers', function() { | ||
describe('BN.js/Arithmetic', function () { | ||
describe('.add()', function () { | ||
it('should add numbers', function () { | ||
assert.equal(new BN(14).add(new BN(26)).toString(16), '28'); | ||
var k = new BN(0x1234); | ||
var r = k; | ||
for (var i = 0; i < 257; i++) | ||
for (var i = 0; i < 257; i++) { | ||
r = r.add(k); | ||
} | ||
assert.equal(r.toString(16), '125868'); | ||
}); | ||
it('should handle carry properly (in-place)', function() { | ||
it('should handle carry properly (in-place)', function () { | ||
var k = new BN('abcdefabcdefabcdef', 16); | ||
var r = new BN('deadbeef', 16); | ||
for (var i = 0; i < 257; i++) | ||
for (var i = 0; i < 257; i++) { | ||
r.iadd(k); | ||
} | ||
assert.equal(r.toString(16), 'ac79bd9b79be7a277bde'); | ||
}); | ||
it('should properly do positive + negative', function() { | ||
it('should properly do positive + negative', function () { | ||
var a = new BN('abcd', 16); | ||
@@ -31,4 +39,4 @@ var b = new BN('-abce', 16); | ||
var a = new BN('abcd', 16); | ||
var b = new BN('-abce', 16); | ||
a = new BN('abcd', 16); | ||
b = new BN('-abce', 16); | ||
@@ -40,4 +48,4 @@ assert.equal(a.add(b).toString(16), '-1'); | ||
describe('.iaddn()', function() { | ||
it('should allow a sign change', function() { | ||
describe('.iaddn()', function () { | ||
it('should allow a sign change', function () { | ||
var a = new BN(-100); | ||
@@ -52,3 +60,3 @@ assert.equal(a.negative, 1); | ||
it('should add negative number', function() { | ||
it('should add negative number', function () { | ||
var a = new BN(-100); | ||
@@ -62,3 +70,3 @@ assert.equal(a.negative, 1); | ||
it('should allow neg + pos with big number', function() { | ||
it('should allow neg + pos with big number', function () { | ||
var a = new BN('-1000000000', 10); | ||
@@ -72,3 +80,3 @@ assert.equal(a.negative, 1); | ||
it('should carry limb', function() { | ||
it('should carry limb', function () { | ||
var a = new BN('3ffffff', 16); | ||
@@ -80,4 +88,4 @@ | ||
describe('.sub()', function() { | ||
it('should subtract small numbers', function() { | ||
describe('.sub()', function () { | ||
it('should subtract small numbers', function () { | ||
assert.equal(new BN(26).sub(new BN(14)).toString(16), 'c'); | ||
@@ -91,3 +99,3 @@ assert.equal(new BN(14).sub(new BN(26)).toString(16), '-c'); | ||
'31ff3c61db2db84b9823d320907a573f6ad37c437abe458b1802cda041d6384' + | ||
'a7d8daef41395491e2', | ||
'a7d8daef41395491e2', | ||
16); | ||
@@ -99,14 +107,14 @@ var b = new BN( | ||
'31ff3c61db2db84b9823d3208989726578fd75276287cd9516533a9acfb9a67' + | ||
'76281f34583ddb91e2', | ||
'76281f34583ddb91e2', | ||
16); | ||
it('should subtract big numbers', function() { | ||
it('should subtract big numbers', function () { | ||
assert.equal(a.sub(b).cmp(r), 0); | ||
}); | ||
it('should subtract numbers in place', function() { | ||
it('should subtract numbers in place', function () { | ||
assert.equal(b.clone().isub(a).neg().cmp(r), 0); | ||
}); | ||
it('should subtract with carry', function() { | ||
it('should subtract with carry', function () { | ||
// Carry and copy | ||
@@ -117,4 +125,4 @@ var a = new BN('12345', 16); | ||
var a = new BN('12345', 16); | ||
var b = new BN('1000000000000', 16); | ||
a = new BN('12345', 16); | ||
b = new BN('1000000000000', 16); | ||
assert.equal(b.isub(a).toString(16), 'fffffffedcbb'); | ||
@@ -124,4 +132,4 @@ }); | ||
describe('.isubn()', function() { | ||
it('should subtract negative number', function() { | ||
describe('.isubn()', function () { | ||
it('should subtract negative number', function () { | ||
var r = new BN( | ||
@@ -133,3 +141,3 @@ '7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b', 16); | ||
it('should work for positive numbers', function() { | ||
it('should work for positive numbers', function () { | ||
var a = new BN(-100); | ||
@@ -143,3 +151,3 @@ assert.equal(a.negative, 1); | ||
it('should not allow a sign change', function() { | ||
it('should not allow a sign change', function () { | ||
var a = new BN(-100); | ||
@@ -153,9 +161,9 @@ assert.equal(a.negative, 1); | ||
it('should change sign on small numbers at 0', function() { | ||
var a = new BN(0).subn(2) | ||
it('should change sign on small numbers at 0', function () { | ||
var a = new BN(0).subn(2); | ||
assert.equal(a.toString(), '-2'); | ||
}); | ||
it('should change sign on small numbers at 1', function() { | ||
var a = new BN(1).subn(2) | ||
it('should change sign on small numbers at 1', function () { | ||
var a = new BN(1).subn(2); | ||
assert.equal(a.toString(), '-1'); | ||
@@ -165,4 +173,4 @@ }); | ||
function testMethod(name, mul) { | ||
describe(name, function() { | ||
function testMethod (name, mul) { | ||
describe(name, function () { | ||
it('should multiply numbers of different signs', function () { | ||
@@ -180,6 +188,8 @@ assert.equal(mul(new BN(0x1001), new BN(0x1234)).toString(16), | ||
var r = n; | ||
for (var i = 0; i < 4; i++) | ||
for (var i = 0; i < 4; i++) { | ||
r = mul(r, n); | ||
assert.equal(r.toString(16), | ||
'100500a00a005001'); | ||
} | ||
assert.equal(r.toString(16), '100500a00a005001'); | ||
}); | ||
@@ -208,3 +218,3 @@ | ||
mul(new BN('-100000000000'), new BN('3').div(new BN('4'))) | ||
.toString(16), | ||
.toString(16), | ||
'0' | ||
@@ -218,3 +228,3 @@ ); | ||
var q = new BN(q, 16); | ||
q = new BN(q, 16); | ||
assert.equal(mul(q, q).toString(16), qs); | ||
@@ -233,4 +243,4 @@ }); | ||
describe('.imul()', function() { | ||
it('should multiply numbers in-place', function() { | ||
describe('.imul()', function () { | ||
it('should multiply numbers in-place', function () { | ||
var a = new BN('abcdef01234567890abcd', 16); | ||
@@ -242,5 +252,5 @@ var b = new BN('deadbeefa551edebabba8', 16); | ||
var a = new BN('abcdef01234567890abcd214a25123f512361e6d236', 16); | ||
var b = new BN('deadbeefa551edebabba8121234fd21bac0341324dd', 16); | ||
var c = a.mul(b); | ||
a = new BN('abcdef01234567890abcd214a25123f512361e6d236', 16); | ||
b = new BN('deadbeefa551edebabba8121234fd21bac0341324dd', 16); | ||
c = a.mul(b); | ||
@@ -250,3 +260,3 @@ assert.equal(a.imul(b).toString(16), c.toString(16)); | ||
it('should multiply by 0', function() { | ||
it('should multiply by 0', function () { | ||
var a = new BN('abcdef01234567890abcd', 16); | ||
@@ -263,5 +273,4 @@ var b = new BN('0', 16); | ||
var q = new BN(q, 16); | ||
q = new BN(q, 16); | ||
assert.equal(q.isqr().toString(16), qs); | ||
@@ -271,4 +280,4 @@ }); | ||
describe('.muln()', function() { | ||
it('should multiply number by small number', function() { | ||
describe('.muln()', function () { | ||
it('should multiply number by small number', function () { | ||
var a = new BN('abcdef01234567890abcd', 16); | ||
@@ -282,4 +291,4 @@ var b = new BN('dead', 16); | ||
describe('.pow()', function() { | ||
it('should raise number to the power', function() { | ||
describe('.pow()', function () { | ||
it('should raise number to the power', function () { | ||
var a = new BN('ab', 16); | ||
@@ -293,15 +302,15 @@ var b = new BN('13', 10); | ||
describe('.div()', function() { | ||
it('should divide numbers', function() { | ||
describe('.div()', function () { | ||
it('should divide numbers', function () { | ||
assert.equal(new BN('10').div(new BN(256)).toString(16), | ||
'0'); | ||
'0'); | ||
assert.equal(new BN('69527932928').div(new BN('16974594')).toString(16), | ||
'fff'); | ||
'fff'); | ||
assert.equal(new BN('-69527932928').div(new BN('16974594')).toString(16), | ||
'-fff'); | ||
'-fff'); | ||
var b = new BN( | ||
'39e58a8055b6fb264b75ec8c646509784204ac15a8c24e05babc9729ab9' + | ||
'b055c3a9458e4ce3289560a38e08ba8175a9446ce14e608245ab3a9' + | ||
'978a8bd8acaa40', | ||
'b055c3a9458e4ce3289560a38e08ba8175a9446ce14e608245ab3a9' + | ||
'978a8bd8acaa40', | ||
16); | ||
@@ -317,3 +326,3 @@ var n = new BN( | ||
it('should not fail on regression after moving to _wordDiv', function() { | ||
it('should not fail on regression after moving to _wordDiv', function () { | ||
// Regression after moving to word div | ||
@@ -328,9 +337,9 @@ var p = new BN( | ||
assert.equal( | ||
as.div(p).toString(16), | ||
'39e58a8055b6fb264b75ec8c646509784204ac15a8c24e05babc9729e58090b9'); | ||
as.div(p).toString(16), | ||
'39e58a8055b6fb264b75ec8c646509784204ac15a8c24e05babc9729e58090b9'); | ||
var p = new BN( | ||
p = new BN( | ||
'ffffffff00000001000000000000000000000000ffffffffffffffffffffffff', | ||
16); | ||
var a = new BN( | ||
a = new BN( | ||
'fffffffe00000003fffffffd0000000200000001fffffffe00000002ffffffff' + | ||
@@ -345,38 +354,38 @@ 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', | ||
describe('.idivn()', function() { | ||
it('should divide numbers in-place', function() { | ||
describe('.idivn()', function () { | ||
it('should divide numbers in-place', function () { | ||
assert.equal(new BN('10', 16).idivn(3).toString(16), '5'); | ||
assert.equal(new BN('12', 16).idivn(3).toString(16), '6'); | ||
assert.equal(new BN('10000000000000000').idivn(3).toString(10), | ||
'3333333333333333'); | ||
'3333333333333333'); | ||
assert.equal( | ||
new BN('100000000000000000000000000000').idivn(3).toString(10), | ||
'33333333333333333333333333333'); | ||
new BN('100000000000000000000000000000').idivn(3).toString(10), | ||
'33333333333333333333333333333'); | ||
var t = new BN(3); | ||
assert.equal( | ||
new BN('12345678901234567890123456', 16).idivn(3).toString(16), | ||
new BN('12345678901234567890123456', 16).div(t).toString(16)); | ||
new BN('12345678901234567890123456', 16).idivn(3).toString(16), | ||
new BN('12345678901234567890123456', 16).div(t).toString(16)); | ||
}); | ||
}); | ||
describe('.divRound()', function() { | ||
it('should divide numbers with rounding', function() { | ||
describe('.divRound()', function () { | ||
it('should divide numbers with rounding', function () { | ||
assert.equal(new BN(9).divRound(new BN(20)).toString(10), | ||
'0'); | ||
'0'); | ||
assert.equal(new BN(10).divRound(new BN(20)).toString(10), | ||
'1'); | ||
'1'); | ||
assert.equal(new BN(150).divRound(new BN(20)).toString(10), | ||
'8'); | ||
'8'); | ||
assert.equal(new BN(149).divRound(new BN(20)).toString(10), | ||
'7'); | ||
'7'); | ||
assert.equal(new BN(149).divRound(new BN(17)).toString(10), | ||
'9'); | ||
'9'); | ||
assert.equal(new BN(144).divRound(new BN(17)).toString(10), | ||
'8'); | ||
'8'); | ||
assert.equal(new BN(-144).divRound(new BN(17)).toString(10), | ||
'-8'); | ||
'-8'); | ||
}); | ||
it('should return 1 on exact division', function() { | ||
it('should return 1 on exact division', function () { | ||
assert.equal(new BN(144).divRound(new BN(144)).toString(10), '1'); | ||
@@ -386,8 +395,8 @@ }); | ||
describe('.mod()', function() { | ||
it('should mod numbers', function() { | ||
describe('.mod()', function () { | ||
it('should mod numbers', function () { | ||
assert.equal(new BN('10').mod(new BN(256)).toString(16), | ||
'a'); | ||
'a'); | ||
assert.equal(new BN('69527932928').mod(new BN('16974594')).toString(16), | ||
'102f302'); | ||
'102f302'); | ||
@@ -427,3 +436,3 @@ // -178 = 10 * (-17) + (-8) | ||
it('should properly carry the sign inside division', function() { | ||
it('should properly carry the sign inside division', function () { | ||
var a = new BN('945304eb96065b2a98b57a48a06ae28d285a71b5', 'hex'); | ||
@@ -438,4 +447,4 @@ var b = new BN( | ||
describe('.modn()', function() { | ||
it('should act like .mod() on small numbers', function() { | ||
describe('.modn()', function () { | ||
it('should act like .mod() on small numbers', function () { | ||
assert.equal(new BN('10', 16).modn(256).toString(16), '10'); | ||
@@ -446,18 +455,18 @@ assert.equal(new BN('100', 16).modn(256).toString(16), '0'); | ||
assert.equal(new BN('100000000001', 16).modn(257).toString(16), | ||
new BN('100000000001', 16).mod(new BN(257)).toString(16)); | ||
new BN('100000000001', 16).mod(new BN(257)).toString(16)); | ||
assert.equal(new BN('123456789012', 16).modn(3).toString(16), | ||
new BN('123456789012', 16).mod(new BN(3)).toString(16)); | ||
new BN('123456789012', 16).mod(new BN(3)).toString(16)); | ||
}); | ||
}); | ||
describe('.abs()', function() { | ||
it('should return absolute value', function() { | ||
describe('.abs()', function () { | ||
it('should return absolute value', function () { | ||
assert.equal(new BN(0x1001).abs().toString(), '4097'); | ||
assert.equal(new BN(-0x1001).abs().toString(), '4097'); | ||
assert.equal(new BN('ffffffff', 16).abs().toString(), '4294967295'); | ||
}) | ||
}); | ||
}); | ||
describe('.invm()', function() { | ||
it('should invert relatively-prime numbers', function() { | ||
describe('.invm()', function () { | ||
it('should invert relatively-prime numbers', function () { | ||
var p = new BN(257); | ||
@@ -469,6 +478,6 @@ var a = new BN(3); | ||
var p192 = new BN( | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
var a = new BN('deadbeef', 16); | ||
var b = a.invm(p192); | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
a = new BN('deadbeef', 16); | ||
b = a.invm(p192); | ||
assert.equal(a.mul(b).mod(p192).toString(16), '1'); | ||
@@ -483,4 +492,4 @@ | ||
// Even base (take #2) | ||
var a = new BN('5'); | ||
var b = new BN('6'); | ||
a = new BN('5'); | ||
b = new BN('6'); | ||
var r = a.invm(b); | ||
@@ -491,4 +500,4 @@ assert.equal(r.mul(a).mod(b).toString(16), '1'); | ||
describe('.gcd()', function() { | ||
it('should return GCD', function() { | ||
describe('.gcd()', function () { | ||
it('should return GCD', function () { | ||
assert.equal(new BN(3).gcd(new BN(2)).toString(16), '1'); | ||
@@ -500,4 +509,4 @@ assert.equal(new BN(18).gcd(new BN(12)).toString(16), '6'); | ||
describe('.egcd()', function() { | ||
it('should return EGCD', function() { | ||
describe('.egcd()', function () { | ||
it('should return EGCD', function () { | ||
assert.equal(new BN(3).egcd(new BN(2)).gcd.toString(16), '1'); | ||
@@ -509,4 +518,4 @@ assert.equal(new BN(18).egcd(new BN(12)).gcd.toString(16), '6'); | ||
describe('BN.max(a, b)', function() { | ||
it('should return maximum', function() { | ||
describe('BN.max(a, b)', function () { | ||
it('should return maximum', function () { | ||
assert.equal(BN.max(new BN(3), new BN(2)).toString(16), '3'); | ||
@@ -518,4 +527,4 @@ assert.equal(BN.max(new BN(2), new BN(3)).toString(16), '3'); | ||
describe('BN.min(a, b)', function() { | ||
it('should return minimum', function() { | ||
describe('BN.min(a, b)', function () { | ||
it('should return minimum', function () { | ||
assert.equal(BN.min(new BN(3), new BN(2)).toString(16), '2'); | ||
@@ -522,0 +531,0 @@ assert.equal(BN.min(new BN(2), new BN(3)).toString(16), '2'); |
@@ -0,50 +1,51 @@ | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
var BN = require('../').BN; | ||
var fixtures = require('./fixtures'); | ||
describe('BN.js/Binary', function() { | ||
describe('.shl()', function() { | ||
it('should shl numbers', function() { | ||
describe('BN.js/Binary', function () { | ||
describe('.shl()', function () { | ||
it('should shl numbers', function () { | ||
// TODO(indutny): add negative numbers when the time will come | ||
assert.equal(new BN('69527932928').shln(13).toString(16), | ||
'2060602000000'); | ||
'2060602000000'); | ||
assert.equal(new BN('69527932928').shln(45).toString(16), | ||
'206060200000000000000'); | ||
'206060200000000000000'); | ||
}); | ||
it('should ushl numbers', function() { | ||
it('should ushl numbers', function () { | ||
assert.equal(new BN('69527932928').ushln(13).toString(16), | ||
'2060602000000'); | ||
'2060602000000'); | ||
assert.equal(new BN('69527932928').ushln(45).toString(16), | ||
'206060200000000000000'); | ||
'206060200000000000000'); | ||
}); | ||
}); | ||
describe('.shr()', function() { | ||
it('should shr numbers', function() { | ||
describe('.shr()', function () { | ||
it('should shr numbers', function () { | ||
// TODO(indutny): add negative numbers when the time will come | ||
assert.equal(new BN('69527932928').shrn(13).toString(16), | ||
'818180'); | ||
'818180'); | ||
assert.equal(new BN('69527932928').shrn(17).toString(16), | ||
'81818'); | ||
'81818'); | ||
assert.equal(new BN('69527932928').shrn(256).toString(16), | ||
'0'); | ||
'0'); | ||
}); | ||
it('should ushr numbers', function() { | ||
it('should ushr numbers', function () { | ||
assert.equal(new BN('69527932928').ushrn(13).toString(16), | ||
'818180'); | ||
'818180'); | ||
assert.equal(new BN('69527932928').ushrn(17).toString(16), | ||
'81818'); | ||
'81818'); | ||
assert.equal(new BN('69527932928').ushrn(256).toString(16), | ||
'0'); | ||
'0'); | ||
}); | ||
}); | ||
describe('.bincn()', function() { | ||
it('should increment bit', function() { | ||
describe('.bincn()', function () { | ||
it('should increment bit', 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)); | ||
new BN(2).bincn(2).toString(16)); | ||
assert.equal(new BN(0xffffff).bincn(1).toString(16), '1000001'); | ||
@@ -54,4 +55,4 @@ }); | ||
describe('.imaskn()', function() { | ||
it('should mask bits in-place', function() { | ||
describe('.imaskn()', function () { | ||
it('should mask bits in-place', function () { | ||
assert.equal(new BN(0).imaskn(1).toString(16), '0'); | ||
@@ -65,10 +66,10 @@ assert.equal(new BN(3).imaskn(1).toString(16), '1'); | ||
describe('.testn()', function() { | ||
it('should support test specific bit', function() { | ||
describe('.testn()', function () { | ||
it('should support test specific bit', function () { | ||
[ | ||
'ff', | ||
'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' | ||
].forEach(function(hex) { | ||
var bn = new BN(hex, 16) | ||
var bl = bn.bitLength() | ||
].forEach(function (hex) { | ||
var bn = new BN(hex, 16); | ||
var bl = bn.bitLength(); | ||
@@ -81,21 +82,21 @@ for (var i = 0; i < bl; ++i) { | ||
assert.equal(bn.testn(bl), false); | ||
}) | ||
}); | ||
var xbits = '01111001010111001001000100011101' + | ||
'11010011101100011000111001011101' + | ||
'10010100111000000001011000111101' + | ||
'01011111001111100100011110000010' + | ||
'01011010100111010001010011000100' + | ||
'01101001011110100001001111100110' + | ||
'001110010111'; | ||
'11010011101100011000111001011101' + | ||
'10010100111000000001011000111101' + | ||
'01011111001111100100011110000010' + | ||
'01011010100111010001010011000100' + | ||
'01101001011110100001001111100110' + | ||
'001110010111'; | ||
var x = new BN( | ||
'23478905234580795234378912401239784125643978256123048348957342' | ||
) | ||
); | ||
for (var i = 0; i < x.bitLength(); ++i) { | ||
assert.equal(x.testn(i), (xbits.charAt(i) === '1'), 'Failed @ bit ' + i) | ||
assert.equal(x.testn(i), (xbits.charAt(i) === '1'), 'Failed @ bit ' + i); | ||
} | ||
}); | ||
it('should have short-cuts', function() { | ||
it('should have short-cuts', function () { | ||
var x = new BN('abcd', 16); | ||
@@ -106,96 +107,96 @@ assert(!x.testn(128)); | ||
describe('.and()', function() { | ||
it('should and numbers', function() { | ||
describe('.and()', function () { | ||
it('should and numbers', function () { | ||
assert.equal(new BN('1010101010101010101010101010101010101010', 2) | ||
.and(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '0'); | ||
.and(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '0'); | ||
}); | ||
it('should and numbers of different limb-length', function() { | ||
it('should and numbers of different limb-length', function () { | ||
assert.equal( | ||
new BN('abcd0000ffff', 16) | ||
.and(new BN('abcd', 16)).toString(16), | ||
'abcd'); | ||
new BN('abcd0000ffff', 16) | ||
.and(new BN('abcd', 16)).toString(16), | ||
'abcd'); | ||
}); | ||
}); | ||
describe('.iand()', function() { | ||
it('should iand numbers', function() { | ||
describe('.iand()', function () { | ||
it('should iand numbers', function () { | ||
assert.equal(new BN('1010101010101010101010101010101010101010', 2) | ||
.iand(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '0'); | ||
.iand(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '0'); | ||
assert.equal(new BN('1000000000000000000000000000000000000001', 2) | ||
.iand(new BN('1', 2)) | ||
.toString(2), '1') | ||
.iand(new BN('1', 2)) | ||
.toString(2), '1'); | ||
assert.equal(new BN('1', 2) | ||
.iand(new BN('1000000000000000000000000000000000000001', 2)) | ||
.toString(2), '1') | ||
.iand(new BN('1000000000000000000000000000000000000001', 2)) | ||
.toString(2), '1'); | ||
}); | ||
}); | ||
describe('.or()', function() { | ||
describe('.or()', function () { | ||
it('should or numbers', function () { | ||
assert.equal(new BN('1010101010101010101010101010101010101010', 2) | ||
.or(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '1111111111111111111111111111111111111111'); | ||
.or(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '1111111111111111111111111111111111111111'); | ||
}); | ||
it('should or numbers of different limb-length', function() { | ||
it('should or numbers of different limb-length', function () { | ||
assert.equal( | ||
new BN('abcd00000000', 16) | ||
.or(new BN('abcd', 16)).toString(16), | ||
'abcd0000abcd'); | ||
new BN('abcd00000000', 16) | ||
.or(new BN('abcd', 16)).toString(16), | ||
'abcd0000abcd'); | ||
}); | ||
}); | ||
describe('.ior()', function() { | ||
describe('.ior()', function () { | ||
it('should ior numbers', function () { | ||
assert.equal(new BN('1010101010101010101010101010101010101010', 2) | ||
.ior(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '1111111111111111111111111111111111111111'); | ||
.ior(new BN('101010101010101010101010101010101010101', 2)) | ||
.toString(2), '1111111111111111111111111111111111111111'); | ||
assert.equal(new BN('1000000000000000000000000000000000000000', 2) | ||
.ior(new BN('1', 2)) | ||
.toString(2), '1000000000000000000000000000000000000001'); | ||
.ior(new BN('1', 2)) | ||
.toString(2), '1000000000000000000000000000000000000001'); | ||
assert.equal(new BN('1', 2) | ||
.ior(new BN('1000000000000000000000000000000000000000', 2)) | ||
.toString(2), '1000000000000000000000000000000000000001'); | ||
.ior(new BN('1000000000000000000000000000000000000000', 2)) | ||
.toString(2), '1000000000000000000000000000000000000001'); | ||
}); | ||
}); | ||
describe('.xor()', function() { | ||
describe('.xor()', function () { | ||
it('should xor numbers', function () { | ||
assert.equal(new BN('11001100110011001100110011001100', 2) | ||
.xor(new BN('1100110011001100110011001100110', 2)) | ||
.toString(2), '10101010101010101010101010101010'); | ||
.xor(new BN('1100110011001100110011001100110', 2)) | ||
.toString(2), '10101010101010101010101010101010'); | ||
}); | ||
}); | ||
describe('.ixor()', function() { | ||
describe('.ixor()', function () { | ||
it('should ixor numbers', function () { | ||
assert.equal(new BN('11001100110011001100110011001100', 2) | ||
.ixor(new BN('1100110011001100110011001100110', 2)) | ||
.toString(2), '10101010101010101010101010101010'); | ||
.ixor(new BN('1100110011001100110011001100110', 2)) | ||
.toString(2), '10101010101010101010101010101010'); | ||
assert.equal(new BN('11001100110011001100110011001100', 2) | ||
.ixor(new BN('1', 2)) | ||
.toString(2), '11001100110011001100110011001101'); | ||
.ixor(new BN('1', 2)) | ||
.toString(2), '11001100110011001100110011001101'); | ||
assert.equal(new BN('1', 2) | ||
.ixor(new BN('11001100110011001100110011001100', 2)) | ||
.toString(2), '11001100110011001100110011001101'); | ||
.ixor(new BN('11001100110011001100110011001100', 2)) | ||
.toString(2), '11001100110011001100110011001101'); | ||
}); | ||
it('should and numbers of different limb-length', function() { | ||
it('should and numbers of different limb-length', function () { | ||
assert.equal( | ||
new BN('abcd0000ffff', 16) | ||
.xor(new BN('abcd', 16)).toString(16), | ||
'abcd00005432'); | ||
new BN('abcd0000ffff', 16) | ||
.xor(new BN('abcd', 16)).toString(16), | ||
'abcd00005432'); | ||
}); | ||
}); | ||
describe('.setn()', function() { | ||
describe('.setn()', function () { | ||
it('should allow single bits to be set', function () { | ||
assert.equal(new BN(0).setn(2, true).toString(2), '100'); | ||
assert.equal(new BN(0).setn(27, true).toString(2), | ||
'1000000000000000000000000000'); | ||
'1000000000000000000000000000'); | ||
assert.equal(new BN('1000000000000000000000000001', 2).setn(27, false) | ||
.toString(2), '1'); | ||
.toString(2), '1'); | ||
assert.equal(new BN('101', 2).setn(2, false).toString(2), '1'); | ||
@@ -205,25 +206,24 @@ }); | ||
describe('.notn()', function() { | ||
describe('.notn()', function () { | ||
it('should allow bitwise negation', function () { | ||
assert.equal(new BN('111000111', 2).notn(9).toString(2), | ||
'111000'); | ||
'111000'); | ||
assert.equal(new BN('000111000', 2).notn(9).toString(2), | ||
'111000111'); | ||
'111000111'); | ||
assert.equal(new BN('111000111', 2).notn(9).toString(2), | ||
'111000'); | ||
'111000'); | ||
assert.equal(new BN('000111000', 2).notn(9).toString(2), | ||
'111000111'); | ||
'111000111'); | ||
assert.equal(new BN('111000111', 2).notn(32).toString(2), | ||
'11111111111111111111111000111000'); | ||
'11111111111111111111111000111000'); | ||
assert.equal(new BN('000111000', 2).notn(32).toString(2), | ||
'11111111111111111111111111000111'); | ||
'11111111111111111111111111000111'); | ||
assert.equal(new BN('111000111', 2).notn(68).toString(2), | ||
'11111111111111111111111111111111' + | ||
'111111111111111111111111111000111000'); | ||
'11111111111111111111111111111111' + | ||
'111111111111111111111111111000111000'); | ||
assert.equal(new BN('000111000', 2).notn(68).toString(2), | ||
'11111111111111111111111111111111' + | ||
'111111111111111111111111111111000111'); | ||
'11111111111111111111111111111111' + | ||
'111111111111111111111111111111000111'); | ||
}); | ||
}); | ||
}); |
@@ -0,16 +1,17 @@ | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
var BN = require('../').BN; | ||
var fixtures = require('./fixtures'); | ||
describe('BN.js/Constructor', function() { | ||
describe('with Smi input', function() { | ||
it('should accept one limb number', function() { | ||
describe('BN.js/Constructor', function () { | ||
describe('with Smi input', function () { | ||
it('should accept one limb number', function () { | ||
assert.equal(new BN(12345).toString(16), '3039'); | ||
}); | ||
it('should accept two-limb number', function() { | ||
it('should accept two-limb number', function () { | ||
assert.equal(new BN(0x4123456).toString(16), '4123456'); | ||
}); | ||
it('should accept 52 bits of precision', function() { | ||
it('should accept 52 bits of precision', function () { | ||
var num = Math.pow(2, 52); | ||
@@ -20,3 +21,3 @@ assert.equal(new BN(num, 10).toString(10), num.toString(10)); | ||
it('should accept max safe integer', function() { | ||
it('should accept max safe integer', function () { | ||
var num = Math.pow(2, 53) - 1; | ||
@@ -26,8 +27,11 @@ assert.equal(new BN(num, 10).toString(10), num.toString(10)); | ||
it('should not accept an unsafe integer', function() { | ||
it('should not accept an unsafe integer', function () { | ||
var num = Math.pow(2, 53); | ||
assert.throws(function() { new BN(num, 10); }); | ||
assert.throws(function () { | ||
BN(num, 10); | ||
}); | ||
}); | ||
it('should accept two-limb LE number', function() { | ||
it('should accept two-limb LE number', function () { | ||
assert.equal(new BN(0x4123456, null, 'le').toString(16), '56341204'); | ||
@@ -37,4 +41,4 @@ }); | ||
describe('with String input', function() { | ||
it('should accept base-16', function() { | ||
describe('with String input', function () { | ||
it('should accept base-16', function () { | ||
assert.equal(new BN('1A6B765D8CDF', 16).toString(16), '1a6b765d8cdf'); | ||
@@ -44,7 +48,7 @@ assert.equal(new BN('1A6B765D8CDF', 16).toString(), '29048849665247'); | ||
it('should accept base-hex', function() { | ||
it('should accept base-hex', function () { | ||
assert.equal(new BN('FF', 'hex').toString(), '255'); | ||
}); | ||
it('should accept base-16 with spaces', function() { | ||
it('should accept base-16 with spaces', function () { | ||
var num = 'a89c e5af8724 c0a23e0e 0ff77500'; | ||
@@ -54,3 +58,3 @@ assert.equal(new BN(num, 16).toString(16), num.replace(/ /g, '')); | ||
it('should accept long base-16', function() { | ||
it('should accept long base-16', function () { | ||
var num = '123456789abcdef123456789abcdef123456789abcdef'; | ||
@@ -60,3 +64,3 @@ assert.equal(new BN(num, 16).toString(16), num); | ||
it('should accept positive base-10', function() { | ||
it('should accept positive base-10', function () { | ||
assert.equal(new BN('10654321').toString(), '10654321'); | ||
@@ -66,7 +70,7 @@ assert.equal(new BN('29048849665247').toString(16), '1a6b765d8cdf'); | ||
it('should accept negative base-10', function() { | ||
it('should accept negative base-10', function () { | ||
assert.equal(new BN('-29048849665247').toString(16), '-1a6b765d8cdf'); | ||
}); | ||
it('should accept long base-10', function() { | ||
it('should accept long base-10', function () { | ||
var num = '10000000000000000'; | ||
@@ -76,3 +80,3 @@ assert.equal(new BN(num).toString(10), num); | ||
it('should accept base-2', function() { | ||
it('should accept base-2', function () { | ||
var base2 = '11111111111111111111111111111111111111111111111111111'; | ||
@@ -82,3 +86,3 @@ assert.equal(new BN(base2, 2).toString(2), base2); | ||
it('should accept base-36', function() { | ||
it('should accept base-36', function () { | ||
var base36 = 'zzZzzzZzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz'; | ||
@@ -88,20 +92,20 @@ assert.equal(new BN(base36, 36).toString(36), base36.toLowerCase()); | ||
it('should not overflow limbs during base-10', function() { | ||
it('should not overflow limbs during base-10', function () { | ||
var num = '65820182292848241686198767302293' + | ||
'20890292528855852623664389292032'; | ||
'20890292528855852623664389292032'; | ||
assert(new BN(num).words[0] < 0x4000000); | ||
}); | ||
it('should accept base-16 LE integer', function() { | ||
it('should accept base-16 LE integer', function () { | ||
assert.equal(new BN('1A6B765D8CDF', 16, 'le').toString(16), | ||
'df8c5d766b1a'); | ||
'df8c5d766b1a'); | ||
}); | ||
}); | ||
describe('with Array input', function() { | ||
it('should not fail on empty array', function() { | ||
assert.equal(new BN([ ]).toString(16), '0'); | ||
describe('with Array input', function () { | ||
it('should not fail on empty array', function () { | ||
assert.equal(new BN([]).toString(16), '0'); | ||
}); | ||
it('should import/export big endian', function() { | ||
it('should import/export big endian', function () { | ||
assert.equal(new BN([ 1, 2, 3 ]).toString(16), '10203'); | ||
@@ -111,21 +115,21 @@ assert.equal(new BN([ 1, 2, 3, 4 ]).toString(16), '1020304'); | ||
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ]).toString(16), | ||
'102030405060708'); | ||
'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'); | ||
'1,2,3,4,5,6,7,8'); | ||
}); | ||
it('should import little endian', function() { | ||
it('should import little endian', function () { | ||
assert.equal(new BN([ 1, 2, 3 ], 10, 'le').toString(16), '30201'); | ||
assert.equal(new BN([ 1, 2, 3, 4 ], 10, 'le').toString(16), '4030201'); | ||
assert.equal(new BN([ 1, 2, 3, 4, 5 ], 10, 'le').toString(16), | ||
'504030201'); | ||
'504030201'); | ||
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ], 'le').toString(16), | ||
'807060504030201'); | ||
'807060504030201'); | ||
assert.equal(new BN([ 1, 2, 3, 4 ]).toArray('le').join(','), '4,3,2,1'); | ||
assert.equal(new BN([ 1, 2, 3, 4, 5, 6, 7, 8 ]).toArray('le').join(','), | ||
'8,7,6,5,4,3,2,1'); | ||
'8,7,6,5,4,3,2,1'); | ||
}); | ||
it('should import big endian with implicit base', function() { | ||
it('should import big endian with implicit base', function () { | ||
assert.equal(new BN([ 1, 2, 3, 4, 5 ], 'le').toString(16), '504030201'); | ||
@@ -135,4 +139,4 @@ }); | ||
describe('with BN input', function() { | ||
it('should clone BN', function() { | ||
describe('with BN input', function () { | ||
it('should clone BN', function () { | ||
var num = new BN(12345); | ||
@@ -139,0 +143,0 @@ assert.equal(new BN(num).toString(10), '12345'); |
@@ -167,3 +167,3 @@ exports.dhGroups = { | ||
'641440ccf248e8643b2bd1e1f9e8239356ab91098fcb431d', | ||
q: 'a899c59999bf877d96442d284359783bdc64b5f878b688fe' + | ||
q: 'a899c59999bf877d96442d284359783bdc64b5f878b688fe' + | ||
'51407f0526e616553ad0aaaac4d5bed3046f10a1faaf42bb' + | ||
@@ -170,0 +170,0 @@ '2342dc4b7908eea0c46e4c4576897675c2bfdc4467870d3d' + |
@@ -0,1 +1,3 @@ | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
@@ -5,6 +7,6 @@ var BN = require('../../').BN; | ||
describe('BN.js/Slow DH test', function() { | ||
describe('BN.js/Slow DH test', function () { | ||
var groups = fixtures.dhGroups; | ||
Object.keys(groups).forEach(function(name) { | ||
it('should match public key for ' + name + ' group', function() { | ||
Object.keys(groups).forEach(function (name) { | ||
it('should match public key for ' + name + ' group', function () { | ||
var group = groups[name]; | ||
@@ -11,0 +13,0 @@ |
@@ -0,9 +1,10 @@ | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
var BN = require('../').BN; | ||
var fixtures = require('./fixtures'); | ||
describe('BN.js/Reduction context', function() { | ||
function testMethod(name, fn) { | ||
describe(name + ' method', function() { | ||
it('should support add, iadd, sub, isub operations', function() { | ||
describe('BN.js/Reduction context', function () { | ||
function testMethod (name, fn) { | ||
describe(name + ' method', function () { | ||
it('should support add, iadd, sub, isub operations', function () { | ||
var p = new BN(257); | ||
@@ -23,6 +24,6 @@ var m = fn(p); | ||
it('should support pow and mul operations', function() { | ||
it('should support pow and mul operations', function () { | ||
var p192 = new BN( | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
var m = fn(p192); | ||
@@ -35,44 +36,48 @@ var a = new BN(123); | ||
assert.equal(a.toRed(m).redPow(new BN(3)).fromRed() | ||
.cmp(a.sqr().mul(a)), 0); | ||
.cmp(a.sqr().mul(a)), 0); | ||
assert.equal(a.toRed(m).redPow(new BN(4)).fromRed() | ||
.cmp(a.sqr().sqr()), 0); | ||
.cmp(a.sqr().sqr()), 0); | ||
assert.equal(a.toRed(m).redPow(new BN(8)).fromRed() | ||
.cmp(a.sqr().sqr().sqr()), 0); | ||
.cmp(a.sqr().sqr().sqr()), 0); | ||
assert.equal(a.toRed(m).redPow(new BN(9)).fromRed() | ||
.cmp(a.sqr().sqr().sqr().mul(a)), 0); | ||
.cmp(a.sqr().sqr().sqr().mul(a)), 0); | ||
assert.equal(a.toRed(m).redPow(new BN(17)).fromRed() | ||
.cmp(a.sqr().sqr().sqr().sqr().mul(a)), 0); | ||
.cmp(a.sqr().sqr().sqr().sqr().mul(a)), 0); | ||
assert.equal( | ||
a.toRed(m).redPow(new BN('deadbeefabbadead', 16)).fromRed() | ||
.toString(16), | ||
'3aa0e7e304e320b68ef61592bcb00341866d6fa66e11a4d6'); | ||
a.toRed(m).redPow(new BN('deadbeefabbadead', 16)).fromRed() | ||
.toString(16), | ||
'3aa0e7e304e320b68ef61592bcb00341866d6fa66e11a4d6'); | ||
}); | ||
it('should sqrtm numbers', function() { | ||
it('should sqrtm numbers', function () { | ||
var p = new BN(263); | ||
var m = fn(p); | ||
var q = new BN(11).toRed(m); | ||
var qr = q.redSqrt(true, p); | ||
assert.equal(qr.redSqr().cmp(q), 0); | ||
var qr = q.redSqrt(false, p); | ||
qr = q.redSqrt(false, p); | ||
assert.equal(qr.redSqr().cmp(q), 0); | ||
var p = new BN( | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
var m = fn(p); | ||
var q = new BN(13).toRed(m); | ||
var qr = q.redSqrt(true, p); | ||
p = new BN( | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
m = fn(p); | ||
q = new BN(13).toRed(m); | ||
qr = q.redSqrt(true, p); | ||
assert.equal(qr.redSqr().cmp(q), 0); | ||
var qr = q.redSqrt(false, p); | ||
qr = q.redSqrt(false, p); | ||
assert.equal(qr.redSqr().cmp(q), 0); | ||
// Tonelli-shanks | ||
var p = new BN(13); | ||
var m = fn(p); | ||
var q = new BN(10).toRed(m); | ||
p = new BN(13); | ||
m = fn(p); | ||
q = new BN(10).toRed(m); | ||
assert.equal(q.redSqrt().fromRed().toString(10), '7'); | ||
}); | ||
it('should invm numbers', function() { | ||
it('should invm numbers', function () { | ||
var p = new BN(257); | ||
@@ -85,19 +90,20 @@ var m = fn(p); | ||
it('should invm numbers (regression)', function() { | ||
it('should invm numbers (regression)', function () { | ||
var p = new BN( | ||
'ffffffff00000001000000000000000000000000ffffffffffffffffffffffff', | ||
16); | ||
'ffffffff00000001000000000000000000000000ffffffffffffffffffffffff', | ||
16); | ||
var a = new BN( | ||
'e1d969b8192fbac73ea5b7921896d6a2263d4d4077bb8e5055361d1f7f8163f3', | ||
16); | ||
'e1d969b8192fbac73ea5b7921896d6a2263d4d4077bb8e5055361d1f7f8163f3', | ||
16); | ||
var m = fn(p); | ||
var a = a.toRed(m); | ||
a = a.toRed(m); | ||
assert.equal(a.redInvm().fromRed().negative, 0); | ||
}); | ||
it('should imul numbers', function() { | ||
it('should imul numbers', function () { | ||
var p = new BN( | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
'fffffffffffffffffffffffffffffffeffffffffffffffff', | ||
16); | ||
var m = fn(p); | ||
@@ -110,7 +116,7 @@ | ||
assert.equal(a.toRed(m).redIMul(b.toRed(m)).fromRed().toString(16), | ||
c.toString(16)); | ||
c.toString(16)); | ||
}); | ||
it('should pow(base, 0) == 1', function() { | ||
var base = new BN(256).toRed( BN.red('k256')); | ||
it('should pow(base, 0) == 1', function () { | ||
var base = new BN(256).toRed(BN.red('k256')); | ||
var exponent = new BN(0); | ||
@@ -121,3 +127,3 @@ var result = base.redPow(exponent); | ||
it('should reduce when converting to red', function() { | ||
it('should reduce when converting to red', function () { | ||
var p = new BN(257); | ||
@@ -127,3 +133,3 @@ var m = fn(p); | ||
assert.doesNotThrow(function() { | ||
assert.doesNotThrow(function () { | ||
var b = a.redISub(new BN(512).toRed(m)); | ||
@@ -139,4 +145,4 @@ b.redISub(new BN(512).toRed(m)); | ||
describe('Pseudo-Mersenne Primes', function() { | ||
it('should reduce numbers mod k256', function() { | ||
describe('Pseudo-Mersenne Primes', function () { | ||
it('should reduce numbers mod k256', function () { | ||
var p = BN._prime('k256'); | ||
@@ -148,4 +154,4 @@ | ||
var num = new BN('fedcba9876543210fedcba9876543210dead' + | ||
'fedcba9876543210fedcba9876543210dead', | ||
16); | ||
'fedcba9876543210fedcba9876543210dead', | ||
16); | ||
var exp = num.mod(p.p).toString(16); | ||
@@ -155,11 +161,12 @@ assert.equal(p.ireduce(num).toString(16), exp); | ||
var regr = new BN('f7e46df64c1815962bf7bc9c56128798' + | ||
'3f4fcef9cb1979573163b477eab93959' + | ||
'335dfb29ef07a4d835d22aa3b6797760' + | ||
'70a8b8f59ba73d56d01a79af9', | ||
16); | ||
var exp = regr.mod(p.p).toString(16); | ||
'3f4fcef9cb1979573163b477eab93959' + | ||
'335dfb29ef07a4d835d22aa3b6797760' + | ||
'70a8b8f59ba73d56d01a79af9', | ||
16); | ||
exp = regr.mod(p.p).toString(16); | ||
assert.equal(p.ireduce(regr).toString(16), exp); | ||
}); | ||
it('should not fail to invm number mod k256', function() { | ||
it('should not fail to invm number mod k256', function () { | ||
var regr2 = new BN( | ||
@@ -171,3 +178,3 @@ '6c150c4aa9a8cf1934485d40674d4a7cd494675537bda36d49405c5d2c6f496f', 16); | ||
it('should correctly square the number', function() { | ||
it('should correctly square the number', function () { | ||
var p = BN._prime('k256').p; | ||
@@ -177,4 +184,4 @@ var red = BN.red('k256'); | ||
var n = new BN('9cd8cb48c3281596139f147c1364a3ed' + | ||
'e88d3f310fdb0eb98c924e599ca1b3c9', | ||
16); | ||
'e88d3f310fdb0eb98c924e599ca1b3c9', | ||
16); | ||
var expected = n.sqr().mod(p); | ||
@@ -189,40 +196,40 @@ var actual = n.toRed(red).redSqr().fromRed(); | ||
function bits2int (obits, q) { | ||
var bits = new BN(obits) | ||
var shift = (obits.length << 3) - q.bitLength() | ||
var bits = new BN(obits); | ||
var shift = (obits.length << 3) - q.bitLength(); | ||
if (shift > 0) { | ||
bits.ishrn(shift) | ||
bits.ishrn(shift); | ||
} | ||
return bits | ||
return bits; | ||
} | ||
var t = new Buffer('aff1651e4cd6036d57aa8b2a05ccf1a9d5a40166340ecbbdc55' + | ||
'be10b568aa0aa3d05ce9a2fcec9df8ed018e29683c6051cb83e' + | ||
'46ce31ba4edb045356a8d0d80b', 'hex'); | ||
'be10b568aa0aa3d05ce9a2fcec9df8ed018e29683c6051cb83e' + | ||
'46ce31ba4edb045356a8d0d80b', 'hex'); | ||
var g = new BN('5c7ff6b06f8f143fe8288433493e4769c4d988ace5be25a0e24809670' + | ||
'716c613d7b0cee6932f8faa7c44d2cb24523da53fbe4f6ec3595892d1' + | ||
'aa58c4328a06c46a15662e7eaa703a1decf8bbb2d05dbe2eb956c142a' + | ||
'338661d10461c0d135472085057f3494309ffa73c611f78b32adbb574' + | ||
'0c361c9f35be90997db2014e2ef5aa61782f52abeb8bd6432c4dd097b' + | ||
'c5423b285dafb60dc364e8161f4a2a35aca3a10b1c4d203cc76a470a3' + | ||
'3afdcbdd92959859abd8b56e1725252d78eac66e71ba9ae3f1dd24871' + | ||
'99874393cd4d832186800654760e1e34c09e4d155179f9ec0dc4473f9' + | ||
'96bdce6eed1cabed8b6f116f7ad9cf505df0f998e34ab27514b0ffe7', | ||
16); | ||
'716c613d7b0cee6932f8faa7c44d2cb24523da53fbe4f6ec3595892d1' + | ||
'aa58c4328a06c46a15662e7eaa703a1decf8bbb2d05dbe2eb956c142a' + | ||
'338661d10461c0d135472085057f3494309ffa73c611f78b32adbb574' + | ||
'0c361c9f35be90997db2014e2ef5aa61782f52abeb8bd6432c4dd097b' + | ||
'c5423b285dafb60dc364e8161f4a2a35aca3a10b1c4d203cc76a470a3' + | ||
'3afdcbdd92959859abd8b56e1725252d78eac66e71ba9ae3f1dd24871' + | ||
'99874393cd4d832186800654760e1e34c09e4d155179f9ec0dc4473f9' + | ||
'96bdce6eed1cabed8b6f116f7ad9cf505df0f998e34ab27514b0ffe7', | ||
16); | ||
var p = new BN('9db6fb5951b66bb6fe1e140f1d2ce5502374161fd6538df1648218642' + | ||
'f0b5c48c8f7a41aadfa187324b87674fa1822b00f1ecf8136943d7c55' + | ||
'757264e5a1a44ffe012e9936e00c1d3e9310b01c7d179805d3058b2a9' + | ||
'f4bb6f9716bfe6117c6b5b3cc4d9be341104ad4a80ad6c94e005f4b99' + | ||
'3e14f091eb51743bf33050c38de235567e1b34c3d6a5c0ceaa1a0f368' + | ||
'213c3d19843d0b4b09dcb9fc72d39c8de41f1bf14d4bb4563ca283716' + | ||
'21cad3324b6a2d392145bebfac748805236f5ca2fe92b871cd8f9c36d' + | ||
'3292b5509ca8caa77a2adfc7bfd77dda6f71125a7456fea153e433256' + | ||
'a2261c6a06ed3693797e7995fad5aabbcfbe3eda2741e375404ae25b', | ||
16); | ||
'f0b5c48c8f7a41aadfa187324b87674fa1822b00f1ecf8136943d7c55' + | ||
'757264e5a1a44ffe012e9936e00c1d3e9310b01c7d179805d3058b2a9' + | ||
'f4bb6f9716bfe6117c6b5b3cc4d9be341104ad4a80ad6c94e005f4b99' + | ||
'3e14f091eb51743bf33050c38de235567e1b34c3d6a5c0ceaa1a0f368' + | ||
'213c3d19843d0b4b09dcb9fc72d39c8de41f1bf14d4bb4563ca283716' + | ||
'21cad3324b6a2d392145bebfac748805236f5ca2fe92b871cd8f9c36d' + | ||
'3292b5509ca8caa77a2adfc7bfd77dda6f71125a7456fea153e433256' + | ||
'a2261c6a06ed3693797e7995fad5aabbcfbe3eda2741e375404ae25b', | ||
16); | ||
var q = new BN('f2c3119374ce76c9356990b465374a17f23f9ed35089bd969f61c6dde' + | ||
'9998c1f', 16); | ||
'9998c1f', 16); | ||
var k = bits2int(t, q); | ||
var expectedR = '89ec4bb1400eccff8e7d9aa515cd1de7803f2daff09693ee7fd1353e' + | ||
'90a68307'; | ||
'90a68307'; | ||
var r = g.toRed(BN.mont(p)).redPow(k).fromRed().mod(q); | ||
assert.equal(r.toString(16), expectedR); | ||
}) | ||
}); | ||
}); |
@@ -0,45 +1,47 @@ | ||
/* global describe, it */ | ||
var assert = require('assert'); | ||
var BN = require('../').BN; | ||
var fixtures = require('./fixtures'); | ||
describe('BN.js/Utils', function() { | ||
describe('.toString()', function() { | ||
describe('binary padding', function() { | ||
it('should have a length of 256', function() { | ||
describe('BN.js/Utils', function () { | ||
describe('.toString()', function () { | ||
describe('binary padding', function () { | ||
it('should have a length of 256', function () { | ||
var a = new BN(0); | ||
assert.equal(a.toString(2, 256).length, 256); | ||
}); | ||
}); | ||
describe('hex padding', function() { | ||
it('should have length of 8 from leading 15', function() { | ||
describe('hex padding', function () { | ||
it('should have length of 8 from leading 15', function () { | ||
var a = new BN('ffb9602', 16); | ||
var b = new Buffer(a.toString('hex', 2), 'hex'); | ||
assert.equal(a.toString('hex', 2).length, 8); | ||
}); | ||
it('should have length of 8 from leading zero', function() { | ||
it('should have length of 8 from leading zero', function () { | ||
var a = new BN('fb9604', 16); | ||
var b = new Buffer(a.toString('hex', 8), 'hex'); | ||
assert.equal(a.toString('hex', 8).length, 8); | ||
}); | ||
it('should have length of 8 from leading zeros', function() { | ||
it('should have length of 8 from leading zeros', function () { | ||
var a = new BN(0); | ||
var b = new Buffer(a.toString('hex', 8), 'hex'); | ||
assert.equal(a.toString('hex', 8).length, 8); | ||
}); | ||
it('should have length of 64 from leading 15', function() { | ||
it('should have length of 64 from leading 15', function () { | ||
var a = new BN( | ||
'ffb96ff654e61130ba8422f0debca77a0ea74ae5ea8bca9b54ab64aabf01003', | ||
16); | ||
var b = new Buffer(a.toString('hex', 2), 'hex'); | ||
'ffb96ff654e61130ba8422f0debca77a0ea74ae5ea8bca9b54ab64aabf01003', | ||
16); | ||
assert.equal(a.toString('hex', 2).length, 64); | ||
}); | ||
it('should have length of 64 from leading zero', function() { | ||
it('should have length of 64 from leading zero', function () { | ||
var a = new BN( | ||
'fb96ff654e61130ba8422f0debca77a0ea74ae5ea8bca9b54ab64aabf01003', | ||
16); | ||
var b = new Buffer(a.toString('hex', 64), 'hex'); | ||
'fb96ff654e61130ba8422f0debca77a0ea74ae5ea8bca9b54ab64aabf01003', | ||
16); | ||
assert.equal(a.toString('hex', 64).length, 64); | ||
@@ -50,4 +52,4 @@ }); | ||
describe('.isNeg()', function() { | ||
it('should return true for negative numbers', function() { | ||
describe('.isNeg()', function () { | ||
it('should return true for negative numbers', function () { | ||
assert.equal(new BN(-1).isNeg(), true); | ||
@@ -60,4 +62,4 @@ assert.equal(new BN(1).isNeg(), false); | ||
describe('.isOdd()', function() { | ||
it('should return true for odd numbers', function() { | ||
describe('.isOdd()', function () { | ||
it('should return true for odd numbers', function () { | ||
assert.equal(new BN(0).isOdd(), false); | ||
@@ -72,4 +74,4 @@ assert.equal(new BN(1).isOdd(), true); | ||
describe('.isEven()', function() { | ||
it('should return true for even numbers', function() { | ||
describe('.isEven()', function () { | ||
it('should return true for even numbers', function () { | ||
assert.equal(new BN(0).isEven(), true); | ||
@@ -84,4 +86,4 @@ assert.equal(new BN(1).isEven(), false); | ||
describe('.isZero()', function() { | ||
it('should return true for zero', function() { | ||
describe('.isZero()', function () { | ||
it('should return true for zero', function () { | ||
assert.equal(new BN(0).isZero(), true); | ||
@@ -93,4 +95,4 @@ assert.equal(new BN(1).isZero(), false); | ||
describe('.bitLength()', function() { | ||
it('should return proper bitLength', function() { | ||
describe('.bitLength()', function () { | ||
it('should return proper bitLength', function () { | ||
assert.equal(new BN(0).bitLength(), 0); | ||
@@ -110,4 +112,4 @@ assert.equal(new BN(0x1).bitLength(), 1); | ||
describe('.byteLength()', function() { | ||
it('should return proper byteLength', function() { | ||
describe('.byteLength()', function () { | ||
it('should return proper byteLength', function () { | ||
assert.equal(new BN(0).byteLength(), 0); | ||
@@ -127,4 +129,4 @@ assert.equal(new BN(0x1).byteLength(), 1); | ||
describe('.toArray()', function() { | ||
it('should zero pad to desired lengths', function() { | ||
describe('.toArray()', function () { | ||
it('should zero pad to desired lengths', function () { | ||
var n = new BN(0x123456); | ||
@@ -135,5 +137,5 @@ assert.deepEqual(n.toArray('be', 5), [ 0x00, 0x00, 0x12, 0x34, 0x56 ]); | ||
it('should throw when naturally larger than desired length', function() { | ||
it('should throw when naturally larger than desired length', function () { | ||
var n = new BN(0x123456); | ||
assert.throws(function() { | ||
assert.throws(function () { | ||
n.toArray('be', 2); | ||
@@ -144,4 +146,4 @@ }); | ||
describe('.toNumber()', function() { | ||
it('should return proper Number if below the limit', function() { | ||
describe('.toNumber()', function () { | ||
it('should return proper Number if below the limit', function () { | ||
var n = new BN(0x123456); | ||
@@ -151,5 +153,5 @@ assert.deepEqual(n.toNumber(), 0x123456); | ||
it('should throw when number exceeds 53 bits', function() { | ||
it('should throw when number exceeds 53 bits', function () { | ||
var n = new BN(1).iushln(54); | ||
assert.throws(function() { | ||
assert.throws(function () { | ||
n.toNumber(); | ||
@@ -160,4 +162,4 @@ }); | ||
describe('.zeroBits()', function() { | ||
it('should return proper zeroBits', function() { | ||
describe('.zeroBits()', function () { | ||
it('should return proper zeroBits', function () { | ||
assert.equal(new BN(0).zeroBits(), 0); | ||
@@ -176,4 +178,4 @@ assert.equal(new BN(0x1).zeroBits(), 0); | ||
describe('.toJSON', function() { | ||
it('should return hex string', function() { | ||
describe('.toJSON', function () { | ||
it('should return hex string', function () { | ||
assert.equal(new BN(0x123).toJSON(), '123'); | ||
@@ -183,4 +185,4 @@ }); | ||
describe('.cmpn', function() { | ||
it('should return -1, 0, 1 correctly', function() { | ||
describe('.cmpn', function () { | ||
it('should return -1, 0, 1 correctly', function () { | ||
assert.equal(new BN(42).cmpn(42), 0); | ||
@@ -192,3 +194,3 @@ assert.equal(new BN(42).cmpn(43), -1); | ||
assert.equal(new BN(0x3fffffe).cmpn(0x3fffffd), 1); | ||
assert.throws(function() { | ||
assert.throws(function () { | ||
new BN(0x3fffffe).cmpn(0x4000000); | ||
@@ -202,4 +204,4 @@ }); | ||
describe('.cmp', function() { | ||
it('should return -1, 0, 1 correctly', function() { | ||
describe('.cmp', function () { | ||
it('should return -1, 0, 1 correctly', function () { | ||
assert.equal(new BN(42).cmp(new BN(42)), 0); | ||
@@ -206,0 +208,0 @@ assert.equal(new BN(42).cmp(new BN(43)), -1); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
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
3
4846
166250
12