Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mega_honk.test.cpp
Go to the documentation of this file.
1#include <cstddef>
2#include <cstdint>
3#include <gtest/gtest.h>
4
13
14using namespace bb;
15
17
18using FlavorTypes = ::testing::Types<MegaFlavor, MegaZKFlavor>;
19
20template <typename Flavor> class MegaHonkTests : public ::testing::Test {
21 public:
23
33
39 {
40 auto prover_instance = std::make_shared<ProverInstance>(builder);
41 auto verification_key = std::make_shared<VerificationKey>(prover_instance->get_precomputed());
42 auto vk_and_hash = std::make_shared<typename Flavor::VKAndHash>(verification_key);
43 Prover prover(prover_instance, verification_key);
44 Verifier verifier(vk_and_hash);
45 auto proof = prover.construct_proof();
46 bool verified = verifier.verify_proof(proof).result;
47
48 return verified;
49 }
50};
51
53
63TYPED_TEST(MegaHonkTests, ProofLengthCheck)
64{
65 using Flavor = TypeParam;
68
69 auto builder = Builder{};
70 DefaultIO::add_default(builder);
71
72 // Construct a mega proof and ensure its size matches expectation; if not, the constant may need to be updated
74 auto verification_key = std::make_shared<typename Flavor::VerificationKey>(prover_instance->get_precomputed());
75 UltraProver_<Flavor> prover(prover_instance, verification_key);
76 HonkProof mega_proof = prover.construct_proof();
78}
79
86{
87 using Flavor = TypeParam;
89
91
92 // Construct and verify Honk proof
93 bool honk_verified = this->construct_and_verify_honk_proof(builder);
94 EXPECT_TRUE(honk_verified);
95}
96
102TYPED_TEST(MegaHonkTests, DynamicVirtualSizeIncrease)
103{
104 using Flavor = TypeParam;
105
106 // In MegaZKFlavor, we mask witness polynomials by placing random values at the indices `dyadic_circuit_size`-i for
107 // i=1,2,3. This mechanism does not work with structured polynomials yet.
109 GTEST_SKIP() << "Skipping 'DynamicVirtualSizeIncrease' test for MegaZKFlavor.";
110 }
112 using Prover = UltraProver_<Flavor>;
113 using Verifier = UltraVerifier_<Flavor, DefaultIO>;
114
116
117 auto builder_copy = builder;
118
119 // Construct and verify Honk proof using a structured trace
120 auto prover_instance = std::make_shared<ProverInstance_<Flavor>>(builder);
121 auto prover_instance_copy = std::make_shared<ProverInstance_<Flavor>>(builder_copy);
122 auto circuit_size = prover_instance->dyadic_size();
123
124 auto doubled_circuit_size = 2 * circuit_size;
125 prover_instance_copy->polynomials.increase_polynomials_virtual_size(doubled_circuit_size);
126 // TODO(https://github.com/AztecProtocol/barretenberg/issues/1158)
127 // prover_instance_copy->dyadic_circuit_size = doubled_circuit_size;
128
129 auto verification_key = std::make_shared<typename Flavor::VerificationKey>(prover_instance->get_precomputed());
130 Prover prover(prover_instance, verification_key);
131
132 auto verification_key_copy = std::make_shared<typename Flavor::VerificationKey>(prover_instance->get_precomputed());
133 Prover prover_copy(prover_instance_copy, verification_key_copy);
134
135 for (auto [entry, entry_copy] : zip_view(verification_key->get_all(), verification_key_copy->get_all())) {
136 EXPECT_EQ(entry, entry_copy);
137 }
138
139 auto vk_and_hash = std::make_shared<typename Flavor::VKAndHash>(verification_key);
140 Verifier verifier(vk_and_hash);
141 auto proof = prover.construct_proof();
142
143 auto relation_failures =
144 RelationChecker<Flavor>::check_all(prover_instance->polynomials, prover_instance->relation_parameters);
145 EXPECT_TRUE(relation_failures.empty());
146 bool result = verifier.verify_proof(proof).result;
147 EXPECT_TRUE(result);
148
149 auto vk_and_hash_copy = std::make_shared<typename Flavor::VKAndHash>(verification_key_copy);
150 Verifier verifier_copy(vk_and_hash_copy);
151 auto proof_copy = prover_copy.construct_proof();
152
153 auto relation_failures_copy =
154 RelationChecker<Flavor>::check_all(prover_instance->polynomials, prover_instance->relation_parameters);
155 EXPECT_TRUE(relation_failures.empty());
156 bool result_copy = verifier_copy.verify_proof(proof_copy).result;
157 EXPECT_TRUE(result_copy);
158}
159
168{
169 using Flavor = TypeParam;
170 // In MegaZKFlavor, we mask witness polynomials by placing random values at the indices `dyadic_circuit_size`-i, for
171 // i=1,2,3. This mechanism does not work with structured polynomials yet.
173 GTEST_SKIP() << "Skipping 'PolySwap' test for MegaZKFlavor.";
174 }
176
177 // Construct a simple circuit and make a copy of it
180 auto builder_copy = builder;
181
182 // Construct two identical proving keys
184 auto prover_instance_2 = std::make_shared<typename TestFixture::ProverInstance>(builder_copy);
185
186 // Tamper with the polys of pkey 1 in such a way that verification should fail
187 for (size_t i = 0; i < prover_instance_1->dyadic_size(); ++i) {
188 if (prover_instance_1->polynomials.q_arith[i] != 0) {
189 prover_instance_1->polynomials.w_l.at(i) += 1;
190 break;
191 }
192 }
193
194 // Swap the polys of the two proving keys; result should be pkey 1 is valid and pkey 2 should fail
195 std::swap(prover_instance_1->polynomials, prover_instance_2->polynomials);
196
197 { // Verification based on pkey 1 should succeed
198 auto verification_key =
199 std::make_shared<typename TestFixture::VerificationKey>(prover_instance_1->get_precomputed());
200 auto vk_and_hash = std::make_shared<typename Flavor::VKAndHash>(verification_key);
201 typename TestFixture::Prover prover(prover_instance_1, verification_key);
202 typename TestFixture::Verifier verifier(vk_and_hash);
203 auto proof = prover.construct_proof();
204 bool result = verifier.verify_proof(proof).result;
205 EXPECT_TRUE(result);
206 }
207
208 { // Verification based on pkey 2 should fail
209 auto verification_key =
210 std::make_shared<typename TestFixture::VerificationKey>(prover_instance_2->get_precomputed());
211 auto vk_and_hash = std::make_shared<typename Flavor::VKAndHash>(verification_key);
212 typename TestFixture::Prover prover(prover_instance_2, verification_key);
213 typename TestFixture::Verifier verifier(vk_and_hash);
214 auto proof = prover.construct_proof();
215 bool result = verifier.verify_proof(proof).result;
216 EXPECT_FALSE(result);
217 }
218}
Curve::AffineElement Point
typename Flavor::VerificationKey VerificationKey
static void SetUpTestSuite()
bool construct_and_verify_honk_proof(auto &builder)
Construct and a verify a Honk proof.
CommitmentKey object over a pairing group 𝔾₁.
Manages the data that is propagated on the public inputs of an application/function circuit.
static constexpr size_t PUBLIC_INPUTS_SIZE
The verification key is responsible for storing the commitments to the precomputed (non-witnessk) pol...
ECCVMCircuitBuilder CircuitBuilder
static constexpr size_t PROOF_LENGTH_WITHOUT_PUB_INPUTS
static void construct_simple_circuit(MegaBuilder &builder)
Generate a simple test circuit with some ECC op gates and conventional arithmetic gates.
A ProverInstance is normally constructed from a finalized circuit and it contains all the information...
static AllSubrelationFailures check_all(const auto &polynomials, const auto &params)
Check that the provided polynomials satisfy all relations for a given Flavor.
Output verify_proof(const Proof &proof)
Perform ultra verification.
The VerifierInstance encapsulates all the necessary information for a Honk Verifier to verify a proof...
typename Group::affine_element AffineElement
Definition bn254.hpp:22
bb::fr ScalarField
Definition bn254.hpp:18
Manages the data that is propagated on the public inputs of an application/function circuit.
AluTraceBuilder builder
Definition alu.test.cpp:124
auto & engine
testing::Types< MegaFlavor, UltraFlavor, UltraZKFlavor, UltraRollupFlavor > FlavorTypes
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
std::vector< fr > HonkProof
Definition proof.hpp:15
TYPED_TEST_SUITE(ShpleminiTest, TestSettings)
TYPED_TEST(ShpleminiTest, CorrectnessOfMultivariateClaimBatching)
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13