Commit Graph

934 Commits

Author SHA1 Message Date
merge-script
93e79181da Merge bitcoin/bitcoin#33786: script: remove dead code in CountWitnessSigOps
24bcad3d4d refactor: remove dead code in `CountWitnessSigOps` (Lőrinc)

Pull request description:

  Found while reviewing #32840

  The `nullptr` witness path was dead in normal code paths: replacing it with reference enables us deleting unreachable logic.

  Code coverage proof:
  https://maflcko.github.io/b-c-cov/total.coverage/src/script/interpreter.cpp.gcov.html#L2135

ACKs for top commit:
  kevkevinpal:
    ACK [24bcad3](24bcad3d4d)
  maflcko:
    review ACK 24bcad3d4d 🐏
  darosior:
    Neat. utACK 24bcad3d4d.
  stickies-v:
    ACK 24bcad3d4d

Tree-SHA512: 92c87e431f06a15d8eeb02e20e9154b272c4586ddacf77c8d83783091485fb82c24ecbd711db7043a92cf6169746db24ad46a5904d694aea9d3c3aa96da725f0
2025-11-07 12:46:46 +00:00
Lőrinc
24bcad3d4d refactor: remove dead code in CountWitnessSigOps
Found while reviewing #32840

The `nullptr` witness path was dead in normal code paths: removing it deletes unreachable logic.

Code coverage proof:
https://maflcko.github.io/b-c-cov/total.coverage/src/script/interpreter.cpp.gcov.html#L2135
2025-11-04 22:51:25 +01:00
Hennadii Stepanov
5efdb0ef30 ci: Update Clang in "tidy" job
This change updates to IWYU 0.25, which is compatible with Clang 21.
Fixes new "modernize-use-default-member-init" warnings.
The warning in `interpreter.cpp` is a false positive, so it has been
suppressed.
2025-10-28 15:33:57 +00:00
Ava Chow
c6c4edf324 Merge bitcoin/bitcoin#32983: rpc: refactor: use string_view in Arg/MaybeArg
b63428ac9c rpc: refactor: use more (Maybe)Arg<std::string_view> (stickies-v)
037830ca0d refactor: increase string_view usage (stickies-v)
b3bf18f0ba rpc: refactor: use string_view in Arg/MaybeArg (stickies-v)

Pull request description:

  The `RPCHelpMan::{Arg,MaybeArg}` helpers avoid copying (potentially) large strings by returning them as `const std::string*` (`MaybeArg`) or `const std::string&` (`Arg`). For `MaybeArg`, this has the not-so-nice effect that users need to deal with raw pointers, potentially also requiring new functions (e.g. [`EnsureUniqueWalletName` ](d127b25199 (diff-d8bfcfbdd5fa7d5c52d38c1fe5eeac9ce5c5a794cdfaf683585140fa70a32374R32))) with raw pointers being implemented.

  This PR aims to improve on this by returning a trivially copyable `std::string_view` (`Arg`) or `std::optional<std::string_view>` (`MaybeArg`), modernizing the interface without introducing any additional copying overhead. In doing so, it also generalizes whether we return by value or by pointer/reference using `std::is_trivially_copyable_v` instead of defining the types manually.

  In cases where functions currently take a `const std::string&` and it would be too much work / touching consensus logic to update them (`signmessage.cpp`), a `std::string` copy is made (which was already happening anyway).

  The last 2 commits increase usage of the `{Arg,MaybeArg}<std::string_view>` helpers, and could be dropped/pruned if anything turns out to be controversial - I just think it's a nice little cleanup.

ACKs for top commit:
  maflcko:
    re-ACK b63428ac9c 🎉
  achow101:
    ACK b63428ac9c
  pablomartin4btc:
    re-ACK [b63428a](b63428ac9c)
  w0xlt:
    reACK b63428ac9c

Tree-SHA512: b4942c353a1658c22a88d8c9b402c288ad35265a3b88aa2072b1f9b6d921cd073194ed4b00b807cb48ca440f47c87ef3d8e0dd1a5d814be58fc7743f26288277
2025-10-24 10:33:51 -07:00
merge-script
99cb2054bd Merge bitcoin/bitcoin#33600: refactor: Construct g_verify_flag_names on first use
faa9d10c84 refactor: Construct g_verify_flag_names on first use (MarcoFalke)

Pull request description:

  The current usage of the `g_verify_flag_names` map seems fine and I can not see a static initialization order fiasco here.

  However, it seems brittle to hope this remains the case in the future. Also, it triggers a msan false-positive in the fuzz CI task. (C.f https://github.com/bitcoin-core/qa-assets/actions/runs/18352815555/job/52413137315?pr=241#step:7:5245)

  So just apply the "Construct on first use" idiom.

ACKs for top commit:
  kevkevinpal:
    ACK [faa9d10](faa9d10c84)
  ajtowns:
    ACK faa9d10c84
  janb84:
    lgtm ACK faa9d10c84
  stickies-v:
    ACK faa9d10c84

Tree-SHA512: 6685dfc91c99a8245722e07fac99a7a6d58586c30964be7ccd74a176dfbf00c6255c8594621e2909640763924f51d3efd4ce65ed65eaeeb1d05c2fd01fe63604
2025-10-23 05:55:55 -04:00
merge-script
48aa0e98d0 Merge bitcoin/bitcoin#29675: wallet: Be able to receive and spend inputs involving MuSig2 aggregate keys
ac599c4a9c test: Test MuSig2 in the wallet (Ava Chow)
68ef954c4c wallet: Keep secnonces in DescriptorScriptPubKeyMan (Ava Chow)
4a273edda0 sign: Create MuSig2 signatures for known MuSig2 aggregate keys (Ava Chow)
258db93889 sign: Add CreateMuSig2AggregateSig (Ava Chow)
bf69442b3f sign: Add CreateMuSig2PartialSig (Ava Chow)
512b17fc56 sign: Add CreateMuSig2Nonce (Ava Chow)
82ea67c607 musig: Add MuSig2AggregatePubkeys variant that validates the aggregate (Ava Chow)
d99a081679 psbt: MuSig2 data in Fill/FromSignatureData (Ava Chow)
4d8b4f5336 signingprovider: Add musig2 secnonces (Ava Chow)
c06a1dc86f Add MuSig2SecNonce class for secure allocation of musig nonces (Ava Chow)
9baff05e49 sign: Include taproot output key's KeyOriginInfo in sigdata (Ava Chow)
4b24bfeab9 pubkey: Return tweaks from BIP32 derivation (Ava Chow)
f14876213a musig: Move synthetic xpub construction to its own function (Ava Chow)
fb8720f1e0 sign: Refactor Schnorr sighash computation out of CreateSchnorrSig (Ava Chow)
a4cfddda64 tests: Clarify why musig derivation adds a pubkey and xpub (Ava Chow)
39a63bf2e7 descriptors: Add a doxygen comment for has_hardened output_parameter (Ava Chow)
2320184d0e descriptors: Fix meaning of any_key_parsed (Ava Chow)

Pull request description:

  This PR implements MuSig2 signing so that the wallet can receive and spend from imported `musig(0` descriptors.

  The libsecp musig module is enabled so that it can be used for all of the MuSig2 cryptography.

  Secnonces are handled in a separate class which holds the libsecp secnonce object in a `secure_unique_ptr`. Since secnonces must not be used, this class has no serialization and will only live in memory. A restart of the software will require a restart of the MuSig2 signing process.

ACKs for top commit:
  fjahr:
    tACK ac599c4a9c
  rkrux:
    lgtm tACK ac599c4a9c
  theStack:
    Code-review ACK ac599c4a9c 🗝️

Tree-SHA512: 626b9adc42ed2403e2f4405321eb9ce009a829c07d968e95ab288fe4940b195b0af35ca279a4a7fa51af76e55382bad6f63a23bca14a84140559b3c667e7041e
2025-10-14 16:25:52 -04:00
MarcoFalke
faa9d10c84 refactor: Construct g_verify_flag_names on first use 2025-10-10 16:57:40 +02:00
stickies-v
b3bf18f0ba rpc: refactor: use string_view in Arg/MaybeArg
Modernizes interface by not forcing users to deal with raw pointers,
without adding copying overhead. Generalizes the logic of whether
we return by value or by optional/pointer.

In cases where functions take a `const std::string&` and it would
be too much work to update them, a string copy is made (which was
already happening anyway).
2025-10-02 12:53:25 +01:00
Ava Chow
4a273edda0 sign: Create MuSig2 signatures for known MuSig2 aggregate keys
When creating Taproot signatures, if the key being signed for is known
to be a MuSig2 aggregate key, do the MuSig2 signing algorithms.

First try to create the aggregate signature. This will fail if there are
not enough partial signatures or public nonces. If it does fail, try to
create a partial signature with all participant keys. This will fail for
those keys that we do not have the private keys for, and if there are
not enough public nonces. Lastly, if the partial signatures could not be
created, add our own public nonces for the private keys that we know, if
they do not yet exist.
2025-09-30 11:15:36 -07:00
Ava Chow
258db93889 sign: Add CreateMuSig2AggregateSig 2025-09-30 11:06:43 -07:00
Ava Chow
bf69442b3f sign: Add CreateMuSig2PartialSig 2025-09-30 11:06:43 -07:00
Ava Chow
512b17fc56 sign: Add CreateMuSig2Nonce 2025-09-30 11:06:43 -07:00
Ava Chow
d99a081679 psbt: MuSig2 data in Fill/FromSignatureData 2025-09-30 11:06:43 -07:00
Ava Chow
4d8b4f5336 signingprovider: Add musig2 secnonces
Adds GetMuSig2SecNonces which returns secp256k1_musig_secnonce*, and
DeleteMuSig2Session which removes the MuSig2 secnonce from wherever it
was retrieved. FlatSigningProvider stores it as a pointer to a map of
session id to secnonce so that deletion will actually delete from the
object that actually owns the secnonces.

The session id is just a unique identifier for the caller to determine
what secnonces have been created.
2025-09-30 11:06:43 -07:00
Ava Chow
9baff05e49 sign: Include taproot output key's KeyOriginInfo in sigdata 2025-09-30 11:06:43 -07:00
Ava Chow
f14876213a musig: Move synthetic xpub construction to its own function 2025-09-16 16:09:31 -07:00
Anthony Towns
417437eb01 script/verify_flags: extend script_verify_flags to 64 bits 2025-08-14 10:17:32 +10:00
Anthony Towns
3cbbcb66ef script/interpreter: make script_verify_flag_name an ordinary enum
Instead of having `SCRIPT_VERIFY_FOO = (1U << n)` just have it
be `n` directly, and do the bit shifting when converting it to
`script_verify_flags`.
2025-08-14 10:17:32 +10:00
Anthony Towns
bddcadee82 script/verify_flags: make script_verify_flags type safe
`using script_verify_flags = uint32_t` allows implicit conversion to
and from int, so replace it with a class to have the compiler ensure we
use the correct type. Provide from_int and as_int to allow for explicit
conversions when desired.

Introduces the type `script_verify_flag_name` for the individual flag
name enumeration.
2025-08-14 10:17:32 +10:00
Anthony Towns
a5ead122fe script/interpreter: introduce script_verify_flags typename
Previously the SCRIPT_VERIFY_* flags were specified as either uint32_t,
unsigned int, or unsigned. This converts them to a common type alias in
preparation for changing the underlying type.
2025-08-14 10:17:32 +10:00
Anthony Towns
5db8cd2d37 Move mapFlagNames and FormatScriptFlags logic to script/interpreter.h
Moves FormatScriptFlags logic into GetScriptFlagNames which returns a
vector of strings. For completeness, also has GetScriptFlagNames report
on any bits that do not match a known script flag.
2025-08-14 10:17:30 +10:00
Pieter Wuille
92af9f74d7 script: (optimization) introduce sighash midstate caching 2025-08-06 09:33:17 -04:00
Pieter Wuille
8f3ddb0bcc script: (refactor) prepare for introducing sighash midstate cache 2025-08-06 09:33:17 -04:00
Ava Chow
fb8720f1e0 sign: Refactor Schnorr sighash computation out of CreateSchnorrSig
There will be other functions within MutableTransactionSignatureCreator
that need to compute the same sighash, so make it a separate member
function.
2025-07-31 15:40:25 -07:00
Ava Chow
39a63bf2e7 descriptors: Add a doxygen comment for has_hardened output_parameter 2025-07-31 15:40:25 -07:00
Ava Chow
2320184d0e descriptors: Fix meaning of any_key_parsed
Invert any_key_parsed so that the name matches the behavior.
2025-07-31 15:40:25 -07:00
merge-script
5ee4e79669 Merge bitcoin/bitcoin#31244: descriptors: MuSig2
5fe7915c86 doc: Add musig() example (Ava Chow)
d576079ab4 tests: Test musig() parsing (Ava Chow)
a53924bee3 descriptor: Parse musig() key expressions (Ava Chow)
9473e9606c descriptors: Move DeriveType parsing into its own function (Ava Chow)
4af0dca096 descriptor: Add MuSigPubkeyProvider (Ava Chow)
d00d95437d Add MuSig2 Keyagg Cache helper functions (Ava Chow)
8ecea91bf2 sign: Add GetMuSig2ParticipantPubkeys to SigningProvider (Ava Chow)
fac0ee0bfc build: Enable secp256k1 musig module (Ava Chow)
1894f97503 descriptors: Add PubkeyProvider::IsBIP32() (Ava Chow)
12bc1d0b1e util/string: Allow Split to include the separator (Ava Chow)
8811312571 script/parsing: Allow Const to not skip the found constant (Ava Chow)
5fe4c66462 XOnlyPubKey: Add GetCPubKeys (Ava Chow)

Pull request description:

  Implements parsing of BIP 390 `musig()` descriptors.

  Split from #29675

ACKs for top commit:
  w0xlt:
    reACK 5fe7915c86
  rkrux:
    ACK 5fe7915c86
  theStack:
    re-ACK 5fe7915c86 🎹
  Sjors:
    ACK 5fe7915c86

Tree-SHA512: a5be6288e277187fb9a1e2adf4e9822b46b1b8380d732b2fabd53f317c839aecb1971b04410486cbd65047fbc20956675d4d676f56caa37a44ff0e4d12b9b081
2025-07-31 16:51:39 -04:00
Lőrinc
d5104cfbae prevector: store P2WSH/P2TR/P2PK scripts inline
The current `prevector` size of 28 bytes (chosen to fill the `sizeof(CScript)` aligned size) was introduced in 2015 (https://github.com/bitcoin/bitcoin/pull/6914) before SegWit and TapRoot.
However, the increasingly common `P2WSH` and `P2TR` scripts are both 34 bytes, and are forced to use heap (re)allocation rather than efficient inline storage.

The core trade-off of this change is to eliminate heap allocations for common 34-36 byte scripts at the cost of increasing the base memory footprint of all `CScript` objects by 8 bytes (while still respecting peak memory usage defined by `-dbcache`).

Increasing the `prevector` size allows these scripts to be stored inline, avoiding extra heap allocations, reducing potential memory fragmentation, and improving performance during cache flushes. Massif analysis confirms a lower stable memory usage after flushing, suggesting the elimination of heap allocations outweighs the larger base size for common workloads.

Due to memory alignment, increasing the `prevector` size to 36 bytes doesn't change the overall `sizeof(CScript)` compared to an increase to 34 bytes, allowing us to include `P2PK` scripts as well at no additional memory cost.

Performance benchmarks for AssumeUTXO load and flush show:
* Small dbcache (450MB): ~1-3% performance improvement (despite more frequent flushes)
* Large dbcache (4500MB): ~6-8% performance improvement due to fewer heap allocations (and basically the number of flushes)
* Very large dbcache (4500MB): ~5-6% performance improvement due to fewer heap allocations (and memory limit not being reached, so there's no memory penalty)

Full IBD and reindex-chainstate with larger `dbcache` values also show an overall ~3-4% speedup.

Co-authored-by: Ava Chow <github@achow101.com>
Co-authored-by: Andrew Toth <andrewstoth@gmail.com>
Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
2025-07-25 22:33:50 -07:00
Lőrinc
65ac7f6d4d refactor: modernize CScriptBase definition 2025-07-25 16:23:38 -07:00
MarcoFalke
face8123fd log: [refactor] Use info level for init logs
This refactor does not change behavior.
2025-07-25 09:50:50 +02:00
merge-script
a92e8b10a5 Merge bitcoin/bitcoin#32564: miniscript, refactor: Make operator""_mst consteval (re-take)
a34fb9ad6c miniscript: Make `operator""_mst` `consteval` (Pieter Wuille)
14052162b1 Revert "miniscript: make operator_mst consteval" (Hennadii Stepanov)

Pull request description:

  Same as https://github.com/bitcoin/bitcoin/pull/28657, but without the refactoring required to work around [fixed](https://github.com/bitcoin/bitcoin/pull/28657#discussion_r2095743353) MSVC bugs.

  The second commit has been taken from https://github.com/bitcoin/bitcoin/pull/29167.

ACKs for top commit:
  sipa:
    ACK a34fb9ad6c
  hodlinator:
    re-ACK a34fb9ad6c

Tree-SHA512: 8b531f9d6c450a8a5218865da05ffb5093d09ce2c0bee9874c0160795c4b1713928730d894ea3cd0b12b133346971ae3a00ed2fe8d9fd8a50b67a74ef81fde98
2025-07-02 15:06:33 +01:00
Pieter Wuille
a34fb9ad6c miniscript: Make operator""_mst consteval 2025-06-25 11:33:18 +01:00
Hennadii Stepanov
14052162b1 Revert "miniscript: make operator_mst consteval"
This reverts commit 63317103c9.

operator""_mst has been manually adjusted according to commit
faf2162565
2025-06-25 11:32:57 +01:00
Ava Chow
a53924bee3 descriptor: Parse musig() key expressions 2025-06-24 13:40:12 -07:00
Ava Chow
9473e9606c descriptors: Move DeriveType parsing into its own function 2025-06-24 13:39:54 -07:00
Ava Chow
4af0dca096 descriptor: Add MuSigPubkeyProvider 2025-06-24 13:35:29 -07:00
Ava Chow
8ecea91bf2 sign: Add GetMuSig2ParticipantPubkeys to SigningProvider 2025-06-11 11:11:48 -07:00
Ava Chow
1894f97503 descriptors: Add PubkeyProvider::IsBIP32() 2025-06-11 11:11:48 -07:00
Ava Chow
8811312571 script/parsing: Allow Const to not skip the found constant
When parsing a descriptor, it is useful to be able to check whether a
string begins with a substring without consuming that substring as
another function such as Func() will be used later which requires that
substring to be present at the beginning.

Specifically, for MuSig2, this modified Const will be used to determine
whether a an expression begins with "musig(" before a subsequent
Func("musig", ...) is used.
2025-06-11 11:07:35 -07:00
Ava Chow
a118256948 script: Add IsPayToTaproot() 2025-05-14 14:00:43 -07:00
MarcoFalke
fa2125e7b8 Remove unused IsSingleKey 2025-05-09 15:06:51 +02:00
MarcoFalke
fa55dd01df descriptors: Reject + sign when parsing multi threshold 2025-04-28 17:05:37 +02:00
MarcoFalke
fa6f77ed3c descriptors: Reject + sign in ParseKeyPathNum 2025-04-28 17:05:36 +02:00
Ava Chow
acee5c59e6 descriptors: Have GetPrivKey fill keys directly
Instead of GetPrivKey returning a key and having the caller fill the
FlatSigningProvider, have GetPrivKey take the FlatSigningProvider and
fill it by itself. This will be necessary for descriptors such as
musig() where there are private keys that need to be added to the
FlatSigningProvider but do not directly appear in any resulting scripts.

GetPrivKey is now changed to void as the caller no longer cares whether
it succeeds or fails.
2025-04-14 16:32:01 -07:00
Ava Chow
4b0303197e descriptors: Move FlatSigningProvider pubkey filling to GetPubKey
Instead of MakeScripts inconsistently filling the output
FlatSigningProvider with the pubkeys involved, just do it in GetPubKey.
2025-04-14 16:32:01 -07:00
Ava Chow
25a3b9b0f5 descriptors: Have GetPubKey fill origins directly
Instead of having ExpandHelper fill in the origins in the
FlatSigningProvider output, have GetPubKey do it by itself. This reduces
the extra variables needed in order to track and set origins in
ExpandHelper.

Also changes GetPubKey to return a std::optional<CPubKey> rather than
using a bool and output parameters.
2025-04-14 16:32:01 -07:00
Ava Chow
6268bde0af descriptor: Remove unused parent_info from BIP32PUbKeyProvider::GetPubKey 2025-04-14 16:32:01 -07:00
Ava Chow
0ff072caa1 wallet, rpc: Only allow keypool import from single key descriptors
Legacy wallets should only import keys to the keypool if they came in a
single key descriptor. Instead of relying on assumptions about the
descriptor based on how many pubkeys show up after expanding the
descriptor, explicitly mark descriptors as being single key type and use
that for the check.
2025-04-14 16:32:01 -07:00
Hodlinator
3693e4d6ee miniscript: Correct off-by-one assert guards
Co-authored-by: Antoine Poinsot <darosior@protonmail.com>
2025-04-12 09:46:56 +02:00
merge-script
a4fd565191 Merge bitcoin/bitcoin#31727: miniscript: convert non-critical asserts to CHECK_NONFATAL
ff0194a7ce miniscript: convert non-critical asserts to CHECK_NONFATAL (Antoine Poinsot)

Pull request description:

  The Miniscript code contains assertions to prevent ending up in an insane state or prevent UB, but also to enforce logical invariants. For the latter it is not necessary to crash the program if they are broken. Raising an exception suffices, especially as this code is often called through the RPC interface which can in turn handle the exception and the user can report it to developers.

  This revives #28678 from Pieter Wuille.

ACKs for top commit:
  hodlinator:
    ACK ff0194a7ce
  TheCharlatan:
    ACK ff0194a7ce
  brunoerg:
    code review ACK ff0194a7ce

Tree-SHA512: 8ed8f7b494e46ecf7cdebe75120cd0ffe543b6bc289bf882dac631fe2ec2cae590d5f7bc2316e52db085791694b136dffbc71c40c1e16886fa53ab00bd8cabd0
2025-04-10 14:06:21 -04:00