diff --git a/ScriptInterpreter.js b/ScriptInterpreter.js index 27a217e..d1992c7 100644 --- a/ScriptInterpreter.js +++ b/ScriptInterpreter.js @@ -119,7 +119,10 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_14: case OP_15: case OP_16: - this.stack.push(bigintToBuffer(opcode - OP_1 + 1)); + var opint = opcode - OP_1 + 1; + var opbuf = bigintToBuffer(opint); + console.log('op'+opcode+' = '+opint+', '+buffertools.toHex(opbuf)); + this.stack.push(opbuf); break; case OP_NOP: @@ -352,8 +355,6 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, case OP_SIZE: // (in -- in size) var value = bignum(this.stackTop().length); - //var topSize = util.bytesNeededToStore(castBigint(this.stackTop()).toNumber()); - //var value = bignum(topSize); this.stack.push(bigintToBuffer(value)); break; @@ -396,6 +397,10 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, // (x1 x2 - bool) var v1 = this.stackTop(2); var v2 = this.stackTop(1); + console.log('equal'); + console.log(v1); + console.log(v2); + var value = buffertools.compare(v1, v2) === 0; // OP_NOTEQUAL is disabled because it would be too easy to say @@ -476,6 +481,9 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType, // (x1 x2 -- out) var v1 = castBigint(this.stackTop(2)); var v2 = castBigint(this.stackTop(1)); + console.log('add'); + console.log(v1); + console.log(v2); var num; switch (opcode) { case OP_ADD: @@ -874,7 +882,6 @@ var castBigint = ScriptInterpreter.castBigint = function castBigint(v) { if (!v.length) { return bignum(0); } - // Arithmetic operands must be in range [-2^31...2^31] if (v.length > 4) { throw new Error("Bigint cast overflow (> 4 bytes)"); @@ -883,46 +890,55 @@ var castBigint = ScriptInterpreter.castBigint = function castBigint(v) { var w = new Buffer(v.length); v.copy(w); w = buffertools.reverse(w); - if (w[0] & 0x80) { - w[0] &= 0x7f; - return bignum.fromBuffer(w).neg(); + console.log('v ='+buffertools.toHex(w)); + var isNeg = w[0] & 0x80; + if (isNeg) { + for (var i = 0; i>> 0 - }; -}; var fitsInNBits = function(integer, n) { // TODO: make this efficient!!! return integer.toString(2).replace('-','').length < n; @@ -127,6 +121,21 @@ exports.bytesNeededToStore = bytesNeededToStore = function(integer) { return Math.ceil(((integer).toString(2).replace('-','').length + 1)/ 8); }; +exports.negativeBuffer = negativeBuffer = function(b) { + // implement two-complement negative + var c = new Buffer(b.length); + // negate each byte + for (var i=0; i=0; i--){ + c[i] += 1; + if (c[i] !== 0) break; + } + console.log('negative of '+buffertools.toHex(b)+' is '+buffertools.toHex(c)); + return c; +} exports.intToBuffer = function(integer) { var size = bytesNeededToStore(integer); @@ -139,27 +148,15 @@ exports.intToBuffer = function(integer) { if (si.lenght === 1) { si = '0' + si; } - buf.word8((neg?-1:1)*parseInt(si, 16)); + buf.word8(parseInt(si, 16)); } - return buf.buffer(); - - var data = null; - if (fitsInNBits(integer, 8)) { - data = new Buffer(1); - data.writeInt8(integer, 0); - } else if (fitsInNBits(integer, 16)) { - data = new Buffer(2); - data.writeInt16LE(integer, 0); - } else if (fitsInNBits(integer, 32)) { - data = new Buffer(4); - data.writeInt32LE(integer, 0); - } else { - var x = intTo64Bits(integer); - data = new Buffer(8); - data.writeInt32LE(x.hi, 0); // high part contains sign information (signed) - data.writeUInt32LE(x.lo, 4); // low part encoded as unsigned integer + var ret = buf.buffer(); + if (neg) { + ret = buffertools.reverse(ret); + ret = negativeBuffer(ret); + ret = buffertools.reverse(ret); } - return data; + return ret; }; var formatValue = exports.formatValue = function (valueBuffer) {