Skip to content

Commit

Permalink
Template composer lib; split plonk tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
codygunton committed Apr 11, 2023
1 parent c4bd95a commit 92e7e3e
Show file tree
Hide file tree
Showing 19 changed files with 386 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "standard_honk_composer_helper.hpp"
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/honk/flavor/flavor.hpp"
#include "barretenberg/proof_system/flavor/flavor.hpp"
#include "barretenberg/honk/pcs/commitment_key.hpp"
#include "barretenberg/numeric/bitop/get_msb.hpp"

Expand All @@ -22,16 +23,15 @@ namespace proof_system::honk {
* @param num_reserved_gates The number of reserved gates.
* @return Pointer to the initialized proving key updated with selector polynomials.
* */
template <typename CircuitConstructor>
std::shared_ptr<plonk::proving_key> StandardHonkComposerHelper<CircuitConstructor>::compute_proving_key_base(
std::shared_ptr<plonk::proving_key> StandardHonkComposerHelper::compute_proving_key_base(
const CircuitConstructor& constructor, const size_t minimum_circuit_size, const size_t num_randomized_gates)
{
// Initialize circuit_proving_key
// TODO(#229)(Kesha): replace composer types.
circuit_proving_key = initialize_proving_key(
circuit_proving_key = initialize_proving_key<Flavor>(
constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, ComposerType::STANDARD_HONK);
// Compute lagrange selectors
construct_lagrange_selector_forms(constructor, circuit_proving_key.get());
construct_selector_polynomials(constructor, circuit_proving_key.get());

return circuit_proving_key;
}
Expand Down Expand Up @@ -155,17 +155,17 @@ StandardVerifier StandardHonkComposerHelper<CircuitConstructor>::create_verifier
return output_state;
}

template <typename CircuitConstructor>
// template <typename CircuitConstructor>
template <typename Flavor>
// TODO(Cody): this file should be generic with regard to flavor/arithmetization/whatever.
StandardProver StandardHonkComposerHelper<CircuitConstructor>::create_prover(
StandardProver StandardHonkComposerHelper<typename Flavor::CircuitConstructor>::create_prover(
const CircuitConstructor& circuit_constructor)
{
compute_proving_key(circuit_constructor);
compute_witness(circuit_constructor);

size_t num_sumcheck_rounds(circuit_proving_key->log_circuit_size);
auto manifest = Flavor::create_manifest(circuit_constructor.public_inputs.size(), num_sumcheck_rounds);
// auto manifest = Flavor::create_manifest(circuit_constructor.public_inputs.size(), num_sumcheck_rounds);
StandardProver output_state(std::move(wire_polynomials), circuit_proving_key);

return output_state;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
#include "barretenberg/proof_system/arithmetization/gate_data.hpp"
#include "barretenberg/proof_system/composer/composer_helper_lib.hpp"
#include "barretenberg/proof_system/composer/permutation_helper.hpp"

#include "barretenberg/proof_system/flavor/flavor.hpp"
#include <utility>

namespace proof_system::honk {
// TODO(Kesha): change initializations to specify this parameter
// Cody: What does this mean?
template <typename CircuitConstructor> class StandardHonkComposerHelper {
class StandardHonkComposerHelper {
public:
using Flavor = flavor::Standard;
using CircuitConstructor = typename Flavor::CircuitConstructor;

static constexpr size_t NUM_RANDOMIZED_GATES = 2; // equal to the number of multilinear evaluations leaked
static constexpr size_t num_wires = CircuitConstructor::num_wires;
std::shared_ptr<plonk::proving_key> circuit_proving_key;
Expand Down Expand Up @@ -57,7 +58,7 @@ template <typename CircuitConstructor> class StandardHonkComposerHelper {

StandardVerifier create_verifier(const CircuitConstructor& circuit_constructor);

template <typename Flavor> StandardProver create_prover(const CircuitConstructor& circuit_constructor);
StandardProver create_prover(const CircuitConstructor& circuit_constructor);

// TODO(#216)(Adrian): Seems error prone to provide the number of randomized gates
// Cody: Where should this go? In the flavor (or whatever that becomes)?
Expand Down
14 changes: 7 additions & 7 deletions cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ namespace proof_system::honk {
*/
class StandardHonkComposer {
public:
static constexpr ComposerType type = ComposerType::STANDARD_HONK;
static constexpr merkle::HashType merkle_hash_type = merkle::HashType::LOOKUP_PEDERSEN;
static constexpr pedersen::CommitmentType commitment_type = pedersen::CommitmentType::FIXED_BASE_PEDERSEN;
using Flavor = flavor::Standard;
using CircuitConstructor = StandardCircuitConstructor;
static constexpr ComposerType type = ComposerType::STANDARD_HONK; // TODO(Cody): Get rid of this.

static constexpr size_t UINT_LOG2_BASE = 2;
// An instantiation of the circuit constructor that only depends on arithmetization, not on the proof system
StandardCircuitConstructor circuit_constructor;
CircuitConstructor circuit_constructor;
// Composer helper contains all proof-related material that is separate from circuit creation such as:
// 1) Proving and verification keys
// 2) CRS
// 3) Converting variables to witness vectors/polynomials
StandardHonkComposerHelper<StandardCircuitConstructor> composer_helper;
StandardHonkComposerHelper composer_helper;

// Leaving it in for now just in case
bool contains_recursive_proof = false;
static constexpr size_t num_wires = StandardCircuitConstructor::num_wires;
static constexpr size_t num_wires = CircuitConstructor::num_wires;

/**Standard methods*/

Expand Down Expand Up @@ -180,7 +180,7 @@ class StandardHonkComposer {
void compute_witness() { composer_helper.compute_witness(circuit_constructor); };

StandardVerifier create_verifier() { return composer_helper.create_verifier(circuit_constructor); }
StandardProver create_prover() { return composer_helper.create_prover<honk::StandardHonk>(circuit_constructor); };
StandardProver create_prover() { return composer_helper.create_prover(circuit_constructor); };

size_t& num_gates;
std::vector<barretenberg::fr>& variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,15 @@ namespace proof_system::plonk {
*
* @tparam Program settings needed to establish if w_4 is being used.
* */
template <typename CircuitConstructor>
void StandardPlonkComposerHelper<CircuitConstructor>::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
void StandardPlonkComposerHelper::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
{

if (computed_witness) {
return;
}
auto wire_polynomial_evaluations =
compute_witness_base(circuit_constructor, minimum_circuit_size, NUM_RANDOMIZED_GATES);
construct_wire_polynomials_base<Flavor>(circuit_constructor, minimum_circuit_size, NUM_RANDOMIZED_GATES);

for (size_t j = 0; j < program_width; ++j) {
std::string index = std::to_string(j + 1);
Expand All @@ -54,8 +53,7 @@ void StandardPlonkComposerHelper<CircuitConstructor>::compute_witness(const Circ
*
* @return Pointer to the initialized proving key updated with selector polynomials.
* */
template <typename CircuitConstructor>
std::shared_ptr<plonk::proving_key> StandardPlonkComposerHelper<CircuitConstructor>::compute_proving_key(
std::shared_ptr<plonk::proving_key> StandardPlonkComposerHelper::compute_proving_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_proving_key) {
Expand All @@ -65,12 +63,12 @@ std::shared_ptr<plonk::proving_key> StandardPlonkComposerHelper<CircuitConstruct
const size_t num_randomized_gates = NUM_RANDOMIZED_GATES;
// Initialize circuit_proving_key
// TODO(#229)(Kesha): replace composer types.
circuit_proving_key = proof_system::initialize_proving_key(
circuit_proving_key = proof_system::initialize_proving_key<Flavor>(
circuit_constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, ComposerType::STANDARD);
// Compute lagrange selectors
construct_lagrange_selector_forms(circuit_constructor, circuit_proving_key.get());
construct_selector_polynomials<Flavor>(circuit_constructor, circuit_proving_key.get());
// Make all selectors nonzero
enforce_nonzero_polynomial_selectors(circuit_constructor, circuit_proving_key.get());
enforce_nonzero_selector_polynomials<Flavor>(circuit_constructor, circuit_proving_key.get());
// Compute selectors in monomial form
compute_monomial_and_coset_selector_forms(circuit_proving_key.get(), standard_selector_properties());

Expand All @@ -90,8 +88,7 @@ std::shared_ptr<plonk::proving_key> StandardPlonkComposerHelper<CircuitConstruct
*
* @return Pointer to created circuit verification key.
* */
template <typename CircuitConstructor>
std::shared_ptr<plonk::verification_key> StandardPlonkComposerHelper<CircuitConstructor>::compute_verification_key(
std::shared_ptr<plonk::verification_key> StandardPlonkComposerHelper::compute_verification_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_verification_key) {
Expand All @@ -118,9 +115,7 @@ std::shared_ptr<plonk::verification_key> StandardPlonkComposerHelper<CircuitCons
* @return The verifier.
* */
// TODO(Cody): This should go away altogether.
template <typename CircuitConstructor>
plonk::Verifier StandardPlonkComposerHelper<CircuitConstructor>::create_verifier(
const CircuitConstructor& circuit_constructor)
plonk::Verifier StandardPlonkComposerHelper::create_verifier(const CircuitConstructor& circuit_constructor)
{
auto verification_key = compute_verification_key(circuit_constructor);

Expand All @@ -143,9 +138,7 @@ plonk::Verifier StandardPlonkComposerHelper<CircuitConstructor>::create_verifier
*
* @return Initialized prover.
* */
template <typename CircuitConstructor>
plonk::Prover StandardPlonkComposerHelper<CircuitConstructor>::create_prover(
const CircuitConstructor& circuit_constructor)
plonk::Prover StandardPlonkComposerHelper::create_prover(const CircuitConstructor& circuit_constructor)
{
// Compute q_l, etc. and sigma polynomials.
compute_proving_key(circuit_constructor);
Expand All @@ -172,5 +165,4 @@ plonk::Prover StandardPlonkComposerHelper<CircuitConstructor>::create_prover(
return output_state;
}

template class StandardPlonkComposerHelper<StandardCircuitConstructor>;
} // namespace proof_system::plonk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "barretenberg/proof_system/flavor/flavor.hpp"
#include "barretenberg/srs/reference_string/file_reference_string.hpp"
#include "barretenberg/plonk/proof_system/proving_key/proving_key.hpp"
#include "barretenberg/plonk/proof_system/prover/prover.hpp"
Expand All @@ -14,8 +15,10 @@
namespace proof_system::plonk {
// TODO(Kesha): change initializations to specify this parameter
// Cody: What does this mean?
template <typename CircuitConstructor> class StandardPlonkComposerHelper {
class StandardPlonkComposerHelper {
public:
using Flavor = plonk::flavor::Standard;
using CircuitConstructor = StandardCircuitConstructor;
static constexpr size_t NUM_RANDOMIZED_GATES = 2; // equal to the number of multilinear evaluations leaked
static constexpr size_t program_width = CircuitConstructor::program_width;
std::shared_ptr<plonk::proving_key> circuit_proving_key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ namespace proof_system::plonk {
*
* @return Pointer to the initialized proving key updated with selector polynomials.
* */
template <typename CircuitConstructor>
std::shared_ptr<plonk::proving_key> TurboPlonkComposerHelper<CircuitConstructor>::compute_proving_key(
std::shared_ptr<plonk::proving_key> TurboPlonkComposerHelper::compute_proving_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_proving_key) {
Expand All @@ -40,12 +39,12 @@ std::shared_ptr<plonk::proving_key> TurboPlonkComposerHelper<CircuitConstructor>
const size_t num_randomized_gates = NUM_RANDOMIZED_GATES;
// Initialize circuit_proving_key
// TODO(#229)(Kesha): replace composer types.
circuit_proving_key = initialize_proving_key(
circuit_proving_key = initialize_proving_key<Flavor>(
circuit_constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, ComposerType::TURBO);

construct_lagrange_selector_forms(circuit_constructor, circuit_proving_key.get());
construct_selector_polynomials<Flavor>(circuit_constructor, circuit_proving_key.get());

enforce_nonzero_polynomial_selectors(circuit_constructor, circuit_proving_key.get());
enforce_nonzero_selector_polynomials<Flavor>(circuit_constructor, circuit_proving_key.get());

compute_monomial_and_coset_selector_forms(circuit_proving_key.get(), turbo_selector_properties());

Expand All @@ -63,8 +62,7 @@ std::shared_ptr<plonk::proving_key> TurboPlonkComposerHelper<CircuitConstructor>
*
* @return Pointer to created circuit verification key.
* */
template <typename CircuitConstructor>
std::shared_ptr<plonk::verification_key> TurboPlonkComposerHelper<CircuitConstructor>::compute_verification_key(
std::shared_ptr<plonk::verification_key> TurboPlonkComposerHelper::compute_verification_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_verification_key) {
Expand Down Expand Up @@ -92,16 +90,15 @@ std::shared_ptr<plonk::verification_key> TurboPlonkComposerHelper<CircuitConstru
*
* @tparam Program settings needed to establish if w_4 is being used.
* */
template <typename CircuitConstructor>
void TurboPlonkComposerHelper<CircuitConstructor>::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
void TurboPlonkComposerHelper::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
{

if (computed_witness) {
return;
}
auto wire_polynomial_evaluations =
compute_witness_base(circuit_constructor, minimum_circuit_size, NUM_RANDOMIZED_GATES);
construct_wire_polynomials_base<Flavor>(circuit_constructor, minimum_circuit_size, NUM_RANDOMIZED_GATES);

for (size_t j = 0; j < program_width; ++j) {
std::string index = std::to_string(j + 1);
Expand All @@ -119,9 +116,7 @@ void TurboPlonkComposerHelper<CircuitConstructor>::compute_witness(const Circuit
*
* @return Initialized prover.
* */
template <typename CircuitConstructor>
plonk::TurboProver TurboPlonkComposerHelper<CircuitConstructor>::create_prover(
const CircuitConstructor& circuit_constructor)
plonk::TurboProver TurboPlonkComposerHelper::create_prover(const CircuitConstructor& circuit_constructor)
{
// Compute q_l, etc. and sigma polynomials.
compute_proving_key(circuit_constructor);
Expand Down Expand Up @@ -160,9 +155,7 @@ plonk::TurboProver TurboPlonkComposerHelper<CircuitConstructor>::create_prover(
* @return The verifier.
* */
// TODO(Cody): This should go away altogether.
template <typename CircuitConstructor>
plonk::TurboVerifier TurboPlonkComposerHelper<CircuitConstructor>::create_verifier(
const CircuitConstructor& circuit_constructor)
plonk::TurboVerifier TurboPlonkComposerHelper::create_verifier(const CircuitConstructor& circuit_constructor)
{
auto verification_key = compute_verification_key(circuit_constructor);

Expand All @@ -177,5 +170,4 @@ plonk::TurboVerifier TurboPlonkComposerHelper<CircuitConstructor>::create_verifi
return output_state;
}

template class TurboPlonkComposerHelper<TurboCircuitConstructor>;
} // namespace proof_system::plonk
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "barretenberg/proof_system/flavor/flavor.hpp"
#include "barretenberg/plonk/composer/splitting_tmp/composer_helper/composer_helper_lib.hpp"
#include "barretenberg/proof_system/composer/composer_helper_lib.hpp"
#include "barretenberg/srs/reference_string/file_reference_string.hpp"
Expand All @@ -8,8 +9,11 @@
#include "barretenberg/plonk/proof_system/verifier/verifier.hpp"

namespace proof_system::plonk {
template <typename CircuitConstructor> class TurboPlonkComposerHelper {
class TurboPlonkComposerHelper {
public:
using Flavor = plonk::flavor::Turbo;
using CircuitConstructor = TurboCircuitConstructor;

static constexpr size_t NUM_RANDOMIZED_GATES = 2; // equal to the number of multilinear evaluations leaked
static constexpr size_t program_width = CircuitConstructor::program_width;

Expand Down
Loading

0 comments on commit 92e7e3e

Please sign in to comment.