Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
blake2s_constraint.cpp
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
10#include "round.hpp"
11
12namespace acir_format {
13
14using namespace bb;
15
16template <typename Builder> void create_blake2s_constraints(Builder& builder, const Blake2sConstraint& constraint)
17{
20
21 // Build input byte array by appending constrained byte_arrays
22 byte_array_ct arr = byte_array_ct::constant_padding(&builder, 0); // Start with empty array
23
24 for (const auto& witness_index_num_bits : constraint.inputs) {
25 auto witness_index = witness_index_num_bits.blackbox_input;
26 auto num_bits = witness_index_num_bits.num_bits;
27
28 // XXX: The implementation requires us to truncate the element to the nearest byte and not bit
29 auto num_bytes = round_to_nearest_byte(num_bits);
30
31 field_ct element = to_field_ct(witness_index, builder);
32
33 // byte_array_ct(field, num_bytes) constructor adds range constraints for each byte
34 byte_array_ct element_bytes(element, num_bytes);
35
36 // Safe write: both arr and element_bytes are constrained
37 arr.write(element_bytes);
38 }
39
41
42 for (size_t i = 0; i < output_bytes.size(); ++i) {
43 output_bytes[i].assert_equal(field_ct::from_witness_index(&builder, constraint.result[i]));
44 }
45}
46
48 const Blake2sConstraint& constraint);
50 const Blake2sConstraint& constraint);
51
52} // namespace acir_format
static byte_array_ct hash(const byte_array_ct &input)
Definition blake2s.cpp:133
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.
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
void create_blake2s_constraints(Builder &builder, const Blake2sConstraint &constraint)
uint32_t round_to_nearest_byte(uint32_t num_bits)
Definition round.cpp:23
template void create_blake2s_constraints< MegaCircuitBuilder >(MegaCircuitBuilder &builder, const Blake2sConstraint &constraint)
template void create_blake2s_constraints< UltraCircuitBuilder >(UltraCircuitBuilder &builder, const Blake2sConstraint &constraint)
bb::stdlib::field_t< Builder > to_field_ct(const WitnessOrConstant< typename Builder::FF > &input, Builder &builder)
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
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::vector< Blake2sInput > inputs
std::array< uint32_t, 32 > result