Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake3_constraint.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Nishat], commit: 4a956ceb179c2fe855e4f1fd78f2594e7fc3f5ea}
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
11
12namespace acir_format {
13
14template <typename Builder> void create_blake3_constraints(Builder& builder, const Blake3Constraint& constraint)
15{
18
19 // Build input byte array by appending constrained byte_arrays
20 byte_array_ct arr = byte_array_ct::constant_padding(&builder, 0); // Start with empty array
21
22 for (const auto& witness_index_num_bits : constraint.inputs) {
23 auto witness_index = witness_index_num_bits.blackbox_input;
24 field_ct element = to_field_ct(witness_index, builder);
25
26 // byte_array_ct(field, num_bytes) constructor adds range constraints for each byte. Note that num_bytes =
27 // ceil(witness_index_num_bits.num_bits/8). Here, num_bits is set to 8 when constructing the vector of inputs in
28 // the Blake3 constraint. Hence, we set num_bytes = 1.
29 byte_array_ct element_bytes(element, 1);
30
31 // Safe write: both arr and element_bytes are constrained
32 arr.write(element_bytes);
33 }
34 BB_ASSERT_LTE(arr.size(), 1024U, "Barretenberg does not support blake3 inputs with more than 1024 bytes");
36
37 for (const auto& [output_byte, result_byte_idx] : zip_view(output_bytes.bytes(), constraint.result)) {
38 // Constrain each output byte to equal the corresponding witness
39 // This equality also constrains the result witnesses to be bytes
40 output_byte.assert_equal(field_ct::from_witness_index(&builder, result_byte_idx));
41 }
42}
43
45 const Blake3Constraint& constraint);
47 const Blake3Constraint& constraint);
48
49} // namespace acir_format
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:168
static byte_array_ct hash(const byte_array_ct &input)
Definition blake3s.cpp:182
Represents a dynamic array of bytes in-circuit.
byte_array & write(byte_array const &other)
Appends the contents of another byte_array (other) to the end of this one.
bytes_t const & bytes() const
size_t size() const
static byte_array constant_padding(Builder *parent_context, size_t num_bytes, uint8_t value=0)
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:62
AluTraceBuilder builder
Definition alu.test.cpp:124
template void create_blake3_constraints< bb::UltraCircuitBuilder >(bb::UltraCircuitBuilder &builder, const Blake3Constraint &constraint)
void create_blake3_constraints(Builder &builder, const Blake3Constraint &constraint)
bb::stdlib::field_t< Builder > to_field_ct(const WitnessOrConstant< typename Builder::FF > &input, Builder &builder)
template void create_blake3_constraints< bb::MegaCircuitBuilder >(bb::MegaCircuitBuilder &builder, const Blake3Constraint &constraint)
std::conditional_t< IsGoblinBigGroup< C, Fq, Fr, G >, element_goblin::goblin_element< C, goblin_field< C >, Fr, G >, element_default::element< C, Fq, Fr, G > > element
element wraps either element_default::element or element_goblin::goblin_element depending on parametr...
Definition biggroup.hpp:995
std::array< uint32_t, 32 > result
std::vector< Blake3Input > inputs