mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-27 17:41:44 +02:00
refactor: standardize obfuscation memory alignment
See: * https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2216962117 * https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2220277161 * https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2210851772 Co-authored-by: Russell Yanofsky <russ@yanofsky.org>
This commit is contained in:
@@ -36,21 +36,21 @@ public:
|
|||||||
|
|
||||||
KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
|
KeyType rot_key{m_rotations[key_offset % KEY_SIZE]}; // Continue obfuscation from where we left off
|
||||||
if (target.size() > KEY_SIZE) {
|
if (target.size() > KEY_SIZE) {
|
||||||
// Obfuscate until 64-bit alignment boundary
|
// Obfuscate until KEY_SIZE alignment boundary
|
||||||
if (const auto misalign{std::bit_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
|
if (const auto misalign{reinterpret_cast<uintptr_t>(target.data()) % KEY_SIZE}) {
|
||||||
const size_t alignment{std::min(KEY_SIZE - misalign, target.size())};
|
const size_t alignment{KEY_SIZE - misalign};
|
||||||
XorWord(target.first(alignment), rot_key);
|
XorWord(target.first(alignment), rot_key);
|
||||||
|
|
||||||
target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
|
target = {std::assume_aligned<KEY_SIZE>(target.data() + alignment), target.size() - alignment};
|
||||||
rot_key = m_rotations[(key_offset + alignment) % KEY_SIZE];
|
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 (constexpr auto unroll{8}; target.size() >= KEY_SIZE * unroll; target = target.subspan(KEY_SIZE * unroll)) {
|
||||||
for (size_t i{0}; i < unroll; ++i) {
|
for (size_t i{0}; i < unroll; ++i) {
|
||||||
XorWord(target.subspan(i * KEY_SIZE, KEY_SIZE), rot_key);
|
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)) {
|
for (; target.size() >= KEY_SIZE; target = target.subspan(KEY_SIZE)) {
|
||||||
XorWord(target.first<KEY_SIZE>(), rot_key);
|
XorWord(target.first<KEY_SIZE>(), rot_key);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user