mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-01 08:44:02 +02:00
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:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user