Skip to content

Commit

Permalink
error: remove two more variants which were redundant with threshold e…
Browse files Browse the repository at this point in the history
…rrors
  • Loading branch information
apoelstra committed Nov 23, 2024
1 parent cdac32e commit 75f400f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,6 @@ pub enum Error {
AddrError(bitcoin::address::ParseError),
/// rust-bitcoin p2sh address error
AddrP2shError(bitcoin::address::P2shError),
/// A `CHECKMULTISIG` opcode was preceded by a number > 20
CmsTooManyKeys(u32),
/// A tapscript multi_a cannot support more than Weight::MAX_BLOCK/32 keys
MultiATooManyKeys(u64),
/// While parsing backward, hit beginning of script
UnexpectedStart,
/// Got something we were not expecting
Expand Down Expand Up @@ -504,7 +500,6 @@ impl fmt::Display for Error {
Error::Script(ref e) => fmt::Display::fmt(e, f),
Error::AddrError(ref e) => fmt::Display::fmt(e, f),
Error::AddrP2shError(ref e) => fmt::Display::fmt(e, f),
Error::CmsTooManyKeys(n) => write!(f, "checkmultisig with {} keys", n),
Error::UnexpectedStart => f.write_str("unexpected start of script"),
Error::Unexpected(ref s) => write!(f, "unexpected «{}»", s),
Error::MultiColon(ref s) => write!(f, "«{}» has multiple instances of «:»", s),
Expand Down Expand Up @@ -539,7 +534,6 @@ impl fmt::Display for Error {
Error::PubKeyCtxError(ref pk, ref ctx) => {
write!(f, "Pubkey error: {} under {} scriptcontext", pk, ctx)
}
Error::MultiATooManyKeys(k) => write!(f, "MultiA too many keys {}", k),
Error::TrNoScriptCode => write!(f, "No script code for Tr descriptors"),
Error::MultipathDescLenMismatch => write!(f, "At least two BIP389 key expressions in the descriptor contain tuples of derivation indexes of different lengths"),
Error::AbsoluteLockTime(ref e) => e.fmt(f),
Expand All @@ -560,8 +554,6 @@ impl std::error::Error for Error {
InvalidOpcode(_)
| NonMinimalVerify(_)
| InvalidPush(_)
| CmsTooManyKeys(_)
| MultiATooManyKeys(_)
| UnexpectedStart
| Unexpected(_)
| MultiColon(_)
Expand Down
15 changes: 6 additions & 9 deletions src/miniscript/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::miniscript::lex::{Token as Tk, TokenIter};
use crate::miniscript::limits::{MAX_PUBKEYS_IN_CHECKSIGADD, MAX_PUBKEYS_PER_MULTISIG};
use crate::miniscript::ScriptContext;
use crate::prelude::*;
use crate::primitives::threshold;
#[cfg(doc)]
use crate::Descriptor;
use crate::{
Expand Down Expand Up @@ -538,10 +539,8 @@ pub fn parse<Ctx: ScriptContext>(
},
// CHECKMULTISIG based multisig
Tk::CheckMultiSig, Tk::Num(n) => {
// Check size before allocating keys
if n as usize > MAX_PUBKEYS_PER_MULTISIG {
return Err(Error::CmsTooManyKeys(n));
}
threshold::validate_k_n::<MAX_PUBKEYS_PER_MULTISIG>(1, n as usize).map_err(Error::Threshold)?;

let mut keys = Vec::with_capacity(n as usize);
for _ in 0..n {
match_token!(
Expand All @@ -562,11 +561,9 @@ pub fn parse<Ctx: ScriptContext>(
},
// MultiA
Tk::NumEqual, Tk::Num(k) => {
// Check size before allocating keys
if k as usize > MAX_PUBKEYS_IN_CHECKSIGADD {
return Err(Error::MultiATooManyKeys(MAX_PUBKEYS_IN_CHECKSIGADD as u64))
}
let mut keys = Vec::with_capacity(k as usize); // atleast k capacity
threshold::validate_k_n::<MAX_PUBKEYS_IN_CHECKSIGADD>(k as usize, k as usize).map_err(Error::Threshold)?;

let mut keys = Vec::with_capacity(k as usize); // at least k capacity
while tokens.peek() == Some(&Tk::CheckSigAdd) {
match_token!(
tokens,
Expand Down
16 changes: 11 additions & 5 deletions src/primitives/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@ impl std::error::Error for ThresholdError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

/// Check whether `k` and `n` are valid for an instance of [`Self`].
pub fn validate_k_n<const MAX: usize>(k: usize, n: usize) -> Result<(), ThresholdError> {
if k == 0 || k > n || (MAX > 0 && n > MAX) {
Err(ThresholdError { k, n, max: (MAX > 0).then_some(MAX) })
} else {
Ok(())
}
}

/// Structure representing a k-of-n threshold collection of some arbitrary
/// object `T`.
///
Expand All @@ -54,11 +63,8 @@ pub struct Threshold<T, const MAX: usize> {
impl<T, const MAX: usize> Threshold<T, MAX> {
/// Constructs a threshold directly from a threshold value and collection.
pub fn new(k: usize, inner: Vec<T>) -> Result<Self, ThresholdError> {
if k == 0 || k > inner.len() || (MAX > 0 && inner.len() > MAX) {
Err(ThresholdError { k, n: inner.len(), max: (MAX > 0).then_some(MAX) })
} else {
Ok(Threshold { k, inner })
}
validate_k_n::<MAX>(k, inner.len())?;
Ok(Threshold { k, inner })
}

/// Constructs a threshold from a threshold value and an iterator that yields collection
Expand Down

0 comments on commit 75f400f

Please sign in to comment.