diff --git a/test/arithmetic-test.js b/test/arithmetic-test.js index 24fb4ca..7dc0c12 100644 --- a/test/arithmetic-test.js +++ b/test/arithmetic-test.js @@ -1,6 +1,6 @@ var assert = require('assert'); var BN = require('../').BN; -var fixtures = require('./fixtures'); +var fixtures = require('./fixtures/dh-groups'); describe('BN.js/Arithmetic', function() { describe('.add()', function() { @@ -183,8 +183,8 @@ describe('BN.js/Arithmetic', function() { }); it('should regress mul big numbers', function() { - var q = fixtures.dhGroups.p17.q; - var qs = fixtures.dhGroups.p17.qs; + var q = fixtures.p17.q; + var qs = fixtures.p17.qs; var q = new BN(q, 16); assert.equal(q.sqr().toString(16), qs); diff --git a/test/binary-test.js b/test/binary-test.js index 9a0673a..b3a29b0 100644 --- a/test/binary-test.js +++ b/test/binary-test.js @@ -1,6 +1,5 @@ var assert = require('assert'); var BN = require('../').BN; -var fixtures = require('./fixtures'); describe('BN.js/Binary', function() { describe('.shl()', function() { diff --git a/test/constructor-test.js b/test/constructor-test.js index a5f6c3f..1896eaf 100644 --- a/test/constructor-test.js +++ b/test/constructor-test.js @@ -1,6 +1,5 @@ var assert = require('assert'); var BN = require('../').BN; -var fixtures = require('./fixtures'); describe('BN.js/Constructor', function() { describe('with Smi input', function() { diff --git a/test/fixtures/bn.json b/test/fixtures/bn.json new file mode 100644 index 0000000..0d6eb44 --- /dev/null +++ b/test/fixtures/bn.json @@ -0,0 +1,105 @@ +{ + "valid": [ + { + "dec": "0", + "hex": "00" + }, + { + "dec": "1", + "hex": "01" + }, + { + "dec": "-1", + "hex": "ff" + }, + { + "dec": "127", + "hex": "7f" + }, + { + "dec": "-127", + "hex": "81" + }, + { + "dec": "128", + "hex": "80" + }, + { + "dec": "-128", + "hex": "80" + }, + { + "dec": "255", + "hex": "ff" + }, + { + "dec": "-255", + "hex": "ff01" + }, + { + "dec": "16300", + "hex": "3fac" + }, + { + "dec": "-16300", + "hex": "c054" + }, + { + "dec": "62300", + "hex": "f35c" + }, + { + "dec": "-62300", + "hex": "ff0ca4" + }, + { + "dec": "158798437896437949616241483468158498679", + "hex": "77777777777777777777777777777777" + }, + { + "dec": "115792089237316195423570985008687907852837564279074904382605163141518161494336", + "hex": "fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140" + }, + { + "dec": "48968302285117906840285529799176770990048954789747953886390402978935544927851", + "hex": "6c4313b03f2e7324d75e642f0ab81b734b724e13fec930f309e222470236d66b" + } + ], + "invalid": [ + { + "description": "non-decimal string", + "dec": "invalid" + }, + { + "description": "non-hex string", + "hex": "invalid" + }, + { + "description": "incomplete hex", + "hex": "ffffd0a" + }, + { + "description": "non-decimal alphabet", + "dec": "c2F0b3NoaQo=" + }, + { + "description": "non-hex alphabet", + "hex": "c2F0b3NoaQo=" + }, + { + "description": "internal whitespace", + "dec": "11111 11111", + "hex": "11111 11111" + }, + { + "description": "leading whitespace", + "dec": " 1111111111", + "hex": " 1111111111" + }, + { + "description": "trailing whitespace", + "dec": "1111111111 ", + "hex": "1111111111 " + } + ] +} diff --git a/test/fixtures.js b/test/fixtures/dh-groups.js similarity index 99% rename from test/fixtures.js rename to test/fixtures/dh-groups.js index 29442d9..183eece 100644 --- a/test/fixtures.js +++ b/test/fixtures/dh-groups.js @@ -1,4 +1,4 @@ -exports.dhGroups = { +module.exports = { p16: { prime: 'ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd1' + '29024e088a67cc74020bbea63b139b22514a08798e3404dd' + diff --git a/test/pummel/dh-group-test.js b/test/pummel/dh-group-test.js index dd6481f..54fb139 100644 --- a/test/pummel/dh-group-test.js +++ b/test/pummel/dh-group-test.js @@ -1,9 +1,8 @@ var assert = require('assert'); var BN = require('../../').BN; -var fixtures = require('../fixtures'); +var groups = require('../fixtures/dh-groups'); 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() { var group = groups[name]; diff --git a/test/red-test.js b/test/red-test.js index 9c348b6..a1243be 100644 --- a/test/red-test.js +++ b/test/red-test.js @@ -1,6 +1,5 @@ var assert = require('assert'); var BN = require('../').BN; -var fixtures = require('./fixtures'); describe('BN.js/Reduction context', function() { function testMethod(name, fn) { diff --git a/test/utils-test.js b/test/utils-test.js index c033aa7..a9b73c5 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -1,25 +1,46 @@ var assert = require('assert'); var BN = require('../').BN; -var fixtures = require('./fixtures'); +var fixtures = require('./fixtures/bn'); describe('BN.js/Utils', function() { describe('.toString()', function() { + fixtures.valid.forEach(function(f) { + it('should return ' + f.dec + ' when built with ' + f.dec, function() { + var bi = new BN(f.dec); + + assert.equal(bi.toString(), f.dec); + }); + + it('should return ' + f.dec + ' when built with ' + f.hex, function() { + var bi = new BN(f.hex, 16); + + assert.equal(bi.toString(), f.dec); + }); + + it('should return big-endian twos complement' + + 'hex for ' + f.dec, function() { + var bi = new BN(f.dec); + + assert.equal(bi.toString('hex'), f.hex); + }); + }); + 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() { 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() { var a = new BN(0); - var b = new Buffer(a.toString('hex', 8), 'hex'); + assert.equal(a.toString('hex', 8).length, 8); }); @@ -27,7 +48,7 @@ describe('BN.js/Utils', function() { var a = new BN( 'ffb96ff654e61130ba8422f0debca77a0ea74ae5ea8bca9b54ab64aabf01003', 16); - var b = new Buffer(a.toString('hex', 2), 'hex'); + assert.equal(a.toString('hex', 2).length, 64); }); @@ -35,12 +56,42 @@ describe('BN.js/Utils', function() { var a = new BN( 'fb96ff654e61130ba8422f0debca77a0ea74ae5ea8bca9b54ab64aabf01003', 16); - var b = new Buffer(a.toString('hex', 64), 'hex'); + assert.equal(a.toString('hex', 64).length, 64); }); }); }); + describe('.toArray()', function() { + fixtures.valid.forEach(function(f) { + it('should return a big-endian ' + + 'twos complement integer for ' + f.dec, function() { + var bi = new BN(f.dec); + var ba = new Buffer(bi.toArray()); + + assert.equal(ba.toString('hex'), f.hex); + }); + }); + + fixtures.invalid.forEach(function(f) { + if (f.dec) { + it('should throw on ' + f.dec, function() { + assert.throws(function() { + new BN(f.dec); + }); + }); + } + + if (f.hex) { + it('should throw on ' + f.hex, function() { + assert.throws(function() { + new BN(f.hex, 16); + }); + }); + } + }); + }); + describe('.bitLength()', function() { it('should return proper bitLength', function() { assert.equal(new BN(0).bitLength(), 0);