Skip to content

Commit f8dc78c

Browse files
committed
key: store CKey key material as std::byte
`CKey` already exposes its private key data as `std::byte` through `data()`/`begin()`/`end()`, but internally stored it as `unsigned char`. Store the key material as `std::byte` end-to-end, and only cast to `unsigned char*` at libsecp256k1 call boundaries. This avoids repeated `reinterpret_cast` and makes the code consistently use C++20’s byte type for opaque key data.
1 parent 5763ee8 commit f8dc78c

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

src/key.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ bool CKey::Check(const std::byte* vch) { return Check(UCharCast(vch)); }
162162
void CKey::MakeNewKey(bool fCompressedIn) {
163163
MakeKeyData();
164164
do {
165-
GetStrongRandBytes(MakeWritableByteSpan(*keydata));
165+
GetStrongRandBytes(*keydata);
166166
} while (!Check(begin()));
167167
fCompressed = fCompressedIn;
168168
}
@@ -317,7 +317,7 @@ EllSwiftPubKey CKey::EllSwiftCreate(std::span<const std::byte> ent32) const
317317

318318
auto success = secp256k1_ellswift_create(secp256k1_context_sign,
319319
UCharCast(encoded_pubkey.data()),
320-
keydata->data(),
320+
UCharCast(begin()),
321321
UCharCast(ent32.data()));
322322

323323
// Should always succeed for valid keys (asserted above).
@@ -336,7 +336,7 @@ ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, c
336336
UCharCast(output.data()),
337337
UCharCast(initiating ? our_ellswift.data() : their_ellswift.data()),
338338
UCharCast(initiating ? their_ellswift.data() : our_ellswift.data()),
339-
keydata->data(),
339+
UCharCast(begin()),
340340
initiating ? 0 : 1,
341341
secp256k1_ellswift_xdh_hash_function_bip324,
342342
nullptr);

src/key.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class CKey
5050

5151
private:
5252
/** Internal data container for private key material. */
53-
using KeyType = std::array<unsigned char, 32>;
53+
using KeyType = std::array<std::byte, 32>;
5454

5555
//! Whether the public key corresponding to this private key is (to be) compressed.
5656
bool fCompressed{false};
@@ -117,7 +117,7 @@ class CKey
117117

118118
//! Simple read-only vector-like interface.
119119
unsigned int size() const { return keydata ? keydata->size() : 0; }
120-
const std::byte* data() const { return keydata ? reinterpret_cast<const std::byte*>(keydata->data()) : nullptr; }
120+
const std::byte* data() const { return keydata ? keydata->data() : nullptr; }
121121
const std::byte* begin() const { return data(); }
122122
const std::byte* end() const { return data() + size(); }
123123

0 commit comments

Comments
 (0)