-
Notifications
You must be signed in to change notification settings - Fork 2
/
opcount.go
163 lines (156 loc) · 4.01 KB
/
opcount.go
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
package miniscript
import "fmt"
type maxInt struct {
valid bool
value int
}
func (m maxInt) and(b maxInt) maxInt {
if !m.valid || !b.valid {
return maxInt{}
}
return maxInt{
valid: true,
value: m.value + b.value,
}
}
func (m maxInt) or(b maxInt) maxInt {
if !m.valid {
return b
}
if !b.valid {
return m
}
if m.value >= b.value {
return maxInt{
valid: true,
value: m.value,
}
}
return maxInt{
valid: true,
value: b.value,
}
}
type ops struct {
// Non-push opcodes.
count int
// Number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to dissatisfy.
dsat maxInt
// Number of keys in possibly executed OP_CHECKMULTISIG(VERIFY)s to satisfy.
sat maxInt
}
func computeOpCount(node *AST) (*AST, error) {
zero := maxInt{valid: true, value: 0}
invalid := maxInt{valid: false}
switch node.identifier {
case f_0:
node.opCount = ops{0, zero, invalid}
case f_1:
node.opCount = ops{0, invalid, zero}
case f_pk_k:
node.opCount = ops{0, zero, zero}
case f_pk_h:
node.opCount = ops{3, zero, zero}
case f_older, f_after:
node.opCount = ops{1, invalid, zero}
case f_sha256, f_hash256, f_ripemd160, f_hash160:
// TODO why invalid?
node.opCount = ops{4, invalid, zero}
case f_andor:
x, y, z := node.args[0], node.args[1], node.args[2]
node.opCount = ops{
3 + x.opCount.count + y.opCount.count + z.opCount.count,
z.opCount.dsat.and(x.opCount.dsat),
y.opCount.sat.and(x.opCount.sat).or(z.opCount.sat.and(x.opCount.dsat)),
}
case f_and_v:
x, y := node.args[0], node.args[1]
node.opCount = ops{
1 + x.opCount.count + y.opCount.count,
invalid,
y.opCount.sat.and(x.opCount.sat),
}
case f_and_b:
x, y := node.args[0], node.args[1]
node.opCount = ops{
1 + x.opCount.count + y.opCount.count,
y.opCount.dsat.and(x.opCount.dsat),
y.opCount.sat.and(x.opCount.sat),
}
case f_or_b:
x, z := node.args[0], node.args[1]
node.opCount = ops{
1 + x.opCount.count + z.opCount.count,
z.opCount.dsat.and(x.opCount.dsat),
z.opCount.dsat.and(x.opCount.sat).or(z.opCount.sat.and(x.opCount.dsat)),
}
case f_or_c:
x, z := node.args[0], node.args[1]
node.opCount = ops{
2 + x.opCount.count + z.opCount.count,
invalid,
x.opCount.sat.or(z.opCount.sat.and(x.opCount.dsat)),
}
case f_or_d:
x, z := node.args[0], node.args[1]
node.opCount = ops{
3 + x.opCount.count + z.opCount.count,
z.opCount.dsat.and(x.opCount.dsat),
x.opCount.sat.or(z.opCount.sat.and(x.opCount.dsat)),
}
case f_or_i:
x, z := node.args[0], node.args[1]
node.opCount = ops{
3 + x.opCount.count + z.opCount.count,
x.opCount.dsat.or(z.opCount.dsat),
x.opCount.sat.or(z.opCount.sat),
}
case f_thresh:
k := node.args[0].num
n := len(node.args) - 1
count := 0
dsat := invalid
sat := invalid
for _, arg := range node.args[1:] {
count += arg.opCount.count + 1
dsat = dsat.and(arg.opCount.dsat)
}
for _, subset := range subsets(n, int(k)) {
candidateOps := zero
for i, arg := range node.args[1:] {
if containsInt(subset, i) {
candidateOps = arg.opCount.sat.and(candidateOps)
} else {
candidateOps = arg.opCount.dsat.and(candidateOps)
}
}
sat = sat.or(candidateOps)
}
node.opCount = ops{count, dsat, sat}
case f_multi:
n := len(node.args) - 1
node.opCount = ops{1, maxInt{valid: true, value: n}, maxInt{valid: true, value: n}}
case f_wrap_a:
x := node.args[0]
node.opCount = ops{2 + x.opCount.count, x.opCount.dsat, x.opCount.sat}
case f_wrap_s, f_wrap_c, f_wrap_n:
x := node.args[0]
node.opCount = ops{1 + x.opCount.count, x.opCount.dsat, x.opCount.sat}
case f_wrap_d:
x := node.args[0]
node.opCount = ops{3 + x.opCount.count, zero, x.opCount.sat}
case f_wrap_v:
x := node.args[0]
opVerify := 0
if !node.args[0].props.canCollapseVerify {
opVerify = 1
}
node.opCount = ops{3 + opVerify, invalid, x.opCount.sat}
case f_wrap_j:
x := node.args[0]
node.opCount = ops{4 + x.opCount.count, zero, x.opCount.sat}
default:
return nil, fmt.Errorf("unknown identifier: %s", node.identifier)
}
return node, nil
}