Skip to content

Commit 9d8402f

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

File tree

6 files changed

+127
-76
lines changed

6 files changed

+127
-76
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: 33 additions & 14 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 => 32,
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,23 +210,32 @@ 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((33, 33)),
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),
211224
}
212225
}
213226

214-
fn from_multi(k: usize, n: usize) -> Self {
227+
fn from_multi<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
215228
let num_cost = match (k > 16, n > 16) {
216229
(true, true) => 4,
217230
(false, true) => 3,
218231
(true, false) => 3,
219232
(false, false) => 2,
220233
};
221234
ExtData {
222-
pk_cost: num_cost + 34 * n + 1,
235+
pk_cost: match Ctx::sig_type() {
236+
SigType::Ecdsa => num_cost + 34 * n + 1,
237+
SigType::Schnorr => 0, // placeholder value since global_consensus invalidation follows
238+
},
223239
has_free_verify: true,
224240
ops_count_static: 1,
225241
ops_count_sat: Some(n + 1),
@@ -234,15 +250,18 @@ impl Property for ExtData {
234250
}
235251
}
236252

237-
fn from_multi_a(k: usize, n: usize) -> Self {
253+
fn from_multi_a<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
238254
let num_cost = match (k > 16, n > 16) {
239255
(true, true) => 4,
240256
(false, true) => 3,
241257
(true, false) => 3,
242258
(false, false) => 2,
243259
};
244260
ExtData {
245-
pk_cost: num_cost + 33 * n /*pks*/ + (n-1) /*checksigadds*/ + 1,
261+
pk_cost: match Ctx::sig_type() {
262+
SigType::Ecdsa => 0, // placeholder value since global_consensus invalidation follows
263+
SigType::Schnorr => num_cost + 33 * n /*pks*/ + (n - 1) /*checksigadds*/ + 1,
264+
},
246265
has_free_verify: true,
247266
ops_count_static: 1, // We don't care about opcounts in tapscript
248267
ops_count_sat: Some(n + 1),
@@ -1011,8 +1030,8 @@ impl Property for ExtData {
10111030
let ret = match *fragment {
10121031
Terminal::True => Ok(Self::from_true()),
10131032
Terminal::False => Ok(Self::from_false()),
1014-
Terminal::PkK(..) => Ok(Self::from_pk_k()),
1015-
Terminal::PkH(..) => Ok(Self::from_pk_h()),
1033+
Terminal::PkK(..) => Ok(Self::from_pk_k::<Ctx>()),
1034+
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
10161035
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
10171036
if k == 0 {
10181037
return Err(Error {
@@ -1027,8 +1046,8 @@ impl Property for ExtData {
10271046
});
10281047
}
10291048
match *fragment {
1030-
Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())),
1031-
Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())),
1049+
Terminal::Multi(..) => Ok(Self::from_multi::<Ctx>(k, pks.len())),
1050+
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
10321051
_ => unreachable!(),
10331052
}
10341053
}

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
}

0 commit comments

Comments
 (0)