Skip to content

Commit 25560a3

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

File tree

6 files changed

+89
-54
lines changed

6 files changed

+89
-54
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: 3 additions & 2 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,

src/miniscript/types/extra_props.rs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use miniscript::limits::{
66
};
77

88
use super::{Error, ErrorKind, Property, ScriptContext};
9+
use miniscript::context::SigType;
910
use script_num_size;
1011
use std::cmp;
1112
use std::iter::once;
@@ -177,24 +178,30 @@ impl Property for ExtData {
177178
}
178179
}
179180

180-
fn from_pk_k() -> Self {
181+
fn from_pk_k<Ctx: ScriptContext>() -> Self {
181182
ExtData {
182-
pk_cost: 34,
183+
pk_cost: match Ctx::sig_type() {
184+
SigType::Ecdsa => 34,
185+
SigType::Schnorr => 33,
186+
},
183187
has_free_verify: false,
184188
ops_count_static: 0,
185189
ops_count_sat: Some(0),
186190
ops_count_nsat: Some(0),
187191
stack_elem_count_sat: Some(1),
188192
stack_elem_count_dissat: Some(1),
189-
max_sat_size: Some((73, 73)),
193+
max_sat_size: match Ctx::sig_type() {
194+
SigType::Ecdsa => Some((73, 73)),
195+
SigType::Schnorr => Some((66, 66)),
196+
},
190197
max_dissat_size: Some((1, 1)),
191198
timelock_info: TimeLockInfo::default(),
192199
exec_stack_elem_count_sat: Some(1), // pushes the pk
193200
exec_stack_elem_count_dissat: Some(1),
194201
}
195202
}
196203

197-
fn from_pk_h() -> Self {
204+
fn from_pk_h<Ctx: ScriptContext>() -> Self {
198205
ExtData {
199206
pk_cost: 24,
200207
has_free_verify: false,
@@ -203,8 +210,14 @@ impl Property for ExtData {
203210
ops_count_nsat: Some(3),
204211
stack_elem_count_sat: Some(2),
205212
stack_elem_count_dissat: Some(2),
206-
max_sat_size: Some((34 + 73, 34 + 73)),
207-
max_dissat_size: Some((35, 35)),
213+
max_sat_size: match Ctx::sig_type() {
214+
SigType::Ecdsa => Some((34 + 73, 34 + 73)),
215+
SigType::Schnorr => Some((66 + 33, 33 + 66)),
216+
},
217+
max_dissat_size: match Ctx::sig_type() {
218+
SigType::Ecdsa => Some((35, 35)),
219+
SigType::Schnorr => Some((34, 34)),
220+
},
208221
timelock_info: TimeLockInfo::default(),
209222
exec_stack_elem_count_sat: Some(2), // dup and hash push
210223
exec_stack_elem_count_dissat: Some(2),
@@ -242,7 +255,7 @@ impl Property for ExtData {
242255
(false, false) => 2,
243256
};
244257
ExtData {
245-
pk_cost: num_cost + 33 * n /*pks*/ + (n-1) /*checksigadds*/ + 1,
258+
pk_cost: num_cost + 33 * n /*pks*/ + (n - 1) /*checksigadds*/ + 1,
246259
has_free_verify: true,
247260
ops_count_static: 1, // We don't care about opcounts in tapscript
248261
ops_count_sat: Some(n + 1),
@@ -1011,8 +1024,8 @@ impl Property for ExtData {
10111024
let ret = match *fragment {
10121025
Terminal::True => Ok(Self::from_true()),
10131026
Terminal::False => Ok(Self::from_false()),
1014-
Terminal::PkK(..) => Ok(Self::from_pk_k()),
1015-
Terminal::PkH(..) => Ok(Self::from_pk_h()),
1027+
Terminal::PkK(..) => Ok(Self::from_pk_k::<Ctx>()),
1028+
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
10161029
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
10171030
if k == 0 {
10181031
return Err(Error {

src/miniscript/types/malleability.rs

Lines changed: 3 additions & 2 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,15 +99,15 @@ 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,

src/miniscript/types/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,10 @@ 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
270270
fn from_multi(k: usize, n: usize) -> Self;
@@ -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 {
@@ -563,17 +563,17 @@ 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

@@ -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 {

src/policy/compiler.rs

Lines changed: 33 additions & 13 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,19 +168,30 @@ 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 + 33.0,
188+
},
189+
dissat_cost: Some(
190+
1.0 + match Ctx::sig_type() {
191+
SigType::Ecdsa => 34.0,
192+
SigType::Schnorr => 33.0,
193+
},
194+
),
183195
}
184196
}
185197

@@ -191,6 +203,14 @@ impl Property for CompilerExtData {
191203
}
192204
}
193205

206+
fn from_multi_a(k: usize, n: usize) -> Self {
207+
CompilerExtData {
208+
branch_prob: None,
209+
sat_cost: 66.0 * k as f64 + (n - k) as f64,
210+
dissat_cost: Some(n as f64), /* <w_n> ... <w_1> := 0x00 ... 0x00 (n times) */
211+
}
212+
}
213+
194214
fn from_hash() -> Self {
195215
CompilerExtData {
196216
branch_prob: None,
@@ -311,6 +331,14 @@ impl Property for CompilerExtData {
311331
})
312332
}
313333

334+
fn and_n(a: Self, b: Self) -> Result<Self, types::ErrorKind> {
335+
Ok(CompilerExtData {
336+
branch_prob: None,
337+
sat_cost: a.sat_cost + b.sat_cost,
338+
dissat_cost: a.dissat_cost,
339+
})
340+
}
341+
314342
fn or_b(l: Self, r: Self) -> Result<Self, types::ErrorKind> {
315343
let lprob = l
316344
.branch_prob
@@ -403,14 +431,6 @@ impl Property for CompilerExtData {
403431
})
404432
}
405433

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-
414434
fn threshold<S>(k: usize, n: usize, mut sub_ck: S) -> Result<Self, types::ErrorKind>
415435
where
416436
S: FnMut(usize) -> Result<Self, types::ErrorKind>,

0 commit comments

Comments
 (0)