Skip to content

Commit 6aa0e70

Browse files
committed
Merge bitcoin/bitcoin#31524: refactor: Allow std::byte in Read(LE/BE)
fa83bec refactor: Allow std::byte in Read(LE/BE) (MarcoFalke) Pull request description: Starting with C++17, `std::byte` is often (not always) a better choice over `uint8_t` for new code. However, the existing codebase discourages the use of `std::byte`, when helpers such as `ReadLE32` are used. This is because calling code will be cluttered with byte-casts. Fix it by allowing `std::byte` pointers in `ReadLE32` (and friends). ACKs for top commit: sipa: utACK fa83bec fjahr: Code review ACK fa83bec theuni: utACK fa83bec l0rinc: ACK fa83bec Tree-SHA512: 83604dc9df9ad447ad1b6f81f1e1844554c2c5331fcb78bdba1300e050d9dcbe9cf7a1b2dd250772bb23a8bf02a4ec26441012fe2f4bcc670ef31c15151adb15
2 parents 604bf2e + fa83bec commit 6aa0e70

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

src/crypto/chacha20.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017-2022 The Bitcoin Core developers
1+
// Copyright (c) 2017-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -25,14 +25,14 @@
2525
void ChaCha20Aligned::SetKey(Span<const std::byte> key) noexcept
2626
{
2727
assert(key.size() == KEYLEN);
28-
input[0] = ReadLE32(UCharCast(key.data() + 0));
29-
input[1] = ReadLE32(UCharCast(key.data() + 4));
30-
input[2] = ReadLE32(UCharCast(key.data() + 8));
31-
input[3] = ReadLE32(UCharCast(key.data() + 12));
32-
input[4] = ReadLE32(UCharCast(key.data() + 16));
33-
input[5] = ReadLE32(UCharCast(key.data() + 20));
34-
input[6] = ReadLE32(UCharCast(key.data() + 24));
35-
input[7] = ReadLE32(UCharCast(key.data() + 28));
28+
input[0] = ReadLE32(key.data() + 0);
29+
input[1] = ReadLE32(key.data() + 4);
30+
input[2] = ReadLE32(key.data() + 8);
31+
input[3] = ReadLE32(key.data() + 12);
32+
input[4] = ReadLE32(key.data() + 16);
33+
input[5] = ReadLE32(key.data() + 20);
34+
input[6] = ReadLE32(key.data() + 24);
35+
input[7] = ReadLE32(key.data() + 28);
3636
input[8] = 0;
3737
input[9] = 0;
3838
input[10] = 0;

src/crypto/common.h

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2014-2020 The Bitcoin Core developers
1+
// Copyright (c) 2014-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -7,82 +7,99 @@
77

88
#include <compat/endian.h>
99

10+
#include <concepts>
11+
#include <cstddef>
1012
#include <cstdint>
1113
#include <cstring>
1214

13-
uint16_t static inline ReadLE16(const unsigned char* ptr)
15+
template <typename B>
16+
concept ByteType = std::same_as<B, unsigned char> || std::same_as<B, std::byte>;
17+
18+
template <ByteType B>
19+
inline uint16_t ReadLE16(const B* ptr)
1420
{
1521
uint16_t x;
1622
memcpy(&x, ptr, 2);
1723
return le16toh_internal(x);
1824
}
1925

20-
uint32_t static inline ReadLE32(const unsigned char* ptr)
26+
template <ByteType B>
27+
inline uint32_t ReadLE32(const B* ptr)
2128
{
2229
uint32_t x;
2330
memcpy(&x, ptr, 4);
2431
return le32toh_internal(x);
2532
}
2633

27-
uint64_t static inline ReadLE64(const unsigned char* ptr)
34+
template <ByteType B>
35+
inline uint64_t ReadLE64(const B* ptr)
2836
{
2937
uint64_t x;
3038
memcpy(&x, ptr, 8);
3139
return le64toh_internal(x);
3240
}
3341

34-
void static inline WriteLE16(unsigned char* ptr, uint16_t x)
42+
template <ByteType B>
43+
inline void WriteLE16(B* ptr, uint16_t x)
3544
{
3645
uint16_t v = htole16_internal(x);
3746
memcpy(ptr, &v, 2);
3847
}
3948

40-
void static inline WriteLE32(unsigned char* ptr, uint32_t x)
49+
template <ByteType B>
50+
inline void WriteLE32(B* ptr, uint32_t x)
4151
{
4252
uint32_t v = htole32_internal(x);
4353
memcpy(ptr, &v, 4);
4454
}
4555

46-
void static inline WriteLE64(unsigned char* ptr, uint64_t x)
56+
template <ByteType B>
57+
inline void WriteLE64(B* ptr, uint64_t x)
4758
{
4859
uint64_t v = htole64_internal(x);
4960
memcpy(ptr, &v, 8);
5061
}
5162

52-
uint16_t static inline ReadBE16(const unsigned char* ptr)
63+
template <ByteType B>
64+
inline uint16_t ReadBE16(const B* ptr)
5365
{
5466
uint16_t x;
5567
memcpy(&x, ptr, 2);
5668
return be16toh_internal(x);
5769
}
5870

59-
uint32_t static inline ReadBE32(const unsigned char* ptr)
71+
template <ByteType B>
72+
inline uint32_t ReadBE32(const B* ptr)
6073
{
6174
uint32_t x;
6275
memcpy(&x, ptr, 4);
6376
return be32toh_internal(x);
6477
}
6578

66-
uint64_t static inline ReadBE64(const unsigned char* ptr)
79+
template <ByteType B>
80+
inline uint64_t ReadBE64(const B* ptr)
6781
{
6882
uint64_t x;
6983
memcpy(&x, ptr, 8);
7084
return be64toh_internal(x);
7185
}
7286

73-
void static inline WriteBE16(unsigned char* ptr, uint16_t x)
87+
template <ByteType B>
88+
inline void WriteBE16(B* ptr, uint16_t x)
7489
{
7590
uint16_t v = htobe16_internal(x);
7691
memcpy(ptr, &v, 2);
7792
}
7893

79-
void static inline WriteBE32(unsigned char* ptr, uint32_t x)
94+
template <ByteType B>
95+
inline void WriteBE32(B* ptr, uint32_t x)
8096
{
8197
uint32_t v = htobe32_internal(x);
8298
memcpy(ptr, &v, 4);
8399
}
84100

85-
void static inline WriteBE64(unsigned char* ptr, uint64_t x)
101+
template <ByteType B>
102+
inline void WriteBE64(B* ptr, uint64_t x)
86103
{
87104
uint64_t v = htobe64_internal(x);
88105
memcpy(ptr, &v, 8);

0 commit comments

Comments
 (0)