2022-05-22 17:54:04 +02:00
|
|
|
#ifndef GCRYPT_BLOCK_H
|
|
|
|
#define GCRYPT_BLOCK_H
|
2022-05-22 17:29:38 +02:00
|
|
|
|
2022-05-24 01:02:06 +02:00
|
|
|
#include <cstdint>
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
|
|
|
#include <ostream>
|
2022-05-16 22:15:34 +02:00
|
|
|
|
|
|
|
namespace Leonetienne::GCrypt {
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
/* This class represents a block of data,
|
|
|
|
* and provides functions to manipulate it
|
|
|
|
*/
|
|
|
|
class Block {
|
|
|
|
public:
|
|
|
|
//! Will constuct an uninitialized data block
|
|
|
|
Block();
|
|
|
|
|
|
|
|
//! Will construct this block from a string like "101010".. Length MUST be 512.
|
|
|
|
Block(const std::string& other);
|
|
|
|
|
|
|
|
//! Copy-ctor
|
|
|
|
Block(const Block& other);
|
|
|
|
|
|
|
|
~Block();
|
|
|
|
|
|
|
|
//! Will construct this block from a string like "011101..". Length MUST be 512.
|
|
|
|
void FromString(const std::string& str);
|
|
|
|
|
|
|
|
//! Will create a bitset-compatible string ("0101110..") representation
|
|
|
|
//! of this block. Length will always be 512.
|
|
|
|
std::string ToString() const;
|
|
|
|
|
|
|
|
//! Will matrix-multiply two blocks together.
|
|
|
|
//! Since the matrices values are pretty much sudo-random,
|
|
|
|
//! they will most likely integer-overflow.
|
|
|
|
//! So see this as a one-way function.
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] Block MMul(const Block& other) const;
|
|
|
|
[[nodiscard]] Block operator*(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Will matrix-multiply two blocks together,
|
|
|
|
//! and directly write into this same block.
|
|
|
|
//! Since the matrices values are pretty much sudo-random,
|
|
|
|
//! they will most likely integer-overflow.
|
|
|
|
//! So see this as a one-way function.
|
|
|
|
void MMulInplace(const Block& other);
|
|
|
|
Block& operator*=(const Block& other);
|
|
|
|
|
|
|
|
//! Will xor two blocks together
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] Block Xor(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
//! Will xor two blocks together
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] Block operator^(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Will xor two blocks together, inplace
|
|
|
|
void XorInplace(const Block& other);
|
|
|
|
//! Will xor two blocks together, inplace
|
|
|
|
Block& operator^=(const Block& other);
|
|
|
|
|
2022-05-24 21:41:49 +02:00
|
|
|
//! Will add all the integer making up this block, one by one
|
|
|
|
[[nodiscard]] Block Add(const Block& other) const;
|
|
|
|
//! Will add all the integer making up this block, one by one
|
|
|
|
[[nodiscard]] Block operator+(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
2022-05-24 21:41:49 +02:00
|
|
|
//! Will add all the integer making up this block, one by one, inplace
|
|
|
|
void AddInplace(const Block& other);
|
|
|
|
//! Will add all the integer making up this block, one by one, inplace
|
|
|
|
Block& operator+=(const Block& other);
|
2022-05-24 01:02:06 +02:00
|
|
|
|
2022-05-24 21:41:49 +02:00
|
|
|
//! Will subtract all the integer making up this block, one by one
|
|
|
|
[[nodiscard]] Block Sub(const Block& other) const;
|
|
|
|
//! Will subtract all the integer making up this block, one by one
|
|
|
|
[[nodiscard]] Block operator-(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
2022-05-24 21:41:49 +02:00
|
|
|
//! Will subtract all the integer making up this block, one by one, inplace
|
|
|
|
void SubInplace(const Block& other);
|
|
|
|
//! Will subtract all the integer making up this block, one by one, inplace
|
|
|
|
Block& operator-=(const Block& other);
|
2022-05-24 01:02:06 +02:00
|
|
|
|
2022-05-24 21:41:49 +02:00
|
|
|
//! Will shift rows upwards by 1
|
|
|
|
[[nodiscard]] Block ShiftRowsUp() const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
2022-05-24 21:41:49 +02:00
|
|
|
//! Will shift rows upwards by 1
|
|
|
|
void ShiftRowsUpInplace();
|
|
|
|
|
|
|
|
//! Will shift matrix rows downwards by 1
|
|
|
|
[[nodiscard]] Block ShiftRowsDown() const;
|
|
|
|
|
|
|
|
//! Will shift matrix rows downwards by 1
|
|
|
|
void ShiftRowsDownInplace();
|
|
|
|
|
|
|
|
//! Will shift matrix columns to the left by 1
|
|
|
|
[[nodiscard]] Block ShiftColumnsLeft() const;
|
|
|
|
|
|
|
|
//! Will shift matrix columns to the left by 1
|
|
|
|
void ShiftColumnsLeftInplace();
|
|
|
|
|
|
|
|
//! Will shift matrix columns to the right by 1
|
|
|
|
[[nodiscard]] Block ShiftColumnsRight() const;
|
|
|
|
|
|
|
|
//! Will shift matrix columns to the right by 1
|
|
|
|
void ShiftColumnsRightInplace();
|
|
|
|
|
|
|
|
//! Will shift array cells to the left by 1
|
|
|
|
[[nodiscard]] Block ShiftCellsLeft() const;
|
|
|
|
|
|
|
|
//! Will shift array cells to the left by 1
|
|
|
|
void ShiftCellsLeftInplace();
|
|
|
|
|
|
|
|
//! Will shift array cells to the right by 1
|
|
|
|
[[nodiscard]] Block ShiftCellsRight() const;
|
|
|
|
|
|
|
|
//! Will shift array cells to the right by 1
|
|
|
|
void ShiftCellsRightInplace();
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Will copy a block
|
|
|
|
Block& operator=(const Block& other);
|
|
|
|
|
|
|
|
//! Will compare whether or not two blocks are equal
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] bool operator==(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
//! Will compare whether or not two blocks are unequal
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] bool operator!=(const Block& other) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Will zero all data
|
|
|
|
void Reset();
|
|
|
|
|
2022-05-24 23:18:39 +02:00
|
|
|
//! Will return the state of any given bit
|
|
|
|
[[nodiscard]] bool GetBit(const std::size_t index) const;
|
|
|
|
|
2022-05-24 23:51:17 +02:00
|
|
|
//! Will set the state of any given bit
|
|
|
|
void SetBit(const std::size_t index, const bool state);
|
|
|
|
|
|
|
|
//! Will flip the state of any given bit
|
|
|
|
void FlipBit(const std::size_t index);
|
|
|
|
|
2022-05-24 01:02:06 +02:00
|
|
|
//! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3)
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] std::uint32_t& Get(const std::uint8_t row, const std::uint8_t column);
|
2022-05-24 01:02:06 +02:00
|
|
|
//! Returns 32-bit chunks of data, indexed by matrix coordinates (0-3)
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] const std::uint32_t& Get(const std::uint8_t row, const std::uint8_t column) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] std::uint32_t& Get(const std::uint8_t index);
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] const std::uint32_t& Get(const std::uint8_t index) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] std::uint32_t& operator[](const std::uint8_t index);
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
//! Returns 32-bit chunks of data, indexed by a 1d-index (0-16)
|
2022-05-24 21:41:49 +02:00
|
|
|
[[nodiscard]] const std::uint32_t& operator[](const std::uint8_t index) const;
|
2022-05-24 01:02:06 +02:00
|
|
|
|
|
|
|
static constexpr std::size_t CHUNK_SIZE = sizeof(std::uint32_t);
|
|
|
|
static constexpr std::size_t CHUNK_SIZE_BITS = CHUNK_SIZE * 8;
|
|
|
|
|
|
|
|
friend std::ostream& operator<<(std::ostream& os, const Block& b);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
std::array<std::uint32_t, 16> data;
|
|
|
|
};
|
|
|
|
|
2022-05-16 22:15:34 +02:00
|
|
|
}
|
|
|
|
|
2022-05-24 01:02:06 +02:00
|
|
|
|
2022-05-22 17:29:38 +02:00
|
|
|
#endif
|
|
|
|
|