From 2565f5eda28ae91ba44594f8a7ccf12801b47c1b Mon Sep 17 00:00:00 2001 From: Aman Rojjha Date: Thu, 7 Apr 2022 02:30:55 +0530 Subject: [PATCH] Add SigType check in CompilerExtData for deciding key byte-lengths --- src/miniscript/mod.rs | 32 +++++++++---------- src/miniscript/types/correctness.rs | 7 +++-- src/miniscript/types/extra_props.rs | 16 +++++----- src/miniscript/types/malleability.rs | 7 +++-- src/miniscript/types/mod.rs | 44 +++++++++++++------------- src/policy/compiler.rs | 46 +++++++++++++++++++--------- 6 files changed, 85 insertions(+), 67 deletions(-) diff --git a/src/miniscript/mod.rs b/src/miniscript/mod.rs index c20ed8938..f0c6d3898 100644 --- a/src/miniscript/mod.rs +++ b/src/miniscript/mod.rs @@ -644,12 +644,12 @@ mod tests { let pkk_ms: Miniscript = Miniscript { node: Terminal::Check(Arc::new(Miniscript { node: Terminal::PkK(DummyKey), - ty: Type::from_pk_k(), - ext: types::extra_props::ExtData::from_pk_k(), + ty: Type::from_pk_k::(), + ext: types::extra_props::ExtData::from_pk_k::(), phantom: PhantomData, })), - ty: Type::cast_check(Type::from_pk_k()).unwrap(), - ext: ExtData::cast_check(ExtData::from_pk_k()).unwrap(), + ty: Type::cast_check(Type::from_pk_k::()).unwrap(), + ext: ExtData::cast_check(ExtData::from_pk_k::()).unwrap(), phantom: PhantomData, }; string_rtt(pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(DummyKey)", "pk()"); @@ -657,12 +657,12 @@ mod tests { let pkh_ms: Miniscript = Miniscript { node: Terminal::Check(Arc::new(Miniscript { node: Terminal::PkH(DummyKeyHash), - ty: Type::from_pk_h(), - ext: types::extra_props::ExtData::from_pk_h(), + ty: Type::from_pk_h::(), + ext: types::extra_props::ExtData::from_pk_h::(), phantom: PhantomData, })), - ty: Type::cast_check(Type::from_pk_h()).unwrap(), - ext: ExtData::cast_check(ExtData::from_pk_h()).unwrap(), + ty: Type::cast_check(Type::from_pk_h::()).unwrap(), + ext: ExtData::cast_check(ExtData::from_pk_h::()).unwrap(), phantom: PhantomData, }; string_rtt(pkh_ms, "[B/nduesm]c:[K/nduesm]pk_h(DummyKeyHash)", "pkh()"); @@ -670,12 +670,12 @@ mod tests { let pkk_ms: Segwitv0Script = Miniscript { node: Terminal::Check(Arc::new(Miniscript { node: Terminal::PkK(pk), - ty: Type::from_pk_k(), - ext: types::extra_props::ExtData::from_pk_k(), + ty: Type::from_pk_k::(), + ext: types::extra_props::ExtData::from_pk_k::(), phantom: PhantomData, })), - ty: Type::cast_check(Type::from_pk_k()).unwrap(), - ext: ExtData::cast_check(ExtData::from_pk_k()).unwrap(), + ty: Type::cast_check(Type::from_pk_k::()).unwrap(), + ext: ExtData::cast_check(ExtData::from_pk_k::()).unwrap(), phantom: PhantomData, }; @@ -688,12 +688,12 @@ mod tests { let pkh_ms: Segwitv0Script = Miniscript { node: Terminal::Check(Arc::new(Miniscript { node: Terminal::PkH(hash), - ty: Type::from_pk_h(), - ext: types::extra_props::ExtData::from_pk_h(), + ty: Type::from_pk_h::(), + ext: types::extra_props::ExtData::from_pk_h::(), phantom: PhantomData, })), - ty: Type::cast_check(Type::from_pk_h()).unwrap(), - ext: ExtData::cast_check(ExtData::from_pk_h()).unwrap(), + ty: Type::cast_check(Type::from_pk_h::()).unwrap(), + ext: ExtData::cast_check(ExtData::from_pk_h::()).unwrap(), phantom: PhantomData, }; diff --git a/src/miniscript/types/correctness.rs b/src/miniscript/types/correctness.rs index 9164e42a7..d84b4eaa8 100644 --- a/src/miniscript/types/correctness.rs +++ b/src/miniscript/types/correctness.rs @@ -15,6 +15,7 @@ //! Correctness/Soundness type properties use super::{ErrorKind, Property}; +use ScriptContext; /// Basic type representing where the fragment can go #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)] @@ -146,7 +147,7 @@ impl Property for Correctness { } } - fn from_pk_k() -> Self { + fn from_pk_k() -> Self { Correctness { base: Base::K, input: Input::OneNonZero, @@ -155,7 +156,7 @@ impl Property for Correctness { } } - fn from_pk_h() -> Self { + fn from_pk_h() -> Self { Correctness { base: Base::K, input: Input::AnyNonZero, @@ -164,7 +165,7 @@ impl Property for Correctness { } } - fn from_multi(_: usize, _: usize) -> Self { + fn from_multi(_: usize, _: usize) -> Self { Correctness { base: Base::B, input: Input::AnyNonZero, diff --git a/src/miniscript/types/extra_props.rs b/src/miniscript/types/extra_props.rs index 735c429f3..1fab96539 100644 --- a/src/miniscript/types/extra_props.rs +++ b/src/miniscript/types/extra_props.rs @@ -177,7 +177,7 @@ impl Property for ExtData { } } - fn from_pk_k() -> Self { + fn from_pk_k() -> Self { ExtData { pk_cost: 34, has_free_verify: false, @@ -194,7 +194,7 @@ impl Property for ExtData { } } - fn from_pk_h() -> Self { + fn from_pk_h() -> Self { ExtData { pk_cost: 24, has_free_verify: false, @@ -211,7 +211,7 @@ impl Property for ExtData { } } - fn from_multi(k: usize, n: usize) -> Self { + fn from_multi(k: usize, n: usize) -> Self { let num_cost = match (k > 16, n > 16) { (true, true) => 4, (false, true) => 3, @@ -234,7 +234,7 @@ impl Property for ExtData { } } - fn from_multi_a(k: usize, n: usize) -> Self { + fn from_multi_a(k: usize, n: usize) -> Self { let num_cost = match (k > 16, n > 16) { (true, true) => 4, (false, true) => 3, @@ -1011,8 +1011,8 @@ impl Property for ExtData { let ret = match *fragment { Terminal::True => Ok(Self::from_true()), Terminal::False => Ok(Self::from_false()), - Terminal::PkK(..) => Ok(Self::from_pk_k()), - Terminal::PkH(..) => Ok(Self::from_pk_h()), + Terminal::PkK(..) => Ok(Self::from_pk_k::()), + Terminal::PkH(..) => Ok(Self::from_pk_h::()), Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => { if k == 0 { return Err(Error { @@ -1027,8 +1027,8 @@ impl Property for ExtData { }); } match *fragment { - Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())), - Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())), + Terminal::Multi(..) => Ok(Self::from_multi::(k, pks.len())), + Terminal::MultiA(..) => Ok(Self::from_multi_a::(k, pks.len())), _ => unreachable!(), } } diff --git a/src/miniscript/types/malleability.rs b/src/miniscript/types/malleability.rs index ebef4c114..afb0f6ad1 100644 --- a/src/miniscript/types/malleability.rs +++ b/src/miniscript/types/malleability.rs @@ -15,6 +15,7 @@ //! Malleability-related Type properties use super::{ErrorKind, Property}; +use ScriptContext; /// Whether the fragment has a dissatisfaction, and if so, whether /// it is unique. Affects both correctness and malleability-freeness, @@ -98,7 +99,7 @@ impl Property for Malleability { } } - fn from_pk_k() -> Self { + fn from_pk_k() -> Self { Malleability { dissat: Dissat::Unique, safe: true, @@ -106,7 +107,7 @@ impl Property for Malleability { } } - fn from_pk_h() -> Self { + fn from_pk_h() -> Self { Malleability { dissat: Dissat::Unique, safe: true, @@ -114,7 +115,7 @@ impl Property for Malleability { } } - fn from_multi(_: usize, _: usize) -> Self { + fn from_multi(_: usize, _: usize) -> Self { Malleability { dissat: Dissat::Unique, safe: true, diff --git a/src/miniscript/types/mod.rs b/src/miniscript/types/mod.rs index 33b246481..3633c2b03 100644 --- a/src/miniscript/types/mod.rs +++ b/src/miniscript/types/mod.rs @@ -261,18 +261,18 @@ pub trait Property: Sized { fn from_false() -> Self; /// Type property of the `PkK` fragment - fn from_pk_k() -> Self; + fn from_pk_k() -> Self; /// Type property of the `PkH` fragment - fn from_pk_h() -> Self; + fn from_pk_h() -> Self; /// Type property of a `Multi` fragment - fn from_multi(k: usize, n: usize) -> Self; + fn from_multi(k: usize, n: usize) -> Self; /// Type property of a `MultiA` fragment - fn from_multi_a(k: usize, n: usize) -> Self { + fn from_multi_a(k: usize, n: usize) -> Self { // default impl same as multi - Self::from_multi(k, n) + Self::from_multi::(k, n) } /// Type property of a hash fragment @@ -414,8 +414,8 @@ pub trait Property: Sized { let ret = match *fragment { Terminal::True => Ok(Self::from_true()), Terminal::False => Ok(Self::from_false()), - Terminal::PkK(..) => Ok(Self::from_pk_k()), - Terminal::PkH(..) => Ok(Self::from_pk_h()), + Terminal::PkK(..) => Ok(Self::from_pk_k::()), + Terminal::PkH(..) => Ok(Self::from_pk_h::()), Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => { if k == 0 { return Err(Error { @@ -430,8 +430,8 @@ pub trait Property: Sized { }); } match *fragment { - Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())), - Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())), + Terminal::Multi(..) => Ok(Self::from_multi::(k, pks.len())), + Terminal::MultiA(..) => Ok(Self::from_multi_a::(k, pks.len())), _ => unreachable!(), } } @@ -563,24 +563,24 @@ impl Property for Type { } } - fn from_pk_k() -> Self { + fn from_pk_k() -> Self { Type { - corr: Property::from_pk_k(), - mall: Property::from_pk_k(), + corr: Property::from_pk_k::(), + mall: Property::from_pk_k::(), } } - fn from_pk_h() -> Self { + fn from_pk_h() -> Self { Type { - corr: Property::from_pk_h(), - mall: Property::from_pk_h(), + corr: Property::from_pk_h::(), + mall: Property::from_pk_h::(), } } - fn from_multi(k: usize, n: usize) -> Self { + fn from_multi(k: usize, n: usize) -> Self { Type { - corr: Property::from_multi(k, n), - mall: Property::from_multi(k, n), + corr: Property::from_multi::(k, n), + mall: Property::from_multi::(k, n), } } @@ -797,8 +797,8 @@ impl Property for Type { let ret = match *fragment { Terminal::True => Ok(Self::from_true()), Terminal::False => Ok(Self::from_false()), - Terminal::PkK(..) => Ok(Self::from_pk_k()), - Terminal::PkH(..) => Ok(Self::from_pk_h()), + Terminal::PkK(..) => Ok(Self::from_pk_k::()), + Terminal::PkH(..) => Ok(Self::from_pk_h::()), Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => { if k == 0 { return Err(Error { @@ -813,8 +813,8 @@ impl Property for Type { }); } match *fragment { - Terminal::Multi(..) => Ok(Self::from_multi(k, pks.len())), - Terminal::MultiA(..) => Ok(Self::from_multi_a(k, pks.len())), + Terminal::Multi(..) => Ok(Self::from_multi::(k, pks.len())), + Terminal::MultiA(..) => Ok(Self::from_multi_a::(k, pks.len())), _ => unreachable!(), } } diff --git a/src/policy/compiler.rs b/src/policy/compiler.rs index 6cb4ed22d..494ccf67c 100644 --- a/src/policy/compiler.rs +++ b/src/policy/compiler.rs @@ -22,6 +22,7 @@ use std::convert::From; use std::marker::PhantomData; use std::{cmp, error, f64, fmt, mem}; +use miniscript::context::SigType; use miniscript::limits::MAX_PUBKEYS_PER_MULTISIG; use miniscript::types::{self, ErrorKind, ExtData, Property, Type}; use miniscript::ScriptContext; @@ -167,26 +168,41 @@ impl Property for CompilerExtData { } } - fn from_pk_k() -> Self { + fn from_pk_k() -> Self { CompilerExtData { branch_prob: None, - sat_cost: 73.0, + sat_cost: match Ctx::sig_type() { + SigType::Ecdsa => 73.0, + SigType::Schnorr => 1.0 /* */ + 64.0 /* sig */ + 1.0, /* */ + }, dissat_cost: Some(1.0), } } - fn from_pk_h() -> Self { + fn from_pk_h() -> Self { CompilerExtData { branch_prob: None, - sat_cost: 73.0 + 34.0, - dissat_cost: Some(1.0 + 34.0), + sat_cost: match Ctx::sig_type() { + SigType::Ecdsa => 73.0 + 34.0, + SigType::Schnorr => 66.0 + 32.0, + }, + dissat_cost: Some( + 1.0 + match Ctx::sig_type() { + SigType::Ecdsa => 34.0, + SigType::Schnorr => 32.0, + }, + ), } } - fn from_multi(k: usize, _n: usize) -> Self { + fn from_multi(k: usize, _n: usize) -> Self { CompilerExtData { branch_prob: None, - sat_cost: 1.0 + 73.0 * k as f64, + sat_cost: 1.0 + + match Ctx::sig_type() { + SigType::Ecdsa => 73.0 * k as f64, + SigType::Schnorr => unreachable!(), + }, dissat_cost: Some(1.0 * (k + 1) as f64), } } @@ -311,6 +327,14 @@ impl Property for CompilerExtData { }) } + fn and_n(a: Self, b: Self) -> Result { + Ok(CompilerExtData { + branch_prob: None, + sat_cost: a.sat_cost + b.sat_cost, + dissat_cost: a.dissat_cost, + }) + } + fn or_b(l: Self, r: Self) -> Result { let lprob = l .branch_prob @@ -403,14 +427,6 @@ impl Property for CompilerExtData { }) } - fn and_n(a: Self, b: Self) -> Result { - Ok(CompilerExtData { - branch_prob: None, - sat_cost: a.sat_cost + b.sat_cost, - dissat_cost: a.dissat_cost, - }) - } - fn threshold(k: usize, n: usize, mut sub_ck: S) -> Result where S: FnMut(usize) -> Result,