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: Complete, auditors: [Luke], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
9#include <array>
10
13
14#include "../../primitives/field/field.hpp"
15
16namespace bb::stdlib {
17
18template <typename Builder> class SHA256 {
19
21
22 static constexpr fr base{ 16 };
23
51 base.pow(32 - 7) + base.pow(32 - 18), // limb 0: rot7 + rot18
52 base.pow(32 - 18 + 3) + fr(1), // limb 1: rot18 + shift3
53 base.pow(10 - 7) + base.pow(32 - 18 + 10) + base.pow(10 - 3), // limb 2: rot7 + rot18 + shift3
54 base.pow(18 - 7) + fr(1) + base.pow(18 - 3), // limb 3: rot7 + rot18 + shift3
55 };
56
85 base.pow(32 - 17) + base.pow(32 - 19), // limb 0: rot17 + rot19
86 base.pow(32 - 17 + 3) + base.pow(32 - 19 + 3), // limb 1: rot17 + rot19
87 base.pow(32 - 19 + 10) + fr(1), // limb 2: rot19 + shift10
88 base.pow(18 - 17) + base.pow(18 - 10), // limb 3: rot17 + shift10
89 };
90
91 static constexpr std::array<uint64_t, 64> round_constants{
92 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
93 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
94 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
95 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
96 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
97 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
98 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
99 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
100 };
120 sparse_value(const field_ct& in = 0)
121 : normal(in)
122 {
123 if (normal.is_constant()) {
124 sparse = field_ct(in.get_context(),
125 bb::fr(numeric::map_into_sparse_form<16>(uint256_t(in.get_value()).data[0])));
126 }
127 }
128
129 sparse_value(const sparse_value& other) = default;
130 sparse_value(sparse_value&& other) = default;
131 sparse_value& operator=(const sparse_value& other) = default;
133 ~sparse_value() = default;
134
137 };
138
139 static sparse_witness_limbs convert_witness(const field_ct& input);
140
142
146
147 static field_ct add_normalize(const field_ct& a, const field_ct& b);
148
149 public:
151 const std::array<field_ct, 16>& input);
152
154};
155} // namespace bb::stdlib
static sparse_value map_into_maj_sparse_form(const field_ct &input)
Convert a field element to sparse form for use in the Majority function.
Definition sha256.cpp:219
static field_ct add_normalize(const field_ct &a, const field_ct &b)
Compute (a + b) mod 2^32 with circuit constraints.
Definition sha256.cpp:329
static std::array< field_ct, 64 > extend_witness(const std::array< field_ct, 16 > &w_in)
Extend the 16-word message block to 64 words per SHA-256 specification.
Definition sha256.cpp:82
static constexpr std::array< fr, 4 > right_multipliers
Multipliers for computing σ₁ during message schedule extension.
Definition sha256.hpp:84
static field_ct choose_with_sigma1(sparse_value &e, const sparse_value &f, const sparse_value &g)
Compute Σ₁(e) + Ch(e,f,g) for SHA-256 compression rounds.
Definition sha256.cpp:246
static constexpr std::array< uint64_t, 64 > round_constants
Definition sha256.hpp:91
static sparse_witness_limbs convert_witness(const field_ct &input)
Convert a 32-bit value to sparse limbs form for message schedule extension.
Definition sha256.cpp:45
static field_ct majority_with_sigma0(sparse_value &a, const sparse_value &b, const sparse_value &c)
Compute Σ₀(a) + Maj(a,b,c) for SHA-256 compression rounds.
Definition sha256.cpp:291
field_t< Builder > field_ct
Definition sha256.hpp:20
static constexpr std::array< fr, 4 > left_multipliers
Multipliers for computing σ₀ during message schedule extension.
Definition sha256.hpp:50
static sparse_value map_into_choose_sparse_form(const field_ct &input)
Convert a field element to sparse form for use in the Choose function.
Definition sha256.cpp:199
static std::array< field_ct, 8 > sha256_block(const std::array< field_ct, 8 > &h_init, const std::array< field_ct, 16 > &input)
Apply the SHA-256 compression function to a single 512-bit message block.
Definition sha256.cpp:379
static constexpr fr base
Definition sha256.hpp:22
bool is_constant() const
Definition field.hpp:429
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)
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:120
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
std::array< field_ct, 4 > rotated_limb_corrections
Definition sha256.hpp:115
sparse_witness_limbs & operator=(sparse_witness_limbs &&other)=default
std::array< field_ct, 4 > sparse_limbs
Definition sha256.hpp:113
sparse_witness_limbs(const field_ct &in=0)
Definition sha256.hpp:102