@@ -3278,15 +3278,13 @@ static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion,
32783278 // BIP341 Taproot: 32-byte non-P2SH witness v1 program (which encodes a P2C-tweaked pubkey)
32793279 if (!(flags & SCRIPT_VERIFY_TAPROOT)) return set_success (serror);
32803280 if (stack.size () == 0 ) return set_error (serror, SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY);
3281- valtype padding ;
3281+ valtype annex ;
32823282 if (stack.size () >= 2 && !stack.back ().empty () && stack.back ()[0 ] == ANNEX_TAG) {
32833283 // Drop annex (this is non-standard; see IsWitnessStandard)
3284- const valtype& annex = SpanPopBack (stack);
3284+ // ELEMENTS: store the annex for CheckSimplicity
3285+ annex = SpanPopBack (stack);
32853286 execdata.m_annex_hash = (CHashWriter (SER_GETHASH, 0 ) << annex).GetSHA256 ();
32863287 execdata.m_annex_present = true ;
3287- if (!stack.back ().empty () && (stack.back ()[0 ] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSIMPLICITY) {
3288- padding = annex;
3289- }
32903288 } else {
32913289 execdata.m_annex_present = false ;
32923290 }
@@ -3327,20 +3325,20 @@ static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion,
33273325 simplicityRawTap.pathLen = (control.size () - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE;
33283326 simplicityRawTap.scriptCMR = script_bytes.data ();
33293327 int64_t minCost = 0 ;
3330- if ((flags & SCRIPT_VERIFY_ANNEX_PADDING) && padding .size () > 0 ) {
3331- valtype zero_padding (padding .size (), 0 );
3328+ if ((flags & SCRIPT_VERIFY_ANNEX_PADDING) && annex .size () > 0 ) {
3329+ valtype zero_padding (annex .size (), 0 );
33323330 zero_padding[0 ] = ANNEX_TAG;
3333- if (padding != zero_padding) {
3331+ if (annex != zero_padding) {
33343332 return set_error (serror, SCRIPT_ERR_SIMPLICITY_PADDING_NONZERO);
33353333 }
33363334 // Compute what the budget would have been without the padding.
33373335 // budget includes the padding cost, so subtracting this stack item won't underflow.
3338- minCost = budget - ::GetSerializeSize (padding );
3339- if (! zero_padding.empty () ) {
3336+ minCost = budget - ::GetSerializeSize (annex );
3337+ if (zero_padding.size () > 1 ) {
33403338 // Set the minCost to what the budget would have been if the padding were one byte smaller.
33413339 zero_padding.pop_back ();
3342- minCost += ::GetSerializeSize (zero_padding);
33433340 }
3341+ minCost += ::GetSerializeSize (zero_padding);
33443342 }
33453343 return checker.CheckSimplicity (simplicity_program, simplicity_witness, simplicityRawTap, minCost, budget, serror);
33463344 }
0 commit comments