-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: circuit simulator for Ultra and GoblinUltra verifiers (#1195)
This PR introduces the `CircuitSimulator` repurposed specifically to simulate native verifiers via the recursive verifiers, currently specialised for bn254. Currently the simulator is able to simulate the `UltraVerifier` and `GoblinUltraVerifier`. Resolves AztecProtocol/barretenberg#944. --------- Co-authored-by: maramihali <mara@aztecprotocol.com>
- Loading branch information
1 parent
86c106f
commit 0032a3a
Showing
53 changed files
with
1,077 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
barretenberg_module(simulator_bench stdlib_honk_recursion stdlib_sha256 crypto_merkle_tree) |
134 changes: 134 additions & 0 deletions
134
barretenberg/cpp/src/barretenberg/benchmark/simulator_bench/simulator.bench.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
#include "barretenberg/goblin/goblin.hpp" | ||
#include "barretenberg/goblin/mock_circuits.hpp" | ||
#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" | ||
#include <benchmark/benchmark.h> | ||
|
||
using namespace benchmark; | ||
using namespace bb; | ||
|
||
namespace { | ||
template <typename RecursiveFlavor> class SimulatorFixture : public benchmark::Fixture { | ||
|
||
public: | ||
using Flavor = typename RecursiveFlavor::NativeFlavor; | ||
using ProverInstance = ProverInstance_<Flavor>; | ||
using Builder = typename Flavor::CircuitBuilder; | ||
using VerificationKey = typename Flavor::VerificationKey; | ||
using CircuitSimulator = typename RecursiveFlavor::CircuitBuilder; | ||
using SimulatingVerifier = stdlib::recursion::honk::UltraRecursiveVerifier_<RecursiveFlavor>; | ||
|
||
struct VerifierInput { | ||
HonkProof proof; | ||
std::shared_ptr<VerificationKey> verification_key; | ||
}; | ||
|
||
void SetUp([[maybe_unused]] const ::benchmark::State& state) override | ||
{ | ||
bb::srs::init_crs_factory("../srs_db/ignition"); | ||
} | ||
|
||
/** | ||
* @brief Create a Honk proof (either Ultra or GoblinUltra) for a non-trivial circuit. | ||
* | ||
* @param large determines whether the circuit is 2^17 or 2^19 | ||
*/ | ||
static VerifierInput create_proof(bool large = false) | ||
{ | ||
|
||
auto builder = construct_mock_function_circuit(large); | ||
auto instance = std::make_shared<ProverInstance>(builder); | ||
UltraProver_<Flavor> prover(instance); | ||
auto ultra_proof = prover.construct_proof(); | ||
auto verification_key = std::make_shared<VerificationKey>(instance->proving_key); | ||
return { ultra_proof, verification_key }; | ||
} | ||
|
||
/** | ||
* @brief Populate the builder with non-trivial operations that mock a circuit encountered in practice. | ||
* | ||
* @param large determines whether the circuit is 2^17 or 2^19 | ||
*/ | ||
static Builder construct_mock_function_circuit(bool large = false) | ||
{ | ||
using InnerCurve = bb::stdlib::bn254<Builder>; | ||
using fr_ct = InnerCurve::ScalarField; | ||
using point_ct = InnerCurve::AffineElement; | ||
using fr = typename InnerCurve::ScalarFieldNative; | ||
using point = typename InnerCurve::GroupNative::affine_element; | ||
Builder builder; | ||
|
||
// Perform a batch mul which will add some arbitrary goblin-style ECC op gates if the circuit arithmetic is | ||
// goblinisied otherwise it will add the conventional nonnative gates | ||
size_t num_points = 5; | ||
std::vector<point_ct> circuit_points; | ||
std::vector<fr_ct> circuit_scalars; | ||
for (size_t i = 0; i < num_points; ++i) { | ||
circuit_points.push_back(point_ct::from_witness(&builder, point::random_element())); | ||
circuit_scalars.push_back(fr_ct::from_witness(&builder, fr::random_element())); | ||
} | ||
point_ct::batch_mul(circuit_points, circuit_scalars); | ||
|
||
// Determine number of times to execute the below operations that constitute the mock circuit logic. Note | ||
// that the circuit size does not scale linearly with number of iterations due to e.g. amortization of lookup | ||
|
||
const size_t NUM_ITERATIONS_LARGE = 12; // results in circuit size 2^19 (502238 gates) | ||
const size_t NUM_ITERATIONS_MEDIUM = 3; // results in circuit size 2^17 (124843 gates) | ||
const size_t NUM_ITERATIONS = large ? NUM_ITERATIONS_LARGE : NUM_ITERATIONS_MEDIUM; | ||
|
||
stdlib::generate_sha256_test_circuit(builder, NUM_ITERATIONS); // min gates: ~39k | ||
stdlib::generate_ecdsa_verification_test_circuit(builder, NUM_ITERATIONS); // min gates: ~41k | ||
stdlib::generate_merkle_membership_test_circuit(builder, NUM_ITERATIONS); // min gates: ~29k | ||
|
||
return builder; | ||
} | ||
}; | ||
|
||
BENCHMARK_TEMPLATE_F(SimulatorFixture, GoblinNative, bb::GoblinUltraRecursiveFlavor_<bb::CircuitSimulatorBN254>) | ||
(benchmark::State& state) | ||
{ | ||
auto verifier_input = SimulatorFixture::create_proof(); | ||
for (auto _ : state) { | ||
UltraVerifier_<Flavor> ultra_verifier{ verifier_input.verification_key }; | ||
ultra_verifier.verify_proof((verifier_input.proof)); | ||
} | ||
} | ||
|
||
BENCHMARK_TEMPLATE_F(SimulatorFixture, GoblinSimulated, bb::GoblinUltraRecursiveFlavor_<bb::CircuitSimulatorBN254>) | ||
(benchmark::State& state) | ||
{ | ||
auto verifier_input = SimulatorFixture::create_proof(); | ||
for (auto _ : state) { | ||
CircuitSimulator simulator; | ||
SimulatingVerifier ultra_verifier{ &simulator, verifier_input.verification_key }; | ||
ultra_verifier.verify_proof((verifier_input.proof)); | ||
} | ||
} | ||
|
||
BENCHMARK_TEMPLATE_F(SimulatorFixture, UltraNative, bb::UltraRecursiveFlavor_<bb::CircuitSimulatorBN254>) | ||
(benchmark::State& state) | ||
{ | ||
auto verifier_input = SimulatorFixture::create_proof(); | ||
for (auto _ : state) { | ||
UltraVerifier_<typename SimulatorFixture::Flavor> ultra_verifier{ verifier_input.verification_key }; | ||
ultra_verifier.verify_proof((verifier_input.proof)); | ||
} | ||
} | ||
|
||
BENCHMARK_TEMPLATE_F(SimulatorFixture, UltraSimulated, bb::UltraRecursiveFlavor_<bb::CircuitSimulatorBN254>) | ||
(benchmark::State& state) | ||
{ | ||
auto verifier_input = SimulatorFixture::create_proof(); | ||
for (auto _ : state) { | ||
CircuitSimulator simulator; | ||
SimulatingVerifier ultra_verifier{ &simulator, verifier_input.verification_key }; | ||
ultra_verifier.verify_proof((verifier_input.proof)); | ||
} | ||
} | ||
|
||
BENCHMARK_REGISTER_F(SimulatorFixture, GoblinSimulated)->Unit(benchmark::kMillisecond); | ||
BENCHMARK_REGISTER_F(SimulatorFixture, UltraSimulated)->Unit(benchmark::kMillisecond); | ||
BENCHMARK_REGISTER_F(SimulatorFixture, GoblinNative)->Unit(benchmark::kMillisecond); | ||
BENCHMARK_REGISTER_F(SimulatorFixture, UltraNative)->Unit(benchmark::kMillisecond); | ||
|
||
} // namespace | ||
BENCHMARK_MAIN(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
barretenberg/cpp/src/barretenberg/plonk_honk_shared/types/merkle_hash_type.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#pragma once | ||
|
||
namespace bb::merkle { | ||
// TODO(Cody) Get rid of this? | ||
// TODO(https://github.com/AztecProtocol/barretenberg/issues/426) | ||
enum HashType { FIXED_BASE_PEDERSEN, LOOKUP_PEDERSEN }; | ||
} // namespace bb::merkle | ||
} // namespace bb::merkle |
4 changes: 2 additions & 2 deletions
4
barretenberg/cpp/src/barretenberg/plonk_honk_shared/types/pedersen_commitment_type.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#pragma once | ||
|
||
namespace bb::pedersen { | ||
// TODO(Cody) Get rid of this? | ||
// TODO(https://github.com/AztecProtocol/barretenberg/issues/426) | ||
enum CommitmentType { FIXED_BASE_PEDERSEN, LOOKUP_PEDERSEN }; | ||
} // namespace bb::pedersen | ||
} // namespace bb::pedersen |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.