barretenberg
Loading...
Searching...
No Matches
byte_array.hpp
1#pragma once
2#include "../bool/bool.hpp"
3#include "../circuit_builders/circuit_builders_fwd.hpp"
4#include "../field/field.hpp"
5#include "../safe_uint/safe_uint.hpp"
6namespace proof_system::plonk {
7namespace stdlib {
8
9template <typename Builder> class byte_array {
10 public:
11 typedef std::vector<field_t<Builder>> bytes_t;
12
13 byte_array(Builder* parent_context = nullptr);
14 byte_array(Builder* parent_context, size_t const n);
15 byte_array(Builder* parent_context, std::string const& input);
16 byte_array(Builder* parent_context, std::vector<uint8_t> const& input);
17 byte_array(Builder* parent_context, bytes_t const& input);
18 byte_array(Builder* parent_context, bytes_t&& input);
19 byte_array(const field_t<Builder>& input, const size_t num_bytes = 32);
20 byte_array(const safe_uint_t<Builder>& input, const size_t num_bytes = 32);
21
22 template <typename ItBegin, typename ItEnd>
23 byte_array(Builder* parent_context, ItBegin const& begin, ItEnd const& end)
24 : context(parent_context)
25 , values(begin, end)
26 {
27 for (auto& val : values) {
28 val = val.normalize();
29 }
30 }
31
32 byte_array(const byte_array& other);
33 byte_array(byte_array&& other);
34
35 byte_array& operator=(const byte_array& other);
36 byte_array& operator=(byte_array&& other);
37
38 explicit operator field_t<Builder>() const;
39
40 field_t<Builder> operator[](const size_t index) const
41 {
42 assert(values.size() > 0);
43 return values[index];
44 }
45
46 byte_array& write(byte_array const& other);
47 byte_array& write_at(byte_array const& other, size_t index);
48
49 byte_array slice(size_t offset) const;
50 byte_array slice(size_t offset, size_t length) const;
51 byte_array reverse() const;
52
53 size_t size() const { return values.size(); }
54
55 bytes_t const& bytes() const { return values; }
56
57 bool_t<Builder> get_bit(size_t index) const;
58
59 void set_bit(size_t index, bool_t<Builder> const& value);
60
61 void set_byte(size_t index, const field_t<Builder>& byte_val)
62 {
63 ASSERT(index < values.size());
64 values[index] = byte_val;
65 }
66
67 void set_context(Builder* ctx)
68 {
69 ASSERT(context == nullptr);
70 context = ctx;
71 }
72
73 Builder* get_context() const { return context; }
74
75 std::vector<uint8_t> get_value() const;
76
77 std::string get_string() const;
78
79 private:
80 Builder* context;
81 bytes_t values;
82
83 struct byte_slice {
87 };
88 byte_slice split_byte(const size_t bit_index) const;
89};
90
91template <typename Builder> inline std::ostream& operator<<(std::ostream& os, byte_array<Builder> const& arr)
92{
93 std::ios_base::fmtflags f(os.flags());
94 os << "[" << std::hex << std::setfill('0');
95 for (auto byte : arr.get_value()) {
96 os << ' ' << std::setw(2) << +(unsigned char)byte;
97 }
98 os << " ]";
99 os.flags(f);
100 return os;
101}
102
103EXTERN_STDLIB_TYPE(byte_array);
104
105} // namespace stdlib
106} // namespace proof_system::plonk
Definition: standard_circuit_builder.hpp:12
Definition: byte_array.hpp:9
bool_t< Builder > get_bit(size_t index) const
Extract a bit from the byte array.
Definition: byte_array.cpp:298
void set_bit(size_t index, bool_t< Builder > const &value)
Set a bit in the byte array.
Definition: byte_array.cpp:320
byte_array reverse() const
Reverse the bytes in the byte array.
Definition: byte_array.cpp:264
Definition: field.hpp:10
Definition: safe_uint.hpp:17
Definition: widget.bench.cpp:13