Skip to content

Commit

Permalink
refactor: Move remaining data out of Honk UltraComposer (#4848)
Browse files Browse the repository at this point in the history
As the name says, move all data out of the composer. This ripples into
changes in where the commitment key, crs factory, and srs are stored and
how they are initialized. The basic idea is to use the global versions
of these things when available, and to store and initialize them early,
in either flavor classes or instance classes (which will become flavor
classes).
  • Loading branch information
codygunton authored Mar 1, 2024
1 parent c4357c8 commit 823e071
Show file tree
Hide file tree
Showing 35 changed files with 107 additions and 152 deletions.
22 changes: 11 additions & 11 deletions barretenberg/cpp/scripts/bb-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ IMAGE_URI=$(calculate_image_uri $REPOSITORY)
retry docker pull $IMAGE_URI

TESTS=(
client_ivc_tests
flavor_tests
relations_tests
transcript_tests
commitment_schemes_tests
sumcheck_tests
eccvm_tests
translator_vm_tests
protogalaxy_tests
ultra_honk_tests
goblin_tests
client_ivc_tests
dsl_tests
crypto_aes128_tests
crypto_blake2s_tests
crypto_blake3s_tests
Expand All @@ -22,23 +32,13 @@ TESTS=(
crypto_poseidon2_tests
crypto_schnorr_tests
crypto_sha256_tests
dsl_tests
ecc_tests
eccvm_tests
flavor_tests
goblin_tests
join_split_example_proofs_inner_proof_data_tests
join_split_example_proofs_notes_tests
numeric_tests
plonk_tests
polynomials_tests
protogalaxy_tests
relations_tests
srs_tests
sumcheck_tests
transcript_tests
translator_vm_tests
ultra_honk_tests
vm_tests
)
TESTS_STR="${TESTS[@]}"
Expand Down
22 changes: 22 additions & 0 deletions barretenberg/cpp/scripts/ultra_honk_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
set -eu

# Move above script dir.
cd $(dirname $0)/..

cmake --preset clang16
cmake --build --preset clang16

cd build/

./bin/flavor_tests
./bin/relations_tests
./bin/transcript_tests
./bin/commitment_schemes_tests
./bin/sumcheck_tests
./bin/eccvm_tests
./bin/translator_vm_tests
./bin/protogalaxy_tests
./bin/ultra_honk_tests
./bin/goblin_tests
./bin/client_ivc_tests
./bin/stdlib_recursion_tests
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

namespace bb {

constexpr size_t COMMITMENT_TEST_NUM_POINTS = 4096;

template <class CK> inline std::shared_ptr<CK> CreateCommitmentKey();

template <> inline std::shared_ptr<CommitmentKey<curve::BN254>> CreateCommitmentKey<CommitmentKey<curve::BN254>>()
{
srs::init_crs_factory("../srs_db/ignition");
constexpr size_t n = 4096;
return std::make_shared<CommitmentKey<curve::BN254>>(n);
return std::make_shared<CommitmentKey<curve::BN254>>(COMMITMENT_TEST_NUM_POINTS);
}
// For IPA
template <> inline std::shared_ptr<CommitmentKey<curve::Grumpkin>> CreateCommitmentKey<CommitmentKey<curve::Grumpkin>>()
{
srs::init_grumpkin_crs_factory("../srs_db/grumpkin");
constexpr size_t n = 4096;
return std::make_shared<CommitmentKey<curve::Grumpkin>>(n);
return std::make_shared<CommitmentKey<curve::Grumpkin>>(COMMITMENT_TEST_NUM_POINTS);
}

template <typename CK> inline std::shared_ptr<CK> CreateCommitmentKey()
Expand All @@ -39,20 +39,16 @@ template <>
inline std::shared_ptr<VerifierCommitmentKey<curve::BN254>> CreateVerifierCommitmentKey<
VerifierCommitmentKey<curve::BN254>>()
{
constexpr size_t n = 4096;
std::shared_ptr<bb::srs::factories::CrsFactory<curve::BN254>> crs_factory(
new bb::srs::factories::FileCrsFactory<curve::BN254>("../srs_db/ignition", 4096));
return std::make_shared<VerifierCommitmentKey<curve::BN254>>(n, crs_factory);
return std::make_shared<VerifierCommitmentKey<curve::BN254>>();
}
// For IPA
template <>
inline std::shared_ptr<VerifierCommitmentKey<curve::Grumpkin>> CreateVerifierCommitmentKey<
VerifierCommitmentKey<curve::Grumpkin>>()
{
constexpr size_t n = 4096;
std::shared_ptr<bb::srs::factories::CrsFactory<curve::Grumpkin>> crs_factory(
new bb::srs::factories::FileCrsFactory<curve::Grumpkin>("../srs_db/grumpkin", 4096));
return std::make_shared<VerifierCommitmentKey<curve::Grumpkin>>(n, crs_factory);
auto crs_factory = std::make_shared<srs::factories::FileCrsFactory<curve::Grumpkin>>("../srs_db/grumpkin",
COMMITMENT_TEST_NUM_POINTS);
return std::make_shared<VerifierCommitmentKey<curve::Grumpkin>>(COMMITMENT_TEST_NUM_POINTS, crs_factory);
}
template <typename VK> inline std::shared_ptr<VK> CreateVerifierCommitmentKey()
// requires std::default_initializable<VK>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
#include "barretenberg/numeric/bitop/pow.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/polynomial_arithmetic.hpp"
#include "barretenberg/srs/factories/crs_factory.hpp"
#include "barretenberg/srs/factories/file_crs_factory.hpp"
#include "barretenberg/srs/global_crs.hpp"

#include <cstddef>
#include <memory>
Expand All @@ -35,19 +34,13 @@ template <> class VerifierCommitmentKey<curve::BN254> {
using Commitment = typename Curve::AffineElement;

public:
VerifierCommitmentKey() = delete;
std::shared_ptr<bb::srs::factories::VerifierCrs<Curve>> srs;

/**
* @brief Construct a new Kate Verification Key object from existing SRS
*
* @param num_points
* @param srs verifier G2 point
*/
VerifierCommitmentKey(
[[maybe_unused]] size_t num_points, // TODO(https://github.com/AztecProtocol/barretenberg/issues/874)
std::shared_ptr<bb::srs::factories::CrsFactory<Curve>> crs_factory)
: srs(crs_factory->get_verifier_crs())
{}
VerifierCommitmentKey()
{
srs::init_crs_factory("../srs_db/ignition");
srs = srs::get_crs_factory<Curve>()->get_verifier_crs();
};

/**
* @brief verifies a pairing equation over 2 points using the verifier SRS
Expand All @@ -65,8 +58,6 @@ template <> class VerifierCommitmentKey<curve::BN254> {

return (result == Curve::TargetField::one());
}

std::shared_ptr<bb::srs::factories::VerifierCrs<Curve>> srs;
};

/**
Expand All @@ -89,7 +80,7 @@ template <> class VerifierCommitmentKey<curve::Grumpkin> {
* @param num_points specifies the length of the SRS
* @param path is the location to the SRS file
*/
VerifierCommitmentKey(size_t num_points, std::shared_ptr<bb::srs::factories::CrsFactory<Curve>> crs_factory)
VerifierCommitmentKey(size_t num_points, const std::shared_ptr<bb::srs::factories::CrsFactory<Curve>>& crs_factory)
: pippenger_runtime_state(num_points)
, srs(crs_factory->get_verifier_crs(num_points))

Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/flavor/ecc_vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ template <typename CycleGroup_T, typename Curve_T, typename PCS_T> class ECCVMBa
* resolve that, and split out separate PrecomputedPolynomials/Commitments data for clarity but also for
* portability of our circuits.
*/
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>>;
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;

/**
* @brief A container for polynomials produced after the first round of sumcheck.
Expand Down
8 changes: 7 additions & 1 deletion barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
#include "barretenberg/polynomials/univariate.hpp"
#include "barretenberg/proof_system/types/circuit_type.hpp"
#include <array>
#include <barretenberg/srs/global_crs.hpp>
#include <concepts>
#include <vector>

Expand Down Expand Up @@ -142,20 +143,25 @@ class ProvingKey_ : public PrecomputedPolynomials, public WitnessPolynomials {
*
* @tparam PrecomputedEntities An instance of PrecomputedEntities_ with affine_element data type and handle type.
*/
template <typename PrecomputedCommitments> class VerificationKey_ : public PrecomputedCommitments {
template <typename PrecomputedCommitments, typename VerifierCommitmentKey>
class VerificationKey_ : public PrecomputedCommitments {
public:
std::shared_ptr<VerifierCommitmentKey> pcs_verification_key;

VerificationKey_() = default;
VerificationKey_(const size_t circuit_size, const size_t num_public_inputs)
{
this->circuit_size = circuit_size;
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;
};

template <typename ProvingKeyPtr> VerificationKey_(const ProvingKeyPtr& proving_key)
{
this->circuit_size = proving_key->circuit_size;
this->log_circuit_size = numeric::get_msb(this->circuit_size);
this->num_public_inputs = proving_key->num_public_inputs;
this->pcs_verification_key = std::make_shared<VerifierCommitmentKey>();

for (auto [polynomial, commitment] : zip_view(proving_key->get_precomputed_polynomials(), this->get_all())) {
commitment = proving_key->commitment_key->commit(polynomial);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ class AvmFlavor {
RefArray<Polynomial, 0> get_table_column_wires() { return {}; };
};

using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>>;
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;

using FoldedPolynomials = AllEntities<std::vector<FF>>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class ToyFlavor {
RefArray<Polynomial, 0> get_table_column_wires() { return {}; };
};

using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>>;
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;

using FoldedPolynomials = AllEntities<std::vector<FF>>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ class GoblinTranslatorFlavor {
* resolve that, and split out separate PrecomputedPolynomials/Commitments data for clarity but also for
* portability of our circuits.
*/
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>>;
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;

/**
* @brief A field element for each entity of the flavor. These entities represent the prover polynomials
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/flavor/goblin_ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ class GoblinUltraFlavor {
* circuits.
* @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/876)
*/
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>>;
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;

/**
* @brief A container for storing the partially evaluated multivariates produced by sumcheck.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/polynomials/univariate.hpp"
#include "barretenberg/proof_system/circuit_builder/goblin_ultra_circuit_builder.hpp"
#include "barretenberg/srs/factories/crs_factory.hpp"
#include <array>
#include <concepts>
#include <span>
#include <string>
#include <type_traits>
#include <vector>

#include "barretenberg/stdlib/primitives/curves/bn254.hpp"
#include "barretenberg/stdlib/primitives/field/field.hpp"
#include "barretenberg/stdlib/recursion/honk/transcript/transcript.hpp"
Expand Down Expand Up @@ -104,7 +96,8 @@ template <typename BuilderType> class GoblinUltraRecursiveFlavor_ {
* circuits.
* This differs from GoblinUltra in how we construct the commitments.
*/
class VerificationKey : public VerificationKey_<GoblinUltraFlavor::PrecomputedEntities<Commitment>> {
class VerificationKey
: public VerificationKey_<GoblinUltraFlavor::PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
{
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/cpp/src/barretenberg/flavor/ultra.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class UltraFlavor {
* that, and split out separate PrecomputedPolynomials/Commitments data for clarity but also for portability of our
* circuits.
*/
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>>;
using VerificationKey = VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey>;

/**
* @brief A field element for each entity of the flavor. These entities represent the prover polynomials
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ template <typename BuilderType> class UltraRecursiveFlavor_ {
* that, and split out separate PrecomputedPolynomials/Commitments data for clarity but also for portability of our
* circuits.
*/
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>> {
class VerificationKey : public VerificationKey_<PrecomputedEntities<Commitment>, VerifierCommitmentKey> {
public:
VerificationKey(const size_t circuit_size, const size_t num_public_inputs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ namespace bb {
* */
template <IsUltraFlavor Flavor>
DeciderProver_<Flavor>::DeciderProver_(const std::shared_ptr<Instance>& inst,
const std::shared_ptr<CommitmentKey>& commitment_key,
const std::shared_ptr<Transcript>& transcript)
: accumulator(std::move(inst))
, transcript(transcript)
, commitment_key(commitment_key)
, commitment_key(inst->commitment_key)
{}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ template <IsUltraFlavor Flavor> class DeciderProver_ {

public:
explicit DeciderProver_(const std::shared_ptr<Instance>&,
const std::shared_ptr<CommitmentKey>&,
const std::shared_ptr<Transcript>& transcript = std::make_shared<Transcript>());

BB_PROFILE void execute_relation_check_rounds();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ template <typename Flavor>
DeciderVerifier_<Flavor>::DeciderVerifier_(const std::shared_ptr<Transcript>& transcript,
const std::shared_ptr<VerifierInstance>& accumulator)
: accumulator(accumulator)
, pcs_verification_key(accumulator->verification_key->pcs_verification_key)
, transcript(transcript)
{}

template <typename Flavor>
DeciderVerifier_<Flavor>::DeciderVerifier_()
: pcs_verification_key(std::make_unique<VerifierCommitmentKey>(0, bb::srs::get_bn254_crs_factory()))
: pcs_verification_key(std::make_unique<VerifierCommitmentKey>())
, transcript(std::make_shared<Transcript>())
{}

Expand Down Expand Up @@ -52,7 +54,7 @@ template <typename Flavor> bool DeciderVerifier_<Flavor>::verify_proof(const Hon
multivariate_challenge,
transcript);

auto verified = pcs_verification_key->pairing_check(pairing_points[0], pairing_points[1]);
auto verified = accumulator->pcs_verification_key->pairing_check(pairing_points[0], pairing_points[1]);

return sumcheck_verified.value() && verified;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ std::shared_ptr<typename ProverInstances::Instance> ProtoGalaxyProver_<ProverIns
auto vanishing_polynomial_at_challenge = challenge * (challenge - FF(1));
std::vector<FF> lagranges{ FF(1) - challenge, challenge };

// TODO(https://github.com/AztecProtocol/barretenberg/issues/881): bad pattern
auto next_accumulator = std::make_shared<Instance>();
next_accumulator->is_accumulator = true;
next_accumulator->instance_size = instances[0]->instance_size;
next_accumulator->log_instance_size = instances[0]->log_instance_size;
next_accumulator->commitment_key = instances[0]->commitment_key;

// Compute the next target sum and send the next folding parameters to the verifier
FF next_target_sum =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
namespace bb {
template <class ProverInstances_> struct ProtogalaxyProofConstructionState {
using FF = typename ProverInstances_::FF;
using Instance = typename ProverInstances_::Instance;
using ProverInstance = typename ProverInstances_::Instance;

std::shared_ptr<Instance> accumulator;
std::shared_ptr<ProverInstance> accumulator;
Polynomial<FF> perturbator;
std::vector<FF> deltas;
Univariate<FF, ProverInstances_::BATCHED_EXTENDED_LENGTH, ProverInstances_::NUM> combiner_quotient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
// verifier and check that the result agrees.
auto native_verifier = inner_composer.create_verifier(instance->verification_key);
auto native_result = native_verifier.verify_proof(inner_proof);
auto recursive_result = native_verifier.pcs_verification_key->pairing_check(pairing_points[0].get_value(),
pairing_points[1].get_value());
auto recursive_result = native_verifier.key->pcs_verification_key->pairing_check(pairing_points[0].get_value(),
pairing_points[1].get_value());
EXPECT_EQ(recursive_result, native_result);

// Check 2: Ensure that the underlying native and recursive verification algorithms agree by ensuring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ class RecursiveMergeVerifierTest : public testing::Test {
GoblinMockCircuits::construct_simple_initial_circuit(sample_circuit);

// Generate a proof over the inner circuit
InnerComposer inner_composer;
MergeProver merge_prover{ op_queue };
auto merge_proof = merge_prover.construct_proof();

Expand All @@ -65,7 +64,7 @@ class RecursiveMergeVerifierTest : public testing::Test {
// verifier and check that the result agrees.
MergeVerifier native_verifier;
bool verified_native = native_verifier.verify_proof(merge_proof);
VerifierCommitmentKey pcs_verification_key(0, srs::get_bn254_crs_factory());
VerifierCommitmentKey pcs_verification_key;
auto verified_recursive =
pcs_verification_key.pairing_check(pairing_points[0].get_value(), pairing_points[1].get_value());
EXPECT_EQ(verified_native, verified_recursive);
Expand Down
Loading

0 comments on commit 823e071

Please sign in to comment.