diff --git a/src/lib.rs b/src/lib.rs index 20b0e4331..fde7ac66b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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 @@ -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), @@ -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), @@ -560,8 +554,6 @@ impl std::error::Error for Error { InvalidOpcode(_) | NonMinimalVerify(_) | InvalidPush(_) - | CmsTooManyKeys(_) - | MultiATooManyKeys(_) | UnexpectedStart | Unexpected(_) | MultiColon(_) diff --git a/src/miniscript/decode.rs b/src/miniscript/decode.rs index 0090b7516..17e81112a 100644 --- a/src/miniscript/decode.rs +++ b/src/miniscript/decode.rs @@ -538,10 +538,10 @@ pub fn parse( }, // 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)); - } + // Check size before allocating keys. Note that constructing a + // vector of ()s, no matter the size, takes no allocations. + Threshold::<(), MAX_PUBKEYS_PER_MULTISIG>::new(1, vec![(); n as usize]).map_err(Error::Threshold)?; + let mut keys = Vec::with_capacity(n as usize); for _ in 0..n { match_token!( @@ -562,10 +562,10 @@ pub fn parse( }, // 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)) - } + // Check size before allocating keys. Note that constructing a + // vector of ()s, no matter the size, takes no allocations. + Threshold::<(), MAX_PUBKEYS_IN_CHECKSIGADD>::new(k as usize, vec![(); k as usize]).map_err(Error::Threshold)?; + let mut keys = Vec::with_capacity(k as usize); // atleast k capacity while tokens.peek() == Some(&Tk::CheckSigAdd) { match_token!(