diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp index cfc8fc59c5b..4f9bc58192c 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp @@ -8,12 +8,29 @@ #include #include +// Gets the transcript URL from the BARRETENBERG_TRANSCRIPT_URL environment variable, if set. +// Otherwise returns the default URL. +inline std::string getTranscriptURL() +{ + const char* ENV_VAR_NAME = "BARRETENBERG_TRANSCRIPT_URL"; + const std::string DEFAULT_URL = "https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat"; + + const char* env_url = std::getenv(ENV_VAR_NAME); + + auto environment_variable_exists = (env_url && *env_url); + + return environment_variable_exists ? std::string(env_url) : DEFAULT_URL; +} + inline std::vector download_g1_data(size_t num_points) { size_t g1_start = 28; size_t g1_end = g1_start + num_points * 64 - 1; - std::string command = "curl -s -H \"Range: bytes=" + std::to_string(g1_start) + "-" + std::to_string(g1_end) + - "\" 'https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat'"; + + std::string url = getTranscriptURL(); + + std::string command = + "curl -s -H \"Range: bytes=" + std::to_string(g1_start) + "-" + std::to_string(g1_end) + "\" '" + url + "'"; return exec_pipe(command); } @@ -22,8 +39,11 @@ inline std::vector download_g2_data() { size_t g2_start = 28 + 5040001 * 64; size_t g2_end = g2_start + 128 - 1; - std::string command = "curl -s -H \"Range: bytes=" + std::to_string(g2_start) + "-" + std::to_string(g2_end) + - "\" 'https://aztec-ignition.s3.amazonaws.com/MAIN%20IGNITION/monomial/transcript00.dat'"; + + std::string url = getTranscriptURL(); + + std::string command = + "curl -s -H \"Range: bytes=" + std::to_string(g2_start) + "-" + std::to_string(g2_end) + "\" '" + url + "'"; return exec_pipe(command); } @@ -31,38 +51,44 @@ inline std::vector download_g2_data() inline std::vector get_g1_data(const std::filesystem::path& path, size_t num_points) { std::filesystem::create_directories(path); + try { + std::ifstream size_file(path / "size"); + size_t size = 0; + if (size_file) { + size_file >> size; + size_file.close(); + } + if (size >= num_points) { + vinfo("using cached crs at: ", path); + auto data = read_file(path / "g1.dat"); + auto points = std::vector(num_points); + barretenberg::srs::IO::read_affine_elements_from_buffer( + points.data(), (char*)data.data(), num_points * 64); + return points; + } + + std::ofstream new_size_file(path / "size"); + if (!new_size_file) { + throw std::runtime_error("Failed to open size file for writing"); + } + new_size_file << num_points; + new_size_file.close(); + + vinfo("downloading crs..."); + auto data = download_g1_data(num_points); + + write_file(path / "g1.dat", data); - std::ifstream size_file(path / "size"); - size_t size = 0; - if (size_file) { - size_file >> size; - size_file.close(); - } - if (size >= num_points) { - vinfo("using cached crs at: ", path); - auto data = read_file(path / "g1.dat"); auto points = std::vector(num_points); barretenberg::srs::IO::read_affine_elements_from_buffer( - points.data(), (char*)data.data(), num_points * 64); + points.data(), (char*)data.data(), data.size()); return points; + } catch (std::exception& e) { + std::filesystem::remove(path / "size"); + std::filesystem::remove(path / "g1.dat"); + // We cannot do anything here except tell the user there is an error and stop the cli + throw std::runtime_error("Failed to download srs: " + std::string(e.what())); } - - std::ofstream new_size_file(path / "size"); - if (!new_size_file) { - throw std::runtime_error("Failed to open size file for writing"); - } - new_size_file << num_points; - new_size_file.close(); - - vinfo("downloading crs..."); - auto data = download_g1_data(num_points); - - write_file(path / "g1.dat", data); - - auto points = std::vector(num_points); - barretenberg::srs::IO::read_affine_elements_from_buffer( - points.data(), (char*)data.data(), data.size()); - return points; } inline barretenberg::g2::affine_element get_g2_data(const std::filesystem::path& path)