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
16static auto& engine = numeric::get_debug_randomness();
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>(initial_transcript);
76 verifier_transcript->template receive_from_prover<Fq>("init");
77
78 // Create proving key and prover
79 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
80 TranslatorProver prover{ proving_key, prover_transcript };
81
82 // Generate proof
83 auto proof = prover.construct_proof();
84
85 // Create verifier
86 auto verification_key = std::make_shared<TranslatorFlavor::VerificationKey>(proving_key->proving_key);
87 TranslatorVerifier verifier(verification_key, verifier_transcript);
88
89 // Get accumulated_result from the prover
90 uint256_t accumulated_result = prover.get_accumulated_result();
91
92 // Commit to op queue wires
94 op_queue_commitments[0] =
95 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.op);
96 op_queue_commitments[1] =
97 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.x_lo_y_hi);
98 op_queue_commitments[2] =
99 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.x_hi_z_1);
100 op_queue_commitments[3] =
101 proving_key->proving_key->commitment_key.commit(proving_key->proving_key->polynomials.y_lo_z_2);
102
103 // Verify proof and return result
104 return verifier.verify_proof(
105 proof, evaluation_challenge_x, batching_challenge_v, accumulated_result, op_queue_commitments);
106 }
107};
108
116TEST_F(TranslatorTests, ProofLengthCheck)
117{
118 using Fq = fq;
119
120 Fq batching_challenge_v = Fq::random_element();
121 Fq evaluation_challenge_x = Fq::random_element();
122
123 // Generate a circuit and its verification key (computed at runtime from the proving key)
124 CircuitBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
125
126 // Setup prover transcript
127 auto prover_transcript = std::make_shared<Transcript>();
128 prover_transcript->send_to_verifier("init", Fq::random_element());
129 prover_transcript->export_proof();
130 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
131 TranslatorProver prover{ proving_key, prover_transcript };
132
133 // Generate proof
134 auto proof = prover.construct_proof();
135
137}
138
144{
145 using Fq = fq;
146
147 Fq batching_challenge_v = Fq::random_element();
148 Fq evaluation_challenge_x = Fq::random_element();
149
150 // Generate a circuit without no-ops
151 CircuitBuilder circuit_builder = generate_test_circuit(batching_challenge_v, evaluation_challenge_x);
152
153 EXPECT_TRUE(TranslatorCircuitChecker::check(circuit_builder));
154 bool verified = prove_and_verify(circuit_builder, evaluation_challenge_x, batching_challenge_v);
155 EXPECT_TRUE(verified);
156}
157
164{
165 using Fq = fq;
166
167 Fq batching_challenge_v = Fq::random_element();
168 Fq evaluation_challenge_x = Fq::random_element();
169
170 // Add the same operations to the ECC op queue; the native computation is performed under the hood.
171 auto op_queue = std::make_shared<bb::ECCOpQueue>();
172 op_queue->no_op_ultra_only();
173 add_random_ops(op_queue, CircuitBuilder::NUM_RANDOM_OPS_START);
174 add_mixed_ops(op_queue, 100);
175 op_queue->merge();
176 auto circuit_builder = CircuitBuilder{ batching_challenge_v, evaluation_challenge_x, op_queue, /*avm_mode=*/true };
177
178 EXPECT_TRUE(TranslatorCircuitChecker::check(circuit_builder));
179 bool verified = prove_and_verify(circuit_builder, evaluation_challenge_x, batching_challenge_v);
180 EXPECT_TRUE(verified);
181}
182
192{
193 using Fq = fq;
194
195 auto prover_transcript = std::make_shared<Transcript>();
196 prover_transcript->send_to_verifier("init", Fq::random_element());
197 prover_transcript->export_proof();
198 Fq batching_challenge_v = Fq::random_element();
199 Fq evaluation_challenge_x = Fq::random_element();
200
201 // Generate the default fixed VK
203
204 // Lambda for manually computing a verification key for a given circuit and comparing it to the fixed VK
205 auto compare_computed_vk_against_fixed = [&](size_t circuit_size_parameter) {
206 CircuitBuilder circuit_builder =
207 generate_test_circuit(batching_challenge_v, evaluation_challenge_x, circuit_size_parameter);
208 auto proving_key = std::make_shared<TranslatorProvingKey>(circuit_builder);
209 TranslatorProver prover{ proving_key, prover_transcript };
210 TranslatorFlavor::VerificationKey computed_vk(proving_key->proving_key);
211 auto labels = TranslatorFlavor::VerificationKey::get_labels();
212 size_t index = 0;
213 for (auto [vk_commitment, fixed_commitment] : zip_view(computed_vk.get_all(), fixed_vk.get_all())) {
214 EXPECT_EQ(vk_commitment, fixed_commitment)
215 << "Mismatch between computed vk_commitment and fixed_commitment at label: " << labels[index];
216 ++index;
217 }
218
219 EXPECT_EQ(computed_vk, fixed_vk);
220 };
221
222 // Check consistency of the fixed VK with the computed VK for some different circuit sizes
223 const size_t circuit_size_parameter_1 = 1 << 2;
224 const size_t circuit_size_parameter_2 = 1 << 3;
225
226 compare_computed_vk_against_fixed(circuit_size_parameter_1);
227 compare_computed_vk_against_fixed(circuit_size_parameter_2);
228}
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, const uint256_t &accumulated_result, const std::array< Commitment, TranslatorFlavor::NUM_OP_QUEUE_WIRES > &op_queue_wire_commitments)
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.
Definition api.hpp:5
field< Bn254FqParams > fq
Definition fq.hpp:169
TEST_F(IPATest, ChallengesAreZero)
Definition ipa.test.cpp:185
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