Skip to content

Commit

Permalink
Add SigType check in CompilerExtData for deciding key byte-lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
SarcasticNastik committed Apr 10, 2022
1 parent 05a71e2 commit 2565f5e
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 67 deletions.
32 changes: 16 additions & 16 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,38 +644,38 @@ mod tests {
let pkk_ms: Miniscript<DummyKey, Segwitv0> = 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::<Segwitv0>(),
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
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::<Segwitv0>()).unwrap(),
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
phantom: PhantomData,
};
string_rtt(pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(DummyKey)", "pk()");

let pkh_ms: Miniscript<DummyKey, Segwitv0> = 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::<Segwitv0>(),
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
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::<Segwitv0>()).unwrap(),
ext: ExtData::cast_check(ExtData::from_pk_h::<Segwitv0>()).unwrap(),
phantom: PhantomData,
};
string_rtt(pkh_ms, "[B/nduesm]c:[K/nduesm]pk_h(DummyKeyHash)", "pkh()");

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::<Segwitv0>(),
ext: types::extra_props::ExtData::from_pk_k::<Segwitv0>(),
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::<Segwitv0>()).unwrap(),
ext: ExtData::cast_check(ExtData::from_pk_k::<Segwitv0>()).unwrap(),
phantom: PhantomData,
};

Expand All @@ -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::<Segwitv0>(),
ext: types::extra_props::ExtData::from_pk_h::<Segwitv0>(),
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::<Segwitv0>()).unwrap(),
ext: ExtData::cast_check(ExtData::from_pk_h::<Segwitv0>()).unwrap(),
phantom: PhantomData,
};

Expand Down
7 changes: 4 additions & 3 deletions src/miniscript/types/correctness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -146,7 +147,7 @@ impl Property for Correctness {
}
}

fn from_pk_k() -> Self {
fn from_pk_k<Ctx: ScriptContext>() -> Self {
Correctness {
base: Base::K,
input: Input::OneNonZero,
Expand All @@ -155,7 +156,7 @@ impl Property for Correctness {
}
}

fn from_pk_h() -> Self {
fn from_pk_h<Ctx: ScriptContext>() -> Self {
Correctness {
base: Base::K,
input: Input::AnyNonZero,
Expand All @@ -164,7 +165,7 @@ impl Property for Correctness {
}
}

fn from_multi(_: usize, _: usize) -> Self {
fn from_multi<Ctx: ScriptContext>(_: usize, _: usize) -> Self {
Correctness {
base: Base::B,
input: Input::AnyNonZero,
Expand Down
16 changes: 8 additions & 8 deletions src/miniscript/types/extra_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Property for ExtData {
}
}

fn from_pk_k() -> Self {
fn from_pk_k<Ctx: ScriptContext>() -> Self {
ExtData {
pk_cost: 34,
has_free_verify: false,
Expand All @@ -194,7 +194,7 @@ impl Property for ExtData {
}
}

fn from_pk_h() -> Self {
fn from_pk_h<Ctx: ScriptContext>() -> Self {
ExtData {
pk_cost: 24,
has_free_verify: false,
Expand All @@ -211,7 +211,7 @@ impl Property for ExtData {
}
}

fn from_multi(k: usize, n: usize) -> Self {
fn from_multi<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
let num_cost = match (k > 16, n > 16) {
(true, true) => 4,
(false, true) => 3,
Expand All @@ -234,7 +234,7 @@ impl Property for ExtData {
}
}

fn from_multi_a(k: usize, n: usize) -> Self {
fn from_multi_a<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
let num_cost = match (k > 16, n > 16) {
(true, true) => 4,
(false, true) => 3,
Expand Down Expand Up @@ -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::<Ctx>()),
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
if k == 0 {
return Err(Error {
Expand All @@ -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::<Ctx>(k, pks.len())),
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
_ => unreachable!(),
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/miniscript/types/malleability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -98,23 +99,23 @@ impl Property for Malleability {
}
}

fn from_pk_k() -> Self {
fn from_pk_k<Ctx: ScriptContext>() -> Self {
Malleability {
dissat: Dissat::Unique,
safe: true,
non_malleable: true,
}
}

fn from_pk_h() -> Self {
fn from_pk_h<Ctx: ScriptContext>() -> Self {
Malleability {
dissat: Dissat::Unique,
safe: true,
non_malleable: true,
}
}

fn from_multi(_: usize, _: usize) -> Self {
fn from_multi<Ctx: ScriptContext>(_: usize, _: usize) -> Self {
Malleability {
dissat: Dissat::Unique,
safe: true,
Expand Down
44 changes: 22 additions & 22 deletions src/miniscript/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ctx: ScriptContext>() -> Self;

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

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

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

/// Type property of a hash fragment
Expand Down Expand Up @@ -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::<Ctx>()),
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
if k == 0 {
return Err(Error {
Expand All @@ -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::<Ctx>(k, pks.len())),
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
_ => unreachable!(),
}
}
Expand Down Expand Up @@ -563,24 +563,24 @@ impl Property for Type {
}
}

fn from_pk_k() -> Self {
fn from_pk_k<Ctx: ScriptContext>() -> Self {
Type {
corr: Property::from_pk_k(),
mall: Property::from_pk_k(),
corr: Property::from_pk_k::<Ctx>(),
mall: Property::from_pk_k::<Ctx>(),
}
}

fn from_pk_h() -> Self {
fn from_pk_h<Ctx: ScriptContext>() -> Self {
Type {
corr: Property::from_pk_h(),
mall: Property::from_pk_h(),
corr: Property::from_pk_h::<Ctx>(),
mall: Property::from_pk_h::<Ctx>(),
}
}

fn from_multi(k: usize, n: usize) -> Self {
fn from_multi<Ctx: ScriptContext>(k: usize, n: usize) -> Self {
Type {
corr: Property::from_multi(k, n),
mall: Property::from_multi(k, n),
corr: Property::from_multi::<Ctx>(k, n),
mall: Property::from_multi::<Ctx>(k, n),
}
}

Expand Down Expand Up @@ -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::<Ctx>()),
Terminal::PkH(..) => Ok(Self::from_pk_h::<Ctx>()),
Terminal::Multi(k, ref pks) | Terminal::MultiA(k, ref pks) => {
if k == 0 {
return Err(Error {
Expand All @@ -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::<Ctx>(k, pks.len())),
Terminal::MultiA(..) => Ok(Self::from_multi_a::<Ctx>(k, pks.len())),
_ => unreachable!(),
}
}
Expand Down
46 changes: 31 additions & 15 deletions src/policy/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -167,26 +168,41 @@ impl Property for CompilerExtData {
}
}

fn from_pk_k() -> Self {
fn from_pk_k<Ctx: ScriptContext>() -> Self {
CompilerExtData {
branch_prob: None,
sat_cost: 73.0,
sat_cost: match Ctx::sig_type() {
SigType::Ecdsa => 73.0,
SigType::Schnorr => 1.0 /* <var_int> */ + 64.0 /* sig */ + 1.0, /* <sighash_type> */
},
dissat_cost: Some(1.0),
}
}

fn from_pk_h() -> Self {
fn from_pk_h<Ctx: ScriptContext>() -> 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<Ctx: ScriptContext>(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),
}
}
Expand Down Expand Up @@ -311,6 +327,14 @@ impl Property for CompilerExtData {
})
}

fn and_n(a: Self, b: Self) -> Result<Self, types::ErrorKind> {
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<Self, types::ErrorKind> {
let lprob = l
.branch_prob
Expand Down Expand Up @@ -403,14 +427,6 @@ impl Property for CompilerExtData {
})
}

fn and_n(a: Self, b: Self) -> Result<Self, types::ErrorKind> {
Ok(CompilerExtData {
branch_prob: None,
sat_cost: a.sat_cost + b.sat_cost,
dissat_cost: a.dissat_cost,
})
}

fn threshold<S>(k: usize, n: usize, mut sub_ck: S) -> Result<Self, types::ErrorKind>
where
S: FnMut(usize) -> Result<Self, types::ErrorKind>,
Expand Down

0 comments on commit 2565f5e

Please sign in to comment.