Skip to content

Conversation

w0xlt
Copy link
Contributor

@w0xlt w0xlt commented Apr 23, 2025

Currently, XOnlyPubKey::GetKeyIDs() has vector as return type, but always returns a pair of public key IDs.

This PR changes the code for readability reasons.
There may be negligible efficiency gains, but the goal is to make the code more readable and to make the intent of the function clear.
There is no change in behavior.

@DrahtBot
Copy link
Contributor

DrahtBot commented Apr 23, 2025

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/32332.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK hodlinator, rkrux

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #32724 (Musig2 tests by w0xlt)
  • #31244 (descriptors: MuSig2 by achow101)
  • #29675 (wallet: Be able to receive and spend inputs involving MuSig2 aggregate keys by achow101)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

Copy link
Contributor

@hodlinator hodlinator left a comment

Choose a reason for hiding this comment

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

Was nerd-sniped by this PR decreasing heap usage. Otherwise these kinds of refactorings are a bit frowned upon.

src/pubkey.cpp Outdated
CPubKey fullpubkey;
fullpubkey.Set(b, b + 33);
out.push_back(fullpubkey.GetID());
CKeyID idEvenPubKey = fullpubkey.GetID();
Copy link
Contributor

Choose a reason for hiding this comment

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

New code should use snake_case for variables according to developer-notes.md.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks.

src/pubkey.h Outdated
* This is needed for key lookups since keys are indexed by CKeyID.
*/
std::vector<CKeyID> GetKeyIDs() const;
std::pair<CKeyID, CKeyID> GetKeyIDs() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

If you instead return std::array<CKeyID, 2> the data is still on the stack and you don't need to touch signingprovider.h.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Thanks.

@w0xlt
Copy link
Contributor Author

w0xlt commented Apr 24, 2025

@hodlinator yes, this decreases heap usage, but I don't think there are any relevant performance gains.

@DrahtBot DrahtBot mentioned this pull request Jun 11, 2025
Copy link
Contributor

@hodlinator hodlinator left a comment

Choose a reason for hiding this comment

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

ACK 4041cbf

src/pubkey.cpp Outdated
@@ -197,21 +197,24 @@ constexpr XOnlyPubKey XOnlyPubKey::NUMS_H{
[]() consteval { return XOnlyPubKey{"50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0"_hex_u8}; }(),
};

std::vector<CKeyID> XOnlyPubKey::GetKeyIDs() const
std::array<CKeyID,2> XOnlyPubKey::GetKeyIDs() const
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Grepped for std::array<\w+,\w+ and only found 2 other occurrences (vs 89 for std::array<\w+, \w+).

Suggested change
std::array<CKeyID,2> XOnlyPubKey::GetKeyIDs() const
std::array<CKeyID, 2> XOnlyPubKey::GetKeyIDs() const

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in acf73d3

src/pubkey.cpp Outdated
out.push_back(fullpubkey.GetID());

CPubKey full_pubkey;
full_pubkey.Set(b, b + 33);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could use cbegin/cend here and below:

Suggested change
full_pubkey.Set(b, b + 33);
full_pubkey.Set(std::cbegin(b), std::cend(b));

Copy link
Contributor Author

@w0xlt w0xlt Jun 17, 2025

Choose a reason for hiding this comment

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

Done in acf73d3

src/pubkey.h Outdated
@@ -282,10 +282,10 @@ class XOnlyPubKey
/** Construct a Taproot tweaked output point with this point as internal key. */
std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;

/** Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
/** Returns two CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Could be more precise:

Suggested change
/** Returns two CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
/** Returns CKeyIDs for the even and odd CPubKeys that could have been used to create this XOnlyPubKey.

Copy link
Contributor Author

@w0xlt w0xlt Jun 17, 2025

Choose a reason for hiding this comment

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

Done in acf73d3

@w0xlt w0xlt force-pushed the xonly_pair_pubkey branch from 4041cbf to acf73d3 Compare June 17, 2025 22:55
src/pubkey.cpp Outdated
CKeyID id_odd_pubKey{full_pubkey.GetID()};

// Return [0] = even-Y, [1] = odd-Y
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubKey };
Copy link
Contributor

@hodlinator hodlinator Jun 18, 2025

Choose a reason for hiding this comment

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

nit - casing inconsistency:

Suggested change
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubKey };
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubkey };

Edit: could just drop _pub[kK]ey as we are in the XOnlyPubKey:

Suggested change
return std::array<CKeyID, 2>{ id_even_pubkey, id_odd_pubKey };
return std::array<CKeyID, 2>{ id_even, id_odd };

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 1718177

@w0xlt w0xlt force-pushed the xonly_pair_pubkey branch from acf73d3 to 1718177 Compare June 18, 2025 17:30
Copy link
Contributor

@hodlinator hodlinator left a comment

Choose a reason for hiding this comment

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

re-ACK 1718177

Tiny refactor that clarifies the XOnlyPubKey::GetKeyIDs() interface by explicitly returning 2 ids (making heap-usage less likely as a bonus). Also documents even/odd Y values.

Only trivial changes since first ACK (please resolve).

Copy link
Contributor

@rkrux rkrux left a comment

Choose a reason for hiding this comment

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

ACK 1718177

Usually minor refactoring changes are avoided but I don't seem to mind this change. It does increase the readability marginally as I like seeing an array of size two in the return type instead of vector while going through the callers of GetKeyIDs function.
Both array & vector satisfy the requirements of the CPP Container as well.

@achow101
Copy link
Member

-0

I don't think such refactorings are meaningfully useful. While it may improve readability a bit, I don't think the difference is enough to justify making this change. Such refactorings both can cause merge conflicts with other higher priority work, and also make git blame a little bit harder.

@DrahtBot
Copy link
Contributor

🐙 This pull request conflicts with the target branch and needs rebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants