Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
crs_factory.test.cpp
Go to the documentation of this file.
12#include <fstream>
13#include <gtest/gtest.h>
14#include <utility>
15
16using namespace bb;
17using namespace bb::srs::factories;
18using namespace bb::curve;
19namespace fs = std::filesystem;
20
21namespace {
22// BN254 consistency checker
23void check_bn254_consistency(const fs::path& crs_download_path, size_t num_points, bool allow_download)
24{
25 NativeBn254CrsFactory file_crs(crs_download_path, allow_download);
26
27 // read G1
28 std::vector<g1::affine_element> g1_points(num_points);
29 auto g1_buf = read_file(bb::srs::bb_crs_path() / "bn254_g1.dat", num_points * sizeof(g1::affine_element));
30 for (size_t i = 0; i < num_points; ++i) {
31 g1_points[i] = from_buffer<g1::affine_element>(g1_buf, i * sizeof(g1::affine_element));
32 }
33
34 // read G2
35 auto g2_buf = read_file(bb::srs::bb_crs_path() / "bn254_g2.dat", sizeof(g2::affine_element));
36 auto g2_point = from_buffer<g2::affine_element>(g2_buf);
37
38 // build in-memory CRS
39 MemBn254CrsFactory mem_crs(g1_points, g2_point);
40
41 // prover CRS
42 auto f_prover = file_crs.get_crs(num_points);
43 auto m_prover = mem_crs.get_crs(num_points);
44 EXPECT_EQ(m_prover->get_monomial_size(), f_prover->get_monomial_size());
45 for (size_t i = 0; i < num_points; ++i) {
46 EXPECT_EQ(std::make_pair(i, m_prover->get_monomial_points()[i]),
47 std::make_pair(i, f_prover->get_monomial_points()[i]));
48 }
49 // verifier CRS
50 auto f_ver = file_crs.get_verifier_crs();
51 auto m_ver = mem_crs.get_verifier_crs();
52 EXPECT_EQ(m_ver->get_g2x(), f_ver->get_g2x());
53 EXPECT_EQ(0,
54 memcmp(m_ver->get_precomputed_g2_lines(),
55 f_ver->get_precomputed_g2_lines(),
56 sizeof(pairing::miller_lines) * 2));
57}
58
59// Grumpkin consistency checker
60void check_grumpkin_consistency(const fs::path& crs_download_path, size_t num_points, bool allow_download)
61{
62 NativeGrumpkinCrsFactory file_crs(crs_download_path, allow_download);
63
64 // read G1
65 std::vector<Grumpkin::AffineElement> points(num_points);
66 auto data =
67 read_file(bb::srs::bb_crs_path() / "grumpkin_g1.flat.dat", num_points * sizeof(Grumpkin::AffineElement));
68
69 for (size_t i = 0; i < num_points; ++i) {
70 points[i] = from_buffer<Grumpkin::AffineElement>(data, i * sizeof(g1::affine_element));
71 }
72 MemGrumpkinCrsFactory mem_crs(points);
73
74 // prover CRS
75 auto f_prover = file_crs.get_crs(num_points);
76 auto m_prover = mem_crs.get_crs(num_points);
77 EXPECT_EQ(m_prover->get_monomial_size(), f_prover->get_monomial_size());
78 for (size_t i = 0; i < num_points; ++i) {
79 EXPECT_EQ(std::make_pair(i, m_prover->get_monomial_points()[i]),
80 std::make_pair(i, f_prover->get_monomial_points()[i]));
81 }
82}
83} // namespace
84
86{
87 check_bn254_consistency(bb::srs::bb_crs_path(), 1024, /*allow_download=*/false);
88 const std::filesystem::path& temp_crs_path = "barretenberg_srs_test_crs_bn254";
89 fs::remove_all(temp_crs_path);
90 fs::create_directories(temp_crs_path);
91 // Tiny download check to test the 'net CRS' path
92 ASSERT_ANY_THROW(check_bn254_consistency(temp_crs_path, 1, /*allow_download=*/false));
93 check_bn254_consistency(temp_crs_path, 1, /*allow_download=*/true);
94}
95
96TEST(CrsFactory, grumpkin)
97{
98 check_grumpkin_consistency(bb::srs::bb_crs_path(), 1024, /*allow_download=*/false);
99 const std::filesystem::path& temp_crs_path = "barretenberg_srs_test_crs_grumpkin";
100 fs::remove_all(temp_crs_path);
101 fs::create_directories(temp_crs_path);
102 // Tiny download check to test the 'net CRS' path
103 ASSERT_ANY_THROW(check_grumpkin_consistency(temp_crs_path, 1, /*allow_download=*/false));
104 check_grumpkin_consistency(temp_crs_path, 1, /*allow_download=*/true);
105}
106
107TEST(CrsFactory, Bn254Fallback)
108{
109 // Test that fallback works when primary URL fails
110 const std::filesystem::path& temp_crs_path = "barretenberg_srs_test_crs_bn254_fallback";
111 fs::remove_all(temp_crs_path);
112 fs::create_directories(temp_crs_path);
113
114 // Use a bad primary URL that will fail, forcing fallback to the real S3 URL
115 std::string bad_primary = "http://nonexistent.invalid/g1.dat";
116 std::string good_fallback = "http://crs.aztec-labs.com/g1.dat";
117
118 // This should succeed by falling back to the working URL
119 auto points = bb::get_bn254_g1_data(temp_crs_path, 1, /*allow_download=*/true, bad_primary, good_fallback);
120 EXPECT_EQ(points.size(), 1);
121 // Verify the downloaded point matches the expected first element
122 EXPECT_EQ(points[0], bb::srs::BN254_G1_FIRST_ELEMENT);
123
124 fs::remove_all(temp_crs_path);
125}
126
127TEST(CrsFactory, Bn254HashVerification)
128{
129 // Verify whatever CRS data we have on disk against the embedded chunk hashes.
130 auto g1_path = bb::srs::bb_crs_path() / "bn254_g1.dat";
131 size_t file_size = get_file_size(g1_path);
132
133 // Round down to a whole number of 8MB chunks (the verify function requires alignment).
134 size_t verifiable_size = (file_size / bb::srs::CRS_HASH_CHUNK_SIZE) * bb::srs::CRS_HASH_CHUNK_SIZE;
135 ASSERT_GT(verifiable_size, 0) << "Need at least one 8MB chunk of CRS data on disk";
136
137 auto data = read_file(g1_path, verifiable_size);
139
140 // Corrupt a byte and verify that hash check catches it
141 data[100] ^= 0xFF;
142 EXPECT_ANY_THROW(bb::srs::verify_bn254_crs_integrity(data));
143}
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
const std::vector< MemoryValue > data
const size_t num_points
void verify_bn254_crs_integrity(const std::vector< uint8_t > &data)
Verify downloaded CRS data against embedded SHA256 chunk hashes.
std::filesystem::path bb_crs_path()
constexpr g1::affine_element BN254_G1_FIRST_ELEMENT
Expected first G1 element from BN254 CRS.
constexpr size_t CRS_HASH_CHUNK_SIZE
SHA256 hashes for integrity verification of downloaded BN254 CRS G1 data.
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
std::vector< g1::affine_element > get_bn254_g1_data(const std::filesystem::path &path, size_t num_points, bool allow_download, const std::string &primary_url, const std::string &fallback_url)
std::vector< uint8_t > read_file(const std::string &filename, size_t bytes=0)
Definition file_io.hpp:30
TEST(BoomerangMegaCircuitBuilder, BasicCircuit)
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