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
13namespace {
14constexpr uint32_t init_constants[8]{ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
15 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
16
17constexpr uint32_t round_constants[64]{
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
26};
27
28constexpr uint32_t ror(uint32_t val, uint32_t shift)
29{
30 return (val >> (shift & 31U)) | (val << (32U - (shift & 31U)));
31}
32
33} // namespace
34
35namespace bb::crypto {
36void prepare_constants(std::array<uint32_t, 8>& input)
37{
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];
46}
47
48std::array<uint32_t, 8> sha256_block(const std::array<uint32_t, 8>& h_init, const std::array<uint32_t, 16>& input)
49{
50 std::array<uint32_t, 64> w;
51
55 for (size_t i = 0; i < 16; ++i) {
56 w[i] = input[i];
57 }
58
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;
66 }
67
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];
79
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); // === (e & f) ^ (~e & g), `+` op is cheaper
86 uint32_t temp1 = h + S1 + ch + round_constants[i] + w[i];
87 uint32_t S0 = ror(a, 2U) ^ ror(a, 13U) ^ ror(a, 22U);
88 uint32_t maj = (a & b) ^ (a & c) ^ (b & c); // (a & (b + c - (T0 * 2))) + T0; // === (a & b) ^ (a & c) ^ (b & c)
89 uint32_t temp2 = S0 + maj;
90
91 h = g;
92 g = f;
93 f = e;
94 e = d + temp1;
95 d = c;
96 c = b;
97 b = a;
98 a = temp1 + temp2;
99 }
100
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];
113 return output;
114}
115
116Sha256Hash sha256_block(const std::vector<uint8_t>& input)
117{
118 BB_ASSERT_EQ(input.size(), 64U);
119 std::array<uint32_t, 8> result;
120 prepare_constants(result);
121 std::array<uint32_t, 16> hash_input;
122 memcpy((void*)&hash_input[0], (void*)&input[0], 64);
123 if (is_little_endian()) {
124 for (size_t j = 0; j < hash_input.size(); ++j) {
125 hash_input[j] = __builtin_bswap32(hash_input[j]);
126 }
127 }
128 result = sha256_block(result, hash_input);
129
130 Sha256Hash output;
131 memcpy((void*)&output[0], (void*)&result[0], 32);
132 if (is_little_endian()) {
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]);
136 }
137 }
138
139 return output;
140}
141
142template <typename ByteContainer> Sha256Hash sha256(const ByteContainer& input)
143{
144 std::vector<uint8_t> message_schedule;
145
146 std::copy(input.begin(), input.end(), std::back_inserter(message_schedule));
147 uint64_t l = message_schedule.size() * 8;
148 message_schedule.push_back(0x80);
149
150 uint32_t num_zero_bytes = ((448U - (message_schedule.size() << 3U)) & 511U) >> 3U;
151
152 for (size_t i = 0; i < num_zero_bytes; ++i) {
153 message_schedule.push_back(0x00);
154 }
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);
158 }
159 std::array<uint32_t, 8> rolling_hash;
160 prepare_constants(rolling_hash);
161 const size_t num_blocks = message_schedule.size() / 64;
162 for (size_t i = 0; i < num_blocks; ++i) {
163 std::array<uint32_t, 16> hash_input;
164 memcpy((void*)&hash_input[0], (void*)&message_schedule[i * 64], 64);
165 if (is_little_endian()) {
166 for (size_t j = 0; j < hash_input.size(); ++j) {
167 hash_input[j] = __builtin_bswap32(hash_input[j]);
168 }
169 }
170 rolling_hash = sha256_block(rolling_hash, hash_input);
171 }
172
173 Sha256Hash output;
174 memcpy((void*)&output[0], (void*)&rolling_hash[0], 32);
175 if (is_little_endian()) {
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]);
179 }
180 }
181
182 return output;
183}
184
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);
187template Sha256Hash sha256<std::string>(const std::string& input);
188template Sha256Hash sha256<std::span<uint8_t>>(const std::span<uint8_t>& input);
189
190} // namespace bb::crypto
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:59
FF a
FF b
constexpr uint32_t round_constants[64]
void prepare_constants(std::array< uint32_t, 8 > &input)
Definition sha256.cpp:36
template Sha256Hash sha256< std::string >(const std::string &input)
Sha256Hash sha256(const ByteContainer &input)
Definition sha256.cpp:142
std::array< uint8_t, 32 > Sha256Hash
Definition sha256.hpp:18
std::array< uint32_t, 8 > sha256_block(const std::array< uint32_t, 8 > &h_init, const std::array< uint32_t, 16 > &input)
Definition sha256.cpp:48
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
bool is_little_endian()
Definition net.hpp:10
constexpr uint64_t ror(uint64_t val, uint64_t shift)