Skip to content

SigType check for cost-analysis in SegwitV1 scripts #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,38 +654,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 @@ -698,12 +698,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
5 changes: 3 additions & 2 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 crate::ScriptContext;

/// Basic type representing where the fragment can go
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
Expand Down Expand Up @@ -142,7 +143,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 @@ -151,7 +152,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 Down
31 changes: 22 additions & 9 deletions src/miniscript/types/extra_props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::cmp;
use std::iter::once;

use super::{Error, ErrorKind, Property, ScriptContext};
use crate::miniscript::context::SigType;
use crate::miniscript::limits::{
HEIGHT_TIME_THRESHOLD, SEQUENCE_LOCKTIME_DISABLE_FLAG, SEQUENCE_LOCKTIME_TYPE_FLAG,
};
Expand Down Expand Up @@ -183,30 +184,42 @@ impl Property for ExtData {
}
}

fn from_pk_k() -> Self {
fn from_pk_k<Ctx: ScriptContext>() -> Self {
ExtData {
pk_cost: 34,
pk_cost: match Ctx::sig_type() {
SigType::Ecdsa => 34,
SigType::Schnorr => 33,
},
has_free_verify: false,
ops: OpLimits::new(0, Some(0), Some(0)),
stack_elem_count_sat: Some(1),
stack_elem_count_dissat: Some(1),
max_sat_size: Some((73, 73)),
max_sat_size: match Ctx::sig_type() {
SigType::Ecdsa => Some((73, 73)),
SigType::Schnorr => Some((66, 66)),
},
max_dissat_size: Some((1, 1)),
timelock_info: TimeLockInfo::default(),
exec_stack_elem_count_sat: Some(1), // pushes the pk
exec_stack_elem_count_dissat: Some(1),
}
}

fn from_pk_h() -> Self {
fn from_pk_h<Ctx: ScriptContext>() -> Self {
ExtData {
pk_cost: 24,
has_free_verify: false,
ops: OpLimits::new(3, Some(0), Some(0)),
stack_elem_count_sat: Some(2),
stack_elem_count_dissat: Some(2),
max_sat_size: Some((34 + 73, 34 + 73)),
max_dissat_size: Some((35, 35)),
max_sat_size: match Ctx::sig_type() {
SigType::Ecdsa => Some((34 + 73, 34 + 73)),
SigType::Schnorr => Some((66 + 33, 33 + 66)),
},
max_dissat_size: match Ctx::sig_type() {
SigType::Ecdsa => Some((35, 35)),
SigType::Schnorr => Some((34, 34)),
},
timelock_info: TimeLockInfo::default(),
exec_stack_elem_count_sat: Some(2), // dup and hash push
exec_stack_elem_count_dissat: Some(2),
Expand Down Expand Up @@ -244,7 +257,7 @@ impl Property for ExtData {
(false, false) => 2,
};
ExtData {
pk_cost: num_cost + 33 * n /*pks*/ + (n-1) /*checksigadds*/ + 1,
pk_cost: num_cost + 33 * n /*pks*/ + (n - 1) /*checksigadds*/ + 1,
has_free_verify: true,
// These numbers are irrelevant here are there is no op limit in tapscript
ops: OpLimits::new(n, Some(0), Some(0)),
Expand Down Expand Up @@ -903,8 +916,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 Down
5 changes: 3 additions & 2 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 crate::ScriptContext;

/// Whether the fragment has a dissatisfaction, and if so, whether
/// it is unique. Affects both correctness and malleability-freeness,
Expand Down Expand Up @@ -94,15 +95,15 @@ 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,
Expand Down
24 changes: 12 additions & 12 deletions src/miniscript/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ 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;
Expand Down Expand Up @@ -413,8 +413,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 Down Expand Up @@ -562,17 +562,17 @@ 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>(),
}
}

Expand Down Expand Up @@ -796,8 +796,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 Down
34 changes: 27 additions & 7 deletions src/policy/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::marker::PhantomData;
use std::sync::Arc;
use std::{cmp, error, f64, fmt, hash, mem};

use crate::miniscript::context::SigType;
use crate::miniscript::limits::MAX_PUBKEYS_PER_MULTISIG;
use crate::miniscript::types::{self, ErrorKind, ExtData, Property, Type};
use crate::miniscript::ScriptContext;
Expand Down Expand Up @@ -165,19 +166,30 @@ 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 + 33.0,
},
dissat_cost: Some(
1.0 + match Ctx::sig_type() {
SigType::Ecdsa => 34.0,
SigType::Schnorr => 33.0,
},
),
}
}

Expand All @@ -189,6 +201,14 @@ impl Property for CompilerExtData {
}
}

fn from_multi_a(k: usize, n: usize) -> Self {
CompilerExtData {
branch_prob: None,
sat_cost: 66.0 * k as f64 + (n - k) as f64,
dissat_cost: Some(n as f64), /* <w_n> ... <w_1> := 0x00 ... 0x00 (n times) */
}
}

fn from_hash() -> Self {
CompilerExtData {
branch_prob: None,
Expand Down Expand Up @@ -1258,7 +1278,7 @@ mod tests {
let compilation: DummyTapAstElemExt =
best_t(&mut BTreeMap::new(), &policy, 1.0, None).unwrap();

assert_eq!(compilation.cost_1d(1.0, None), 88.0 + 74.109375);
assert_eq!(compilation.cost_1d(1.0, None), 87.0 + 67.0390625);
assert_eq!(
policy.lift().unwrap().sorted(),
compilation.ms.lift().unwrap().sorted()
Expand All @@ -1271,7 +1291,7 @@ mod tests {
let compilation: DummyTapAstElemExt =
best_t(&mut BTreeMap::new(), &policy, 1.0, None).unwrap();

assert_eq!(compilation.cost_1d(1.0, None), 438.0 + 299.4003295898438);
assert_eq!(compilation.cost_1d(1.0, None), 433.0 + 275.7909749348958);
assert_eq!(
policy.lift().unwrap().sorted(),
compilation.ms.lift().unwrap().sorted()
Expand Down