@helios-lang/effect 0.6.7 → 0.6.8

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.
@@ -39,28 +39,7 @@ export const addIntegerV1 = {
39
39
  cpuModel: Cost.Linear(0, 1)(Cost.Max),
40
40
  memModel: Cost.Linear(2, 3)(Cost.Max),
41
41
  call: ([a, b]) => {
42
- if (a === undefined) {
43
- throw new Error("a is undefined in addInteger()");
44
- }
45
- if (b === undefined) {
46
- throw new Error("b is undefined in addInteger");
47
- }
48
- if (a._tag != "Const") {
49
- return Either.left(new WrongArgType(0, "Const", a._tag));
50
- }
51
- if (typeof a.value != "bigint") {
52
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
53
- }
54
- if (b._tag != "Const") {
55
- return Either.left(new WrongArgType(1, "Const", b._tag));
56
- }
57
- if (typeof b.value != "bigint") {
58
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
59
- }
60
- return Either.right({
61
- _tag: "Const",
62
- value: a.value + b.value
63
- });
42
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a + b })));
64
43
  }
65
44
  };
66
45
  export const subtractIntegerV1 = {
@@ -70,28 +49,7 @@ export const subtractIntegerV1 = {
70
49
  cpuModel: Cost.Linear(145, 146)(Cost.Max),
71
50
  memModel: Cost.Linear(147, 148)(Cost.Max),
72
51
  call: ([a, b]) => {
73
- if (a === undefined) {
74
- throw new Error("a is undefined in subtractInteger()");
75
- }
76
- if (b === undefined) {
77
- throw new Error("b is undefined in subtractInteger()");
78
- }
79
- if (a._tag != "Const") {
80
- return Either.left(new WrongArgType(0, "Const", a._tag));
81
- }
82
- if (typeof a.value != "bigint") {
83
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
84
- }
85
- if (b._tag != "Const") {
86
- return Either.left(new WrongArgType(1, "Const", b._tag));
87
- }
88
- if (typeof b.value != "bigint") {
89
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
90
- }
91
- return Either.right({
92
- _tag: "Const",
93
- value: a.value - b.value
94
- });
52
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a - b })));
95
53
  }
96
54
  };
97
55
  export const subtractIntegerV2 = {
@@ -111,28 +69,7 @@ export const multiplyIntegerV1 = {
111
69
  cpuModel: Cost.Linear(115, 116)(Cost.Sum),
112
70
  memModel: Cost.Linear(117, 118)(Cost.Sum),
113
71
  call: ([a, b]) => {
114
- if (a === undefined) {
115
- throw new Error("a is undefined in multiplyInteger()");
116
- }
117
- if (b === undefined) {
118
- throw new Error("b is undefined in multiplyInteger()");
119
- }
120
- if (a._tag != "Const") {
121
- return Either.left(new WrongArgType(0, "Const", a._tag));
122
- }
123
- if (typeof a.value != "bigint") {
124
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
125
- }
126
- if (b._tag != "Const") {
127
- return Either.left(new WrongArgType(1, "Const", b._tag));
128
- }
129
- if (typeof b.value != "bigint") {
130
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
131
- }
132
- return Either.right({
133
- _tag: "Const",
134
- value: a.value * b.value
135
- });
72
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a * b })));
136
73
  }
137
74
  };
138
75
  export const multiplyIntegerV2 = {
@@ -152,25 +89,7 @@ export const divideIntegerV1 = {
152
89
  cpuModel: Cost.ConstantBelowDiag(49)(Cost.Linear(50, 51)(Cost.Prod)),
153
90
  memModel: Cost.AtLeast(53)(Cost.Diff),
154
91
  call: ([a, b]) => {
155
- if (a === undefined) {
156
- throw new Error("a is undefined in divideInteger()");
157
- }
158
- if (b === undefined) {
159
- throw new Error("b is undefined in divideInteger()");
160
- }
161
- if (a._tag != "Const") {
162
- return Either.left(new WrongArgType(0, "Const", a._tag));
163
- }
164
- if (typeof a.value != "bigint") {
165
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
166
- }
167
- if (b._tag != "Const") {
168
- return Either.left(new WrongArgType(1, "Const", b._tag));
169
- }
170
- if (typeof b.value != "bigint") {
171
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
172
- }
173
- return evalDivide(a.value, b.value).pipe(Either.map((result) => ({ _tag: "Const", value: result })));
92
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.flatMap(([a, b]) => evalDivide(a, b)), Either.map((value) => ({ _tag: "Const", value })));
174
93
  }
175
94
  };
176
95
  export function evalDivide(x, y) {
@@ -192,25 +111,7 @@ export const quotientIntegerV1 = {
192
111
  cpuModel: Cost.ConstantBelowDiag(121)(Cost.Linear(122, 123)(Cost.Prod)),
193
112
  memModel: Cost.AtLeast(125)(Cost.Diff),
194
113
  call: ([a, b]) => {
195
- if (a === undefined) {
196
- throw new Error("a is undefined in quotientInteger()");
197
- }
198
- if (b === undefined) {
199
- throw new Error("b is undefined in quotientInteger()");
200
- }
201
- if (a._tag != "Const") {
202
- return Either.left(new WrongArgType(0, "Const", a._tag));
203
- }
204
- if (typeof a.value != "bigint") {
205
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
206
- }
207
- if (b._tag != "Const") {
208
- return Either.left(new WrongArgType(1, "Const", b._tag));
209
- }
210
- if (typeof b.value != "bigint") {
211
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
212
- }
213
- return evalQuotient(a.value, b.value).pipe(Either.map((result) => ({ _tag: "Const", value: result })));
114
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.flatMap(([a, b]) => evalQuotient(a, b)), Either.map((value) => ({ _tag: "Const", value })));
214
115
  }
215
116
  };
216
117
  export function evalQuotient(x, y) {
@@ -238,25 +139,7 @@ export const remainderIntegerV1 = {
238
139
  cpuModel: Cost.ConstantBelowDiag(127)(Cost.Linear(128, 129)(Cost.Prod)),
239
140
  memModel: Cost.AtLeast(131)(Cost.Diff),
240
141
  call: ([a, b]) => {
241
- if (a === undefined) {
242
- throw new Error("a is undefined in remainderInteger()");
243
- }
244
- if (b === undefined) {
245
- throw new Error("b is undefined in remainderInteger()");
246
- }
247
- if (a._tag != "Const") {
248
- return Either.left(new WrongArgType(0, "Const", a._tag));
249
- }
250
- if (typeof a.value != "bigint") {
251
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
252
- }
253
- if (b._tag != "Const") {
254
- return Either.left(new WrongArgType(1, "Const", b._tag));
255
- }
256
- if (typeof b.value != "bigint") {
257
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
258
- }
259
- return evalRemainder(a.value, b.value).pipe(Either.map((result) => ({ _tag: "Const", value: result })));
142
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.flatMap(([a, b]) => evalRemainder(a, b)), Either.map((value) => ({ _tag: "Const", value })));
260
143
  }
261
144
  };
262
145
  export function evalRemainder(x, y) {
@@ -284,25 +167,7 @@ export const modIntegerV1 = {
284
167
  cpuModel: Cost.ConstantBelowDiag(109)(Cost.Linear(110, 111)(Cost.Prod)),
285
168
  memModel: Cost.AtLeast(113)(Cost.Diff),
286
169
  call: ([a, b]) => {
287
- if (a === undefined) {
288
- throw new Error("a is undefined in modInteger()");
289
- }
290
- if (b === undefined) {
291
- throw new Error("b is undefined in modInteger()");
292
- }
293
- if (a._tag != "Const") {
294
- return Either.left(new WrongArgType(0, "Const", a._tag));
295
- }
296
- if (typeof a.value != "bigint") {
297
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
298
- }
299
- if (b._tag != "Const") {
300
- return Either.left(new WrongArgType(1, "Const", b._tag));
301
- }
302
- if (typeof b.value != "bigint") {
303
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
304
- }
305
- return evalMod(a.value, b.value).pipe(Either.map((result) => ({ _tag: "Const", value: result })));
170
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.flatMap(([a, b]) => evalMod(a, b)), Either.map((value) => ({ _tag: "Const", value })));
306
171
  }
307
172
  };
308
173
  export function evalMod(x, y) {
@@ -339,28 +204,7 @@ export const equalsIntegerV1 = {
339
204
  cpuModel: Cost.Linear(66, 67)(Cost.Min),
340
205
  memModel: Cost.Constant(68),
341
206
  call: ([a, b]) => {
342
- if (a === undefined) {
343
- throw new Error("a is undefined in equalsInteger()");
344
- }
345
- if (b === undefined) {
346
- throw new Error("b is undefined in equalsInteger()");
347
- }
348
- if (a._tag != "Const") {
349
- return Either.left(new WrongArgType(0, "Const", a._tag));
350
- }
351
- if (typeof a.value != "bigint") {
352
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
353
- }
354
- if (b._tag != "Const") {
355
- return Either.left(new WrongArgType(1, "Const", b._tag));
356
- }
357
- if (typeof b.value != "bigint") {
358
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
359
- }
360
- return Either.right({
361
- _tag: "Const",
362
- value: a.value === b.value
363
- });
207
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a === b })));
364
208
  }
365
209
  };
366
210
  export const equalsIntegerV3 = {
@@ -375,28 +219,7 @@ export const lessThanIntegerV1 = {
375
219
  cpuModel: Cost.Linear(94, 95)(Cost.Min),
376
220
  memModel: Cost.Constant(96),
377
221
  call: ([a, b]) => {
378
- if (a === undefined) {
379
- throw new Error("a is undefined in lessThanInteger()");
380
- }
381
- if (b === undefined) {
382
- throw new Error("b is undefined in lessThanInteger()");
383
- }
384
- if (a._tag != "Const") {
385
- return Either.left(new WrongArgType(0, "Const", a._tag));
386
- }
387
- if (typeof a.value != "bigint") {
388
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
389
- }
390
- if (b._tag != "Const") {
391
- return Either.left(new WrongArgType(1, "Const", b._tag));
392
- }
393
- if (typeof b.value != "bigint") {
394
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
395
- }
396
- return Either.right({
397
- _tag: "Const",
398
- value: a.value < b.value
399
- });
222
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a < b })));
400
223
  }
401
224
  };
402
225
  export const lessThanIntegerV3 = {
@@ -411,28 +234,7 @@ export const lessThanEqualsIntegerV1 = {
411
234
  cpuModel: Cost.Linear(91, 92)(Cost.Min),
412
235
  memModel: Cost.Constant(93),
413
236
  call: ([a, b]) => {
414
- if (a === undefined) {
415
- throw new Error("a is undefined in lessThanEqualsInteger()");
416
- }
417
- if (b === undefined) {
418
- throw new Error("b is undefined in lessThanEqualsInteger()");
419
- }
420
- if (a._tag != "Const") {
421
- return Either.left(new WrongArgType(0, "Const", a._tag));
422
- }
423
- if (typeof a.value != "bigint") {
424
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
425
- }
426
- if (b._tag != "Const") {
427
- return Either.left(new WrongArgType(1, "Const", b._tag));
428
- }
429
- if (typeof b.value != "bigint") {
430
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
431
- }
432
- return Either.right({
433
- _tag: "Const",
434
- value: a.value <= b.value
435
- });
237
+ return Either.all([expectInteger(a, 0), expectInteger(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a <= b })));
436
238
  }
437
239
  };
438
240
  export const lessThanEqualsIntegerV3 = {
@@ -447,28 +249,7 @@ export const appendByteStringV1 = {
447
249
  cpuModel: Cost.Linear(4, 5)(Cost.Sum),
448
250
  memModel: Cost.Linear(6, 7)(Cost.Sum),
449
251
  call: ([a, b]) => {
450
- if (a === undefined) {
451
- throw new Error("a is undefined in appendByteString()");
452
- }
453
- if (b === undefined) {
454
- throw new Error("b is undefined in appendByteString()");
455
- }
456
- if (a._tag != "Const") {
457
- return Either.left(new WrongArgType(0, "Const", a._tag));
458
- }
459
- if (!(a.value instanceof Uint8Array)) {
460
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
461
- }
462
- if (b._tag != "Const") {
463
- return Either.left(new WrongArgType(1, "Const", b._tag));
464
- }
465
- if (!(b.value instanceof Uint8Array)) {
466
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(b.value)));
467
- }
468
- return Either.right({
469
- _tag: "Const",
470
- value: Bytes.concat(a.value, b.value)
471
- });
252
+ return Either.all([expectBytes(a, 0), expectBytes(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: Bytes.concat(a, b) })));
472
253
  }
473
254
  };
474
255
  export const consByteStringV1 = {
@@ -478,28 +259,10 @@ export const consByteStringV1 = {
478
259
  cpuModel: Cost.Linear(39, 40)(Cost.Second),
479
260
  memModel: Cost.Linear(41, 42)(Cost.Sum),
480
261
  call: ([a, b]) => {
481
- if (a === undefined) {
482
- throw new Error("a is undefined in consByteString()");
483
- }
484
- if (b === undefined) {
485
- throw new Error("b is undefined in consByteString()");
486
- }
487
- if (a._tag != "Const") {
488
- return Either.left(new WrongArgType(0, "Const", a._tag));
489
- }
490
- if (typeof a.value != "bigint") {
491
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
492
- }
493
- if (b._tag != "Const") {
494
- return Either.left(new WrongArgType(1, "Const", b._tag));
495
- }
496
- if (!(b.value instanceof Uint8Array)) {
497
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(b.value)));
498
- }
499
- return Either.right({
262
+ return Either.all([expectInteger(a, 0), expectBytes(b, 1)]).pipe(Either.map(([a, b]) => ({
500
263
  _tag: "Const",
501
- value: Bytes.concat([Number(a.value % 256n)], b.value)
502
- });
264
+ value: Bytes.concat([Number(a % 256n)], b)
265
+ })));
503
266
  }
504
267
  };
505
268
  export const sliceByteStringV1 = {
@@ -509,41 +272,16 @@ export const sliceByteStringV1 = {
509
272
  cpuModel: Cost.Linear(139, 140)(Cost.Third),
510
273
  memModel: Cost.Linear(141, 142)(Cost.Third),
511
274
  call: ([a, b, c]) => {
512
- if (a === undefined) {
513
- throw new Error("a is undefined in sliceByteString()");
514
- }
515
- if (b === undefined) {
516
- throw new Error("b is undefined in sliceByteString()");
517
- }
518
- if (c === undefined) {
519
- throw new Error("c is undefined in sliceByteString()");
520
- }
521
- if (a._tag != "Const") {
522
- return Either.left(new WrongArgType(0, "Const", a._tag));
523
- }
524
- if (typeof a.value != "bigint") {
525
- return Either.left(new WrongArgType(0, "integer", Value.describeType(a.value)));
526
- }
527
- if (b._tag != "Const") {
528
- return Either.left(new WrongArgType(1, "Const", b._tag));
529
- }
530
- if (typeof b.value != "bigint") {
531
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
532
- }
533
- if (c._tag != "Const") {
534
- return Either.left(new WrongArgType(2, "Const", c._tag));
535
- }
536
- if (!(c.value instanceof Uint8Array)) {
537
- return Either.left(new WrongArgType(2, "bytes", Value.describeType(c.value)));
538
- }
539
- const bytes = c.value;
540
- const start = Math.max(Number(a.value), 0);
541
- const end = Math.min(start + Number(b.value) - 1, bytes.length - 1);
542
- const res = end < start ? new Uint8Array([]) : bytes.slice(start, end + 1);
543
- return Either.right({
544
- _tag: "Const",
545
- value: res
546
- });
275
+ return Either.all([
276
+ expectInteger(a, 0),
277
+ expectInteger(b, 1),
278
+ expectBytes(c, 2)
279
+ ]).pipe(Either.map(([a, b, bytes]) => {
280
+ const start = Math.max(Number(a), 0);
281
+ const end = Math.min(start + Number(b) - 1, bytes.length - 1);
282
+ const value = end < start ? new Uint8Array([]) : bytes.slice(start, end + 1);
283
+ return { _tag: "Const", value };
284
+ }));
547
285
  }
548
286
  };
549
287
  export const sliceByteStringV2 = {
@@ -563,19 +301,7 @@ export const lengthOfByteStringV1 = {
563
301
  cpuModel: Cost.Constant(83),
564
302
  memModel: Cost.Constant(84),
565
303
  call: ([a]) => {
566
- if (a === undefined) {
567
- throw new Error("a is undefined in lengthOfByteString()");
568
- }
569
- if (a._tag != "Const") {
570
- return Either.left(new WrongArgType(0, "Const", a._tag));
571
- }
572
- if (!(a.value instanceof Uint8Array)) {
573
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
574
- }
575
- return Either.right({
576
- _tag: "Const",
577
- value: BigInt(a.value.length)
578
- });
304
+ return expectBytes(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: BigInt(a.length) })));
579
305
  }
580
306
  };
581
307
  export const lengthOfByteStringV3 = {
@@ -590,33 +316,12 @@ export const indexByteStringV1 = {
590
316
  cpuModel: Cost.Constant(81),
591
317
  memModel: Cost.Constant(82),
592
318
  call: ([a, b]) => {
593
- if (a === undefined) {
594
- throw new Error("a is undefined in indexByteString()");
595
- }
596
- if (b === undefined) {
597
- throw new Error("b is undefined in indexByteString()");
598
- }
599
- if (a._tag != "Const") {
600
- return Either.left(new WrongArgType(0, "Const", a._tag));
601
- }
602
- if (!(a.value instanceof Uint8Array)) {
603
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
604
- }
605
- if (b._tag != "Const") {
606
- return Either.left(new WrongArgType(1, "Const", b._tag));
607
- }
608
- if (typeof b.value != "bigint") {
609
- return Either.left(new WrongArgType(1, "integer", Value.describeType(b.value)));
610
- }
611
- const bytes = a.value;
612
- const i = Number(b.value);
613
- if (i < 0 || i >= bytes.length) {
614
- return Either.left(new OutOfRange(bytes.length, i));
615
- }
616
- return Either.right({
617
- _tag: "Const",
618
- value: BigInt(bytes[i])
619
- });
319
+ return Either.all([expectBytes(a, 0), expectInteger(b, 1)]).pipe(Either.flatMap(([bytes, b]) => {
320
+ const i = Number(b);
321
+ return i < 0 || i >= bytes.length
322
+ ? Either.left(new OutOfRange(bytes.length, i))
323
+ : Either.right({ _tag: "Const", value: BigInt(bytes[i]) });
324
+ }));
620
325
  }
621
326
  };
622
327
  export const indexByteStringV3 = {
@@ -631,28 +336,7 @@ export const equalsByteStringV1 = {
631
336
  cpuModel: Cost.ConstantOffDiag(59)(Cost.Linear(60, 61)(Cost.First)),
632
337
  memModel: Cost.Constant(62),
633
338
  call: ([a, b]) => {
634
- if (a === undefined) {
635
- throw new Error("a is undefined in equalsByteString()");
636
- }
637
- if (b === undefined) {
638
- throw new Error("b is undefined in equalsByteString()");
639
- }
640
- if (a._tag != "Const") {
641
- return Either.left(new WrongArgType(0, "Const", a._tag));
642
- }
643
- if (!(a.value instanceof Uint8Array)) {
644
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
645
- }
646
- if (b._tag != "Const") {
647
- return Either.left(new WrongArgType(1, "Const", b._tag));
648
- }
649
- if (!(b.value instanceof Uint8Array)) {
650
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(b.value)));
651
- }
652
- return Either.right({
653
- _tag: "Const",
654
- value: Bytes.equals(a.value, b.value)
655
- });
339
+ return Either.all([expectBytes(a, 0), expectBytes(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: Bytes.equals(a, b) })));
656
340
  }
657
341
  };
658
342
  export const equalsByteStringV3 = {
@@ -667,28 +351,7 @@ export const lessThanByteStringV1 = {
667
351
  cpuModel: Cost.Linear(85, 86)(Cost.Min),
668
352
  memModel: Cost.Constant(87),
669
353
  call: ([a, b]) => {
670
- if (a === undefined) {
671
- throw new Error("a is undefined in lessThanByteString()");
672
- }
673
- if (b === undefined) {
674
- throw new Error("b is undefined in lessThanByteString()");
675
- }
676
- if (a._tag != "Const") {
677
- return Either.left(new WrongArgType(0, "Const", a._tag));
678
- }
679
- if (!(a.value instanceof Uint8Array)) {
680
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
681
- }
682
- if (b._tag != "Const") {
683
- return Either.left(new WrongArgType(1, "Const", b._tag));
684
- }
685
- if (!(b.value instanceof Uint8Array)) {
686
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(b.value)));
687
- }
688
- return Either.right({
689
- _tag: "Const",
690
- value: Bytes.compare(a.value, b.value) == -1
691
- });
354
+ return Either.all([expectBytes(a, 0), expectBytes(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: Bytes.compare(a, b) == -1 })));
692
355
  }
693
356
  };
694
357
  export const lessThanByteStringV3 = {
@@ -703,28 +366,7 @@ export const lessThanEqualsByteStringV1 = {
703
366
  cpuModel: Cost.Linear(88, 89)(Cost.Min),
704
367
  memModel: Cost.Constant(90),
705
368
  call: ([a, b]) => {
706
- if (a === undefined) {
707
- throw new Error("a is undefined in lessThanEqualsByteString()");
708
- }
709
- if (b === undefined) {
710
- throw new Error("b is undefined in lessThanEqualsByteString()");
711
- }
712
- if (a._tag != "Const") {
713
- return Either.left(new WrongArgType(0, "Const", a._tag));
714
- }
715
- if (!(a.value instanceof Uint8Array)) {
716
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
717
- }
718
- if (b._tag != "Const") {
719
- return Either.left(new WrongArgType(1, "Const", b._tag));
720
- }
721
- if (!(b.value instanceof Uint8Array)) {
722
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(b.value)));
723
- }
724
- return Either.right({
725
- _tag: "Const",
726
- value: Bytes.compare(a.value, b.value) <= 0
727
- });
369
+ return Either.all([expectBytes(a, 0), expectBytes(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: Bytes.compare(a, b) <= 0 })));
728
370
  }
729
371
  };
730
372
  export const lessThanEqualsByteStringV3 = {
@@ -739,19 +381,7 @@ export const sha2_256V1 = {
739
381
  cpuModel: Cost.Linear(133, 134)(Cost.First),
740
382
  memModel: Cost.Constant(135),
741
383
  call: ([a]) => {
742
- if (a === undefined) {
743
- throw new Error("a is undefined in sha2_256()");
744
- }
745
- if (a._tag != "Const") {
746
- return Either.left(new WrongArgType(0, "Const", a._tag));
747
- }
748
- if (!(a.value instanceof Uint8Array)) {
749
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
750
- }
751
- return Either.right({
752
- _tag: "Const",
753
- value: Crypto.Sha2_256.hashSync(a.value)
754
- });
384
+ return expectBytes(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: Crypto.Sha2_256.hashSync(a) })));
755
385
  }
756
386
  };
757
387
  export const sha2_256V2 = {
@@ -771,19 +401,7 @@ export const sha3_256V1 = {
771
401
  cpuModel: Cost.Linear(136, 137)(Cost.First),
772
402
  memModel: Cost.Constant(138),
773
403
  call: ([a]) => {
774
- if (a === undefined) {
775
- throw new Error("a is undefined in sha3_256()");
776
- }
777
- if (a._tag != "Const") {
778
- return Either.left(new WrongArgType(0, "Const", a._tag));
779
- }
780
- if (!(a.value instanceof Uint8Array)) {
781
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
782
- }
783
- return Either.right({
784
- _tag: "Const",
785
- value: Crypto.Sha3_256.hashSync(a.value)
786
- });
404
+ return expectBytes(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: Crypto.Sha3_256.hashSync(a) })));
787
405
  }
788
406
  };
789
407
  export const sha3_256V2 = {
@@ -803,19 +421,7 @@ export const blake2b_256V1 = {
803
421
  cpuModel: Cost.Linear(14, 15)(Cost.First),
804
422
  memModel: Cost.Constant(16),
805
423
  call: ([a]) => {
806
- if (a === undefined) {
807
- throw new Error("a is undefined in blake2b_256()");
808
- }
809
- if (a._tag != "Const") {
810
- return Either.left(new WrongArgType(0, "Const", a._tag));
811
- }
812
- if (!(a.value instanceof Uint8Array)) {
813
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
814
- }
815
- return Either.right({
816
- _tag: "Const",
817
- value: Crypto.Blake2b.hashSync(a.value)
818
- });
424
+ return expectBytes(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: Crypto.Blake2b.hashSync(a) })));
819
425
  }
820
426
  };
821
427
  export const verifyEd25519SignatureV1 = {
@@ -825,45 +431,22 @@ export const verifyEd25519SignatureV1 = {
825
431
  cpuModel: Cost.Linear(163, 164)(Cost.Third),
826
432
  memModel: Cost.Constant(165),
827
433
  call: ([pk, message, signature]) => {
828
- if (pk === undefined) {
829
- throw new Error("pk is undefined in verifyEd25519Signature()");
830
- }
831
- if (message === undefined) {
832
- throw new Error("message is undefined in verifyEd25519Signature()");
833
- }
834
- if (signature === undefined) {
835
- throw new Error("signature is undefined in verifyEd25519Signature()");
836
- }
837
- if (pk._tag != "Const") {
838
- return Either.left(new WrongArgType(0, "Const", pk._tag));
839
- }
840
- if (!(pk.value instanceof Uint8Array)) {
841
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(pk.value)));
842
- }
843
- if (pk.value.length != 32) {
844
- return Either.left(new InvalidLength("verifyEd25519Signature", "publicKey", 32, pk.value.length));
845
- }
846
- if (message._tag != "Const") {
847
- return Either.left(new WrongArgType(1, "Const", message._tag));
848
- }
849
- if (!(message.value instanceof Uint8Array)) {
850
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(message.value)));
851
- }
852
- if (signature._tag != "Const") {
853
- return Either.left(new WrongArgType(1, "Const", signature._tag));
854
- }
855
- if (!(signature.value instanceof Uint8Array)) {
856
- return Either.left(new WrongArgType(1, "bytes", Value.describeType(signature.value)));
857
- }
858
- if (signature.value.length != 64) {
859
- return Either.left(new InvalidLength("verifyEd25519Signature", "signature", 64, pk.value.length));
860
- }
861
- // length has been validated above
862
- const b = Either.getOrThrow(Crypto.Ed25519.verify(signature.value, message.value, pk.value));
863
- return Either.right({
864
- _tag: "Const",
865
- value: b
866
- });
434
+ return Either.all([
435
+ expectBytes(pk, 0),
436
+ expectBytes(message, 1),
437
+ expectBytes(signature, 2)
438
+ ]).pipe(Either.flatMap(([pk, message, signature]) => {
439
+ if (pk.length != 32) {
440
+ return Either.left(new InvalidLength("verifyEd25519Signature", "publicKey", 32, pk.length));
441
+ }
442
+ if (signature.length != 64) {
443
+ return Either.left(new InvalidLength("verifyEd25519Signature", "signature", 64, signature.length));
444
+ }
445
+ return Either.right({
446
+ _tag: "Const",
447
+ value: Either.getOrThrow(Crypto.Ed25519.verify(signature, message, pk))
448
+ });
449
+ }));
867
450
  }
868
451
  };
869
452
  export const verifyEd25519SignatureV2 = {
@@ -883,28 +466,7 @@ export const appendStringV1 = {
883
466
  cpuModel: Cost.Linear(8, 9)(Cost.Sum),
884
467
  memModel: Cost.Linear(10, 11)(Cost.Sum),
885
468
  call: ([a, b]) => {
886
- if (a === undefined) {
887
- throw new Error("a is undefined in appendString()");
888
- }
889
- if (b === undefined) {
890
- throw new Error("b is undefined in appendString()");
891
- }
892
- if (a._tag != "Const") {
893
- return Either.left(new WrongArgType(0, "Const", a._tag));
894
- }
895
- if (typeof a.value != "string") {
896
- return Either.left(new WrongArgType(0, "string", Value.describeType(a.value)));
897
- }
898
- if (b._tag != "Const") {
899
- return Either.left(new WrongArgType(1, "Const", b._tag));
900
- }
901
- if (typeof b.value != "string") {
902
- return Either.left(new WrongArgType(1, "string", Value.describeType(b.value)));
903
- }
904
- return Either.right({
905
- _tag: "Const",
906
- value: a.value + b.value
907
- });
469
+ return Either.all([expectString(a, 0), expectString(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a + b })));
908
470
  }
909
471
  };
910
472
  export const equalsStringV1 = {
@@ -914,28 +476,7 @@ export const equalsStringV1 = {
914
476
  cpuModel: Cost.ConstantOffDiag(69)(Cost.Linear(70, 71)(Cost.First)),
915
477
  memModel: Cost.Constant(72),
916
478
  call: ([a, b]) => {
917
- if (a === undefined) {
918
- throw new Error("a is undefined in equalsString()");
919
- }
920
- if (b === undefined) {
921
- throw new Error("b is undefined in equalsString()");
922
- }
923
- if (a._tag != "Const") {
924
- return Either.left(new WrongArgType(0, "Const", a._tag));
925
- }
926
- if (typeof a.value != "string") {
927
- return Either.left(new WrongArgType(0, "string", Value.describeType(a.value)));
928
- }
929
- if (b._tag != "Const") {
930
- return Either.left(new WrongArgType(1, "Const", b._tag));
931
- }
932
- if (typeof b.value != "string") {
933
- return Either.left(new WrongArgType(1, "string", Value.describeType(b.value)));
934
- }
935
- return Either.right({
936
- _tag: "Const",
937
- value: a.value === b.value
938
- });
479
+ return Either.all([expectString(a, 0), expectString(b, 1)]).pipe(Either.map(([a, b]) => ({ _tag: "Const", value: a === b })));
939
480
  }
940
481
  };
941
482
  export const equalsStringV3 = {
@@ -950,19 +491,7 @@ export const encodeUtf8V1 = {
950
491
  cpuModel: Cost.Linear(55, 56)(Cost.First),
951
492
  memModel: Cost.Linear(57, 58)(Cost.First),
952
493
  call: ([a]) => {
953
- if (a === undefined) {
954
- throw new Error("a is undefined in encodeUtf8()");
955
- }
956
- if (a._tag != "Const") {
957
- return Either.left(new WrongArgType(0, "Const", a._tag));
958
- }
959
- if (typeof a.value != "string") {
960
- return Either.left(new WrongArgType(0, "string", Value.describeType(a.value)));
961
- }
962
- return Either.right({
963
- _tag: "Const",
964
- value: Utf8.encode(a.value)
965
- });
494
+ return expectString(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: Utf8.encode(a) })));
966
495
  }
967
496
  };
968
497
  export const encodeUtf8V3 = {
@@ -977,16 +506,7 @@ export const decodeUtf8V1 = {
977
506
  cpuModel: Cost.Linear(45, 46)(Cost.First),
978
507
  memModel: Cost.Linear(47, 48)(Cost.First),
979
508
  call: ([a]) => {
980
- if (a === undefined) {
981
- throw new Error("a is undefined in decodeUtf8()");
982
- }
983
- if (a._tag != "Const") {
984
- return Either.left(new WrongArgType(0, "Const", a._tag));
985
- }
986
- if (!(a.value instanceof Uint8Array)) {
987
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(a.value)));
988
- }
989
- return Utf8.decode(a.value).pipe(Either.map((s) => ({ _tag: "Const", value: s })));
509
+ return expectBytes(a, 0).pipe(Either.flatMap((a) => Utf8.decode(a)), Either.map((s) => ({ _tag: "Const", value: s })));
990
510
  }
991
511
  };
992
512
  export const ifThenElseV1 = {
@@ -996,22 +516,13 @@ export const ifThenElseV1 = {
996
516
  cpuModel: Cost.Constant(79),
997
517
  memModel: Cost.Constant(80),
998
518
  call: ([cond, a, b]) => {
999
- if (cond === undefined) {
1000
- throw new Error("cond is undefined in ifThenElse()");
1001
- }
1002
519
  if (a === undefined) {
1003
520
  throw new Error("a is undefined in ifThenElse()");
1004
521
  }
1005
522
  if (b === undefined) {
1006
523
  throw new Error("b is undefined in ifThenElse()");
1007
524
  }
1008
- if (cond._tag != "Const") {
1009
- return Either.left(new WrongArgType(0, "Const", cond._tag));
1010
- }
1011
- if (typeof cond.value != "boolean") {
1012
- return Either.left(new WrongArgType(0, "bool", Value.describeType(cond.value)));
1013
- }
1014
- return Either.right(cond.value ? a : b);
525
+ return expectBool(cond, 0).pipe(Either.map((cond) => (cond ? a : b)));
1015
526
  }
1016
527
  };
1017
528
  export const ifThenElseV3 = {
@@ -1026,19 +537,10 @@ export const chooseUnitV1 = {
1026
537
  cpuModel: Cost.Constant(37),
1027
538
  memModel: Cost.Constant(38),
1028
539
  call: ([a, b]) => {
1029
- if (a === undefined) {
1030
- throw new Error("a is undefined in chooseUnit()");
1031
- }
1032
540
  if (b === undefined) {
1033
541
  throw new Error("b is undefined in chooseUnit()");
1034
542
  }
1035
- if (a._tag != "Const") {
1036
- return Either.left(new WrongArgType(0, "Const", a._tag));
1037
- }
1038
- if (a.value !== null) {
1039
- return Either.left(new WrongArgType(0, "unit", Value.describeType(a.value)));
1040
- }
1041
- return Either.right(b);
543
+ return expectUnit(a, 0).pipe(Either.map(() => b));
1042
544
  }
1043
545
  };
1044
546
  export const traceV1 = {
@@ -1048,20 +550,13 @@ export const traceV1 = {
1048
550
  cpuModel: Cost.Constant(151),
1049
551
  memModel: Cost.Constant(152),
1050
552
  call: ([message, after], ctx) => {
1051
- if (message === undefined) {
1052
- throw new Error("message is undefined in trace()");
1053
- }
1054
553
  if (after === undefined) {
1055
554
  throw new Error("after is undefined in trace()");
1056
555
  }
1057
- if (message._tag != "Const") {
1058
- return Either.left(new WrongArgType(0, "Const", message._tag));
1059
- }
1060
- if (typeof message.value != "string") {
1061
- return Either.left(new WrongArgType(0, "string", Value.describeType(message.value)));
1062
- }
1063
- ctx.print(message.value);
1064
- return Either.right(after);
556
+ return expectString(message, 0).pipe(Either.map((message) => {
557
+ ctx.print(message);
558
+ return after;
559
+ }));
1065
560
  }
1066
561
  };
1067
562
  export const traceV2 = {
@@ -1081,19 +576,7 @@ export const fstPairV1 = {
1081
576
  cpuModel: Cost.Constant(73),
1082
577
  memModel: Cost.Constant(74),
1083
578
  call: ([a]) => {
1084
- if (a === undefined) {
1085
- throw new Error("a is undefined in fstPair()");
1086
- }
1087
- if (a._tag != "Const") {
1088
- return Either.left(new WrongArgType(0, "Const", a._tag));
1089
- }
1090
- if (!(typeof a.value == "object" && a.value != null && "first" in a.value)) {
1091
- return Either.left(new WrongArgType(0, "pair", Value.describeType(a.value)));
1092
- }
1093
- return Either.right({
1094
- _tag: "Const",
1095
- value: a.value.first
1096
- });
579
+ return expectPair(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: a.first })));
1097
580
  }
1098
581
  };
1099
582
  export const fstPairV3 = {
@@ -1108,19 +591,7 @@ export const sndPairV1 = {
1108
591
  cpuModel: Cost.Constant(143),
1109
592
  memModel: Cost.Constant(144),
1110
593
  call: ([a]) => {
1111
- if (a === undefined) {
1112
- throw new Error("a is undefined in sndPair()");
1113
- }
1114
- if (a._tag != "Const") {
1115
- return Either.left(new WrongArgType(0, "Const", a._tag));
1116
- }
1117
- if (!(typeof a.value == "object" && a.value != null && "first" in a.value)) {
1118
- return Either.left(new WrongArgType(0, "pair", Value.describeType(a.value)));
1119
- }
1120
- return Either.right({
1121
- _tag: "Const",
1122
- value: a.value.second
1123
- });
594
+ return expectPair(a, 0).pipe(Either.map((a) => ({ _tag: "Const", value: a.second })));
1124
595
  }
1125
596
  };
1126
597
  export const sndPairV2 = {
@@ -1140,24 +611,13 @@ export const chooseListV1 = {
1140
611
  cpuModel: Cost.Constant(35),
1141
612
  memModel: Cost.Constant(36),
1142
613
  call: ([lst, a, b]) => {
1143
- if (lst === undefined) {
1144
- throw new Error("lst is undefined in chooseList()");
1145
- }
1146
614
  if (a === undefined) {
1147
615
  throw new Error("a is undefined in chooseList()");
1148
616
  }
1149
617
  if (b === undefined) {
1150
618
  throw new Error("b is undefined in chooseList()");
1151
619
  }
1152
- if (lst._tag != "Const") {
1153
- return Either.left(new WrongArgType(0, "Const", lst._tag));
1154
- }
1155
- if (!(typeof lst.value == "object" &&
1156
- lst.value != null &&
1157
- "items" in lst.value)) {
1158
- return Either.left(new WrongArgType(0, "list", Value.describeType(lst.value)));
1159
- }
1160
- return Either.right(lst.value.items.length == 0 ? a : b);
620
+ return expectList(lst, 0).pipe(Either.map((lst) => (lst.items.length == 0 ? a : b)));
1161
621
  }
1162
622
  };
1163
623
  export const mkConsV1 = {
@@ -1167,33 +627,15 @@ export const mkConsV1 = {
1167
627
  cpuModel: Cost.Constant(101),
1168
628
  memModel: Cost.Constant(102),
1169
629
  call: ([item, list]) => {
1170
- if (item === undefined) {
1171
- throw new Error("item is undefined in mkCons()");
1172
- }
1173
- if (list === undefined) {
1174
- throw new Error("list is undefined in mkCons()");
1175
- }
1176
- if (list._tag != "Const") {
1177
- return Either.left(new WrongArgType(0, "Const", list._tag));
1178
- }
1179
- if (!(typeof list.value == "object" &&
1180
- list.value != null &&
1181
- "items" in list.value)) {
1182
- return Either.left(new WrongArgType(0, "list", Value.describeType(list.value)));
1183
- }
1184
- if (item._tag != "Const") {
1185
- return Either.left(new WrongArgType(1, "Const", item._tag));
1186
- }
1187
- if (Value.toType(item.value) != list.value.itemType) {
1188
- return Either.left(new WrongArgType(1, Value.describeType(list.value.itemType), Value.describeType(item.value)));
1189
- }
1190
- return Either.right({
1191
- _tag: "Const",
1192
- value: {
1193
- itemType: list.value.itemType,
1194
- items: [item.value].concat(list.value.items)
1195
- }
1196
- });
630
+ return Either.all([expectConst(item, 1), expectList(list, 0)]).pipe(Either.flatMap(([item, list]) => Value.toType(item) != list.itemType
631
+ ? Either.left(new WrongArgType(1, Value.describeType(list.itemType), Value.describeType(item)))
632
+ : Either.right({
633
+ _tag: "Const",
634
+ value: {
635
+ itemType: list.itemType,
636
+ items: [item].concat(list.items)
637
+ }
638
+ })));
1197
639
  }
1198
640
  };
1199
641
  export const mkConsV3 = {
@@ -1208,23 +650,9 @@ export const headListV1 = {
1208
650
  cpuModel: Cost.Constant(75),
1209
651
  memModel: Cost.Constant(76),
1210
652
  call: ([l]) => {
1211
- if (l === undefined) {
1212
- throw new Error("list is undefined in headList()");
1213
- }
1214
- if (l._tag != "Const") {
1215
- return Either.left(new WrongArgType(0, "Const", l._tag));
1216
- }
1217
- if (!(typeof l.value == "object" && l.value != null && "items" in l.value)) {
1218
- return Either.left(new WrongArgType(0, "list", Value.describeType(l.value)));
1219
- }
1220
- if (l.value.items.length == 0) {
1221
- return Either.left(new OutOfRange(0, 0));
1222
- }
1223
- const head = l.value.items[0];
1224
- return Either.right({
1225
- _tag: "Const",
1226
- value: head
1227
- });
653
+ return expectList(l, 0).pipe(Either.flatMap((l) => l.items.length == 0
654
+ ? Either.left(new OutOfRange(0, 0))
655
+ : Either.right({ _tag: "Const", value: l.items[0] })));
1228
656
  }
1229
657
  };
1230
658
  export const headListV3 = {
@@ -1239,25 +667,15 @@ export const tailListV1 = {
1239
667
  cpuModel: Cost.Constant(149),
1240
668
  memModel: Cost.Constant(150),
1241
669
  call: ([l]) => {
1242
- if (l === undefined) {
1243
- throw new Error("list is undefined in tailList()");
1244
- }
1245
- if (l._tag != "Const") {
1246
- return Either.left(new WrongArgType(0, "Const", l._tag));
1247
- }
1248
- if (!(typeof l.value == "object" && l.value != null && "items" in l.value)) {
1249
- return Either.left(new WrongArgType(0, "list", Value.describeType(l.value)));
1250
- }
1251
- if (l.value.items.length == 0) {
1252
- return Either.left(new OutOfRange(0, 0));
1253
- }
1254
- return Either.right({
1255
- _tag: "Const",
1256
- value: {
1257
- itemType: l.value.itemType,
1258
- items: l.value.items.slice(1)
1259
- }
1260
- });
670
+ return expectList(l, 0).pipe(Either.flatMap((l) => l.items.length == 0
671
+ ? Either.left(new OutOfRange(0, 0))
672
+ : Either.right({
673
+ _tag: "Const",
674
+ value: {
675
+ itemType: l.itemType,
676
+ items: l.items.slice(1)
677
+ }
678
+ })));
1261
679
  }
1262
680
  };
1263
681
  export const tailListV2 = {
@@ -1277,19 +695,7 @@ export const nullListV1 = {
1277
695
  cpuModel: Cost.Constant(119),
1278
696
  memModel: Cost.Constant(120),
1279
697
  call: ([l]) => {
1280
- if (l === undefined) {
1281
- throw new Error("list is undefined in nullList()");
1282
- }
1283
- if (l._tag != "Const") {
1284
- return Either.left(new WrongArgType(0, "Const", l._tag));
1285
- }
1286
- if (!(typeof l.value == "object" && l.value != null && "items" in l.value)) {
1287
- return Either.left(new WrongArgType(0, "list", Value.describeType(l.value)));
1288
- }
1289
- return Either.right({
1290
- _tag: "Const",
1291
- value: l.value.items.length == 0
1292
- });
698
+ return expectList(l, 0).pipe(Either.map((l) => ({ _tag: "Const", value: l.items.length == 0 })));
1293
699
  }
1294
700
  };
1295
701
  export const nullListV3 = {
@@ -1304,9 +710,6 @@ export const chooseDataV1 = {
1304
710
  cpuModel: Cost.Constant(33),
1305
711
  memModel: Cost.Constant(34),
1306
712
  call: ([cond, constrCase, mapCase, listCase, intCase, bytesCase]) => {
1307
- if (cond === undefined) {
1308
- throw new Error("cond is undefined in chooseData()");
1309
- }
1310
713
  if (constrCase === undefined) {
1311
714
  throw new Error("constrCase is undefined in chooseData()");
1312
715
  }
@@ -1322,33 +725,27 @@ export const chooseDataV1 = {
1322
725
  if (bytesCase === undefined) {
1323
726
  throw new Error("bytesCase is undefined in chooseData()");
1324
727
  }
1325
- if (cond._tag != "Const") {
1326
- return Either.left(new WrongArgType(0, "Const", cond._tag));
1327
- }
1328
- if (!(typeof cond.value == "object" &&
1329
- cond.value != null &&
1330
- "data" in cond.value)) {
1331
- return Either.left(new WrongArgType(0, "data", Value.describeType(cond.value)));
1332
- }
1333
- if ("fields" in cond.value.data) {
1334
- return Either.right(constrCase);
1335
- }
1336
- else if ("map" in cond.value.data) {
1337
- return Either.right(mapCase);
1338
- }
1339
- else if ("list" in cond.value.data) {
1340
- return Either.right(listCase);
1341
- }
1342
- else if ("int" in cond.value.data) {
1343
- return Either.right(intCase);
1344
- }
1345
- else if ("bytes" in cond.value.data) {
1346
- return Either.right(bytesCase);
1347
- }
1348
- else {
1349
- // this is a defect
1350
- throw new Error(`unexpected data format in chooseData (got: ${cond.value.data})`);
1351
- }
728
+ return expectData(cond, 0).pipe(Either.map((cond) => {
729
+ if ("fields" in cond.data) {
730
+ return constrCase;
731
+ }
732
+ else if ("map" in cond.data) {
733
+ return mapCase;
734
+ }
735
+ else if ("list" in cond.data) {
736
+ return listCase;
737
+ }
738
+ else if ("int" in cond.data) {
739
+ return intCase;
740
+ }
741
+ else if ("bytes" in cond.data) {
742
+ return bytesCase;
743
+ }
744
+ else {
745
+ // this is a defect
746
+ throw new Error(`unexpected data format in chooseData (got: ${cond.data})`);
747
+ }
748
+ }));
1352
749
  }
1353
750
  };
1354
751
  export const constrDataV1 = {
@@ -1358,30 +755,12 @@ export const constrDataV1 = {
1358
755
  cpuModel: Cost.Constant(43),
1359
756
  memModel: Cost.Constant(44),
1360
757
  call: ([tag, fields]) => {
1361
- if (tag === undefined) {
1362
- throw new Error("tag is undefined in constrData()");
1363
- }
1364
- if (fields === undefined) {
1365
- throw new Error("fields is undefined in constrData()");
1366
- }
1367
- if (tag._tag != "Const") {
1368
- return Either.left(new WrongArgType(0, "Const", tag._tag));
1369
- }
1370
- if (typeof tag.value != "bigint") {
1371
- return Either.left(new WrongArgType(0, "integer", Value.describeType(tag.value)));
1372
- }
1373
- if (fields._tag != "Const") {
1374
- return Either.left(new WrongArgType(1, "Const", fields._tag));
1375
- }
1376
- if (!(Value.isList(fields.value) && fields.value.itemType == Type.Data)) {
1377
- return Either.left(new WrongArgType(1, "data list", Value.describeType(fields.value)));
1378
- }
1379
- return Either.right({
758
+ return Either.all([expectInteger(tag, 0), expectDataList(fields, 1)]).pipe(Either.map(([tag, fields]) => ({
1380
759
  _tag: "Const",
1381
760
  value: {
1382
761
  data: {
1383
- constructor: Number(tag.value),
1384
- fields: fields.value.items.map((item) => {
762
+ constructor: Number(tag),
763
+ fields: fields.items.map((item) => {
1385
764
  if (!Value.isData(item)) {
1386
765
  throw new Error("expected only data value fields");
1387
766
  }
@@ -1389,7 +768,7 @@ export const constrDataV1 = {
1389
768
  })
1390
769
  }
1391
770
  }
1392
- });
771
+ })));
1393
772
  }
1394
773
  };
1395
774
  export const mapDataV1 = {
@@ -1399,20 +778,11 @@ export const mapDataV1 = {
1399
778
  cpuModel: Cost.Constant(99),
1400
779
  memModel: Cost.Constant(100),
1401
780
  call: ([pairs]) => {
1402
- if (pairs === undefined) {
1403
- throw new Error("pairs is undefined in mapData()");
1404
- }
1405
- if (pairs._tag != "Const") {
1406
- return Either.left(new WrongArgType(0, "Const", pairs._tag));
1407
- }
1408
- if (!(Value.isList(pairs.value) && pairs.value.itemType == Type.DataPair)) {
1409
- return Either.left(new WrongArgType(0, "data pair list", Value.describeType(pairs.value)));
1410
- }
1411
- return Either.right({
781
+ return expectDataPairList(pairs, 0).pipe(Either.map((pairs) => ({
1412
782
  _tag: "Const",
1413
783
  value: {
1414
784
  data: {
1415
- map: pairs.value.items.map((pair) => {
785
+ map: pairs.items.map((pair) => {
1416
786
  if (!Value.isPair(pair)) {
1417
787
  // this is a defect
1418
788
  throw new Error("expected data pair");
@@ -1429,7 +799,7 @@ export const mapDataV1 = {
1429
799
  })
1430
800
  }
1431
801
  }
1432
- });
802
+ })));
1433
803
  }
1434
804
  };
1435
805
  export const mapDataV3 = {
@@ -1444,20 +814,11 @@ export const listDataV1 = {
1444
814
  cpuModel: Cost.Constant(97),
1445
815
  memModel: Cost.Constant(98),
1446
816
  call: ([list]) => {
1447
- if (list === undefined) {
1448
- throw new Error("list is undefined in listData()");
1449
- }
1450
- if (list._tag != "Const") {
1451
- return Either.left(new WrongArgType(0, "Const", list._tag));
1452
- }
1453
- if (!(Value.isList(list.value) && list.value.itemType == Type.Data)) {
1454
- return Either.left(new WrongArgType(0, "data list", Value.describeType(list.value)));
1455
- }
1456
- return Either.right({
817
+ return expectDataList(list, 0).pipe(Either.map((list) => ({
1457
818
  _tag: "Const",
1458
819
  value: {
1459
820
  data: {
1460
- list: list.value.items.map((item) => {
821
+ list: list.items.map((item) => {
1461
822
  if (!Value.isData(item)) {
1462
823
  throw new Error("expected data item");
1463
824
  }
@@ -1465,7 +826,7 @@ export const listDataV1 = {
1465
826
  })
1466
827
  }
1467
828
  }
1468
- });
829
+ })));
1469
830
  }
1470
831
  };
1471
832
  export const listDataV3 = {
@@ -1480,23 +841,7 @@ export const iDataV1 = {
1480
841
  cpuModel: Cost.Constant(77),
1481
842
  memModel: Cost.Constant(78),
1482
843
  call: ([x]) => {
1483
- if (x === undefined) {
1484
- throw new Error("x is undefined in iData()");
1485
- }
1486
- if (x._tag != "Const") {
1487
- return Either.left(new WrongArgType(0, "Const", x._tag));
1488
- }
1489
- if (typeof x.value != "bigint") {
1490
- return Either.left(new WrongArgType(0, "integer", Value.describeType(x.value)));
1491
- }
1492
- return Either.right({
1493
- _tag: "Const",
1494
- value: {
1495
- data: {
1496
- int: x.value
1497
- }
1498
- }
1499
- });
844
+ return expectInteger(x, 0).pipe(Either.map((x) => ({ _tag: "Const", value: { data: { int: x } } })));
1500
845
  }
1501
846
  };
1502
847
  export const iDataV3 = {
@@ -1511,23 +856,7 @@ export const bDataV1 = {
1511
856
  cpuModel: Cost.Constant(12),
1512
857
  memModel: Cost.Constant(13),
1513
858
  call: ([b]) => {
1514
- if (b === undefined) {
1515
- throw new Error("b is undefined in bData()");
1516
- }
1517
- if (b._tag != "Const") {
1518
- return Either.left(new WrongArgType(0, "Const", b._tag));
1519
- }
1520
- if (!(b.value instanceof Uint8Array)) {
1521
- return Either.left(new WrongArgType(0, "bytes", Value.describeType(b.value)));
1522
- }
1523
- return Either.right({
1524
- _tag: "Const",
1525
- value: {
1526
- data: {
1527
- bytes: b.value
1528
- }
1529
- }
1530
- });
859
+ return expectBytes(b, 0).pipe(Either.map((b) => ({ _tag: "Const", value: { data: { bytes: b } } })));
1531
860
  }
1532
861
  };
1533
862
  export const unConstrDataV1 = {
@@ -1537,28 +866,18 @@ export const unConstrDataV1 = {
1537
866
  cpuModel: Cost.Constant(155),
1538
867
  memModel: Cost.Constant(156),
1539
868
  call: ([data]) => {
1540
- if (data === undefined) {
1541
- throw new Error("data is undefined in unConstrData()");
1542
- }
1543
- if (data._tag != "Const") {
1544
- return Either.left(new WrongArgType(0, "Const", data._tag));
1545
- }
1546
- if (!Value.isData(data.value)) {
1547
- return Either.left(new WrongArgType(0, "data", Value.describeType(data.value)));
1548
- }
1549
- if (!("fields" in data.value.data)) {
1550
- return Either.left(new WrongArgType(0, "constr data", Object.keys(data.value.data).join("")));
1551
- }
1552
- return Either.right({
1553
- _tag: "Const",
1554
- value: {
1555
- first: BigInt(data.value.data.constructor),
1556
- second: {
1557
- itemType: Type.Data,
1558
- items: data.value.data.fields.map((d) => ({ data: d }))
869
+ return expectData(data, 0).pipe(Either.flatMap((data) => "fields" in data.data
870
+ ? Either.right({
871
+ _tag: "Const",
872
+ value: {
873
+ first: BigInt(data.data.constructor),
874
+ second: {
875
+ itemType: Type.Data,
876
+ items: data.data.fields.map((d) => ({ data: d }))
877
+ }
1559
878
  }
1560
- }
1561
- });
879
+ })
880
+ : Either.left(new WrongArgType(0, "constr data", Object.keys(data.data).join("")))));
1562
881
  }
1563
882
  };
1564
883
  export const unConstrDataV2 = {
@@ -1578,28 +897,18 @@ export const unMapDataV1 = {
1578
897
  cpuModel: Cost.Constant(161),
1579
898
  memModel: Cost.Constant(162),
1580
899
  call: ([data]) => {
1581
- if (data === undefined) {
1582
- throw new Error("data is undefined in unMapData()");
1583
- }
1584
- if (data._tag != "Const") {
1585
- return Either.left(new WrongArgType(0, "Const", data._tag));
1586
- }
1587
- if (!Value.isData(data.value)) {
1588
- return Either.left(new WrongArgType(0, "data", Value.describeType(data.value)));
1589
- }
1590
- if (!("map" in data.value.data)) {
1591
- return Either.left(new WrongArgType(0, "map data", Value.describeType(data.value)));
1592
- }
1593
- return Either.right({
1594
- _tag: "Const",
1595
- value: {
1596
- itemType: Type.Data,
1597
- items: data.value.data.map.map((d) => ({
1598
- first: { data: d.k },
1599
- second: { data: d.v }
1600
- }))
1601
- }
1602
- });
900
+ return expectData(data, 0).pipe(Either.flatMap((data) => "map" in data.data
901
+ ? Either.right({
902
+ _tag: "Const",
903
+ value: {
904
+ itemType: Type.Data,
905
+ items: data.data.map.map((d) => ({
906
+ first: { data: d.k },
907
+ second: { data: d.v }
908
+ }))
909
+ }
910
+ })
911
+ : Either.left(new WrongArgType(0, "map data", Value.describeType(data)))));
1603
912
  }
1604
913
  };
1605
914
  export const unMapDataV2 = {
@@ -1619,25 +928,15 @@ export const unListDataV1 = {
1619
928
  cpuModel: Cost.Constant(159),
1620
929
  memModel: Cost.Constant(160),
1621
930
  call: ([data]) => {
1622
- if (data === undefined) {
1623
- throw new Error("data is undefined in unListData()");
1624
- }
1625
- if (data._tag != "Const") {
1626
- return Either.left(new WrongArgType(0, "Const", data._tag));
1627
- }
1628
- if (!Value.isData(data.value)) {
1629
- return Either.left(new WrongArgType(0, "data", Value.describeType(data.value)));
1630
- }
1631
- if (!("list" in data.value.data)) {
1632
- return Either.left(new WrongArgType(0, "list data", Value.describeType(data.value)));
1633
- }
1634
- return Either.right({
1635
- _tag: "Const",
1636
- value: {
1637
- itemType: Type.Data,
1638
- items: data.value.data.list.map((d) => ({ data: d }))
1639
- }
1640
- });
931
+ return expectData(data, 0).pipe(Either.flatMap((data) => "list" in data.data
932
+ ? Either.right({
933
+ _tag: "Const",
934
+ value: {
935
+ itemType: Type.Data,
936
+ items: data.data.list.map((d) => ({ data: d }))
937
+ }
938
+ })
939
+ : Either.left(new WrongArgType(0, "list data", Value.describeType(data)))));
1641
940
  }
1642
941
  };
1643
942
  export const unListDataV2 = {
@@ -1657,22 +956,9 @@ export const unIDataV1 = {
1657
956
  cpuModel: Cost.Constant(157),
1658
957
  memModel: Cost.Constant(158),
1659
958
  call: ([data]) => {
1660
- if (data === undefined) {
1661
- throw new Error("data is undefined in unIData()");
1662
- }
1663
- if (data._tag != "Const") {
1664
- return Either.left(new WrongArgType(0, "Const", data._tag));
1665
- }
1666
- if (!Value.isData(data.value)) {
1667
- return Either.left(new WrongArgType(0, "data", Value.describeType(data.value)));
1668
- }
1669
- if (!("int" in data.value.data)) {
1670
- return Either.left(new WrongArgType(0, "int data", Value.describeType(data.value)));
1671
- }
1672
- return Either.right({
1673
- _tag: "Const",
1674
- value: data.value.data.int
1675
- });
959
+ return expectData(data, 0).pipe(Either.flatMap((data) => "int" in data.data
960
+ ? Either.right({ _tag: "Const", value: data.data.int })
961
+ : Either.left(new WrongArgType(0, "int data", Value.describeType(data)))));
1676
962
  }
1677
963
  };
1678
964
  export const unIDataV2 = {
@@ -1692,22 +978,9 @@ export const unBDataV1 = {
1692
978
  cpuModel: Cost.Constant(153),
1693
979
  memModel: Cost.Constant(154),
1694
980
  call: ([data]) => {
1695
- if (data === undefined) {
1696
- throw new Error("data is undefined in unBData()");
1697
- }
1698
- if (data._tag != "Const") {
1699
- return Either.left(new WrongArgType(0, "Const", data._tag));
1700
- }
1701
- if (!Value.isData(data.value)) {
1702
- return Either.left(new WrongArgType(0, "data", Value.describeType(data.value)));
1703
- }
1704
- if (!("bytes" in data.value.data)) {
1705
- return Either.left(new WrongArgType(0, "byte data", Value.describeType(data.value)));
1706
- }
1707
- return Either.right({
1708
- _tag: "Const",
1709
- value: data.value.data.bytes
1710
- });
981
+ return expectData(data, 0).pipe(Either.flatMap((data) => "bytes" in data.data
982
+ ? Either.right({ _tag: "Const", value: data.data.bytes })
983
+ : Either.left(new WrongArgType(0, "byte data", Value.describeType(data)))));
1711
984
  }
1712
985
  };
1713
986
  export const unBDataV2 = {
@@ -1727,28 +1000,10 @@ export const equalsDataV1 = {
1727
1000
  cpuModel: Cost.Linear(63, 64)(Cost.Min),
1728
1001
  memModel: Cost.Constant(65),
1729
1002
  call: ([a, b]) => {
1730
- if (a === undefined) {
1731
- throw new Error("a is undefined in equalsData()");
1732
- }
1733
- if (b === undefined) {
1734
- throw new Error("b is undefined in equalsData()");
1735
- }
1736
- if (a._tag != "Const") {
1737
- return Either.left(new WrongArgType(0, "Const", a._tag));
1738
- }
1739
- if (!Value.isData(a.value)) {
1740
- return Either.left(new WrongArgType(0, "data", Value.describeType(a.value)));
1741
- }
1742
- if (b._tag != "Const") {
1743
- return Either.left(new WrongArgType(0, "Const", b._tag));
1744
- }
1745
- if (!Value.isData(b.value)) {
1746
- return Either.left(new WrongArgType(0, "data", Value.describeType(b.value)));
1747
- }
1748
- return Either.right({
1003
+ return Either.all([expectData(a, 0), expectData(b, 1)]).pipe(Either.map(([a, b]) => ({
1749
1004
  _tag: "Const",
1750
- value: dataToString(a.value.data) == dataToString(b.value.data)
1751
- });
1005
+ value: dataToString(a.data) == dataToString(b.data)
1006
+ })));
1752
1007
  }
1753
1008
  };
1754
1009
  export const equalsDataV3 = {
@@ -1763,31 +1018,13 @@ export const mkPairDataV1 = {
1763
1018
  cpuModel: Cost.Constant(107),
1764
1019
  memModel: Cost.Constant(108),
1765
1020
  call: ([a, b]) => {
1766
- if (a === undefined) {
1767
- throw new Error("a is undefined in mkPairData()");
1768
- }
1769
- if (b === undefined) {
1770
- throw new Error("b is undefined in mkPairData()");
1771
- }
1772
- if (a._tag != "Const") {
1773
- return Either.left(new WrongArgType(0, "Const", a._tag));
1774
- }
1775
- if (!Value.isData(a.value)) {
1776
- return Either.left(new WrongArgType(0, "data", Value.describeType(a.value)));
1777
- }
1778
- if (b._tag != "Const") {
1779
- return Either.left(new WrongArgType(0, "Const", b._tag));
1780
- }
1781
- if (!Value.isData(b.value)) {
1782
- return Either.left(new WrongArgType(0, "data", Value.describeType(b.value)));
1783
- }
1784
- return Either.right({
1021
+ return Either.all([expectData(a, 0), expectData(b, 1)]).pipe(Either.map(([a, b]) => ({
1785
1022
  _tag: "Const",
1786
1023
  value: {
1787
- first: a.value,
1788
- second: b.value
1024
+ first: a,
1025
+ second: b
1789
1026
  }
1790
- });
1027
+ })));
1791
1028
  }
1792
1029
  };
1793
1030
  export const mkPairDataV3 = {
@@ -1802,22 +1039,13 @@ export const mkNilDataV1 = {
1802
1039
  cpuModel: Cost.Constant(103),
1803
1040
  memModel: Cost.Constant(104),
1804
1041
  call: ([unit]) => {
1805
- if (unit === undefined) {
1806
- throw new Error("unit is undefined in mkNilData()");
1807
- }
1808
- if (unit._tag != "Const") {
1809
- return Either.left(new WrongArgType(0, "Const", unit._tag));
1810
- }
1811
- if (unit.value !== null) {
1812
- return Either.left(new WrongArgType(0, "null", Value.describeType(unit.value)));
1813
- }
1814
- return Either.right({
1042
+ return expectUnit(unit, 0, "null").pipe(Either.map(() => ({
1815
1043
  _tag: "Const",
1816
1044
  value: {
1817
1045
  itemType: Type.Data,
1818
1046
  items: []
1819
1047
  }
1820
- });
1048
+ })));
1821
1049
  }
1822
1050
  };
1823
1051
  export const mkNilDataV3 = {
@@ -1832,22 +1060,13 @@ export const mkNilPairDataV1 = {
1832
1060
  cpuModel: Cost.Constant(105),
1833
1061
  memModel: Cost.Constant(106),
1834
1062
  call: ([unit]) => {
1835
- if (unit === undefined) {
1836
- throw new Error("unit is undefined in mkNilPairData()");
1837
- }
1838
- if (unit._tag != "Const") {
1839
- return Either.left(new WrongArgType(0, "Const", unit._tag));
1840
- }
1841
- if (unit.value !== null) {
1842
- return Either.left(new WrongArgType(0, "null", Value.describeType(unit.value)));
1843
- }
1844
- return Either.right({
1063
+ return expectUnit(unit, 0, "null").pipe(Either.map(() => ({
1845
1064
  _tag: "Const",
1846
1065
  value: {
1847
1066
  itemType: Type.DataPair,
1848
1067
  items: []
1849
1068
  }
1850
- });
1069
+ })));
1851
1070
  }
1852
1071
  };
1853
1072
  export const mkNilPairDataV3 = {
@@ -1862,19 +1081,10 @@ export const serialiseDataV2 = {
1862
1081
  cpuModel: Cost.Linear(133, 134)(Cost.First),
1863
1082
  memModel: Cost.Linear(135, 136)(Cost.First),
1864
1083
  call: ([data]) => {
1865
- if (data === undefined) {
1866
- throw new Error("data is undefined in serialiseData()");
1867
- }
1868
- if (data._tag != "Const") {
1869
- return Either.left(new WrongArgType(0, "Const", data._tag));
1870
- }
1871
- if (!Value.isData(data.value)) {
1872
- return Either.left(new WrongArgType(0, "data", Value.describeType(data.value)));
1873
- }
1874
- return Either.right({
1084
+ return expectData(data, 0).pipe(Either.map((data) => ({
1875
1085
  _tag: "Const",
1876
- value: Bytes.toUint8Array(encodeData(data.value.data))
1877
- });
1086
+ value: Bytes.toUint8Array(encodeData(data.data))
1087
+ })));
1878
1088
  }
1879
1089
  };
1880
1090
  export const serialiseDataV3 = {
@@ -1912,6 +1122,285 @@ export const verifySchnorrSecp256k1SignatureV3 = {
1912
1122
  cpuModel: Cost.Linear(190, 191)(Cost.Third),
1913
1123
  memModel: Cost.Constant(192)
1914
1124
  };
1125
+ function expectConst(arg, index) {
1126
+ if (arg === undefined) {
1127
+ return Either.left(new WrongArgType(index, "Const", "undefined"));
1128
+ }
1129
+ else if (arg._tag != "Const") {
1130
+ return Either.left(new WrongArgType(index, "Const", arg._tag));
1131
+ }
1132
+ else {
1133
+ return Either.right(arg.value);
1134
+ }
1135
+ }
1136
+ function expectG1(arg, index) {
1137
+ return expectConst(arg, index).pipe(Either.flatMap((value) => typeof value == "object" && value != null && "g1Element" in value
1138
+ ? Either.right(Value.tupleToG1(value.g1Element))
1139
+ : Either.left(new WrongArgType(index, "bls12_381_G1_element", Value.describeType(value)))));
1140
+ }
1141
+ function expectG2(arg, index) {
1142
+ return expectConst(arg, index).pipe(Either.flatMap((value) => typeof value == "object" && value != null && "g2Element" in value
1143
+ ? Either.right(Value.tupleToG2(value.g2Element))
1144
+ : Either.left(new WrongArgType(index, "bls12_381_G2_element", Value.describeType(value)))));
1145
+ }
1146
+ function expectMlResult(arg, index) {
1147
+ return expectConst(arg, index).pipe(Either.flatMap((value) => typeof value == "object" && value != null && "mlResult" in value
1148
+ ? Either.right([
1149
+ [
1150
+ [value.mlResult[0][0][0], value.mlResult[0][0][1]],
1151
+ [value.mlResult[0][1][0], value.mlResult[0][1][1]],
1152
+ [value.mlResult[0][2][0], value.mlResult[0][2][1]]
1153
+ ],
1154
+ [
1155
+ [value.mlResult[1][0][0], value.mlResult[1][0][1]],
1156
+ [value.mlResult[1][1][0], value.mlResult[1][1][1]],
1157
+ [value.mlResult[1][2][0], value.mlResult[1][2][1]]
1158
+ ]
1159
+ ])
1160
+ : Either.left(new WrongArgType(index, "bls12_381_mlresult", Value.describeType(value)))));
1161
+ }
1162
+ function expectBytes(arg, index) {
1163
+ return expectConst(arg, index).pipe(Either.flatMap((value) => value instanceof Uint8Array
1164
+ ? Either.right(value)
1165
+ : Either.left(new WrongArgType(index, "bytes", Value.describeType(value)))));
1166
+ }
1167
+ function expectInteger(arg, index) {
1168
+ return expectConst(arg, index).pipe(Either.flatMap((value) => typeof value == "bigint"
1169
+ ? Either.right(value)
1170
+ : Either.left(new WrongArgType(index, "integer", Value.describeType(value)))));
1171
+ }
1172
+ function expectBool(arg, index) {
1173
+ return expectConst(arg, index).pipe(Either.flatMap((value) => typeof value == "boolean"
1174
+ ? Either.right(value)
1175
+ : Either.left(new WrongArgType(index, "bool", Value.describeType(value)))));
1176
+ }
1177
+ function expectString(arg, index) {
1178
+ return expectConst(arg, index).pipe(Either.flatMap((value) => typeof value == "string"
1179
+ ? Either.right(value)
1180
+ : Either.left(new WrongArgType(index, "string", Value.describeType(value)))));
1181
+ }
1182
+ function expectUnit(arg, index, expected = "unit") {
1183
+ return expectConst(arg, index).pipe(Either.flatMap((value) => value === null
1184
+ ? Either.right(value)
1185
+ : Either.left(new WrongArgType(index, expected, Value.describeType(value)))));
1186
+ }
1187
+ function expectData(arg, index) {
1188
+ return expectConst(arg, index).pipe(Either.flatMap((value) => Value.isData(value)
1189
+ ? Either.right(value)
1190
+ : Either.left(new WrongArgType(index, "data", Value.describeType(value)))));
1191
+ }
1192
+ function expectPair(arg, index) {
1193
+ return expectConst(arg, index).pipe(Either.flatMap((value) => Value.isPair(value)
1194
+ ? Either.right(value)
1195
+ : Either.left(new WrongArgType(index, "pair", Value.describeType(value)))));
1196
+ }
1197
+ function expectList(arg, index, expected = "list") {
1198
+ return expectConst(arg, index).pipe(Either.flatMap((value) => Value.isList(value)
1199
+ ? Either.right(value)
1200
+ : Either.left(new WrongArgType(index, expected, Value.describeType(value)))));
1201
+ }
1202
+ function expectDataList(arg, index) {
1203
+ return expectList(arg, index, "data list").pipe(Either.flatMap((list) => list.itemType == Type.Data
1204
+ ? Either.right(list)
1205
+ : Either.left(new WrongArgType(index, "data list", Value.describeType(list)))));
1206
+ }
1207
+ function expectDataPairList(arg, index) {
1208
+ return expectList(arg, index, "data pair list").pipe(Either.flatMap((list) => list.itemType == Type.DataPair
1209
+ ? Either.right(list)
1210
+ : Either.left(new WrongArgType(index, "data pair list", Value.describeType(list)))));
1211
+ }
1212
+ function cryptoFailure(message) {
1213
+ return Bytes.DecodeException([], message);
1214
+ }
1215
+ function tryCrypto(fn) {
1216
+ try {
1217
+ return Either.right(fn());
1218
+ }
1219
+ catch (e) {
1220
+ return Either.left(cryptoFailure(e instanceof Error ? e.message : String(e)));
1221
+ }
1222
+ }
1223
+ export const bls12_381_G1_addV3 = {
1224
+ name: "bls12_381_G1_add",
1225
+ forceCount: 0,
1226
+ nArgs: 2,
1227
+ cpuModel: Cost.Constant(197),
1228
+ memModel: Cost.Constant(198),
1229
+ call: ([a, b]) => Either.all([expectG1(a, 0), expectG1(b, 1)]).pipe(Either.map(([a, b]) => ({
1230
+ _tag: "Const",
1231
+ value: { g1Element: Value.g1ToTuple(Crypto.Bls12_381.g1Add(a, b)) }
1232
+ })))
1233
+ };
1234
+ export const bls12_381_G1_negV3 = {
1235
+ name: "bls12_381_G1_neg",
1236
+ forceCount: 0,
1237
+ nArgs: 1,
1238
+ cpuModel: Cost.Constant(206),
1239
+ memModel: Cost.Constant(207),
1240
+ call: ([a]) => expectG1(a, 0).pipe(Either.map((a) => ({
1241
+ _tag: "Const",
1242
+ value: { g1Element: Value.g1ToTuple(Crypto.Bls12_381.g1Neg(a)) }
1243
+ })))
1244
+ };
1245
+ export const bls12_381_G1_scalarMulV3 = {
1246
+ name: "bls12_381_G1_scalarMul",
1247
+ forceCount: 0,
1248
+ nArgs: 2,
1249
+ cpuModel: Cost.Linear(208, 209)(Cost.First),
1250
+ memModel: Cost.Constant(210),
1251
+ call: ([s, p]) => Either.all([expectInteger(s, 0), expectG1(p, 1)]).pipe(Either.map(([s, p]) => ({
1252
+ _tag: "Const",
1253
+ value: { g1Element: Value.g1ToTuple(Crypto.Bls12_381.g1ScalarMul(s, p)) }
1254
+ })))
1255
+ };
1256
+ export const bls12_381_G1_equalV3 = {
1257
+ name: "bls12_381_G1_equal",
1258
+ forceCount: 0,
1259
+ nArgs: 2,
1260
+ cpuModel: Cost.Constant(201),
1261
+ memModel: Cost.Constant(202),
1262
+ call: ([a, b]) => Either.all([expectG1(a, 0), expectG1(b, 1)]).pipe(Either.map(([a, b]) => ({
1263
+ _tag: "Const",
1264
+ value: Crypto.Bls12_381.g1Equals(a, b)
1265
+ })))
1266
+ };
1267
+ export const bls12_381_G1_hashToGroupV3 = {
1268
+ name: "bls12_381_G1_hashToGroup",
1269
+ forceCount: 0,
1270
+ nArgs: 2,
1271
+ cpuModel: Cost.Linear(203, 204)(Cost.Sum),
1272
+ memModel: Cost.Constant(205),
1273
+ call: ([msg, dst]) => Either.all([expectBytes(msg, 0), expectBytes(dst, 1)]).pipe(Either.flatMap(([msg, dst]) => tryCrypto(() => Crypto.Bls12_381.g1HashToGroup(msg, dst)).pipe(Either.map((p) => ({
1274
+ _tag: "Const",
1275
+ value: { g1Element: Value.g1ToTuple(p) }
1276
+ })))))
1277
+ };
1278
+ export const bls12_381_G1_compressV3 = {
1279
+ name: "bls12_381_G1_compress",
1280
+ forceCount: 0,
1281
+ nArgs: 1,
1282
+ cpuModel: Cost.Constant(199),
1283
+ memModel: Cost.Constant(200),
1284
+ call: ([a]) => expectG1(a, 0).pipe(Either.map((a) => ({
1285
+ _tag: "Const",
1286
+ value: Crypto.Bls12_381.g1Compress(a)
1287
+ })))
1288
+ };
1289
+ export const bls12_381_G1_uncompressV3 = {
1290
+ name: "bls12_381_G1_uncompress",
1291
+ forceCount: 0,
1292
+ nArgs: 1,
1293
+ cpuModel: Cost.Constant(211),
1294
+ memModel: Cost.Constant(212),
1295
+ call: ([a]) => expectBytes(a, 0).pipe(Either.flatMap((bytes) => Crypto.Bls12_381.g1Uncompress(bytes).pipe(Either.map((p) => ({
1296
+ _tag: "Const",
1297
+ value: { g1Element: Value.g1ToTuple(p) }
1298
+ })))))
1299
+ };
1300
+ export const bls12_381_G2_addV3 = {
1301
+ name: "bls12_381_G2_add",
1302
+ forceCount: 0,
1303
+ nArgs: 2,
1304
+ cpuModel: Cost.Constant(213),
1305
+ memModel: Cost.Constant(214),
1306
+ call: ([a, b]) => Either.all([expectG2(a, 0), expectG2(b, 1)]).pipe(Either.map(([a, b]) => ({
1307
+ _tag: "Const",
1308
+ value: { g2Element: Value.g2ToTuple(Crypto.Bls12_381.g2Add(a, b)) }
1309
+ })))
1310
+ };
1311
+ export const bls12_381_G2_negV3 = {
1312
+ name: "bls12_381_G2_neg",
1313
+ forceCount: 0,
1314
+ nArgs: 1,
1315
+ cpuModel: Cost.Constant(222),
1316
+ memModel: Cost.Constant(223),
1317
+ call: ([a]) => expectG2(a, 0).pipe(Either.map((a) => ({
1318
+ _tag: "Const",
1319
+ value: { g2Element: Value.g2ToTuple(Crypto.Bls12_381.g2Neg(a)) }
1320
+ })))
1321
+ };
1322
+ export const bls12_381_G2_scalarMulV3 = {
1323
+ name: "bls12_381_G2_scalarMul",
1324
+ forceCount: 0,
1325
+ nArgs: 2,
1326
+ cpuModel: Cost.Linear(224, 225)(Cost.First),
1327
+ memModel: Cost.Constant(226),
1328
+ call: ([s, p]) => Either.all([expectInteger(s, 0), expectG2(p, 1)]).pipe(Either.map(([s, p]) => ({
1329
+ _tag: "Const",
1330
+ value: { g2Element: Value.g2ToTuple(Crypto.Bls12_381.g2ScalarMul(s, p)) }
1331
+ })))
1332
+ };
1333
+ export const bls12_381_G2_equalV3 = {
1334
+ name: "bls12_381_G2_equal",
1335
+ forceCount: 0,
1336
+ nArgs: 2,
1337
+ cpuModel: Cost.Constant(217),
1338
+ memModel: Cost.Constant(218),
1339
+ call: ([a, b]) => Either.all([expectG2(a, 0), expectG2(b, 1)]).pipe(Either.map(([a, b]) => ({
1340
+ _tag: "Const",
1341
+ value: Crypto.Bls12_381.g2Equals(a, b)
1342
+ })))
1343
+ };
1344
+ export const bls12_381_G2_hashToGroupV3 = {
1345
+ name: "bls12_381_G2_hashToGroup",
1346
+ forceCount: 0,
1347
+ nArgs: 2,
1348
+ cpuModel: Cost.Linear(219, 220)(Cost.Sum),
1349
+ memModel: Cost.Constant(221),
1350
+ call: ([msg, dst]) => Either.all([expectBytes(msg, 0), expectBytes(dst, 1)]).pipe(Either.flatMap(([msg, dst]) => tryCrypto(() => Crypto.Bls12_381.g2HashToGroup(msg, dst)).pipe(Either.map((p) => ({
1351
+ _tag: "Const",
1352
+ value: { g2Element: Value.g2ToTuple(p) }
1353
+ })))))
1354
+ };
1355
+ export const bls12_381_G2_compressV3 = {
1356
+ name: "bls12_381_G2_compress",
1357
+ forceCount: 0,
1358
+ nArgs: 1,
1359
+ cpuModel: Cost.Constant(215),
1360
+ memModel: Cost.Constant(216),
1361
+ call: ([a]) => expectG2(a, 0).pipe(Either.map((a) => ({
1362
+ _tag: "Const",
1363
+ value: Crypto.Bls12_381.g2Compress(a)
1364
+ })))
1365
+ };
1366
+ export const bls12_381_G2_uncompressV3 = {
1367
+ name: "bls12_381_G2_uncompress",
1368
+ forceCount: 0,
1369
+ nArgs: 1,
1370
+ cpuModel: Cost.Constant(227),
1371
+ memModel: Cost.Constant(228),
1372
+ call: ([a]) => expectBytes(a, 0).pipe(Either.flatMap((bytes) => Crypto.Bls12_381.g2Uncompress(bytes).pipe(Either.map((p) => ({
1373
+ _tag: "Const",
1374
+ value: { g2Element: Value.g2ToTuple(p) }
1375
+ })))))
1376
+ };
1377
+ export const bls12_381_millerLoopV3 = {
1378
+ name: "bls12_381_millerLoop",
1379
+ forceCount: 0,
1380
+ nArgs: 2,
1381
+ cpuModel: Cost.Constant(231),
1382
+ memModel: Cost.Constant(232),
1383
+ call: ([a, b]) => Either.all([expectG1(a, 0), expectG2(b, 1)]).pipe(Either.flatMap(([a, b]) => tryCrypto(() => Crypto.Bls12_381.millerLoop(a, b)).pipe(Either.map((mlResult) => ({ _tag: "Const", value: { mlResult } })))))
1384
+ };
1385
+ export const bls12_381_mulMlResultV3 = {
1386
+ name: "bls12_381_mulMlResult",
1387
+ forceCount: 0,
1388
+ nArgs: 2,
1389
+ cpuModel: Cost.Constant(233),
1390
+ memModel: Cost.Constant(234),
1391
+ call: ([a, b]) => Either.all([expectMlResult(a, 0), expectMlResult(b, 1)]).pipe(Either.map(([a, b]) => ({
1392
+ _tag: "Const",
1393
+ value: { mlResult: Crypto.Bls12_381.mulMlResult(a, b) }
1394
+ })))
1395
+ };
1396
+ export const bls12_381_finalVerifyV3 = {
1397
+ name: "bls12_381_finalVerify",
1398
+ forceCount: 0,
1399
+ nArgs: 2,
1400
+ cpuModel: Cost.Constant(229),
1401
+ memModel: Cost.Constant(230),
1402
+ call: ([a, b]) => Either.all([expectMlResult(a, 0), expectMlResult(b, 1)]).pipe(Either.flatMap(([a, b]) => tryCrypto(() => Crypto.Bls12_381.finalVerify(a, b)).pipe(Either.map((value) => ({ _tag: "Const", value })))))
1403
+ };
1915
1404
  export const V1 = [
1916
1405
  addIntegerV1, // 0
1917
1406
  subtractIntegerV1, // 1
@@ -2075,7 +1564,23 @@ export const V3 = [
2075
1564
  mkNilPairDataV3, // 50
2076
1565
  serialiseDataV3, // 51
2077
1566
  verifyEcdsaSecp256k1SignatureV3, // 52
2078
- verifySchnorrSecp256k1SignatureV3 // 53
2079
- // TODO: remaining builtins
1567
+ verifySchnorrSecp256k1SignatureV3, // 53
1568
+ bls12_381_G1_addV3, // 54
1569
+ bls12_381_G1_negV3, // 55
1570
+ bls12_381_G1_scalarMulV3, // 56
1571
+ bls12_381_G1_equalV3, // 57
1572
+ bls12_381_G1_hashToGroupV3, // 58
1573
+ bls12_381_G1_compressV3, // 59
1574
+ bls12_381_G1_uncompressV3, // 60
1575
+ bls12_381_G2_addV3, // 61
1576
+ bls12_381_G2_negV3, // 62
1577
+ bls12_381_G2_scalarMulV3, // 63
1578
+ bls12_381_G2_equalV3, // 64
1579
+ bls12_381_G2_hashToGroupV3, // 65
1580
+ bls12_381_G2_compressV3, // 66
1581
+ bls12_381_G2_uncompressV3, // 67
1582
+ bls12_381_millerLoopV3, // 68
1583
+ bls12_381_mulMlResultV3, // 69
1584
+ bls12_381_finalVerifyV3 // 70
2080
1585
  ];
2081
1586
  //# sourceMappingURL=Builtins.js.map