Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
byte_array.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#include "../bool/bool.hpp"
9#include "../circuit_builders/circuit_builders_fwd.hpp"
10#include "../field/field.hpp"
12namespace bb::stdlib {
13
28template <typename Builder> class byte_array {
30
31 private:
34
35 public:
36 byte_array(Builder* parent_context = nullptr);
37 byte_array(Builder* parent_context, size_t const n);
38 byte_array(Builder* parent_context, std::string const& input);
39 byte_array(Builder* parent_context, std::vector<uint8_t> const& input);
40 byte_array(Builder* parent_context, bytes_t const& input);
41 byte_array(Builder* parent_context, bytes_t&& input);
42 byte_array(const field_t<Builder>& input,
43 const size_t num_bytes = 32,
45
46 byte_array(const byte_array& other);
47 byte_array(byte_array&& other);
48
49 byte_array& operator=(const byte_array& other);
51
52 explicit operator field_t<Builder>() const;
53
54 field_t<Builder> operator[](const size_t index) const
55 {
56 assert(values.size() > 0);
57 return values[index];
58 }
59
60 field_t<Builder>& operator[](const size_t index)
61 {
62 BB_ASSERT_LT(index, values.size());
63
64 return values[index];
65 }
66
67 byte_array& write(byte_array const& other);
68 byte_array& write_at(byte_array const& other, size_t index);
69
70 byte_array slice(size_t offset) const;
71 byte_array slice(size_t offset, size_t length) const;
72 byte_array reverse() const;
73
74 size_t size() const { return values.size(); }
75
76 bytes_t const& bytes() const { return values; }
77
78 Builder* get_context() const { return context; }
79
80 // Out-of-circuit methods
81 std::vector<uint8_t> get_value() const;
82 std::string get_string() const;
83
84 // OriginTag-specific methods
86 {
87 for (auto& value : values) {
88 value.set_origin_tag(tag);
89 }
90 }
91
93 {
94 bb::OriginTag tag{};
95 for (auto& value : values) {
96 tag = bb::OriginTag(tag, value.tag);
97 }
98 return tag;
99 }
100
105 {
106 for (auto& value : values) {
107 value.set_free_witness_tag();
108 }
109 }
110
115 {
116 for (auto& value : values) {
117 value.unset_free_witness_tag();
118 }
119 }
120};
121
122template <typename Builder> inline std::ostream& operator<<(std::ostream& os, byte_array<Builder> const& arr)
123{
124 std::ios_base::fmtflags f(os.flags());
125 os << "[" << std::hex << std::setfill('0');
126 for (auto byte : arr.get_value()) {
127 os << ' ' << std::setw(2) << +(unsigned char)byte;
128 }
129 os << " ]";
130 os.flags(f);
131 return os;
132}
133} // namespace bb::stdlib
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:115
Represents a dynamic array of bytes in-circuit.
byte_array slice(size_t offset) const
Slice bytes from the byte array starting at offset. Does not add any constraints.
byte_array reverse() const
Reverse the bytes in the byte array.
byte_array & write_at(byte_array const &other, size_t index)
Overwrites this byte_array starting at index with the contents of other. Asserts that the write does ...
void set_origin_tag(bb::OriginTag tag)
byte_array & write(byte_array const &other)
Appends the contents of another byte_array (other) to the end of this one.
void unset_free_witness_tag()
Unset the free witness flag for the byte array.
byte_array & operator=(const byte_array &other)
std::vector< uint8_t > get_value() const
A helper converting a byte_array into the vector of its uint8_t values.
bytes_t const & bytes() const
field_t< Builder > & operator[](const size_t index)
size_t size() const
field_t< Builder > operator[](const size_t index) const
std::string get_string() const
Given a byte_array, compute a vector containing the values of its entries and convert it to a string.
Builder * get_context() const
bb::OriginTag get_origin_tag() const
void set_free_witness_tag()
Set the free witness flag for the byte array.
typename std::vector< field_t< Builder > > bytes_t
uint8_t const size_t length
Definition data_store.hpp:9
ssize_t offset
Definition engine.cpp:36
std::ostream & operator<<(std::ostream &os, uint256_t const &a)
Definition uint256.hpp:246
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13