Draft
Conversation
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.
aa90395 to
f8dc78c
Compare
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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::byteandstd::span.Fix
Migrate the internal RNG plumbing (
MixExtract,ProcRand,GetStrongRandBytes) and key validation/storage (Check,KeyType) tostd::byte.Conversions to
unsigned char*are now pushed to the few C boundaries (e.g.libsecp256k1), while existingunsigned charbuffers at call sites are adapted viaMakeWritableByteSpan.A dedicated test commit adds coverage that exercises
GetStrongRandBytesoutput and verifiesCKey::Setaccepts bothunsigned charandstd::byteinput buffers (and rejects invalid key material), helping confirm the refactor is behavior-preserving.Continues the
std::byte/std::spanmodernization work in bitcoin#31524 and bitcoin#32743 and bitcoin#31519.