2
2
3
3
//! Malleability-related Type properties
4
4
5
- use super :: { ErrorKind , Property } ;
6
- use crate :: ScriptContext ;
5
+ use super :: ErrorKind ;
7
6
8
7
/// Whether the fragment has a dissatisfaction, and if so, whether
9
8
/// it is unique. Affects both correctness and malleability-freeness,
@@ -27,11 +26,21 @@ pub enum Dissat {
27
26
}
28
27
29
28
impl Dissat {
29
+ // FIXME rustc should eventually support derived == on enums in constfns
30
+ const fn constfn_eq ( self , other : Self ) -> bool {
31
+ matches ! (
32
+ ( self , other) ,
33
+ ( Dissat :: None , Dissat :: None )
34
+ | ( Dissat :: Unique , Dissat :: Unique )
35
+ | ( Dissat :: Unknown , Dissat :: Unknown )
36
+ )
37
+ }
38
+
30
39
/// Check whether given `Dissat` is a subtype of `other`. That is,
31
40
/// if some Dissat is `Unique` then it must be `Unknown`.
32
- fn is_subtype ( & self , other : Self ) -> bool {
41
+ const fn is_subtype ( & self , other : Self ) -> bool {
33
42
match ( * self , other) {
34
- ( x, y) if x == y => true ,
43
+ ( x, y) if x. constfn_eq ( y ) => true ,
35
44
( _, Dissat :: Unknown ) => true ,
36
45
_ => false ,
37
46
}
@@ -73,101 +82,101 @@ impl Malleability {
73
82
}
74
83
}
75
84
76
- impl Property for Malleability {
77
- fn from_true ( ) -> Self {
78
- Malleability { dissat : Dissat :: None , safe : false , non_malleable : true }
79
- }
80
-
81
- fn from_false ( ) -> Self {
82
- Malleability { dissat : Dissat :: Unique , safe : true , non_malleable : true }
83
- }
84
-
85
- fn from_pk_k < Ctx : ScriptContext > ( ) -> Self {
85
+ impl Malleability {
86
+ /// Constructor for the malleabilitiy properties of the `pk_k` fragment.
87
+ pub const fn pk_k ( ) -> Self {
86
88
Malleability { dissat : Dissat :: Unique , safe : true , non_malleable : true }
87
89
}
88
90
89
- fn from_pk_h < Ctx : ScriptContext > ( ) -> Self {
91
+ /// Constructor for the malleabilitiy properties of the `pk_h` fragment.
92
+ pub const fn pk_h ( ) -> Self {
90
93
Malleability { dissat : Dissat :: Unique , safe : true , non_malleable : true }
91
94
}
92
95
93
- fn from_multi ( _: usize , _: usize ) -> Self {
96
+ /// Constructor for the malleabilitiy properties of the `multi` fragment.
97
+ pub const fn multi ( ) -> Self {
94
98
Malleability { dissat : Dissat :: Unique , safe : true , non_malleable : true }
95
99
}
96
100
97
- fn from_multi_a ( _: usize , _: usize ) -> Self {
101
+ /// Constructor for the malleabilitiy properties of the `multi_a` fragment.
102
+ pub const fn multi_a ( ) -> Self {
98
103
Malleability { dissat : Dissat :: Unique , safe : true , non_malleable : true }
99
104
}
100
105
101
- fn from_hash ( ) -> Self {
106
+ /// Constructor for the malleabilitiy properties of any of the hash fragments.
107
+ pub const fn hash ( ) -> Self {
102
108
Malleability { dissat : Dissat :: Unknown , safe : false , non_malleable : true }
103
109
}
104
110
105
- fn from_time ( _: u32 ) -> Self {
111
+ /// Constructor for the malleabilitiy properties of either `after` or `older`.
112
+ pub const fn time ( ) -> Self {
106
113
Malleability { dissat : Dissat :: None , safe : false , non_malleable : true }
107
114
}
108
115
109
- fn cast_alt ( self ) -> Result < Self , ErrorKind > { Ok ( self ) }
116
+ /// Constructor for the malleabilitiy properties of the `a:` fragment.
117
+ pub const fn cast_alt ( self ) -> Self { self }
110
118
111
- fn cast_swap ( self ) -> Result < Self , ErrorKind > { Ok ( self ) }
119
+ /// Constructor for the malleabilitiy properties of the `s:` fragment.
120
+ pub const fn cast_swap ( self ) -> Self { self }
112
121
113
- fn cast_check ( self ) -> Result < Self , ErrorKind > { Ok ( self ) }
122
+ /// Constructor for the malleabilitiy properties of the `c:` fragment.
123
+ pub const fn cast_check ( self ) -> Self { self }
114
124
115
- fn cast_dupif ( self ) -> Result < Self , ErrorKind > {
116
- Ok ( Malleability {
117
- dissat : if self . dissat == Dissat :: None {
125
+ /// Constructor for the malleabilitiy properties of the `d:` fragment.
126
+ pub const fn cast_dupif ( self ) -> Self {
127
+ Malleability {
128
+ dissat : if self . dissat . constfn_eq ( Dissat :: None ) {
118
129
Dissat :: Unique
119
130
} else {
120
131
Dissat :: Unknown
121
132
} ,
122
133
safe : self . safe ,
123
134
non_malleable : self . non_malleable ,
124
- } )
135
+ }
125
136
}
126
137
127
- fn cast_verify ( self ) -> Result < Self , ErrorKind > {
128
- Ok ( Malleability {
129
- dissat : Dissat :: None ,
130
- safe : self . safe ,
131
- non_malleable : self . non_malleable ,
132
- } )
138
+ /// Constructor for the malleabilitiy properties of the `v:` fragment.
139
+ pub const fn cast_verify ( self ) -> Self {
140
+ Malleability { dissat : Dissat :: None , safe : self . safe , non_malleable : self . non_malleable }
133
141
}
134
142
135
- fn cast_nonzero ( self ) -> Result < Self , ErrorKind > {
136
- Ok ( Malleability {
137
- dissat : if self . dissat == Dissat :: None {
143
+ /// Constructor for the malleabilitiy properties of the `j:` fragment.
144
+ pub const fn cast_nonzero ( self ) -> Self {
145
+ Malleability {
146
+ dissat : if self . dissat . constfn_eq ( Dissat :: None ) {
138
147
Dissat :: Unique
139
148
} else {
140
149
Dissat :: Unknown
141
150
} ,
142
151
safe : self . safe ,
143
152
non_malleable : self . non_malleable ,
144
- } )
153
+ }
145
154
}
146
155
147
- fn cast_zeronotequal ( self ) -> Result < Self , ErrorKind > { Ok ( self ) }
156
+ /// Constructor for the malleabilitiy properties of the `n:` fragment.
157
+ pub const fn cast_zeronotequal ( self ) -> Self { self }
148
158
149
- fn cast_true ( self ) -> Result < Self , ErrorKind > {
150
- Ok ( Malleability {
151
- dissat : Dissat :: None ,
152
- safe : self . safe ,
153
- non_malleable : self . non_malleable ,
154
- } )
159
+ /// Constructor for the malleabilitiy properties of the `t:` fragment.
160
+ pub const fn cast_true ( self ) -> Self {
161
+ Malleability { dissat : Dissat :: None , safe : self . safe , non_malleable : self . non_malleable }
155
162
}
156
163
157
- fn cast_or_i_false ( self ) -> Result < Self , ErrorKind > {
158
- Ok ( Malleability {
159
- dissat : if self . dissat == Dissat :: None {
164
+ /// Constructor for the malleabilitiy properties of the `l:` or `u:` fragments.
165
+ pub const fn cast_or_i_false ( self ) -> Self {
166
+ Malleability {
167
+ dissat : if self . dissat . constfn_eq ( Dissat :: None ) {
160
168
Dissat :: Unique
161
169
} else {
162
170
Dissat :: Unknown
163
171
} ,
164
172
safe : self . safe ,
165
173
non_malleable : self . non_malleable ,
166
- } )
174
+ }
167
175
}
168
176
169
- fn and_b ( left : Self , right : Self ) -> Result < Self , ErrorKind > {
170
- Ok ( Malleability {
177
+ /// Constructor for the malleabilitiy properties of the `and_b` fragment.
178
+ pub const fn and_b ( left : Self , right : Self ) -> Self {
179
+ Malleability {
171
180
dissat : match ( left. dissat , right. dissat ) {
172
181
( Dissat :: None , Dissat :: None ) => Dissat :: None ,
173
182
( Dissat :: None , _) if left. safe => Dissat :: None ,
@@ -183,57 +192,62 @@ impl Property for Malleability {
183
192
} ,
184
193
safe : left. safe || right. safe ,
185
194
non_malleable : left. non_malleable && right. non_malleable ,
186
- } )
195
+ }
187
196
}
188
197
189
- fn and_v ( left : Self , right : Self ) -> Result < Self , ErrorKind > {
190
- Ok ( Malleability {
198
+ /// Constructor for the malleabilitiy properties of the `and_v` fragment.
199
+ pub const fn and_v ( left : Self , right : Self ) -> Self {
200
+ Malleability {
191
201
dissat : match ( left. safe , right. dissat ) {
192
202
( _, Dissat :: None ) => Dissat :: None , // fy
193
203
( true , _) => Dissat :: None , // sx
194
204
_ => Dissat :: Unknown ,
195
205
} ,
196
206
safe : left. safe || right. safe ,
197
207
non_malleable : left. non_malleable && right. non_malleable ,
198
- } )
208
+ }
199
209
}
200
210
201
- fn or_b ( left : Self , right : Self ) -> Result < Self , ErrorKind > {
202
- Ok ( Malleability {
211
+ /// Constructor for the malleabilitiy properties of the `or_b` fragment.
212
+ pub const fn or_b ( left : Self , right : Self ) -> Self {
213
+ Malleability {
203
214
dissat : Dissat :: Unique ,
204
215
safe : left. safe && right. safe ,
205
216
non_malleable : left. non_malleable
206
- && left. dissat == Dissat :: Unique
217
+ && left. dissat . constfn_eq ( Dissat :: Unique )
207
218
&& right. non_malleable
208
- && right. dissat == Dissat :: Unique
219
+ && right. dissat . constfn_eq ( Dissat :: Unique )
209
220
&& ( left. safe || right. safe ) ,
210
- } )
221
+ }
211
222
}
212
223
213
- fn or_d ( left : Self , right : Self ) -> Result < Self , ErrorKind > {
214
- Ok ( Malleability {
224
+ /// Constructor for the malleabilitiy properties of the `or_d` fragment.
225
+ pub const fn or_d ( left : Self , right : Self ) -> Self {
226
+ Malleability {
215
227
dissat : right. dissat ,
216
228
safe : left. safe && right. safe ,
217
229
non_malleable : left. non_malleable
218
- && left. dissat == Dissat :: Unique
230
+ && left. dissat . constfn_eq ( Dissat :: Unique )
219
231
&& right. non_malleable
220
232
&& ( left. safe || right. safe ) ,
221
- } )
233
+ }
222
234
}
223
235
224
- fn or_c ( left : Self , right : Self ) -> Result < Self , ErrorKind > {
225
- Ok ( Malleability {
236
+ /// Constructor for the malleabilitiy properties of the `or_c` fragment.
237
+ pub const fn or_c ( left : Self , right : Self ) -> Self {
238
+ Malleability {
226
239
dissat : Dissat :: None ,
227
240
safe : left. safe && right. safe ,
228
241
non_malleable : left. non_malleable
229
- && left. dissat == Dissat :: Unique
242
+ && left. dissat . constfn_eq ( Dissat :: Unique )
230
243
&& right. non_malleable
231
244
&& ( left. safe || right. safe ) ,
232
- } )
245
+ }
233
246
}
234
247
235
- fn or_i ( left : Self , right : Self ) -> Result < Self , ErrorKind > {
236
- Ok ( Malleability {
248
+ /// Constructor for the malleabilitiy properties of the `or_i` fragment.
249
+ pub const fn or_i ( left : Self , right : Self ) -> Self {
250
+ Malleability {
237
251
dissat : match ( left. dissat , right. dissat ) {
238
252
( Dissat :: None , Dissat :: None ) => Dissat :: None ,
239
253
( Dissat :: Unique , Dissat :: None ) => Dissat :: Unique ,
@@ -242,11 +256,12 @@ impl Property for Malleability {
242
256
} ,
243
257
safe : left. safe && right. safe ,
244
258
non_malleable : left. non_malleable && right. non_malleable && ( left. safe || right. safe ) ,
245
- } )
259
+ }
246
260
}
247
261
248
- fn and_or ( a : Self , b : Self , c : Self ) -> Result < Self , ErrorKind > {
249
- Ok ( Malleability {
262
+ /// Constructor for the malleabilitiy properties of the `andor` fragment.
263
+ pub const fn and_or ( a : Self , b : Self , c : Self ) -> Self {
264
+ Malleability {
250
265
dissat : match ( a. safe , b. dissat , c. dissat ) {
251
266
( _, Dissat :: None , Dissat :: Unique ) => Dissat :: Unique , //E: ez fy
252
267
( true , _, Dissat :: Unique ) => Dissat :: Unique , // E: ez sx
@@ -257,13 +272,15 @@ impl Property for Malleability {
257
272
safe : ( a. safe || b. safe ) && c. safe ,
258
273
non_malleable : a. non_malleable
259
274
&& c. non_malleable
260
- && a. dissat == Dissat :: Unique
275
+ && a. dissat . constfn_eq ( Dissat :: Unique )
261
276
&& b. non_malleable
262
277
&& ( a. safe || b. safe || c. safe ) ,
263
- } )
278
+ }
264
279
}
265
280
266
- fn threshold < S > ( k : usize , n : usize , mut sub_ck : S ) -> Result < Self , ErrorKind >
281
+ /// Constructor for the malleabilitiy properties of the `thresh` fragment.
282
+ // Cannot be constfn because it takes a closure.
283
+ pub fn threshold < S > ( k : usize , n : usize , mut sub_ck : S ) -> Result < Self , ErrorKind >
267
284
where
268
285
S : FnMut ( usize ) -> Result < Self , ErrorKind > ,
269
286
{
0 commit comments