Merge bitcoin/bitcoin#33039: refactor,test: follow-ups to multi-byte block obfuscation

86e3a0a8cb refactor: standardize obfuscation memory alignment (Lőrinc)
13f00345c0 refactor: write `Obfuscation` object when new key is generated in dbwrapper (Lőrinc)
e5b1b7c557 refactor: rename `OBFUSCATION_KEY_KEY` (Lőrinc)
298bf95105 refactor: simplify `Obfuscation::HexKey` (Lőrinc)
2dea045425 test: make `obfuscation_serialize` more thorough (Lőrinc)
a17d8202c3 test: merge xor_roundtrip_random_chunks and xor_bytes_reference (Lőrinc)

Pull request description:

  Follow up for https://github.com/bitcoin/bitcoin/pull/31144
  Applied the remaining comments in separate commits - except for the last one where I could group them.
  Please see the commit messages for more context.

ACKs for top commit:
  achow101:
    ACK 86e3a0a8cb
  ryanofsky:
    Code review ACK 86e3a0a8cb, just tweaking key write assert as suggested
  hodlinator:
    ACK 86e3a0a8cb

Tree-SHA512: 967510a141fbb57bf9d088d92b554cf2fffc2f6aa0eab756cbae3230f53e9b04ceebcc6fea5f3383c01ad41985ecde5b5686c64a771ca9deae3497b9b88c1c8b
This commit is contained in:
Ava Chow
2025-08-06 15:46:18 -07:00
4 changed files with 37 additions and 57 deletions

View File

@@ -36,21 +36,21 @@ public:
KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
if (target.size() > KEY_SIZE) {
// Obfuscate until 64-bit alignment boundary
if (const auto misalign{std::bit_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
const size_t alignment{std::min(KEY_SIZE - misalign, target.size())};
// Obfuscate until KEY_SIZE alignment boundary
if (const auto misalign{reinterpret_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
const size_t alignment{KEY_SIZE - misalign};
XorWord(target.first(alignment), rot_key);
target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];
}
// Aligned obfuscation in 64-byte chunks
// Aligned obfuscation in 8*KEY_SIZE chunks
for (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {
for (size_t i{0}; i < unroll; ++i) {
XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);
}
}
// Aligned obfuscation in 64-bit chunks
// Aligned obfuscation in KEY_SIZE chunks
for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
XorWord(target.first<KEY_SIZE>(), rot_key);
}
@@ -78,7 +78,7 @@ public:
std::string HexKey() const
{
return HexStr(std::bit_cast<std::array<uint8_t, KEY_SIZE>>(m_rotations[0]));
return HexStr(std::as_bytes(std::span{&m_rotations[0], 1}));
}
private: