22 std::ifstream file(filename, std::ios::binary | std::ios::ate);
27 file.seekg(0, std::ios::end);
28 return (
size_t)file.tellg();
31inline std::vector<uint8_t>
read_file(
const std::string& filename,
size_t bytes = 0)
36 constexpr size_t CHUNK = 65536;
37 auto read_chunked = [](
int fd,
const std::string& name) {
38 std::vector<uint8_t> result;
43 result.resize(total + CHUNK);
44 n =
::read(fd, result.data() + total, CHUNK);
48 total +=
static_cast<size_t>(n);
52 THROW std::runtime_error(
"Failed to read from " + name +
": " + strerror(errno));
58 if (filename ==
"-") {
59 return read_chunked(STDIN_FILENO,
"stdin");
66 auto raw_size = std::filesystem::file_size(filename, ec);
69 int fd = open(filename.c_str(), O_RDONLY);
71 THROW std::runtime_error(
"Unable to open file: " + filename +
" (" + strerror(errno) +
")");
74 std::vector<uint8_t> fileData;
75 if (!file_size.has_value()) {
77 fileData = read_chunked(fd, filename);
81 size_t to_read = bytes == 0 ? *file_size : bytes;
82 fileData.resize(to_read);
84 while (total < to_read) {
85 ssize_t got =
::read(fd, fileData.data() + total,
static_cast<unsigned int>(to_read - total));
88 THROW std::runtime_error(
"Failed to read file: " + filename +
" (" + strerror(errno) +
")");
93 total +=
static_cast<size_t>(got);
95 fileData.resize(total);
106 int fd = open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
108 THROW std::runtime_error(
"Failed to open file for writing: " + filename +
" (" + strerror(errno) +
")");
112 size_t total_written = 0;
113 while (total_written <
data.size()) {
115 ::write(fd,
data.data() + total_written,
static_cast<unsigned int>(
data.size() - total_written));
118 THROW std::runtime_error(
"Failed to write to file: " + filename +
" (" + strerror(errno) +
")");
120 total_written +=
static_cast<size_t>(written);
127 std::stringstream ss;
129 for (
size_t i = 0; i < fields.size(); ++i) {
130 ss <<
'"' << fields[i] <<
'"';
131 if (i != fields.size() - 1) {
146inline std::vector<uint8_t>
read_vk_file(
const std::filesystem::path& vk_path)
150 }
catch (
const std::runtime_error&) {
151 THROW std::runtime_error(
"Unable to open file: " + vk_path.string() +
152 "\nGenerate a vk during proving by running `bb prove` with an additional `--write_vk` "
153 "flag, or run `bb write_vk` to generate a standalone vk."
154 "\nIf you already have a vk file, specify its path with `--vk_path <path>`.");
161 if (
buffer.size() %
sizeof(T) != 0) {
162 THROW std::runtime_error(std::string(object_name) +
" size must be a multiple of " +
std::to_string(
sizeof(T)) +
165 return ::many_from_buffer<T>(
buffer);
171inline size_t get_file_size(
const std::filesystem::path& filename)
176inline std::vector<uint8_t>
read_file(
const std::filesystem::path& filename,
size_t bytes = 0)
178 return read_file(filename.string(), bytes);
const std::vector< MemoryValue > data
std::unique_ptr< uint8_t[]> buffer
Entry point for Barretenberg command-line interface.
void read(B &it, field2< base_field, Params > &value)
std::vector< T > many_from_buffer_exact(const std::vector< uint8_t > &buffer, std::string_view object_name)
std::vector< uint8_t > read_vk_file(const std::filesystem::path &vk_path)
Read a verification key file with an actionable error message if not found.
void write(B &buf, field2< base_field, Params > const &value)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
void write_file(const std::string &filename, std::span< const uint8_t > data)
std::string field_elements_to_json(const std::vector< Fr > &fields)
size_t get_file_size(std::string const &filename)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
std::string to_string(bb::avm2::ValueTag tag)