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

refactor: Allow std::span construction from CKey #29133

Merged
merged 2 commits into from
Jan 17, 2024

Conversation

maflcko
Copy link
Member

@maflcko maflcko commented Dec 22, 2023

Is is possible to construct a Span from a reference to a CKey. However, the same is not possible with std::span.

Fix that.

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 22, 2023

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

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK shaavan, willcl-ark

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:

  • #28774 (Avoid returning references to mutex guarded members by vasild)
  • #28710 (Remove the legacy wallet and BDB dependency 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.

@DrahtBot DrahtBot changed the title refactor: Allow std::span construction from CKey refactor: Allow std::span construction from CKey Dec 22, 2023
@DrahtBot
Copy link
Contributor

Guix builds (on x86_64)

File commit 4b1196a
(master)
commit b6d4aaa
(master and this pull)
SHA256SUMS.part 7aefd803a584a152... ab079c58401ba0fb...
*-aarch64-linux-gnu-debug.tar.gz 735f38fb37b239f8... 4dbdbe5e93c73851...
*-aarch64-linux-gnu.tar.gz 83a257c74e184b96... 988aaff726a6daa2...
*-arm-linux-gnueabihf-debug.tar.gz 513acf435b18fab2... b8b4ecdca2193722...
*-arm-linux-gnueabihf.tar.gz 581f911fcca5ae02... 76777352461e8bc6...
*-arm64-apple-darwin-unsigned.tar.gz 7a3f2b4e01f27896... 19d1adb684d05225...
*-arm64-apple-darwin-unsigned.zip dff45b014236e990... 47db0212b16e9ed6...
*-arm64-apple-darwin.tar.gz 0d9696b7bd76b36f... c43711dc9f00aa77...
*-powerpc64-linux-gnu-debug.tar.gz dfe6cfdcd6ad3c6f... 419d862f3e56bbf8...
*-powerpc64-linux-gnu.tar.gz 54c013201daad1ff... bbfd8c4b3727a09e...
*-powerpc64le-linux-gnu-debug.tar.gz 8e944b002273b678... ce4db42977bd22a2...
*-powerpc64le-linux-gnu.tar.gz 1ba8199984d7c43a... d8293f8d7c529a5c...
*-riscv64-linux-gnu-debug.tar.gz e9abe48aeb7acc68... f1f2c5ba6f83255d...
*-riscv64-linux-gnu.tar.gz f4d1484dec65b967... 62afb94bd8008b15...
*-x86_64-apple-darwin-unsigned.tar.gz 915650aa35e6e322... f0fcc5c9b634cdf5...
*-x86_64-apple-darwin-unsigned.zip 6a6139cb415de743... 0629e63a88039b69...
*-x86_64-apple-darwin.tar.gz c479d099d1df9a85... 097d74e47b61b648...
*-x86_64-linux-gnu-debug.tar.gz 9e27161179f18a4a... 1c4818ca22c3cc3e...
*-x86_64-linux-gnu.tar.gz f69632ca3dc98896... 937b9ff173077441...
*.tar.gz c04997c9f9e51d80... 838da9ae9aa9e619...
guix_build.log 5528a9baa3a070f3... 86f09be4ba8fad36...
guix_build.log.diff cf884818846014a3...

Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

Code Review ACK

Notes:

Commit-1:
Since std::span requires a mutable pointer to the data for certain operations, this change makes XOnlyPubKey compatible with std::span.

Commit-2:
Since the return type of begin() and end() much match the data type of the std::span of the container, this change makes CKey compatible with std::span, and allows std::span<std::byte> construction for it.

Commit-3:
Tests that the above changes made CKey compatible with std::span.

I am curious about one thing, though.

If we now have std::span since C++20, are we thinking of making every container in the codebase std::span compatible and moving away from needing to use our ### custom Span class?

@maflcko
Copy link
Member Author

maflcko commented Jan 2, 2024

If we now have std::span since C++20, are we thinking of making every container in the codebase std::span compatible and moving away from needing to use our ### custom Span class?

Yes, I think it makes sense to allow new code to use std::span directly. Also, I think it makes sense to (longer term) remove Span (or make it an alias of std::span) to avoid confusion which to use in new code, and to get the additional compile-time checks from std::span.

This is needed for consistency, and also to allow std::span construction
from XOnlyPubKey.
@maflcko maflcko force-pushed the 2312-key-span- branch 2 times, most recently from fae4d89 to 84b2dfb Compare January 16, 2024 14:28
@maflcko
Copy link
Member Author

maflcko commented Jan 16, 2024

I dropped the last commit, to not introduce new std::span usage for now. To verify the changes in this pull, the commit's diff can be used. (Fails to compile before, compiles after):

diff --git a/src/key.cpp b/src/key.cpp
index 2bd6396298..300c95e35a 100644
--- a/src/key.cpp
+++ b/src/key.cpp
@@ -385,7 +385,7 @@ bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
     return key.Derive(out.key, out.chaincode, _nChild, chaincode);
 }
 
-void CExtKey::SetSeed(Span<const std::byte> seed)
+void CExtKey::SetSeed(std::span<const std::byte> seed)
 {
     static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
     std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
diff --git a/src/key.h b/src/key.h
index d6b26f891d..d63ec6c1cc 100644
--- a/src/key.h
+++ b/src/key.h
@@ -12,10 +12,10 @@
 #include <support/allocators/secure.h>
 #include <uint256.h>
 
+#include <span>
 #include <stdexcept>
 #include <vector>
 
-
 /**
  * CPrivKey is a serialized private key, with all parameters included
  * (SIZE bytes)
@@ -227,7 +227,7 @@ struct CExtKey {
     void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
     [[nodiscard]] bool Derive(CExtKey& out, unsigned int nChild) const;
     CExtPubKey Neuter() const;
-    void SetSeed(Span<const std::byte> seed);
+    void SetSeed(std::span<const std::byte> seed);
 };
 
 /** Initialize the elliptic curve support. May not be called twice without calling ECC_Stop first. */

Copy link
Contributor

@shaavan shaavan left a comment

Choose a reason for hiding this comment

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

ReACK fa96d93

Changes since my last review

  • Removed the last commit, which was temporarily introduced to test the workings of changes in this commit.
  • Tested that the above patch leads to failure in master, while successful compilation with this PR.

Copy link
Member

@willcl-ark willcl-ark left a comment

Choose a reason for hiding this comment

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

ACK fa96d93

A nice simple change to enable using std::span with CKey.

The PR description could be updated now that the verification commit has been removed.

@fanquake fanquake merged commit 5142681 into bitcoin:master Jan 17, 2024
16 checks passed
@maflcko maflcko deleted the 2312-key-span- branch January 18, 2024 10:29
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.

None yet

5 participants