-
Notifications
You must be signed in to change notification settings - Fork 131
/
context.rs
289 lines (261 loc) · 11.1 KB
/
context.rs
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
// Miniscript
// Written in 2019 by
// Sanket Kanjalkar and Andrew Poelstra
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
use miniscript::types::extra_props::{
MAX_OPS_PER_SCRIPT, MAX_STANDARD_P2WSH_SCRIPT_SIZE, MAX_STANDARD_P2WSH_STACK_ITEMS,
};
use std::fmt;
use bitcoin;
use {descriptor::SortedMultiVec, Miniscript, MiniscriptKey, Terminal, ToPublicKey};
/// Error for Script Context
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum ScriptContextError {
/// Script Context does not permit PkH for non-malleability
/// It is not possible to estimate the pubkey size at the creation
/// time because of uncompressed pubkeys
MalleablePkH,
/// Script Context does not permit OrI for non-malleability
/// Legacy fragments allow non-minimal IF which results in malleability
MalleableOrI,
/// Script Context does not permit DupIf for non-malleability
/// Legacy fragments allow non-minimal IF which results in malleability
MalleableDupIf,
/// Only Compressed keys allowed under current descriptor
/// Segwitv0 fragments do not allow uncompressed pubkeys
CompressedOnly,
/// At least one satisfaction path in the Miniscript fragment has more than
/// `MAX_STANDARD_P2WSH_STACK_ITEMS` (100) witness elements.
MaxWitnessItemssExceeded,
/// At least one satisfaction path in the Miniscript fragment contains more
/// than `MAX_OPS_PER_SCRIPT`(201) opcodes.
MaxOpCountExceeded,
/// The Miniscript corresponding Script would be larger than `MAX_STANDARD_P2WSH_SCRIPT_SIZE`
/// bytes.
MaxWitnessScriptSizeExceeded,
}
impl fmt::Display for ScriptContextError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
ScriptContextError::MalleablePkH => write!(f, "PkH is malleable under Legacy rules"),
ScriptContextError::MalleableOrI => write!(f, "OrI is malleable under Legacy rules"),
ScriptContextError::MalleableDupIf => {
write!(f, "DupIf is malleable under Legacy rules")
}
ScriptContextError::CompressedOnly => {
write!(f, "Uncompressed pubkeys not allowed in segwit context")
}
ScriptContextError::MaxWitnessItemssExceeded => write!(
f,
"At least one spending path in the Miniscript fragment has more \
witness items than MAX_STANDARD_P2WSH_STACK_ITEMS.",
),
ScriptContextError::MaxOpCountExceeded => write!(
f,
"At least one satisfaction path in the Miniscript fragment contains \
more than MAX_OPS_PER_SCRIPT opcodes."
),
ScriptContextError::MaxWitnessScriptSizeExceeded => write!(
f,
"The Miniscript corresponding Script would be larger than \
MAX_STANDARD_P2WSH_SCRIPT_SIZE bytes."
),
}
}
}
/// The ScriptContext for Miniscript. Additional type information associated with
/// miniscript that is used for carrying out checks that dependant on the
/// context under which the script is used.
/// For example, disallowing uncompressed keys in Segwit context
pub trait ScriptContext:
fmt::Debug + Clone + Ord + PartialOrd + Eq + PartialEq + private::Sealed
{
/// Depending on ScriptContext, fragments can be malleable. For Example,
/// under Legacy context, PkH is malleable because it is possible to
/// estimate the cost of satisfaction because of compressed keys
/// This is currently only used in compiler code for removing malleable
/// compilations.
/// This does NOT recursively check if the children of the fragment are
/// valid or not. Since the compilation proceeds in a leaf to root fashion,
/// a recursive check is unnecessary.
fn check_frag_non_malleable<Pk: MiniscriptKey, Ctx: ScriptContext>(
_frag: &Terminal<Pk, Ctx>,
) -> Result<(), ScriptContextError>;
/// Depending on script Context, some of the Miniscripts might not be valid.
/// For example, in Segwit Context requiring a too high number of stack elements
/// for a satisfaction path is non-standard.
/// In both legacy and Segwit contexts using more than 201 OPs is invalid by consensus.
fn check_ms_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
ms: &Miniscript<Pk, Ctx>,
) -> Result<(), ScriptContextError>;
}
/// Legacy ScriptContext
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Legacy {}
impl ScriptContext for Legacy {
fn check_frag_non_malleable<Pk: MiniscriptKey, Ctx: ScriptContext>(
frag: &Terminal<Pk, Ctx>,
) -> Result<(), ScriptContextError> {
match *frag {
Terminal::PkH(ref _pkh) => Err(ScriptContextError::MalleablePkH),
Terminal::OrI(ref _a, ref _b) => Err(ScriptContextError::MalleableOrI),
Terminal::DupIf(ref _ms) => Err(ScriptContextError::MalleableDupIf),
_ => Ok(()),
}
}
fn check_ms_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
ms: &Miniscript<Pk, Ctx>,
) -> Result<(), ScriptContextError> {
if let Some(op_count) = ms.ext.ops_count_sat {
if op_count > MAX_OPS_PER_SCRIPT {
return Err(ScriptContextError::MaxOpCountExceeded);
}
}
Ok(())
}
}
/// Segwitv0 ScriptContext
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Segwitv0 {}
impl ScriptContext for Segwitv0 {
fn check_frag_non_malleable<Pk: MiniscriptKey, Ctx: ScriptContext>(
_frag: &Terminal<Pk, Ctx>,
) -> Result<(), ScriptContextError> {
Ok(())
}
fn check_ms_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
ms: &Miniscript<Pk, Ctx>,
) -> Result<(), ScriptContextError> {
// We don't need to know if this is actually a p2wsh as the standard satisfaction for
// other Segwitv0 defined programs all require (much) less than 100 elements.
// The witness script item is accounted for in max_satisfaction_witness_elements().
if let Some(max_witness_items) = ms.max_satisfaction_witness_elements() {
if max_witness_items > MAX_STANDARD_P2WSH_STACK_ITEMS {
return Err(ScriptContextError::MaxWitnessItemssExceeded);
}
}
if ms.ext.pk_cost > MAX_STANDARD_P2WSH_SCRIPT_SIZE {
return Err(ScriptContextError::MaxWitnessScriptSizeExceeded);
}
if let Some(op_count) = ms.ext.ops_count_sat {
if op_count > MAX_OPS_PER_SCRIPT {
return Err(ScriptContextError::MaxOpCountExceeded);
}
}
match ms.node {
Terminal::PkK(ref pk) => {
if pk.is_uncompressed() {
return Err(ScriptContextError::CompressedOnly);
}
Ok(())
}
_ => Ok(()),
}
}
}
/// Any ScriptContext. None of the checks should ever be invoked from
/// under this context.
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum Any {}
impl ScriptContext for Any {
fn check_frag_non_malleable<Pk: MiniscriptKey, Ctx: ScriptContext>(
_frag: &Terminal<Pk, Ctx>,
) -> Result<(), ScriptContextError> {
unreachable!()
}
fn check_ms_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
_ms: &Miniscript<Pk, Ctx>,
) -> Result<(), ScriptContextError> {
unreachable!()
}
}
#[allow(unsafe_code)]
impl Any {
pub(crate) fn from_legacy<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Legacy>,
) -> &Miniscript<Pk, Any> {
// The fields Miniscript<Pk, Legacy> and Miniscript<Any, Legacy> only
// differ in PhantomData. This unsafe assumes that the unlerlying byte
// representation of the both should be the same. There is a Miri test
// checking the same.
unsafe {
use std::mem::transmute;
transmute::<&Miniscript<Pk, Legacy>, &Miniscript<Pk, Any>>(ms)
}
}
pub(crate) fn from_segwitv0<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Segwitv0>,
) -> &Miniscript<Pk, Any> {
// The fields Miniscript<Pk, Legacy> and Miniscript<Any, Legacy> only
// differ in PhantomData. This unsafe assumes that the unlerlying byte
// representation of the both should be the same. There is a Miri test
// checking the same.
unsafe {
use std::mem::transmute;
transmute::<&Miniscript<Pk, Segwitv0>, &Miniscript<Pk, Any>>(ms)
}
}
pub(crate) fn from_smv_legacy<Pk: MiniscriptKey + ToPublicKey>(
smv: &SortedMultiVec<Pk, Legacy>,
) -> &Miniscript<Pk, Any> {
let ms = &Miniscript::from_ast(smv.sorted_node()).unwrap();
// The fields Miniscript<Pk, Legacy> and Miniscript<Any, Legacy> only
// differ in PhantomData. This unsafe assumes that the unlerlying byte
// representation of the both should be the same. There is a Miri test
// checking the same.
unsafe {
use std::mem::transmute;
transmute::<&Miniscript<Pk, Legacy>, &Miniscript<Pk, Any>>(ms)
}
}
pub(crate) fn from_smv_segwitv0<Pk: MiniscriptKey + ToPublicKey>(
smv: &SortedMultiVec<bitcoin::PublicKey, Segwitv0>,
) -> &Miniscript<bitcoin::PublicKey, Any> {
let ms = &Miniscript::from_ast(smv.sorted_node()).unwrap();
// The fields Miniscript<Pk, Legacy> and Miniscript<Any, Legacy> only
// differ in PhantomData. This unsafe assumes that the unlerlying byte
// representation of the both should be the same. There is a Miri test
// checking the same.
unsafe {
use std::mem::transmute;
transmute::<
&Miniscript<bitcoin::PublicKey, Segwitv0>,
&Miniscript<bitcoin::PublicKey, Any>,
>(ms)
}
}
}
/// Private Mod to prevent downstream from implementing this public trait
mod private {
use super::{Any, Legacy, Segwitv0};
pub trait Sealed {}
// Implement for those same types, but no others.
impl Sealed for Legacy {}
impl Sealed for Segwitv0 {}
impl Sealed for Any {}
}
#[cfg(test)]
mod tests {
use super::{Any, Legacy, Segwitv0};
use std::str::FromStr;
use {DummyKey, Miniscript};
type Segwitv0Script = Miniscript<DummyKey, Segwitv0>;
type LegacyScript = Miniscript<DummyKey, Legacy>;
//miri test for unsafe code
#[test]
fn miri_test_context_transform() {
let segwit_ms = Segwitv0Script::from_str("andor(pk(),or_i(and_v(vc:pk_h(),hash160(1111111111111111111111111111111111111111)),older(1008)),pk())").unwrap();
let legacy_ms = LegacyScript::from_str("andor(pk(),or_i(and_v(vc:pk_h(),hash160(1111111111111111111111111111111111111111)),older(1008)),pk())").unwrap();
let _any = Any::from_legacy(&legacy_ms);
let _any = Any::from_segwitv0(&segwit_ms);
}
}