Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
file_io.hpp
Go to the documentation of this file.
1#pragma once
5#include <cstdint>
6#include <cstring>
7#include <fcntl.h>
8#include <filesystem>
9#include <fstream>
10#include <ios>
11#include <iostream>
12#include <sstream>
13#include <sys/stat.h>
14#include <unistd.h>
15#include <vector>
16
17namespace bb {
18inline size_t get_file_size(std::string const& filename)
19{
20 // Open the file in binary mode and move to the end.
21 std::ifstream file(filename, std::ios::binary | std::ios::ate);
22 if (!file) {
23 return 0;
24 }
25
26 file.seekg(0, std::ios::end);
27 return (size_t)file.tellg();
28}
29
30inline std::vector<uint8_t> read_file(const std::string& filename, size_t bytes = 0)
31{
32 // Standard input. We'll iterate over the stream and reallocate.
33 if (filename == "-") {
35 }
36
37 std::ifstream file(filename, std::ios::binary);
38 if (!file) {
39 THROW std::runtime_error("Unable to open file: " + filename);
40 }
41
42 // Unseekable, pipe or process substitution. We'll iterate over the stream and reallocate.
43 if (!file.seekg(0, std::ios::end)) {
44 file.clear();
46 }
47
48 // Get the file size.
49 auto size = static_cast<size_t>(file.tellg());
50 file.seekg(0, std::ios::beg);
51
52 // Create a vector preallocated with enough space for the file data and read it.
53 auto to_read = bytes == 0 ? size : bytes;
54 std::vector<uint8_t> fileData(to_read);
55 file.read(reinterpret_cast<char*>(fileData.data()), (std::streamsize)to_read);
56 return fileData;
57}
58
59inline void write_file(const std::string& filename, std::vector<uint8_t> const& data)
60{
61 struct stat st;
62 if (stat(filename.c_str(), &st) == 0 && S_ISFIFO(st.st_mode)) {
63 // Writing to a pipe or file descriptor
64 int fd = open(filename.c_str(), O_WRONLY);
65 if (fd == -1) {
66 THROW std::runtime_error("Failed to open file descriptor: " + filename);
67 }
68
69 size_t total_written = 0;
70 size_t data_size = data.size();
71 while (total_written < data_size) {
72 ssize_t written = ::write(fd, data.data() + total_written, data_size - total_written);
73 if (written == -1) {
74 close(fd);
75 THROW std::runtime_error("Failed to write to file descriptor: " + filename);
76 }
77 total_written += static_cast<size_t>(written);
78 }
79 close(fd);
80 } else {
81 std::ofstream file(filename, std::ios::binary);
82 if (!file) {
83 THROW std::runtime_error("Failed to open data file for writing: " + filename + " (" + strerror(errno) +
84 ")");
85 }
86 file.write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
87 file.close();
88 }
89}
90
91template <typename Fr> inline std::string field_elements_to_json(const std::vector<Fr>& fields)
92{
93 std::stringstream ss;
94 ss << "[";
95 for (size_t i = 0; i < fields.size(); ++i) {
96 ss << '"' << fields[i] << '"';
97 if (i != fields.size() - 1) {
98 ss << ",";
99 }
100 }
101 ss << "]";
102 return ss.str();
103}
104
112inline std::vector<uint8_t> read_vk_file(const std::filesystem::path& vk_path)
113{
114 try {
115 return read_file(vk_path);
116 } catch (const std::runtime_error&) {
117 THROW std::runtime_error("Unable to open file: " + vk_path.string() +
118 "\nGenerate a vk during proving by running `bb prove` with an additional `--write_vk` "
119 "flag, or run `bb write_vk` to generate a standalone vk."
120 "\nIf you already have a vk file, specify its path with `--vk_path <path>`.");
121 }
122}
123} // namespace bb
const std::vector< MemoryValue > data
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
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.
Definition file_io.hpp:112
void write(B &buf, field2< base_field, Params > const &value)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:30
void write_file(const std::string &filename, std::vector< uint8_t > const &data)
Definition file_io.hpp:59
std::string field_elements_to_json(const std::vector< Fr > &fields)
Definition file_io.hpp:91
size_t get_file_size(std::string const &filename)
Definition file_io.hpp:18
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
#define THROW