Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/script/opcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ class Opcode {

toNum(minimal, limit) {
if (this.value === opcodes.OP_0)
return ScriptNum.fromInt(0);
return ScriptNum.fromBigInt(0n);

if (this.value === opcodes.OP_1NEGATE)
return ScriptNum.fromInt(-1);
return ScriptNum.fromBigInt(-1n);

if (this.value >= opcodes.OP_1 && this.value <= opcodes.OP_16)
return ScriptNum.fromInt(this.value - 0x50);
return ScriptNum.fromBigInt(BigInt(this.value - 0x50));

if (!this.data)
return null;
Expand Down
91 changes: 51 additions & 40 deletions lib/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -897,37 +897,38 @@ class Script extends bio.Struct {
if (stack.length < 1)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);

let num = stack.getNum(-1, minimal, 4);
let binum = stack.getNum(-1, minimal, 4).toBigInt();
let cmp;

switch (op.value) {
case opcodes.OP_1ADD:
num.iaddn(1);
binum = bigInt64(binum + 1n);
break;
case opcodes.OP_1SUB:
num.isubn(1);
binum = bigInt64(binum - 1n);
break;
case opcodes.OP_NEGATE:
num.ineg();
binum = bigInt64(-binum);
break;
case opcodes.OP_ABS:
num.iabs();
if (binum < 0n)
binum = bigInt64(-binum);
break;
case opcodes.OP_NOT:
cmp = num.isZero();
num = ScriptNum.fromBool(cmp);
cmp = binum === 0n;
binum = cmp ? 1n : 0n;
break;
case opcodes.OP_0NOTEQUAL:
cmp = !num.isZero();
num = ScriptNum.fromBool(cmp);
cmp = binum !== 0n;
binum = BigInt(cmp);
break;
default:
assert(false, 'Fatal script error.');
break;
}

stack.pop();
stack.pushNum(num);
stack.pushNum(ScriptNum.fromBigInt(binum));

break;
}
Expand All @@ -947,59 +948,59 @@ class Script extends bio.Struct {
if (stack.length < 2)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);

const n1 = stack.getNum(-2, minimal, 4);
const n2 = stack.getNum(-1, minimal, 4);
const bn1 = stack.getNum(-2, minimal, 4).toBigInt();
const bn2 = stack.getNum(-1, minimal, 4).toBigInt();

let num, cmp;
let binum, cmp;

switch (op.value) {
case opcodes.OP_ADD:
num = n1.iadd(n2);
binum = bigInt64(bn1 + bn2);
break;
case opcodes.OP_SUB:
num = n1.isub(n2);
binum = bigInt64(bn1 - bn2);
break;
case opcodes.OP_BOOLAND:
cmp = n1.toBool() && n2.toBool();
num = ScriptNum.fromBool(cmp);
cmp = bn1 !== 0n && bn2 !== 0n;
binum = BigInt(cmp);
break;
case opcodes.OP_BOOLOR:
cmp = n1.toBool() || n2.toBool();
num = ScriptNum.fromBool(cmp);
cmp = bn1 !== 0n || bn2 !== 0n;
binum = BigInt(cmp);
break;
case opcodes.OP_NUMEQUAL:
cmp = n1.eq(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 === bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_NUMEQUALVERIFY:
cmp = n1.eq(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 === bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_NUMNOTEQUAL:
cmp = !n1.eq(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 !== bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_LESSTHAN:
cmp = n1.lt(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 < bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_GREATERTHAN:
cmp = n1.gt(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 > bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_LESSTHANOREQUAL:
cmp = n1.lte(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 <= bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_GREATERTHANOREQUAL:
cmp = n1.gte(n2);
num = ScriptNum.fromBool(cmp);
cmp = bn1 >= bn2;
binum = BigInt(cmp);
break;
case opcodes.OP_MIN:
num = ScriptNum.min(n1, n2);
binum = bn1 < bn2 ? bn1 : bn2;
break;
case opcodes.OP_MAX:
num = ScriptNum.max(n1, n2);
binum = bn1 > bn2 ? bn1 : bn2;
break;
default:
assert(false, 'Fatal script error.');
Expand All @@ -1008,7 +1009,7 @@ class Script extends bio.Struct {

stack.pop();
stack.pop();
stack.pushNum(num);
stack.pushNum(ScriptNum.fromBigInt(binum));

if (op.value === opcodes.OP_NUMEQUALVERIFY) {
if (!stack.getBool(-1))
Expand All @@ -1022,11 +1023,11 @@ class Script extends bio.Struct {
if (stack.length < 3)
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);

const n1 = stack.getNum(-3, minimal, 4);
const n2 = stack.getNum(-2, minimal, 4);
const n3 = stack.getNum(-1, minimal, 4);
const n1 = stack.getNum(-3, minimal, 4).toBigInt();
const n2 = stack.getNum(-2, minimal, 4).toBigInt();
const n3 = stack.getNum(-1, minimal, 4).toBigInt();

const val = n2.lte(n1) && n1.lt(n3);
const val = n2 <= n1 && n1 < n3;

stack.pop();
stack.pop();
Expand Down Expand Up @@ -2457,6 +2458,16 @@ function checksig(msg, sig, key) {
return secp256k1.verify(msg, sig.slice(0, -1), key);
}

/**
* Make sure number is int64.
* @param {BigInt} value
* @returns {BigInt}
*/

function bigInt64(value) {
return BigInt.asIntN(64, value);
}

/*
* Expose
*/
Expand Down
Loading