txscript: fix off-by-one error due to new OP_CODESEPARATOR behavior in segwit
txscript: fix off-by-one error due to new OP_CODESEPARATOR behavior in segwit

This commit fixes an off-by-one error which is only manifested by the
new behavior of OP_CODESEPARATOR within sig hashes triggered by the
segwit behavior. The current behavior within the Script VM
(txscript.Engine) known to be fully correct to the extent that it has
been verified. However, once segwit activates a consensus divergence
would emerge due exactly *when* the program counter was incremented in
the previous code (pre-this-commit).

Currently (pre-segwit) when calculating the pre-image to a transaction
sighash for signature verification, *all* instances of OP_CODESEPARATOR
are removed from the subScript being signed before generating the final
sighash. The addition of SegWIt has additional nerved the behavior of
OP_CODESEPARATOR by no longer removing them (and starting after the
last instance), but instead simply starting the subScript to be
directly *after* the last instance of an OP_CODESEPARATOR within the
pkScript.

Due to this new behavior, without this commit, an off-by-one error
(which only matters post-segwit), would case txscript to generate an
incorrect subScript since the instance of OP_CODESEPARATOR would remain
as part of the subScript instead of being sliced off as the new
behavior dictates. The off-by-one error itself is manifsted due to a
slight divergence in txscript.Engine’s logic compared to Bitcoin Core.
In Bitcoin Core script verification is as follows: first the next
op-code is fetched, then program counter is incremented, and finally
the op-code itself is executed. Before this commit, btcd flipped the
order of the last two steps, executing the op-code *before* the program
counter was incremented.

This commit fixes the post-segwit consensus divergence by incrementing
the program-counter *before* the next op-code is executed. It is
important to note that this divergence is only significant pre-segwit,
meaning that txscript.Engine is still consensus compliant independent
of this commit.

adiabat/btcd,adiabat/btcd