@givelotus/compiler 0.0.0-stage → 0.2.0
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.
- package/README.md +331 -2
- package/artifact-v2.schema.json +58 -0
- package/artifact.schema.json +19 -0
- package/dist/ast.d.ts +292 -0
- package/dist/ast.js +150 -0
- package/dist/bindings.d.ts +6 -0
- package/dist/bindings.js +134 -0
- package/dist/call-runtime.d.ts +84 -0
- package/dist/call-runtime.js +249 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +22 -0
- package/dist/compiler.d.ts +79 -0
- package/dist/compiler.js +125 -0
- package/dist/diagnostics.d.ts +163 -0
- package/dist/diagnostics.js +498 -0
- package/dist/ecash.d.ts +41 -0
- package/dist/ecash.js +190 -0
- package/dist/fees.d.ts +164 -0
- package/dist/fees.js +208 -0
- package/dist/fixture-cli.d.ts +2 -0
- package/dist/fixture-cli.js +41 -0
- package/dist/fixtures.d.ts +68 -0
- package/dist/fixtures.js +46 -0
- package/dist/forth-ir.d.ts +73 -0
- package/dist/forth-ir.js +147 -0
- package/dist/generic-cli.d.ts +2 -0
- package/dist/generic-cli.js +140 -0
- package/dist/generic.d.ts +48 -0
- package/dist/generic.js +2566 -0
- package/dist/lints.d.ts +8 -0
- package/dist/lints.js +204 -0
- package/dist/names.d.ts +1 -0
- package/dist/names.js +7 -0
- package/dist/opcodes.d.ts +154 -0
- package/dist/opcodes.js +162 -0
- package/dist/optimizer.d.ts +58 -0
- package/dist/optimizer.js +786 -0
- package/dist/package-fetch-cli.d.ts +2 -0
- package/dist/package-fetch-cli.js +17 -0
- package/dist/package-lock-cli.d.ts +2 -0
- package/dist/package-lock-cli.js +28 -0
- package/dist/package-sources.d.ts +82 -0
- package/dist/package-sources.js +785 -0
- package/dist/package-update-cli.d.ts +2 -0
- package/dist/package-update-cli.js +20 -0
- package/dist/packages.d.ts +231 -0
- package/dist/packages.js +1324 -0
- package/dist/parser.d.ts +32 -0
- package/dist/parser.js +575 -0
- package/dist/policy.d.ts +83 -0
- package/dist/policy.js +207 -0
- package/dist/runtime.d.ts +58 -0
- package/dist/runtime.js +126 -0
- package/dist/scheduler.d.ts +38 -0
- package/dist/scheduler.js +76 -0
- package/dist/semantic-ir.d.ts +76 -0
- package/dist/semantic-ir.js +231 -0
- package/dist/std.d.ts +27 -0
- package/dist/std.js +282 -0
- package/dist/target-compiler.d.ts +9 -0
- package/dist/target-compiler.js +139 -0
- package/dist/targets.d.ts +53 -0
- package/dist/targets.js +67 -0
- package/dist/token-cli.d.ts +2 -0
- package/dist/token-cli.js +82 -0
- package/dist/token-metadata.d.ts +43 -0
- package/dist/token-metadata.js +166 -0
- package/dist/toml-subset.d.ts +19 -0
- package/dist/toml-subset.js +81 -0
- package/dist/transaction-runtime.d.ts +182 -0
- package/dist/transaction-runtime.js +646 -0
- package/dist/version.d.ts +3 -0
- package/dist/version.js +3 -0
- package/dist/vm-cost.d.ts +11 -0
- package/dist/vm-cost.js +541 -0
- package/package.json +74 -4
- package/policy-artifact.schema.json +20 -0
- package/std/custody/arbiter-escrow.lotus +20 -0
- package/std/custody/htlc.lotus +15 -0
- package/std/custody/multisig-2of3.lotus +13 -0
- package/std/custody/single-owner-wallet.lotus +9 -0
- package/std/custody/vault-with-recovery.lotus +14 -0
- package/std/defi/amm-pool-v2.lotus +97 -0
- package/std/defi/indexed-limit-order.lotus +37 -0
- package/std/nft/nft-collection.lotus +51 -0
- package/std/nft/royalty-transfer-rules.lotus +38 -0
- package/std/nft/unique-nft.lotus +30 -0
- package/std/tokens/capped-supply-token.lotus +76 -0
- package/std/tokens/fixed-supply-token.lotus +29 -0
- package/std/tokens/lrc20.lotus +26 -0
- package/std/tokens/standard-transfer-rules.lotus +15 -0
- package/std/tokens/stateful-token-merge.lotus +132 -0
- package/std/tokens/upgradable-token-rules.lotus +70 -0
- package/std/tokens/voucher-mint-token.lotus +31 -0
- package/std-pkg/lotus.toml +9 -0
- package/std-pkg/src/lrc20.lotus +38 -0
- package/std-pkg/src/stateful_token.lotus +131 -0
- package/std-pkg/src/upgradable.lotus +68 -0
package/dist/vm-cost.js
ADDED
|
@@ -0,0 +1,541 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* P05 static VM charge of one invocation: the reference's size pass
|
|
3
|
+
* (tools/lotus-v2-reference, vm_cost.static_size_cost) under the approved opcode
|
|
4
|
+
* schedule, plus 1 cu per script byte. The pass tracks an upper bound on every
|
|
5
|
+
* stack item's size and charges each instruction from its operand and result
|
|
6
|
+
* bounds; loops multiply their body by the static bound, and a conditional charges
|
|
7
|
+
* its dearer arm. Anything the pass cannot follow charges at MAX_ELEMENT_BYTES.
|
|
8
|
+
*/
|
|
9
|
+
const LIMIT = 8192; // MAX_ELEMENT_BYTES
|
|
10
|
+
const MIN_NUMERIC = 16, MAX_NUMERIC = 1024, MAX_MUL = 2048;
|
|
11
|
+
const STRUCTURAL_BYTE = 1n;
|
|
12
|
+
const ENFORCE_PER_OUTPUT = 1n;
|
|
13
|
+
const CHECKDATASIG_PRICES = [210, 620, 49_400, 210];
|
|
14
|
+
const MERKLE_STEP_PRICES = [2, 2, 2, 4];
|
|
15
|
+
/** The reference's recommended_opcodes(), DEFAULT_OP and signature charges. */
|
|
16
|
+
const COSTS = (() => {
|
|
17
|
+
const ops = {};
|
|
18
|
+
for (const op of [0x98, 0x99, 0x9a, 0x9b, 0x9e, 0x9f, 0xad, 0xae])
|
|
19
|
+
ops[op] = [2, 20, 20];
|
|
20
|
+
for (const op of [0x9c, 0x9d, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xaf])
|
|
21
|
+
ops[op] = [2, 25, 1];
|
|
22
|
+
Object.assign(ops, {
|
|
23
|
+
0xeb: [2, 0, 0], 0x88: [1, 0, 1], 0x8a: [2, 25, 4], 0x8b: [2, 17, 17], 0x94: [1, 11, 1], 0xa0: [2, 7, 7],
|
|
24
|
+
0xa1: [2, 24, 24], 0xa2: [2, 24, 24], 0xa3: [2, 25, 25], 0xb0: [2, 9, 9], 0xb1: [2, 9, 9], 0xb2: [2, 8, 8],
|
|
25
|
+
0xb3: [3, 175, 1], 0xb4: [4, 34, 1], 0xb8: [3, 5, 0], 0xb9: [4, 5, 0], 0xba: [2, 11, 0], 0xbe: [2, 14, 0],
|
|
26
|
+
0xbf: [3, 0, 0], 0xc2: [4, 0, 1], 0xc3: [2, 11, 0],
|
|
27
|
+
0xbb: 210, 0xbc: 210, // CHECKSIG, CHECKSIGVERIFY
|
|
28
|
+
});
|
|
29
|
+
return ops;
|
|
30
|
+
})();
|
|
31
|
+
function charge(op, operands, results) {
|
|
32
|
+
const f = COSTS[op] ?? [1, 1, 1];
|
|
33
|
+
if (typeof f === 'number')
|
|
34
|
+
return BigInt(f);
|
|
35
|
+
const sized = f[1] * operands.reduce((a, b) => a + b, 0) + f[2] * results.reduce((a, b) => a + b, 0);
|
|
36
|
+
return BigInt(f[0] + Math.ceil(sized / 1024));
|
|
37
|
+
}
|
|
38
|
+
function decode(script) {
|
|
39
|
+
const list = [];
|
|
40
|
+
for (let i = 0; i < script.length;) {
|
|
41
|
+
const op = script[i++];
|
|
42
|
+
if (op >= 1 && op <= 75) {
|
|
43
|
+
list.push({ op, data: script.subarray(i, i + op) });
|
|
44
|
+
i += op;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (op === 0x4c) {
|
|
48
|
+
let n = script[i++];
|
|
49
|
+
if (n === 0xfd) {
|
|
50
|
+
n = script[i] | (script[i + 1] << 8);
|
|
51
|
+
i += 2;
|
|
52
|
+
}
|
|
53
|
+
else if (n >= 0xfe)
|
|
54
|
+
throw new Error('PUSHDATA longer than an element');
|
|
55
|
+
list.push({ op, data: script.subarray(i, i + n) });
|
|
56
|
+
i += n;
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
list.push({ op });
|
|
60
|
+
}
|
|
61
|
+
let cursor = 0;
|
|
62
|
+
const sequence = (stops) => {
|
|
63
|
+
const nodes = [];
|
|
64
|
+
while (cursor < list.length) {
|
|
65
|
+
const ins = list[cursor++];
|
|
66
|
+
if (ins.op === 0x63 || ins.op === 0x64 || ins.op === 0x6a) {
|
|
67
|
+
if (!stops.includes(ins.op))
|
|
68
|
+
throw new Error('unmatched structure delimiter');
|
|
69
|
+
return { nodes, end: ins.op };
|
|
70
|
+
}
|
|
71
|
+
if (ins.op === 0x61 || ins.op === 0x62) {
|
|
72
|
+
const then = sequence([0x63, 0x64]);
|
|
73
|
+
let otherwise = [], end = then.end;
|
|
74
|
+
if (end === 0x63) {
|
|
75
|
+
const e = sequence([0x64]);
|
|
76
|
+
otherwise = e.nodes;
|
|
77
|
+
end = e.end;
|
|
78
|
+
}
|
|
79
|
+
if (end !== 0x64)
|
|
80
|
+
throw new Error('unclosed IF');
|
|
81
|
+
nodes.push({ cond: ins.op, then: then.nodes, otherwise, hasElse: then.end === 0x63 });
|
|
82
|
+
}
|
|
83
|
+
else if (ins.op === 0x69) {
|
|
84
|
+
const body = sequence([0x6a]);
|
|
85
|
+
if (body.end !== 0x6a)
|
|
86
|
+
throw new Error('unclosed ENUMERATE');
|
|
87
|
+
nodes.push({ loop: body.nodes });
|
|
88
|
+
}
|
|
89
|
+
else
|
|
90
|
+
nodes.push(ins);
|
|
91
|
+
}
|
|
92
|
+
return { nodes };
|
|
93
|
+
};
|
|
94
|
+
return sequence([]).nodes;
|
|
95
|
+
}
|
|
96
|
+
function literal(ins) {
|
|
97
|
+
if (!ins)
|
|
98
|
+
return undefined;
|
|
99
|
+
if (ins.data) {
|
|
100
|
+
if (ins.data.length !== 16)
|
|
101
|
+
return undefined;
|
|
102
|
+
let n = 0n;
|
|
103
|
+
for (let i = 15; i >= 0; i--)
|
|
104
|
+
n = (n << 8n) | BigInt(ins.data[i]);
|
|
105
|
+
if (n >= 1n << 127n)
|
|
106
|
+
n -= 1n << 128n;
|
|
107
|
+
return n >= -(1n << 52n) && n <= 1n << 52n ? Number(n) : (n < 0n ? -Infinity : Infinity);
|
|
108
|
+
}
|
|
109
|
+
if (ins.op === 0)
|
|
110
|
+
return 0;
|
|
111
|
+
if (ins.op === 0x4d)
|
|
112
|
+
return -1;
|
|
113
|
+
if (ins.op >= 0x4e && ins.op <= 0x5d)
|
|
114
|
+
return ins.op - 0x4d;
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
const cs = (n) => (n < 253 ? 1 : n <= 0xffff ? 3 : n <= 0xffffffff ? 5 : 9);
|
|
118
|
+
const NUMERIC = new Set([0x78, 0x8b, 0x8c, ...range(0x98, 0xb5), 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd,
|
|
119
|
+
0xd0, 0xd1, 0xd3, 0xd4, 0xd5, 0xd6, 0xd8, 0xda, 0xde, 0xe0, 0xe1, 0xe3, 0xe5, 0xe8, 0xe9, 0xea, 0xf1, 0xf5, 0xf6]);
|
|
120
|
+
const HASHES = new Set([...range(0xb8, 0xbb), 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xce, 0xcf, 0xd2, 0xd7, 0xd9, 0xe2, 0xe4, 0xee, 0xef, 0xf2]);
|
|
121
|
+
const FIELD_READS = new Set([0xdb, 0xdc, 0xe6, 0xe7, 0xf0]);
|
|
122
|
+
function range(a, b) { return Array.from({ length: b - a }, (_, i) => a + i); }
|
|
123
|
+
const UNKNOWN = null;
|
|
124
|
+
function merge(a, b) {
|
|
125
|
+
if (a === undefined)
|
|
126
|
+
return b;
|
|
127
|
+
if (b === undefined)
|
|
128
|
+
return a;
|
|
129
|
+
if (a === null || b === null || a.main.length !== b.main.length || a.alt.length !== b.alt.length)
|
|
130
|
+
return UNKNOWN;
|
|
131
|
+
return { main: a.main.map((x, i) => Math.max(x, b.main[i])), alt: a.alt.map((x, i) => Math.max(x, b.alt[i])) };
|
|
132
|
+
}
|
|
133
|
+
const sameHeight = (a, b) => !!a && !!b && a.main.length === b.main.length && a.alt.length === b.alt.length;
|
|
134
|
+
export function staticInvocationCu(script, initialSizes, inputCount, outputCount, fields = {}) {
|
|
135
|
+
const ast = decode(script);
|
|
136
|
+
const fieldSize = (op) => fields[op];
|
|
137
|
+
const scan = (op) => (op === 0xeb ? ENFORCE_PER_OUTPUT * BigInt(outputCount) : 0n);
|
|
138
|
+
const selectorPrice = (prices, previous) => BigInt(prices[literal(previous) ?? -1] ?? Math.max(...prices));
|
|
139
|
+
const operationCu = (op, operands, results, previous, proofBound) => {
|
|
140
|
+
if (op === 0xbd)
|
|
141
|
+
return selectorPrice(CHECKDATASIG_PRICES, previous);
|
|
142
|
+
if (op === 0xc0 || op === 0xc1) {
|
|
143
|
+
const cap = op === 0xc0 ? 64 : 256;
|
|
144
|
+
const steps = Math.min(cap, Math.floor((proofBound ?? LIMIT) / 32));
|
|
145
|
+
return 3n + selectorPrice(MERKLE_STEP_PRICES, previous) * BigInt(steps);
|
|
146
|
+
}
|
|
147
|
+
return charge(op, operands, results) + scan(op);
|
|
148
|
+
};
|
|
149
|
+
const worst = (op, previous) => {
|
|
150
|
+
let ops = Array(6).fill(LIMIT);
|
|
151
|
+
if (op === 0xeb)
|
|
152
|
+
ops = Array(21).fill(LIMIT);
|
|
153
|
+
if (op === 0xc3)
|
|
154
|
+
ops = [...Array(65).fill(LIMIT), cs(64) + 64 * cs(LIMIT)];
|
|
155
|
+
if ([0xa0, 0xa1, 0xa2, 0xb2, 0xb3, 0xb4].includes(op)) {
|
|
156
|
+
const limbs = Math.max(1, Math.floor((Math.min(LIMIT, 1024) + 15) / 16)), quad = limbs * limbs;
|
|
157
|
+
if (quad > 1)
|
|
158
|
+
ops = [...ops, (quad - 1) * 16];
|
|
159
|
+
}
|
|
160
|
+
return operationCu(op, ops, Array(4).fill(LIMIT), previous);
|
|
161
|
+
};
|
|
162
|
+
const clamp = (n) => Math.min(Math.max(n, MIN_NUMERIC), MAX_NUMERIC);
|
|
163
|
+
const limbs = (n) => Math.max(1, Math.floor((Math.min(n, MAX_NUMERIC) + 15) / 16));
|
|
164
|
+
const transfer = (ins, state, previous) => {
|
|
165
|
+
const op = ins.op;
|
|
166
|
+
if (state === null)
|
|
167
|
+
return [UNKNOWN, worst(op, previous)];
|
|
168
|
+
const main = [...state.main], alt = [...state.alt], operands = [], result = [];
|
|
169
|
+
let proofBound;
|
|
170
|
+
class Under extends Error {
|
|
171
|
+
}
|
|
172
|
+
const pop = () => { if (!main.length)
|
|
173
|
+
throw new Under(); const s = main.pop(); operands.push(s); return s; };
|
|
174
|
+
const push = (s) => { const v = Math.min(s, LIMIT); main.push(v); result.push(v); };
|
|
175
|
+
const need = (n) => { if (main.length < n)
|
|
176
|
+
throw new Under(); };
|
|
177
|
+
try {
|
|
178
|
+
if (ins.data)
|
|
179
|
+
push(ins.data.length);
|
|
180
|
+
else if (op === 0 || (op >= 0x4d && op <= 0x5d))
|
|
181
|
+
push(16);
|
|
182
|
+
else if (op === 0x60 || op === 0x66) { /* nothing */ }
|
|
183
|
+
else if (op === 0x70)
|
|
184
|
+
alt.push(pop());
|
|
185
|
+
else if (op === 0x71) {
|
|
186
|
+
if (!alt.length)
|
|
187
|
+
throw new Under();
|
|
188
|
+
const s = alt.pop();
|
|
189
|
+
operands.push(s);
|
|
190
|
+
push(s);
|
|
191
|
+
}
|
|
192
|
+
else if (op === 0x72) {
|
|
193
|
+
pop();
|
|
194
|
+
pop();
|
|
195
|
+
}
|
|
196
|
+
else if (op === 0x73 || op === 0x74 || op === 0x75) {
|
|
197
|
+
const [count, offset] = op === 0x73 ? [2, 2] : op === 0x74 ? [3, 3] : [2, 4];
|
|
198
|
+
need(offset);
|
|
199
|
+
for (const s of main.slice(main.length - offset, main.length - offset + count))
|
|
200
|
+
push(s);
|
|
201
|
+
}
|
|
202
|
+
else if (op === 0x76 || op === 0x77) {
|
|
203
|
+
const count = op === 0x76 ? 6 : 4;
|
|
204
|
+
need(count);
|
|
205
|
+
const tail = main.slice(-count);
|
|
206
|
+
operands.push(...tail);
|
|
207
|
+
main.splice(main.length - count, count, ...tail.slice(2), ...tail.slice(0, 2));
|
|
208
|
+
}
|
|
209
|
+
else if (op === 0x78)
|
|
210
|
+
push(16);
|
|
211
|
+
else if (op === 0x79)
|
|
212
|
+
pop();
|
|
213
|
+
else if (op === 0x7a) {
|
|
214
|
+
need(1);
|
|
215
|
+
operands.push(main.at(-1));
|
|
216
|
+
push(main.at(-1));
|
|
217
|
+
}
|
|
218
|
+
else if (op === 0x7b) {
|
|
219
|
+
need(2);
|
|
220
|
+
operands.push(main.splice(-2, 1)[0]);
|
|
221
|
+
}
|
|
222
|
+
else if (op === 0x7c) {
|
|
223
|
+
need(2);
|
|
224
|
+
operands.push(main.at(-2));
|
|
225
|
+
push(main.at(-2));
|
|
226
|
+
}
|
|
227
|
+
else if (op === 0x7d || op === 0x7e) {
|
|
228
|
+
pop();
|
|
229
|
+
const index = literal(previous);
|
|
230
|
+
if (index !== undefined && index >= 0 && index < main.length) {
|
|
231
|
+
const chosen = main[main.length - 1 - index];
|
|
232
|
+
operands.push(chosen);
|
|
233
|
+
if (op === 0x7d)
|
|
234
|
+
push(chosen);
|
|
235
|
+
else
|
|
236
|
+
push(main.splice(main.length - 1 - index, 1)[0]);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const largest = main.length ? Math.max(...main) : LIMIT;
|
|
240
|
+
operands.push(largest);
|
|
241
|
+
if (op === 0x7d)
|
|
242
|
+
push(largest);
|
|
243
|
+
else
|
|
244
|
+
main.fill(largest);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if (op === 0x7f) {
|
|
248
|
+
need(3);
|
|
249
|
+
const t = main.slice(-3);
|
|
250
|
+
operands.push(...t);
|
|
251
|
+
main.splice(-3, 3, t[1], t[2], t[0]);
|
|
252
|
+
}
|
|
253
|
+
else if (op === 0x80) {
|
|
254
|
+
need(2);
|
|
255
|
+
const t = main.slice(-2);
|
|
256
|
+
operands.push(...t);
|
|
257
|
+
main.splice(-2, 2, t[1], t[0]);
|
|
258
|
+
}
|
|
259
|
+
else if (op === 0x81) {
|
|
260
|
+
need(2);
|
|
261
|
+
const [a, b] = main.slice(-2);
|
|
262
|
+
operands.push(a, b);
|
|
263
|
+
main.splice(-2, 2, b, a, b);
|
|
264
|
+
result.push(b);
|
|
265
|
+
}
|
|
266
|
+
else if (op === 0x88) {
|
|
267
|
+
const b = pop(), a = pop();
|
|
268
|
+
push(a + b);
|
|
269
|
+
}
|
|
270
|
+
else if (op === 0x89) {
|
|
271
|
+
pop();
|
|
272
|
+
const s = pop();
|
|
273
|
+
push(s);
|
|
274
|
+
push(s);
|
|
275
|
+
}
|
|
276
|
+
else if (op === 0x8a) {
|
|
277
|
+
pop();
|
|
278
|
+
pop();
|
|
279
|
+
const w = literal(previous);
|
|
280
|
+
push(w !== undefined && w >= 0 && w <= LIMIT ? w : LIMIT);
|
|
281
|
+
}
|
|
282
|
+
else if (op === 0x8b)
|
|
283
|
+
push(clamp(pop()));
|
|
284
|
+
else if (op === 0x8c) {
|
|
285
|
+
need(1);
|
|
286
|
+
operands.push(main.at(-1));
|
|
287
|
+
push(16);
|
|
288
|
+
}
|
|
289
|
+
else if (op === 0x8d)
|
|
290
|
+
push(pop());
|
|
291
|
+
else if ([0x8e, 0x8f, 0x90, 0x93].includes(op)) {
|
|
292
|
+
const b = pop(), a = pop();
|
|
293
|
+
push(Math.max(a, b));
|
|
294
|
+
if (op === 0x93)
|
|
295
|
+
push(Math.max(a, b));
|
|
296
|
+
}
|
|
297
|
+
else if (op === 0x91 || op === 0x92) {
|
|
298
|
+
pop();
|
|
299
|
+
pop();
|
|
300
|
+
if (op === 0x91)
|
|
301
|
+
push(16);
|
|
302
|
+
}
|
|
303
|
+
else if (op === 0x94) {
|
|
304
|
+
pop();
|
|
305
|
+
push(pop());
|
|
306
|
+
}
|
|
307
|
+
else if ((op >= 0x98 && op < 0x9e) || op === 0xb3 || op === 0xb4) {
|
|
308
|
+
const a = pop();
|
|
309
|
+
if (op === 0xb3 || op === 0xb4) {
|
|
310
|
+
const l = limbs(a);
|
|
311
|
+
if (l > 1)
|
|
312
|
+
operands.push((l * l - 1) * 16);
|
|
313
|
+
}
|
|
314
|
+
push(op === 0x9c || op === 0x9d || op === 0xb3 ? 16 : clamp(a));
|
|
315
|
+
}
|
|
316
|
+
else if ([0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3].includes(op) || (op >= 0xa4 && op < 0xaf)) {
|
|
317
|
+
const b = pop(), a = pop(), ca = clamp(a), cb = clamp(b);
|
|
318
|
+
if (op === 0xa0 || op === 0xa1 || op === 0xa2) {
|
|
319
|
+
const q = limbs(a) * limbs(b);
|
|
320
|
+
if (q > 1)
|
|
321
|
+
operands.push((q - 1) * 16);
|
|
322
|
+
}
|
|
323
|
+
if (op === 0xa0)
|
|
324
|
+
push(a <= 16 && b <= 16 ? 16 : Math.min(MAX_MUL, ca + cb));
|
|
325
|
+
else if (op === 0xa3)
|
|
326
|
+
push(ca);
|
|
327
|
+
else if ([0x9e, 0x9f, 0xa1, 0xa2, 0xad, 0xae].includes(op))
|
|
328
|
+
push(Math.max(ca, cb));
|
|
329
|
+
else if (op !== 0xa7)
|
|
330
|
+
push(16);
|
|
331
|
+
}
|
|
332
|
+
else if (op === 0xaf || op === 0xb0 || op === 0xb1) {
|
|
333
|
+
pop();
|
|
334
|
+
const b = pop(), a = pop();
|
|
335
|
+
if (op === 0xaf)
|
|
336
|
+
push(16);
|
|
337
|
+
else {
|
|
338
|
+
push(clamp(Math.max(a, b)));
|
|
339
|
+
push(16);
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
else if (op === 0xb2) {
|
|
343
|
+
const b = pop(), a = pop(), q = limbs(a) * limbs(b);
|
|
344
|
+
if (q > 1)
|
|
345
|
+
operands.push((q - 1) * 16);
|
|
346
|
+
const w = clamp(Math.max(a, b));
|
|
347
|
+
push(w);
|
|
348
|
+
push(w);
|
|
349
|
+
}
|
|
350
|
+
else if (HASHES.has(op)) {
|
|
351
|
+
const arity = { 0xbf: 2, 0xc0: 3, 0xc1: 4, 0xc2: 1, 0xd9: 2, 0xe4: 2 }[op] ?? ([0xce, 0xcf, 0xf2].includes(op) ? 0 : 1);
|
|
352
|
+
for (let i = 0; i < arity; i++) {
|
|
353
|
+
const operand = pop();
|
|
354
|
+
if ((op === 0xc0 || op === 0xc1) && i === 1)
|
|
355
|
+
proofBound = operand;
|
|
356
|
+
}
|
|
357
|
+
push(32);
|
|
358
|
+
}
|
|
359
|
+
else if (op === 0xeb)
|
|
360
|
+
return [UNKNOWN, worst(op)];
|
|
361
|
+
else if (op === 0xc3) {
|
|
362
|
+
const k = literal(previous);
|
|
363
|
+
if (k === undefined || k < 0 || k > 64)
|
|
364
|
+
return [UNKNOWN, worst(op)];
|
|
365
|
+
pop();
|
|
366
|
+
let framing = cs(k);
|
|
367
|
+
for (let i = 0; i < k; i++)
|
|
368
|
+
framing += cs(pop());
|
|
369
|
+
operands.push(framing);
|
|
370
|
+
push(32);
|
|
371
|
+
}
|
|
372
|
+
else if (op === 0xbb || op === 0xbc) {
|
|
373
|
+
pop();
|
|
374
|
+
pop();
|
|
375
|
+
if (op === 0xbb)
|
|
376
|
+
push(16);
|
|
377
|
+
}
|
|
378
|
+
else if (op === 0xbd) {
|
|
379
|
+
pop();
|
|
380
|
+
pop();
|
|
381
|
+
pop();
|
|
382
|
+
pop();
|
|
383
|
+
push(16);
|
|
384
|
+
}
|
|
385
|
+
else if (op === 0xdd) {
|
|
386
|
+
pop();
|
|
387
|
+
pop();
|
|
388
|
+
push(16);
|
|
389
|
+
}
|
|
390
|
+
else if (op === 0xec) {
|
|
391
|
+
pop();
|
|
392
|
+
push(16);
|
|
393
|
+
push(16);
|
|
394
|
+
}
|
|
395
|
+
else if (op === 0xed)
|
|
396
|
+
pop();
|
|
397
|
+
// CHECKLOCKTIMEVERIFY, CHECKSEQUENCEVERIFY: N(kind) N(bound) -> empty.
|
|
398
|
+
else if (op === 0xf3 || op === 0xf4) {
|
|
399
|
+
pop();
|
|
400
|
+
pop();
|
|
401
|
+
}
|
|
402
|
+
else if (op === 0xdf) {
|
|
403
|
+
pop();
|
|
404
|
+
pop();
|
|
405
|
+
pop();
|
|
406
|
+
push(LIMIT);
|
|
407
|
+
}
|
|
408
|
+
else if (NUMERIC.has(op)) {
|
|
409
|
+
if ([0xd1, 0xd3, 0xd4, 0xd5, 0xd6, 0xd8, 0xda, 0xde, 0xe0, 0xe1, 0xe3, 0xe5].includes(op))
|
|
410
|
+
pop();
|
|
411
|
+
if (op === 0xe8 || op === 0xe9 || op === 0xea) {
|
|
412
|
+
pop();
|
|
413
|
+
pop();
|
|
414
|
+
}
|
|
415
|
+
push(16);
|
|
416
|
+
}
|
|
417
|
+
else if (FIELD_READS.has(op)) {
|
|
418
|
+
const index = literal(previous);
|
|
419
|
+
for (let i = 0; i < (op === 0xdb || op === 0xe6 ? 2 : op === 0xf0 ? 0 : 1); i++)
|
|
420
|
+
pop();
|
|
421
|
+
const sizes = fieldSize(op);
|
|
422
|
+
push(op === 0xf0 ? script.length
|
|
423
|
+
: sizes?.length && index !== undefined && index >= 0 && index < sizes.length ? sizes[index]
|
|
424
|
+
: sizes?.length ? Math.max(...sizes) : LIMIT);
|
|
425
|
+
}
|
|
426
|
+
else
|
|
427
|
+
return [UNKNOWN, worst(op)];
|
|
428
|
+
}
|
|
429
|
+
catch (e) {
|
|
430
|
+
if (e instanceof Under)
|
|
431
|
+
return [UNKNOWN, worst(op, previous)];
|
|
432
|
+
throw e;
|
|
433
|
+
}
|
|
434
|
+
return [{ main, alt }, operationCu(op, operands, result, previous, proofBound)];
|
|
435
|
+
};
|
|
436
|
+
const sequence = (nodes, state, entries) => {
|
|
437
|
+
let current = state;
|
|
438
|
+
const exits = new Map();
|
|
439
|
+
let total = 0n, worstTotal = 0n;
|
|
440
|
+
let previous;
|
|
441
|
+
for (const node of nodes) {
|
|
442
|
+
const starting = current === undefined ? UNKNOWN : current;
|
|
443
|
+
let part, worstPart;
|
|
444
|
+
if ('op' in node) {
|
|
445
|
+
if (node.op === 0x6b || node.op === 0x6d) {
|
|
446
|
+
const popped = node.op === 0x6b ? 1 : 2;
|
|
447
|
+
const after = starting === null || starting.main.length < popped ? UNKNOWN
|
|
448
|
+
: { main: starting.main.slice(0, -popped), alt: starting.alt };
|
|
449
|
+
const lit = literal(previous);
|
|
450
|
+
const targets = lit === 1 && entries.length ? [1] : range(1, entries.length + 1);
|
|
451
|
+
for (const depth of targets) {
|
|
452
|
+
const target = entries[entries.length - depth];
|
|
453
|
+
exits.set(depth, merge(exits.get(depth), sameHeight(after, target) ? after : UNKNOWN));
|
|
454
|
+
}
|
|
455
|
+
const depthSize = starting !== null ? 16 : LIMIT;
|
|
456
|
+
if (node.op === 0x6b) {
|
|
457
|
+
part = charge(node.op, [depthSize], []);
|
|
458
|
+
current = undefined;
|
|
459
|
+
}
|
|
460
|
+
else {
|
|
461
|
+
const condSize = starting !== null && starting.main.length >= 2 ? starting.main.at(-2) : LIMIT;
|
|
462
|
+
part = charge(node.op, [depthSize, condSize], []);
|
|
463
|
+
current = current !== undefined ? after : undefined;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
else if (node.op === 0x65) {
|
|
467
|
+
const next = starting !== null && starting.main.length ? { main: starting.main.slice(0, -1), alt: starting.alt } : UNKNOWN;
|
|
468
|
+
part = charge(node.op, [starting?.main.length ? starting.main.at(-1) : LIMIT], []);
|
|
469
|
+
current = current !== undefined ? next : undefined;
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
const [next, p] = transfer(node, starting, previous);
|
|
473
|
+
part = p;
|
|
474
|
+
current = current !== undefined ? next : undefined;
|
|
475
|
+
}
|
|
476
|
+
worstPart = worst(node.op, previous);
|
|
477
|
+
previous = node;
|
|
478
|
+
}
|
|
479
|
+
else if ('cond' in node) {
|
|
480
|
+
const condSize = starting?.main.length ? starting.main.at(-1) : LIMIT;
|
|
481
|
+
const arm = starting !== null && starting.main.length ? { main: starting.main.slice(0, -1), alt: starting.alt } : UNKNOWN;
|
|
482
|
+
const left = sequence(node.then, arm, entries), right = sequence(node.otherwise, arm, entries);
|
|
483
|
+
part = charge(node.cond, [condSize], []) + charge(0x64, [], []) + (node.hasElse ? charge(0x63, [], []) : 0n)
|
|
484
|
+
+ (left.cost > right.cost ? left.cost : right.cost);
|
|
485
|
+
worstPart = charge(node.cond, Array(6).fill(LIMIT), Array(4).fill(LIMIT)) + charge(0x64, [], [])
|
|
486
|
+
+ (node.hasElse ? charge(0x63, [], []) : 0n) + (left.worst > right.worst ? left.worst : right.worst);
|
|
487
|
+
current = current !== undefined ? merge(left.state, right.state) : undefined;
|
|
488
|
+
for (const source of [left.exits, right.exits])
|
|
489
|
+
for (const [d, s] of source)
|
|
490
|
+
exits.set(d, merge(exits.get(d), s));
|
|
491
|
+
previous = undefined;
|
|
492
|
+
}
|
|
493
|
+
else {
|
|
494
|
+
const entry = starting !== null && starting.main.length ? { main: starting.main.slice(0, -1), alt: starting.alt } : UNKNOWN;
|
|
495
|
+
const lit = previous && !previous.data && previous.op === 0xc9 ? inputCount
|
|
496
|
+
: previous && !previous.data && previous.op === 0xca ? outputCount : literal(previous);
|
|
497
|
+
const bound = lit !== undefined && lit >= 0 && lit <= 256 ? lit : 256;
|
|
498
|
+
const start = entry !== null ? { main: [...entry.main, 16], alt: entry.alt } : UNKNOWN;
|
|
499
|
+
const inner = [...entries, entry];
|
|
500
|
+
let body = sequence(node.loop, start, inner);
|
|
501
|
+
const localBreak = body.exits.get(1);
|
|
502
|
+
const joined = merge(body.state, localBreak);
|
|
503
|
+
let post;
|
|
504
|
+
const blowUp = (b) => ({ cost: b.worst, state: UNKNOWN, exits: new Map([...b.exits.keys()].map(d => [d, UNKNOWN])), worst: b.worst });
|
|
505
|
+
if (joined !== undefined && sameHeight(joined, entry)) {
|
|
506
|
+
const widened = { main: joined.main.map((x, i) => (x > entry.main[i] ? LIMIT : entry.main[i])),
|
|
507
|
+
alt: joined.alt.map((x, i) => (x > entry.alt[i] ? LIMIT : entry.alt[i])) };
|
|
508
|
+
const second = sequence(node.loop, { main: [...widened.main, 16], alt: widened.alt }, inner);
|
|
509
|
+
const secondJoined = merge(second.state, second.exits.get(1));
|
|
510
|
+
if (secondJoined !== undefined && sameHeight(secondJoined, widened)
|
|
511
|
+
&& [...secondJoined.main, ...secondJoined.alt].every((x, i) => x <= [...widened.main, ...widened.alt][i])) {
|
|
512
|
+
body = second;
|
|
513
|
+
post = merge(entry, secondJoined);
|
|
514
|
+
}
|
|
515
|
+
else {
|
|
516
|
+
body = blowUp(second);
|
|
517
|
+
post = UNKNOWN;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
else if (joined === undefined)
|
|
521
|
+
post = merge(entry, localBreak);
|
|
522
|
+
else {
|
|
523
|
+
body = blowUp(body);
|
|
524
|
+
post = UNKNOWN;
|
|
525
|
+
}
|
|
526
|
+
part = charge(0x69, [16], []) + BigInt(bound) * (charge(0x6a, [], []) + body.cost);
|
|
527
|
+
worstPart = charge(0x69, Array(6).fill(LIMIT), Array(4).fill(LIMIT)) + BigInt(bound) * (charge(0x6a, [], []) + body.worst);
|
|
528
|
+
current = current !== undefined ? post : undefined;
|
|
529
|
+
for (const [d, s] of body.exits)
|
|
530
|
+
if (d > 1)
|
|
531
|
+
exits.set(d - 1, merge(exits.get(d - 1), s));
|
|
532
|
+
previous = undefined;
|
|
533
|
+
}
|
|
534
|
+
total += part;
|
|
535
|
+
worstTotal += worstPart;
|
|
536
|
+
}
|
|
537
|
+
return { cost: total, state: current, exits, worst: worstTotal };
|
|
538
|
+
};
|
|
539
|
+
const sizes = initialSizes.map(s => Math.min(s, LIMIT));
|
|
540
|
+
return STRUCTURAL_BYTE * BigInt(script.length) + sequence(ast, { main: sizes, alt: [] }, []).cost;
|
|
541
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,76 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@givelotus/compiler",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Lotus (XPI) smart-contract compiler featuring a Pratt parser, Forth IR, nominal hash types, static compute-unit (cu) bounds, and the Edition 2026.2 std/ library.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"homepage": "https://lotus.net/developers/",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/givelotus/lotus.git",
|
|
10
|
+
"directory": "tools/lotus-compiler"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/givelotus/lotus/issues"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"lotus",
|
|
17
|
+
"xpi",
|
|
18
|
+
"compiler",
|
|
19
|
+
"covenants",
|
|
20
|
+
"smart-contracts",
|
|
21
|
+
"utxo"
|
|
22
|
+
],
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"type": "module",
|
|
27
|
+
"files": [
|
|
28
|
+
"dist/",
|
|
29
|
+
"std/",
|
|
30
|
+
"std-pkg/",
|
|
31
|
+
"*.schema.json",
|
|
32
|
+
"README.md"
|
|
33
|
+
],
|
|
34
|
+
"exports": {
|
|
35
|
+
".": "./dist/compiler.js",
|
|
36
|
+
"./runtime": "./dist/runtime.js",
|
|
37
|
+
"./fixtures": "./dist/fixtures.js",
|
|
38
|
+
"./generic": "./dist/generic.js",
|
|
39
|
+
"./policy": "./dist/policy.js",
|
|
40
|
+
"./call-runtime": "./dist/call-runtime.js",
|
|
41
|
+
"./transaction-runtime": "./dist/transaction-runtime.js",
|
|
42
|
+
"./packages": "./dist/packages.js",
|
|
43
|
+
"./targets": "./dist/targets.js",
|
|
44
|
+
"./semantic-ir": "./dist/semantic-ir.js",
|
|
45
|
+
"./ecash": "./dist/ecash.js",
|
|
46
|
+
"./target-compiler": "./dist/target-compiler.js",
|
|
47
|
+
"./std": "./dist/std.js",
|
|
48
|
+
"./token-metadata": "./dist/token-metadata.js"
|
|
49
|
+
},
|
|
50
|
+
"bin": {
|
|
51
|
+
"lotus-compile": "dist/cli.js",
|
|
52
|
+
"lotus-payment-fixture": "dist/fixture-cli.js",
|
|
53
|
+
"lotus-contract-compile": "dist/generic-cli.js",
|
|
54
|
+
"lotus-package-lock": "dist/package-lock-cli.js",
|
|
55
|
+
"lotus-package-fetch": "dist/package-fetch-cli.js",
|
|
56
|
+
"lotus-package-update": "dist/package-update-cli.js",
|
|
57
|
+
"lotus-token": "dist/token-cli.js"
|
|
58
|
+
},
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build": "tsc -p .",
|
|
61
|
+
"test": "npm run build && node --test test/*.test.mjs",
|
|
62
|
+
"prepublishOnly": "npm test",
|
|
63
|
+
"demo:pipeline": "npm run build && node examples/compiler-pipeline/run.mjs"
|
|
64
|
+
},
|
|
65
|
+
"dependencies": {
|
|
66
|
+
"@noble/curves": "1.9.1",
|
|
67
|
+
"@noble/hashes": "1.8.0"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@types/node": "22.15.30",
|
|
71
|
+
"typescript": "5.9.3"
|
|
72
|
+
},
|
|
73
|
+
"engines": {
|
|
74
|
+
"node": ">=20"
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema":"https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id":"lotus/policy-artifact/1",
|
|
4
|
+
"title":"Lotus V2 LC-1 companion policy artifact",
|
|
5
|
+
"description":"Runtime loader additionally recomputes exact policy encoding/digest and validates contiguous slots, distinct roots, canonical paths, and data exclusion. Application digests are not consensus fields.",
|
|
6
|
+
"type":"object",
|
|
7
|
+
"additionalProperties":false,
|
|
8
|
+
"required":["schema","languageVersion","name","target","targetDigest","profileDigest","compilerRevision","optimizerMode","constructor","bindings","provenance","branches","paths","policyEncoding","policyDigest"],
|
|
9
|
+
"properties":{
|
|
10
|
+
"schema":{"const":"lotus/policy-artifact/1"},"languageVersion":{"const":1},
|
|
11
|
+
"name":{"$ref":"#/$defs/name"},"target":{"type":"string","minLength":1},
|
|
12
|
+
"targetDigest":{"$ref":"#/$defs/hash"},"profileDigest":{"$ref":"#/$defs/hash"},
|
|
13
|
+
"compilerRevision":{"type":"string","minLength":1},"optimizerMode":{"type":"string","minLength":1},
|
|
14
|
+
"constructor":{"type":"array"},"bindings":{"type":"object"},"provenance":{"type":"object"},
|
|
15
|
+
"branches":{"type":"array","minItems":1,"maxItems":16,"items":{"type":"object","additionalProperties":false,"required":["name","index","kind","root"],"properties":{"name":{"$ref":"#/$defs/name"},"index":{"type":"integer","minimum":0,"maximum":15},"kind":{"enum":["spend","data"]},"root":{"$ref":"#/$defs/hash"},"artifactDigest":{"$ref":"#/$defs/hash"}},"allOf":[{"if":{"properties":{"kind":{"const":"spend"}}},"then":{"required":["artifactDigest"]},"else":{"not":{"required":["artifactDigest"]}}}]}},
|
|
16
|
+
"paths":{"type":"array","minItems":1,"maxItems":16,"items":{"type":"object","additionalProperties":false,"required":["name","pathIndex","slots"],"properties":{"name":{"$ref":"#/$defs/name"},"pathIndex":{"type":"integer","minimum":0,"maximum":15},"slots":{"type":"array","minItems":1,"maxItems":16,"uniqueItems":true,"items":{"type":"integer","minimum":0,"maximum":15}}}}},
|
|
17
|
+
"policyEncoding":{"type":"string","pattern":"^([0-9a-f]{2})+$"},"policyDigest":{"$ref":"#/$defs/hash"}
|
|
18
|
+
},
|
|
19
|
+
"$defs":{"name":{"type":"string","pattern":"^[A-Za-z_][A-Za-z_0-9]*$"},"hash":{"type":"string","pattern":"^[0-9a-f]{64}$"}}
|
|
20
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
pragma edition "2026.1";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// 2-of-3 Arbiter Escrow with timeout refund to buyer.
|
|
5
|
+
contract ArbiterEscrow<buyer: PublicKey, seller: PublicKey, arbiter: PublicKey, timeout_height: Int> {
|
|
6
|
+
function release(buyer_sig: Signature, seller_sig: Signature) {
|
|
7
|
+
require(checksig(buyer_sig, buyer));
|
|
8
|
+
require(checksig(seller_sig, seller));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function arbitrate(arbiter_sig: Signature, party_sig: Signature) {
|
|
12
|
+
require(checksig(arbiter_sig, arbiter));
|
|
13
|
+
require(checksig(party_sig, buyer) || checksig(party_sig, seller));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function timeout_refund(buyer_sig: Signature) {
|
|
17
|
+
require(check_lock(1, timeout_height));
|
|
18
|
+
require(checksig(buyer_sig, buyer));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
pragma edition "2026.1";
|
|
2
|
+
pragma optimizer "v2";
|
|
3
|
+
|
|
4
|
+
// Hash Time-Locked Contract (HTLC) for non-custodial cross-chain atomic swaps.
|
|
5
|
+
contract HTLC<recipient: PublicKey, refund_key: PublicKey, secret_hash: Bytes32, refund_height: Int> {
|
|
6
|
+
function claim(sig: Signature, preimage: Bytes(64)) {
|
|
7
|
+
require(sha256(preimage) == secret_hash);
|
|
8
|
+
require(checksig(sig, recipient));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function refund(sig: Signature) {
|
|
12
|
+
require(check_lock(1, refund_height));
|
|
13
|
+
require(checksig(sig, refund_key));
|
|
14
|
+
}
|
|
15
|
+
}
|