mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-25 05:51:08 +02:00
optimization: migrate fixed-size obfuscation from std::vector<std::byte>
to uint64_t
All former `std::vector<std::byte>` keys were replaced with `uint64_t` (we still serialize them as vectors but convert immediately to `uint64_t` on load). This is why some tests still generate vector keys and convert them to `uint64_t` later instead of generating them directly. In `Obfuscation::Unserialize` we can safely throw an `std::ios_base::failure` since during mempool fuzzing `mempool_persist.cpp#L141` catches and ignored these errors. > C++ compiler .......................... GNU 14.2.0 | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 0.04 | 28,365,698,819.44 | 0.0% | 0.34 | 0.13 | 2.714 | 0.07 | 0.0% | 5.33 | `ObfuscationBench` > C++ compiler .......................... Clang 20.1.7 | ns/byte | byte/s | err% | ins/byte | cyc/byte | IPC | bra/byte | miss% | total | benchmark |--------------------:|--------------------:|--------:|----------------:|----------------:|-------:|---------------:|--------:|----------:|:---------- | 0.08 | 13,012,464,203.00 | 0.0% | 0.65 | 0.28 | 2.338 | 0.13 | 0.8% | 5.50 | `ObfuscationBench` Co-authored-by: Hodlinator <172445034+hodlinator@users.noreply.github.com> Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
@@ -10,6 +10,9 @@
|
||||
#include <tinyformat.h>
|
||||
#include <util/strencodings.h>
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <climits>
|
||||
#include <ios>
|
||||
|
||||
class Obfuscation
|
||||
@@ -18,58 +21,77 @@ public:
|
||||
using KeyType = uint64_t;
|
||||
static constexpr size_t KEY_SIZE{sizeof(KeyType)};
|
||||
|
||||
Obfuscation() : m_key{KEY_SIZE, std::byte{0}} {}
|
||||
Obfuscation() { SetRotations(0); }
|
||||
explicit Obfuscation(std::span<const std::byte, KEY_SIZE> key_bytes)
|
||||
{
|
||||
m_key = {key_bytes.begin(), key_bytes.end()};
|
||||
SetRotations(ToKey(key_bytes));
|
||||
}
|
||||
|
||||
operator bool() const { return ToKey() != 0; }
|
||||
operator bool() const { return m_rotations[0] != 0; }
|
||||
|
||||
void operator()(std::span<std::byte> write, size_t key_offset = 0) const
|
||||
void operator()(std::span<std::byte> target, size_t key_offset = 0) const
|
||||
{
|
||||
assert(m_key.size() == KEY_SIZE);
|
||||
key_offset %= KEY_SIZE;
|
||||
if (!*this) return;
|
||||
|
||||
for (size_t i = 0, j = key_offset; i != write.size(); i++) {
|
||||
write[i] ^= m_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;
|
||||
const KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
|
||||
for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
|
||||
XorWord(target.first<KEY_SIZE>(), rot_key);
|
||||
}
|
||||
XorWord(target, rot_key);
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
void Serialize(Stream& s) const
|
||||
{
|
||||
s << m_key;
|
||||
// Use vector serialization for convenient compact size prefix.
|
||||
std::vector<std::byte> bytes{KEY_SIZE};
|
||||
std::memcpy(bytes.data(), &m_rotations[0], KEY_SIZE);
|
||||
s << bytes;
|
||||
}
|
||||
|
||||
template <typename Stream>
|
||||
void Unserialize(Stream& s)
|
||||
{
|
||||
s >> m_key;
|
||||
if (m_key.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
|
||||
std::vector<std::byte> bytes{KEY_SIZE};
|
||||
s >> bytes;
|
||||
if (bytes.size() != KEY_SIZE) throw std::ios_base::failure(strprintf("Obfuscation key size should be exactly %s bytes long", KEY_SIZE));
|
||||
SetRotations(ToKey(std::span<std::byte, KEY_SIZE>(bytes)));
|
||||
}
|
||||
|
||||
std::string HexKey() const
|
||||
{
|
||||
return HexStr(m_key);
|
||||
return HexStr(std::bit_cast<std::array<uint8_t, KEY_SIZE>>(m_rotations[0]));
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::byte> m_key;
|
||||
// Cached key rotations for different offsets.
|
||||
std::array<KeyType, KEY_SIZE> m_rotations;
|
||||
|
||||
KeyType ToKey() const
|
||||
void SetRotations(KeyType key)
|
||||
{
|
||||
for (size_t i{0}; i < KEY_SIZE; ++i) {
|
||||
int key_rotation_bits{int(CHAR_BIT * i)};
|
||||
if constexpr (std::endian::native == std::endian::big) key_rotation_bits *= -1;
|
||||
m_rotations[i] = std::rotr(key, key_rotation_bits);
|
||||
}
|
||||
}
|
||||
|
||||
static KeyType ToKey(std::span<const std::byte, KEY_SIZE> key_span)
|
||||
{
|
||||
KeyType key{};
|
||||
std::memcpy(&key, m_key.data(), KEY_SIZE);
|
||||
std::memcpy(&key, key_span.data(), KEY_SIZE);
|
||||
return key;
|
||||
}
|
||||
|
||||
static void XorWord(std::span<std::byte> target, KeyType key)
|
||||
{
|
||||
assert(target.size() <= KEY_SIZE);
|
||||
if (target.empty()) return;
|
||||
KeyType raw{};
|
||||
std::memcpy(&raw, target.data(), target.size());
|
||||
raw ^= key;
|
||||
std::memcpy(target.data(), &raw, target.size());
|
||||
}
|
||||
};
|
||||
|
||||
#endif // BITCOIN_UTIL_OBFUSCATION_H
|
||||
|
Reference in New Issue
Block a user