handle minimal data better.

This commit is contained in:
Christopher Jeffrey 2016-02-22 23:13:46 -08:00
parent 4cc5d73959
commit 1ced7af7b4
2 changed files with 10 additions and 8 deletions

View File

@ -1087,7 +1087,7 @@ script.bool = function bool(value) {
return false; return false;
}; };
script.num = function num(value, useNum, minimaldata) { script.num = function num(value, useNum, flags) {
if (utils.isFinite(value)) if (utils.isFinite(value))
return useNum ? value : new bn(value, 'le'); return useNum ? value : new bn(value, 'le');
@ -1097,7 +1097,7 @@ script.num = function num(value, useNum, minimaldata) {
assert(Buffer.isBuffer(value)); 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, // If the low bits on the last byte are unset,
// fail if The value's second to last byte does // fail if The value's second to last byte does
// not have the high bit set. A number can't // 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 // If we are signed, do (~num + 1) to get
// the positive counterpart and set bn's // the positive counterpart and set bn's
// negative flag. // 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(); value = new bn(value, 'le').notn(value.length * 8).addn(1).neg();
} }
} else { } else {
// Optimize by avoiding big numbers
if (useNum && value.length <= 1)
return value.length === 0 ? 0 : value[0];
value = new bn(value, 'le'); value = new bn(value, 'le');
} }

View File

@ -966,14 +966,17 @@ utils.nonce = function nonce() {
utils.isNegZero = function isNegZero(bytes, order) { utils.isNegZero = function isNegZero(bytes, order) {
var s = 0; var s = 0;
var b, res;
if (order === 'le') if (order === 'le')
s = bytes.length - 1; s = bytes.length - 1;
if (bytes[s] & 0x80) { if (bytes[s] & 0x80) {
bytes = bytes.slice(); b = bytes[s];
bytes[s] &= ~0x80; 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; return false;