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: 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#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 ASSERT(context != nullptr);
26
27 // Initialize the bus vector entries from the input entries which are un-normalized and possibly constants
28 for (const auto& entry : entries_in) {
29 if (entry.is_constant()) { // create a constant witness from the constant
30 auto const_var_idx = context->put_constant_variable(entry.get_value());
31 entries.emplace_back(field_pt::from_witness_index(context, const_var_idx));
32 } else { // normalize the raw entry
33 entries.emplace_back(entry.normalize());
34 }
35 // Add the entry to the bus vector data
36 context->append_to_bus_vector(bus_idx, entries.back().get_witness_index());
37 }
38 length = entries.size();
39}
40
41template <typename Builder>
44{
45 // Ensure the read is valid
46 auto raw_index = static_cast<size_t>(uint256_t(index.get_value()).data[0]);
47 if (raw_index >= length) {
48 context->failure("bus_vector: access out of bounds");
49 }
50
51 // The read index must be a witness; if constant, add it as a constant variable
52 uint32_t index_witness_idx = 0;
53 if (index.is_constant()) {
54 index_witness_idx = context->put_constant_variable(index.get_value());
55 } else {
56 index_witness_idx = index.normalize().get_witness_index();
57 }
58
59 // Read from the bus vector at the specified index. Creates a single read gate
60 uint32_t output_idx = context->read_bus_vector(bus_idx, index_witness_idx);
61 return field_pt::from_witness_index(context, output_idx);
62}
63
65} // namespace bb::stdlib
#define ASSERT(expression,...)
Definition assert.hpp:49
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:42
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:59
StrictMock< MockContext > context
uint8_t const size_t length
Definition data_store.hpp:9
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13