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
17
18class TranslatorTests : public ::testing::Test {
20 using Fr = fr;
21 using Fq = fq;
22
23 protected:
25
26 // Helper function to add no-ops
27 static void add_random_ops(std::shared_ptr<bb::ECCOpQueue>& op_queue, size_t count = 1)
28 {
29 for (size_t i = 0; i < count; i++) {
30 op_queue->random_op_ultra_only();
31 }
32 }
33
34 static void add_mixed_ops(std::shared_ptr<bb::ECCOpQueue>& op_queue, size_t count = 100)
35 {
36 auto P1 = G1::random_element();
37 auto P2 = G1::random_element();
38 auto z = Fr::random_element();
39 for (size_t i = 0; i < count; i++) {
40 op_queue->add_accumulate(P1);
41 op_queue->mul_accumulate(P2, z);
42 }
43 op_queue->eq_and_reset();
44 }
45
46 // Construct a test circuit based on some random operations
47 static CircuitBuilder generate_test_circuit(const Fq& batching_challenge_v,
48 const Fq& evaluation_challenge_x,
49 const size_t circuit_size_parameter = 500)
50 {
51
52 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
53 auto op_queue = std::make_shared<bb::ECCOpQueue>();
54 op_queue->no_op_ultra_only();
56 add_mixed_ops(op_queue, circuit_size_parameter / 2);
57 op_queue->merge();
58 add_mixed_ops(op_queue, circuit_size_parameter / 2);
60 op_queue->merge(MergeSettings::APPEND, ECCOpQueue::OP_QUEUE_SIZE - op_queue->get_current_subtable_size());
61
62 return CircuitBuilder{ batching_challenge_v, evaluation_challenge_x, op_queue };
63 }
64
65 static bool prove_and_verify(const CircuitBuilder& circuit_builder,
66 const Fq& evaluation_challenge_x,
67 const Fq& batching_challenge_v)
68 {
69 // Setup prover transcript
70 auto prover_transcript = std::make_shared<Transcript>();
71 prover_transcript->send_to_verifier("init", Fq::random_element());
72 auto initial_transcript = prover_transcript->export_proof();
73
74 // Setup verifier transcript
75 auto verifier_transcript = std::make_shared<Transcript>();
76 verifier_transcript->load_proof(initial_transcript);
77 verifier_transcript->template receive_from_prover<Fq>("init");
78
79 // Create proving key and prover
80 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
81 TranslatorProver prover{ proving_key, prover_transcript };
82
83 // Generate proof
84 auto proof = prover.construct_proof();
85
86 // Create verifier
87 auto verification_key = std::make_shared<TranslatorFlavor::VerificationKey>(proving_key->proving_key);
88 TranslatorVerifier verifier(verification_key, verifier_transcript);
89
90 // Verify proof and return result
91 return verifier.verify_proof(proof, evaluation_challenge_x, batching_challenge_v);
92 }
93};
94
102TEST_F(TranslatorTests, ProofLengthCheck)
103{
104 using Fq = fq;
105
106 Fq batching_challenge_v = Fq::random_element();
107 Fq evaluation_challenge_x = Fq::random_element();
108
109 // Generate a circuit and its verification key (computed at runtime from the proving key)
110 CircuitBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
111
112 // Setup prover transcript
113 auto prover_transcript = std::make_shared<Transcript>();
114 prover_transcript->send_to_verifier("init", Fq::random_element());
115 prover_transcript->export_proof();
116 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
117 TranslatorProver prover{ proving_key, prover_transcript };
118
119 // Generate proof
120 auto proof = prover.construct_proof();
121
123}
124
130{
131 using Fq = fq;
132
133 Fq batching_challenge_v = Fq::random_element();
134 Fq evaluation_challenge_x = Fq::random_element();
135
136 // Generate a circuit without no-ops
137 CircuitBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
138
139 EXPECT_TRUE(TranslatorCircuitChecker::check(circuit_builder));
140 bool verified = prove_and_verify(circuit_builder, evaluation_challenge_x, batching_challenge_v);
141 EXPECT_TRUE(verified);
142}
143
150{
151 using Fq = fq;
152
153 Fq batching_challenge_v = Fq::random_element();
154 Fq evaluation_challenge_x = Fq::random_element();
155
156 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
157 auto op_queue = std::make_shared<bb::ECCOpQueue>();
158 op_queue->no_op_ultra_only();
159 add_random_ops(op_queue, CircuitBuilder::NUM_RANDOM_OPS_START);
160 add_mixed_ops(op_queue, 100);
161 op_queue->merge();
162 auto circuit_builder = CircuitBuilder{ batching_challenge_v, evaluation_challenge_x, op_queue, /*avm_mode=*/true };
163
164 EXPECT_TRUE(TranslatorCircuitChecker::check(circuit_builder));
165 bool verified = prove_and_verify(circuit_builder, evaluation_challenge_x, batching_challenge_v);
166 EXPECT_TRUE(verified);
167}
168
178{
179 using Fq = fq;
180
181 auto prover_transcript = std::make_shared<Transcript>();
182 prover_transcript->send_to_verifier("init", Fq::random_element());
183 prover_transcript->export_proof();
184 Fq batching_challenge_v = Fq::random_element();
185 Fq evaluation_challenge_x = Fq::random_element();
186
187 // Generate the default fixed VK
189
190 // Lambda for manually computing a verification key for a given circuit and comparing it to the fixed VK
191 auto compare_computed_vk_against_fixed = [&](size_t circuit_size_parameter) {
192 CircuitBuilder circuit_builder =
193 generate_test_circuit(batching_challenge_v, evaluation_challenge_x, circuit_size_parameter);
194 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
195 TranslatorProver prover{ proving_key, prover_transcript };
196 TranslatorFlavor::VerificationKey computed_vk(proving_key->proving_key);
197 auto labels = TranslatorFlavor::VerificationKey::get_labels();
198 size_t index = 0;
199 for (auto [vk_commitment, fixed_commitment] : zip_view(computed_vk.get_all(), fixed_vk.get_all())) {
200 EXPECT_EQ(vk_commitment, fixed_commitment)
201 << "Mismatch between computed vk_commitment and fixed_commitment at label: " << labels[index];
202 ++index;
203 }
204
205 EXPECT_EQ(computed_vk, fixed_vk);
206 };
207
208 // Check consistency of the fixed VK with the computed VK for some different circuit sizes
209 const size_t circuit_size_parameter_1 = 1 << 2;
210 const size_t circuit_size_parameter_2 = 1 << 3;
211
212 compare_computed_vk_against_fixed(circuit_size_parameter_1);
213 compare_computed_vk_against_fixed(circuit_size_parameter_2);
214}
static bool prove_and_verify(const CircuitBuilder &circuit_builder, const Fq &evaluation_challenge_x, const Fq &batching_challenge_v)
static void add_random_ops(std::shared_ptr< bb::ECCOpQueue > &op_queue, size_t count=1)
static CircuitBuilder generate_test_circuit(const Fq &batching_challenge_v, const Fq &evaluation_challenge_x, const size_t circuit_size_parameter=500)
static void SetUpTestSuite()
static void add_mixed_ops(std::shared_ptr< bb::ECCOpQueue > &op_queue, size_t count=100)
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 const size_t OP_QUEUE_SIZE
TranslatorCircuitBuilder creates a circuit that evaluates the correctness of the evaluation of EccOpQ...
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
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:188
field< Bn254FrParams > fr
Definition fr.hpp:174
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept