Browse Source

simplify use of bignum in ScriptInterpreter

patch-2
Ryan X. Charles 11 years ago
parent
commit
d035b54418
  1. 38
      lib/ScriptInterpreter.js

38
lib/ScriptInterpreter.js

@ -242,7 +242,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
case Opcode.map.OP_DEPTH: case Opcode.map.OP_DEPTH:
// -- stacksize // -- stacksize
var value = new bignum(this.stack.length); var value = bignum(this.stack.length);
this.stack.push(intToBufferSM(value)); this.stack.push(intToBufferSM(value));
break; break;
@ -351,7 +351,7 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
case Opcode.map.OP_SIZE: case Opcode.map.OP_SIZE:
// (in -- in size) // (in -- in size)
var value = new bignum(this.stackTop().length); var value = bignum(this.stackTop().length);
this.stack.push(intToBufferSM(value)); this.stack.push(intToBufferSM(value));
break; break;
@ -427,16 +427,16 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
var num = bufferSMToInt(this.stackTop()); var num = bufferSMToInt(this.stackTop());
switch (opcode) { switch (opcode) {
case Opcode.map.OP_1ADD: case Opcode.map.OP_1ADD:
num = num.add(new bignum(1)); num = num.add(1);
break; break;
case Opcode.map.OP_1SUB: case Opcode.map.OP_1SUB:
num = num.sub(new bignum(1)); num = num.sub(1);
break; break;
case Opcode.map.OP_2MUL: case Opcode.map.OP_2MUL:
num = num.mul(new bignum(2)); num = num.mul(2);
break; break;
case Opcode.map.OP_2DIV: case Opcode.map.OP_2DIV:
num = num.div(new bignum(2)); num = num.div(2);
break; break;
case Opcode.map.OP_NEGATE: case Opcode.map.OP_NEGATE:
num = num.neg(); num = num.neg();
@ -445,10 +445,10 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
num = num.abs(); num = num.abs();
break; break;
case Opcode.map.OP_NOT: case Opcode.map.OP_NOT:
num = new bignum(num.cmp(new bignum(0)) == 0 ? 1 : 0); num = bignum(num.cmp(0) == 0 ? 1 : 0);
break; break;
case Opcode.map.OP_0NOTEQUAL: case Opcode.map.OP_0NOTEQUAL:
num = new bignum(num.cmp(new bignum(0)) == 0 ? 0 : 1); num = bignum(num.cmp(0) == 0 ? 0 : 1);
break; break;
} }
this.stack[this.stack.length - 1] = intToBufferSM(num); this.stack[this.stack.length - 1] = intToBufferSM(num);
@ -494,51 +494,51 @@ ScriptInterpreter.prototype.eval = function eval(script, tx, inIndex, hashType,
break; break;
case Opcode.map.OP_LSHIFT: case Opcode.map.OP_LSHIFT:
if (v2.cmp(new bignum(0)) < 0 || v2.cmp(new bignum(2048)) > 0) { if (v2.cmp(0) < 0 || v2.cmp(bignum(2048)) > 0) {
throw new Error("OP_LSHIFT parameter out of bounds"); throw new Error("OP_LSHIFT parameter out of bounds");
} }
num = v1.shiftLeft(v2); num = v1.shiftLeft(v2);
break; break;
case Opcode.map.OP_RSHIFT: case Opcode.map.OP_RSHIFT:
if (v2.cmp(new bignum(0)) < 0 || v2.cmp(new bignum(2048)) > 0) { if (v2.cmp(0) < 0 || v2.cmp(bignum(2048)) > 0) {
throw new Error("OP_RSHIFT parameter out of bounds"); throw new Error("OP_RSHIFT parameter out of bounds");
} }
num = v1.shiftRight(v2); num = v1.shiftRight(v2);
break; break;
case Opcode.map.OP_BOOLAND: case Opcode.map.OP_BOOLAND:
num = new bignum((v1.cmp(new bignum(0)) != 0 && v2.cmp(new bignum(0)) != 0) ? 1 : 0); num = bignum((v1.cmp(0) != 0 && v2.cmp(0) != 0) ? 1 : 0);
break; break;
case Opcode.map.OP_BOOLOR: case Opcode.map.OP_BOOLOR:
num = new bignum((v1.cmp(new bignum(0)) != 0 || v2.cmp(new bignum(0)) != 0) ? 1 : 0); num = bignum((v1.cmp(0) != 0 || v2.cmp(0) != 0) ? 1 : 0);
break; break;
case Opcode.map.OP_NUMEQUAL: case Opcode.map.OP_NUMEQUAL:
case Opcode.map.OP_NUMEQUALVERIFY: case Opcode.map.OP_NUMEQUALVERIFY:
num = new bignum(v1.cmp(v2) == 0 ? 1 : 0); num = bignum(v1.cmp(v2) == 0 ? 1 : 0);
break; break;
case Opcode.map.OP_NUMNOTEQUAL: case Opcode.map.OP_NUMNOTEQUAL:
; ;
num = new bignum(v1.cmp(v2) != 0 ? 1 : 0); num = bignum(v1.cmp(v2) != 0 ? 1 : 0);
break; break;
case Opcode.map.OP_LESSTHAN: case Opcode.map.OP_LESSTHAN:
num = new bignum(v1.lt(v2) ? 1 : 0); num = bignum(v1.lt(v2) ? 1 : 0);
break; break;
case Opcode.map.OP_GREATERTHAN: case Opcode.map.OP_GREATERTHAN:
num = new bignum(v1.gt(v2) ? 1 : 0); num = bignum(v1.gt(v2) ? 1 : 0);
break; break;
case Opcode.map.OP_LESSTHANOREQUAL: case Opcode.map.OP_LESSTHANOREQUAL:
num = new bignum(v1.gt(v2) ? 0 : 1); num = bignum(v1.gt(v2) ? 0 : 1);
break; break;
case Opcode.map.OP_GREATERTHANOREQUAL: case Opcode.map.OP_GREATERTHANOREQUAL:
num = new bignum(v1.lt(v2) ? 0 : 1); num = bignum(v1.lt(v2) ? 0 : 1);
break; break;
case Opcode.map.OP_MIN: case Opcode.map.OP_MIN:
@ -835,7 +835,7 @@ ScriptInterpreter.prototype.getPrimitiveStack = function getPrimitiveStack() {
return buffertools.toHex(chunk.slice(0)); return buffertools.toHex(chunk.slice(0));
} }
var num = bufferSMToInt(chunk); var num = bufferSMToInt(chunk);
if (num.cmp(new bignum(-128)) >= 0 && num.cmp(new bignum(127)) <= 0) { if (num.cmp(-128) >= 0 && num.cmp(127) <= 0) {
return num.toNumber(); return num.toNumber();
} else { } else {
return buffertools.toHex(chunk.slice(0)); return buffertools.toHex(chunk.slice(0));

Loading…
Cancel
Save