Skip to content

Commit

Permalink
feat(bb): Use an environment variable to set the transcript URL (#1750)
Browse files Browse the repository at this point in the history
Related to #1749 

# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [ ] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [ ] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [ ] Every change is related to the PR description.
- [ ] I have
[linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
this pull request to relevant issues (if any exist).
  • Loading branch information
kevaundray authored Aug 23, 2023
1 parent 0b2d17c commit 31488c1
Showing 1 changed file with 57 additions and 31 deletions.
88 changes: 57 additions & 31 deletions circuits/cpp/barretenberg/cpp/src/barretenberg/bb/get_crs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,29 @@
#include <fstream>
#include <ios>

// 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<uint8_t> 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);
}
Expand All @@ -22,47 +39,56 @@ inline std::vector<uint8_t> 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);
}

inline std::vector<barretenberg::g1::affine_element> 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<barretenberg::g1::affine_element>(num_points);
barretenberg::srs::IO<curve::BN254>::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<barretenberg::g1::affine_element>(num_points);
barretenberg::srs::IO<curve::BN254>::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<barretenberg::g1::affine_element>(num_points);
barretenberg::srs::IO<curve::BN254>::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)
Expand Down

0 comments on commit 31488c1

Please sign in to comment.