From 3dacd57fef88563c8e61e2645fe38bbfe4e9d071 Mon Sep 17 00:00:00 2001 From: hashlag Date: Sun, 6 Sep 2026 02:45:45 +0300 Subject: [PATCH] Replace Decryptor CRTP base with a concept. --- Chaos/Cipher/Block/Decryptor.hpp | 49 ++++++------------- Chaos/Cipher/Block/Des/DesCrypt.hpp | 4 +- ChaosTests/Cipher/Block/Des/DesCryptTests.cpp | 18 +++---- 3 files changed, 28 insertions(+), 43 deletions(-) diff --git a/Chaos/Cipher/Block/Decryptor.hpp b/Chaos/Cipher/Block/Decryptor.hpp index e27c130..d3eeb23 100644 --- a/Chaos/Cipher/Block/Decryptor.hpp +++ b/Chaos/Cipher/Block/Decryptor.hpp @@ -1,44 +1,27 @@ #ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP #define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP +#include +#include +#include + namespace Chaos::Cipher::Block { template -class Decryptor +concept Decryptor = requires(T decryptor, + typename T::Block block, + uint8_t * outBegin, uint8_t * outEnd, + uint8_t * inBegin, uint8_t * inEnd) { -public: - template - void DecryptBlock(OutputIt outBegin, OutputIt outEnd, - InputIt inBegin, InputIt inEnd) const - { - Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd); - } - - template - 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(*this); - } - - T & Impl() - { - return static_cast(*this); - } + typename T::Block; + typename T::Key; + requires std::constructible_from; + requires std::unsigned_integral>; + requires std::unsigned_integral>; + decryptor.DecryptBlock(outBegin, outEnd, inBegin, inEnd); + { decryptor.DecryptBlock(block) } -> std::same_as; + { decryptor.GetBlockSize() } -> std::unsigned_integral; }; } // namespace Chaos::Cipher::Block diff --git a/Chaos/Cipher/Block/Des/DesCrypt.hpp b/Chaos/Cipher/Block/Des/DesCrypt.hpp index b6d72db..de6119f 100644 --- a/Chaos/Cipher/Block/Des/DesCrypt.hpp +++ b/Chaos/Cipher/Block/Des/DesCrypt.hpp @@ -281,7 +281,7 @@ public: static_assert(Chaos::Cipher::Block::Encryptor); - class Decryptor : public Chaos::Cipher::Block::Decryptor + class Decryptor { public: using Block = DesCrypt::Block; @@ -327,6 +327,8 @@ public: Inner_::KeySchedule Schedule_; }; + static_assert(Chaos::Cipher::Block::Decryptor); + private: using BlockHalf = uint32_t; using RawBlockArray = Service::SeArray; diff --git a/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp b/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp index d525671..7d9b86b 100644 --- a/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp +++ b/ChaosTests/Cipher/Block/Des/DesCryptTests.cpp @@ -529,9 +529,9 @@ TEST(DesCryptTests, EncryptUInt64BlockGenericTest) ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data)); } -template -static std::vector DecryptThroughBase(const Decryptor & dec, - InputIt begin, InputIt end) +template +static std::vector DecryptGeneric(const DecryptorImpl & dec, + InputIt begin, InputIt end) { std::vector result; result.resize(dec.GetBlockSize(), 0); @@ -540,7 +540,7 @@ static std::vector DecryptThroughBase(const Decryptor & dec, return result; } -TEST(DesCryptTests, DecryptThroughBaseTest) +TEST(DesCryptTests, DecryptGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -550,16 +550,16 @@ TEST(DesCryptTests, DecryptThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Decryptor dec(desKey); - ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end())); + ASSERT_EQ(expected, DecryptGeneric(dec, data.begin(), data.end())); } -template -static uint64_t DecryptUInt64BlockThroughBase(const Decryptor & dec, uint64_t block) +template +static uint64_t DecryptUInt64BlockGeneric(const DecryptorImpl & dec, uint64_t block) { return dec.DecryptBlock(block); } -TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) +TEST(DesCryptTests, DecryptUInt64BlockGenericTest) { std::array key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 }; @@ -569,5 +569,5 @@ TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest) DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Decryptor dec(desKey); - ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data)); + ASSERT_EQ(expected, DecryptUInt64BlockGeneric(dec, data)); }