Merge pull request #1043 from eordano/performance/bn

Drop inherited and modified BN class for big improvement on performance
This commit is contained in:
Manuel Aráoz 2015-02-06 11:28:02 -03:00
commit 6e27dc3a9a
22 changed files with 171 additions and 189 deletions

View File

@ -195,10 +195,10 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
* @returns {BN} - An instance of BN with the decoded difficulty bits * @returns {BN} - An instance of BN with the decoded difficulty bits
*/ */
BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(info) { BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(info) {
var target = BN(this.bits & 0xffffff); var target = new BN(this.bits & 0xffffff);
var mov = 8 * ((this.bits >>> 24) - 3); var mov = 8 * ((this.bits >>> 24) - 3);
while (mov-- > 0) { while (mov-- > 0) {
target = target.mul(2); target = target.mul(new BN(2));
} }
return target; return target;
}; };

View File

@ -1,18 +1,9 @@
'use strict'; 'use strict';
var _BN = require('bn.js'); var BN = require('bn.js');
var $ = require('../util/preconditions'); var $ = require('../util/preconditions');
var _ = require('lodash'); var _ = require('lodash');
var BN = function BN(n, base) {
if (!(this instanceof BN)) {
return new BN(n, base);
}
_BN.apply(this, arguments);
};
BN.prototype = _BN.prototype;
var reversebuf = function(buf) { var reversebuf = function(buf) {
var buf2 = new Buffer(buf.length); var buf2 = new Buffer(buf.length);
for (var i = 0; i < buf.length; i++) { for (var i = 0; i < buf.length; i++) {
@ -21,18 +12,18 @@ var reversebuf = function(buf) {
return buf2; return buf2;
}; };
BN.Zero = new BN(0);
BN.One = new BN(1);
BN.Minus1 = new BN(-1);
BN.fromNumber = function(n) { BN.fromNumber = function(n) {
$.checkArgument(_.isNumber(n)); $.checkArgument(_.isNumber(n));
return BN(n); return new BN(n);
};
BN.prototype.toNumber = function() {
return parseInt(this.toString(10), 10);
}; };
BN.fromString = function(str) { BN.fromString = function(str) {
$.checkArgument(_.isString(str)); $.checkArgument(_.isString(str));
return BN(str); return new BN(str);
}; };
BN.fromBuffer = function(buf, opts) { BN.fromBuffer = function(buf, opts) {
@ -44,19 +35,37 @@ BN.fromBuffer = function(buf, opts) {
return bn; return bn;
}; };
BN.trim = function(buf, natlen) { /**
return buf.slice(natlen - buf.length, buf.length); * Instantiate a BigNumber from a "signed magnitude buffer"
* (a buffer where the most significant bit represents the sign (0 = positive, -1 = negative))
*/
BN.fromSM = function(buf, opts) {
var ret;
if (buf.length === 0) {
return BN.fromBuffer(new Buffer([0]));
}
var endian = 'big';
if (opts) {
endian = opts.endian;
}
if (endian === 'little') {
buf = reversebuf(buf);
}
if (buf[0] & 0x80) {
buf[0] = buf[0] & 0x7f;
ret = BN.fromBuffer(buf);
ret.neg().copy(ret);
} else {
ret = BN.fromBuffer(buf);
}
return ret;
}; };
BN.pad = function(buf, natlen, size) {
var rbuf = new Buffer(size); BN.prototype.toNumber = function() {
for (var i = 0; i < buf.length; i++) { return parseInt(this.toString(10), 10);
rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
}
for (i = 0; i < size - natlen; i++) {
rbuf[i] = 0;
}
return rbuf;
}; };
BN.prototype.toBuffer = function(opts) { BN.prototype.toBuffer = function(opts) {
@ -85,35 +94,9 @@ BN.prototype.toBuffer = function(opts) {
return buf; return buf;
}; };
// signed magnitude buffer
// most significant bit represents sign (0 = positive, -1 = negative)
BN.fromSM = function(buf, opts) {
var ret;
if (buf.length === 0) {
return BN.fromBuffer(new Buffer([0]));
}
var endian = 'big';
if (opts) {
endian = opts.endian;
}
if (endian === 'little') {
buf = reversebuf(buf);
}
if (buf[0] & 0x80) {
buf[0] = buf[0] & 0x7f;
ret = BN.fromBuffer(buf);
ret.neg().copy(ret);
} else {
ret = BN.fromBuffer(buf);
}
return ret;
};
BN.prototype.toSMBigEndian = function() { BN.prototype.toSMBigEndian = function() {
var buf; var buf;
if (this.cmp(0) === -1) { if (this.cmp(BN.Zero) === -1) {
buf = this.neg().toBuffer(); buf = this.neg().toBuffer();
if (buf[0] & 0x80) { if (buf[0] & 0x80) {
buf = Buffer.concat([new Buffer([0x80]), buf]); buf = Buffer.concat([new Buffer([0x80]), buf]);
@ -132,6 +115,7 @@ BN.prototype.toSMBigEndian = function() {
} }
return buf; return buf;
}; };
BN.prototype.toSM = function(opts) { BN.prototype.toSM = function(opts) {
var endian = opts ? opts.endian : 'big'; var endian = opts ? opts.endian : 'big';
var buf = this.toSMBigEndian(); var buf = this.toSMBigEndian();
@ -142,10 +126,13 @@ BN.prototype.toSM = function(opts) {
return buf; return buf;
}; };
// This is analogous to the constructor for CScriptNum in bitcoind. Many ops in /**
// bitcoind's script interpreter use CScriptNum, which is not really a proper * Create a BN from a "ScriptNum":
// bignum. Instead, an error is thrown if trying to input a number bigger than * This is analogous to the constructor for CScriptNum in bitcoind. Many ops in
// 4 bytes. We copy that behavior here. * bitcoind's script interpreter use CScriptNum, which is not really a proper
* bignum. Instead, an error is thrown if trying to input a number bigger than
* 4 bytes. We copy that behavior here.
*/
BN.fromScriptNumBuffer = function(buf, fRequireMinimal) { BN.fromScriptNumBuffer = function(buf, fRequireMinimal) {
var nMaxNumSize = 4; var nMaxNumSize = 4;
$.checkArgument(buf.length <= nMaxNumSize, new Error('script number overflow')); $.checkArgument(buf.length <= nMaxNumSize, new Error('script number overflow'));
@ -172,29 +159,18 @@ BN.fromScriptNumBuffer = function(buf, fRequireMinimal) {
}); });
}; };
// The corollary to the above, with the notable exception that we do not throw /**
// an error if the output is larger than four bytes. (Which can happen if * The corollary to the above, with the notable exception that we do not throw
// performing a numerical operation that results in an overflow to more than 4 * an error if the output is larger than four bytes. (Which can happen if
// bytes). * performing a numerical operation that results in an overflow to more than 4
* bytes).
*/
BN.prototype.toScriptNumBuffer = function() { BN.prototype.toScriptNumBuffer = function() {
return this.toSM({ return this.toSM({
endian: 'little' endian: 'little'
}); });
}; };
var decorate = function decorate(name) {
BN.prototype['_' + name] = BN.prototype[name];
var f = function(b) {
if (typeof b === 'string') {
b = new BN(b);
} else if (typeof b === 'number') {
b = new BN(b.toString());
}
return this['_' + name](b);
};
BN.prototype[name] = f;
};
BN.prototype.gt = function(b) { BN.prototype.gt = function(b) {
return this.cmp(b) > 0; return this.cmp(b) > 0;
}; };
@ -203,13 +179,19 @@ BN.prototype.lt = function(b) {
return this.cmp(b) < 0; return this.cmp(b) < 0;
}; };
decorate('add'); BN.trim = function(buf, natlen) {
decorate('sub'); return buf.slice(natlen - buf.length, buf.length);
decorate('mul'); };
decorate('mod');
decorate('div'); BN.pad = function(buf, natlen, size) {
decorate('cmp'); var rbuf = new Buffer(size);
decorate('gt'); for (var i = 0; i < buf.length; i++) {
decorate('lt'); rbuf[rbuf.length - 1 - i] = buf[buf.length - 1 - i];
}
for (i = 0; i < size - natlen; i++) {
rbuf[i] = 0;
}
return rbuf;
};
module.exports = BN; module.exports = BN;

View File

@ -66,7 +66,7 @@ ECDSA.prototype.randomK = function() {
var k; var k;
do { do {
k = BN.fromBuffer(Random.getRandomBuffer(32)); k = BN.fromBuffer(Random.getRandomBuffer(32));
} while (!(k.lt(N) && k.gt(0))); } while (!(k.lt(N) && k.gt(BN.Zero)));
this.k = k; this.k = k;
return this; return this;
}; };
@ -97,7 +97,7 @@ ECDSA.prototype.deterministicK = function(badrs) {
var N = Point.getN(); var N = Point.getN();
// also explained in 3.2, we must ensure T is in the proper range (0, N) // also explained in 3.2, we must ensure T is in the proper range (0, N)
for (var i = 0; i < badrs || !(T.lt(N) && T.gt(0)); i++) { for (var i = 0; i < badrs || !(T.lt(N) && T.gt(BN.Zero)); i++) {
k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00])]), k); k = Hash.sha256hmac(Buffer.concat([v, new Buffer([0x00])]), k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
v = Hash.sha256hmac(v, k); v = Hash.sha256hmac(v, k);
@ -164,7 +164,7 @@ ECDSA.prototype.sigError = function() {
var r = this.sig.r; var r = this.sig.r;
var s = this.sig.s; var s = this.sig.s;
if (!(r.gt(0) && r.lt(Point.getN())) || !(s.gt(0) && s.lt(Point.getN()))) { if (!(r.gt(BN.Zero) && r.lt(Point.getN())) || !(s.gt(BN.Zero) && s.lt(Point.getN()))) {
return 'r and s not in range'; return 'r and s not in range';
} }
@ -212,7 +212,7 @@ ECDSA.prototype._findSignature = function(d, e) {
Q = G.mul(k); Q = G.mul(k);
r = Q.x.mod(N); r = Q.x.mod(N);
s = k.invm(N).mul(e.add(d.mul(r))).mod(N); s = k.invm(N).mul(e.add(d.mul(r))).mod(N);
} while (r.cmp(0) <= 0 || s.cmp(0) <= 0); } while (r.cmp(BN.Zero) <= 0 || s.cmp(BN.Zero) <= 0);
s = ECDSA.toLowS(s); s = ECDSA.toLowS(s);
return { return {

View File

@ -49,7 +49,7 @@ Point.fromX = function fromX(odd, x){
* @returns {Point} An instance of the base point. * @returns {Point} An instance of the base point.
*/ */
Point.getG = function getG() { Point.getG = function getG() {
return Point(ec.curve.g.getX(), ec.curve.g.getY()); return ec.curve.g;
}; };
/** /**
@ -60,7 +60,7 @@ Point.getG = function getG() {
* @returns {BN} A BN instance of the number of points on the curve * @returns {BN} A BN instance of the number of points on the curve
*/ */
Point.getN = function getN() { Point.getN = function getN() {
return BN(ec.curve.n.toArray()); return new BN(ec.curve.n.toArray());
}; };
Point.prototype._getX = Point.prototype.getX; Point.prototype._getX = Point.prototype.getX;
@ -72,7 +72,7 @@ Point.prototype._getX = Point.prototype.getX;
* @returns {BN} A BN instance of the X coordinate * @returns {BN} A BN instance of the X coordinate
*/ */
Point.prototype.getX = function getX() { Point.prototype.getX = function getX() {
return BN(this._getX().toArray()); return new BN(this._getX().toArray());
}; };
Point.prototype._getY = Point.prototype.getY; Point.prototype._getY = Point.prototype.getY;
@ -84,7 +84,7 @@ Point.prototype._getY = Point.prototype.getY;
* @returns {BN} A BN instance of the Y coordinate * @returns {BN} A BN instance of the Y coordinate
*/ */
Point.prototype.getY = function getY() { Point.prototype.getY = function getY() {
return BN(this._getY().toArray()); return new BN(this._getY().toArray());
}; };
/** /**
@ -102,7 +102,7 @@ Point.prototype.validate = function validate() {
throw new Error('Point cannot be equal to Infinity'); throw new Error('Point cannot be equal to Infinity');
} }
if (this.getX().cmp(0) === 0 || this.getY().cmp(0) === 0){ if (this.getX().cmp(BN.Zero) === 0 || this.getY().cmp(BN.Zero) === 0){
throw new Error('Invalid x,y value for curve, cannot equal 0.'); throw new Error('Invalid x,y value for curve, cannot equal 0.');
} }
@ -112,8 +112,8 @@ Point.prototype.validate = function validate() {
throw new Error('Invalid y value for curve.'); throw new Error('Invalid y value for curve.');
} }
var xValidRange = (this.getX().gt(-1) && this.getX().lt(Point.getN())); var xValidRange = (this.getX().gt(BN.Minus1) && this.getX().lt(Point.getN()));
var yValidRange = (this.getY().gt(-1) && this.getY().lt(Point.getN())); var yValidRange = (this.getY().gt(BN.Minus1) && this.getY().lt(Point.getN()));
if ( !xValidRange || !yValidRange ) { if ( !xValidRange || !yValidRange ) {
throw new Error('Point does not lie on the curve'); throw new Error('Point does not lie on the curve');

View File

@ -264,8 +264,8 @@ Signature.isTxDER = function(buf) {
* See also BIP 62, "low S values in signatures" * See also BIP 62, "low S values in signatures"
*/ */
Signature.prototype.hasLowS = function() { Signature.prototype.hasLowS = function() {
if (this.s.lt(1) || if (this.s.lt(new BN(1)) ||
this.s.gt(BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0'))) { this.s.gt(new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0'))) {
return false; return false;
} }
return true; return true;

View File

@ -141,13 +141,13 @@ BufferReader.prototype.readVarintBN = function() {
var first = this.readUInt8(); var first = this.readUInt8();
switch (first) { switch (first) {
case 0xFD: case 0xFD:
return BN(this.readUInt16LE()); return new BN(this.readUInt16LE());
case 0xFE: case 0xFE:
return BN(this.readUInt32LE()); return new BN(this.readUInt32LE());
case 0xFF: case 0xFF:
return this.readUInt64LEBN(); return this.readUInt64LEBN();
default: default:
return BN(first); return new BN(first);
} }
}; };

View File

@ -47,7 +47,7 @@ var PrivateKey = function PrivateKey(data, network) {
var info = this._classifyArguments(data, network); var info = this._classifyArguments(data, network);
// validation // validation
if (!info.bn || info.bn.cmp(0) === 0){ if (!info.bn || info.bn.cmp(new BN(0)) === 0){
throw new TypeError('Number can not be equal to zero, undefined, null or false'); throw new TypeError('Number can not be equal to zero, undefined, null or false');
} }
if (!info.bn.lt(Point.getN())) { if (!info.bn.lt(Point.getN())) {
@ -102,7 +102,7 @@ PrivateKey.prototype._classifyArguments = function(data, network) {
info.network = Networks.get(data); info.network = Networks.get(data);
} else if (typeof(data) === 'string'){ } else if (typeof(data) === 'string'){
if (JSUtil.isHexa(data)) { if (JSUtil.isHexa(data)) {
info.bn = BN(new Buffer(data, 'hex')); info.bn = new BN(new Buffer(data, 'hex'));
} else { } else {
info = PrivateKey._transformWIF(data, network); info = PrivateKey._transformWIF(data, network);
} }
@ -246,7 +246,7 @@ PrivateKey._transformJSON = function(json) {
if (JSUtil.isValidJSON(json)) { if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json); json = JSON.parse(json);
} }
var bn = BN(json.bn, 'hex'); var bn = new BN(json.bn, 'hex');
return { return {
bn: bn, bn: bn,
network: json.network, network: json.network,

View File

@ -169,18 +169,18 @@ PublicKey._transformDER = function(buf, strict) {
if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) { if (xbuf.length !== 32 || ybuf.length !== 32 || buf.length !== 65) {
throw new TypeError('Length of x and y must be 32 bytes'); throw new TypeError('Length of x and y must be 32 bytes');
} }
x = BN(xbuf); x = new BN(xbuf);
y = BN(ybuf); y = new BN(ybuf);
info.point = new Point(x, y); info.point = new Point(x, y);
info.compressed = false; info.compressed = false;
} else if (buf[0] === 0x03) { } else if (buf[0] === 0x03) {
xbuf = buf.slice(1); xbuf = buf.slice(1);
x = BN(xbuf); x = new BN(xbuf);
info = PublicKey._transformX(true, x); info = PublicKey._transformX(true, x);
info.compressed = true; info.compressed = true;
} else if (buf[0] === 0x02) { } else if (buf[0] === 0x02) {
xbuf = buf.slice(1); xbuf = buf.slice(1);
x = BN(xbuf); x = new BN(xbuf);
info = PublicKey._transformX(false, x); info = PublicKey._transformX(false, x);
info.compressed = true; info.compressed = true;
} else { } else {
@ -228,8 +228,8 @@ PublicKey._transformJSON = function(json) {
if (JSUtil.isValidJSON(json)) { if (JSUtil.isValidJSON(json)) {
json = JSON.parse(json); json = JSON.parse(json);
} }
var x = BN(json.x, 'hex'); var x = new BN(json.x, 'hex');
var y = BN(json.y, 'hex'); var y = new BN(json.y, 'hex');
var point = new Point(x, y); var point = new Point(x, y);
return new PublicKey(point, { return new PublicKey(point, {
compressed: json.compressed compressed: json.compressed

View File

@ -400,7 +400,7 @@ Interpreter.prototype.step = function() {
// ( -- value) // ( -- value)
// ScriptNum bn((int)opcode - (int)(Opcode.OP_1 - 1)); // ScriptNum bn((int)opcode - (int)(Opcode.OP_1 - 1));
n = opcodenum - (Opcode.OP_1 - 1); n = opcodenum - (Opcode.OP_1 - 1);
buf = BN(n).toScriptNumBuffer(); buf = new BN(n).toScriptNumBuffer();
this.stack.push(buf); this.stack.push(buf);
// The result of these opcodes should always be the minimal way to push the data // The result of these opcodes should always be the minimal way to push the data
// they push, so no need for a CheckMinimalPush here. // they push, so no need for a CheckMinimalPush here.
@ -623,7 +623,7 @@ Interpreter.prototype.step = function() {
case Opcode.OP_DEPTH: case Opcode.OP_DEPTH:
{ {
// -- stacksize // -- stacksize
buf = BN(this.stack.length).toScriptNumBuffer(); buf = new BN(this.stack.length).toScriptNumBuffer();
this.stack.push(buf); this.stack.push(buf);
} }
break; break;
@ -748,7 +748,7 @@ Interpreter.prototype.step = function() {
this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION'; this.errstr = 'SCRIPT_ERR_INVALID_STACK_OPERATION';
return false; return false;
} }
bn = BN(this.stack[this.stack.length - 1].length); bn = new BN(this.stack[this.stack.length - 1].length);
this.stack.push(bn.toScriptNumBuffer()); this.stack.push(bn.toScriptNumBuffer());
} }
break; break;
@ -803,24 +803,24 @@ Interpreter.prototype.step = function() {
bn = BN.fromScriptNumBuffer(buf, fRequireMinimal); bn = BN.fromScriptNumBuffer(buf, fRequireMinimal);
switch (opcodenum) { switch (opcodenum) {
case Opcode.OP_1ADD: case Opcode.OP_1ADD:
bn = bn.add(1); bn = bn.add(BN.One);
break; break;
case Opcode.OP_1SUB: case Opcode.OP_1SUB:
bn = bn.sub(1); bn = bn.sub(BN.One);
break; break;
case Opcode.OP_NEGATE: case Opcode.OP_NEGATE:
bn = bn.neg(); bn = bn.neg();
break; break;
case Opcode.OP_ABS: case Opcode.OP_ABS:
if (bn.cmp(0) < 0) { if (bn.cmp(BN.Zero) < 0) {
bn = bn.neg(); bn = bn.neg();
} }
break; break;
case Opcode.OP_NOT: case Opcode.OP_NOT:
bn = BN((bn.cmp(0) === 0) + 0); bn = new BN((bn.cmp(BN.Zero) === 0) + 0);
break; break;
case Opcode.OP_0NOTEQUAL: case Opcode.OP_0NOTEQUAL:
bn = BN((bn.cmp(0) !== 0) + 0); bn = new BN((bn.cmp(BN.Zero) !== 0) + 0);
break; break;
//default: assert(!'invalid opcode'); break; // TODO: does this ever occur? //default: assert(!'invalid opcode'); break; // TODO: does this ever occur?
} }
@ -850,7 +850,7 @@ Interpreter.prototype.step = function() {
} }
bn1 = BN.fromScriptNumBuffer(this.stack[this.stack.length - 2], fRequireMinimal); bn1 = BN.fromScriptNumBuffer(this.stack[this.stack.length - 2], fRequireMinimal);
bn2 = BN.fromScriptNumBuffer(this.stack[this.stack.length - 1], fRequireMinimal); bn2 = BN.fromScriptNumBuffer(this.stack[this.stack.length - 1], fRequireMinimal);
bn = BN(0); bn = new BN(0);
switch (opcodenum) { switch (opcodenum) {
case Opcode.OP_ADD: case Opcode.OP_ADD:
@ -863,39 +863,39 @@ Interpreter.prototype.step = function() {
// case Opcode.OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break; // case Opcode.OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break;
case Opcode.OP_BOOLAND: case Opcode.OP_BOOLAND:
bn = BN(((bn1.cmp(0) !== 0) && (bn2.cmp(0) !== 0)) + 0); bn = new BN(((bn1.cmp(BN.Zero) !== 0) && (bn2.cmp(BN.Zero) !== 0)) + 0);
break; break;
// case Opcode.OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break; // case Opcode.OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break;
case Opcode.OP_BOOLOR: case Opcode.OP_BOOLOR:
bn = BN(((bn1.cmp(0) !== 0) || (bn2.cmp(0) !== 0)) + 0); bn = new BN(((bn1.cmp(BN.Zero) !== 0) || (bn2.cmp(BN.Zero) !== 0)) + 0);
break; break;
// case Opcode.OP_NUMEQUAL: bn = (bn1 == bn2); break; // case Opcode.OP_NUMEQUAL: bn = (bn1 == bn2); break;
case Opcode.OP_NUMEQUAL: case Opcode.OP_NUMEQUAL:
bn = BN((bn1.cmp(bn2) === 0) + 0); bn = new BN((bn1.cmp(bn2) === 0) + 0);
break; break;
// case Opcode.OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break; // case Opcode.OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break;
case Opcode.OP_NUMEQUALVERIFY: case Opcode.OP_NUMEQUALVERIFY:
bn = BN((bn1.cmp(bn2) === 0) + 0); bn = new BN((bn1.cmp(bn2) === 0) + 0);
break; break;
// case Opcode.OP_NUMNOTEQUAL: bn = (bn1 != bn2); break; // case Opcode.OP_NUMNOTEQUAL: bn = (bn1 != bn2); break;
case Opcode.OP_NUMNOTEQUAL: case Opcode.OP_NUMNOTEQUAL:
bn = BN((bn1.cmp(bn2) !== 0) + 0); bn = new BN((bn1.cmp(bn2) !== 0) + 0);
break; break;
// case Opcode.OP_LESSTHAN: bn = (bn1 < bn2); break; // case Opcode.OP_LESSTHAN: bn = (bn1 < bn2); break;
case Opcode.OP_LESSTHAN: case Opcode.OP_LESSTHAN:
bn = BN((bn1.cmp(bn2) < 0) + 0); bn = new BN((bn1.cmp(bn2) < 0) + 0);
break; break;
// case Opcode.OP_GREATERTHAN: bn = (bn1 > bn2); break; // case Opcode.OP_GREATERTHAN: bn = (bn1 > bn2); break;
case Opcode.OP_GREATERTHAN: case Opcode.OP_GREATERTHAN:
bn = BN((bn1.cmp(bn2) > 0) + 0); bn = new BN((bn1.cmp(bn2) > 0) + 0);
break; break;
// case Opcode.OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break; // case Opcode.OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break;
case Opcode.OP_LESSTHANOREQUAL: case Opcode.OP_LESSTHANOREQUAL:
bn = BN((bn1.cmp(bn2) <= 0) + 0); bn = new BN((bn1.cmp(bn2) <= 0) + 0);
break; break;
// case Opcode.OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break; // case Opcode.OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break;
case Opcode.OP_GREATERTHANOREQUAL: case Opcode.OP_GREATERTHANOREQUAL:
bn = BN((bn1.cmp(bn2) >= 0) + 0); bn = new BN((bn1.cmp(bn2) >= 0) + 0);
break; break;
case Opcode.OP_MIN: case Opcode.OP_MIN:
bn = (bn1.cmp(bn2) < 0 ? bn1 : bn2); bn = (bn1.cmp(bn2) < 0 ? bn1 : bn2);

View File

@ -727,18 +727,18 @@ Transaction.prototype.verify = function() {
} }
// Check for negative or overflow output values // Check for negative or overflow output values
var valueoutbn = BN(0); var valueoutbn = new BN(0);
for (var i = 0; i < this.outputs.length; i++) { for (var i = 0; i < this.outputs.length; i++) {
var txout = this.outputs[i]; var txout = this.outputs[i];
var valuebn = txout._satoshisBN; var valuebn = txout._satoshisBN;
if (valuebn.lt(0)) { if (valuebn.lt(BN.Zero)) {
return 'transaction txout ' + i + ' negative'; return 'transaction txout ' + i + ' negative';
} }
if (valuebn.gt(BN(Transaction.MAX_MONEY, 10))) { if (valuebn.gt(new BN(Transaction.MAX_MONEY, 10))) {
return 'transaction txout ' + i + ' greater than MAX_MONEY'; return 'transaction txout ' + i + ' greater than MAX_MONEY';
} }
valueoutbn = valueoutbn.add(valuebn); valueoutbn = valueoutbn.add(valuebn);
if (valueoutbn.gt(Transaction.MAX_MONEY)) { if (valueoutbn.gt(new BN(Transaction.MAX_MONEY))) {
return 'transaction txout ' + i + ' total output greater than MAX_MONEY'; return 'transaction txout ' + i + ' total output greater than MAX_MONEY';
} }
} }

View File

@ -69,13 +69,13 @@ describe('Block', function() {
it('should instantiate from a raw block binary', function() { it('should instantiate from a raw block binary', function() {
var x = Block.fromRawBlock(dataRawBlockBinary); var x = Block.fromRawBlock(dataRawBlockBinary);
x.header.version.should.equal(2); x.header.version.should.equal(2);
BN(x.header.bits).toString('hex').should.equal('1c3fffc0'); new BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
}); });
it('should instantiate from raw block buffer', function() { it('should instantiate from raw block buffer', function() {
var x = Block.fromRawBlock(dataRawBlockBuffer); var x = Block.fromRawBlock(dataRawBlockBuffer);
x.header.version.should.equal(2); x.header.version.should.equal(2);
BN(x.header.bits).toString('hex').should.equal('1c3fffc0'); new BN(x.header.bits).toString('hex').should.equal('1c3fffc0');
}); });
}); });

View File

@ -185,13 +185,13 @@ describe('BlockHeader', function() {
it('should instantiate from a raw block binary', function() { it('should instantiate from a raw block binary', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBinary); var x = BlockHeader.fromRawBlock(dataRawBlockBinary);
x.version.should.equal(2); x.version.should.equal(2);
BN(x.bits).toString('hex').should.equal('1c3fffc0'); new BN(x.bits).toString('hex').should.equal('1c3fffc0');
}); });
it('should instantiate from raw block buffer', function() { it('should instantiate from raw block buffer', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer); var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
x.version.should.equal(2); x.version.should.equal(2);
BN(x.bits).toString('hex').should.equal('1c3fffc0'); new BN(x.bits).toString('hex').should.equal('1c3fffc0');
}); });
}); });

View File

@ -70,7 +70,7 @@ describe('BN', function() {
describe('to/from ScriptNumBuffer', function() { describe('to/from ScriptNumBuffer', function() {
[0, 1, 10, 256, 1000, 65536, 65537, -1, -1000, -65536, -65537].forEach(function(n) { [0, 1, 10, 256, 1000, 65536, 65537, -1, -1000, -65536, -65537].forEach(function(n) {
it('rountrips correctly for ' + n, function() { it('rountrips correctly for ' + n, function() {
BN.fromScriptNumBuffer(BN(n).toScriptNumBuffer()).toNumber().should.equal(n); BN.fromScriptNumBuffer(new BN(n).toScriptNumBuffer()).toNumber().should.equal(n);
}); });
}); });
}); });
@ -83,7 +83,7 @@ describe('BN', function() {
describe('#toString', function() { describe('#toString', function() {
it('should make a string', function() { it('should make a string', function() {
BN(5).toString().should.equal('5'); new BN(5).toString().should.equal('5');
}); });
}); });

View File

@ -42,8 +42,8 @@ describe('ECDSA', function() {
it('calulates this known i', function() { it('calulates this known i', function() {
var hashbuf = Hash.sha256(new Buffer('some data')); var hashbuf = Hash.sha256(new Buffer('some data'));
var r = BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10); var r = new BN('71706645040721865894779025947914615666559616020894583599959600180037551395766', 10);
var s = BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10); var s = new BN('109412465507152403114191008482955798903072313614214706891149785278625167723646', 10);
var ecdsa = new ECDSA({ var ecdsa = new ECDSA({
privkey: new Privkey(BN.fromBuffer(Hash.sha256(new Buffer('test')))), privkey: new Privkey(BN.fromBuffer(Hash.sha256(new Buffer('test')))),
hashbuf: hashbuf, hashbuf: hashbuf,
@ -83,7 +83,7 @@ describe('ECDSA', function() {
it('should generate a random k that is (almost always) greater than this relatively small number', function() { it('should generate a random k that is (almost always) greater than this relatively small number', function() {
ecdsa.randomK(); ecdsa.randomK();
var k1 = ecdsa.k; var k1 = ecdsa.k;
var k2 = BN(Math.pow(2, 32)).mul(BN(Math.pow(2, 32))).mul(BN(Math.pow(2, 32))); var k2 = new BN(Math.pow(2, 32)).mul(new BN(Math.pow(2, 32))).mul(new BN(Math.pow(2, 32)));
k2.gt(k1).should.equal(false); k2.gt(k1).should.equal(false);
}); });
@ -110,7 +110,7 @@ describe('ECDSA', function() {
// https://github.com/bitcoinjs/bitcoinjs-lib/blob/10630873ebaa42381c5871e20336fbfb46564ac8/test/fixtures/ecdsa.json#L6 // https://github.com/bitcoinjs/bitcoinjs-lib/blob/10630873ebaa42381c5871e20336fbfb46564ac8/test/fixtures/ecdsa.json#L6
var ecdsa = new ECDSA(); var ecdsa = new ECDSA();
ecdsa.hashbuf = Hash.sha256(new Buffer('Everything should be made as simple as possible, but not simpler.')); ecdsa.hashbuf = Hash.sha256(new Buffer('Everything should be made as simple as possible, but not simpler.'));
ecdsa.privkey = new Privkey(BN(1)); ecdsa.privkey = new Privkey(new BN(1));
ecdsa.privkey2pubkey(); ecdsa.privkey2pubkey();
ecdsa.deterministicK(); ecdsa.deterministicK();
ecdsa.k.toBuffer().toString('hex') ecdsa.k.toBuffer().toString('hex')
@ -125,7 +125,7 @@ describe('ECDSA', function() {
describe('#toPublicKey', function() { describe('#toPublicKey', function() {
it('should calculate the correct public key', function() { it('should calculate the correct public key', function() {
ecdsa.k = BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10); ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.sign(); ecdsa.sign();
ecdsa.sig.i = 0; ecdsa.sig.i = 0;
var pubkey = ecdsa.toPublicKey(); var pubkey = ecdsa.toPublicKey();
@ -133,7 +133,7 @@ describe('ECDSA', function() {
}); });
it('should calculate the correct public key for this signature with low s', function() { it('should calculate the correct public key for this signature with low s', function() {
ecdsa.k = BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10); ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.sig = Signature.fromString('3045022100ec3cfe0e335791ad278b4ec8eac93d0347' + ecdsa.sig = Signature.fromString('3045022100ec3cfe0e335791ad278b4ec8eac93d0347' +
'a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43'); 'a97877bb1d54d35d189e225c15f6650220278cf15b05ce47fb37d2233802899d94c774d5480bba9f0f2d996baa13370c43');
ecdsa.sig.i = 0; ecdsa.sig.i = 0;
@ -142,7 +142,7 @@ describe('ECDSA', function() {
}); });
it('should calculate the correct public key for this signature with high s', function() { it('should calculate the correct public key for this signature with high s', function() {
ecdsa.k = BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10); ecdsa.k = new BN('114860389168127852803919605627759231199925249596762615988727970217268189974335', 10);
ecdsa.sign(); ecdsa.sign();
ecdsa.sig = Signature.fromString('3046022100ec3cfe0e335791ad278b4ec8eac93d0347' + ecdsa.sig = Signature.fromString('3046022100ec3cfe0e335791ad278b4ec8eac93d0347' +
'a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe'); 'a97877bb1d54d35d189e225c15f665022100d8730ea4fa31b804c82ddcc7fd766269f33a079ea38e012c9238f2e2bcff34fe');
@ -167,15 +167,15 @@ describe('ECDSA', function() {
'710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex')); '710fb053bb9f2b933173ff9a7baad41d04514751e6851f5304fd243751703bed21b914f6be218c0fa354a341', 'hex'));
ecdsa.pubkey = pk; ecdsa.pubkey = pk;
ecdsa.sig = new Signature(); ecdsa.sig = new Signature();
ecdsa.sig.r = BN(0); ecdsa.sig.r = new BN(0);
ecdsa.sig.s = BN(0); ecdsa.sig.s = new BN(0);
ecdsa.sigError().should.equal('r and s not in range'); ecdsa.sigError().should.equal('r and s not in range');
}); });
it('should return an error if the signature is incorrect', function() { it('should return an error if the signature is incorrect', function() {
ecdsa.sig = Signature.fromString('3046022100e9915e6236695f093a4128ac2a956c40' + ecdsa.sig = Signature.fromString('3046022100e9915e6236695f093a4128ac2a956c40' +
'ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827'); 'ed971531de2f4f41ba05fac7e2bd019c02210094e6a4a769cc7f2a8ab3db696c7cd8d56bcdbfff860a8c81de4bc6a798b90827');
ecdsa.sig.r = ecdsa.sig.r.add(BN(1)); ecdsa.sig.r = ecdsa.sig.r.add(new BN(1));
ecdsa.sigError().should.equal('Invalid signature'); ecdsa.sigError().should.equal('Invalid signature');
}); });
@ -248,7 +248,7 @@ describe('ECDSA', function() {
it('should verify a valid signature, and unverify an invalid signature', function() { it('should verify a valid signature, and unverify an invalid signature', function() {
var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey); var sig = ECDSA.sign(ecdsa.hashbuf, ecdsa.privkey);
ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey).should.equal(true); ECDSA.verify(ecdsa.hashbuf, sig, ecdsa.pubkey).should.equal(true);
var fakesig = new Signature(sig.r.add(1), sig.s); var fakesig = new Signature(sig.r.add(new BN(1)), sig.s);
ECDSA.verify(ecdsa.hashbuf, fakesig, ecdsa.pubkey).should.equal(false); ECDSA.verify(ecdsa.hashbuf, fakesig, ecdsa.pubkey).should.equal(false);
}); });
it('should work with big and little endian', function() { it('should work with big and little endian', function() {
@ -270,8 +270,8 @@ describe('ECDSA', function() {
k: BN.fromBuffer(new Buffer(obj.k, 'hex')), k: BN.fromBuffer(new Buffer(obj.k, 'hex')),
hashbuf: Hash.sha256(new Buffer(obj.message)), hashbuf: Hash.sha256(new Buffer(obj.message)),
sig: new Signature().set({ sig: new Signature().set({
r: BN(obj.signature.r), r: new BN(obj.signature.r),
s: BN(obj.signature.s), s: new BN(obj.signature.s),
i: obj.i i: obj.i
}) })
}); });
@ -290,7 +290,7 @@ describe('ECDSA', function() {
it('should validate invalid.sigError vector ' + i + ': ' + obj.description, function() { it('should validate invalid.sigError vector ' + i + ': ' + obj.description, function() {
var ecdsa = ECDSA().set({ var ecdsa = ECDSA().set({
pubkey: Pubkey.fromPoint(point.fromX(true, 1)), pubkey: Pubkey.fromPoint(point.fromX(true, 1)),
sig: new Signature(BN(obj.signature.r), BN(obj.signature.s)), sig: new Signature(new BN(obj.signature.r), new BN(obj.signature.s)),
hashbuf: Hash.sha256(new Buffer(obj.message)) hashbuf: Hash.sha256(new Buffer(obj.message))
}); });
ecdsa.sigError().should.equal(obj.exception); ecdsa.sigError().should.equal(obj.exception);

View File

@ -78,7 +78,7 @@ describe('Point', function() {
it('should accurately multiply g by 2', function() { it('should accurately multiply g by 2', function() {
var g = Point.getG(); var g = Point.getG();
var b = g.mul(BN(2)); var b = g.mul(new BN(2));
b.getX().toString().should.equal('8956589192654700423125292042593569236064414582962220983'+ b.getX().toString().should.equal('8956589192654700423125292042593569236064414582962220983'+
'3684329913297188986597'); '3684329913297188986597');
b.getY().toString().should.equal('1215839929969383032296780861271339863615536788704162817'+ b.getY().toString().should.equal('1215839929969383032296780861271339863615536788704162817'+
@ -88,7 +88,7 @@ describe('Point', function() {
it('should accurately multiply g by n-1', function() { it('should accurately multiply g by n-1', function() {
var g = Point.getG(); var g = Point.getG();
var n = Point.getN(); var n = Point.getN();
var b = g.mul(n.sub(1)); var b = g.mul(n.sub(new BN(1)));
b.getX().toString().should.equal('55066263022277343669578718895168534326250603453777594175'+ b.getX().toString().should.equal('55066263022277343669578718895168534326250603453777594175'+
'500187360389116729240'); '500187360389116729240');
b.getY().toString().should.equal('83121579216557378445487899878180864668798711284981320763'+ b.getY().toString().should.equal('83121579216557378445487899878180864668798711284981320763'+
@ -101,7 +101,7 @@ describe('Point', function() {
it('should accurately multiply g by n+1', function() { it('should accurately multiply g by n+1', function() {
var g = Point.getG(); var g = Point.getG();
var n = Point.getN(); var n = Point.getN();
var b = g.mul(n.add(1)); var b = g.mul(n.add(new BN(1)));
b.getX().toString().should.equal('550662630222773436695787188951685343262506034537775941755'+ b.getX().toString().should.equal('550662630222773436695787188951685343262506034537775941755'+
'00187360389116729240'); '00187360389116729240');
b.getY().toString().should.equal('326705100207588169780830851305070431844712733806592432759'+ b.getY().toString().should.equal('326705100207588169780830851305070431844712733806592432759'+

View File

@ -18,8 +18,8 @@ describe('Signature', function() {
}); });
it('should work with conveniently setting r, s', function() { it('should work with conveniently setting r, s', function() {
var r = BN(); var r = new BN();
var s = BN(); var s = new BN();
var sig = new Signature(r, s); var sig = new Signature(r, s);
should.exist(sig); should.exist(sig);
sig.r.toString().should.equal(r.toString()); sig.r.toString().should.equal(r.toString());
@ -47,8 +47,8 @@ describe('Signature', function() {
blank blank
]); ]);
var sig = Signature.fromCompact(compressed); var sig = Signature.fromCompact(compressed);
sig.r.cmp(0).should.equal(0); sig.r.cmp(BN.Zero).should.equal(0);
sig.s.cmp(0).should.equal(0); sig.s.cmp(BN.Zero).should.equal(0);
}); });
}); });
@ -166,8 +166,8 @@ describe('Signature', function() {
describe('#toDER', function() { describe('#toDER', function() {
it('should convert these known r and s values into a known signature', function() { it('should convert these known r and s values into a known signature', function() {
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810'); var r = new BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130'); var s = new BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature({ var sig = new Signature({
r: r, r: r,
s: s s: s
@ -180,8 +180,8 @@ describe('Signature', function() {
describe('#toString', function() { describe('#toString', function() {
it('should convert this signature in to hex DER', function() { it('should convert this signature in to hex DER', function() {
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810'); var r = new BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130'); var s = new BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature({ var sig = new Signature({
r: r, r: r,
s: s s: s
@ -234,9 +234,9 @@ describe('Signature', function() {
}); });
describe('#hasLowS', function() { describe('#hasLowS', function() {
it('should detect high and low S', function() { it('should detect high and low S', function() {
var r = BN('63173831029936981022572627018246571655303050627048489594159321588908385378810'); var r = new BN('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = BN('4331694221846364448463828256391194279133231453999942381442030409253074198130'); var s = new BN('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var s2 = BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B2000'); var s2 = new BN('7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B2000');
var sig = new Signature({ var sig = new Signature({
r: r, r: r,
s: s s: s

View File

@ -230,7 +230,7 @@ describe('BufferReader', function() {
}); });
it('should read a 9 byte varint', function() { it('should read a 9 byte varint', function() {
var buf = BufferWriter().writeVarintBN(BN(Math.pow(2, 54).toString())).concat(); var buf = BufferWriter().writeVarintBN(new BN(Math.pow(2, 54).toString())).concat();
var br = new BufferReader(buf); var br = new BufferReader(buf);
br.readVarintBuf().length.should.equal(9); br.readVarintBuf().length.should.equal(9);
}); });
@ -259,7 +259,7 @@ describe('BufferReader', function() {
}); });
it('should throw an error on a 9 byte varint over the javascript uint precision limit', function() { it('should throw an error on a 9 byte varint over the javascript uint precision limit', function() {
var buf = BufferWriter().writeVarintBN(BN(Math.pow(2, 54).toString())).concat(); var buf = BufferWriter().writeVarintBN(new BN(Math.pow(2, 54).toString())).concat();
var br = new BufferReader(buf); var br = new BufferReader(buf);
(function() { (function() {
br.readVarintNum(); br.readVarintNum();
@ -267,7 +267,7 @@ describe('BufferReader', function() {
}); });
it('should not throw an error on a 9 byte varint not over the javascript uint precision limit', function() { it('should not throw an error on a 9 byte varint not over the javascript uint precision limit', function() {
var buf = BufferWriter().writeVarintBN(BN(Math.pow(2, 53).toString())).concat(); var buf = BufferWriter().writeVarintBN(new BN(Math.pow(2, 53).toString())).concat();
var br = new BufferReader(buf); var br = new BufferReader(buf);
(function() { (function() {
br.readVarintNum(); br.readVarintNum();

View File

@ -106,7 +106,7 @@ describe('BufferWriter', function() {
it('should write 1', function() { it('should write 1', function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeUInt64BEBN(BN(1)).concat().toString('hex').should.equal('0000000000000001'); bw.writeUInt64BEBN(new BN(1)).concat().toString('hex').should.equal('0000000000000001');
}); });
}); });
@ -115,7 +115,7 @@ describe('BufferWriter', function() {
it('should write 1', function() { it('should write 1', function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeUInt64LEBN(BN(1)).concat().toString('hex').should.equal('0100000000000000'); bw.writeUInt64LEBN(new BN(1)).concat().toString('hex').should.equal('0100000000000000');
}); });
}); });
@ -161,25 +161,25 @@ describe('BufferWriter', function() {
it('should write a 1 byte varint', function() { it('should write a 1 byte varint', function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeVarintBN(BN(1)); bw.writeVarintBN(new BN(1));
bw.concat().length.should.equal(1); bw.concat().length.should.equal(1);
}); });
it('should write a 3 byte varint', function() { it('should write a 3 byte varint', function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeVarintBN(BN(1000)); bw.writeVarintBN(new BN(1000));
bw.concat().length.should.equal(3); bw.concat().length.should.equal(3);
}); });
it('should write a 5 byte varint', function() { it('should write a 5 byte varint', function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeVarintBN(BN(Math.pow(2, 16 + 1))); bw.writeVarintBN(new BN(Math.pow(2, 16 + 1)));
bw.concat().length.should.equal(5); bw.concat().length.should.equal(5);
}); });
it('should write a 9 byte varint', function() { it('should write a 9 byte varint', function() {
var bw = new BufferWriter(); var bw = new BufferWriter();
bw.writeVarintBN(BN(Math.pow(2, 32 + 1))); bw.writeVarintBN(new BN(Math.pow(2, 32 + 1)));
bw.concat().length.should.equal(9); bw.concat().length.should.equal(9);
}); });

View File

@ -21,7 +21,7 @@ describe('Varint', function() {
//various ways to use the constructor //various ways to use the constructor
Varint(Varint(0).toBuffer()).toNumber().should.equal(0); Varint(Varint(0).toBuffer()).toNumber().should.equal(0);
Varint(0).toNumber().should.equal(0); Varint(0).toNumber().should.equal(0);
Varint(BN(0)).toNumber().should.equal(0); Varint(new BN(0)).toNumber().should.equal(0);
}); });
describe('#set', function() { describe('#set', function() {
@ -80,7 +80,7 @@ describe('Varint', function() {
describe('#fromBN', function() { describe('#fromBN', function() {
it('should set a number', function() { it('should set a number', function() {
var varint = Varint().fromBN(BN(5)); var varint = Varint().fromBN(new BN(5));
varint.toNumber().should.equal(5); varint.toNumber().should.equal(5);
}); });
@ -109,7 +109,7 @@ describe('Varint', function() {
it('should return a buffer', function() { it('should return a buffer', function() {
var varint = Varint(5); var varint = Varint(5);
varint.toBN().toString().should.equal(BN(5).toString()); varint.toBN().toString().should.equal(new BN(5).toString());
}); });
}); });

View File

@ -129,13 +129,13 @@ describe('PrivateKey', function() {
it('should not be able to instantiate with unknown network', function() { it('should not be able to instantiate with unknown network', function() {
expect(function() { expect(function() {
return new PrivateKey(BN(2), 'unknown'); return new PrivateKey(new BN(2), 'unknown');
}).to.throw('Must specify the network ("livenet" or "testnet")'); }).to.throw('Must specify the network ("livenet" or "testnet")');
}); });
it('should not create a zero private key', function() { it('should not create a zero private key', function() {
expect(function() { expect(function() {
var bn = BN(0); var bn = new BN(0);
return new PrivateKey(bn); return new PrivateKey(bn);
}).to.throw(TypeError); }).to.throw(TypeError);
}); });
@ -295,7 +295,7 @@ describe('PrivateKey', function() {
it('should set bn gt 0 and lt n, and should be compressed', function() { it('should set bn gt 0 and lt n, and should be compressed', function() {
var privkey = PrivateKey.fromRandom(); var privkey = PrivateKey.fromRandom();
privkey.bn.gt(BN(0)).should.equal(true); privkey.bn.gt(new BN(0)).should.equal(true);
privkey.bn.lt(Point.getN()).should.equal(true); privkey.bn.lt(Point.getN()).should.equal(true);
privkey.compressed.should.equal(true); privkey.compressed.should.equal(true);
}); });
@ -343,7 +343,7 @@ describe('PrivateKey', function() {
it('should convert this known PrivateKey to known PublicKey', function() { it('should convert this known PrivateKey to known PublicKey', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex'))); var privkey = new PrivateKey(new BN(new Buffer(privhex, 'hex')));
var pubkey = privkey.toPublicKey(); var pubkey = privkey.toPublicKey();
pubkey.toString().should.equal(pubhex); pubkey.toString().should.equal(pubhex);
}); });
@ -351,7 +351,7 @@ describe('PrivateKey', function() {
it('should have a "publicKey" property', function() { it('should have a "publicKey" property', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex'))); var privkey = new PrivateKey(new BN(new Buffer(privhex, 'hex')));
privkey.publicKey.toString().should.equal(pubhex); privkey.publicKey.toString().should.equal(pubhex);
}); });

View File

@ -49,7 +49,7 @@ describe('PublicKey', function() {
it('from a private key', function() { it('from a private key', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex'))); var privkey = new PrivateKey(new BN(new Buffer(privhex, 'hex')));
var pk = new PublicKey(privkey); var pk = new PublicKey(privkey);
pk.toString().should.equal(pubhex); pk.toString().should.equal(pubhex);
}); });

View File

@ -46,7 +46,7 @@ Script.fromBitcoindString = function(str) {
opcodenum = Opcode[opstr]; opcodenum = Opcode[opstr];
bw.writeUInt8(opcodenum); bw.writeUInt8(opcodenum);
} else if (!isNaN(parseInt(token))) { } else if (!isNaN(parseInt(token))) {
var script = Script().add(BN(token).toScriptNumBuffer()); var script = Script().add(new BN(token).toScriptNumBuffer());
tbuf = script.toBuffer(); tbuf = script.toBuffer();
bw.write(tbuf); bw.write(tbuf);
} else { } else {
@ -77,21 +77,21 @@ describe('Interpreter', function() {
describe('@castToBool', function() { describe('@castToBool', function() {
it('should cast these bufs to bool correctly', function() { it('should cast these bufs to bool correctly', function() {
Interpreter.castToBool(BN(0).toSM({ Interpreter.castToBool(new BN(0).toSM({
endian: 'little' endian: 'little'
})).should.equal(false); })).should.equal(false);
Interpreter.castToBool(new Buffer('0080', 'hex')).should.equal(false); //negative 0 Interpreter.castToBool(new Buffer('0080', 'hex')).should.equal(false); //negative 0
Interpreter.castToBool(BN(1).toSM({ Interpreter.castToBool(new BN(1).toSM({
endian: 'little' endian: 'little'
})).should.equal(true); })).should.equal(true);
Interpreter.castToBool(BN(-1).toSM({ Interpreter.castToBool(new BN(-1).toSM({
endian: 'little' endian: 'little'
})).should.equal(true); })).should.equal(true);
var buf = new Buffer('00', 'hex'); var buf = new Buffer('00', 'hex');
var bool = BN.fromSM(buf, { var bool = BN.fromSM(buf, {
endian: 'little' endian: 'little'
}).cmp(0) !== 0; }).cmp(BN.Zero) !== 0;
Interpreter.castToBool(buf).should.equal(bool); Interpreter.castToBool(buf).should.equal(bool);
}); });