Skip to content

Commit 5142681

Browse files
committed
Merge bitcoin/bitcoin#29133: refactor: Allow std::span construction from CKey
fa96d93 refactor: Allow std::span construction from CKey (MarcoFalke) 999962d Add missing XOnlyPubKey::data() to get mutable data (MarcoFalke) Pull request description: Is is possible to construct a `Span` from a reference to a `CKey`. However, the same is not possible with `std::span`. Fix that. ACKs for top commit: shaavan: ReACK fa96d93 willcl-ark: ACK fa96d93 Tree-SHA512: 44fccdce5f32bc16b44f3b1bd32e86d9eabfd09bca6abe79f2d6db0cb0b5e4aaeaff710f023cb21ccde9315d2007d55f1b43f29416e81bceeeabe3948f673d3a
2 parents c818607 + fa96d93 commit 5142681

File tree

5 files changed

+19
-18
lines changed

5 files changed

+19
-18
lines changed

src/key.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ CPrivKey CKey::GetPrivKey() const {
179179
size_t seckeylen;
180180
seckey.resize(SIZE);
181181
seckeylen = SIZE;
182-
ret = ec_seckey_export_der(secp256k1_context_sign, seckey.data(), &seckeylen, begin(), fCompressed);
182+
ret = ec_seckey_export_der(secp256k1_context_sign, seckey.data(), &seckeylen, UCharCast(begin()), fCompressed);
183183
assert(ret);
184184
seckey.resize(seckeylen);
185185
return seckey;
@@ -190,7 +190,7 @@ CPubKey CKey::GetPubKey() const {
190190
secp256k1_pubkey pubkey;
191191
size_t clen = CPubKey::SIZE;
192192
CPubKey result;
193-
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, begin());
193+
int ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pubkey, UCharCast(begin()));
194194
assert(ret);
195195
secp256k1_ec_pubkey_serialize(secp256k1_context_sign, (unsigned char*)result.begin(), &clen, &pubkey, fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED);
196196
assert(result.size() == clen);
@@ -220,19 +220,19 @@ bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool gr
220220
WriteLE32(extra_entropy, test_case);
221221
secp256k1_ecdsa_signature sig;
222222
uint32_t counter = 0;
223-
int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, (!grind && test_case) ? extra_entropy : nullptr);
223+
int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), UCharCast(begin()), secp256k1_nonce_function_rfc6979, (!grind && test_case) ? extra_entropy : nullptr);
224224

225225
// Grind for low R
226226
while (ret && !SigHasLowR(&sig) && grind) {
227227
WriteLE32(extra_entropy, ++counter);
228-
ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, extra_entropy);
228+
ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), UCharCast(begin()), secp256k1_nonce_function_rfc6979, extra_entropy);
229229
}
230230
assert(ret);
231231
secp256k1_ecdsa_signature_serialize_der(secp256k1_context_sign, vchSig.data(), &nSigLen, &sig);
232232
vchSig.resize(nSigLen);
233233
// Additional verification step to prevent using a potentially corrupted signature
234234
secp256k1_pubkey pk;
235-
ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pk, begin());
235+
ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &pk, UCharCast(begin()));
236236
assert(ret);
237237
ret = secp256k1_ecdsa_verify(secp256k1_context_static, &sig, hash.begin(), &pk);
238238
assert(ret);
@@ -258,15 +258,15 @@ bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig)
258258
vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
259259
int rec = -1;
260260
secp256k1_ecdsa_recoverable_signature rsig;
261-
int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &rsig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, nullptr);
261+
int ret = secp256k1_ecdsa_sign_recoverable(secp256k1_context_sign, &rsig, hash.begin(), UCharCast(begin()), secp256k1_nonce_function_rfc6979, nullptr);
262262
assert(ret);
263263
ret = secp256k1_ecdsa_recoverable_signature_serialize_compact(secp256k1_context_sign, &vchSig[1], &rec, &rsig);
264264
assert(ret);
265265
assert(rec != -1);
266266
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
267267
// Additional verification step to prevent using a potentially corrupted signature
268268
secp256k1_pubkey epk, rpk;
269-
ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &epk, begin());
269+
ret = secp256k1_ec_pubkey_create(secp256k1_context_sign, &epk, UCharCast(begin()));
270270
assert(ret);
271271
ret = secp256k1_ecdsa_recover(secp256k1_context_static, &rpk, &rsig, hash.begin());
272272
assert(ret);
@@ -279,7 +279,7 @@ bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint2
279279
{
280280
assert(sig.size() == 64);
281281
secp256k1_keypair keypair;
282-
if (!secp256k1_keypair_create(secp256k1_context_sign, &keypair, begin())) return false;
282+
if (!secp256k1_keypair_create(secp256k1_context_sign, &keypair, UCharCast(begin()))) return false;
283283
if (merkle_root) {
284284
secp256k1_xonly_pubkey pubkey;
285285
if (!secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, &keypair)) return false;
@@ -324,7 +324,7 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
324324
BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
325325
} else {
326326
assert(size() == 32);
327-
BIP32Hash(cc, nChild, 0, begin(), vout.data());
327+
BIP32Hash(cc, nChild, 0, UCharCast(begin()), vout.data());
328328
}
329329
memcpy(ccChild.begin(), vout.data()+32, 32);
330330
keyChild.Set(begin(), begin() + 32, true);

src/key.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ class CKey
100100
{
101101
if (size_t(pend - pbegin) != std::tuple_size_v<KeyType>) {
102102
ClearKeyData();
103-
} else if (Check(&pbegin[0])) {
103+
} else if (Check(UCharCast(&pbegin[0]))) {
104104
MakeKeyData();
105105
memcpy(keydata->data(), (unsigned char*)&pbegin[0], keydata->size());
106106
fCompressed = fCompressedIn;
@@ -112,8 +112,8 @@ class CKey
112112
//! Simple read-only vector-like interface.
113113
unsigned int size() const { return keydata ? keydata->size() : 0; }
114114
const std::byte* data() const { return keydata ? reinterpret_cast<const std::byte*>(keydata->data()) : nullptr; }
115-
const unsigned char* begin() const { return keydata ? keydata->data() : nullptr; }
116-
const unsigned char* end() const { return begin() + size(); }
115+
const std::byte* begin() const { return data(); }
116+
const std::byte* end() const { return data() + size(); }
117117

118118
//! Check whether this private key is valid.
119119
bool IsValid() const { return !!keydata; }

src/key_io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ std::string EncodeSecret(const CKey& key)
228228
{
229229
assert(key.IsValid());
230230
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SECRET_KEY);
231-
data.insert(data.end(), key.begin(), key.end());
231+
data.insert(data.end(), UCharCast(key.begin()), UCharCast(key.end()));
232232
if (key.IsCompressed()) {
233233
data.push_back(1);
234234
}

src/pubkey.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,10 +285,11 @@ class XOnlyPubKey
285285
CPubKey GetEvenCorrespondingCPubKey() const;
286286

287287
const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
288-
const unsigned char* data() const { return m_keydata.begin(); }
289288
static constexpr size_t size() { return decltype(m_keydata)::size(); }
289+
const unsigned char* data() const { return m_keydata.begin(); }
290290
const unsigned char* begin() const { return m_keydata.begin(); }
291291
const unsigned char* end() const { return m_keydata.end(); }
292+
unsigned char* data() { return m_keydata.begin(); }
292293
unsigned char* begin() { return m_keydata.begin(); }
293294
unsigned char* end() { return m_keydata.end(); }
294295
bool operator==(const XOnlyPubKey& other) const { return m_keydata == other.m_keydata; }

src/wallet/scriptpubkeyman.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ bool LegacyScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, WalletBat
280280
{
281281
const CKey &key = mKey.second;
282282
CPubKey vchPubKey = key.GetPubKey();
283-
CKeyingMaterial vchSecret(key.begin(), key.end());
283+
CKeyingMaterial vchSecret{UCharCast(key.begin()), UCharCast(key.end())};
284284
std::vector<unsigned char> vchCryptedSecret;
285285
if (!EncryptSecret(master_key, vchSecret, vchPubKey.GetHash(), vchCryptedSecret)) {
286286
encrypted_batch = nullptr;
@@ -810,7 +810,7 @@ bool LegacyScriptPubKeyMan::AddKeyPubKeyInner(const CKey& key, const CPubKey &pu
810810
}
811811

812812
std::vector<unsigned char> vchCryptedSecret;
813-
CKeyingMaterial vchSecret(key.begin(), key.end());
813+
CKeyingMaterial vchSecret{UCharCast(key.begin()), UCharCast(key.end())};
814814
if (!EncryptSecret(m_storage.GetEncryptionKey(), vchSecret, pubkey.GetHash(), vchCryptedSecret)) {
815815
return false;
816816
}
@@ -2088,7 +2088,7 @@ bool DescriptorScriptPubKeyMan::Encrypt(const CKeyingMaterial& master_key, Walle
20882088
{
20892089
const CKey &key = key_in.second;
20902090
CPubKey pubkey = key.GetPubKey();
2091-
CKeyingMaterial secret(key.begin(), key.end());
2091+
CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
20922092
std::vector<unsigned char> crypted_secret;
20932093
if (!EncryptSecret(master_key, secret, pubkey.GetHash(), crypted_secret)) {
20942094
return false;
@@ -2261,7 +2261,7 @@ bool DescriptorScriptPubKeyMan::AddDescriptorKeyWithDB(WalletBatch& batch, const
22612261
}
22622262

22632263
std::vector<unsigned char> crypted_secret;
2264-
CKeyingMaterial secret(key.begin(), key.end());
2264+
CKeyingMaterial secret{UCharCast(key.begin()), UCharCast(key.end())};
22652265
if (!EncryptSecret(m_storage.GetEncryptionKey(), secret, pubkey.GetHash(), crypted_secret)) {
22662266
return false;
22672267
}

0 commit comments

Comments
 (0)