@lucid-evolution/uplc 0.3.0 → 0.5.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 CHANGED
@@ -28,7 +28,75 @@ pnpm build
28
28
 
29
29
  The package ships two builds of the evaluator:
30
30
 
31
- - **speed** (`-O3`, ~1.77 MB wasm): faster script evaluation.
32
- - **size** (`-Oz`, ~0.94 MB wasm): smaller download.
31
+ - **speed** (`-O3`, ~1.93 MB wasm): faster script evaluation.
32
+ - **size** (`-Oz`, ~1.03 MB wasm): smaller download.
33
33
 
34
34
  `@lucid-evolution/uplc` resolves to the speed build on Node, Bun and Deno, and to the size build in browser bundles and edge runtimes (`browser`, `worker`, `workerd` and `edge-light` export conditions). To pick a variant explicitly, import `@lucid-evolution/uplc/speed` or `@lucid-evolution/uplc/size`.
35
+
36
+ ## Evaluating a program
37
+
38
+ `eval_program_raw` evaluates a UPLC program applied to `Data` arguments, outside of any transaction. The caller builds whatever arguments the program expects, including its script context.
39
+
40
+ ```ts
41
+ import { eval_program_raw } from "@lucid-evolution/uplc";
42
+
43
+ const result = eval_program_raw(
44
+ programFlat, // Uint8Array: the program, flat-encoded without a CBOR wrapper
45
+ [datum, redeemer, context], // Uint8Array[]: each argument as CBOR-encoded Data
46
+ 3, // Plutus version: 1, 2 or 3
47
+ 11, // protocol major version: 5 to 11
48
+ costModel, // BigInt64Array: every cost model parameter for this Plutus version
49
+ 10_000_000_000n, // CPU budget
50
+ 14_000_000n, // memory budget
51
+ true, // charge every step, and stop as soon as the budget runs out
52
+ );
53
+
54
+ result.ok; // whether the program succeeded (for Plutus V3, returned unit)
55
+ result.error_kind; // 0 ok, 1 over budget, 2 failed, 3 Plutus V3 result not unit
56
+ result.cpu; // bigint
57
+ result.mem; // bigint
58
+ result.logs; // string[]: the program's traces
59
+ ```
60
+
61
+ With `false` as the last argument, steps are charged and the budget is checked every 200 steps as the ledger does. That only changes the cost reported for a program that fails, as the ledger does not charge the steps it took since the budget was last checked; the Haskell evaluator reports the same cost. It throws only when the inputs are malformed or unsupported.
62
+
63
+ ## Evaluating a builtin
64
+
65
+ `eval_builtin_raw` evaluates one builtin applied to constant arguments as the evaluator does for a Plutus V3 script at a protocol version (9 to 12), for callers that step through UPLC themselves.
66
+
67
+ ```ts
68
+ import {
69
+ BuiltinErrorKind,
70
+ BuiltinValueKind,
71
+ eval_builtin_raw,
72
+ } from "@lucid-evolution/uplc";
73
+
74
+ const result = eval_builtin_raw(
75
+ 0, // the builtin's flat tag: 0 is addInteger
76
+ [one, two], // Uint8Array[]: each argument, encoded as its kind says
77
+ Uint8Array.of(BuiltinValueKind.Flat, BuiltinValueKind.Flat),
78
+ 11, // protocol major version: 9 to 12
79
+ );
80
+
81
+ result.ok; // whether the builtin returned a value
82
+ result.error_kind; // a BuiltinErrorKind: Ok, NotAvailable, RejectedArguments or Failed
83
+ result.result_kind; // a BuiltinValueKind
84
+ result.result; // Uint8Array: the value, encoded as result_kind says
85
+ result.cpu; // bigint
86
+ result.mem; // bigint
87
+ result.error; // the evaluator's message when the builtin did not return a value
88
+ ```
89
+
90
+ A `Flat` value is a flat-encoded `(program 1.1.0 (con …))`. Flat cannot encode BLS12-381 points, so a point crosses as its compressed bytes (`G1`, `G2`), and a list or array of points as the compressed points one after the other (`G1List`, `G2List`, `G1Array`, `G2Array`). The Miller-loop builtins (68 to 70) are not supported. `cpu` and `mem` are the builtin's charge under Plutus' default Plutus V3 cost model for the protocol version, excluding the machine's per-step costs; they are 0 when the builtin is not available or rejects its arguments, as the machine fails before costing it. It throws only when the inputs are malformed or unsupported.
91
+
92
+ `uplc_build_info()` returns JSON that describes the build: the uplc crate it was built from, the latest protocol version it supports, each Plutus version's cost model length, a digest of what a fixed set of programs evaluate to, and whether those programs give the expected results (`"selfTest": "pass"`).
93
+
94
+ ## Tests
95
+
96
+ ```bash
97
+ pnpm test
98
+ ```
99
+
100
+ `test/fixtures/builtins.json` holds builtin applications with the outcome and cost the evaluator gives them. It is generated by the crate's tests (`packages/uplc/rust/src/builtin/tests.rs`), which check each one against the machine; rerun `cargo test` with `UPDATE_BUILTIN_FIXTURE=1` after changing the cases.
101
+
102
+ `test/fixtures/programs.json` holds programs with their outcome and cost under Plutus V3 and protocol version 11, as given by the Haskell evaluator (plutus-core 1.71).
@@ -16,3 +16,219 @@ export function apply_params_to_script(
16
16
  params_bytes: Uint8Array,
17
17
  plutus_script_bytes: Uint8Array,
18
18
  ): Uint8Array;
19
+ /**
20
+ * Evaluates a UPLC program applied to Data arguments, outside of any
21
+ * transaction: the caller builds whatever arguments the program expects,
22
+ * including its script context.
23
+ *
24
+ * - `program_flat`: the program, flat-encoded without a CBOR wrapper.
25
+ * - `args_cbor`: the arguments, each one CBOR-encoded Data, applied in order.
26
+ * - `language`: the Plutus version to evaluate as, 1, 2 or 3.
27
+ * - `protocol_major_version`: the protocol version whose semantics to use,
28
+ * from 5 (Alonzo) to 12 (Dijkstra).
29
+ * - `cost_model`: the cost model parameters, every one the evaluator knows
30
+ * for this Plutus version (350 for Plutus V3), in the ledger's order.
31
+ * - `budget_cpu`, `budget_mem`: the budget.
32
+ * - `strict_budget`: charge every step as the machine takes it, and stop the
33
+ * program as soon as it goes over budget, instead of charging steps and
34
+ * checking the budget every 200 steps as the ledger does. Only the cost of a
35
+ * program that fails differs: the ledger does not charge the steps it took
36
+ * since the budget was last checked.
37
+ *
38
+ * Fails only when the inputs are malformed or unsupported. A program that fails
39
+ * is an [`EvalProgramResult`] that is not `ok`.
40
+ */
41
+ export function eval_program_raw(
42
+ program_flat: Uint8Array,
43
+ args_cbor: Uint8Array[],
44
+ language: number,
45
+ protocol_major_version: number,
46
+ cost_model: BigInt64Array,
47
+ budget_cpu: bigint,
48
+ budget_mem: bigint,
49
+ strict_budget: boolean,
50
+ ): EvalProgramResult;
51
+ /**
52
+ * Describes this build of the evaluator as JSON: the uplc crate it was built
53
+ * from, the length of each Plutus version's cost model, and a digest of what a
54
+ * fixed set of programs evaluate to, with their costs, under Plutus V3 and the
55
+ * latest protocol version. Two builds with the same digest agree on those
56
+ * programs; `selfTest` says whether this one gives the expected results.
57
+ */
58
+ export function uplc_build_info(): string;
59
+ /**
60
+ * Evaluates the builtin `tag` applied to `args` as a Plutus V3 script
61
+ * evaluates it at a protocol version, without any program around it: the
62
+ * result, or how it fails, is what the machine gives for the saturated
63
+ * application.
64
+ *
65
+ * - `tag`: the builtin's flat tag (0 for addInteger to 100 for scaleValue).
66
+ * The Miller-loop builtins (68 to 70) are not supported, since their
67
+ * results cannot be returned.
68
+ * - `args`: exactly the builtin's arguments, in order, each encoded as the
69
+ * matching entry of `arg_kinds` says, a [`BuiltinValueKind`]: 0 for a
70
+ * flat-encoded `(program 1.1.0 (con …))`, 1 for a compressed G1 point, 2
71
+ * for a compressed G2 point, 3 for a list of G1 points, 4 for a list of G2
72
+ * points, 5 for an array of G1 points and 6 for an array of G2 points, each
73
+ * list or array as the compressed points one after the other. Flat cannot
74
+ * encode BLS12-381 points, so they cross as their compressed bytes.
75
+ * - `protocol_major_version`: from 9 (Chang, which introduced Plutus V3) to
76
+ * 12 (Dijkstra). It decides which builtins are available and the semantics
77
+ * they have.
78
+ *
79
+ * The result's `cpu` and `mem` are the builtin's charge for the application
80
+ * under Plutus' default Plutus V3 cost model (not the ledger's protocol
81
+ * parameters), excluding the machine's per-step costs.
82
+ *
83
+ * Fails only when the inputs are malformed or unsupported: an unknown or
84
+ * Miller-loop tag, a number of arguments other than the builtin's arity, an
85
+ * argument that does not decode as its kind says or is not a constant, an
86
+ * invalid compressed point, a protocol version out of range, or a result that
87
+ * none of the kinds can encode. A builtin that fails is an
88
+ * [`EvalBuiltinResult`] that is not `ok`.
89
+ */
90
+ export function eval_builtin_raw(
91
+ tag: number,
92
+ args: Uint8Array[],
93
+ arg_kinds: Uint8Array,
94
+ protocol_major_version: number,
95
+ ): EvalBuiltinResult;
96
+ /**
97
+ * Why the builtin given to [`eval_builtin_raw`] did not return a value: the
98
+ * codes of the result's `error_kind`.
99
+ */
100
+ export enum BuiltinErrorKind {
101
+ /**
102
+ * The builtin returned a value.
103
+ */
104
+ Ok = 0,
105
+ /**
106
+ * The builtin is not available to Plutus V3 at the protocol version.
107
+ */
108
+ NotAvailable = 1,
109
+ /**
110
+ * The builtin did not accept its arguments, which fails the application
111
+ * before it is costed.
112
+ */
113
+ RejectedArguments = 2,
114
+ /**
115
+ * The builtin failed after it was costed.
116
+ */
117
+ Failed = 3,
118
+ }
119
+ /**
120
+ * How a value of [`eval_builtin_raw`] is encoded: the codes of its
121
+ * `arg_kinds` and of the result's `result_kind`.
122
+ */
123
+ export enum BuiltinValueKind {
124
+ /**
125
+ * A flat-encoded program whose body is a constant,
126
+ * `(program 1.1.0 (con …))`. A result in this form is always version
127
+ * 1.1.0.
128
+ */
129
+ Flat = 0,
130
+ /**
131
+ * A BLS12-381 G1 point, as its 48-byte compressed encoding.
132
+ */
133
+ G1 = 1,
134
+ /**
135
+ * A BLS12-381 G2 point, as its 96-byte compressed encoding.
136
+ */
137
+ G2 = 2,
138
+ /**
139
+ * A list of BLS12-381 G1 points, as their compressed encodings one after
140
+ * the other.
141
+ */
142
+ G1List = 3,
143
+ /**
144
+ * A list of BLS12-381 G2 points, as their compressed encodings one after
145
+ * the other.
146
+ */
147
+ G2List = 4,
148
+ /**
149
+ * An array of BLS12-381 G1 points, as their compressed encodings one after
150
+ * the other.
151
+ */
152
+ G1Array = 5,
153
+ /**
154
+ * An array of BLS12-381 G2 points, as their compressed encodings one after
155
+ * the other.
156
+ */
157
+ G2Array = 6,
158
+ }
159
+ /**
160
+ * The outcome of [`eval_builtin_raw`].
161
+ */
162
+ export class EvalBuiltinResult {
163
+ private constructor();
164
+ free(): void;
165
+ /**
166
+ * Whether the builtin returned a value.
167
+ */
168
+ ok: boolean;
169
+ /**
170
+ * Why the builtin did not return a value, a [`BuiltinErrorKind`]: 0 when
171
+ * it did, 1 when it is not available to Plutus V3 at the protocol version,
172
+ * 2 when it did not accept its arguments (so the machine fails before
173
+ * costing it) and 3 when it failed after being costed.
174
+ */
175
+ error_kind: number;
176
+ /**
177
+ * How `result` encodes the value, a [`BuiltinValueKind`] as for the
178
+ * arguments of [`eval_builtin_raw`]; 0 when there is no value.
179
+ */
180
+ result_kind: number;
181
+ /**
182
+ * The value the builtin returned; empty when there is none.
183
+ */
184
+ result: Uint8Array;
185
+ /**
186
+ * The CPU the builtin's cost model charges for this application, under
187
+ * Plutus' default cost model for Plutus V3 at the protocol version; 0
188
+ * when the builtin was not costed (error kinds 1 and 2). It leaves out the
189
+ * machine's own step costs for evaluating the application (the builtin,
190
+ * each force and each apply).
191
+ */
192
+ cpu: bigint;
193
+ /**
194
+ * The memory the builtin's cost model charges for this application; see
195
+ * `cpu`.
196
+ */
197
+ mem: bigint;
198
+ /**
199
+ * The evaluator's description of why the builtin did not return a value;
200
+ * empty when it did.
201
+ */
202
+ error: string;
203
+ }
204
+ /**
205
+ * The outcome of [`eval_program_raw`].
206
+ */
207
+ export class EvalProgramResult {
208
+ private constructor();
209
+ free(): void;
210
+ /**
211
+ * Whether the program succeeded: it did not fail and, for Plutus V3, it
212
+ * returned unit.
213
+ */
214
+ ok: boolean;
215
+ /**
216
+ * The CPU the program used. A program that went over budget is charged
217
+ * what it used up to the point where the machine stopped it.
218
+ */
219
+ cpu: bigint;
220
+ /**
221
+ * The memory the program used.
222
+ */
223
+ mem: bigint;
224
+ /**
225
+ * Why the program did not succeed: 0 when it did, 1 when it went over
226
+ * budget, 2 when it failed in any other way and 3 when a Plutus V3 program
227
+ * returned something other than unit.
228
+ */
229
+ error_kind: number;
230
+ /**
231
+ * The messages the program traced, in order.
232
+ */
233
+ logs: string[];
234
+ }