Skip to content

Commit 407b9d0

Browse files
committed
pkg: use bigint instead of n64.
1 parent 4e87fb1 commit 407b9d0

File tree

6 files changed

+377
-94
lines changed

6 files changed

+377
-94
lines changed

lib/script/opcode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,13 @@ class Opcode {
212212

213213
toNum(minimal, limit) {
214214
if (this.value === opcodes.OP_0)
215-
return ScriptNum.fromInt(0);
215+
return ScriptNum.fromBigInt(0n);
216216

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

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

223223
if (!this.data)
224224
return null;

lib/script/script.js

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -897,37 +897,38 @@ class Script extends bio.Struct {
897897
if (stack.length < 1)
898898
throw new ScriptError('INVALID_STACK_OPERATION', op, ip);
899899

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

903903
switch (op.value) {
904904
case opcodes.OP_1ADD:
905-
num.iaddn(1);
905+
binum = bigInt64(binum + 1n);
906906
break;
907907
case opcodes.OP_1SUB:
908-
num.isubn(1);
908+
binum = bigInt64(binum - 1n);
909909
break;
910910
case opcodes.OP_NEGATE:
911-
num.ineg();
911+
binum = bigInt64(-binum);
912912
break;
913913
case opcodes.OP_ABS:
914-
num.iabs();
914+
if (binum < 0n)
915+
binum = bigInt64(-binum);
915916
break;
916917
case opcodes.OP_NOT:
917-
cmp = num.isZero();
918-
num = ScriptNum.fromBool(cmp);
918+
cmp = binum === 0n;
919+
binum = cmp ? 1n : 0n;
919920
break;
920921
case opcodes.OP_0NOTEQUAL:
921-
cmp = !num.isZero();
922-
num = ScriptNum.fromBool(cmp);
922+
cmp = binum !== 0n;
923+
binum = BigInt(cmp);
923924
break;
924925
default:
925926
assert(false, 'Fatal script error.');
926927
break;
927928
}
928929

929930
stack.pop();
930-
stack.pushNum(num);
931+
stack.pushNum(ScriptNum.fromBigInt(binum));
931932

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

950-
const n1 = stack.getNum(-2, minimal, 4);
951-
const n2 = stack.getNum(-1, minimal, 4);
951+
const bn1 = stack.getNum(-2, minimal, 4).toBigInt();
952+
const bn2 = stack.getNum(-1, minimal, 4).toBigInt();
952953

953-
let num, cmp;
954+
let binum, cmp;
954955

955956
switch (op.value) {
956957
case opcodes.OP_ADD:
957-
num = n1.iadd(n2);
958+
binum = bigInt64(bn1 + bn2);
958959
break;
959960
case opcodes.OP_SUB:
960-
num = n1.isub(n2);
961+
binum = bigInt64(bn1 - bn2);
961962
break;
962963
case opcodes.OP_BOOLAND:
963-
cmp = n1.toBool() && n2.toBool();
964-
num = ScriptNum.fromBool(cmp);
964+
cmp = bn1 !== 0n && bn2 !== 0n;
965+
binum = BigInt(cmp);
965966
break;
966967
case opcodes.OP_BOOLOR:
967-
cmp = n1.toBool() || n2.toBool();
968-
num = ScriptNum.fromBool(cmp);
968+
cmp = bn1 !== 0n || bn2 !== 0n;
969+
binum = BigInt(cmp);
969970
break;
970971
case opcodes.OP_NUMEQUAL:
971-
cmp = n1.eq(n2);
972-
num = ScriptNum.fromBool(cmp);
972+
cmp = bn1 === bn2;
973+
binum = BigInt(cmp);
973974
break;
974975
case opcodes.OP_NUMEQUALVERIFY:
975-
cmp = n1.eq(n2);
976-
num = ScriptNum.fromBool(cmp);
976+
cmp = bn1 === bn2;
977+
binum = BigInt(cmp);
977978
break;
978979
case opcodes.OP_NUMNOTEQUAL:
979-
cmp = !n1.eq(n2);
980-
num = ScriptNum.fromBool(cmp);
980+
cmp = bn1 !== bn2;
981+
binum = BigInt(cmp);
981982
break;
982983
case opcodes.OP_LESSTHAN:
983-
cmp = n1.lt(n2);
984-
num = ScriptNum.fromBool(cmp);
984+
cmp = bn1 < bn2;
985+
binum = BigInt(cmp);
985986
break;
986987
case opcodes.OP_GREATERTHAN:
987-
cmp = n1.gt(n2);
988-
num = ScriptNum.fromBool(cmp);
988+
cmp = bn1 > bn2;
989+
binum = BigInt(cmp);
989990
break;
990991
case opcodes.OP_LESSTHANOREQUAL:
991-
cmp = n1.lte(n2);
992-
num = ScriptNum.fromBool(cmp);
992+
cmp = bn1 <= bn2;
993+
binum = BigInt(cmp);
993994
break;
994995
case opcodes.OP_GREATERTHANOREQUAL:
995-
cmp = n1.gte(n2);
996-
num = ScriptNum.fromBool(cmp);
996+
cmp = bn1 >= bn2;
997+
binum = BigInt(cmp);
997998
break;
998999
case opcodes.OP_MIN:
999-
num = ScriptNum.min(n1, n2);
1000+
binum = bn1 < bn2 ? bn1 : bn2;
10001001
break;
10011002
case opcodes.OP_MAX:
1002-
num = ScriptNum.max(n1, n2);
1003+
binum = bn1 > bn2 ? bn1 : bn2;
10031004
break;
10041005
default:
10051006
assert(false, 'Fatal script error.');
@@ -1008,7 +1009,7 @@ class Script extends bio.Struct {
10081009

10091010
stack.pop();
10101011
stack.pop();
1011-
stack.pushNum(num);
1012+
stack.pushNum(ScriptNum.fromBigInt(binum));
10121013

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

1025-
const n1 = stack.getNum(-3, minimal, 4);
1026-
const n2 = stack.getNum(-2, minimal, 4);
1027-
const n3 = stack.getNum(-1, minimal, 4);
1026+
const n1 = stack.getNum(-3, minimal, 4).toBigInt();
1027+
const n2 = stack.getNum(-2, minimal, 4).toBigInt();
1028+
const n3 = stack.getNum(-1, minimal, 4).toBigInt();
10281029

1029-
const val = n2.lte(n1) && n1.lt(n3);
1030+
const val = n2 <= n1 && n1 < n3;
10301031

10311032
stack.pop();
10321033
stack.pop();
@@ -2457,6 +2458,16 @@ function checksig(msg, sig, key) {
24572458
return secp256k1.verify(msg, sig.slice(0, -1), key);
24582459
}
24592460

2461+
/**
2462+
* Make sure number is int64.
2463+
* @param {BigInt} value
2464+
* @returns {BigInt}
2465+
*/
2466+
2467+
function bigInt64(value) {
2468+
return BigInt.asIntN(64, value);
2469+
}
2470+
24602471
/*
24612472
* Expose
24622473
*/

0 commit comments

Comments
 (0)