Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sha256.hpp
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#pragma once
10#include <array>
11
14
15#include "../../primitives/field/field.hpp"
16
17namespace bb::stdlib {
18
19template <typename Builder> class SHA256 {
20
37
38 static constexpr uint64_t init_constants[8]{ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
39 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
40
41 static constexpr fr base{ 16 };
42
44 (base.pow(32 - 7) + base.pow(32 - 18)),
45 (base.pow(32 - 18 + 3) + 1),
46 (base.pow(32 - 18 + 10) + base.pow(10 - 7) + base.pow(10 - 3)),
47 (base.pow(18 - 7) + base.pow(18 - 3) + 1),
48 };
49
51 base.pow(32 - 17) + base.pow(32 - 19),
52 base.pow(32 - 17 + 3) + base.pow(32 - 19 + 3),
53 base.pow(32 - 19 + 10) + fr(1),
54 base.pow(18 - 17) + base.pow(18 - 10),
55 };
56
57 static constexpr uint64_t round_constants[64]{
58 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
59 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
60 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
61 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
62 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
63 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
64 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
65 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
66 };
87 struct sparse_value {
88 sparse_value(const field_ct& in = 0)
89 {
90 normal = in;
91 if (normal.witness_index == IS_CONSTANT) {
92 sparse = field_ct(in.get_context(),
93 bb::fr(numeric::map_into_sparse_form<16>(uint256_t(in.get_value()).data[0])));
94 }
95 }
96
97 sparse_value(const sparse_value& other) = default;
98 sparse_value(sparse_value&& other) = default;
99
100 sparse_value& operator=(const sparse_value& other) = default;
102
105 };
106
107 static void prepare_constants(std::array<field_ct, 8>& input);
109
110 static field_ct choose(sparse_value& e, const sparse_value& f, const sparse_value& g);
111
112 static field_ct majority(sparse_value& a, const sparse_value& b, const sparse_value& c);
115
116 static field_ct add_normalize(const field_ct& a, const field_ct& b);
117
118 public:
120 const std::array<field_ct, 16>& input);
121
123
124 static byte_array<Builder> hash(const byte_array_ct& input);
125};
126} // namespace bb::stdlib
static field_ct add_normalize(const field_ct &a, const field_ct &b)
Definition sha256.cpp:213
static constexpr uint64_t init_constants[8]
Definition sha256.hpp:38
static std::array< field_ct, 64 > extend_witness(const std::array< field_ct, 16 > &w_in)
Definition sha256.cpp:60
static constexpr std::array< fr, 4 > right_multipliers
Definition sha256.hpp:50
static field_ct majority(sparse_value &a, const sparse_value &b, const sparse_value &c)
Definition sha256.cpp:188
static void prepare_constants(std::array< field_ct, 8 > &input)
Definition sha256.cpp:26
static field_ct choose(sparse_value &e, const sparse_value &f, const sparse_value &g)
Definition sha256.cpp:162
field_t< Builder > field_ct
Definition sha256.hpp:21
static sparse_value map_into_choose_sparse_form(const field_ct &e)
Definition sha256.cpp:142
static byte_array< Builder > hash(const byte_array_ct &input)
Definition sha256.cpp:308
static sparse_value map_into_maj_sparse_form(const field_ct &e)
Definition sha256.cpp:152
static constexpr std::array< fr, 4 > left_multipliers
Definition sha256.hpp:43
static sparse_witness_limbs convert_witness(const field_ct &w)
Definition sha256.cpp:34
static std::array< field_ct, 8 > sha256_block(const std::array< field_ct, 8 > &h_init, const std::array< field_ct, 16 > &input)
Definition sha256.cpp:237
static constexpr uint64_t round_constants[64]
Definition sha256.hpp:57
static constexpr fr base
Definition sha256.hpp:41
Represents a dynamic array of bytes in-circuit.
uint32_t witness_index
Definition field.hpp:132
FF a
FF b
void g(field_t< Builder > state[BLAKE_STATE_SIZE], size_t a, size_t b, size_t c, size_t d, field_t< Builder > x, field_t< Builder > y, const bool last_update=false)
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
BB_INLINE constexpr field pow(const uint256_t &exponent) const noexcept
sparse_value(const sparse_value &other)=default
sparse_value & operator=(const sparse_value &other)=default
sparse_value(sparse_value &&other)=default
sparse_value(const field_ct &in=0)
Definition sha256.hpp:88
sparse_value & operator=(sparse_value &&other)=default
sparse_witness_limbs(sparse_witness_limbs &&other)=default
sparse_witness_limbs & operator=(const sparse_witness_limbs &other)=default
sparse_witness_limbs(const sparse_witness_limbs &other)=default
sparse_witness_limbs & operator=(sparse_witness_limbs &&other)=default
std::array< field_ct, 4 > rotated_limbs
Definition sha256.hpp:83
std::array< field_ct, 4 > sparse_limbs
Definition sha256.hpp:81
sparse_witness_limbs(const field_ct &in=0)
Definition sha256.hpp:68