Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FreshEyes] crypto, refactor: add new KeyPair class #15

Open
wants to merge 2 commits into
base: bitcoin-fresheyes-staging-master-30051
Choose a base branch
from

Conversation

adamjonas
Copy link
Owner

The author josibake wrote the following PR called crypto, refactor: add new KeyPair class, issue number 30051 in bitcoin/bitcoin cloned by FreshEyes below:

Broken out from #28201


The wallet returns an untweaked internal key for taproot outputs. If the output commits to a tree of scripts, this key needs to be tweaked with the merkle root. Even if the output does not commit to a tree of scripts, BIP341/342 recommend commiting to a hash of the public key.

Previously, this logic for applying the taptweak was implemented in the CKey::SignSchnorr method.

This PR moves introduces a KeyPair class which wraps a secp256k1_keypair type and refactors SignSchnorr to use this new KeyPair. The KeyPair class is created with an optional merkle_root argument and the logic from BIP341 is applied depending on the state of the merkle_root argument.

The motivation for this refactor is to be able to use the tap tweak logic outside of signing, e.g. in silent payments when retrieving the private key (see #28201).

Outside of silent payments, since we almost always convert a CKey to a secp256k1_keypair when doing anything with taproot keys, it seems generally useful to have a way to model this type in our code base.

josibake and others added 2 commits May 10, 2024 19:52
The wallet returns an untweaked internal key for taproot outputs. If the
output commits to a tree of scripts, this key needs to be tweaked with
the merkle root. Even if the output does not commit to a tree of
scripts, BIP341/342 recommend commiting to a hash of the public key.

Previously, this logic for applying the taptweak was implemented in the
::SignSchnorr method.

Move this tweaking and signing logic to a new class, KeyPair, and add
a method to CKey for computing a KeyPair, CKey::ComputeKeyPair. This
class is a wrapper for the `secp256k1_keypair` type.

The motivation is to be able to use the the tweaked internal key outside
of signing, e.g. in silent payments when retreiving the private key before
ECDH. Having the KeyPair class is also a general improvement in that we
almost always convert to `secp256k1_keypair` objects when using taproot
private keys with libsecp256k1.

Co-authored-by: Cory Fields <[email protected]>
Reuse existing bip340 test vectors.
Copy link

There were 36 issue comments left by 4 reviewers for the pull request

* (this is used for key path spending, with specific
* Merkle root of the script tree).
*/
KeyPair ComputeKeyPair(const uint256* merkle_root) const;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 authors commented here with:

  • comment link https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1596806833 at 2024/05/10, 14:11:53 UTC
  • comment link https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1596979700 at 2024/05/10, 16:44:18 UTC.

// Repeat the same check, but use the KeyPair directly without any merkle tweak
KeyPair keypair = key.ComputeKeyPair(/*merkle_root=*/nullptr);
CKey keypair_seckey;
BOOST_CHECK(keypair.GetKey(keypair_seckey));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 authors commented here with:

  • comment link https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1597605164 at 2024/05/12, 10:16:07 UTC
  • comment link https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1598114179 at 2024/05/13, 08:55:21 UTC.

private:
KeyPair(const CKey& key, const uint256* merkle_root);

using KeyType = std::array<unsigned char, 96>;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 authors commented here with:

  • comment link https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1597605647 at 2024/05/12, 10:18:42 UTC
  • comment link https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1598119960 at 2024/05/13, 08:59:33 UTC.

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