#include <iostream>
#include <array>
using namespace std;
#define uchar unsigned char

static std::uint32_t ror32(std::uint32_t value, int by) noexcept
{
	return (value >> by) | (value << (32 - by));
}
static std::uint32_t shr32(std::uint32_t value, int by) noexcept { return value >> by; }
static std::uint32_t rol32(std::uint32_t value, int by) noexcept
{
	return (value << by) | (value >> (32 - by));
}
int main() {
	// your code goes here
	return 0;
}

struct Sha256Algorithm
{
    using underlying_type = std::uint32_t;
    using message_length_type = std::uint64_t;
    constexpr static std::size_t chunk_size = 64;

    constexpr static std::size_t number_of_rounds = 64;

    Sha256Algorithm() noexcept { clear(); }

    void process_full_chunk(const std::array<uchar, chunk_size>& chunk) noexcept
    {
        std::uint32_t words[64];

//            sha_fill_initial_words(&chunk[0], words);

        for (std::size_t i = 16; i < number_of_rounds; ++i)
        {
            const auto w0 = words[i - 15];
            const auto s0 = ror32(w0, 7) ^ ror32(w0, 18) ^ shr32(w0, 3);
            const auto w1 = words[i - 2];
            const auto s1 = ror32(w1, 17) ^ ror32(w1, 19) ^ shr32(w1, 10);
            words[i] = words[i - 16] + s0 + words[i - 7] + s1;
        }

        std::uint32_t local[8];
        std::copy(begin(), end(), std::begin(local));

        for (std::size_t i = 0; i < number_of_rounds; ++i)
        {
            const auto a = local[0];
            const auto b = local[1];
            const auto c = local[2];

            const auto s0 = ror32(a, 2) ^ ror32(a, 13) ^ ror32(a, 22);
            const auto maj = (a & b) ^ (a & c) ^ (b & c);
            const auto tmp1 = s0 + maj;

            const auto e = local[4];

            const auto s1 = ror32(e, 6) ^ ror32(e, 11) ^ ror32(e, 25);
            const auto ch = (e & local[5]) ^ (~e & local[6]);
            const auto tmp2 = local[7] + s1 + ch + round_constants[i] + words[i];

            for (std::size_t j = 12; j > 0; --j)
            {
                local[j] = local[j - 1];
            }
            local[4] += tmp2;
            local[0] = tmp1 + tmp2;
        }

        for (std::size_t i = 0; i < 8; ++i)
        {
            m_digest[i] += local[i];
        }
    }

    void clear() noexcept
    {
        m_digest[0] = 0x6a09e667;
        m_digest[1] = 0xbb67ae85;
        m_digest[2] = 0x3c6ef372;
        m_digest[3] = 0xa54ff53a;
        m_digest[4] = 0x510e527f;
        m_digest[5] = 0x9b05688c;
        m_digest[6] = 0x1f83d9ab;
        m_digest[7] = 0x5be0cd19;
    }

    constexpr static std::array<std::uint32_t, number_of_rounds> round_constants = {
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
        0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
        0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
        0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};

    std::uint32_t* begin() noexcept { return &m_digest[0]; }
    std::uint32_t* end() noexcept { return &m_digest[8]; }

    std::uint32_t m_digest[8];
};