refactor: move util::Xor to Obfuscation().Xor

This is meant to focus the usages to narrow the scope of the obfuscation optimization.

`Obfuscation::Xor` is mostly a move.

Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
This commit is contained in:
Lőrinc
2025-07-05 13:39:15 +02:00
parent fa5d296e3b
commit 377aab8e5a
5 changed files with 26 additions and 28 deletions

View File

@@ -6,11 +6,29 @@
#define BITCOIN_UTIL_OBFUSCATION_H
#include <cstdint>
#include <span.h>
class Obfuscation
{
public:
static constexpr size_t KEY_SIZE{sizeof(uint64_t)};
void Xor(std::span<std::byte> write, std::span<const std::byte> key, size_t key_offset = 0)
{
assert(key.size() == KEY_SIZE);
key_offset %= KEY_SIZE;
for (size_t i = 0, j = key_offset; i != write.size(); i++) {
write[i] ^= key[j++];
// This potentially acts on very many bytes of data, so it's
// important that we calculate `j`, i.e. the `key` index in this
// way instead of doing a %, which would effectively be a division
// for each byte Xor'd -- much slower than need be.
if (j == KEY_SIZE)
j = 0;
}
}
};
#endif // BITCOIN_UTIL_OBFUSCATION_H