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 18, 2022
1 parent 05a71e2 commit 25560a3
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 54 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
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 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 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 @@ -6,6 +6,7 @@ use miniscript::limits::{
};

use super::{Error, ErrorKind, Property, ScriptContext};
use miniscript::context::SigType;
use script_num_size;
use std::cmp;
use std::iter::once;
Expand Down Expand Up @@ -177,24 +178,30 @@ 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_count_static: 0,
ops_count_sat: Some(0),
ops_count_nsat: 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,
Expand All @@ -203,8 +210,14 @@ impl Property for ExtData {
ops_count_nsat: Some(3),
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 @@ -242,7 +255,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,
ops_count_static: 1, // We don't care about opcounts in tapscript
ops_count_sat: Some(n + 1),
Expand Down Expand Up @@ -1011,8 +1024,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 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,15 +99,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 @@ -261,10 +261,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 @@ -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 Down Expand Up @@ -563,17 +563,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 @@ -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 Down
46 changes: 33 additions & 13 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,19 +168,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 @@ -191,6 +203,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 @@ -311,6 +331,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 +431,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 25560a3

Please sign in to comment.