Templatified block class

This commit is contained in:
Leonetienne 2022-05-25 16:38:16 +02:00
parent 21cbd80488
commit f7d8093668
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
2 changed files with 183 additions and 123 deletions

View File

@ -5,24 +5,26 @@
#include <array> #include <array>
#include <string> #include <string>
#include <ostream> #include <ostream>
#include <bitset>
namespace Leonetienne::GCrypt { namespace Leonetienne::GCrypt {
/* This class represents a block of data, /* This class represents a block of data,
* and provides functions to manipulate it * and provides functions to manipulate it
*/ */
class Block { template <typename T>
class Basic_Block {
public: public:
//! Will constuct an uninitialized data block //! Will constuct an uninitialized data block
Block(); Basic_Block();
//! Will construct this block from a string like "101010".. Length MUST be 512. //! Will construct this block from a string like "101010".. Length MUST be 512.
Block(const std::string& other); Basic_Block(const std::string& other);
//! Copy-ctor //! Copy-ctor
Block(const Block& other); Basic_Block(const Basic_Block<T>& other);
~Block(); ~Basic_Block();
//! Will construct this block from a string like "011101..". Length MUST be 512. //! Will construct this block from a string like "011101..". Length MUST be 512.
void FromString(const std::string& str); void FromString(const std::string& str);
@ -35,90 +37,90 @@ namespace Leonetienne::GCrypt {
//! Since the matrices values are pretty much sudo-random, //! Since the matrices values are pretty much sudo-random,
//! they will most likely integer-overflow. //! they will most likely integer-overflow.
//! So see this as a one-way function. //! So see this as a one-way function.
[[nodiscard]] Block MMul(const Block& other) const; [[nodiscard]] Basic_Block<T> MMul(const Basic_Block<T>& other) const;
[[nodiscard]] Block operator*(const Block& other) const; [[nodiscard]] Basic_Block<T> operator*(const Basic_Block<T>& other) const;
//! Will matrix-multiply two blocks together, //! Will matrix-multiply two blocks together,
//! and directly write into this same block. //! and directly write into this same block.
//! Since the matrices values are pretty much sudo-random, //! Since the matrices values are pretty much sudo-random,
//! they will most likely integer-overflow. //! they will most likely integer-overflow.
//! So see this as a one-way function. //! So see this as a one-way function.
void MMulInplace(const Block& other); void MMulInplace(const Basic_Block<T>& other);
Block& operator*=(const Block& other); Basic_Block<T>& operator*=(const Basic_Block<T>& other);
//! Will xor two blocks together //! Will xor two blocks together
[[nodiscard]] Block Xor(const Block& other) const; [[nodiscard]] Basic_Block<T> Xor(const Basic_Block<T>& other) const;
//! Will xor two blocks together //! Will xor two blocks together
[[nodiscard]] Block operator^(const Block& other) const; [[nodiscard]] Basic_Block<T> operator^(const Basic_Block<T>& other) const;
//! Will xor two blocks together, inplace //! Will xor two blocks together, inplace
void XorInplace(const Block& other); void XorInplace(const Basic_Block<T>& other);
//! Will xor two blocks together, inplace //! Will xor two blocks together, inplace
Block& operator^=(const Block& other); Basic_Block<T>& operator^=(const Basic_Block<T>& other);
//! Will add all the integer making up this block, one by one //! Will add all the integer making up this block, one by one
[[nodiscard]] Block Add(const Block& other) const; [[nodiscard]] Basic_Block<T> Add(const Basic_Block<T>& other) const;
//! Will add all the integer making up this block, one by one //! Will add all the integer making up this block, one by one
[[nodiscard]] Block operator+(const Block& other) const; [[nodiscard]] Basic_Block<T> operator+(const Basic_Block<T>& other) const;
//! Will add all the integer making up this block, one by one, inplace //! Will add all the integer making up this block, one by one, inplace
void AddInplace(const Block& other); void AddInplace(const Basic_Block<T>& other);
//! Will add all the integer making up this block, one by one, inplace //! Will add all the integer making up this block, one by one, inplace
Block& operator+=(const Block& other); Basic_Block<T>& operator+=(const Basic_Block<T>& other);
//! Will subtract all the integer making up this block, one by one //! Will subtract all the integer making up this block, one by one
[[nodiscard]] Block Sub(const Block& other) const; [[nodiscard]] Basic_Block<T> Sub(const Basic_Block<T>& other) const;
//! Will subtract all the integer making up this block, one by one //! Will subtract all the integer making up this block, one by one
[[nodiscard]] Block operator-(const Block& other) const; [[nodiscard]] Basic_Block<T> operator-(const Basic_Block<T>& other) const;
//! Will subtract all the integer making up this block, one by one, inplace //! Will subtract all the integer making up this block, one by one, inplace
void SubInplace(const Block& other); void SubInplace(const Basic_Block<T>& other);
//! Will subtract all the integer making up this block, one by one, inplace //! Will subtract all the integer making up this block, one by one, inplace
Block& operator-=(const Block& other); Basic_Block<T>& operator-=(const Basic_Block<T>& other);
//! Will shift rows upwards by 1 //! Will shift rows upwards by 1
[[nodiscard]] Block ShiftRowsUp() const; [[nodiscard]] Basic_Block<T> ShiftRowsUp() const;
//! Will shift rows upwards by 1 //! Will shift rows upwards by 1
void ShiftRowsUpInplace(); void ShiftRowsUpInplace();
//! Will shift matrix rows downwards by 1 //! Will shift matrix rows downwards by 1
[[nodiscard]] Block ShiftRowsDown() const; [[nodiscard]] Basic_Block<T> ShiftRowsDown() const;
//! Will shift matrix rows downwards by 1 //! Will shift matrix rows downwards by 1
void ShiftRowsDownInplace(); void ShiftRowsDownInplace();
//! Will shift matrix columns to the left by 1 //! Will shift matrix columns to the left by 1
[[nodiscard]] Block ShiftColumnsLeft() const; [[nodiscard]] Basic_Block<T> ShiftColumnsLeft() const;
//! Will shift matrix columns to the left by 1 //! Will shift matrix columns to the left by 1
void ShiftColumnsLeftInplace(); void ShiftColumnsLeftInplace();
//! Will shift matrix columns to the right by 1 //! Will shift matrix columns to the right by 1
[[nodiscard]] Block ShiftColumnsRight() const; [[nodiscard]] Basic_Block<T> ShiftColumnsRight() const;
//! Will shift matrix columns to the right by 1 //! Will shift matrix columns to the right by 1
void ShiftColumnsRightInplace(); void ShiftColumnsRightInplace();
//! Will shift array cells to the left by 1 //! Will shift array cells to the left by 1
[[nodiscard]] Block ShiftCellsLeft() const; [[nodiscard]] Basic_Block<T> ShiftCellsLeft() const;
//! Will shift array cells to the left by 1 //! Will shift array cells to the left by 1
void ShiftCellsLeftInplace(); void ShiftCellsLeftInplace();
//! Will shift array cells to the right by 1 //! Will shift array cells to the right by 1
[[nodiscard]] Block ShiftCellsRight() const; [[nodiscard]] Basic_Block<T> ShiftCellsRight() const;
//! Will shift array cells to the right by 1 //! Will shift array cells to the right by 1
void ShiftCellsRightInplace(); void ShiftCellsRightInplace();
//! Will copy a block //! Will copy a block
Block& operator=(const Block& other); Basic_Block<T>& operator=(const Basic_Block<T>& other);
//! Will compare whether or not two blocks are equal //! Will compare whether or not two blocks are equal
[[nodiscard]] bool operator==(const Block& other) const; [[nodiscard]] bool operator==(const Basic_Block<T>& other) const;
//! Will compare whether or not two blocks are unequal //! Will compare whether or not two blocks are unequal
[[nodiscard]] bool operator!=(const Block& other) const; [[nodiscard]] bool operator!=(const Basic_Block<T>& other) const;
//! Will zero all data //! Will zero all data
void Reset(); void Reset();
@ -133,46 +135,59 @@ namespace Leonetienne::GCrypt {
void FlipBit(const std::size_t index); void FlipBit(const std::size_t index);
//! Will shift all bits to the left by 1 //! Will shift all bits to the left by 1
[[nodiscard]] Block ShiftBitsLeft() const; [[nodiscard]] Basic_Block<T> ShiftBitsLeft() const;
//! Will shift all bits to the left by 1, inplace //! Will shift all bits to the left by 1, inplace
void ShiftBitsLeftInplace(); void ShiftBitsLeftInplace();
//! Will shift all bits to the right by 1 //! Will shift all bits to the right by 1
[[nodiscard]] Block ShiftBitsRight() const; [[nodiscard]] Basic_Block<T> ShiftBitsRight() const;
//! Will shift all bits to the right by 1, inplace //! Will shift all bits to the right by 1, inplace
void ShiftBitsRightInplace(); void ShiftBitsRightInplace();
//! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3) //! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3)
[[nodiscard]] std::uint32_t& Get(const std::uint8_t row, const std::uint8_t column); [[nodiscard]] T& Get(const std::uint8_t row, const std::uint8_t column);
//! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3) //! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3)
[[nodiscard]] const std::uint32_t& Get(const std::uint8_t row, const std::uint8_t column) const; [[nodiscard]] const T& Get(const std::uint8_t row, const std::uint8_t column) const;
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16) //! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
[[nodiscard]] std::uint32_t& Get(const std::uint8_t index); [[nodiscard]] T& Get(const std::uint8_t index);
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16) //! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
[[nodiscard]] const std::uint32_t& Get(const std::uint8_t index) const; [[nodiscard]] const T& Get(const std::uint8_t index) const;
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16) //! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
[[nodiscard]] std::uint32_t& operator[](const std::uint8_t index); [[nodiscard]] T& operator[](const std::uint8_t index);
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16) //! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
[[nodiscard]] const std::uint32_t& operator[](const std::uint8_t index) const; [[nodiscard]] const T& operator[](const std::uint8_t index) const;
static constexpr std::size_t CHUNK_SIZE = sizeof(std::uint32_t); static constexpr std::size_t CHUNK_SIZE = sizeof(T);
static constexpr std::size_t CHUNK_SIZE_BITS = CHUNK_SIZE * 8; static constexpr std::size_t CHUNK_SIZE_BITS = CHUNK_SIZE * 8;
friend std::ostream& operator<<(std::ostream& os, const Block& b); friend std::ostream& operator<<(std::ostream& os, const Basic_Block<T>& b) {
for (std::size_t i = 0; i < b.data.size(); i++) {
os << std::bitset<Basic_Block<T>::CHUNK_SIZE_BITS>(b.data[i]).to_string();
}
return os;
}
private: private:
std::array<std::uint32_t, 16> data; std::array<T, 16> data;
}; };
} // Instantiate templates
template class Basic_Block<std::uint32_t>;
//template class Basic_Block<std::uint16_t>;
//! This a full-sizes 512-bit block
typedef Basic_Block<std::uint32_t> Block;
//! This is a half-block used within the feistel class
//typedef Basic_Block<std::uint16_t> Halfblock;
}
#endif #endif

View File

@ -2,7 +2,6 @@
#include "GCrypt/Config.h" #include "GCrypt/Config.h"
#include "GCrypt/Util.h" #include "GCrypt/Util.h"
#include <sstream> #include <sstream>
#include <bitset>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
@ -12,18 +11,22 @@
namespace Leonetienne::GCrypt { namespace Leonetienne::GCrypt {
Block::Block() { template <typename T>
Basic_Block<T>::Basic_Block() {
} }
Block::Block(const std::string& str) { template <typename T>
Basic_Block<T>::Basic_Block(const std::string& str) {
FromString(str); FromString(str);
} }
Block::Block(const Block& other) { template <typename T>
Basic_Block<T>::Basic_Block(const Basic_Block<T>& other) {
data = other.data; data = other.data;
} }
void Block::FromString(const std::string& str) { template <typename T>
void Basic_Block<T>::FromString(const std::string& str) {
assert(str.length() == BLOCK_SIZE); assert(str.length() == BLOCK_SIZE);
@ -36,7 +39,8 @@ namespace Leonetienne::GCrypt {
return; return;
} }
std::string Block::ToString() const { template <typename T>
std::string Basic_Block<T>::ToString() const {
std::stringstream ss; std::stringstream ss;
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
@ -45,9 +49,10 @@ namespace Leonetienne::GCrypt {
return ss.str(); return ss.str();
} }
Block Block::MMul(const Block& o) const { template <typename T>
Basic_Block<T> Basic_Block<T>::MMul(const Basic_Block<T>& o) const {
Block m; Basic_Block<T> m;
// Maybe pre-calculate the 1d-index...? // Maybe pre-calculate the 1d-index...?
@ -74,13 +79,15 @@ namespace Leonetienne::GCrypt {
return m; return m;
} }
Block Block::operator*(const Block& other) const { template <typename T>
Basic_Block<T> Basic_Block<T>::operator*(const Basic_Block<T>& other) const {
return this->MMul(other); return this->MMul(other);
} }
void Block::MMulInplace(const Block& o) { template <typename T>
void Basic_Block<T>::MMulInplace(const Basic_Block<T>& o) {
Block m = *this; Basic_Block<T> m = *this;
// Maybe pre-calculate the 1d-index...? // Maybe pre-calculate the 1d-index...?
@ -107,86 +114,102 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block& Block::operator*=(const Block& other) { template <typename T>
Basic_Block<T>& Basic_Block<T>::operator*=(const Basic_Block<T>& other) {
MMulInplace(other); MMulInplace(other);
return *this; return *this;
} }
Block Block::Xor(const Block& other) const { template <typename T>
Basic_Block<T> Basic_Block<T>::Xor(const Basic_Block<T>& other) const {
Block m; Basic_Block<T> m;
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
m.Get(i) = this->Get(i) ^ other.Get(i); m.Get(i) = this->Get(i) ^ other.Get(i);
} }
return m; return m;
} }
Block Block::operator^(const Block& other) const { template <typename T>
Basic_Block<T> Basic_Block<T>::operator^(const Basic_Block<T>& other) const {
return Xor(other); return Xor(other);
} }
void Block::XorInplace(const Block& other) { template <typename T>
void Basic_Block<T>::XorInplace(const Basic_Block<T>& other) {
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
this->Get(i) ^= other.Get(i); this->Get(i) ^= other.Get(i);
} }
return; return;
} }
Block& Block::operator^=(const Block& other) { template <typename T>
Basic_Block<T>& Basic_Block<T>::operator^=(const Basic_Block<T>& other) {
XorInplace(other); XorInplace(other);
return *this; } return *this;
Block Block::Add(const Block& other) const { }
Block m; template <typename T>
Basic_Block<T> Basic_Block<T>::Add(const Basic_Block<T>& other) const {
Basic_Block<T> m;
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
m.Get(i) = this->Get(i) + other.Get(i); m.Get(i) = this->Get(i) + other.Get(i);
} }
return m; return m;
} }
Block Block::operator+(const Block& other) const { template <typename T>
Basic_Block<T> Basic_Block<T>::operator+(const Basic_Block<T>& other) const {
return Add(other); return Add(other);
} }
void Block::AddInplace(const Block& other) { template <typename T>
void Basic_Block<T>::AddInplace(const Basic_Block<T>& other) {
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
this->Get(i) += other.Get(i); this->Get(i) += other.Get(i);
} }
return; return;
} }
Block& Block::operator+=(const Block& other) { template <typename T>
Basic_Block<T>& Basic_Block<T>::operator+=(const Basic_Block<T>& other) {
AddInplace(other); AddInplace(other);
return *this; return *this;
} }
Block Block::Sub(const Block& other) const { template <typename T>
Basic_Block<T> Basic_Block<T>::Sub(const Basic_Block<T>& other) const {
Block m; Basic_Block<T> m;
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
m.Get(i) = this->Get(i) - other.Get(i); m.Get(i) = this->Get(i) - other.Get(i);
} }
return m; return m;
} }
Block Block::operator-(const Block& other) const { template <typename T>
Basic_Block<T> Basic_Block<T>::operator-(const Basic_Block<T>& other) const {
return Sub(other); return Sub(other);
} }
void Block::SubInplace(const Block& other) { template <typename T>
void Basic_Block<T>::SubInplace(const Basic_Block<T>& other) {
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
this->Get(i) -= other.Get(i); this->Get(i) -= other.Get(i);
} }
return; return;
} }
Block& Block::operator-=(const Block& other) { template <typename T>
Basic_Block<T>& Basic_Block<T>::operator-=(const Basic_Block<T>& other) {
SubInplace(other); SubInplace(other);
return *this; return *this;
} }
void Block::ShiftRowsUpInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftRowsUpInplace() {
Basic_Block<T> tmp = *this;
Get(MAT_INDEX(0, 0)) = tmp.Get(MAT_INDEX(1, 0)); Get(MAT_INDEX(0, 0)) = tmp.Get(MAT_INDEX(1, 0));
Get(MAT_INDEX(0, 1)) = tmp.Get(MAT_INDEX(1, 1)); Get(MAT_INDEX(0, 1)) = tmp.Get(MAT_INDEX(1, 1));
@ -211,8 +234,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftRowsUp() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftRowsUp() const {
Basic_Block<T> b;
b.Get(MAT_INDEX(0, 0)) = Get(MAT_INDEX(1, 0)); b.Get(MAT_INDEX(0, 0)) = Get(MAT_INDEX(1, 0));
b.Get(MAT_INDEX(0, 1)) = Get(MAT_INDEX(1, 1)); b.Get(MAT_INDEX(0, 1)) = Get(MAT_INDEX(1, 1));
@ -237,8 +261,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftRowsDownInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftRowsDownInplace() {
Basic_Block<T> tmp = *this;
Get(MAT_INDEX(0, 0)) = tmp.Get(MAT_INDEX(3, 0)); Get(MAT_INDEX(0, 0)) = tmp.Get(MAT_INDEX(3, 0));
Get(MAT_INDEX(0, 1)) = tmp.Get(MAT_INDEX(3, 1)); Get(MAT_INDEX(0, 1)) = tmp.Get(MAT_INDEX(3, 1));
@ -263,8 +288,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftRowsDown() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftRowsDown() const {
Basic_Block<T> b;
b.Get(MAT_INDEX(0, 0)) = Get(MAT_INDEX(3, 0)); b.Get(MAT_INDEX(0, 0)) = Get(MAT_INDEX(3, 0));
b.Get(MAT_INDEX(0, 1)) = Get(MAT_INDEX(3, 1)); b.Get(MAT_INDEX(0, 1)) = Get(MAT_INDEX(3, 1));
@ -289,8 +315,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftColumnsLeftInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftColumnsLeftInplace() {
Basic_Block<T> tmp = *this;
Get(MAT_INDEX(0, 0)) = tmp.Get(MAT_INDEX(0, 1)); Get(MAT_INDEX(0, 0)) = tmp.Get(MAT_INDEX(0, 1));
Get(MAT_INDEX(1, 0)) = tmp.Get(MAT_INDEX(1, 1)); Get(MAT_INDEX(1, 0)) = tmp.Get(MAT_INDEX(1, 1));
@ -315,8 +342,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftColumnsLeft() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftColumnsLeft() const {
Basic_Block<T> b;
b.Get(MAT_INDEX(0, 0)) = Get(MAT_INDEX(0, 1)); b.Get(MAT_INDEX(0, 0)) = Get(MAT_INDEX(0, 1));
b.Get(MAT_INDEX(1, 0)) = Get(MAT_INDEX(1, 1)); b.Get(MAT_INDEX(1, 0)) = Get(MAT_INDEX(1, 1));
@ -341,8 +369,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftColumnsRightInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftColumnsRightInplace() {
Basic_Block<T> tmp = *this;
Get(MAT_INDEX(0, 1)) = tmp.Get(MAT_INDEX(0, 0)); Get(MAT_INDEX(0, 1)) = tmp.Get(MAT_INDEX(0, 0));
Get(MAT_INDEX(1, 1)) = tmp.Get(MAT_INDEX(1, 0)); Get(MAT_INDEX(1, 1)) = tmp.Get(MAT_INDEX(1, 0));
@ -367,8 +396,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftColumnsRight() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftColumnsRight() const {
Basic_Block<T> b;
b.Get(MAT_INDEX(0, 1)) = Get(MAT_INDEX(0, 0)); b.Get(MAT_INDEX(0, 1)) = Get(MAT_INDEX(0, 0));
b.Get(MAT_INDEX(1, 1)) = Get(MAT_INDEX(1, 0)); b.Get(MAT_INDEX(1, 1)) = Get(MAT_INDEX(1, 0));
@ -393,8 +423,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftCellsLeftInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftCellsLeftInplace() {
Basic_Block<T> tmp = *this;
Get(15) = tmp.Get(0); Get(15) = tmp.Get(0);
@ -405,8 +436,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftCellsLeft() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftCellsLeft() const {
Basic_Block<T> b;
b.Get(15) = Get(0); b.Get(15) = Get(0);
@ -417,8 +449,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftCellsRightInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftCellsRightInplace() {
Basic_Block<T> tmp = *this;
Get(0) = tmp.Get(15); Get(0) = tmp.Get(15);
@ -429,8 +462,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftCellsRight() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftCellsRight() const {
Basic_Block<T> b;
b.Get(0) = Get(15); b.Get(0) = Get(15);
@ -441,12 +475,14 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
Block& Block::operator=(const Block& other) { template <typename T>
Basic_Block<T>& Basic_Block<T>::operator=(const Basic_Block<T>& other) {
data = other.data; data = other.data;
return *this; return *this;
} }
bool Block::GetBit(const std::size_t index) const { template <typename T>
bool Basic_Block<T>::GetBit(const std::size_t index) const {
// Fetch index of integer the bit is located in // Fetch index of integer the bit is located in
const std::size_t intIndex = index / CHUNK_SIZE_BITS; const std::size_t intIndex = index / CHUNK_SIZE_BITS;
@ -459,7 +495,8 @@ namespace Leonetienne::GCrypt {
return data[intIndex] & bitmask; return data[intIndex] & bitmask;
} }
void Block::SetBit(const std::size_t index, const bool state) { template <typename T>
void Basic_Block<T>::SetBit(const std::size_t index, const bool state) {
// Fetch index of integer the bit is located in // Fetch index of integer the bit is located in
const std::size_t intIndex = index / CHUNK_SIZE_BITS; const std::size_t intIndex = index / CHUNK_SIZE_BITS;
@ -481,7 +518,8 @@ namespace Leonetienne::GCrypt {
return; return;
} }
void Block::FlipBit(const std::size_t index) { template <typename T>
void Basic_Block<T>::FlipBit(const std::size_t index) {
// Fetch index of integer the bit is located in // Fetch index of integer the bit is located in
const std::size_t intIndex = index / CHUNK_SIZE_BITS; const std::size_t intIndex = index / CHUNK_SIZE_BITS;
@ -496,8 +534,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftBitsLeft() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftBitsLeft() const {
Basic_Block<T> b;
// First, copy this block over // First, copy this block over
b = *this; b = *this;
@ -529,8 +568,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftBitsLeftInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftBitsLeftInplace() {
Basic_Block<T> tmp = *this;
// Then, shift all integers individually // Then, shift all integers individually
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
@ -559,8 +599,9 @@ namespace Leonetienne::GCrypt {
return; return;
} }
Block Block::ShiftBitsRight() const { template <typename T>
Block b; Basic_Block<T> Basic_Block<T>::ShiftBitsRight() const {
Basic_Block<T> b;
// First, copy this block over // First, copy this block over
b = *this; b = *this;
@ -592,8 +633,9 @@ namespace Leonetienne::GCrypt {
return b; return b;
} }
void Block::ShiftBitsRightInplace() { template <typename T>
Block tmp = *this; void Basic_Block<T>::ShiftBitsRightInplace() {
Basic_Block<T> tmp = *this;
// Then, shift all integers individually // Then, shift all integers individually
for (std::size_t i = 0; i < data.size(); i++) { for (std::size_t i = 0; i < data.size(); i++) {
@ -622,56 +664,59 @@ namespace Leonetienne::GCrypt {
return; return;
} }
std::uint32_t& Block::Get(const std::uint8_t row, const std::uint8_t column){ template <typename T>
T& Basic_Block<T>::Get(const std::uint8_t row, const std::uint8_t column){
return data[MAT_INDEX(row, column)]; return data[MAT_INDEX(row, column)];
} }
const std::uint32_t& Block::Get(const std::uint8_t row, const std::uint8_t column) const { template <typename T>
const T& Basic_Block<T>::Get(const std::uint8_t row, const std::uint8_t column) const {
return data[MAT_INDEX(row, column)]; return data[MAT_INDEX(row, column)];
} }
std::uint32_t& Block::Get(const std::uint8_t index) { template <typename T>
T& Basic_Block<T>::Get(const std::uint8_t index) {
return data[index]; return data[index];
} }
const std::uint32_t& Block::Get(const std::uint8_t index) const { template <typename T>
const T& Basic_Block<T>::Get(const std::uint8_t index) const {
return data[index]; return data[index];
} }
std::uint32_t& Block::operator[](const std::uint8_t index) { template <typename T>
T& Basic_Block<T>::operator[](const std::uint8_t index) {
return data[index]; return data[index];
} }
const std::uint32_t& Block::operator[](const std::uint8_t index) const { template <typename T>
const T& Basic_Block<T>::operator[](const std::uint8_t index) const {
return data[index]; return data[index];
} }
bool Block::operator==(const Block& other) const { template <typename T>
bool Basic_Block<T>::operator==(const Basic_Block<T>& other) const {
return data == other.data; return data == other.data;
} }
bool Block::operator!=(const Block& other) const { template <typename T>
bool Basic_Block<T>::operator!=(const Basic_Block<T>& other) const {
return data != other.data; return data != other.data;
} }
std::ostream& operator<<(std::ostream& os, const Block& b) {
for (std::size_t i = 0; i < b.data.size(); i++) {
os << std::bitset<Block::CHUNK_SIZE_BITS>(b.data[i]).to_string();
}
return os;
}
#if defined _WIN32 || defined _WIN64 #if defined _WIN32 || defined _WIN64
#pragma optimize("", off ) #pragma optimize("", off )
#elif defined __GNUG__ #elif defined __GNUG__
#pragma GCC push_options #pragma GCC push_options
#pragma GCC optimize ("O0") #pragma GCC optimize ("O0")
#endif #endif
Block::~Block() { template <typename T>
Basic_Block<T>::~Basic_Block() {
Reset(); Reset();
} }
void Block::Reset() { template <typename T>
void Basic_Block<T>::Reset() {
memset(data.data(), 0, CHUNK_SIZE*data.size()); memset(data.data(), 0, CHUNK_SIZE*data.size());
return; return;
} }