Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
sha256_constraint.cpp
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
10
11namespace acir_format {
12
13template <typename Builder>
15{
17
18 std::array<field_ct, 8> hash_inputs; // previous (or initial) hash state
19 std::array<field_ct, 16> inputs; // message block to compress
20
21 // Get the witness assignment for each witness index
22 // AUDITTODO: We do not range-check the inputs here, assuming lookup tables in sha256_block
23 // provide implicit 32-bit constraints. However, analysis shows this assumption is incomplete:
24 // - inputs[0] is NEVER lookup-constrained
25 // - hash_values[3] and hash_values[7] are used in arithmetic before being lookup-constrained
26 // These values are only weakly bounded (~35 bits) by add_normalize overflow constraints.
27 // See AUDITTODO in stdlib/hash/sha256/sha256.cpp for details and recommended fix.
28 for (auto [input, witness_or_constant] : zip_view(inputs, constraint.inputs)) {
29 input = to_field_ct(witness_or_constant, builder);
30 }
31 for (auto [hash_input, witness_or_constant] : zip_view(hash_inputs, constraint.hash_values)) {
32 hash_input = to_field_ct(witness_or_constant, builder);
33 }
34
35 // Compute sha256 compression
37
38 // Constrain outputs to match expected witness indices
39 for (auto [output, result_idx] : zip_view(output_state, constraint.result)) {
40 field_ct result_witness = field_ct::from_witness_index(&builder, result_idx);
41 output.assert_equal(result_witness);
42 }
43}
44
46 const Sha256Compression& constraint);
48 const Sha256Compression& constraint);
49
50} // namespace acir_format
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 field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:62
AluTraceBuilder builder
Definition alu.test.cpp:124
AvmProvingInputs inputs
template void create_sha256_compression_constraints< bb::MegaCircuitBuilder >(bb::MegaCircuitBuilder &builder, const Sha256Compression &constraint)
void create_sha256_compression_constraints(Builder &builder, const Sha256Compression &constraint)
template void create_sha256_compression_constraints< bb::UltraCircuitBuilder >(bb::UltraCircuitBuilder &builder, const Sha256Compression &constraint)
bb::stdlib::field_t< Builder > to_field_ct(const WitnessOrConstant< typename Builder::FF > &input, Builder &builder)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::array< WitnessOrConstant< bb::fr >, 8 > hash_values
std::array< uint32_t, 8 > result
std::array< WitnessOrConstant< bb::fr >, 16 > inputs