@cashscript/utils 0.11.2 → 0.11.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- declare const _default: "\n# This file can be run with CashProof to prove that the optimisations preserve exact functionality\n# This includes most of CashScript's bytecode optimisations, although some are incompatible with CashProof\n\n# Hardcoded arithmetic\n# OP_NOT OP_IF <=> OP_NOTIF;\nOP_1 OP_ADD <=> OP_1ADD;\nOP_1 OP_SUB <=> OP_1SUB;\nOP_1 OP_NEGATE <=> OP_1NEGATE;\nOP_0 OP_NUMEQUAL OP_NOT <=> OP_0NOTEQUAL;\nOP_NUMEQUAL OP_NOT <=> OP_NUMNOTEQUAL;\nOP_SHA256 OP_SHA256 <=> OP_HASH256;\nOP_SHA256 OP_RIPEMD160 <=> OP_HASH160;\n\n# Hardcoded stack ops\nOP_2 OP_PICK OP_1 OP_PICK OP_3 OP_PICK <=> OP_3DUP OP_SWAP;\nOP_2 OP_PICK OP_2 OP_PICK OP_2 OP_PICK <=> OP_3DUP;\n\nOP_0 OP_PICK OP_2 OP_PICK <=> OP_2DUP OP_SWAP;\nOP_2 OP_PICK OP_4 OP_PICK <=> OP_2OVER OP_SWAP;\nOP_3 OP_PICK OP_3 OP_PICK <=> OP_2OVER;\n\nOP_2 OP_ROLL OP_3 OP_ROLL <=> OP_2SWAP OP_SWAP;\nOP_3 OP_ROLL OP_3 OP_ROLL <=> OP_2SWAP;\nOP_4 OP_ROLL OP_5 OP_ROLL <=> OP_2ROT OP_SWAP;\nOP_5 OP_ROLL OP_5 OP_ROLL <=> OP_2ROT;\n\nOP_0 OP_PICK <=> OP_DUP;\nOP_1 OP_PICK <=> OP_OVER;\nOP_0 OP_ROLL <=> ;\nOP_1 OP_ROLL <=> OP_SWAP;\nOP_2 OP_ROLL <=> OP_ROT;\nOP_DROP OP_DROP <=> OP_2DROP;\n\n# Secondary effects\nOP_DUP OP_SWAP <=> OP_DUP;\nOP_SWAP OP_SWAP <=> ;\nOP_2SWAP OP_2SWAP <=> ;\nOP_ROT OP_ROT OP_ROT <=> ;\nOP_2ROT OP_2ROT OP_2ROT <=> ;\nOP_OVER OP_OVER <=> OP_2DUP;\nOP_DUP OP_DROP <=> ;\nOP_DUP OP_NIP <=> ;\n\n# Enabling secondary effects\nOP_DUP OP_OVER <=> OP_DUP OP_DUP;\n\n# Merge OP_VERIFY\nOP_EQUAL OP_VERIFY <=> OP_EQUALVERIFY;\nOP_NUMEQUAL OP_VERIFY <=> OP_NUMEQUALVERIFY;\nOP_CHECKSIG OP_VERIFY <=> OP_CHECKSIGVERIFY;\n# OP_CHECKMULTISIG OP_VERIFY <=> OP_CHECKMULTISIGVERIFY;\nOP_CHECKDATASIG OP_VERIFY <=> OP_CHECKDATASIGVERIFY;\n\n# Remove/replace extraneous OP_SWAP\n# OP_SWAP OP_AND <=> OP_AND;\n# OP_SWAP OP_OR <=> OP_OR;\n# OP_SWAP OP_XOR <=> OP_XOR;\nOP_SWAP OP_ADD <=> OP_ADD;\nOP_SWAP OP_EQUAL <=> OP_EQUAL;\nOP_SWAP OP_NUMEQUAL <=> OP_NUMEQUAL;\nOP_SWAP OP_NUMNOTEQUAL <=> OP_NUMNOTEQUAL;\nOP_SWAP OP_GREATERTHANOREQUAL <=> OP_LESSTHANOREQUAL;\nOP_SWAP OP_LESSTHANOREQUAL <=> OP_GREATERTHANOREQUAL;\nOP_SWAP OP_GREATERTHAN <=> OP_LESSTHAN;\nOP_SWAP OP_LESSTHAN <=> OP_GREATERTHAN;\nOP_SWAP OP_DROP <=> OP_NIP;\nOP_SWAP OP_NIP <=> OP_DROP;\n\n# Remove/replace extraneous OP_DUP\n# OP_DUP OP_AND <=> ;\n# OP_DUP OP_OR <=> ;\nOP_DUP OP_DROP <=> ;\nOP_DUP OP_NIP <=> ;\n\n# Random optimisations (don't know what I'm targeting with this)\nOP_2DUP OP_DROP <=> OP_OVER;\nOP_2DUP OP_NIP <=> OP_DUP;\nOP_CAT OP_DROP <=> OP_2DROP;\nOP_NIP OP_DROP <=> OP_2DROP;\n\n# Far-fetched stuff\nOP_DUP OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_OVER OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_2 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_3 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_4 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_5 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_6 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_7 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_8 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_9 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_10 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_11 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_12 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_13 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_14 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_15 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_16 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\n\nOP_DUP OP_ROT OP_DROP <=> OP_NIP OP_DUP;\nOP_OVER OP_ROT OP_DROP <=> OP_SWAP;\nOP_2 OP_PICK OP_ROT OP_DROP <=> OP_NIP OP_OVER;\n\nOP_0 OP_NIP <=> OP_DROP OP_0;\nOP_1 OP_NIP <=> OP_DROP OP_1;\nOP_2 OP_NIP <=> OP_DROP OP_2;\nOP_3 OP_NIP <=> OP_DROP OP_3;\nOP_4 OP_NIP <=> OP_DROP OP_4;\nOP_5 OP_NIP <=> OP_DROP OP_5;\nOP_6 OP_NIP <=> OP_DROP OP_6;\nOP_7 OP_NIP <=> OP_DROP OP_7;\nOP_8 OP_NIP <=> OP_DROP OP_8;\nOP_9 OP_NIP <=> OP_DROP OP_9;\nOP_10 OP_NIP <=> OP_DROP OP_10;\nOP_11 OP_NIP <=> OP_DROP OP_11;\nOP_12 OP_NIP <=> OP_DROP OP_12;\nOP_13 OP_NIP <=> OP_DROP OP_13;\nOP_14 OP_NIP <=> OP_DROP OP_14;\nOP_15 OP_NIP <=> OP_DROP OP_15;\nOP_16 OP_NIP <=> OP_DROP OP_16;\n\nOP_2 OP_PICK OP_SWAP OP_2 OP_PICK OP_NIP <=> OP_DROP OP_2DUP;\n";
1
+ declare const _default: "\n# This file can be run with CashProof to prove that the optimisations preserve exact functionality\n# This includes most of CashScript's bytecode optimisations, although some are incompatible with CashProof\n\n# Hardcoded arithmetic\n# OP_NOT OP_IF <=> OP_NOTIF;\nOP_1 OP_ADD <=> OP_1ADD;\nOP_1 OP_SUB <=> OP_1SUB;\nOP_1 OP_NEGATE <=> OP_1NEGATE;\nOP_0 OP_NUMEQUAL OP_NOT <=> OP_0NOTEQUAL;\nOP_NUMEQUAL OP_NOT <=> OP_NUMNOTEQUAL;\nOP_SHA256 OP_SHA256 <=> OP_HASH256;\nOP_SHA256 OP_RIPEMD160 <=> OP_HASH160;\n\n# Hardcoded stack ops\nOP_2 OP_PICK OP_1 OP_PICK OP_3 OP_PICK <=> OP_3DUP OP_SWAP;\nOP_2 OP_PICK OP_2 OP_PICK OP_2 OP_PICK <=> OP_3DUP;\n\nOP_0 OP_PICK OP_2 OP_PICK <=> OP_2DUP OP_SWAP;\nOP_2 OP_PICK OP_4 OP_PICK <=> OP_2OVER OP_SWAP;\nOP_3 OP_PICK OP_3 OP_PICK <=> OP_2OVER;\n\nOP_2 OP_ROLL OP_3 OP_ROLL <=> OP_2SWAP OP_SWAP;\nOP_3 OP_ROLL OP_3 OP_ROLL <=> OP_2SWAP;\nOP_4 OP_ROLL OP_5 OP_ROLL <=> OP_2ROT OP_SWAP;\nOP_5 OP_ROLL OP_5 OP_ROLL <=> OP_2ROT;\n\nOP_0 OP_PICK <=> OP_DUP;\nOP_1 OP_PICK <=> OP_OVER;\nOP_0 OP_ROLL <=> ;\nOP_1 OP_ROLL <=> OP_SWAP;\nOP_2 OP_ROLL <=> OP_ROT;\nOP_DROP OP_DROP <=> OP_2DROP;\n\n# Secondary effects\nOP_DUP OP_SWAP <=> OP_DUP;\nOP_SWAP OP_SWAP <=> ;\nOP_2SWAP OP_2SWAP <=> ;\nOP_ROT OP_ROT OP_ROT <=> ;\nOP_2ROT OP_2ROT OP_2ROT <=> ;\nOP_OVER OP_OVER <=> OP_2DUP;\nOP_DUP OP_DROP <=> ;\nOP_DUP OP_NIP <=> ;\n\n# Enabling secondary effects\nOP_DUP OP_OVER <=> OP_DUP OP_DUP;\n\n# Merge OP_VERIFY\nOP_EQUAL OP_VERIFY <=> OP_EQUALVERIFY;\nOP_NUMEQUAL OP_VERIFY <=> OP_NUMEQUALVERIFY;\nOP_CHECKSIG OP_VERIFY <=> OP_CHECKSIGVERIFY;\n# OP_CHECKMULTISIG OP_VERIFY <=> OP_CHECKMULTISIGVERIFY;\nOP_CHECKDATASIG OP_VERIFY <=> OP_CHECKDATASIGVERIFY;\n\n# Remove/replace extraneous OP_SWAP\n# OP_SWAP OP_AND <=> OP_AND;\n# OP_SWAP OP_OR <=> OP_OR;\n# OP_SWAP OP_XOR <=> OP_XOR;\nOP_SWAP OP_ADD <=> OP_ADD;\nOP_SWAP OP_EQUAL <=> OP_EQUAL;\nOP_SWAP OP_NUMEQUAL <=> OP_NUMEQUAL;\nOP_SWAP OP_NUMNOTEQUAL <=> OP_NUMNOTEQUAL;\nOP_SWAP OP_GREATERTHANOREQUAL <=> OP_LESSTHANOREQUAL;\nOP_SWAP OP_LESSTHANOREQUAL <=> OP_GREATERTHANOREQUAL;\nOP_SWAP OP_GREATERTHAN <=> OP_LESSTHAN;\nOP_SWAP OP_LESSTHAN <=> OP_GREATERTHAN;\nOP_SWAP OP_DROP <=> OP_NIP;\nOP_SWAP OP_NIP <=> OP_DROP;\n\n# Remove/replace extraneous OP_DUP\n# OP_DUP OP_AND <=> ;\n# OP_DUP OP_OR <=> ;\nOP_DUP OP_DROP <=> ;\nOP_DUP OP_NIP <=> ;\n\n# Random optimisations (don't know what I'm targeting with this)\nOP_2DUP OP_DROP <=> OP_OVER;\nOP_2DUP OP_NIP <=> OP_DUP;\nOP_CAT OP_DROP <=> OP_2DROP;\nOP_NIP OP_DROP <=> OP_2DROP;\n\n# Far-fetched stuff\nOP_DUP OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_OVER OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_2 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_3 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_4 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_5 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_6 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_7 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_8 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_9 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_10 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_11 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_12 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_13 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_14 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_15 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\nOP_16 OP_PICK OP_ROT OP_SWAP OP_DROP <=> OP_SWAP;\n\nOP_DUP OP_ROT OP_DROP <=> OP_NIP OP_DUP;\nOP_OVER OP_ROT OP_DROP <=> OP_SWAP;\nOP_2 OP_PICK OP_ROT OP_DROP <=> OP_NIP OP_OVER;\n\nOP_0 OP_NIP <=> OP_DROP OP_0;\nOP_1 OP_NIP <=> OP_DROP OP_1;\nOP_2 OP_NIP <=> OP_DROP OP_2;\nOP_3 OP_NIP <=> OP_DROP OP_3;\nOP_4 OP_NIP <=> OP_DROP OP_4;\nOP_5 OP_NIP <=> OP_DROP OP_5;\nOP_6 OP_NIP <=> OP_DROP OP_6;\nOP_7 OP_NIP <=> OP_DROP OP_7;\nOP_8 OP_NIP <=> OP_DROP OP_8;\nOP_9 OP_NIP <=> OP_DROP OP_9;\nOP_10 OP_NIP <=> OP_DROP OP_10;\nOP_11 OP_NIP <=> OP_DROP OP_11;\nOP_12 OP_NIP <=> OP_DROP OP_12;\nOP_13 OP_NIP <=> OP_DROP OP_13;\nOP_14 OP_NIP <=> OP_DROP OP_14;\nOP_15 OP_NIP <=> OP_DROP OP_15;\nOP_16 OP_NIP <=> OP_DROP OP_16;\n\nOP_2 OP_PICK OP_SWAP OP_2 OP_PICK OP_NIP <=> OP_DROP OP_2DUP;\n\n# .slice(0, x) optimisation & .slice(x, y.length) optimisation\nOP_0 OP_SPLIT OP_NIP <=> ;\nOP_SIZE OP_SPLIT OP_DROP <=> ;\n";
2
2
  export default _default;
@@ -121,5 +121,9 @@ OP_15 OP_NIP <=> OP_DROP OP_15;
121
121
  OP_16 OP_NIP <=> OP_DROP OP_16;
122
122
 
123
123
  OP_2 OP_PICK OP_SWAP OP_2 OP_PICK OP_NIP <=> OP_DROP OP_2DUP;
124
+
125
+ # .slice(0, x) optimisation & .slice(x, y.length) optimisation
126
+ OP_0 OP_SPLIT OP_NIP <=> ;
127
+ OP_SIZE OP_SPLIT OP_DROP <=> ;
124
128
  `;
125
129
  //# sourceMappingURL=cashproof-optimisations.js.map
@@ -100,6 +100,9 @@ const provableOptimisations = [
100
100
  ['OP_15 OP_NIP', 'OP_DROP OP_15'],
101
101
  ['OP_16 OP_NIP', 'OP_DROP OP_16'],
102
102
  ['OP_2 OP_PICK OP_SWAP OP_2 OP_PICK OP_NIP', 'OP_DROP OP_2DUP'],
103
+ // .slice(0, x) optimisation & .slice(x, y.length) optimisation
104
+ ['OP_0 OP_SPLIT OP_NIP', ''],
105
+ ['OP_SIZE OP_SPLIT OP_DROP', ''],
103
106
  ];
104
107
  const unprovableOptimisations = [
105
108
  // Hardcoded arithmetic
package/dist/types.d.ts CHANGED
@@ -12,8 +12,9 @@ export declare class BytesType {
12
12
  toString(): string;
13
13
  }
14
14
  export declare class TupleType {
15
- elementType?: Type | undefined;
16
- constructor(elementType?: Type | undefined);
15
+ leftType: Type;
16
+ rightType: Type;
17
+ constructor(leftType: Type, rightType: Type);
17
18
  toString(): string;
18
19
  }
19
20
  export declare enum PrimitiveType {
package/dist/types.js CHANGED
@@ -20,11 +20,12 @@ export class BytesType {
20
20
  }
21
21
  }
22
22
  export class TupleType {
23
- constructor(elementType) {
24
- this.elementType = elementType;
23
+ constructor(leftType, rightType) {
24
+ this.leftType = leftType;
25
+ this.rightType = rightType;
25
26
  }
26
27
  toString() {
27
- return `(${this.elementType}, ${this.elementType})`;
28
+ return `(${this.leftType}, ${this.rightType})`;
28
29
  }
29
30
  }
30
31
  export var PrimitiveType;
@@ -103,7 +104,12 @@ export function explicitlyCastable(from, to) {
103
104
  export function implicitlyCastable(actual, expected) {
104
105
  if (!actual || !expected)
105
106
  return false;
106
- // Tuples can't be cast
107
+ if (actual instanceof TupleType && expected instanceof TupleType) {
108
+ const leftIsCompatible = implicitlyCastable(actual.leftType, expected.leftType);
109
+ const rightIsCompatible = implicitlyCastable(actual.rightType, expected.rightType);
110
+ return leftIsCompatible && rightIsCompatible;
111
+ }
112
+ // Can't cast between Tuple and non-Tuple
107
113
  if (actual instanceof TupleType || expected instanceof TupleType)
108
114
  return false;
109
115
  // Arrays can be cast if their elements can be cast (don't think this is actually used ever)
@@ -170,7 +176,7 @@ export function parseType(str) {
170
176
  return PrimitiveType[str.toUpperCase()];
171
177
  }
172
178
  export function isPrimitive(type) {
173
- return !!PrimitiveType[type.toString().toUpperCase()];
179
+ return Boolean(PrimitiveType[type.toString().toUpperCase()]);
174
180
  }
175
181
  // Denotes whether an opcode belongs to the "start" or "end" of the statement it's in (defaults to "start")
176
182
  // Examples:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cashscript/utils",
3
- "version": "0.11.2",
3
+ "version": "0.11.4",
4
4
  "description": "CashScript utilities and types",
5
5
  "keywords": [
6
6
  "bitcoin cash",
@@ -50,7 +50,7 @@
50
50
  "@jest/globals": "^29.7.0",
51
51
  "eslint": "^8.54.0",
52
52
  "jest": "^29.7.0",
53
- "typescript": "^5.7.3"
53
+ "typescript": "^5.9.2"
54
54
  },
55
- "gitHead": "81e7dc182ef71909af721d62751ef403bcfcc41e"
55
+ "gitHead": "d878d068b91ddc5a4ca1807376520ae451e4a0ed"
56
56
  }