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 // Internal constructors - do NOT add constraints
36 // Only for use by member functions (slice, reverse, from_constants)
37 byte_array(Builder* parent_context, bytes_t const& input);
38 byte_array(Builder* parent_context, bytes_t&& input);
39
40 // Create byte_array from constant values without adding range constraints
41 // Safe for padding and other constant data - constants can't be manipulated by the prover
42 static byte_array from_constants(Builder* parent_context, std::vector<uint8_t> const& input);
43
44 public:
45 explicit byte_array(Builder* parent_context, std::string const& input);
46 // Explicit to prevent implicit conversion from size_t to std::vector<uint8_t>
47 explicit byte_array(Builder* parent_context, std::vector<uint8_t> const& input);
48 // Explicit to prevent implicit conversions from size_t/int to field_t
49 explicit byte_array(const field_t<Builder>& input,
50 const size_t num_bytes = 32,
52
53 // Convenience method for creating constant padding (common use case)
54 static byte_array constant_padding(Builder* parent_context, size_t num_bytes, uint8_t value = 0)
55 {
56 return from_constants(parent_context, std::vector<uint8_t>(num_bytes, value));
57 }
58
59 // Copy and move operations
60 byte_array(const byte_array& other);
61 byte_array(byte_array&& other) noexcept;
62 byte_array& operator=(const byte_array& other);
63 byte_array& operator=(byte_array&& other) noexcept;
64 explicit operator field_t<Builder>() const;
65
66 field_t<Builder> operator[](const size_t index) const
67 {
68 BB_ASSERT_LT(index, values.size());
69 return values[index];
70 }
71
72 // Append another byte_array to this one
73 byte_array& write(byte_array const& other);
74
75 // Overwrite bytes starting at index with contents of other
76 byte_array& write_at(byte_array const& other, size_t index);
77
78 byte_array slice(size_t offset) const;
79 byte_array slice(size_t offset, size_t length) const;
80 byte_array reverse() const;
81
82 size_t size() const { return values.size(); }
83
84 bytes_t const& bytes() const { return values; }
85
86 Builder* get_context() const { return context; }
87
88 // Out-of-circuit methods
89 std::vector<uint8_t> get_value() const;
90
91 // OriginTag-specific methods
93 {
94 for (auto& value : values) {
95 value.set_origin_tag(tag);
96 }
97 }
98
100 {
102 for (auto& value : values) {
103 tag = bb::OriginTag(tag, value.tag);
104 }
105 return tag;
106 }
107
112 {
113 for (auto& value : values) {
114 value.set_free_witness_tag();
115 }
116 }
117
122 {
123 for (auto& value : values) {
124 value.unset_free_witness_tag();
125 }
126 }
127};
128
129template <typename Builder> inline std::ostream& operator<<(std::ostream& os, byte_array<Builder> const& arr)
130{
131 std::ios_base::fmtflags f(os.flags());
132 os << "[" << std::hex << std::setfill('0');
133 for (auto byte : arr.get_value()) {
134 os << ' ' << std::setw(2) << +(unsigned char)byte;
135 }
136 os << " ]";
137 os.flags(f);
138 return os;
139}
140} // namespace bb::stdlib
#define BB_ASSERT_LT(left, right,...)
Definition assert.hpp:137
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.
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
static byte_array from_constants(Builder *parent_context, std::vector< uint8_t > const &input)
Create a byte_array from constant values without adding range constraints.
size_t size() const
field_t< Builder > operator[](const size_t index) const
Builder * get_context() const
bb::OriginTag get_origin_tag() const
static byte_array constant_padding(Builder *parent_context, size_t num_bytes, uint8_t value=0)
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:245
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13