Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sha256_compression.cpp
Go to the documentation of this file.
1// This file is taken from barretenberg/crypto/sha256/sha256.cpp since the low level sha256_block function is not
2// exposed directly
4
5#include <array>
6#include <cstddef>
7#include <cstdint>
8
9namespace bb::avm2::simulation {
10
11constexpr uint32_t round_constants[64] = {
12 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
13 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
14 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
15 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
16 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
17 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
18 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
19 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
20};
21
22// Taken from barretenberg/crypto/sha256/sha256.cpp since it is not exposed directly
23std::array<uint32_t, 8> sha256_block(const std::array<uint32_t, 8>& h_init, const std::array<uint32_t, 16>& input)
24{
25 std::array<uint32_t, 64> w;
26
30 for (size_t i = 0; i < 16; ++i) {
31 w[i] = input[i];
32 }
33
37 for (size_t i = 16; i < 64; ++i) {
38 uint32_t s0 = ror(w[i - 15], 7) ^ ror(w[i - 15], 18) ^ (w[i - 15] >> 3);
39 uint32_t s1 = ror(w[i - 2], 17) ^ ror(w[i - 2], 19) ^ (w[i - 2] >> 10);
40 w[i] = w[i - 16] + w[i - 7] + s0 + s1;
41 }
42
46 uint32_t a = h_init[0];
47 uint32_t b = h_init[1];
48 uint32_t c = h_init[2];
49 uint32_t d = h_init[3];
50 uint32_t e = h_init[4];
51 uint32_t f = h_init[5];
52 uint32_t g = h_init[6];
53 uint32_t h = h_init[7];
54
58 for (size_t i = 0; i < 64; ++i) {
59 uint32_t S1 = ror(e, 6U) ^ ror(e, 11U) ^ ror(e, 25U);
60 uint32_t ch = (e & f) ^ (~e & g); // === (e & f) ^ (~e & g), `+` op is cheaper
61 uint32_t temp1 = h + S1 + ch + round_constants[i] + w[i];
62 uint32_t S0 = ror(a, 2U) ^ ror(a, 13U) ^ ror(a, 22U);
63 uint32_t maj = (a & b) ^ (a & c) ^ (b & c); // (a & (b + c - (T0 * 2))) + T0; // === (a & b) ^ (a & c) ^ (b & c)
64 uint32_t temp2 = S0 + maj;
65
66 h = g;
67 g = f;
68 f = e;
69 e = d + temp1;
70 d = c;
71 c = b;
72 b = a;
73 a = temp1 + temp2;
74 }
75
79 return {
80 a + h_init[0], b + h_init[1], c + h_init[2], d + h_init[3],
81 e + h_init[4], f + h_init[5], g + h_init[6], h + h_init[7],
82 };
83}
84} // namespace bb::avm2::simulation
FF a
FF b
std::array< uint32_t, 8 > sha256_block(const std::array< uint32_t, 8 > &h_init, const std::array< uint32_t, 16 > &input)
constexpr uint32_t ror(uint32_t val, uint32_t shift)
constexpr uint32_t round_constants[64]
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13