Add GetBit() method to Block

This commit is contained in:
Leonetienne 2022-05-24 23:18:39 +02:00
parent db0add6e6e
commit e552e1a6f8
No known key found for this signature in database
GPG Key ID: C33879CD92E9708C
3 changed files with 29 additions and 0 deletions

View File

@ -123,6 +123,9 @@ namespace Leonetienne::GCrypt {
//! Will zero all data //! Will zero all data
void Reset(); void Reset();
//! Will return the state of any given bit
[[nodiscard]] bool GetBit(const std::size_t index) const;
//! 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]] std::uint32_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)

View File

@ -1,4 +1,5 @@
#include "GCrypt/Block.h" #include "GCrypt/Block.h"
#include <iostream>
#include "GCrypt/Config.h" #include "GCrypt/Config.h"
#include <sstream> #include <sstream>
#include <bitset> #include <bitset>
@ -447,6 +448,16 @@ namespace Leonetienne::GCrypt {
return *this; return *this;
} }
bool Block::GetBit(const std::size_t index) const {
// Fetch index of integer the bit is located in
const std::size_t intIndex = index / CHUNK_SIZE_BITS;
// Fetch bit index relative to that int
const std::size_t relBitIndex = index - (intIndex * CHUNK_SIZE_BITS);
return data[intIndex] & (1 << (CHUNK_SIZE_BITS - relBitIndex - 1));
}
std::uint32_t& Block::Get(const std::uint8_t row, const std::uint8_t column){ std::uint32_t& Block::Get(const std::uint8_t row, const std::uint8_t column){
return data[MAT_INDEX(row, column)]; return data[MAT_INDEX(row, column)];
} }

View File

@ -633,3 +633,18 @@ TEST_CASE(__FILE__"/multiple-combined-shifts-and-additions-can-be-undone", "[Blo
REQUIRE(a == initial_a); REQUIRE(a == initial_a);
} }
// Tests that the get-bit method works
TEST_CASE(__FILE__"/get-bit", "[Block]") {
// Setup
Block a = Key::FromPassword("Halleluja");
// Exercise
std::stringstream ss;
for (std::size_t i = 0; i < 512; i++) {
ss << a.GetBit(i);
}
// Verify
REQUIRE(ss.str() == a.ToString());
}