Skip to content

Commit 2565f5e

Browse files
Add SigType check in CompilerExtData for deciding key byte-lengths
1 parent 05a71e2 commit 2565f5e

File tree

6 files changed

+85
-67
lines changed

6 files changed

+85
-67
lines changed

src/miniscript/mod.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -644,38 +644,38 @@ mod tests {
644644
let pkk_ms: Miniscript<DummyKey, Segwitv0> = Miniscript {
645645
node: Terminal::Check(Arc::new(Miniscript {
646646
node: Terminal::PkK(DummyKey),
647-
ty: Type::from_pk_k(),
648-
ext: types::extra_props::ExtData::from_pk_k(),
647+
ty: Type::from_pk_k::<Segwitv0>(),
648+
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
649649
phantom: PhantomData,
650650
})),
651-
ty: Type::cast_check(Type::from_pk_k()).unwrap(),
652-
ext: ExtData::cast_check(ExtData::from_pk_k()).unwrap(),
651+
ty: Type::cast_check(Type::from_pk_k::<Segwitv0>()).unwrap(),
652+
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
653653
phantom: PhantomData,
654654
};
655655
string_rtt(pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(DummyKey)", "pk()");
656656

657657
let pkh_ms: Miniscript<DummyKey, Segwitv0> = Miniscript {
658658
node: Terminal::Check(Arc::new(Miniscript {
659659
node: Terminal::PkH(DummyKeyHash),
660-
ty: Type::from_pk_h(),
661-
ext: types::extra_props::ExtData::from_pk_h(),
660+
ty: Type::from_pk_h::<Segwitv0>(),
661+
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
662662
phantom: PhantomData,
663663
})),
664-
ty: Type::cast_check(Type::from_pk_h()).unwrap(),
665-
ext: ExtData::cast_check(ExtData::from_pk_h()).unwrap(),
664+
ty: Type::cast_check(Type::from_pk_h::<Segwitv0>()).unwrap(),
665+
ext: ExtData::cast_check(ExtData::from_pk_h::<Segwitv0>()).unwrap(),
666666
phantom: PhantomData,
667667
};
668668
string_rtt(pkh_ms, "[B/nduesm]c:[K/nduesm]pk_h(DummyKeyHash)", "pkh()");
669669

670670
let pkk_ms: Segwitv0Script = Miniscript {
671671
node: Terminal::Check(Arc::new(Miniscript {
672672
node: Terminal::PkK(pk),
673-
ty: Type::from_pk_k(),
674-
ext: types::extra_props::ExtData::from_pk_k(),
673+
ty: Type::from_pk_k::<Segwitv0>(),
674+
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
675675
phantom: PhantomData,
676676
})),
677-
ty: Type::cast_check(Type::from_pk_k()).unwrap(),
678-
ext: ExtData::cast_check(ExtData::from_pk_k()).unwrap(),
677+
ty: Type::cast_check(Type::from_pk_k::<Segwitv0>()).unwrap(),
678+
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
679679
phantom: PhantomData,
680680
};
681681

@@ -688,12 +688,12 @@ mod tests {
688688
let pkh_ms: Segwitv0Script = Miniscript {
689689
node: Terminal::Check(Arc::new(Miniscript {
690690
node: Terminal::PkH(hash),
691-
ty: Type::from_pk_h(),
692-
ext: types::extra_props::ExtData::from_pk_h(),
691+
ty: Type::from_pk_h::<Segwitv0>(),
692+
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
693693
phantom: PhantomData,
694694
})),
695-
ty: Type::cast_check(Type::from_pk_h()).unwrap(),
696-
ext: ExtData::cast_check(ExtData::from_pk_h()).unwrap(),
695+
ty: Type::cast_check(Type::from_pk_h::<Segwitv0>()).unwrap(),
696+
ext: ExtData::cast_check(ExtData::from_pk_h::<Segwitv0>()).unwrap(),
697697
phantom: PhantomData,
698698
};
699699

src/miniscript/types/correctness.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! Correctness/Soundness type properties
1616
1717
use super::{ErrorKind, Property};
18+
use ScriptContext;
1819

1920
/// Basic type representing where the fragment can go
2021
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
@@ -146,7 +147,7 @@ impl Property for Correctness {
146147
}
147148
}
148149

149-
fn from_pk_k() -> Self {
150+
fn from_pk_k<Ctx: ScriptContext>() -> Self {
150151
Correctness {
151152
base: Base::K,
152153
input: Input::OneNonZero,
@@ -155,7 +156,7 @@ impl Property for Correctness {
155156
}
156157
}
157158

158-
fn from_pk_h() -> Self {
159+
fn from_pk_h<Ctx: ScriptContext>() -> Self {
159160
Correctness {
160161
base: Base::K,
161162
input: Input::AnyNonZero,
@@ -164,7 +165,7 @@ impl Property for Correctness {
164165
}
165166
}
166167

167-
fn from_multi(_: usize, _: usize) -> Self {
168+
fn from_multi<Ctx: ScriptContext>(_: usize, _: usize) -> Self {
168169
Correctness {
169170
base: Base::B,
170171
input: Input::AnyNonZero,

src/miniscript/types/extra_props.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl Property for ExtData {
177177
}
178178
}
179179

180-
fn from_pk_k() -> Self {
180+
fn from_pk_k<Ctx: ScriptContext>() -> Self {
181181
ExtData {
182182
pk_cost: 34,
183183
has_free_verify: false,
@@ -194,7 +194,7 @@ impl Property for ExtData {
194194
}
195195
}
196196

197-
fn from_pk_h() -> Self {
197+
fn from_pk_h<Ctx: ScriptContext>() -> Self {
198198
ExtData {
199199
pk_cost: 24,
200200
has_free_verify: false,
@@ -211,7 +211,7 @@ impl Property for ExtData {
211211
}
212212
}
213213

214-
fn from_multi(k: usize, n: usize) -> Self {
214+
fn from_multi<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
215215
let num_cost = match (k > 16, n > 16) {
216216
(true, true) => 4,
217217
(false, true) => 3,
@@ -234,7 +234,7 @@ impl Property for ExtData {
234234
}
235235
}
236236

237-
fn from_multi_a(k: usize, n: usize) -> Self {
237+
fn from_multi_a<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
238238
let num_cost = match (k > 16, n > 16) {
239239
(true, true) => 4,
240240
(false, true) => 3,
@@ -1011,8 +1011,8 @@ impl Property for ExtData {
10111011
let ret = match *fragment {
10121012
Terminal::True => Ok(Self::from_true()),
10131013
Terminal::False => Ok(Self::from_false()),
1014-
Terminal::PkK(..) => Ok(Self::from_pk_k()),
1015-
Terminal::PkH(..) => Ok(Self::from_pk_h()),
1014+
Terminal::PkK(..) => Ok(Self::from_pk_k::<Ctx>()),
1015+
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
10161016
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
10171017
if k == 0 {
10181018
return Err(Error {
@@ -1027,8 +1027,8 @@ impl Property for ExtData {
10271027
});
10281028
}
10291029
match *fragment {
1030-
Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())),
1031-
Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())),
1030+
Terminal::Multi(..) => Ok(Self::from_multi::<Ctx>(k, pks.len())),
1031+
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
10321032
_ => unreachable!(),
10331033
}
10341034
}

src/miniscript/types/malleability.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! Malleability-related Type properties
1616
1717
use super::{ErrorKind, Property};
18+
use ScriptContext;
1819

1920
/// Whether the fragment has a dissatisfaction, and if so, whether
2021
/// it is unique. Affects both correctness and malleability-freeness,
@@ -98,23 +99,23 @@ impl Property for Malleability {
9899
}
99100
}
100101

101-
fn from_pk_k() -> Self {
102+
fn from_pk_k<Ctx: ScriptContext>() -> Self {
102103
Malleability {
103104
dissat: Dissat::Unique,
104105
safe: true,
105106
non_malleable: true,
106107
}
107108
}
108109

109-
fn from_pk_h() -> Self {
110+
fn from_pk_h<Ctx: ScriptContext>() -> Self {
110111
Malleability {
111112
dissat: Dissat::Unique,
112113
safe: true,
113114
non_malleable: true,
114115
}
115116
}
116117

117-
fn from_multi(_: usize, _: usize) -> Self {
118+
fn from_multi<Ctx: ScriptContext>(_: usize, _: usize) -> Self {
118119
Malleability {
119120
dissat: Dissat::Unique,
120121
safe: true,

src/miniscript/types/mod.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -261,18 +261,18 @@ pub trait Property: Sized {
261261
fn from_false() -> Self;
262262

263263
/// Type property of the `PkK` fragment
264-
fn from_pk_k() -> Self;
264+
fn from_pk_k<Ctx: ScriptContext>() -> Self;
265265

266266
/// Type property of the `PkH` fragment
267-
fn from_pk_h() -> Self;
267+
fn from_pk_h<Ctx: ScriptContext>() -> Self;
268268

269269
/// Type property of a `Multi` fragment
270-
fn from_multi(k: usize, n: usize) -> Self;
270+
fn from_multi<Ctx: ScriptContext>(k: usize, n: usize) -> Self;
271271

272272
/// Type property of a `MultiA` fragment
273-
fn from_multi_a(k: usize, n: usize) -> Self {
273+
fn from_multi_a<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
274274
// default impl same as multi
275-
Self::from_multi(k, n)
275+
Self::from_multi::<Ctx>(k, n)
276276
}
277277

278278
/// Type property of a hash fragment
@@ -414,8 +414,8 @@ pub trait Property: Sized {
414414
let ret = match *fragment {
415415
Terminal::True => Ok(Self::from_true()),
416416
Terminal::False => Ok(Self::from_false()),
417-
Terminal::PkK(..) => Ok(Self::from_pk_k()),
418-
Terminal::PkH(..) => Ok(Self::from_pk_h()),
417+
Terminal::PkK(..) => Ok(Self::from_pk_k::<Ctx>()),
418+
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
419419
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
420420
if k == 0 {
421421
return Err(Error {
@@ -430,8 +430,8 @@ pub trait Property: Sized {
430430
});
431431
}
432432
match *fragment {
433-
Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())),
434-
Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())),
433+
Terminal::Multi(..) => Ok(Self::from_multi::<Ctx>(k, pks.len())),
434+
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
435435
_ => unreachable!(),
436436
}
437437
}
@@ -563,24 +563,24 @@ impl Property for Type {
563563
}
564564
}
565565

566-
fn from_pk_k() -> Self {
566+
fn from_pk_k<Ctx: ScriptContext>() -> Self {
567567
Type {
568-
corr: Property::from_pk_k(),
569-
mall: Property::from_pk_k(),
568+
corr: Property::from_pk_k::<Ctx>(),
569+
mall: Property::from_pk_k::<Ctx>(),
570570
}
571571
}
572572

573-
fn from_pk_h() -> Self {
573+
fn from_pk_h<Ctx: ScriptContext>() -> Self {
574574
Type {
575-
corr: Property::from_pk_h(),
576-
mall: Property::from_pk_h(),
575+
corr: Property::from_pk_h::<Ctx>(),
576+
mall: Property::from_pk_h::<Ctx>(),
577577
}
578578
}
579579

580-
fn from_multi(k: usize, n: usize) -> Self {
580+
fn from_multi<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
581581
Type {
582-
corr: Property::from_multi(k, n),
583-
mall: Property::from_multi(k, n),
582+
corr: Property::from_multi::<Ctx>(k, n),
583+
mall: Property::from_multi::<Ctx>(k, n),
584584
}
585585
}
586586

@@ -797,8 +797,8 @@ impl Property for Type {
797797
let ret = match *fragment {
798798
Terminal::True => Ok(Self::from_true()),
799799
Terminal::False => Ok(Self::from_false()),
800-
Terminal::PkK(..) => Ok(Self::from_pk_k()),
801-
Terminal::PkH(..) => Ok(Self::from_pk_h()),
800+
Terminal::PkK(..) => Ok(Self::from_pk_k::<Ctx>()),
801+
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
802802
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
803803
if k == 0 {
804804
return Err(Error {
@@ -813,8 +813,8 @@ impl Property for Type {
813813
});
814814
}
815815
match *fragment {
816-
Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())),
817-
Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())),
816+
Terminal::Multi(..) => Ok(Self::from_multi::<Ctx>(k, pks.len())),
817+
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
818818
_ => unreachable!(),
819819
}
820820
}

src/policy/compiler.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::convert::From;
2222
use std::marker::PhantomData;
2323
use std::{cmp, error, f64, fmt, mem};
2424

25+
use miniscript::context::SigType;
2526
use miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
2627
use miniscript::types::{self, ErrorKind, ExtData, Property, Type};
2728
use miniscript::ScriptContext;
@@ -167,26 +168,41 @@ impl Property for CompilerExtData {
167168
}
168169
}
169170

170-
fn from_pk_k() -> Self {
171+
fn from_pk_k<Ctx: ScriptContext>() -> Self {
171172
CompilerExtData {
172173
branch_prob: None,
173-
sat_cost: 73.0,
174+
sat_cost: match Ctx::sig_type() {
175+
SigType::Ecdsa => 73.0,
176+
SigType::Schnorr => 1.0 /* <var_int> */ + 64.0 /* sig */ + 1.0, /* <sighash_type> */
177+
},
174178
dissat_cost: Some(1.0),
175179
}
176180
}
177181

178-
fn from_pk_h() -> Self {
182+
fn from_pk_h<Ctx: ScriptContext>() -> Self {
179183
CompilerExtData {
180184
branch_prob: None,
181-
sat_cost: 73.0 + 34.0,
182-
dissat_cost: Some(1.0 + 34.0),
185+
sat_cost: match Ctx::sig_type() {
186+
SigType::Ecdsa => 73.0 + 34.0,
187+
SigType::Schnorr => 66.0 + 32.0,
188+
},
189+
dissat_cost: Some(
190+
1.0 + match Ctx::sig_type() {
191+
SigType::Ecdsa => 34.0,
192+
SigType::Schnorr => 32.0,
193+
},
194+
),
183195
}
184196
}
185197

186-
fn from_multi(k: usize, _n: usize) -> Self {
198+
fn from_multi<Ctx: ScriptContext>(k: usize, _n: usize) -> Self {
187199
CompilerExtData {
188200
branch_prob: None,
189-
sat_cost: 1.0 + 73.0 * k as f64,
201+
sat_cost: 1.0
202+
+ match Ctx::sig_type() {
203+
SigType::Ecdsa => 73.0 * k as f64,
204+
SigType::Schnorr => unreachable!(),
205+
},
190206
dissat_cost: Some(1.0 * (k + 1) as f64),
191207
}
192208
}
@@ -311,6 +327,14 @@ impl Property for CompilerExtData {
311327
})
312328
}
313329

330+
fn and_n(a: Self, b: Self) -> Result<Self, types::ErrorKind> {
331+
Ok(CompilerExtData {
332+
branch_prob: None,
333+
sat_cost: a.sat_cost + b.sat_cost,
334+
dissat_cost: a.dissat_cost,
335+
})
336+
}
337+
314338
fn or_b(l: Self, r: Self) -> Result<Self, types::ErrorKind> {
315339
let lprob = l
316340
.branch_prob
@@ -403,14 +427,6 @@ impl Property for CompilerExtData {
403427
})
404428
}
405429

406-
fn and_n(a: Self, b: Self) -> Result<Self, types::ErrorKind> {
407-
Ok(CompilerExtData {
408-
branch_prob: None,
409-
sat_cost: a.sat_cost + b.sat_cost,
410-
dissat_cost: a.dissat_cost,
411-
})
412-
}
413-
414430
fn threshold<S>(k: usize, n: usize, mut sub_ck: S) -> Result<Self, types::ErrorKind>
415431
where
416432
S: FnMut(usize) -> Result<Self, types::ErrorKind>,

0 commit comments

Comments
 (0)