merge bitcoin#28012: Allow FastRandomContext::randbytes for std::byte, Allow std::byte serialization

This commit is contained in:
Kittywhiskers Van Gogh 2023-06-30 11:30:59 +02:00 committed by pasta
parent 4eeafa267c
commit 2b26a87874
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
4 changed files with 14 additions and 6 deletions

View File

@ -630,15 +630,18 @@ uint256 FastRandomContext::rand256() noexcept
return ret; return ret;
} }
std::vector<unsigned char> FastRandomContext::randbytes(size_t len) template <typename B>
std::vector<B> FastRandomContext::randbytes(size_t len)
{ {
if (requires_seed) RandomSeed(); if (requires_seed) RandomSeed();
std::vector<unsigned char> ret(len); std::vector<B> ret(len);
if (len > 0) { if (len > 0) {
rng.Keystream(ret.data(), len); rng.Keystream(UCharCast(ret.data()), len);
} }
return ret; return ret;
} }
template std::vector<unsigned char> FastRandomContext::randbytes(size_t);
template std::vector<std::byte> FastRandomContext::randbytes(size_t);
void FastRandomContext::fillrand(Span<std::byte> output) void FastRandomContext::fillrand(Span<std::byte> output)
{ {

View File

@ -200,7 +200,8 @@ public:
} }
/** Generate random bytes. */ /** Generate random bytes. */
std::vector<unsigned char> randbytes(size_t len); template <typename B = unsigned char>
std::vector<B> randbytes(size_t len);
/** Fill a byte Span with random bytes. */ /** Fill a byte Span with random bytes. */
void fillrand(Span<std::byte> output); void fillrand(Span<std::byte> output);

View File

@ -196,6 +196,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
#ifndef CHAR_EQUALS_INT8 #ifndef CHAR_EQUALS_INT8
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
#endif #endif
template <typename Stream> void Serialize(Stream& s, std::byte a) { ser_writedata8(s, uint8_t(a)); }
template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); } template<typename Stream> inline void Serialize(Stream& s, int8_t a ) { ser_writedata8(s, a); }
template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); } template<typename Stream> inline void Serialize(Stream& s, uint8_t a ) { ser_writedata8(s, a); }
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); } template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
@ -211,6 +212,7 @@ template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) {
#ifndef CHAR_EQUALS_INT8 #ifndef CHAR_EQUALS_INT8
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
#endif #endif
template <typename Stream> void Unserialize(Stream& s, std::byte& a) { a = std::byte{ser_readdata8(s)}; }
template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); } template<typename Stream> inline void Unserialize(Stream& s, int8_t& a ) { a = ser_readdata8(s); }
template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); } template<typename Stream> inline void Unserialize(Stream& s, uint8_t& a ) { a = ser_readdata8(s); }
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); } template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }

View File

@ -295,11 +295,13 @@ BOOST_AUTO_TEST_CASE(class_methods)
{ {
DataStream ds; DataStream ds;
const std::string in{"ab"}; const std::string in{"ab"};
ds << Span{in}; ds << Span{in} << std::byte{'c'};
std::array<std::byte, 2> out; std::array<std::byte, 2> out;
ds >> Span{out}; std::byte out_3;
ds >> Span{out} >> out_3;
BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'}); BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'}); BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
} }
} }