-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathminiscript.als
1123 lines (869 loc) · 27.5 KB
/
miniscript.als
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Miniscript specification (c) by Dmitry Petukhov (https://github.com/dgpv)
// Licensed under a Creative Commons Attribution-ShareAlike 4.0 International
// License <http://creativecommons.org/licenses/by-sa/4.0/>
module miniscript
open util/ternary
open util/graph[Node]
// The specification consists of four sections. First is the Nodes section,
// which presents the node specifications that references witnesses defined
// in Witnesses section and is using the definitions from the Definitions
// section. The predicates, run and check clauses for analysis of this
// specification are in the Analysis section.
/*****************/
/* Nodes section */
/*****************/
sig Zero extends Node {
} {
type = B + z + u + d + s + e
no args
no wit
correctness_holds
non_malleability_holds
never [ sat ]
always [ dsat ]
never [ nc_sat ]
never [ nc_dsat ]
no timelocks
}
sig One extends Node {
} {
type = B + z + u + f
no args
no wit
correctness_holds
non_malleability_holds
always [ sat ]
never [ dsat ]
never [ nc_sat ]
never [ nc_dsat ]
no timelocks
}
sig Pk_k extends Node {
} {
type = K + o + n + d + u + s + e
no args
#wit = 1
wit[0] in Sig
correctness_holds
non_malleability_holds
xpect [ sat, wit[0] in ValidSig ]
xpect [ dsat, wit[0] in EmptySig ]
never [ nc_sat ]
never [ nc_dsat ]
no timelocks
}
sig Pk_h extends Node {
} {
type = K + n + u + d + s + e
no args
#wit = 2
wit[0] in PubKey
wit[1] in Sig
correctness_holds
non_malleability_holds
xpect [ sat, wit[1] in ValidSig ]
xpect [ dsat, wit[1] in EmptySig ]
never [ nc_sat ]
never [ nc_dsat ]
no timelocks
}
abstract sig Timelock extends Node {
} {
type = B + z + f
no args
no wit
correctness_holds
non_malleability_holds
always [ sat ]
never [ dsat ]
never [ nc_sat ]
never [ nc_dsat ]
timelocks = tl_height or timelocks = tl_time
}
sig Older extends Timelock {}
sig After extends Timelock {}
abstract sig Hash extends Node {
} {
type = B + o + n + u + d
no args
#wit = 1
wit[0] in Preimage
correctness_holds
non_malleability_holds
xpect [ sat, wit[0] in CorrectPreimage ]
xpect [ dsat, wit[0] in WrongPreimage ]
never [ nc_sat ]
never [ nc_dsat ]
no timelocks
}
sig Sha256 extends Hash {}
sig Hash256 extends Hash {}
sig Ripemd160 extends Hash {}
sig Hash160 extends Hash {}
sig Andor extends Node {
} {
no wit
#args = 3
let X = args[0], Y = args[1], Z = args[2]
{
type = btype[Y]
+ maybe [ z, z[X] and z[Y] and z[Z] ]
+ maybe [ o, (z[X] and o[Y] and o[Z]) or
(o[X] and z[Y] and z[Z]) ]
+ maybe [ u, u[Y] and u[Z] ]
+ maybe [ d, d[Z] ]
+ maybe [ s, s[Z] and (s[X] or s[Y]) ]
+ maybe [ f, f[Z] and (s[X] or f[Y]) ]
+ maybe [ e, e[Z] and (s[X] or f[Y]) ]
xpect [
correctness_holds,
{
B + d + u in X.@type
btype[Y] in B + K + V
btype[Z] = btype[Y]
}
]
xpect [ non_malleability_holds, non_malleable_args and
e[X] and (s[X] or s[Y] or s[Z]) ]
xpect [
sat, (sat[Y] and sat[X]) or
(sat[Z] and dsat[X])
]
xpect [ dsat, (dsat[Z] and dsat[X]) or nc_dsat ]
never [ nc_sat ]
xpect [ nc_dsat, dsat[Y] and sat[X] ]
timelocks = timelocks_combined[X + Y] + timelocks_combined[X + Z]
args.ignored = ( dsat[X] => Y else Z )
}
}
sig And_v extends Node {
} {
no wit
#args = 2
let X = args[0], Y = args[1]
{
type = btype[Y]
+ maybe [ z, (z[X] and z[Y]) ]
+ maybe [ o, (z[X] and o[Y]) or
(z[Y] and o[X]) ]
+ maybe [ n, n[X] or
(z[X] and n[Y]) ]
+ maybe [ u, u[Y] ]
+ maybe [ s, s[X] or s[Y] ]
+ maybe [ f, s[X] or f[Y] ]
xpect [
correctness_holds,
{
btype[X] in V
btype[Y] in B + K + V
}
]
xpect [ non_malleability_holds, non_malleable_args ]
xpect [ sat, sat[Y] and sat[X] ]
xpect [ dsat, nc_dsat ]
never [ nc_sat ]
xpect [ nc_dsat, dsat[Y] and sat[X] ]
timelocks = timelocks_combined[X + Y]
no args.ignored
}
}
sig And_b extends Node {
} {
no wit
#args = 2
let X = args[0], Y = args[1]
{
type = B + maybe [ z, (z[X] and z[Y]) ]
+ maybe [ o, (z[X] and o[Y]) or
(z[Y] and o[X]) ]
+ maybe [ n, n[X] or
(z[X] and n[Y]) ]
+ maybe [ d, d[X] and d[Y] ]
+ just [ u ]
+ maybe [ s, s[X] or s[Y] ]
+ maybe [ f, (f[X] and f[Y]) or
(s[X] and f[X]) or
(s[Y] and f[Y]) ]
+ maybe [ e, e[X] and e[Y] and s[X] and s[Y] ]
xpect [
correctness_holds,
{
btype[X] in B
btype[Y] in W
}
]
xpect [ non_malleability_holds, non_malleable_args ]
xpect [ sat, sat[Y] and sat[X] ]
xpect [ dsat, (dsat[Y] and dsat[X]) or nc_dsat ]
never [ nc_sat ]
xpect [ nc_dsat, ( sat[Y] and dsat[X]) or
(dsat[Y] and sat[X]) ]
timelocks = timelocks_combined[X + Y]
no args.ignored
}
}
sig Or_b extends Node {
} {
no wit
#args = 2
let X = args[0], Z = args[1]
{
type = B + maybe [ z, (z[X] and z[Z]) ]
+ maybe [ o, (z[X] and o[Z]) or
(z[Z] and o[X]) ]
+ just [ d ]
+ just [ u ]
+ maybe [ s, (s[X] and s[Z]) ]
+ just [ e ]
xpect [
correctness_holds,
{
B + d in X.@type
W + d in Z.@type
}
]
xpect [ non_malleability_holds, non_malleable_args and
e[X] and e[Z] and (s[X] or s[Z]) ]
xpect [
sat, (dsat[Z] and sat[X]) or
( sat[Z] and dsat[X]) or
nc_sat
]
xpect [ dsat, dsat[Z] and dsat[X] ]
xpect [ nc_sat, sat[Z] and sat[X] ]
never [ nc_dsat ]
timelocks = (@timelocks[X] + @timelocks[Z])
no args.ignored
}
}
sig Or_c extends Node {
} {
no wit
#args = 2
let X = args[0], Z = args[1]
{
type = V + maybe [ z, z[X] and z[Z] ]
+ maybe [ o, o[X] and z[Z] ]
+ maybe [ s, s[X] and s[Z] ]
+ just [ f ]
xpect [
correctness_holds,
{
B + d + u in X.@type
btype[Z] in V
}
]
xpect [ non_malleability_holds, non_malleable_args and
e[X] and (s[X] or s[Z]) ]
xpect [ sat, sat[X] or (sat[Z] and dsat[X]) ]
never [ dsat ]
never [ nc_sat ]
never [ nc_dsat ]
timelocks = (@timelocks[X] + @timelocks[Z])
args.ignored = maybe [ Z, sat[X] ]
}
}
sig Or_d extends Node {
} {
no wit
#args = 2
let X = args[0], Z = args[1]
{
type = B + maybe [ z, z[X] and z[Z] ]
+ maybe [ o, o[X] and z[Z] ]
+ maybe [ d, d[Z] ]
+ maybe [ u, u[Z] ]
+ maybe [ s, s[X] and s[Z] ]
+ maybe [ f, f[Z] ]
+ maybe [ e, e[Z] ]
xpect [
correctness_holds,
{
B + d + u in X.@type
btype[Z] in B
}
]
xpect [ non_malleability_holds, non_malleable_args and
e[X] and (s[X] or s[Z]) ]
xpect [ sat, sat[X] or (sat[Z] and dsat[X]) ]
xpect [ dsat, dsat[Z] and dsat[X] ]
never [ nc_sat ]
never [ nc_dsat ]
timelocks = (@timelocks[X] + @timelocks[Z])
args.ignored = maybe [ Z, sat[X] ]
}
}
sig Or_i extends Node {
} {
#wit = 1
wit[0] in WitBool
#args = 2
let X = args[0], Z = args[1]
{
type = btype[X]
+ maybe [ o, z[X] and z[Z] ]
+ maybe [ u, u[X] and u[Z] ]
+ maybe [ d, d[X] or d[Z] ]
+ maybe [ s, s[X] and s[Z] ]
+ maybe [ f, f[X] and f[Z] ]
+ maybe [ e, (e[X] and f[Z]) or
(e[Z] and f[X]) ]
xpect [
correctness_holds,
{
btype[X] in B + K + V
btype[X] = btype[Z]
}
]
xpect [ non_malleability_holds, non_malleable_args and
(s[X] or s[Z]) ]
xpect [ sat, wit[0] in WitZero => sat[Z] else sat[X] ]
xpect [ dsat, wit[0] in WitZero => dsat[Z] else dsat[X] ]
never [ nc_sat ]
never [ nc_dsat ]
timelocks = (@timelocks[X] + @timelocks[Z])
args.ignored = ( wit[0] in WitZero => X else Z )
}
}
sig Thresh extends Node {
num_args: Int,
required: Int
} {
no wit
#args = num_args
1 < required
required < num_args
type = B + maybe [ z, all arg: args.elems | z[arg] ]
+ maybe [ o, one arg_o: args.elems, args_non_o: args.elems - arg_o
{
o[arg_o]
all arg: args_non_o | z[arg]
} ]
+ just [ d ]
+ just [ u ]
+ maybe [ s, #{ arg: args.elems | not s[arg] } <= required.minus[1] ]
+ maybe [ e, all arg: args.elems | s[arg] ]
xpect [
correctness_holds,
{
B + d + u in args.first.@type
all arg: args.rest.elems {
W + d + u in arg.@type
}
}
]
xpect [
non_malleability_holds,
{
non_malleable_args
all arg: args.elems | e[arg]
#{ arg: args.elems | not s[arg] } <= required
}
]
let num_sats = #{arg: args.elems | sat[arg]},
num_dsats = #{arg: args.elems | dsat[arg]}
{
xpect [ sat, num_sats = required ]
xpect [ dsat, num_dsats = num_args or nc_dsat ]
never [ nc_sat ]
xpect [ nc_dsat, num_sats != required and num_dsats != num_args ]
}
timelocks = timelocks_combined[args.elems]
no args.ignored
}
sig Multi extends Node {
num_args: Int,
required: Int
} {
num_args <= 20
1 < required
required < num_args
no args
correctness_holds
non_malleability_holds
#wit = required.plus[1]
wit.last in DummyWitness
all wt: wit.butlast.elems | wt in Sig
type = B + n + d + u + s + e
let all_empty = (all wt: wit.butlast.elems | wt in EmptySig),
all_valid = (all wt: wit.butlast.elems | wt in ValidSig)
{
all_empty or all_valid // Otherwise fails with ERR_SIG_NULLFAIL
xpect [ sat, all_valid ]
xpect [ dsat, all_empty ]
never [ nc_sat ]
never [ nc_dsat ]
}
no timelocks
}
abstract sig Wrapper extends Node {
} {
#args = 1
xpect [ non_malleability_holds, non_malleable_args ]
timelocks = @timelocks[args[0]]
}
sig AWrap extends Wrapper {
} {
no wit
let X = args[0]
{
xpect [ correctness_holds, B in X.@type ]
type = W + maybe [ d, d[X] ]
+ maybe [ u, u[X] ]
+ maybe [ s, s[X] ]
+ maybe [ f, f[X] ]
+ maybe [ e, e[X] ]
xpect [ sat, sat[X] ]
xpect [ dsat, dsat[X] ]
never [ nc_sat ]
never [ nc_dsat ]
no args.ignored
}
}
sig SWrap extends Wrapper {
} {
no wit
let X = args[0]
{
xpect [ correctness_holds, B + o in X.@type ]
type = W + maybe [ d, d[X] ]
+ maybe [ u, u[X] ]
+ maybe [ s, s[X] ]
+ maybe [ f, f[X] ]
+ maybe [ e, e[X] ]
xpect [ sat, sat[X] ]
xpect [ dsat, dsat[X] ]
never [ nc_sat ]
never [ nc_dsat ]
no args.ignored
}
}
sig CWrap extends Wrapper {
} {
no wit
let X = args[0]
{
xpect [ correctness_holds, K in X.@type ]
type = B + maybe [ o, o[X] ]
+ maybe [ n, n[X] ]
+ maybe [ d, d[X] ]
+ just [ u ]
+ just [ s ]
+ maybe [ f, f[X] ]
+ maybe [ e, e[X] ]
xpect [ sat, sat[X] ]
xpect [ dsat, dsat[X] ]
never [ nc_sat ]
never [ nc_dsat ]
no args.ignored
}
}
sig DWrap extends Wrapper {
} {
#wit = 1
wit[0] in WitBool
let X = args[0]
{
xpect [ correctness_holds, V + z in X.@type ]
type = B + o + n + d + u
+ maybe [ s, s[X] ]
+ just [ e ]
xpect [ sat, wit[0] in WitOne ] // sat[X] is irrelevant because X is V
xpect [ dsat, wit[0] in WitZero ]
never [ nc_sat ]
never [ nc_dsat ]
args.ignored = maybe [ X, wit[0] in WitZero ]
}
}
sig VWrap extends Wrapper {
} {
no wit
let X = args[0]
{
xpect [ correctness_holds, B in X.@type ]
type = V + maybe [ z, z[X] ]
+ maybe [ o, o[X] ]
+ maybe [ n, n[X] ]
+ maybe [ s, s[X] ]
+ just [ f ]
always [ sat[X] ] // VERIFY will be applied
always [ sat ]
never [ dsat ]
never [ nc_sat ]
never [ nc_dsat ]
no args.ignored
}
}
sig JWrap extends Wrapper {
} {
#wit = 1 or #wit = 0
#wit = 1 => wit[0] in WitZero
let X = args[0]
{
xpect [ correctness_holds, B + n in X.@type ]
type = B + maybe [ o, o[X] ]
+ just [ n ]
+ just [ d ]
+ maybe [ u, u[X] ]
+ maybe [ s, s[X] ]
+ maybe [ e, f[X] ]
xpect [ sat, #wit = 0 and sat[X] ]
xpect [ dsat, #wit > 0 or nc_dsat ]
never [ nc_sat ]
xpect [ nc_dsat, #wit = 0 and dsat[X] ]
args.ignored = maybe [ X, #wit > 0 ]
}
}
sig NWrap extends Wrapper {
} {
no wit
let X = args[0]
{
xpect [ correctness_holds, B in X.@type ]
type = B + maybe [ z, z[X] ]
+ maybe [ o, o[X] ]
+ maybe [ n, n[X] ]
+ maybe [ d, d[X] ]
+ just [ u ]
+ maybe [ s, s[X] ]
+ maybe [ f, f[X] ]
+ maybe [ e, e[X] ]
xpect [ sat, sat[X] ]
xpect [ dsat, dsat[X] ]
never [ nc_sat ]
never [ nc_dsat ]
no args.ignored
}
}
/*********************/
/* Witnesses section */
/*********************/
abstract sig Witness {}
lone sig DummyWitness extends Witness {} // CHECKMULTISIG dummy input
abstract sig WitBool extends Witness {}
lone sig WitZero extends WitBool {}
lone sig WitOne extends WitBool {}
abstract sig Sig extends Witness {}
lone sig ValidSig extends Sig {}
lone sig EmptySig extends Sig {}
lone sig PubKey extends Witness {}
abstract sig Preimage extends Witness {}
lone sig CorrectPreimage extends Preimage {}
lone sig WrongPreimage extends Preimage {}
/***********************/
/* Definitions section */
/***********************/
// Anything that is needed for the spec to function and be readable
// The node definition
abstract sig Node {
type: set TypeDesignator,
args: seq Node,
wit: seq Witness,
timelocks: set TimelockType
} {
V in type => this not in DSat // V cannot be dissatisfied
}
one sig RootNode in Node {
} {
B in type
this not in IgnoredNode
}
fact {
treeRootedAt[args.as_set, RootNode]
// no stray witnesses
Witness in RootNode.*(args.as_set).wit.elems
// no duplicate args
(sum node: RootNode.*(args.as_set) | #node.args) = #(RootNode.^(args.as_set))
}
// more readable access to BasicType
fun btype [node: Node]: BasicType { node.type & BasicType }
// to define ignored-node relation more readably
fun ignored [a: seq Node]: set Node { a.elems & IgnoredNode }
// Convenience macro to convert sequence to set
let as_set[sq] = select13[sq]
// Node types are modelled as a single enum, from which
// subsets are drawn to designate different kind of types
//
// Using one-letter identifiers for types is a bit
// risky, because we could confuse 'z' type modifier
// with 'Z' argument, but at the same time one-letter
// types make the spec more readable, and closer to the
// original specification in prose, which also uses
// one-letter identifiers. This means that any
// one-letter identifier we use should be directly linked
// to their spec meaning (no 'n: Node', only 'node: Node'),
// to avoid any potential clashes
enum TypeDesignator { B, V, K, W, z, o, n, d, u, s, f, e }
sig BasicType in TypeDesignator {}
fact { B + V + K + W = BasicType }
sig CorrectnessTypeModifier in TypeDesignator {}
fact { z + o + n + d + u = CorrectnessTypeModifier }
sig NonmalleabilityTypeModifier in TypeDesignator {}
fact { s + f + e = NonmalleabilityTypeModifier }
pred basic_types_and_modifiers_correctly_specified {
BasicType + CorrectnessTypeModifier + NonmalleabilityTypeModifier = TypeDesignator
no BasicType & CorrectnessTypeModifier
no BasicType & NonmalleabilityTypeModifier
no NonmalleabilityTypeModifier & CorrectnessTypeModifier
all node: Node {
#btype[node] = 1 // single basic type always specified
}
}
// convenience predicates for type modifiers
pred z [node: Node] { z in node.type }
pred o [node: Node] { o in node.type }
pred n [node: Node] { n in node.type }
pred d [node: Node] { d in node.type }
pred u [node: Node] { u in node.type }
pred s [node: Node] { s in node.type }
pred f [node: Node] { f in node.type }
pred e [node: Node] { e in node.type }
// convenience macros for readability
let maybe [ set_exp, cond ] = { cond => set_exp else none }
let xpect [ bool_exp, cond ] = { cond => bool_exp else !bool_exp }
let never [ bool_exp ] = { not bool_exp }
let always [ bool_exp ] = { bool_exp }
let just [ exp ] = { exp }
sig Sat in Node {} // satisfied nodes
sig NC_Sat in Node {} // non-canonically satisfied nodes
sig DSat in Node {} // dissatisfied nodes
sig NC_DSat in Node {} // non-canonically dissatisfied nodes
pred sat [node: Node] { node in Sat }
pred nc_sat [node: Node] { node in NC_Sat }
pred dsat [node: Node] { node in DSat }
pred nc_dsat [node: Node] { node in NC_DSat }
enum TimelockType { tl_height, tl_time, tl_conflict }
// Nodes for which combination of height and time locks is forbidden
// (Thresh and And_*) must use this function to combine timelocks of
// their args. If there's a conflict, tl_conflict will be added.
fun timelocks_combined [nodes: set Node]: set TimelockType {
let combined = timelocks[nodes] {
combined + maybe[ tl_conflict, tl_height + tl_time in combined ]
}
}
// Nodes that won't be executed with current witness configuration will be
// placed in IgnoredNode and their descendands will be
// placed in TransitivelyIgnoredNode
sig IgnoredNode in Node {}
sig TransitivelyIgnoredNode in Node {}
fact { TransitivelyIgnoredNode = IgnoredNode.^(args.as_set) }
// Predicates to help define correctness and non-malleability properties
sig CorrectnessHolds in Node {}
sig NonMalleableHolds in Node {}
pred correctness_holds [node: Node] { node in CorrectnessHolds }
pred non_malleability_holds [node: Node] { node in NonMalleableHolds }
pred non_malleable_args [node: Node] { node.args.elems in NonMalleableHolds }
pred correctness_holds_for_all_nodes { Node = CorrectnessHolds }
/********************/
/* Analysis section */
/********************/
// Predicates for reducing the search space for solver
pred one_is_only_used_by_And_v {
no (One & RootNode)
no (One & (Node - And_v).args.elems)
no (One & And_v.args[0])
}
pred zero_is_only_used_by_Andor_and_Or_i {
no (Zero & RootNode)
no (Zero & (Node - Andor - Or_i).args.elems)
no (Zero & Andor.args.butlast.elems)
all node: Or_i | node.args[0] in Zero <=> node.args[1] not in Zero
}
pred no_useless_one_or_zero {
one_is_only_used_by_And_v
zero_is_only_used_by_Andor_and_Or_i
}
pred no_useless_wrappers {
no wrp: Wrapper | wrp.type = wrp.args[0].type
}
pred identical_fragments_disabled {
// Only Sha256
no Hash256
no Ripemd160
no Hash160
// Only After
no Older
}
pred reduced_search_space {
identical_fragments_disabled
Thresh.num_args <= 3
Multi.num_args <= 3
no_useless_one_or_zero
no_useless_wrappers
}
// run and check clauses
pred main_search_predicate {
reduced_search_space
correctness_holds_for_all_nodes
RootNode in NonMalleableHolds
tl_conflict not in timelocks[RootNode]
RootNode.sat
s[RootNode]
}
// Note that currently there are 8 possible witness instances:
// one DummyWitness, one PubKey, two of Sig, two of Preimage, WitBool.
// If more witness types are added, the max witness counts for the
// run and check clauses should be updated.
run main {
main_search_predicate
} for 6 but 12 Node, 8 Witness, 6 Int, 4 seq
pred sat_iff_dsat {
correctness_holds_for_all_nodes => {
Node = Sat + DSat and no Sat & DSat // sat iff dsat
}
}
pred sat_s_dsat_f_always_have_valid_sig {
{
correctness_holds_for_all_nodes
RootNode in NonMalleableHolds
}
implies {
all node: Sat | s[node] => ValidSig in node.*(args.as_set).wit.elems
all node: DSat | f[node] => ValidSig in node.*(args.as_set).wit.elems
}
}
pred no_type_conflicts_on_correcntess {
correctness_holds_for_all_nodes => {
all node: Node {
z + o not in node.type
n + z not in node.type
V + d not in node.type
V + u not in node.type
}
}
}
pred no_type_conflicts_on_non_malleability {
{
correctness_holds_for_all_nodes
RootNode in NonMalleableHolds
}
implies
{
all node: Node {
e + f not in node.type
V + e not in node.type
d + f not in node.type
}
}
}
pred implications_on_correcntess_hold {
correctness_holds_for_all_nodes => {
all node: Node | K in node.type implies u[node]
}
}
pred implications_on_non_malleability_hold {
correctness_holds_for_all_nodes => {
all node: Node {
e[node] implies d[node]
V in node.type implies f[node]
K in node.type implies s[node]
z[node] implies node in NonMalleableHolds