Skip to content

key: migrate CKey key data to std::byte#101

Draft
l0rinc wants to merge 6 commits intomasterfrom
l0rinc/ckey-std-byte-migration
Draft

key: migrate CKey key data to std::byte#101
l0rinc wants to merge 6 commits intomasterfrom
l0rinc/ckey-std-byte-migration

Conversation

@l0rinc
Copy link
Copy Markdown
Owner

@l0rinc l0rinc commented Jan 17, 2026

Problem

Several internal interfaces in the RNG and key code represent opaque byte buffers as unsigned char (and sometimes as pointer+length), even when the values are not meant to be treated as an arithmetic type.
This obscures intent, makes it easier for new code to accidentally perform arithmetic on raw bytes, and forces repeated casting at call sites as more interfaces move toward std::byte and std::span.

Fix

Migrate the internal RNG plumbing (MixExtract, ProcRand, GetStrongRandBytes) and key validation/storage (Check, KeyType) to std::byte.

Conversions to unsigned char* are now pushed to the few C boundaries (e.g. libsecp256k1), while existing unsigned char buffers at call sites are adapted via MakeWritableByteSpan.
A dedicated test commit adds coverage that exercises GetStrongRandBytes output and verifies CKey::Set accepts both unsigned char and std::byte input buffers (and rejects invalid key material), helping confirm the refactor is behavior-preserving.

Continues the std::byte/std::span modernization work in bitcoin#31524 and bitcoin#32743 and bitcoin#31519.

Add unit test coverage for the byte-oriented refactors that follow.

The tests exercise GetStrongRandBytes output and verify that CKey::Set accepts both unsigned char and std::byte input buffers (and rejects obviously invalid key material).
`RNGState::MixExtract` returns raw bytes and was previously expressed as `unsigned char*` plus a separate length.

Use `std::span<std::byte>` to make the byte-oriented intent explicit and keep pointer/length handling localized to callers that still work with `unsigned char` buffers.
`ProcRand` is an internal helper that optionally returns up to 32 random bytes.

Switch it from `unsigned char*` + length to `std::span<std::byte>` now that `RNGState::MixExtract` also takes a byte span. This removes redundant pointer/size plumbing and keeps the internal RNG APIs consistently byte-oriented.
`GetStrongRandBytes` produces raw byte output, but historically used `unsigned char` (an arithmetic type) as its element type.

Switch it to `std::span<std::byte>` and adjust callers to pass `MakeWritableByteSpan(...)` when the underlying storage is still `unsigned char`. This keeps the RNG API byte-oriented and supports follow-up `std::byte` migrations with smaller diffs.
`CKey::Check` validates raw private key bytes and does not treat them as an arithmetic type.

Switch its argument from `unsigned char*` to `std::byte*`, using span helpers at the call sites and `UCharCast` only at the libsecp256k1 boundary. This keeps the internal key API byte-oriented ahead of storing 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.
@l0rinc l0rinc force-pushed the l0rinc/ckey-std-byte-migration branch from aa90395 to f8dc78c Compare January 18, 2026 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant