Replace Encryptor CRTP base with a concept.
Chaos Ci / test (push) Successful in 5m6s
Chaos Ci / benchmark (push) Successful in 1m29s

This commit is contained in:
hashlag
2026-09-06 02:27:35 +03:00
parent f0228877b4
commit 2c6a24197a
3 changed files with 28 additions and 43 deletions
+3 -1
View File
@@ -233,7 +233,7 @@ public:
Inner_::RawKey Key_; Inner_::RawKey Key_;
}; };
class Encryptor : public Chaos::Cipher::Block::Encryptor<Encryptor> class Encryptor
{ {
public: public:
using Block = DesCrypt::Block; using Block = DesCrypt::Block;
@@ -279,6 +279,8 @@ public:
Inner_::KeySchedule Schedule_; Inner_::KeySchedule Schedule_;
}; };
static_assert(Chaos::Cipher::Block::Encryptor<Encryptor>);
class Decryptor : public Chaos::Cipher::Block::Decryptor<Decryptor> class Decryptor : public Chaos::Cipher::Block::Decryptor<Decryptor>
{ {
public: public:
+16 -33
View File
@@ -1,44 +1,27 @@
#ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP #ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
#define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP #define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
#include <concepts>
#include <cstdint>
#include <type_traits>
namespace Chaos::Cipher::Block namespace Chaos::Cipher::Block
{ {
template<typename T> template<typename T>
class Encryptor concept Encryptor = requires(T encryptor,
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 EncryptBlock(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().EncryptBlock(outBegin, outEnd, inBegin, inEnd); encryptor.EncryptBlock(outBegin, outEnd, inBegin, inEnd);
} { encryptor.EncryptBlock(block) } -> std::same_as<typename T::Block>;
{ encryptor.GetBlockSize() } -> std::unsigned_integral;
template<typename Block>
auto EncryptBlock(Block block) const
{
return Impl().EncryptBlock(block);
}
auto GetBlockSize() const
{
return Impl().GetBlockSize();
}
protected:
Encryptor() = 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
@@ -486,8 +486,8 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
} }
} }
template<typename Impl, typename InputIt> template<Encryptor EncryptorImpl, typename InputIt>
static std::vector<uint8_t> EncryptThroughBase(const Encryptor<Impl> & enc, static std::vector<uint8_t> EncryptGeneric(const EncryptorImpl & enc,
InputIt begin, InputIt end) InputIt begin, InputIt end)
{ {
std::vector<uint8_t> result; std::vector<uint8_t> result;
@@ -497,7 +497,7 @@ static std::vector<uint8_t> EncryptThroughBase(const Encryptor<Impl> & enc,
return result; return result;
} }
TEST(DesCryptTests, EncryptThroughBaseTest) TEST(DesCryptTests, EncryptGenericTest)
{ {
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 };
@@ -507,16 +507,16 @@ TEST(DesCryptTests, EncryptThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Encryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end())); ASSERT_EQ(expected, EncryptGeneric(enc, data.begin(), data.end()));
} }
template<typename Impl> template<Encryptor EncryptorImpl>
static uint64_t EncryptUInt64BlockThroughBase(const Encryptor<Impl> & enc, uint64_t block) static uint64_t EncryptUInt64BlockGeneric(const EncryptorImpl & enc, uint64_t block)
{ {
return enc.EncryptBlock(block); return enc.EncryptBlock(block);
} }
TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest) TEST(DesCryptTests, EncryptUInt64BlockGenericTest)
{ {
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 };
@@ -526,7 +526,7 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Encryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data));
} }
template<typename Impl, typename InputIt> template<typename Impl, typename InputIt>