Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
databus.cpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Khashayar], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#include "databus.hpp"
8#include "../circuit_builders/circuit_builders.hpp"
10
11namespace bb::stdlib {
12
13template <typename Builder>
16{
17 // Set the context from the input entries
18 for (const auto& entry : entries_in) {
19 if (entry.get_context() != nullptr) {
20 context = entry.get_context();
21 break;
22 }
23 }
24 // Enforce that builder context is known at this stage. Otherwise first read will fail if the index is a constant.
25 BB_ASSERT(context != nullptr);
26
27 // Single-writer-per-bus_idx: a second writer's reads would alias the first writer's rows
28 // because read_bus_vector indexes the global column from row 0.
29 BB_ASSERT_EQ(context->get_bus_vector(static_cast<size_t>(bus_idx)).size(),
30 static_cast<size_t>(0),
31 "bus_vector::set_values: bus_idx already written.");
32
33 // Initialize the bus vector entries from the input entries which are un-normalized and possibly constants.
34 // append_to_bus_vector creates a binding init-read for each appended entry. The bus column is not part of the
35 // copy-constraint permutation, so this is what links bus_column[i] to its main-wire witness.
36 for (const auto& entry : entries_in) {
37 if (entry.is_constant()) { // create a constant witness from the constant
38 auto const_var_idx = context->put_constant_variable(entry.get_value());
39 entries.emplace_back(field_pt::from_witness_index(context, const_var_idx));
40 } else { // normalize the raw entry
41 entries.emplace_back(entry.normalize());
42 }
43 context->append_to_bus_vector(bus_idx, entries.back().get_witness_index());
44 }
45 length = entries.size();
46
47 // Preserve tags to restore them in future reads (following the ROM/RAM pattern)
48 _tags.resize(entries_in.size());
49 for (size_t i = 0; i < length; ++i) {
50 _tags[i] = entries_in[i].get_origin_tag();
51 }
52}
53
54template <typename Builder>
57{
58 // Ensure the read is valid
59 auto raw_index = static_cast<size_t>(uint256_t(index.get_value()).data[0]);
60 BB_ASSERT_LT(raw_index, length, "bus_vector: access out of bounds");
61
62 // The read index must be a witness; if constant, add it as a constant variable
63 uint32_t index_witness_idx = 0;
64 if (index.is_constant()) {
65 index_witness_idx = context->put_constant_variable(index.get_value());
66 } else {
67 index_witness_idx = index.get_witness_index();
68 }
69
70 // Read from the bus vector at the specified index. Creates a single read gate
71 uint32_t output_idx = context->read_bus_vector(bus_idx, index_witness_idx);
73
74 // If the index is legitimate, restore the tag (following the ROM/RAM pattern)
75 if (raw_index < length) {
76 result.set_origin_tag(_tags[raw_index]);
77 }
78 return result;
79}
80
82} // namespace bb::stdlib
#define BB_ASSERT(expression,...)
Definition assert.hpp:70
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:143
field_pt operator[](const field_pt &index) const
Read from the bus vector with a witness index value. Creates a read gate.
Definition databus.cpp:55
void set_values(const std::vector< field_pt > &entries_in)
Set the entries of the bus vector from possibly unnormalized or constant inputs.
Definition databus.cpp:14
static field_t from_witness_index(Builder *ctx, uint32_t witness_index)
Definition field.cpp:67
StrictMock< MockContext > context
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
VectorField result