Repository: bitcoin/bitcoin
File: src/util/obfuscation.h (C/C++)

Code before:
        SetRotations(ToKey(key_bytes));
    }

    operator bool() const { return m_rotations[0] != 0; }

    void operator()(std::span<std::byte> target, size_t key_offset = 0) const
    {
        if (!*this) return;

        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())};
                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
            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
            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
    {
        // 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)
    {
        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)));
    }


Review comment (suggestion):
nit:
```suggestion
            // Aligned obfuscation of remaining KEY_SIZE chunks
```

Code after:
        SetRotations(ToKey(key_bytes));
    }

    operator bool() const { return m_rotations[0] != 0; }

    void operator()(std::span<std::byte> target, size_t key_offset = 0) const
    {
        if (!*this) return;

        KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
        if (target.size() > KEY_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 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 KEY_SIZE chunks
            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
    {
        // 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)
    {
        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)));
    }