14constexpr uint32_t init_constants[8]{ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
15 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
18 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
19 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
20 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
21 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
22 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
23 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
24 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
25 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
28constexpr uint32_t
ror(uint32_t val, uint32_t shift)
30 return (val >> (shift & 31U)) | (val << (32U - (shift & 31U)));
38 input[0] = init_constants[0];
39 input[1] = init_constants[1];
40 input[2] = init_constants[2];
41 input[3] = init_constants[3];
42 input[4] = init_constants[4];
43 input[5] = init_constants[5];
44 input[6] = init_constants[6];
45 input[7] = init_constants[7];
50 std::array<uint32_t, 64> w;
55 for (
size_t i = 0; i < 16; ++i) {
62 for (
size_t i = 16; i < 64; ++i) {
63 uint32_t s0 =
ror(w[i - 15], 7) ^
ror(w[i - 15], 18) ^ (w[i - 15] >> 3);
64 uint32_t s1 =
ror(w[i - 2], 17) ^
ror(w[i - 2], 19) ^ (w[i - 2] >> 10);
65 w[i] = w[i - 16] + w[i - 7] + s0 + s1;
71 uint32_t
a = h_init[0];
72 uint32_t
b = h_init[1];
73 uint32_t c = h_init[2];
74 uint32_t d = h_init[3];
75 uint32_t e = h_init[4];
76 uint32_t f = h_init[5];
77 uint32_t g = h_init[6];
78 uint32_t h = h_init[7];
83 for (
size_t i = 0; i < 64; ++i) {
84 uint32_t S1 =
ror(e, 6U) ^
ror(e, 11U) ^
ror(e, 25U);
85 uint32_t ch = (e & f) ^ (~e & g);
86 uint32_t temp1 = h + S1 + ch + round_constants[i] + w[i];
88 uint32_t maj = (
a &
b) ^ (
a & c) ^ (
b & c);
89 uint32_t temp2 = S0 + maj;
104 std::array<uint32_t, 8> output;
105 output[0] =
a + h_init[0];
106 output[1] =
b + h_init[1];
107 output[2] = c + h_init[2];
108 output[3] = d + h_init[3];
109 output[4] = e + h_init[4];
110 output[5] = f + h_init[5];
111 output[6] = g + h_init[6];
112 output[7] = h + h_init[7];
119 std::array<uint32_t, 8> result;
122 memcpy((
void*)&hash_input[0], (
void*)&input[0], 64);
124 for (
size_t j = 0; j < hash_input.size(); ++j) {
125 hash_input[j] = __builtin_bswap32(hash_input[j]);
131 memcpy((
void*)&output[0], (
void*)&result[0], 32);
133 uint32_t* output_uint32 = (uint32_t*)&output[0];
134 for (
size_t j = 0; j < 8; ++j) {
135 output_uint32[j] = __builtin_bswap32(output_uint32[j]);
144 std::vector<uint8_t> message_schedule;
147 uint64_t l = message_schedule.size() * 8;
148 message_schedule.push_back(0x80);
150 uint32_t num_zero_bytes = ((448U - (message_schedule.size() << 3U)) & 511U) >> 3U;
152 for (
size_t i = 0; i < num_zero_bytes; ++i) {
153 message_schedule.push_back(0x00);
155 for (
size_t i = 0; i < 8; ++i) {
156 uint8_t
byte =
static_cast<uint8_t
>(l >> (uint64_t)(56 - (i * 8)));
157 message_schedule.push_back(
byte);
159 std::array<uint32_t, 8> rolling_hash;
161 const size_t num_blocks = message_schedule.size() / 64;
162 for (
size_t i = 0; i < num_blocks; ++i) {
164 memcpy((
void*)&hash_input[0], (
void*)&message_schedule[i * 64], 64);
166 for (
size_t j = 0; j < hash_input.size(); ++j) {
167 hash_input[j] = __builtin_bswap32(hash_input[j]);
174 memcpy((
void*)&output[0], (
void*)&rolling_hash[0], 32);
176 uint32_t* output_uint32 = (uint32_t*)&output[0];
177 for (
size_t j = 0; j < 8; ++j) {
178 output_uint32[j] = __builtin_bswap32(output_uint32[j]);
185template Sha256Hash sha256<std::vector<uint8_t>>(
const std::vector<uint8_t>& input);
186template Sha256Hash sha256<std::array<uint8_t, 32>>(
const std::array<uint8_t, 32>& input);
#define BB_ASSERT_EQ(actual, expected,...)
constexpr uint32_t round_constants[64]
void prepare_constants(std::array< uint32_t, 8 > &input)
template Sha256Hash sha256< std::string >(const std::string &input)
Sha256Hash sha256(const ByteContainer &input)
std::array< uint8_t, 32 > Sha256Hash
std::array< uint32_t, 8 > sha256_block(const std::array< uint32_t, 8 > &h_init, const std::array< uint32_t, 16 > &input)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
constexpr uint64_t ror(uint64_t val, uint64_t shift)