Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
translator.test.cpp
Go to the documentation of this file.
9
10#include <gtest/gtest.h>
11using namespace bb;
12
13namespace {
14using CircuitBuilder = TranslatorFlavor::CircuitBuilder;
16using OpQueue = ECCOpQueue;
18
19class TranslatorTests : public ::testing::Test {
20 using G1 = g1::affine_element;
21 using Fr = fr;
22 using Fq = fq;
23
24 protected:
25 static void SetUpTestSuite() { bb::srs::init_file_crs_factory(bb::srs::bb_crs_path()); }
26
27 // Construct a test circuit based on some random operations
28 static CircuitBuilder generate_test_circuit(const Fq& batching_challenge_v,
29 const Fq& evaluation_challenge_x,
30 const size_t circuit_size_parameter = 500)
31 {
32 auto P1 = G1::random_element();
33 auto P2 = G1::random_element();
34 auto z = Fr::random_element();
35
36 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
37 auto op_queue = std::make_shared<bb::ECCOpQueue>();
38 op_queue->no_op_ultra_only();
39
40 for (size_t i = 0; i < circuit_size_parameter; i++) {
41 op_queue->add_accumulate(P1);
42 op_queue->mul_accumulate(P2, z);
43 }
44 op_queue->merge();
45
46 return CircuitBuilder{ batching_challenge_v, evaluation_challenge_x, op_queue };
47 }
48};
49} // namespace
50
58TEST_F(TranslatorTests, ProofLengthCheck)
59{
60 using Fq = fq;
61
62 auto prover_transcript = std::make_shared<Transcript>();
63 Fq batching_challenge_v = Fq::random_element();
64 Fq evaluation_challenge_x = Fq::random_element();
65
66 // Generate a circuit and its verification key (computed at runtime from the proving key)
67 CircuitBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
68
69 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
70 TranslatorProver prover{ proving_key, prover_transcript };
71 auto proof = prover.construct_proof();
73}
74
79TEST_F(TranslatorTests, Basic)
80{
81 using Fq = fq;
82
83 auto prover_transcript = std::make_shared<Transcript>();
84 prover_transcript->send_to_verifier("init", Fq::random_element());
85 auto initial_transcript = prover_transcript->export_proof();
86 Fq batching_challenge_v = Fq::random_element();
87 Fq evaluation_challenge_x = Fq::random_element();
88
89 // Generate a circuit and its verification key (computed at runtime from the proving key)
90 CircuitBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
91
92 EXPECT_TRUE(TranslatorCircuitChecker::check(circuit_builder));
93 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
94 TranslatorProver prover{ proving_key, prover_transcript };
95 auto proof = prover.construct_proof();
96
97 auto verifier_transcript = std::make_shared<Transcript>();
98 verifier_transcript->load_proof(initial_transcript);
99 verifier_transcript->template receive_from_prover<Fq>("init");
100 auto verification_key = std::make_shared<TranslatorFlavor::VerificationKey>(proving_key->proving_key);
101 TranslatorVerifier verifier(verification_key, verifier_transcript);
102 bool verified = verifier.verify_proof(proof, evaluation_challenge_x, batching_challenge_v);
103 EXPECT_TRUE(verified);
104}
105
114TEST_F(TranslatorTests, FixedVK)
115{
116 using Fq = fq;
117
118 auto prover_transcript = std::make_shared<Transcript>();
119 prover_transcript->send_to_verifier("init", Fq::random_element());
120 prover_transcript->export_proof();
121 Fq batching_challenge_v = Fq::random_element();
122 Fq evaluation_challenge_x = Fq::random_element();
123
124 // Generate the default fixed VK
126
127 // Lambda for manually computing a verification key for a given circuit and comparing it to the fixed VK
128 auto compare_computed_vk_against_fixed = [&](size_t circuit_size_parameter) {
129 CircuitBuilder circuit_builder =
130 generate_test_circuit(batching_challenge_v, evaluation_challenge_x, circuit_size_parameter);
131 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
132 TranslatorProver prover{ proving_key, prover_transcript };
133 TranslatorFlavor::VerificationKey computed_vk(proving_key->proving_key);
134 auto labels = TranslatorFlavor::VerificationKey::get_labels();
135 size_t index = 0;
136 for (auto [vk_commitment, fixed_commitment] : zip_view(computed_vk.get_all(), fixed_vk.get_all())) {
137 EXPECT_EQ(vk_commitment, fixed_commitment)
138 << "Mismatch between computed vk_commitment and fixed_commitment at label: " << labels[index];
139 ++index;
140 }
141
142 EXPECT_EQ(computed_vk, fixed_vk);
143 };
144
145 // Check consistency of the fixed VK with the computed VK for some different circuit sizes
146 const size_t circuit_size_parameter_1 = 1 << 2;
147 const size_t circuit_size_parameter_2 = 1 << 3;
148
149 compare_computed_vk_against_fixed(circuit_size_parameter_1);
150 compare_computed_vk_against_fixed(circuit_size_parameter_2);
151}
Common transcript class for both parties. Stores the data for the current round, as well as the manif...
Used to construct execution trace representations of elliptic curve operations.
static bool check(const Builder &circuit)
Check the witness satisifies the circuit.
The verification key is responsible for storing the commitments to the precomputed (non-witnessk) pol...
TranslatorCircuitBuilder CircuitBuilder
static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS
NativeTranscript Transcript
bool verify_proof(const HonkProof &proof, const uint256_t &evaluation_input_x, const BF &batching_challenge_v)
This function verifies a TranslatorFlavor Honk proof for given program settings.
static affine_element random_element(numeric::RNG *engine=nullptr) noexcept
Samples a random point on the curve.
group_elements::affine_element< Fq, Fr, Params > affine_element
Definition group.hpp:42
numeric::RNG & engine
RNG & get_debug_randomness(bool reset, std::uint_fast64_t seed)
Definition engine.cpp:190
std::filesystem::path bb_crs_path()
void init_file_crs_factory(const std::filesystem::path &path)
Entry point for Barretenberg command-line interface.
field< Bn254FqParams > fq
Definition fq.hpp:169
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:123
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
Curve::AffineElement G1
static field random_element(numeric::RNG *engine=nullptr) noexcept