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