forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 2
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
adamjonas
wants to merge
2
commits into
bitcoin-fresheyes-staging-master-30051
Choose a base branch
from
josibake-fresheyes-staging-add-apply-taptweak-method-30051
base: bitcoin-fresheyes-staging-master-30051
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ constexpr static size_t ECDH_SECRET_SIZE = CSHA256::OUTPUT_SIZE; | |
// Used to represent ECDH shared secret (ECDH_SECRET_SIZE bytes) | ||
using ECDHSecret = std::array<std::byte, ECDH_SECRET_SIZE>; | ||
|
||
class KeyPair; | ||
|
||
/** An encapsulated private key. */ | ||
class CKey | ||
{ | ||
|
@@ -203,6 +205,20 @@ class CKey | |
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, | ||
const EllSwiftPubKey& our_ellswift, | ||
bool initiating) const; | ||
/** Compute a KeyPair | ||
* | ||
* Wraps a `secp256k1_keypair` type. `merkle_root` is used to optionally perform tweaking of | ||
* the internal key, as specified in BIP341: | ||
* | ||
* - If merkle_root == nullptr: no tweaking is done, use the internal key directly (this is | ||
* used for signatures in BIP342 script). | ||
* - If merkle_root->IsNull(): tweak the internal key with H_TapTweak(pubkey) (this is used for | ||
* key path spending when no scripts are present). | ||
* - Otherwise: tweak the internal key H_TapTweak(pubkey || *merkle_root) | ||
* (this is used for key path spending, with specific | ||
* Merkle root of the script tree). | ||
*/ | ||
KeyPair ComputeKeyPair(const uint256* merkle_root) const; | ||
}; | ||
|
||
CKey GenerateRandomKey(bool compressed = true) noexcept; | ||
|
@@ -236,6 +252,41 @@ struct CExtKey { | |
void SetSeed(Span<const std::byte> seed); | ||
}; | ||
|
||
/** KeyPair | ||
* | ||
* Wraps a `secp256k1_keypair` type. `merkle_root` is used to optionally perform tweaking of | ||
* the internal key, as specified in BIP341: | ||
* | ||
* - If merkle_root == nullptr: no tweaking is done, use the internal key directly (this is | ||
* used for signatures in BIP342 script). | ||
* - If merkle_root->IsNull(): tweak the internal key with H_TapTweak(pubkey) (this is used for | ||
* key path spending when no scripts are present). | ||
* - Otherwise: tweak the internal key H_TapTweak(pubkey || *merkle_root) | ||
* (this is used for key path spending, with specific | ||
* Merkle root of the script tree). | ||
*/ | ||
class KeyPair | ||
{ | ||
public: | ||
KeyPair(KeyPair&&) noexcept = default; | ||
KeyPair& operator=(KeyPair&&) noexcept = default; | ||
|
||
friend KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const; | ||
[[nodiscard]] bool GetKey(CKey& key) const; | ||
[[nodiscard]] bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256& aux) const; | ||
|
||
//! Simple read-only vector-like interface. | ||
unsigned int size() const { return m_keydata ? m_keydata->size() : 0; } | ||
const std::byte* data() const { return m_keydata ? reinterpret_cast<const std::byte*>(m_keydata->data()) : nullptr; } | ||
const std::byte* begin() const { return data(); } | ||
const std::byte* end() const { return data() + size(); } | ||
private: | ||
KeyPair(const CKey& key, const uint256* merkle_root); | ||
|
||
using KeyType = std::array<unsigned char, 96>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 authors commented here with:
|
||
secure_unique_ptr<KeyType> m_keydata; | ||
}; | ||
|
||
/** Check that required EC support is available at runtime. */ | ||
bool ECC_InitSanityCheck(); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,6 +327,16 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors) | |
// Verify those signatures for good measure. | ||
BOOST_CHECK(pubkey.VerifySchnorr(msg256, sig64)); | ||
|
||
// 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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 authors commented here with:
|
||
BOOST_CHECK(key == keypair_seckey); | ||
bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256); | ||
BOOST_CHECK(kp_ok); | ||
XOnlyPubKey keypair_xonly{keypair_seckey.GetPubKey()}; | ||
BOOST_CHECK(keypair_xonly.VerifySchnorr(msg256, sig64)); | ||
|
||
// Do 10 iterations where we sign with a random Merkle root to tweak, | ||
// and compare against the resulting tweaked keys, with random aux. | ||
// In iteration i=0 we tweak with empty Merkle tree. | ||
|
@@ -340,6 +350,16 @@ BOOST_AUTO_TEST_CASE(bip340_test_vectors) | |
bool ok = key.SignSchnorr(msg256, sig64, &merkle_root, aux256); | ||
BOOST_CHECK(ok); | ||
BOOST_CHECK(tweaked_key.VerifySchnorr(msg256, sig64)); | ||
|
||
// Repeat the same check, but use the KeyPair class directly | ||
KeyPair keypair = key.ComputeKeyPair(&merkle_root); | ||
CKey keypair_seckey; | ||
BOOST_CHECK(keypair.GetKey(keypair_seckey)); | ||
XOnlyPubKey keypair_xonly{keypair_seckey.GetPubKey()}; | ||
BOOST_CHECK(tweaked_key == keypair_xonly); | ||
bool kp_ok = keypair.SignSchnorr(msg256, sig64, aux256); | ||
BOOST_CHECK(kp_ok); | ||
BOOST_CHECK(keypair_xonly.VerifySchnorr(msg256, sig64)); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
https://github.com/bitcoin/bitcoin/pull/30051#discussion_r1596806833
at 2024/05/10, 14:11:53 UTChttps://github.com/bitcoin/bitcoin/pull/30051#discussion_r1596979700
at 2024/05/10, 16:44:18 UTC.