diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 50b6282e..bd05fc16 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1278,31 +1278,6 @@ utils.nonce = function nonce() { // IN THE SOFTWARE. // -/** - * Test whether a script number is negative zero. - * @param {Buffer} bytes - * @param {String} order - `le` or `be`. - * @returns {Boolean} - */ - -utils.isNegZero = function isNegZero(bytes, order) { - var s = 0; - var b, res; - - if (order === 'le') - s = bytes.length - 1; - - if (bytes[s] & 0x80) { - b = bytes[s]; - bytes[s] &= ~0x80; - res = new bn(bytes, order).cmpn(0) === 0; - bytes[s] = b; - return res; - } - - return false; -}; - /** * Read uint8. * @param {Buffer} arr @@ -1487,14 +1462,8 @@ utils.read64 = function read64(arr, off) { num = arr.slice(off, off + 8); - // If we are signed, do (~num + 1) to get - // the positive counterpart and set bn's - // negative flag. - if (num[num.length - 1] & 0x80) { - if (utils.isNegZero(num, 'le')) - return new bn(0); + if (num[num.length - 1] & 0x80) return new bn(num, 'le').notn(64).addn(1).neg(); - } return new bn(num, 'le'); }; @@ -1513,14 +1482,8 @@ utils.read64BE = function read64BE(arr, off) { num = arr.slice(off, off + 8); - // If we are signed, do (~num + 1) to get - // the positive counterpart and set bn's - // negative flag. - if (num[0] & 0x80) { - if (utils.isNegZero(num, 'be')) - return new bn(0); + if (num[0] & 0x80) return new bn(num, 'be').notn(64).addn(1).neg(); - } return new bn(num, 'be'); }; @@ -1980,14 +1943,8 @@ utils.write64 = function write64(dst, num, off) { off = off >>> 0; - // Convert the number to the - // negative byte representation. - if (num.isNeg()) { - if (num.cmpn(0) === 0) - num = new bn(0); - else - num = num.neg().notn(64).addn(1); - } + if (num.isNeg()) + num = num.neg().notn(64).addn(1); if (num.bitLength() > 64) num = num.uand(utils.U64); @@ -2016,14 +1973,8 @@ utils.write64BE = function write64BE(dst, num, off) { off = off >>> 0; - // Convert the number to the - // negative byte representation. - if (num.isNeg()) { - if (num.cmpn(0) === 0) - num = new bn(0); - else - num = num.neg().notn(64).addn(1); - } + if (num.isNeg()) + num = num.neg().notn(64).addn(1); if (num.bitLength() > 64) num = num.uand(utils.U64);