Replace Decryptor CRTP base with a concept.
Chaos Ci / test (push) Successful in 3m4s
Chaos Ci / benchmark (push) Successful in 1m31s

This commit is contained in:
hashlag
2026-09-06 02:45:45 +03:00
parent 2c6a24197a
commit 3dacd57fef
3 changed files with 28 additions and 43 deletions
+16 -33
View File
@@ -1,44 +1,27 @@
#ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP #ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
#define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP #define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
#include <concepts>
#include <cstdint>
#include <type_traits>
namespace Chaos::Cipher::Block namespace Chaos::Cipher::Block
{ {
template<typename T> template<typename T>
class Decryptor concept Decryptor = requires(T decryptor,
typename T::Block block,
uint8_t * outBegin, uint8_t * outEnd,
uint8_t * inBegin, uint8_t * inEnd)
{ {
public: typename T::Block;
template<typename OutputIt, typename InputIt> typename T::Key;
void DecryptBlock(OutputIt outBegin, OutputIt outEnd, requires std::constructible_from<T, typename T::Key>;
InputIt inBegin, InputIt inEnd) const requires std::unsigned_integral<std::remove_cvref_t<decltype(T::BlockSize)>>;
{ requires std::unsigned_integral<std::remove_cvref_t<decltype(T::KeySize)>>;
Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd); decryptor.DecryptBlock(outBegin, outEnd, inBegin, inEnd);
} { decryptor.DecryptBlock(block) } -> std::same_as<typename T::Block>;
{ decryptor.GetBlockSize() } -> std::unsigned_integral;
template<typename Block>
auto DecryptBlock(Block block) const
{
return Impl().DecryptBlock(block);
}
auto GetBlockSize() const
{
return Impl().GetBlockSize();
}
protected:
Decryptor() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
}; };
} // namespace Chaos::Cipher::Block } // namespace Chaos::Cipher::Block
+3 -1
View File
@@ -281,7 +281,7 @@ public:
static_assert(Chaos::Cipher::Block::Encryptor<Encryptor>); static_assert(Chaos::Cipher::Block::Encryptor<Encryptor>);
class Decryptor : public Chaos::Cipher::Block::Decryptor<Decryptor> class Decryptor
{ {
public: public:
using Block = DesCrypt::Block; using Block = DesCrypt::Block;
@@ -327,6 +327,8 @@ public:
Inner_::KeySchedule Schedule_; Inner_::KeySchedule Schedule_;
}; };
static_assert(Chaos::Cipher::Block::Decryptor<Decryptor>);
private: private:
using BlockHalf = uint32_t; using BlockHalf = uint32_t;
using RawBlockArray = Service::SeArray<uint8_t, 8>; using RawBlockArray = Service::SeArray<uint8_t, 8>;
@@ -529,8 +529,8 @@ TEST(DesCryptTests, EncryptUInt64BlockGenericTest)
ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data)); ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data));
} }
template<typename Impl, typename InputIt> template<Decryptor DecryptorImpl, typename InputIt>
static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec, static std::vector<uint8_t> DecryptGeneric(const DecryptorImpl & dec,
InputIt begin, InputIt end) InputIt begin, InputIt end)
{ {
std::vector<uint8_t> result; std::vector<uint8_t> result;
@@ -540,7 +540,7 @@ static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
return result; return result;
} }
TEST(DesCryptTests, DecryptThroughBaseTest) TEST(DesCryptTests, DecryptGenericTest)
{ {
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
@@ -550,16 +550,16 @@ TEST(DesCryptTests, DecryptThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end())); ASSERT_EQ(expected, DecryptGeneric(dec, data.begin(), data.end()));
} }
template<typename Impl> template<Decryptor DecryptorImpl>
static uint64_t DecryptUInt64BlockThroughBase(const Decryptor<Impl> & dec, uint64_t block) static uint64_t DecryptUInt64BlockGeneric(const DecryptorImpl & dec, uint64_t block)
{ {
return dec.DecryptBlock(block); return dec.DecryptBlock(block);
} }
TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) TEST(DesCryptTests, DecryptUInt64BlockGenericTest)
{ {
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
@@ -569,5 +569,5 @@ TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data)); ASSERT_EQ(expected, DecryptUInt64BlockGeneric(dec, data));
} }