diff --git a/Aesi.h b/Aesi.h index 6a4d3a0..94115a0 100644 --- a/Aesi.h +++ b/Aesi.h @@ -60,13 +60,13 @@ class Aesi final { /** * @brief Block line of the number */ - blockLine blocks {}; + blockLine blocks; /** * @enum Aesi::Sign * @brief Specifies sign of the number. Should be Positive, Negative or Zero */ - enum Sign { Zero = 0, Positive = 1, Negative = 2 } sign { Zero }; + enum Sign { Zero = 0, Positive = 1, Negative = 2 } sign; /* ----------------------------------------------------------------------- */ @@ -130,7 +130,7 @@ class Aesi final { /** * @brief Default constructor */ - gpu constexpr Aesi() noexcept : blocks{}, sign { Zero } {}; + gpu constexpr Aesi() noexcept = default; /** * @brief Copy constructor @@ -179,7 +179,7 @@ class Aesi final { * @details Accepts decimal literals along with binary (starting with 0b/0B), octal (0o/0O) and hexadecimal (0x/0X) */ template requires (std::is_same_v || std::is_same_v) - gpu constexpr Aesi(const Char* ptr, std::size_t size) noexcept : Aesi() { + gpu constexpr Aesi(const Char* ptr, std::size_t size) noexcept : Aesi {} { if(size == 0) return; constexpr const Char* characters = [] { @@ -248,7 +248,7 @@ class Aesi final { */ template requires (std::is_same_v, typename std::decay::type> || std::is_same_v, typename std::decay::type>) - gpu constexpr Aesi(String&& stringView) noexcept : Aesi(stringView.data(), stringView.size()){} + gpu constexpr Aesi(String&& stringView) noexcept : Aesi(stringView.data(), stringView.size()) {} /** * @brief Different precision copy constructor @@ -415,6 +415,19 @@ class Aesi final { return *this; } + /** + * @brief Integer multiplication. + * @param Aesi left + * @param Aesi right + * @return Product and carry-out by reference + */ + gpu static constexpr auto multiply(const Aesi& left, const Aesi& right, Aesi& product, block& carryOut) noexcept -> void { + if(left.sign == Zero || right.sign == Zero) { product = 0; carryOut = 0; return; } + product.sign = (left.sign != right.sign ? Negative : Positive); + + + } + /** * @brief Division operator * @param Aesi divisor @@ -422,7 +435,9 @@ class Aesi final { */ [[nodiscard]] gpu constexpr auto operator/(const Aesi& divisor) const noexcept -> Aesi { - Aesi quotient, _; divide(*this, divisor, quotient, _); return quotient; + Aesi quotient, _; + divide(*this, divisor, quotient, _); + return quotient; } /** @@ -797,6 +812,30 @@ class Aesi final { return (blocks[blockNumber] & (0xffU << shift)) >> shift; } + /** + * @brief Set block in number by index starting from the right + * @param Size_t index + * @param Block byte + * @note Does nothing for index out of range + */ + gpu constexpr auto setBlock(std::size_t index, block block) noexcept -> void { + if(index >= blocksNumber) return; blocks[index] = block; + + if(sign == Zero && block != 0) sign = Positive; + if(sign != Zero && block == 0 && isLineEmpty(blocks)) sign = Zero; + } + + /** + * @brief Get block in number by index starting from the right + * @param Size_t index + * @return Block + * @note Returns zero for index out of range + */ + [[nodiscard]] + gpu constexpr auto getBlock(std::size_t index) const noexcept -> block { + if(index >= blocksNumber) return block(); return blocks[index]; + } + /** * @brief Get amount of non-empty bytes in number right to left * @return Size_t @@ -919,8 +958,7 @@ class Aesi final { const Aesi divAbs = divisor.abs(); const auto ratio = number.abs().compareTo(divAbs); - if(!quotient.isZero()) quotient = Aesi {}; - if(!remainder.isZero()) remainder = Aesi {}; + quotient = Aesi {}; remainder = Aesi {}; if(ratio == AesiCMP::greater) { const auto bitsUsed = lineLength(number.blocks) * blockBitLength; @@ -950,7 +988,7 @@ class Aesi final { */ [[nodiscard]] gpu static constexpr auto divide(const Aesi& number, const Aesi& divisor) noexcept -> pair { - pair results = { 0, 0 }; divide(number, divisor, results.first, results.second); return results; + pair results; divide(number, divisor, results.first, results.second); return results; } /** @@ -1084,7 +1122,7 @@ class Aesi final { */ template requires (newBitness != bitness) [[nodiscard]] gpu constexpr auto precisionCast() const noexcept -> Aesi { - Aesi result = 0; + Aesi result {}; long long startBlock = (blocksNumber < (newBitness / blockBitLength) ? blocksNumber - 1 : (newBitness / blockBitLength) - 1); for(; startBlock >= 0; --startBlock) { @@ -1096,6 +1134,7 @@ class Aesi final { return result; } + /* ----------------- @name Public input-output operators. ---------------- */ /** * @brief Print number inside C-style array buffer * @param Byte base TEMPLATE @@ -1195,7 +1234,7 @@ class Aesi final { * @note Works significantly faster for hexadecimal notation */ template requires (std::is_same_v || std::is_same_v) - friend constexpr auto operator<<(std::basic_ostream& ss, const Aesi& value) noexcept -> std::basic_ostream& { + friend constexpr auto operator<<(std::basic_ostream& ss, const Aesi& value) -> std::basic_ostream& { auto flags = ss.flags(); if(value.sign != Zero) { @@ -1238,6 +1277,47 @@ class Aesi final { return ss; } + /** + * @brief Read a number in binary from an input stream + * @param Istream stream + * @param Boolean big_endian + * @return Aesi + * @details Reads number from stream using .read method. Accepts STD streams based on char or wchar_t. + * @note Fills empty bits with 0s on eof of the stream + */ + template requires (std::is_same_v || std::is_same_v) + static constexpr auto readBinary(std::basic_istream& istream, bool bigEndian = true) -> Aesi { + Aesi result {}; result.sign = Positive; + + if(bigEndian) { + for(auto it = result.blocks.rbegin(); it != result.blocks.rend(); ++it) + if(!istream.read(reinterpret_cast(&*it), sizeof(block))) break; + } else { + for(auto& tBlock: result.blocks) + if(!istream.read(reinterpret_cast(&tBlock), sizeof(block))) break; + } + + return result; + } + + /** + * @brief Write a number in binary to the output stream + * @param Ostream stream + * @param Boolean big_endian + * @details Writes number in stream using .write method. Accepts STD streams based on char or wchar_t. + */ + template requires (std::is_same_v || std::is_same_v) + constexpr auto writeBinary(std::basic_ostream& ostream, bool bigEndian = true) noexcept -> void { + if(bigEndian) { + for(auto& block: blocks) + if(!ostream.write(reinterpret_cast(&block), sizeof(block))) break; + } else { + for(auto it = blocks.rbegin(); it != blocks.rend(); ++it) + if(!ostream.write(reinterpret_cast(&*it), sizeof(block))) break; + } + } + /* ----------------------------------------------------------------------- */ + #ifdef __CUDACC__ /** * @brief Object assignation using atomic CUDA operations diff --git a/README.md b/README.md index 1c6a84a..17cb7e4 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,5 @@ std::cout << Aesi<256>::powm(base, power, modulo) << std::endl; // Fine - Sign in bitwise operators depends on the sign of the first operand. - The tilde operator (~) does __NOT__ affect the sign of a number. - Both bitshift operators do not make any effort if the shift value is greater than the bitness of the number. If the shift is negative, the opposite operator is called with the absolute value of the shift. -- Be careful with exponentiation overflow when using the __POWM__ function and similar. -- GetString method for hexadecimal notation returns number representation with letters in __LOWERCASE__. +- Be careful with exponentiation overflow when using the __POWM__ function and similar. - Both display methods (stream operator, getString()) work significantly faster for hexadecimal notation. \ No newline at end of file diff --git a/test/arithmetic/square-root.cpp b/test/arithmetic/square-root.cpp index d3ef693..0f348a3 100644 --- a/test/arithmetic/square-root.cpp +++ b/test/arithmetic/square-root.cpp @@ -5,7 +5,7 @@ TEST(SquareRoot, SquareRoot) { const auto timeStart = std::chrono::system_clock::now(); - Aesi512 m {}; + Aesi512 m; m = "6090725157803385683847790262910475439880944489553750045786895303294805776058380471608951628613013763332789951290275640362243141926540167423390151885627587."; EXPECT_EQ(m.squareRoot(), "78043098079224056927951137119052045281574520557910376004565433003118507400052."); m = "2198442897630760572542562253325024201444903498242188983498631852277766153592956295915984611974235359574640385959965624634128999962215308936301754007314365."; diff --git a/test/benchmarks/measures.db b/test/benchmarks/measures.db index 6fb6e42..03f8e44 100644 Binary files a/test/benchmarks/measures.db and b/test/benchmarks/measures.db differ diff --git a/test/bitwise/bitwise_manipulations.cpp b/test/bitwise/bitwise_manipulations.cpp index 64eef1c..9d165ce 100644 --- a/test/bitwise/bitwise_manipulations.cpp +++ b/test/bitwise/bitwise_manipulations.cpp @@ -660,6 +660,164 @@ TEST(Bitwise, GetSetByte) { (std::chrono::system_clock::now() - timeStart).count());; } +TEST(Bitwise, GetSetBlock) { + { + Aesi<256> v = 0; + v.setBlock(0, 2996518289); v.setBlock(1, 2204378359); v.setBlock(2, 2562794400); v.setBlock(3, 4234170865); v.setBlock(4, 2854339130); v.setBlock(5, 2984941461); v.setBlock(6, 1192451785); v.setBlock(7, 2085386850); + EXPECT_EQ(v, "0b0111110001001100011110100110001001000111000100110101111011001001101100011110101010010111100101011010101000100001110000100011101011111100011000000101000111110001100110001100000100100011101000001000001101100100001001001111011110110010100110110011110110010001"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 5838154); v.setBlock(1, 4092742162); v.setBlock(2, 2093312507); v.setBlock(3, 3235144972); v.setBlock(4, 564683421); v.setBlock(5, 601148827); v.setBlock(6, 666315807); v.setBlock(7, 2123657589); + EXPECT_EQ(v, "0b0111111010010100011100010111010100100111101101110010110000011111001000111101010011001101100110110010000110101000011000101001110111000000110101000110010100001100011111001100010101101001111110111111001111110010010010100001001000000000010110010001010101001010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1899136336); v.setBlock(1, 1962062636); v.setBlock(2, 979511162); v.setBlock(3, 598894541); v.setBlock(4, 19556868); v.setBlock(5, 274897474); v.setBlock(6, 2645406619); v.setBlock(7, 282702636); + EXPECT_EQ(v, "0b0001000011011001101100110010110010011101101011011011001110011011000100000110001010011010010000100000000100101010011010100000010000100011101100100110011111001101001110100110001000100111011110100111010011110010101100110010110001110001001100101000010101010000"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1771363449); v.setBlock(1, 866093798); v.setBlock(2, 515964119); v.setBlock(3, 2117929174); v.setBlock(4, 1590599710); v.setBlock(5, 31653167); v.setBlock(6, 4043874080); v.setBlock(7, 2639487490); + EXPECT_EQ(v, "0b1001110101010011011000100000001011110001000010001001111100100000000000011110001011111101001011110101111011001110101000000001111001111110001111010000100011010110000111101100000011111100110101110011001110011111100010101110011001101001100101001101110001111001"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 816181258); v.setBlock(1, 2362812647); v.setBlock(2, 4154305086); v.setBlock(3, 4046670874); v.setBlock(4, 412614353); v.setBlock(5, 1374776725); v.setBlock(6, 2866844404); v.setBlock(7, 3581549588); + EXPECT_EQ(v, "0b1101010101111010000111000001010010101010111000001001001011110100010100011111000101101101100101010001100010010111111111101101000111110001001100110100110000011010111101111001110110101010001111101000110011010101101010001110011100110000101001011111000000001010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 830011515); v.setBlock(1, 2694135319); v.setBlock(2, 2159315809); v.setBlock(3, 1982781521); v.setBlock(4, 35517563); v.setBlock(5, 3834815279); v.setBlock(6, 66186924); v.setBlock(7, 2160380246); + EXPECT_EQ(v, "0b1000000011000100110010010101011000000011111100011110111010101100111001001001001010100011001011110000001000011101111101000111101101110110001011101101100001010001100000001011010010001011011000011010000010010101001111100001011100110001011110001111100001111011"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 378798562); v.setBlock(1, 32642765); v.setBlock(2, 2394127302); v.setBlock(3, 1229750488); v.setBlock(4, 3582558977); v.setBlock(5, 502626102); v.setBlock(6, 3124542233); v.setBlock(7, 2293532049); + EXPECT_EQ(v, "0b1000100010110100100001011001000110111010001111001011101100011001000111011111010101110111001101101101010110001001100000110000000101001001010011001000000011011000100011101011001101111011110001100000000111110010000101101100110100010110100101000000000111100010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1658413944); v.setBlock(1, 4131334207); v.setBlock(2, 240063001); v.setBlock(3, 1069508628); v.setBlock(4, 3465011114); v.setBlock(5, 512577764); v.setBlock(6, 5974688); v.setBlock(7, 2451745539); + EXPECT_EQ(v, "0b1001001000100010101010110000001100000000010110110010101010100000000111101000110101010000111001001100111010000111110111111010101000111111101111110110100000010100000011100100111100010010000110011111011000111111001010000011111101100010110110010110001101111000"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1123538980); v.setBlock(1, 4097649259); v.setBlock(2, 1974045642); v.setBlock(3, 11707332); v.setBlock(4, 1750136655); v.setBlock(5, 3611228280); v.setBlock(6, 2110847509); v.setBlock(7, 3712711249); + EXPECT_EQ(v, "0b1101110101001011011110100101000101111101110100001111101000010101110101110011111011111000011110000110100001010000111101110100111100000000101100101010001111000100011101011010100110001011110010101111010000111101001010100110101101000010111101111101100000100100"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 2429133743); v.setBlock(1, 1751115826); v.setBlock(2, 3705673661); v.setBlock(3, 3559534575); v.setBlock(4, 3841629607); v.setBlock(5, 1982227590); v.setBlock(6, 2938846263); v.setBlock(7, 1275037345); + EXPECT_EQ(v, "0b0100101111111111100001101010000110101111001010110011110000110111011101100010011001100100100001101110010011111010100111011010011111010100001010100010111111101111110111001110000000010111101111010110100001011111111010000011001010010000110010011010001110101111"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1041401522); v.setBlock(1, 362096520); v.setBlock(2, 1362390126); v.setBlock(3, 796473907); v.setBlock(4, 4144453259); v.setBlock(5, 739787128); v.setBlock(6, 321085936); v.setBlock(7, 3378302733); + EXPECT_EQ(v, "0b1100100101011100110011110000110100010011001000110110000111110000001011000001100001000001011110001111011100000111010101101000101100101111011110010011101000110011010100010011010001101100011011100001010110010101001001111000100000111110000100101000011010110010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 3850991329); v.setBlock(1, 3075011704); v.setBlock(2, 3412884974); v.setBlock(3, 3132340999); v.setBlock(4, 818954321); v.setBlock(5, 1274399492); v.setBlock(6, 3617213044); v.setBlock(7, 3859060060); + EXPECT_EQ(v, "0b1110011000000100100101010101110011010111100110100100101001110100010010111111010111001011000001000011000011010000010000000101000110111010101100111011101100000111110010110110110001111101111011101011011101001000111101000111100011100101100010010111011011100001"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 770884658); v.setBlock(1, 2151066307); v.setBlock(2, 2849326844); v.setBlock(3, 4009558025); v.setBlock(4, 4054752346); v.setBlock(5, 2189473466); v.setBlock(6, 1184082458); v.setBlock(7, 173925068); + EXPECT_EQ(v, "0b0000101001011101111000101100110001000110100100111010101000011010100000101000000010110110101110101111000110101110100111000101101011101110111111010000000000001001101010011101010101000110111111001000000000110110101010101100001100101101111100101100010000110010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1490112118); v.setBlock(1, 2165529776); v.setBlock(2, 3967521584); v.setBlock(3, 2237792965); v.setBlock(4, 3800843785); v.setBlock(5, 3114944526); v.setBlock(6, 482500077); v.setBlock(7, 720353553); + EXPECT_EQ(v, "0b0010101011101111101110010001000100011100110000100101110111101101101110011010101001001000000011101110001010001100010001100000100110000101011000100000001011000101111011000111101110010011001100001000000100010011010111001011000001011000110100010100111001110110"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 3821909907); v.setBlock(1, 2061617179); v.setBlock(2, 4097673352); v.setBlock(3, 466240605); v.setBlock(4, 726595768); v.setBlock(5, 2032737202); v.setBlock(6, 1624450772); v.setBlock(7, 1506376752); + EXPECT_EQ(v, "0b0101100111001001011111000011000001100000110100110010011011010100011110010010100100011011101100100010101101001110111110001011100000011011110010100100010001011101111101000011110110001000100010000111101011100001110010000001101111100011110011011011011110010011"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1369296696); v.setBlock(1, 3142389534); v.setBlock(2, 2876156466); v.setBlock(3, 2596371073); v.setBlock(4, 1547466829); v.setBlock(5, 3220646325); v.setBlock(6, 964875921); v.setBlock(7, 3203319696); + EXPECT_EQ(v, "0b1011111011101110110001111001000000111001100000101101011010010001101111111111011100101001101101010101110000111100011110000100110110011010110000010111101010000001101010110110111010101010001100101011101101001101000011110001111001010001100111011100111100111000"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1790600854); v.setBlock(1, 3083359547); v.setBlock(2, 2883929944); v.setBlock(3, 2919235547); v.setBlock(4, 1945282022); v.setBlock(5, 3157783719); v.setBlock(6, 2868933201); v.setBlock(7, 4117201509); + EXPECT_EQ(v, "0b1111010101100111100000100110010110101011000000000111001001010001101111000011011111110100101001110111001111110010101001011110011010101101111111111111111111011011101010111110010101000111010110001011011111001000010101010011101101101010101110100110011010010110"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 752159466); v.setBlock(1, 1650941344); v.setBlock(2, 3959408811); v.setBlock(3, 4199780363); v.setBlock(4, 1635453441); v.setBlock(5, 1721151066); v.setBlock(6, 3480362280); v.setBlock(7, 1790351015); + EXPECT_EQ(v, "0b0110101010110110100101101010011111001111011100100001110100101000011001101001011010101110010110100110000101111011000010100000000111111010010100111001000000001011111010111111111111001000101010110110001001100111010111011010000000101100110101010000101011101010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 3139548659); v.setBlock(1, 1220688699); v.setBlock(2, 1447387080); v.setBlock(3, 1458401987); v.setBlock(4, 1439544509); v.setBlock(5, 1222297735); v.setBlock(6, 4022003105); v.setBlock(7, 4080170984); + EXPECT_EQ(v, "0b1111001100110010011101111110100011101111101110101110010110100001010010001101101011001000100001110101010111001101101101001011110101010110111011010111001011000011010101100100010101011111110010000100100011000010001110110011101110111011001000011011010111110011"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1762737466); v.setBlock(1, 1801351446); v.setBlock(2, 1161424425); v.setBlock(3, 3643691220); v.setBlock(4, 3277899155); v.setBlock(5, 1735729124); v.setBlock(6, 447836645); v.setBlock(7, 1614015805); + EXPECT_EQ(v, "0b0110000000110011111011010011110100011010101100010111000111100101011001110111010100011111111001001100001101100000110001011001001111011001001011100101000011010100010001010011100111101110001010010110101101011110011100010001011001101001000100010011110100111010"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1277516593); v.setBlock(1, 1627371649); v.setBlock(2, 3270266578); v.setBlock(3, 1964291532); v.setBlock(4, 4095710735); v.setBlock(5, 4016484266); v.setBlock(6, 1408838094); v.setBlock(7, 2785749983); + EXPECT_EQ(v, "0b1010011000001011001010111101111101010011111110010010100111001110111011110110011010101111101010101111010000011111100101100000111101110101000101001011010111001100110000101110110001001110110100100110000011111111101110001000000101001100001001010101101100110001"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 2478182814); v.setBlock(1, 2534050561); v.setBlock(2, 58941359); v.setBlock(3, 3947182061); v.setBlock(4, 1590193474); v.setBlock(5, 3281460096); v.setBlock(6, 2038674258); v.setBlock(7, 819268818); + EXPECT_EQ(v, "0b0011000011010101000011001101001001111001100000111011001101010010110000111001011100011011100000000101111011001000011011010100001011101011010001010011011111101101000000111000001101011111101011111001011100001010100010110000000110010011101101100001000110011110"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 4033279119); v.setBlock(1, 338923298); v.setBlock(2, 390001710); v.setBlock(3, 3835457473); v.setBlock(4, 3120284392); v.setBlock(5, 2317025340); v.setBlock(6, 1580558798); v.setBlock(7, 424692813); + EXPECT_EQ(v, "0b0001100101010000010011000100110101011110001101010110100111001110100010100001101100000000001111001011100111111011110000101110100011100100100111000110111111000001000101110011111011110100001011100001010000110011100011110010001011110000011001101111010010001111"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1683516556); v.setBlock(1, 3950317078); v.setBlock(2, 3806427615); v.setBlock(3, 961832623); v.setBlock(4, 2868767090); v.setBlock(5, 457262732); v.setBlock(6, 2224175541); v.setBlock(7, 378957276); + EXPECT_EQ(v, "0b0001011010010110011011011101110010000100100100100011100110110101000110110100000101000110100011001010101011111101111010010111001000111001010101000110011010101111111000101110000101111001110111111110101101110101000011100001011001100100010110000110110010001100"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1133352752); v.setBlock(1, 3059096484); v.setBlock(2, 2604146896); v.setBlock(3, 2674077280); v.setBlock(4, 246510928); v.setBlock(5, 4113131624); v.setBlock(6, 553565087); v.setBlock(7, 1062874799); + EXPECT_EQ(v, "0b0011111101011010001011101010111100100000111111101011101110011111111101010010100101101000011010000000111010110001011101010101000010011111011000110010111001100000100110110011100000100000110100001011011001010110000110111010010001000011100011011001011100110000"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 2085239680); v.setBlock(1, 1994712778); v.setBlock(2, 3479462472); v.setBlock(3, 623739565); v.setBlock(4, 2417510094); v.setBlock(5, 3795684050); v.setBlock(6, 105387316); v.setBlock(7, 1888000207); + EXPECT_EQ(v, "0b0111000010001000100110001100111100000110010010000001010100110100111000100011110110001010110100101001000000011000010001101100111000100101001011011000001010101101110011110110010001100010010010000111011011100100111001101100101001111100010010100011101110000000"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 1610502617); v.setBlock(1, 2466559644); v.setBlock(2, 2183048355); v.setBlock(3, 3659430499); v.setBlock(4, 1159809795); v.setBlock(5, 1119352621); v.setBlock(6, 107677482); v.setBlock(7, 3872113695); + EXPECT_EQ(v, "0b1110011011001011110001000001111100000110011010110000011100101010010000101011011111110111001011010100010100100001010010110000001111011010000111100111101001100011100000100001111010101100101000111001001100000100101101101001110001011111111111100101000111011001"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 3843633955); v.setBlock(1, 4136288623); v.setBlock(2, 3262052056); v.setBlock(3, 1291433090); v.setBlock(4, 1594212682); v.setBlock(5, 2122434418); v.setBlock(6, 2122560054); v.setBlock(7, 2107769694); + EXPECT_EQ(v, "0b0111110110100010000000110101111001111110100000111011001000110110011111101000000111000111011100100101111100000101110000010100101001001100111110011011010010000010110000100110111011110110110110001111011010001010110000010110111111100101000110010011001100100011"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 3345606279); v.setBlock(1, 3864257319); v.setBlock(2, 2762524990); v.setBlock(3, 2582001197); v.setBlock(4, 1063343893); v.setBlock(5, 284235248); v.setBlock(6, 3064714409); v.setBlock(7, 3223017443); + EXPECT_EQ(v, "0b1100000000011011010101111110001110110110101010111101010010101001000100001111000100010101111100000011111101100001010101110001010110011001111001100011011000101101101001001010100011001001001111101110011001010011111000110010011111000111011010011110011010000111"); + } + { + Aesi<256> v = 0; + v.setBlock(0, 3805244152); v.setBlock(1, 697534818); v.setBlock(2, 2964653839); v.setBlock(3, 202322536); v.setBlock(4, 2921359744); v.setBlock(5, 1197838687); v.setBlock(6, 1881520378); v.setBlock(7, 227742533); + EXPECT_EQ(v, "0b0000110110010011000100110100010101110000001001011011100011111010010001110110010110010001010111111010111000100000011010011000000000001100000011110011001001101000101100001011010100000111000011110010100110010011100010010110001011100010110011110110101011111000"); + } + + { + Aesi<256> v = "0b1000000111111000101101001110100011111110111001010010100110101110100100010110111110000110100110011111011110100010111000000000101100011000000000001110110110110110001111010100000110100000111010101101010110111010110111100011010101110011101110100011001000110001"; + EXPECT_EQ(v.getBlock(0), 1941582385); EXPECT_EQ(v.getBlock(1), 3585793589); EXPECT_EQ(v.getBlock(2), 1027711210); EXPECT_EQ(v.getBlock(3), 402714038); EXPECT_EQ(v.getBlock(4), 4154646539); EXPECT_EQ(v.getBlock(5), 2440005273); EXPECT_EQ(v.getBlock(6), 4276431278); EXPECT_EQ(v.getBlock(7), 2180560104); + } +} + TEST(Bitwise, CountBitsBytes) { const auto timeStart = std::chrono::system_clock::now(); diff --git a/test/operations/binary_read_write.cpp b/test/operations/binary_read_write.cpp new file mode 100644 index 0000000..c950482 --- /dev/null +++ b/test/operations/binary_read_write.cpp @@ -0,0 +1,430 @@ +#include +#include +#include "../../Aesi.h" +#include "../benchmarks/benchmarks.h" + +TEST(Binary, BinaryRead) { + { + unsigned blocks [] = { 0b10101101001111100100101100011001, 0b10011100010101101100011010011111, 0b10101111111011111111110000001010, 0b11100011100100010111011001110111, 0b00010111100000101010100101010111, 0b00101101011000010111100001011111, 0b11100001010011100011000100110010, 0b00111011011001000000101101100010, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1010110100111110010010110001100110011100010101101100011010011111101011111110111111111100000010101110001110010001011101100111011100010111100000101010100101010111001011010110000101111000010111111110000101001110001100010011001000111011011001000000101101100010"); + } + { + unsigned blocks [] = { 0b01000010010011110100011111111110, 0b11010100010001111100100001010110, 0b10010101011011111001000001000101, 0b10100101101100001111101101111010, 0b10001001011001011010111010101100, 0b10111000110011110011010101010011, 0b10011101011111101100111010111111, 0b00011111011110111001010101000101, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0100001001001111010001111111111011010100010001111100100001010110100101010110111110010000010001011010010110110000111110110111101010001001011001011010111010101100101110001100111100110101010100111001110101111110110011101011111100011111011110111001010101000101"); + } + { + unsigned blocks [] = { 0b11011010010110100100000111101101, 0b00011101110010011010100110000111, 0b10010111000001001011001000100011, 0b01011100100010100000000100101010, 0b00101010011111010110100100101111, 0b10001010101010101000100101101011, 0b00010000110100111110100100011000, 0b00101110100101001000110001011010, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1101101001011010010000011110110100011101110010011010100110000111100101110000010010110010001000110101110010001010000000010010101000101010011111010110100100101111100010101010101010001001011010110001000011010011111010010001100000101110100101001000110001011010"); + } + { + unsigned blocks [] = { 0b10100101111011010010011101111111, 0b11110011100010000001101000101110, 0b10000011111100110011000000100010, 0b10110111111101011100110101110111, 0b11000000000111111111100101111101, 0b10011010011111000001001100111111, 0b01010100000101011100110101111001, 0b10010100110111111000011001110100, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1010010111101101001001110111111111110011100010000001101000101110100000111111001100110000001000101011011111110101110011010111011111000000000111111111100101111101100110100111110000010011001111110101010000010101110011010111100110010100110111111000011001110100"); + } + { + unsigned blocks [] = { 0b01100111111010101110110000110010, 0b10001100110101110111010111000111, 0b00111001011001100001110110101000, 0b10010011001110000000001100011011, 0b00110100011001101010010100011110, 0b10011000110010100000100101001010, 0b11010010111001110000000100110110, 0b11111011010111110101100000001111, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0110011111101010111011000011001010001100110101110111010111000111001110010110011000011101101010001001001100111000000000110001101100110100011001101010010100011110100110001100101000001001010010101101001011100111000000010011011011111011010111110101100000001111"); + } + { + unsigned blocks [] = { 0b10011000100110011010100101101010, 0b00100101011111100100010101110011, 0b11111001001010100110111101100100, 0b00111010110100100011000011110100, 0b00111110010101010000000010101000, 0b01101111111010101100001010101100, 0b01111000011100011111010110101000, 0b00001001000001010010011000101101, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1001100010011001101010010110101000100101011111100100010101110011111110010010101001101111011001000011101011010010001100001111010000111110010101010000000010101000011011111110101011000010101011000111100001110001111101011010100000001001000001010010011000101101"); + } + { + unsigned blocks [] = { 0b00111001011011111100011000011110, 0b11000011100001001111101111111011, 0b11101010101100110100001011110001, 0b01010001110100001000001100011010, 0b00000111111101010011000000011110, 0b01111110010111110110111111001010, 0b00001011101111111100101110111000, 0b10100001100100100011110000011000, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0011100101101111110001100001111011000011100001001111101111111011111010101011001101000010111100010101000111010000100000110001101000000111111101010011000000011110011111100101111101101111110010100000101110111111110010111011100010100001100100100011110000011000"); + } + { + unsigned blocks [] = { 0b10010101010001011010101111011000, 0b10100110100110001001000110011001, 0b10111000011100110100010010010010, 0b10011001100110011101011100110001, 0b11000101010001000000101100001110, 0b01010101101000010111101000101110, 0b11010001101111111000101010101110, 0b01011101001000110000100110000001, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1001010101000101101010111101100010100110100110001001000110011001101110000111001101000100100100101001100110011001110101110011000111000101010001000000101100001110010101011010000101111010001011101101000110111111100010101010111001011101001000110000100110000001"); + } + { + unsigned blocks [] = { 0b10001010011001101010000111101010, 0b01000100101000000010011110110111, 0b10110011111100001110101100100111, 0b10111100110110001100100100001001, 0b00111001110010010110110000111001, 0b01001101110011011100110101101000, 0b01110011110000010001111111111110, 0b01101100101000110100011010010010, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1000101001100110101000011110101001000100101000000010011110110111101100111111000011101011001001111011110011011000110010010000100100111001110010010110110000111001010011011100110111001101011010000111001111000001000111111111111001101100101000110100011010010010"); + } + { + unsigned blocks [] = { 0b11101100001010000111001011101010, 0b01001101110100100010111100110010, 0b00010111000100100001011001101111, 0b00000111011001101100110101010001, 0b01010010010100001110001010101111, 0b11001100011101101010100001011110, 0b01110101100001011111010011010000, 0b11111001100010010100010000111100, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1110110000101000011100101110101001001101110100100010111100110010000101110001001000010110011011110000011101100110110011010101000101010010010100001110001010101111110011000111011010101000010111100111010110000101111101001101000011111001100010010100010000111100"); + } + { + unsigned blocks [] = { 0b10101101001101111101101101010011, 0b10111111110101111010111001001010, 0b00000100111000000111000100110001, 0b10100100001101101011001100110011, 0b11111000011100101110000110010000, 0b01000110101010001010111000101000, 0b01000101010100000111001011101010, 0b10010001001011001111010111110101, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1010110100110111110110110101001110111111110101111010111001001010000001001110000001110001001100011010010000110110101100110011001111111000011100101110000110010000010001101010100010101110001010000100010101010000011100101110101010010001001011001111010111110101"); + } + { + unsigned blocks [] = { 0b00111100111001010010000100110010, 0b00101010110111001010110101010000, 0b10110010100000011000110100100101, 0b10111111010110101011010111010100, 0b00111011001001001100101111000000, 0b10111010011100111000001100010010, 0b11011101000100001001100110001111, 0b00010011110100110010101010010000, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0011110011100101001000010011001000101010110111001010110101010000101100101000000110001101001001011011111101011010101101011101010000111011001001001100101111000000101110100111001110000011000100101101110100010000100110011000111100010011110100110010101010010000"); + } + { + unsigned blocks [] = { 0b00110000111000011010110000101001, 0b11110100101010101101011010000110, 0b00011101011100001001101001011110, 0b00110111111101010011010100011100, 0b01111110001000101100100100011110, 0b10100001101101111111000100101111, 0b11001000001100001001001101000111, 0b11001110101110101010010101100100, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0011000011100001101011000010100111110100101010101101011010000110000111010111000010011010010111100011011111110101001101010001110001111110001000101100100100011110101000011011011111110001001011111100100000110000100100110100011111001110101110101010010101100100"); + } + { + unsigned blocks [] = { 0b01010010001011110101111100000010, 0b10111101011001011101100000100111, 0b01110000111101000001111111100101, 0b11000001000001010000101111101101, 0b01101000000010010101101000000101, 0b10110111110101110001100111111100, 0b00101010001001011100101000011111, 0b10000000110110001111110110110001, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0101001000101111010111110000001010111101011001011101100000100111011100001111010000011111111001011100000100000101000010111110110101101000000010010101101000000101101101111101011100011001111111000010101000100101110010100001111110000000110110001111110110110001"); + } + { + unsigned blocks [] = { 0b10001011111101010001000100000111, 0b00110100001010000111010110110000, 0b01000101111001010011011100110110, 0b10010001111010110110100010001100, 0b10111001101001000101011110111000, 0b11110100101001000101111100001100, 0b10000010101010111000101111010010, 0b11110101111100110001000100011010, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1000101111110101000100010000011100110100001010000111010110110000010001011110010100110111001101101001000111101011011010001000110010111001101001000101011110111000111101001010010001011111000011001000001010101011100010111101001011110101111100110001000100011010"); + } + { + unsigned blocks [] = { 0b10110010111011010110001100001101, 0b01101000011101011111001110101001, 0b00010000011010111011010101010000, 0b00001011101101001110000100011010, 0b01001001001101000111111000000100, 0b01010110100011110001111010101101, 0b10001110101001001000110011011010, 0b10010111001000000010110100100000, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1011001011101101011000110000110101101000011101011111001110101001000100000110101110110101010100000000101110110100111000010001101001001001001101000111111000000100010101101000111100011110101011011000111010100100100011001101101010010111001000000010110100100000"); + } + { + unsigned blocks [] = { 0b01101000110100001110111101100101, 0b00001100000000100101011000110101, 0b01111100010100001010111110001110, 0b11100101001010011010101110110100, 0b10111001010010000000011000011001, 0b01000011101100100010011011110111, 0b11101101000011110011101101101010, 0b11011101000101010111101111011111, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0110100011010000111011110110010100001100000000100101011000110101011111000101000010101111100011101110010100101001101010111011010010111001010010000000011000011001010000111011001000100110111101111110110100001111001110110110101011011101000101010111101111011111"); + } + { + unsigned blocks [] = { 0b00100110000001011001111000011111, 0b01000011111100011111101111000111, 0b01100011010101011010001100101100, 0b10001001101100000010000001011000, 0b10111000110101011001100111110101, 0b01011110110011000011101100101000, 0b10101010100101110110100110011100, 0b10001010010110001110001101100110, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0010011000000101100111100001111101000011111100011111101111000111011000110101010110100011001011001000100110110000001000000101100010111000110101011001100111110101010111101100110000111011001010001010101010010111011010011001110010001010010110001110001101100110"); + } + { + unsigned blocks [] = { 0b00000010111110011000010000011001, 0b01100110000101011011000111101011, 0b01101010011010101001100011111010, 0b00000110010001110111100001001001, 0b10000001001101101110000000111011, 0b10111011001111000100110111010000, 0b10101011110100110110011011100001, 0b10001100101100011100011011001010, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0000001011111001100001000001100101100110000101011011000111101011011010100110101010011000111110100000011001000111011110000100100110000001001101101110000000111011101110110011110001001101110100001010101111010011011001101110000110001100101100011100011011001010"); + } + { + unsigned blocks [] = { 0b01001010011110011000011000001001, 0b00001110111101110110100100011111, 0b00001100100011001010101000001110, 0b11110100011010101010010010011100, 0b01011011101001101010110101000100, 0b10011011011000100011001101010111, 0b01011010101001011011110111110000, 0b00101001111011000001011110001110, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0100101001111001100001100000100100001110111101110110100100011111000011001000110010101010000011101111010001101010101001001001110001011011101001101010110101000100100110110110001000110011010101110101101010100101101111011111000000101001111011000001011110001110"); + } + { + unsigned blocks [] = { 0b01000010011101011011010111100101, 0b11111001110010000101101000010011, 0b11001010110100101000101110110111, 0b00110000101010011101001111111011, 0b11110001101100100001000000110111, 0b11011001111001101110101111101000, 0b11100101011001110101110111011100, 0b10111100100000001101101011101101, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0100001001110101101101011110010111111001110010000101101000010011110010101101001010001011101101110011000010101001110100111111101111110001101100100001000000110111110110011110011011101011111010001110010101100111010111011101110010111100100000001101101011101101"); + } + { + unsigned blocks [] = { 0b01001101010001110011010111010101, 0b11010010000101111000101101100000, 0b01111100001111110100000101101000, 0b11101011110100111110111001000111, 0b11001001100000111101100011010001, 0b01110000100010100110001110100100, 0b11100000101111110111100000001100, 0b11111101000111101111110011110001, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0100110101000111001101011101010111010010000101111000101101100000011111000011111101000001011010001110101111010011111011100100011111001001100000111101100011010001011100001000101001100011101001001110000010111111011110000000110011111101000111101111110011110001"); + } + { + unsigned blocks [] = { 0b01001101101100110101101110111001, 0b11100000100110011110010000100011, 0b11111111101101110110101001001000, 0b11100010010100111101101101100100, 0b00011010000011101100011001010001, 0b01101110000101111101100001001000, 0b01110010000000111101000011011100, 0b01001101011110000110101000001001, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0100110110110011010110111011100111100000100110011110010000100011111111111011011101101010010010001110001001010011110110110110010000011010000011101100011001010001011011100001011111011000010010000111001000000011110100001101110001001101011110000110101000001001"); + } + { + unsigned blocks [] = { 0b11010100010100010011100101001100, 0b01000101011111011011011101100000, 0b11011100001111111101110000101110, 0b11000101010000000111011100100010, 0b11011011010101001111110011100111, 0b11010111111010100100011100011101, 0b11001111100111100001000111110100, 0b01000011011111111011001101110111, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1101010001010001001110010100110001000101011111011011011101100000110111000011111111011100001011101100010101000000011101110010001011011011010101001111110011100111110101111110101001000111000111011100111110011110000100011111010001000011011111111011001101110111"); + } + { + unsigned blocks [] = { 0b01010010110010011100010111100101, 0b10110010100101110100010010010101, 0b00000111110000110000011001010111, 0b01010001111111011011111110001001, 0b10011001000110111010100111111001, 0b10011010101101010010111101010011, 0b01111011011110011000010100011010, 0b00010000110101000100001101011011, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b0101001011001001110001011110010110110010100101110100010010010101000001111100001100000110010101110101000111111101101111111000100110011001000110111010100111111001100110101011010100101111010100110111101101111001100001010001101000010000110101000100001101011011"); + } + { + unsigned blocks [] = { 0b00110011001001010101001011100100, 0b00010011011101111100110100110001, 0b11011101101010011111101011001001, 0b00101010101000001100110110001111, 0b00011100101001010100000000011110, 0b11000111100000100000100100101111, 0b11110111111010111000100001001011, 0b01000000001101110101111110100111, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0011001100100101010100101110010000010011011101111100110100110001110111011010100111111010110010010010101010100000110011011000111100011100101001010100000000011110110001111000001000001001001011111111011111101011100010000100101101000000001101110101111110100111"); + } + { + unsigned blocks [] = { 0b10011010101111010011001010100100, 0b00001100011110001001111100010010, 0b11110100110010100111111100010001, 0b11011010011100110011100111001100, 0b00100100101000011010001011010111, 0b11110001000110011101110100100010, 0b10111010000001001111100011101001, 0b11000000110100001010011001111100, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1001101010111101001100101010010000001100011110001001111100010010111101001100101001111111000100011101101001110011001110011100110000100100101000011010001011010111111100010001100111011101001000101011101000000100111110001110100111000000110100001010011001111100"); + } + { + unsigned blocks [] = { 0b00111101110011101001101111100010, 0b10001100111000110101011100111101, 0b11001011010001000110001101010111, 0b10100111111001111011000010011101, 0b01100110011011111110000110010000, 0b11110101000101100010110000011100, 0b10010000010011100010110100110110, 0b00011000100011000001010111111100, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b0011110111001110100110111110001010001100111000110101011100111101110010110100010001100011010101111010011111100111101100001001110101100110011011111110000110010000111101010001011000101100000111001001000001001110001011010011011000011000100011000001010111111100"); + } + { + unsigned blocks [] = { 0b10100000010010010000100100001111, 0b11100111100100111010010001111101, 0b11111100000110011000010110101100, 0b00000111001100101111101101011110, 0b00011110010100100110110111110011, 0b11001010010011110110101100001111, 0b11001111010001010000101110011011, 0b11110001011001101101111110110000, }; + std::stringstream ss; for(auto& block: blocks) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, true), "0b1010000001001001000010010000111111100111100100111010010001111101111111000001100110000101101011000000011100110010111110110101111000011110010100100110110111110011110010100100111101101011000011111100111101000101000010111001101111110001011001101101111110110000"); + } + { + unsigned blocks [] = { 0b11110010100101100101010100100010, 0b10011010100011101111110111011100, 0b10110000000100110110111111100111, 0b01100000111000110100100011011011, 0b11000100001111011000011110001000, 0b10101011100100001010011101000011, 0b01101100001100110000111000000101, 0b00010010001100010100000011010010, }; + std::stringstream ss; for(auto& block: std::views::reverse(blocks)) ss.write(reinterpret_cast(&block), sizeof(unsigned)); + EXPECT_EQ(Aesi<256>::readBinary(ss, false), "0b1111001010010110010101010010001010011010100011101111110111011100101100000001001101101111111001110110000011100011010010001101101111000100001111011000011110001000101010111001000010100111010000110110110000110011000011100000010100010010001100010100000011010010"); + } +} + +TEST(Binary, BinaryWrite) { + { + Aesi<256> l = "0b1000101101001010100110101101100111101011111110000110010001000000101101100011101101110111011110101111001101110000011001101110010101001000010000010010001000011111010111111111000000100001110110111001000111000111100000000001100100110011111100110110000000100110", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1010000101000101100011011111111100101010011111110100110101101000100011111000110000010101000101001011101010001111100011000111111110110001010110111111010110110110100111101000001000111100011011001110010011100010110011001011010110111000100111100110011101010100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1001000011101000000001110101111010101110101100110111111101011001010000101110100101000111010011011111001010110000100101001111110011000101011010010011000111011111011011111010111111111011100101110011100010000011101001101001100100001010101101001100111000011101", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0000000101101100100000010010011100010101001110101001010111011101001111100100110000101100011000101001110101101110100111000101001111001001011101001111010100010010001101011101111111100100000101110100000001111000011010011110111101101001011000010011010101011101", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0111000000100011101100110010000101101010101011101010110101101011001001100011110101011111101011000000111110111110100101101101101010101000111110101101110100111011111000100111110101010110110011110010001010100011111010000010000100100111100000001101010110111010", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0110000011101000001111101101010011010100010000011111001000101100100110111101100111101101111001000111001011100100101110110111100001110110111110011100000001111000111111101100010010101100101110001110111011110010011011111001000101100001101101111010101011001011", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1011010101111011100000011110000100010100001010010000101100110010101111010111011011110000111100101000101101111101100001010011101000101011111011001101011100001011011101110010010101110011100100011101111110110110010000101001111110111010100001100011110111011100", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0011101101101000000010000000000100000011010101100110010001101000000001110001101001011011100011101111100100001110100110111101001110000001110110011010001011001101110111010000101001100011101111001001000010000010101011110111111011010101110011010111010111011100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1010101010000111110100000110100100010101011101111111111011100111101101100100101110010010101111011010011001110110100001111010001001111001111001000001011110100011101010110010100001011101100101100000100100111001011011010000010011111001000000001011001111100010", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0101010000010001110101011100011001001001110110100101011001011111010101000011011101111100011011010111001011111010111111111111110011110011111000100011001111101011100010010011100011011100111010111100011001000101100110011111001101101010010000111111111110111100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0110101111101100111111011100100011111001110111101000110111101110000110000110011011011111111111001001111001010010001011100101111001111001100110010000000011011111011000111001000001010110000101010001000101011011101110001111001101101101000111111011011111000010", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0101000010011101001011001010001110110101001011000001110100101000101010110101001101011101101011110111101001000000000000101110100110101110101111010111101010001011100101010011100101011110101111110000011000001011000010101000100000000000011001001010100001010110", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0010010001111010010011001101101000110000010001011110100000000110100011111010000011101100001100011110111011011000000011011100010101000010001100110011001010010100001011110000110101001111111000001101001110000101110101100110010110110000101010100000110110011000", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0110100000001001010110101010001110111111110100100111000110011000100111101101011001010001000101111101101001010110110000011101000010000110101000011000101111000001111111000111101100000010101011101010111011001000100101010010111101011011010100011001110110111111", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1001110011010010001110001010011010111000110001011110111010011011110101101100110101010110010001001001110010100100000001001011011111111011000001101101101101011010110100011011010010101011000011011010010100011000110000000010111000111000110110000101111010111011", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0111110000100101001011010111111011001101111010111100011100011011111010011001000111000101111001101010011010010010101011010111010100011110111011110001001001000101110100001100110110111101101001010000111011111101110100010110101101101100100011010011000110000001", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1111001010011101001000110000110110110110110001010100101010110011010111101000111100000001011111000100001101111011100111010000100100101001110101111111100111111100010111100001101001101110101010000100011111111001001010111101111100000101100011011111110101100110", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0101110100000101111010101000010010100100001010111110011000010010110110011001101100110010101111011011011011110011010110001100111001001110001011101000111111011110111010100000001010100101100100010011100010011011100010111001000011110100101100011011111000111110", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0011011000100010010010111010100000000010111110111110000000101100101010111101010110010000111011111000110000100011110001110100010110111111010001010001110100000000110001000110101010001011000011111011011110011100101110110101101010010111100010101101101011100010", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0101100110010000110101001100100101011001101010110100000111100001111001100011011100110000001111111110111001110001101101001011001000111111110000000110110111100110101101010101101010111110011010010100010001010101101000101101000011100110110000010000110110000100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0101111011110001111000110001010001101000110000111101001000101010110001110000101100011001101010111100100000010110000010001110000101101101101100111001101011101101111011101000101111000011101001100011110001110010110011001011001111010011000110101111111110101011", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0101010001111111000100010110110011010100111100010101100010101100011001001010010000110000110101011100010011010010000110010001001001110111011100111111110100011111001101000011010101000001110000000111100100001011111100011100101101001001101101101110011010100011", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1010111101101111110011110010100010111100010101100101101001001010111000100100110000011101111011111001110101000100000101101000101110010111111111001011100011011101110000011011101010000001001001111111000000011110101011111100000101000010101101100010110111001111", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0000111110101001101010111111001100011110110010111111110101100110100011010001110111011010100011000101001001010111111110011010001001000110101000010011011001111011100010111110110100000000100011101001110111001101001110000100010001000111011111011000010100111010", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0010110110110100011011010000110100010000011101001011110110110010001111111011000010100101101001110101011111001001001101000001011111001000111101011111101011101110010000001001000111111010010000101111100000011011100100111010011001100000100100111010111110100101", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1000001010001011100011101110100100100111010111101111000001010100101001101010001010110011001111111000011111010101110000011010111100011111001001111100000010110011110111010001001011101111011100011011110101110101001101111101101011100011001111101101111011100100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0100010011101000000110100100100100111110011110000000101111101000011101010010110111110001001010000110010110010011010101000101000000000001001011101010001010111010100100001100000101111110111100000101111000011111011101111011100100111000000000011101111000000000", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1110110110011000001110011101101010000110010100111010101101001111010011011110101100000111101100110000010001011110101011111100111110101100010101011011111010101011010001001000001011101110010001110010101111011100110000001111011001110101011011101011011111011100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b1100001110010001100001000001011001111011111100011101110001010010110000101111111010100011111011111011000101011110110111101101110011010100011110011111011000101001111100011111111101101111110100110110011001011111110011110000010000100001000011110001011101101001", r {}; + std::stringstream ss; l.writeBinary(ss, true); unsigned temp {}; + for(unsigned i = 0; i < 8; ++i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } + { + Aesi<256> l = "0b0111111111010011100110101111111011111110111011100101001000010100010101111110011001111110111110111100100010110001111001110100101101000001111010011110001001111111011101111101010101101010000001010001001101110100100101110110000101101000101000111111101101000100", r {}; + std::stringstream ss; l.writeBinary(ss, false); unsigned temp {}; + for(long long i = 7; i >= 0; --i) { + ss.read(reinterpret_cast(&temp), sizeof(unsigned)); + r.setBlock(i, temp); + } + EXPECT_EQ(l, r); + } +} \ No newline at end of file