Daniel Cousens
11 years ago
13 changed files with 12 additions and 95 deletions
@ -1,38 +0,0 @@ |
|||
var assert = require('assert') |
|||
var BigInteger = require('bigi') |
|||
|
|||
// Import operations
|
|||
BigInteger.fromHex = function(hex) { |
|||
var buffer = new Buffer(hex, 'hex') |
|||
assert.equal(buffer.length, Buffer.byteLength(hex) / 2) |
|||
|
|||
return BigInteger.fromBuffer(buffer) |
|||
} |
|||
|
|||
BigInteger.fromBuffer = function(buffer) { |
|||
assert(Array.isArray(buffer) || Buffer.isBuffer(buffer)) // FIXME: Transitionary
|
|||
|
|||
// FIXME: Transitionary
|
|||
if (Buffer.isBuffer(buffer)) { |
|||
buffer = Array.prototype.slice.call(buffer) |
|||
} |
|||
|
|||
return BigInteger.fromByteArrayUnsigned(buffer) |
|||
} |
|||
|
|||
// Export operations
|
|||
BigInteger.prototype.toBuffer = function(s) { |
|||
if (s != undefined) assert(Number.isFinite(s)) |
|||
|
|||
var buffer = new Buffer(this.toByteArrayUnsigned()) |
|||
var padded = new Buffer(s - buffer.length) |
|||
padded.fill(0) |
|||
|
|||
return Buffer.concat([padded, buffer], s) |
|||
} |
|||
|
|||
BigInteger.prototype.toHex = function(s) { |
|||
return this.toBuffer(s).toString('hex') |
|||
} |
|||
|
|||
module.exports = BigInteger |
@ -1,46 +0,0 @@ |
|||
var assert = require('assert') |
|||
var BigInteger = require('../').BigInteger |
|||
|
|||
var fixtures = require('./fixtures/bigi') |
|||
|
|||
describe('BigInteger', function() { |
|||
describe('fromBuffer/fromHex', function() { |
|||
it('should match the test vectors', function() { |
|||
fixtures.valid.forEach(function(f) { |
|||
assert.equal(BigInteger.fromHex(f.hex).toString(), f.dec) |
|||
assert.equal(BigInteger.fromHex(f.hexPadded).toString(), f.dec) |
|||
}) |
|||
}) |
|||
|
|||
fixtures.invalid.forEach(function(f) { |
|||
it('throws on ' + f.description, function() { |
|||
assert.throws(function() { |
|||
BigInteger.fromHex(f.string) |
|||
}) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
describe('toBuffer/toHex', function() { |
|||
it('should match the test vectors', function() { |
|||
fixtures.valid.forEach(function(f) { |
|||
var bi = new BigInteger(f.dec) |
|||
|
|||
assert.equal(bi.toHex(), f.hex) |
|||
assert.equal(bi.toHex(32), f.hexPadded) |
|||
}) |
|||
}) |
|||
|
|||
it('throws on non-finite padding value', function() { |
|||
var bi = new BigInteger('1') |
|||
|
|||
assert.throws(function() { bi.toHex({}) }) |
|||
assert.throws(function() { bi.toHex([]) }) |
|||
assert.throws(function() { bi.toHex('') }) |
|||
assert.throws(function() { bi.toHex(0 / 0) }) |
|||
assert.throws(function() { bi.toHex(1 / 0) }) |
|||
}) |
|||
}) |
|||
}) |
|||
|
|||
module.exports = BigInteger |
Loading…
Reference in new issue