Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
utils.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
8
12#include <vector>
13
14namespace acir_format {
15
16using namespace bb;
17using namespace bb::stdlib;
18
28template <typename Builder>
29static std::vector<field_t<Builder>> fields_from_witnesses(Builder& builder, std::span<const uint32_t> witness_indices)
30{
32 result.reserve(witness_indices.size());
33 for (const auto& idx : witness_indices) {
34 result.emplace_back(field_t<Builder>::from_witness_index(&builder, idx));
35 }
36 return result;
37}
38
47template <typename Builder> byte_array<Builder> fields_to_bytes(Builder& builder, std::vector<field_t<Builder>>& fields)
48{
50 for (auto& field : fields) {
51 // Construct byte array of length 1 from the field element
52 // The constructor enforces that `field` fits in one byte
53 byte_array<Builder> byte_to_append(field, /*num_bytes=*/1);
54 // Append the new byte to the result
55 result.write(byte_to_append);
56 }
57
58 return result;
59};
60
70template <typename T> std::vector<uint32_t> add_to_witness_and_track_indices(WitnessVector& witness, const T& input)
71{
72 std::vector<uint32_t> indices;
73
75 indices.emplace_back(witness.size());
76 witness.emplace_back(input.x);
77 indices.emplace_back(witness.size());
78 witness.emplace_back(input.y);
79 indices.emplace_back(witness.size());
80 witness.emplace_back(input.is_point_at_infinity() ? bb::fr(1) : bb::fr(0));
81 } else {
82 // If no other type is matched, we assume T is a span of values
83 indices.reserve(input.size());
84 auto witness_idx = static_cast<uint32_t>(witness.size());
85 for (const auto& value : input) {
86 witness.push_back(bb::fr(value));
87 indices.push_back(witness_idx++);
88 }
89 }
90
91 return indices;
92};
93
97inline uint32_t add_to_witness_and_track_indices(WitnessVector& witness, const bb::fr& input)
98{
99 uint32_t index = static_cast<uint32_t>(witness.size());
100 witness.emplace_back(input);
101
102 return index;
103}
104
108template <typename T, size_t N>
110{
111 std::vector<uint32_t> tracked_indices = add_to_witness_and_track_indices(witness, input);
113 std::ranges::copy(tracked_indices, indices.begin());
114 return indices;
115};
116
121template <typename Builder>
122void populate_fields(Builder& builder, const std::vector<field_t<Builder>>& fields, const std::vector<bb::fr>& values)
123{
124 for (auto [field, value] : zip_view(fields, values)) {
125 builder.set_variable(field.get_witness_index(), value);
126 }
127};
128
129} // namespace acir_format
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.
AluTraceBuilder builder
Definition alu.test.cpp:124
void populate_fields(Builder &builder, const std::vector< field_t< Builder > > &fields, const std::vector< bb::fr > &values)
Populate fields in the builder with the given values. To be used in mocking situations.
Definition utils.hpp:122
std::vector< bb::fr > WitnessVector
std::vector< uint32_t > add_to_witness_and_track_indices(WitnessVector &witness, const T &input)
Append values to a witness vector and track their indices.
Definition utils.hpp:70
byte_array< Builder > fields_to_bytes(Builder &builder, std::vector< field_t< Builder > > &fields)
Convert a vector of field_t elements to a byte_array enforcing each element to be a boolean.
Definition utils.hpp:47
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
General class for prime fields see Prime field documentation["field documentation"] for general imple...