Skip to content

Conversation

@jgriffiths
Copy link
Contributor

  • Add Elements support for bip341 signature hashing/signing (keyspend only)
  • Add support for signing Elements p2tr PSETs (keyspend only)
  • Rework bip341 signing to be cleaner/faster/use less memory/support caching
  • Disable Elements descriptor address generation until its implemented

Taproot signing is extremely inefficient without several levels of
caching (e.g. pre-computing tagged hashes, and caching sub-hashes when
signing). This is especially true for Elements which adds much more data
to the signature hash.

Wally's initial hash generation relied on creating the entire preimage
and then hashing it in one call. Since segwit and particularly taproot,
this approach has not aged well, and is not compatible with efficient
tagged hashing.

For bip341 Elements signing, this change implements a new approach which
hashes the data piecemeal rather than concatenating it first. This uses
much less cpu, memory and stack, and eliminates the need to pre-compute
the final allocation length of the preimage before generating it, leading
to less, more readable code.

Additionally, we implement an on-demand cache for preimage data when
signing. By passing in a wally_map to hold the cached data, callers can
greatly decrease the time required to sign.

We replace the existing BTC taproot implementation with this new one,
and fix several bugs that were found as a result:

- The sub-length calculation for tapscripts was incorrect
- The sighash mask for taproot was incorrect
- ANYONECANPAY required all input data when only the given inputs data
  was used.
Use the new signature hash function for both btc and elements.

For signing via PSBT input instead of via the PSBT, this requires that
EC_FLAG_ELEMENTS is passed in flags, in order to correctly use the
Elements-specific tagged hashes. This is because the input alone does
not know if it belongs to a PSET.
This test case was generated from Elements with extra processing to work
around Elements bugs and lack of ELIP-0101 support there.
Having the cache as part of the PSBT is not ideal, but it allows us to
support caching without having to modify a bunch of psbt calls in an
incompatible fashion.

Also includes a drive-by fix to add genesis_blockhash to the tests
ctypes wrapper, as it was missed previously.
@jgriffiths jgriffiths closed this pull request by merging all changes into master in fd74e27 Feb 26, 2025
@jgriffiths jgriffiths deleted the taproot_elements branch February 26, 2025 02:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants