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// === AUDIT STATUS ===
2// internal: { status: not started, auditors: [], date: YYYY-MM-DD }
3// external_1: { status: not started, auditors: [], date: YYYY-MM-DD }
4// external_2: { status: not started, auditors: [], date: YYYY-MM-DD }
5// =====================
6
7#pragma once
8
24
25namespace bb {
26
34template <typename Builder> void generate_sha256_test_circuit(Builder& builder, size_t num_iterations)
35{
38
39 // SHA-256 initial hash values (FIPS 180-4 section 5.3.3)
40 constexpr std::array<uint32_t, 8> H_INIT = { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
41 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 };
42
43 // Initialize h_init as witnesses
45 for (size_t i = 0; i < 8; i++) {
46 h_init[i] = witness_ct(&builder, H_INIT[i]);
47 }
48
49 // Create a block of zeros as witnesses
51 for (size_t i = 0; i < 16; i++) {
52 block[i] = witness_ct(&builder, 0);
53 }
54
55 // Iterate: feed output of compression back as h_init for next round
56 for (size_t i = 0; i < num_iterations; i++) {
57 h_init = stdlib::SHA256<Builder>::sha256_block(h_init, block);
58 }
59}
60
62 public:
78
79 static constexpr size_t NUM_WIRES = Flavor::NUM_WIRES;
80
90 static void construct_mock_app_circuit(MegaBuilder& builder, bool large = false)
91 {
92 BB_BENCH();
93
94 if (large) { // Results in circuit size 2^19
95 generate_sha256_test_circuit<MegaBuilder>(builder, 9);
97 } else { // Results in circuit size 2^17
98 generate_sha256_test_circuit<MegaBuilder>(builder, 8);
100 }
101
102 // TODO(https://github.com/AztecProtocol/barretenberg/issues/911): We require goblin ops to be added to the
103 // function circuit because we cannot support zero commtiments. While the builder handles this at
104 // ProverInstance creation stage via the add_gates_to_ensure_all_polys_are_non_zero function for other
105 // MegaHonk circuits (where we don't explicitly need to add goblin ops), in IVC merge proving happens prior to
106 // folding where the absense of goblin ecc ops will result in zero commitments.
108 }
109
116 {
117 BB_BENCH();
118
119 // Add some arbitrary ecc op gates
120 for (size_t i = 0; i < 3; ++i) {
121 auto point = Point::random_element(&engine);
122 auto scalar = FF::random_element(&engine);
123 builder.queue_ecc_add_accum(point);
124 builder.queue_ecc_mul_accum(point, scalar);
125 }
126 // queues the result of the preceding ECC
127 builder.queue_ecc_eq(); // should be eq and reset
128 }
129
133 static void randomise_op_queue(MegaBuilder& builder, size_t num_ops)
134 {
135
136 for (size_t i = 0; i < num_ops; ++i) {
137 builder.queue_ecc_random_op();
138 }
139 }
140
154
155 static void construct_and_merge_mock_circuits(Goblin& goblin, const size_t num_circuits = 3)
156 {
158 for (size_t idx = 0; idx < num_circuits - 1; ++idx) {
160 if (idx == num_circuits - 2) {
161 // Last circuit appended needs to begin with a no-op for translator to be shiftable
162 builder.queue_ecc_no_op();
163 // Add random ops at START for Translator ZK (lands at beginning of op queue table)
165 // Add hiding op for ECCVM ZK (prepended to ECCVM ops at row 1)
166 builder.queue_ecc_hiding_op(Fq::random_element(), Fq::random_element());
167 }
169 goblin.prove_merge();
170 // Pop the merge proof from the queue, Goblin will be verified at the end
171 goblin.merge_verification_queue.pop_front();
172 }
175 // Add random ops at END for Translator ZK
177 }
178
189 {
190 BB_BENCH();
191
192 // Add operations representing general kernel logic e.g. state updates. Note: these are structured to make
193 // the kernel "full" within the dyadic size 2^17
194 const size_t NUM_ECDSA_VERIFICATIONS = 2;
195 const size_t NUM_SHA_HASHES = 10;
197 generate_sha256_test_circuit<MegaBuilder>(builder, NUM_SHA_HASHES);
198 }
199};
200} // namespace bb
#define BB_BENCH()
Definition bb_bench.hpp:223
CommitmentKey object over a pairing group 𝔾₁.
Used to construct execution trace representations of elliptic curve operations.
void prove_merge(const std::shared_ptr< Transcript > &transcript=std::make_shared< Transcript >(), const MergeSettings merge_settings=MergeSettings::PREPEND)
Construct a merge proof for the goblin ECC ops in the provided circuit; append the proof to the merge...
Definition goblin.cpp:28
std::shared_ptr< OpQueue > op_queue
Definition goblin.hpp:48
std::deque< MergeProof > merge_verification_queue
Definition goblin.hpp:57
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
The verification key is responsible for storing the commitments to the precomputed (non-witness) poly...
static constexpr size_t NUM_WIRES
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.
static void construct_goblin_ecc_op_circuit(MegaCircuitBuilder &builder)
Populate a builder with some arbitrary goblinized ECC ops, one of each type.
The VerifierInstance encapsulates all the necessary information for a Mega Honk Verifier to verify a ...
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:248
static void add_default(Builder &builder)
Add default public inputs when they are not present.
The stdlib counterpart of VerifierInstance, used in recursive folding verification.
AluTraceBuilder builder
Definition alu.test.cpp:124
numeric::RNG & engine
bn254::witness_ct witness_ct
stdlib::field_t< Builder > field_ct
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)
MegaCircuitBuilder_< field< Bn254FrParams > > MegaCircuitBuilder
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
static field random_element(numeric::RNG *engine=nullptr) noexcept
curve::BN254::BaseField Fq