Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sha256.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#include "./sha256.hpp"
10#include <array>
11#include <memory.h>
12#include <span>
13
14namespace {
15
16// Initial hash values H[0..7] (FIPS 180-4 Section 5.3.3)
17// First 32 bits of the fractional parts of the square roots of the first 8 primes (2..19)
18constexpr uint32_t init_constants[8]{ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
19 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
20
21// Round constants K[0..63] (FIPS 180-4 Section 4.2.2)
22// First 32 bits of the fractional parts of the cube roots of the first 64 primes (2..311)
23constexpr uint32_t round_constants[64]{
24 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
25 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
26 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
27 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
28 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
29 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
30 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
31 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
32};
33
43constexpr uint32_t ror(uint32_t val, uint32_t shift)
44{
45 return (val >> (shift & 31U)) | (val << (32U - (shift & 31U)));
46}
47
48} // namespace
49
50namespace bb::crypto {
51void prepare_constants(std::array<uint32_t, 8>& input)
52{
53 input[0] = init_constants[0];
54 input[1] = init_constants[1];
55 input[2] = init_constants[2];
56 input[3] = init_constants[3];
57 input[4] = init_constants[4];
58 input[5] = init_constants[5];
59 input[6] = init_constants[6];
60 input[7] = init_constants[7];
61}
62
73std::array<uint32_t, 8> sha256_block(const std::array<uint32_t, 8>& h_init, const std::array<uint32_t, 16>& input)
74{
75 std::array<uint32_t, 64> w;
76
80 for (size_t i = 0; i < 16; ++i) {
81 w[i] = input[i];
82 }
83
87 for (size_t i = 16; i < 64; ++i) {
88 uint32_t s0 = ror(w[i - 15], 7) ^ ror(w[i - 15], 18) ^ (w[i - 15] >> 3);
89 uint32_t s1 = ror(w[i - 2], 17) ^ ror(w[i - 2], 19) ^ (w[i - 2] >> 10);
90 w[i] = w[i - 16] + w[i - 7] + s0 + s1;
91 }
92
96 uint32_t a = h_init[0];
97 uint32_t b = h_init[1];
98 uint32_t c = h_init[2];
99 uint32_t d = h_init[3];
100 uint32_t e = h_init[4];
101 uint32_t f = h_init[5];
102 uint32_t g = h_init[6];
103 uint32_t h = h_init[7];
104
108 for (size_t i = 0; i < 64; ++i) {
109 uint32_t S1 = ror(e, 6U) ^ ror(e, 11U) ^ ror(e, 25U);
110 uint32_t ch = (e & f) ^ (~e & g); // === (e & f) ^ (~e & g), `+` op is cheaper
111 uint32_t temp1 = h + S1 + ch + round_constants[i] + w[i];
112 uint32_t S0 = ror(a, 2U) ^ ror(a, 13U) ^ ror(a, 22U);
113 uint32_t maj = (a & b) ^ (a & c) ^ (b & c); // (a & (b + c - (T0 * 2))) + T0; // === (a & b) ^ (a & c) ^ (b & c)
114 uint32_t temp2 = S0 + maj;
115
116 h = g;
117 g = f;
118 f = e;
119 e = d + temp1;
120 d = c;
121 c = b;
122 b = a;
123 a = temp1 + temp2;
124 }
125
129 std::array<uint32_t, 8> output;
130 output[0] = a + h_init[0];
131 output[1] = b + h_init[1];
132 output[2] = c + h_init[2];
133 output[3] = d + h_init[3];
134 output[4] = e + h_init[4];
135 output[5] = f + h_init[5];
136 output[6] = g + h_init[6];
137 output[7] = h + h_init[7];
138 return output;
139}
140
150template <typename ByteContainer> Sha256Hash sha256(const ByteContainer& input)
151{
152 std::vector<uint8_t> message_schedule;
153
154 std::copy(input.begin(), input.end(), std::back_inserter(message_schedule));
155 uint64_t l = message_schedule.size() * 8;
156 message_schedule.push_back(0x80);
157
158 uint32_t num_zero_bytes = ((448U - (message_schedule.size() << 3U)) & 511U) >> 3U;
159
160 for (size_t i = 0; i < num_zero_bytes; ++i) {
161 message_schedule.push_back(0x00);
162 }
163 for (size_t i = 0; i < 8; ++i) {
164 uint8_t byte = static_cast<uint8_t>(l >> (uint64_t)(56 - (i * 8)));
165 message_schedule.push_back(byte);
166 }
167 std::array<uint32_t, 8> rolling_hash;
168 prepare_constants(rolling_hash);
169 const size_t num_blocks = message_schedule.size() / 64;
170 for (size_t i = 0; i < num_blocks; ++i) {
171 std::array<uint32_t, 16> hash_input;
172 memcpy((void*)&hash_input[0], (void*)&message_schedule[i * 64], 64);
173 if (is_little_endian()) {
174 for (size_t j = 0; j < hash_input.size(); ++j) {
175 hash_input[j] = __builtin_bswap32(hash_input[j]);
176 }
177 }
178 rolling_hash = sha256_block(rolling_hash, hash_input);
179 }
180
181 Sha256Hash output;
182 memcpy((void*)&output[0], (void*)&rolling_hash[0], 32);
183 if (is_little_endian()) {
184 uint32_t* output_uint32 = (uint32_t*)&output[0];
185 for (size_t j = 0; j < 8; ++j) {
186 output_uint32[j] = __builtin_bswap32(output_uint32[j]);
187 }
188 }
189
190 return output;
191}
192
193template Sha256Hash sha256<std::vector<uint8_t>>(const std::vector<uint8_t>& input);
194template Sha256Hash sha256<std::array<uint8_t, 32>>(const std::array<uint8_t, 32>& input);
195template Sha256Hash sha256<std::string>(const std::string& input);
196template Sha256Hash sha256<std::span<uint8_t>>(const std::span<uint8_t>& input);
197
198} // namespace bb::crypto
FF a
FF b
constexpr uint32_t ror(uint32_t val, uint32_t shift)
constexpr uint32_t round_constants[64]
void prepare_constants(std::array< uint32_t, 8 > &input)
Definition sha256.cpp:51
template Sha256Hash sha256< std::string >(const std::string &input)
Sha256Hash sha256(const ByteContainer &input)
SHA-256 hash function (FIPS 180-4)
Definition sha256.cpp:150
std::array< uint8_t, 32 > Sha256Hash
Definition sha256.hpp:17
std::array< uint32_t, 8 > sha256_block(const std::array< uint32_t, 8 > &h_init, const std::array< uint32_t, 16 > &input)
SHA-256 compression function (FIPS 180-4 Section 6.2.2)
Definition sha256.cpp:73
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
bool is_little_endian()
Definition net.hpp:10