Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
mock_circuits.hpp
Go to the documentation of this file.
1#pragma once
2
18
19namespace bb {
20
28template <typename Builder> void generate_sha256_test_circuit(Builder& builder, size_t num_iterations)
29{
32
33 // SHA-256 initial hash values (FIPS 180-4 section 5.3.3)
34 constexpr std::array<uint32_t, 8> H_INIT = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
35 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
36
37 // Initialize h_init as witnesses
39 for (size_t i = 0; i < 8; i++) {
40 h_init[i] = witness_ct(&builder, H_INIT[i]);
41 }
42
43 // Create a block of zeros as witnesses
45 for (size_t i = 0; i < 16; i++) {
46 block[i] = witness_ct(&builder, 0);
47 }
48
49 // Iterate: feed output of compression back as h_init for next round
50 for (size_t i = 0; i < num_iterations; i++) {
51 h_init = stdlib::SHA256<Builder>::sha256_block(h_init, block);
52 }
53}
54
58template <typename Builder> void generate_poseidon2_hash_test_circuit(Builder& builder, size_t num_inputs)
59{
62
64 inputs.reserve(num_inputs);
65 for (size_t i = 0; i < num_inputs; i++) {
66 inputs.emplace_back(witness_ct(&builder, bb::fr(i + 1)));
67 }
68
70 out.set_public();
71}
72
74 public:
90
91 static constexpr size_t NUM_WIRES = Flavor::NUM_WIRES;
92
102 static void construct_mock_app_circuit(MegaBuilder& builder, bool large = false)
103 {
104 BB_BENCH();
105
106 if (large) { // Results in circuit size 2^19
107 generate_sha256_test_circuit<MegaBuilder>(builder, 9);
109 } else { // Results in circuit size 2^17
110 generate_sha256_test_circuit<MegaBuilder>(builder, 8);
112 }
113 }
114
121 {
122 BB_BENCH();
123
124 // Add some arbitrary ecc op gates
125 for (size_t i = 0; i < 3; ++i) {
126 auto point = Point::random_element(&engine);
127 auto scalar = FF::random_element(&engine);
128 builder.queue_ecc_add_accum(point);
129 builder.queue_ecc_mul_accum(point, scalar);
130 }
131 // queues the result of the preceding ECC
132 builder.queue_ecc_eq(); // should be eq and reset
133 }
134
138 static void randomise_op_queue(MegaBuilder& builder, size_t num_ops)
139 {
140
141 for (size_t i = 0; i < num_ops; ++i) {
142 builder.queue_ecc_random_op();
143 }
144 }
145
159
160 static void construct_and_merge_mock_circuits(Goblin& goblin, const size_t num_circuits = 3)
161 {
162 for (size_t idx = 0; idx < num_circuits - 1; ++idx) {
165 goblin.op_queue->merge();
166 }
169 // The final subtable plays the role of the hiding kernel, whose merge subtable has a fixed size that the
170 // merge verifier hard-codes the shift size from. Pad with ecc ops up to that size, leaving room for the
171 // trailing ZK random ops.
172 const size_t target_size = bb::HIDING_KERNEL_ULTRA_OPS - TranslatorCircuitBuilder::NUM_RANDOM_OPS_END;
173 BB_ASSERT_LTE(goblin.op_queue->get_current_subtable_size(), target_size);
174 while (goblin.op_queue->get_current_subtable_size() < target_size) {
175 builder.queue_ecc_add_accum(Point::random_element(&engine));
176 }
177 // Add random ops at END for Translator ZK
179 }
180
191 {
192 BB_BENCH();
193
194 // Add operations representing general kernel logic e.g. state updates. Note: these are structured to make
195 // the kernel "full" within the dyadic size 2^17
196 const size_t NUM_ECDSA_VERIFICATIONS = 2;
197 const size_t NUM_SHA_HASHES = 10;
199 generate_sha256_test_circuit<MegaBuilder>(builder, NUM_SHA_HASHES);
200 }
201};
202} // namespace bb
#define BB_ASSERT_LTE(left, right,...)
Definition assert.hpp:158
#define BB_BENCH()
Definition bb_bench.hpp:268
CommitmentKey object over a pairing group 𝔾₁.
Manages ECC operations for the Goblin proving system.
std::shared_ptr< OpQueue > op_queue
Definition goblin.hpp:59
Curve::AffineElement Point
static void add_some_ecc_op_gates(MegaBuilder &builder)
Generate a simple test circuit with some ECC op gates and conventional arithmetic gates.
static void construct_mock_app_circuit(MegaBuilder &builder, bool large=false)
Populate a builder with some arbitrary but nontrivial constraints.
static void construct_simple_circuit(MegaBuilder &builder)
Generate a simple test circuit with some ECC op gates and conventional arithmetic gates.
static void construct_mock_folding_kernel(MegaBuilder &builder)
Construct a mock kernel circuit.
static void randomise_op_queue(MegaBuilder &builder, size_t num_ops)
Add some randomness into the op queue.
RecursiveVerifierInstance::VKAndHash RecursiveVKAndHash
static void construct_and_merge_mock_circuits(Goblin &goblin, const size_t num_circuits=3)
static constexpr size_t NUM_WIRES
std::shared_ptr< RecursiveVerifierInstance > RecursiveVerifierAccumulator
static constexpr size_t NUM_WIRES
NativeVerificationKey_< PrecomputedEntities< Commitment >, Codec, HashFunction, CommitmentKey > VerificationKey
The verification key stores commitments to the precomputed (non-witness) polynomials used by the veri...
The recursive counterpart to the "native" Mega flavor.
static void construct_arithmetic_circuit(Builder &builder, const size_t target_log2_dyadic_size=4, bool include_public_inputs=true)
Populate a builder with a specified number of arithmetic gates; includes a PI.
Base Native verification key class.
Definition flavor.hpp:138
The VerifierInstance encapsulates all the necessary information for a Honk Verifier to verify a proof...
typename Flavor::VKAndHash VKAndHash
static FF hash(const std::vector< FF > &input)
Hashes a vector of field elements.
bb::fq BaseField
Definition bn254.hpp:19
typename Group::affine_element AffineElement
Definition bn254.hpp:22
bb::fr ScalarField
Definition bn254.hpp:18
static std::array< field_ct, 8 > sha256_block(const std::array< field_ct, 8 > &h_init, const std::array< field_ct, 16 > &input)
Apply the SHA-256 compression function to a single 512-bit message block.
Definition sha256.cpp:334
static void add_default(Builder &builder)
Add default public inputs when they are not present.
AluTraceBuilder builder
Definition alu.test.cpp:124
numeric::RNG & engine
stdlib::witness_t< Builder > witness_ct
AvmProvingInputs inputs
void generate_ecdsa_verification_test_circuit(Builder &builder, size_t num_iterations)
Generate a simple ecdsa verification circuit for testing purposes.
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
void generate_sha256_test_circuit(Builder &builder, size_t num_iterations)
Generate a test circuit using SHA256 compression (sha256_block)
void generate_poseidon2_hash_test_circuit(Builder &builder, size_t num_inputs)
Generate a test circuit that computes a single poseidon2 hash over a vector of num_inputs field eleme...
MegaCircuitBuilder_< field< Bn254FrParams > > MegaCircuitBuilder
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
bb::VectorAffineElementPushSpan< BaseParams > out
static field random_element(numeric::RNG *engine=nullptr) noexcept