Skip to content

Commit 3dd006a

Browse files
committed
miniscript: remove Ctx::check_witness
This is a weird function. Its role is to double-check a witness after it has been produced to ensure that it falls within limits. If you are working with a sane miniscript this is impossible to fail, and if you're working with an insane miniscript then the checks are too strong -- it verifies standardness rules rather than consensus. This was introduced in #189 without much discussion. It had no tests then and has no tests now -- everything continues to pass. I am removing it for now. I will later replace it with an API where you call Plan::validate if you want to do checks like this. If the user wants to do something oddball like "parse a miniscript without checking satisfaction limits, then produce a satisfaction and see if the result is nonetheless within limits" the workflow will be: 1. Parse an insane Miniscript 2. Produce a Plan 3. Run Plan::validate with tighter validation params than were used in step 1. This gives the user a fair bit more flexibility. It is an awkward and hard to discover workflow, but IMO it's better than we have now.
1 parent 6c723ed commit 3dd006a

File tree

2 files changed

+1
-56
lines changed

2 files changed

+1
-56
lines changed

src/miniscript/context.rs

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::miniscript::limits::{
1515
};
1616
use crate::miniscript::types;
1717
use crate::prelude::*;
18-
use crate::util::witness_to_scriptsig;
1918
use crate::{hash256, Error, ForEachKey, Miniscript, MiniscriptKey, Terminal};
2019

2120
/// Error for Script Context
@@ -189,16 +188,6 @@ where
189188
_frag: &Terminal<Pk, Self>,
190189
) -> Result<(), ScriptContextError>;
191190

192-
/// Check whether the given satisfaction is valid under the ScriptContext
193-
/// For example, segwit satisfactions may fail if the witness len is more
194-
/// 3600 or number of stack elements are more than 100.
195-
fn check_witness(_witness: &[Vec<u8>]) -> Result<(), ScriptContextError> {
196-
// Only really need to do this for segwitv0 and legacy
197-
// Bare is already restricted by standardness rules
198-
// and would reach these limits.
199-
Ok(())
200-
}
201-
202191
/// Each context has slightly different rules on what Pks are allowed in descriptors
203192
/// Legacy/Bare does not allow x_only keys
204193
/// Segwit does not allow uncompressed keys and x_only keys
@@ -389,19 +378,6 @@ impl ScriptContext for Legacy {
389378
}
390379
}
391380

392-
fn check_witness(witness: &[Vec<u8>]) -> Result<(), ScriptContextError> {
393-
// In future, we could avoid by having a function to count only
394-
// len of script instead of converting it.
395-
let script_sig = witness_to_scriptsig(witness);
396-
if script_sig.len() > MAX_SCRIPTSIG_SIZE {
397-
return Err(ScriptContextError::MaxScriptSigSizeExceeded {
398-
actual: script_sig.len(),
399-
limit: MAX_SCRIPTSIG_SIZE,
400-
});
401-
}
402-
Ok(())
403-
}
404-
405381
fn check_global_consensus_validity<Pk: MiniscriptKey>(
406382
ms: &Miniscript<Pk, Self>,
407383
) -> Result<(), ScriptContextError> {
@@ -506,16 +482,6 @@ impl ScriptContext for Segwitv0 {
506482
}
507483
}
508484

509-
fn check_witness(witness: &[Vec<u8>]) -> Result<(), ScriptContextError> {
510-
if witness.len() > MAX_STANDARD_P2WSH_STACK_ITEMS {
511-
return Err(ScriptContextError::MaxWitnessItemsExceeded {
512-
actual: witness.len(),
513-
limit: MAX_STANDARD_P2WSH_STACK_ITEMS,
514-
});
515-
}
516-
Ok(())
517-
}
518-
519485
fn check_global_consensus_validity<Pk: MiniscriptKey>(
520486
ms: &Miniscript<Pk, Self>,
521487
) -> Result<(), ScriptContextError> {
@@ -627,17 +593,6 @@ impl ScriptContext for Tap {
627593
}
628594
}
629595

630-
fn check_witness(witness: &[Vec<u8>]) -> Result<(), ScriptContextError> {
631-
// Note that tapscript has a 1000 limit compared to 100 of segwitv0
632-
if witness.len() > MAX_STACK_SIZE {
633-
return Err(ScriptContextError::MaxWitnessItemsExceeded {
634-
actual: witness.len(),
635-
limit: MAX_STACK_SIZE,
636-
});
637-
}
638-
Ok(())
639-
}
640-
641596
fn check_global_consensus_validity<Pk: MiniscriptKey>(
642597
ms: &Miniscript<Pk, Self>,
643598
) -> Result<(), ScriptContextError> {
@@ -882,13 +837,6 @@ impl ScriptContext for NoChecks {
882837
"NochecksEcdsa"
883838
}
884839

885-
fn check_witness(_witness: &[Vec<u8>]) -> Result<(), ScriptContextError> {
886-
// Only really need to do this for segwitv0 and legacy
887-
// Bare is already restricted by standardness rules
888-
// and would reach these limits.
889-
Ok(())
890-
}
891-
892840
fn check_global_validity<Pk: MiniscriptKey>(
893841
ms: &Miniscript<Pk, Self>,
894842
) -> Result<(), ScriptContextError> {

src/miniscript/mod.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
471471
Pk: ToPublicKey,
472472
{
473473
match satisfaction.stack {
474-
satisfy::Witness::Stack(stack) => {
475-
Ctx::check_witness(&stack)?;
476-
Ok(stack)
477-
}
474+
satisfy::Witness::Stack(stack) => Ok(stack),
478475
satisfy::Witness::Unavailable | satisfy::Witness::Impossible => {
479476
Err(Error::CouldNotSatisfy)
480477
}

0 commit comments

Comments
 (0)