Commit graph

309 commits

Author SHA1 Message Date
yyforyongyu
a5a57b901f
x - demo consensus bug 2026-04-27 08:59:07 +08:00
yyforyongyu
de90fba3dc
txscript: refactor script handling 2026-04-27 08:59:07 +08:00
yyforyongyu
0d31f67963
txscript: align unknown witness version handling with Core
The refreshed Bitcoin Core tx_valid reference tests expect newer
witness versions to short-circuit successfully when
DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM is unset.
2026-04-27 08:45:50 +08:00
yyforyongyu
0fc16396bf
txscript: align empty CHECKSIG pubkey validation with Core
The refreshed Bitcoin Core script reference tests cover cases where an
empty CHECKSIG input must still fail pubkey encoding checks before the
interpreter falls back to a generic false result.
2026-04-27 08:45:50 +08:00
yyforyongyu
818c841ce0
txscript: return ErrCleanStack for witness cleanstack failures
The refreshed Bitcoin Core script reference tests expect witness
clean-stack failures to report CLEANSTACK rather than a generic
EVAL_FALSE result.
2026-04-27 08:45:50 +08:00
yyforyongyu
f837d37265
txscript: update tx_invalid.json from Bitcoin Core
Updated from Bitcoin Core master
(e32bc7f817e7adeffef22a985bf8dbd4aa3e08d8), file
src/test/data/tx_invalid.json.
2026-04-27 08:45:50 +08:00
yyforyongyu
e20edd39ae
txscript: update tx_valid.json from Bitcoin Core
Updated from Bitcoin Core master
(e32bc7f817e7adeffef22a985bf8dbd4aa3e08d8), file
src/test/data/tx_valid.json.
2026-04-27 08:45:50 +08:00
yyforyongyu
a49fdf17a4
txscript: update script_tests.json from Bitcoin Core
Updated from Bitcoin Core master
(e32bc7f817e7adeffef22a985bf8dbd4aa3e08d8), file
src/test/data/script_tests.json.
2026-04-27 08:45:50 +08:00
yyforyongyu
b1aa5a9121
txscript: support latest Core tx_invalid semantics
Handle BADTX sanity cases while keeping tx_invalid on Bitcoin Core's
included-flags convention.
2026-04-27 08:45:50 +08:00
yyforyongyu
897d5e33b6
txscript: support latest Core tx_valid semantics
Bitcoin Core's tx_valid vectors now specify flags to exclude from the
full script-flag set. Apply that parser directly in TestTxValidTests.
2026-04-27 08:45:49 +08:00
yyforyongyu
f4e1c1b253
txscript: support latest Core script_tests format
Add support for newly named Bitcoin Core script-test result tokens and
skip taproot placeholder rows that are already covered by
TestTaprootReferenceTests.
2026-04-27 08:45:49 +08:00
Erick Cestari
d36b9ded6d
txscript: switch template engine from html/template to text/template
html/template automatically HTML-escapes special characters to prevent
XSS, which can silently corrupt arbitrary script data. Since the output
is binary script bytes and not HTML, this escaping provides no benefit
and might breaks correctness.
2026-03-10 10:38:49 -03:00
Olaoluwa Osuntokun
c9bd7b4c0f
txscript: add new ScriptTemplate DSL for writing Scripts
In this commit, we add a new function, `ScriptTemplate` to make the
process of making custom Bitcoin scripts a bit less verbose.

ScriptTemplate processes a script template with parameters and returns the
corresponding script bytes. This functions allows Bitcoin scripts to be
created using a DSL-like syntax, based on Go's templating system.

An example of a simple p2pkh template would be:

   `OP_DUP OP_HASH160 0x14e8948c7afa71b6e6fad621256474b5959e0305 OP_EQUALVERIFY OP_CHECKSIG`

Strings that have the `0x` prefix are assumed to byte strings to be pushed
ontop of the stack. Integers can be passed as normal. If a value can't be
parsed as an integer, then it's assume that it's a byte slice without the 0x
prefix.

Normal go template operations can be used as well. The params argument
houses paramters to pass into the script, for example a local variable
storing a computed public key.
2025-07-11 12:20:05 -07:00
Olaoluwa Osuntokun
e5d2ae489b
txscript: add more detail to invalid tapscript merkle proof error
In this commit, we add more detail to the invalid tapscript merkle proof
error. Before this commit, the error was blank, making such a case hard
to debug. We'll now log the expected witness program, what we derived,
and also the passed in tapscript root.
2025-06-18 18:11:24 -07:00
xinhangzhou
06cf379174 refactor: use maps.Copy for cleaner map handling
Signed-off-by: xinhangzhou <shuangcui@aliyun.com>
2025-03-25 01:53:15 +08:00
Afanti
433781aaff
multi: fix typos 2024-10-08 14:47:41 +02:00
Olaoluwa Osuntokun
c4ed92fb52
Merge pull request #2178 from ProofOfKeags/standardness-cleanup
Update standardness rules congruent to Bitcoin Core
2024-05-21 15:44:24 -07:00
Keagan McClelland
04469e600e txscript: make OP_CODESEPARATOR non-standard in non-segwit scripts 2024-05-21 12:30:16 -07:00
oftenoccur
126b0ecff1
chore: fix some typos in comments (#2164)
Signed-off-by: oftenoccur <ezc5@sina.com>
2024-04-26 08:08:05 -04:00
zhiqiangxu
2f4ff8447d
fix some typos and make OP_DATA_20 explicit in comment (#2080) 2024-04-10 10:08:30 -04:00
mattn
3cb9f602e8
fix typos (#2100) 2024-03-25 09:44:25 -04:00
theedtron
b66f5b8379
multi: fix ioutil deprecated function
update i/o functions to use os / io package functions instead
2024-03-08 17:41:41 -08:00
Thabokani
a4df044cfd
Fix some typos (#2085)
* Dockerfile: fix typo

* blockchain: fix typos

* sample-btcd.conf: fix typos

* server: fix typos

* txscript: fix typos
2024-01-03 16:36:49 -05:00
Olaoluwa Osuntokun
19008edd0f
txscript: use a single shared scratch buffer in segwit sighash calc
We used to use a lot of small buffers for serialization, but now we'll
use one buffer large enough, and slice into it when needed.

``
name                  old time/op    new time/op    delta
CalcWitnessSigHash-8    31.5µs ± 0%    29.2µs ± 0%   -7.05%  (p=0.000 n=10+10)

name                  old alloc/op   new alloc/op   delta
CalcWitnessSigHash-8    19.9kB ± 0%    18.5kB ± 0%   -7.14%  (p=0.000 n=10+10)

name                  old allocs/op  new allocs/op  delta
CalcWitnessSigHash-8       801 ± 0%       445 ± 0%  -44.44%  (p=0.000 n=10+10)
```
2023-12-19 15:02:05 -08:00
Olaoluwa Osuntokun
046a70121a
txscript: use DoubleHashRaw for segwit sighash single calc
We can write direly into the hash writer vs serializing into a buffer,
then writing that into the hash writer.
2023-12-19 15:02:00 -08:00
Olaoluwa Osuntokun
adfb641a36
txscript: use DoubleHashRaw to write directly crypto.Hash for segwit sighash
In this commit, we optimize the sighash calc further by writing directly
into the buffer used for serialization by the sha256.New() instance
rather than to an intermediate buffer, which is then write to the hash
buffer.
2023-12-19 15:01:58 -08:00
Olaoluwa Osuntokun
c3c3545f9b
multi: update main package to chainhash/v1.1.0, use optimized dsha256
In this commit, we update the top-level btcd package to use the latest
version of btcutil and also the chainhash package. With this version
bump, we can now use the new optimized dsha256 routine where applicable.

With this commit, I've covered most of the areas we'll hash an entire
transaction/block/header, but we may want to optimize some other areas
further, in particular, the witness sighash calc.
2023-12-19 15:01:55 -08:00
Kevin Heavey
8677a140eb fix typos in sighash.go 2023-10-20 15:23:55 -04:00
Johan T. Halseth
5c5bef0308
txscript/engine_debug_test: add TestDebugEngine
Add a simple test that make sure the stack and alt stack is correctly
set in the callback from script execution.
2023-08-07 14:22:42 +02:00
Johan T. Halseth
6d38900304
txscript/engine: add execution stepCallback
We add a new stepCallback as optional function closure on the Engine
that will be called every time a step has been performed during script
execution.

It is accessed by calling the NewDebugEngine constructor.

This is only meant to be used in debugging.
2023-08-07 14:22:42 +02:00
cui fliter
e160bb6922 multi: remove repetitive the
Signed-off-by: cui fliter <imcusg@gmail.com>
2023-06-26 15:40:51 +08:00
Calvin Kim
ba5407615d multi: Run gofmt on the entire repository
The doc formatting changes introduced in the recent go version is
increasing the diff for all of the new commits.  Formatting it all in
this commit will help the readability of future PRs by reducing the
diff.
2023-06-21 22:31:09 +09:00
Slyghtning
42653cdcd2 txscript: fix script typos 2023-04-20 11:41:45 +02:00
martonp
a18c2cfbf8 Export MakeScritpNum, AsSmallInt, and IsSmallInt 2023-03-03 15:05:14 -05:00
Oliver Gugger
e1e4196fbc
txscript: allow script builder capacity to be specified
The default script builder allocates 500 bytes of memory for each
script. That is quite large for most applications and therefore wastes a
lot of memory when only small scripts are created.
To avoid breaking backward compatibility, we add a new functional option
to the NewScriptBuilder function that allows the user to specify the
initial allocation size.
2023-02-27 12:38:13 +01:00
Calvin Kim
902f797b0c txscript: Fix typo in IsUnspendable() comment
IsUnspendable allows outputs, not inputs to be pruned instantly.
2023-02-13 08:35:16 -05:00
Olaoluwa Osuntokun
d6efaa7208
txscript: add tests for RawTxInTaprootSignature and RawTxInTapscriptSignature
In this commit, we add tests for the public functions used to generate
keyspend and tapscript signatures. Without the prior commit, these tests
will fail as the keyspend function won't properly add the sighash bytes
for things that aren't sighash default.
2023-01-24 18:43:55 -08:00
Olaoluwa Osuntokun
5e0de1c539
txscript: add new PayToTaprootScript function 2023-01-24 18:43:50 -08:00
Olaoluwa Osuntokun
f6e8292402
txscript: fix sighash bug in RawTxInTaprootSignature
In this commit, we fix a bug in RawTxInTaprootSignature that would cause
the function to not properly apply the sighash flag for non-default
sighash signatures. The logic would end up applying `0x00` as a mask,
which will always be `0x00` on the other end.

The RawTxInTapscriptSignature function was correct, though it had the
ordering switched as it applies the sighash if the type doesn't equal
default.
2023-01-24 18:43:45 -08:00
Olaoluwa Osuntokun
fc47c31b45
txscript: modify TweakTaprootPrivKey to operate on private key copy
In this commit, we fix an inadvertent mutation bug that would at times
cause the private key passed into the tweak function to actually be
*modified* in place.

We fix this by accepting the value instead of a pointer. The actual
private key struct itself contains no pointer fields, so this is
effectively a deep copy via dereference.

We also add a new unit test that fails w/o this change, to show that the
private key was indeed being modified.
2022-10-27 16:19:56 -07:00
Olaoluwa Osuntokun
99e4e00345
txscript: add more detailed taproot errors 2022-03-15 18:23:42 -07:00
Olaoluwa Osuntokun
79c314d503
txscript: add taproot JSON success/fail reference tests
In this commit, we add a total of 2760 taproot reference tests generated
by the bitcoind functional tests located at:
https://github.com/bitcoin/bitcoin/blob/master/test/functional/feature_taproot.py.
The tests aren't deterministic (fresh private keys are generated), so we
time we go to update the set of tests, we'll end up with fresh hashes
(the file name is the sha1 of the raw json test) and tests.
2022-03-15 18:23:29 -07:00
Olaoluwa Osuntokun
5f8660e5c3
txscript: implement OP_CHECKSIGADD
In this commit, we implement OP_CHECKSIGADD which replaces
OP_CHECKMULTISIG* in the tapscript execution environment.
2022-03-15 18:23:26 -07:00
Olaoluwa Osuntokun
3ce6130ee4
txscript: implement OP_CHECKSIG semantics for tapscript validation
In this commit, we implement the new checksig semantics as part of
tapscript validation. Namely:

  * OP_CHECKSIGVERIFY no longer pops the OP_TRUE off the stack (TODO(roasbeef): verify))

  * the new sig ops semantics are added where each sig deducts 50 from a
    starting budget of 50+the weight of the witness

  * NULLFAIL is always enforced, meaning invalid sigs MUST be an empty sig array
2022-03-15 18:23:23 -07:00
Olaoluwa Osuntokun
a4beed9f72
txscript: add new OpcodePosition method to tokenizer to save code sep position
We'll need this to properly generate the sighash during tapscript
validation later
2022-03-15 18:23:21 -07:00
Olaoluwa Osuntokun
3c6be738ed
txscript: always enforce MINIMAL_IF during tapscript execution 2022-03-15 18:23:18 -07:00
Olaoluwa Osuntokun
a7a8ad7d37
txscript: disable OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY for tapscript 2022-03-15 18:23:16 -07:00
Olaoluwa Osuntokun
c1eb15044e
txscript: implement script path verification
In this commit, we use the recently added control block and script tree
verification+generation routines to implement full script path
verification within the VM. This includes verifying the script reveal
commitment, and recursing one layer deeper to execute the revealed
witness script as specified by BIP 342.
2022-03-15 18:23:13 -07:00
Olaoluwa Osuntokun
17e4609494
txscript: add AssembleTaprootScriptTree func for creating input witnesses
In this commit, we add a new AssembleTaprootScriptTree function that
given a list of tapscript leaves, generates a valid tapscript root,
along with the auxiliary proof data needed to spend each output.
2022-03-15 18:23:11 -07:00
Olaoluwa Osuntokun
6fc4199ee4
txscript: add new RawTxInTapscriptSignature to generate tapsript sigs
In this commit, we add a new function `RawTxInTapscriptSignature` that
will be used to generate signatures in the _tapscript_ context. Note
that this differs from top-level taproot as a distinct sighash is used,
and we _always_ accept a root hash to perform the proper tweak.
2022-03-15 18:23:08 -07:00