diff --git a/lib/bcoin/script.js b/lib/bcoin/script.js index aaf1d7ba..56bb1347 100644 --- a/lib/bcoin/script.js +++ b/lib/bcoin/script.js @@ -1087,7 +1087,7 @@ script.bool = function bool(value) { return false; }; -script.num = function num(value, useNum, minimaldata) { +script.num = function num(value, useNum, flags) { if (utils.isFinite(value)) return useNum ? value : new bn(value, 'le'); @@ -1097,7 +1097,7 @@ script.num = function num(value, useNum, minimaldata) { assert(Buffer.isBuffer(value)); - if (minimaldata && value.length > 0) { + if ((flags & constants.flags.VERIFY_MINIMALDATA) && value.length > 0) { // If the low bits on the last byte are unset, // fail if The value's second to last byte does // not have the high bit set. A number can't @@ -1115,10 +1115,6 @@ script.num = function num(value, useNum, minimaldata) { } } - // Optimize by avoiding big numbers - if (useNum && value.length <= 1) - return value.length === 0 ? 0 : value[0]; - // If we are signed, do (~num + 1) to get // the positive counterpart and set bn's // negative flag. @@ -1129,6 +1125,9 @@ script.num = function num(value, useNum, minimaldata) { value = new bn(value, 'le').notn(value.length * 8).addn(1).neg(); } } else { + // Optimize by avoiding big numbers + if (useNum && value.length <= 1) + return value.length === 0 ? 0 : value[0]; value = new bn(value, 'le'); } diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index e0a89fcb..d750b306 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -966,14 +966,17 @@ utils.nonce = function nonce() { utils.isNegZero = function isNegZero(bytes, order) { var s = 0; + var b, res; if (order === 'le') s = bytes.length - 1; if (bytes[s] & 0x80) { - bytes = bytes.slice(); + b = bytes[s]; bytes[s] &= ~0x80; - return new bn(bytes, order).cmpn(0) === 0; + res = new bn(bytes, order).cmpn(0) === 0; + bytes[s] = b; + return res; } return false;