fork download
  1. #include <iostream>
  2. #include <array>
  3. using namespace std;
  4. #define uchar unsigned char
  5.  
  6. static std::uint32_t ror32(std::uint32_t value, int by) noexcept
  7. {
  8. return (value >> by) | (value << (32 - by));
  9. }
  10. static std::uint32_t shr32(std::uint32_t value, int by) noexcept { return value >> by; }
  11. static std::uint32_t rol32(std::uint32_t value, int by) noexcept
  12. {
  13. return (value << by) | (value >> (32 - by));
  14. }
  15. int main() {
  16. // your code goes here
  17. return 0;
  18. }
  19.  
  20. struct Sha256Algorithm
  21. {
  22. using underlying_type = std::uint32_t;
  23. using message_length_type = std::uint64_t;
  24. constexpr static std::size_t chunk_size = 64;
  25.  
  26. constexpr static std::size_t number_of_rounds = 64;
  27.  
  28. Sha256Algorithm() noexcept { clear(); }
  29.  
  30. void process_full_chunk(const std::array<uchar, chunk_size>& chunk) noexcept
  31. {
  32. std::uint32_t words[64];
  33.  
  34. // sha_fill_initial_words(&chunk[0], words);
  35.  
  36. for (std::size_t i = 16; i < number_of_rounds; ++i)
  37. {
  38. const auto w0 = words[i - 15];
  39. const auto s0 = ror32(w0, 7) ^ ror32(w0, 18) ^ shr32(w0, 3);
  40. const auto w1 = words[i - 2];
  41. const auto s1 = ror32(w1, 17) ^ ror32(w1, 19) ^ shr32(w1, 10);
  42. words[i] = words[i - 16] + s0 + words[i - 7] + s1;
  43. }
  44.  
  45. std::uint32_t local[8];
  46. std::copy(begin(), end(), std::begin(local));
  47.  
  48. for (std::size_t i = 0; i < number_of_rounds; ++i)
  49. {
  50. const auto a = local[0];
  51. const auto b = local[1];
  52. const auto c = local[2];
  53.  
  54. const auto s0 = ror32(a, 2) ^ ror32(a, 13) ^ ror32(a, 22);
  55. const auto maj = (a & b) ^ (a & c) ^ (b & c);
  56. const auto tmp1 = s0 + maj;
  57.  
  58. const auto e = local[4];
  59.  
  60. const auto s1 = ror32(e, 6) ^ ror32(e, 11) ^ ror32(e, 25);
  61. const auto ch = (e & local[5]) ^ (~e & local[6]);
  62. const auto tmp2 = local[7] + s1 + ch + round_constants[i] + words[i];
  63.  
  64. for (std::size_t j = 12; j > 0; --j)
  65. {
  66. local[j] = local[j - 1];
  67. }
  68. local[4] += tmp2;
  69. local[0] = tmp1 + tmp2;
  70. }
  71.  
  72. for (std::size_t i = 0; i < 8; ++i)
  73. {
  74. m_digest[i] += local[i];
  75. }
  76. }
  77.  
  78. void clear() noexcept
  79. {
  80. m_digest[0] = 0x6a09e667;
  81. m_digest[1] = 0xbb67ae85;
  82. m_digest[2] = 0x3c6ef372;
  83. m_digest[3] = 0xa54ff53a;
  84. m_digest[4] = 0x510e527f;
  85. m_digest[5] = 0x9b05688c;
  86. m_digest[6] = 0x1f83d9ab;
  87. m_digest[7] = 0x5be0cd19;
  88. }
  89.  
  90. constexpr static std::array<std::uint32_t, number_of_rounds> round_constants = {
  91. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  92. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  93. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  94. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  95. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  96. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  97. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  98. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
  99.  
  100. std::uint32_t* begin() noexcept { return &m_digest[0]; }
  101. std::uint32_t* end() noexcept { return &m_digest[8]; }
  102.  
  103. std::uint32_t m_digest[8];
  104. };
Success #stdin #stdout 0s 4240KB
stdin
Standard input is empty
stdout
Standard output is empty