Merge bitcoin/bitcoin#28012: util: Allow FastRandomContext::randbytes for std::byte, Allow std::byte serialization

fac6af16f4 Allow std::byte serialization (MarcoFalke)
fade43edc4 Allow FastRandomContext::randbytes for all byte types (MarcoFalke)

Pull request description:

  I need this for some stuff, but it should also be useful by itself for other developers that need it.

ACKs for top commit:
  sipa:
    utACK fac6af16f4
  dergoegge:
    Code review ACK fac6af16f4

Tree-SHA512: db4b1bbd6bf6ef6503d59b0b4ed1681db8d935d2d10f8d89f071978ea59b49a1d319bccb4e9717c0c88a4908bbeca4fd0cbff6c655d8a443554fd14146fe16de
This commit is contained in:
fanquake 2023-07-07 10:14:54 +01:00
commit a9dde841e1
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
4 changed files with 14 additions and 6 deletions

View file

@ -589,15 +589,18 @@ uint256 FastRandomContext::rand256() noexcept
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();
std::vector<unsigned char> ret(len);
std::vector<B> ret(len);
if (len > 0) {
rng.Keystream(ret.data(), len);
rng.Keystream(UCharCast(ret.data()), len);
}
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)
{

View file

@ -211,7 +211,8 @@ public:
}
/** 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. */
void fillrand(Span<std::byte> output);

View file

@ -192,6 +192,7 @@ template<typename X> const X& ReadWriteAsHelper(const X& x) { return x; }
#ifndef CHAR_EQUALS_INT8
template <typename Stream> void Serialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
#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, uint8_t a ) { ser_writedata8(s, a); }
template<typename Stream> inline void Serialize(Stream& s, int16_t a ) { ser_writedata16(s, a); }
@ -207,6 +208,7 @@ template <typename Stream, typename B> void Serialize(Stream& s, Span<B> span) {
#ifndef CHAR_EQUALS_INT8
template <typename Stream> void Unserialize(Stream&, char) = delete; // char serialization forbidden. Use uint8_t or int8_t
#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, uint8_t& a ) { a = ser_readdata8(s); }
template<typename Stream> inline void Unserialize(Stream& s, int16_t& a ) { a = ser_readdata16(s); }

View file

@ -244,11 +244,13 @@ BOOST_AUTO_TEST_CASE(class_methods)
{
DataStream ds;
const std::string in{"ab"};
ds << Span{in};
ds << Span{in} << std::byte{'c'};
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(1), std::byte{'b'});
BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
}
}