Skip to content

Commit

Permalink
Turbo!! And also fixed some of the fuzzer compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Rumata888 committed Mar 28, 2023
1 parent 34d8326 commit 7d6b0bb
Show file tree
Hide file tree
Showing 18 changed files with 2,238 additions and 179 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

namespace bonk {

enum TurboSelectors { QM, QC, Q1, Q2, Q3, Q4, Q5, QARITH, QFIXED, QRANGE, QLOGIC, NUM };
inline std::vector<std::string> turbo_selector_names()
{
std::vector<std::string> result{ "q_m", "q_c", "q_1", "q_2", "q_3", "q_4",
"q_5", "q_arith", "q_fixed", "q_range", "q_logic" };
std::vector<std::string> result{ "q_m", "q_c", "q_1", "q_2", "q_3", "q_4",
"q_5", "q_arith", "q_fixed_base", "q_range", "q_logic" };
return result;
}
class TurboCircuitConstructor : public CircuitConstructorBase<TURBO_BONK_WIDTH> {

enum TurboSelectors { QM, QC, Q1, Q2, Q3, Q4, Q5, QARITH, QFIXED, QRANGE, QLOGIC, NUM };

public:
static constexpr plonk::ComposerType type = plonk::ComposerType::TURBO;
static constexpr size_t UINT_LOG2_BASE = 2;
Expand Down Expand Up @@ -45,10 +47,11 @@ class TurboCircuitConstructor : public CircuitConstructorBase<TURBO_BONK_WIDTH>
barretenberg::fr range_gate_evaluation(const size_t index,
const barretenberg::fr alpha_bas,
const barretenberg::fr alpha);
// bool lazy_arithmetic_gate_evaluation_is_zero(size_t index);
// bool lazy_fixed_base_gate_evaluation_is_zero(size_t index);
// bool lazy_logic_gate_evaluation_is_zero(size_t index);
// bool lazy_range_gate_evaluation_is_zero(size_t index);

bool lazy_arithmetic_gate_check(const size_t gate_index);
bool lazy_fixed_base_gate_check(const size_t gate_index);
bool lazy_logic_gate_check(const size_t gate_index);
bool lazy_range_gate_check(const size_t gate_index);

bool check_circuit();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ TEST(turbo_circuit_constructor, base_case)
TurboCircuitConstructor circuit_constructor = TurboCircuitConstructor();
fr a = fr::one();
circuit_constructor.add_public_variable(a);

bool result = circuit_constructor.check_circuit();
EXPECT_EQ(result, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "composer_helper_lib.hpp"
#include "barretenberg/honk/pcs/commitment_key.hpp"
#include "barretenberg/honk/circuit_constructors/standard_circuit_constructor.hpp"
#include "barretenberg/honk/circuit_constructors/turbo_circuit_constructor.hpp"
namespace bonk {

/**
Expand Down Expand Up @@ -68,12 +69,31 @@ void construct_lagrange_selector_forms(const CircuitConstructor& circuit_constru
// this does not clash with any other values we want to place at the end of of the witness vectors. In later
// iterations of the Sumcheck, we will be able to efficiently cancel out any checks in the last 2^k rows, so any
// randomness or unique values should be placed there.

circuit_proving_key->polynomial_store.put(circuit_constructor.selector_names_[j] + "_lagrange",
std::move(selector_poly_lagrange));
}
}

/**
* @brief Fill the last index of each selector polynomial in lagrange form with a non-zero value
*
* @tparam CircuitConstructor The class holding the circuit
* @param circuit_constructor The object holding the circuit
* @param key Pointer to the proving key
*/
template <typename CircuitConstructor>
void enforce_nonzero_polynomial_selectors(const CircuitConstructor& circuit_constructor,
bonk::proving_key* circuit_proving_key)
{
for (size_t j = 0; j < circuit_constructor.num_selectors; ++j) {
auto current_selector =
circuit_proving_key->polynomial_store.get(circuit_constructor.selector_names_[j] + "_lagrange");
current_selector[current_selector.size() - 1] = j + 1;
circuit_proving_key->polynomial_store.put(circuit_constructor.selector_names_[j] + "_lagrange",
std::move(current_selector));
}
}

/**
* @brief Retrieve lagrange forms of selector polynomials and compute monomial and coset-monomial forms and put into
* cache
Expand Down Expand Up @@ -102,7 +122,6 @@ void compute_monomial_and_coset_selector_forms(bonk::proving_key* circuit_provin

// Remove the selector lagrange forms since they will not be needed beyond this point
circuit_proving_key->polynomial_store.remove(selector_properties[i].name + "_lagrange");

circuit_proving_key->polynomial_store.put(selector_properties[i].name, std::move(selector_poly));
circuit_proving_key->polynomial_store.put(selector_properties[i].name + "_fft", std::move(selector_poly_fft));
}
Expand Down Expand Up @@ -175,7 +194,7 @@ std::vector<barretenberg::polynomial> compute_witness_base(const CircuitConstruc
* (1) commitments to the selector, permutation, and lagrange (first/last) polynomials,
* (2) sets the polynomial manifest using the data from proving key.
*/
std::shared_ptr<bonk::verification_key> compute_verification_key_base_common(
std::shared_ptr<bonk::verification_key> compute_verification_key_common(
std::shared_ptr<bonk::proving_key> const& proving_key, std::shared_ptr<bonk::VerifierReferenceString> const& vrs)
{
auto circuit_verification_key = std::make_shared<bonk::verification_key>(
Expand Down Expand Up @@ -206,12 +225,18 @@ std::shared_ptr<bonk::verification_key> compute_verification_key_base_common(

return circuit_verification_key;
}

template std::shared_ptr<bonk::proving_key> initialize_proving_key<StandardCircuitConstructor>(
const StandardCircuitConstructor&, bonk::ReferenceStringFactory*, const size_t, const size_t, plonk::ComposerType);
template void construct_lagrange_selector_forms<StandardCircuitConstructor>(const StandardCircuitConstructor&,
bonk::proving_key*);
template std::vector<barretenberg::polynomial> compute_witness_base<StandardCircuitConstructor>(
const StandardCircuitConstructor&, const size_t, const size_t);
// Ensure we compile all versions so that there are no issues during linkage
#define COMPILE_FOR_CIRCUIT_CONSTRUCTOR(circuit_constructor) \
template std::shared_ptr<bonk::proving_key> initialize_proving_key<circuit_constructor>( \
const circuit_constructor&, bonk::ReferenceStringFactory*, const size_t, const size_t, plonk::ComposerType); \
template void construct_lagrange_selector_forms<circuit_constructor>(const circuit_constructor&, \
bonk::proving_key*); \
template std::vector<barretenberg::polynomial> compute_witness_base<circuit_constructor>( \
const circuit_constructor&, const size_t, const size_t); \
template void enforce_nonzero_polynomial_selectors<circuit_constructor>(const circuit_constructor& constructor, \
bonk::proving_key* circuit_proving_key);

COMPILE_FOR_CIRCUIT_CONSTRUCTOR(StandardCircuitConstructor)
COMPILE_FOR_CIRCUIT_CONSTRUCTOR(TurboCircuitConstructor)

} // namespace bonk
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ std::shared_ptr<bonk::proving_key> initialize_proving_key(const CircuitConstruct
plonk::ComposerType composer_type);

/**
* @brief Construct lagrange selector polynomials from ciruit selector information and put into polynomial cache
* @brief Construct lagrange selector polynomials from circuit selector information and put into polynomial cache
*
* @tparam CircuitConstructor The class holding the circuit
* @param circuit_constructor The object holding the circuit
Expand All @@ -36,6 +36,16 @@ std::shared_ptr<bonk::proving_key> initialize_proving_key(const CircuitConstruct
template <typename CircuitConstructor>
void construct_lagrange_selector_forms(const CircuitConstructor& circuit_constructor, bonk::proving_key* key);

/**
* @brief Fill the last index of each selector polynomial in lagrange form with a non-zero value
*
* @tparam CircuitConstructor The class holding the circuit
* @param circuit_constructor The object holding the circuit
* @param key Pointer to the proving key
*/
template <typename CircuitConstructor>
void enforce_nonzero_polynomial_selectors(const CircuitConstructor& circuit_constructor, bonk::proving_key* key);

/**
* @brief Retrieve lagrange forms of selector polynomials and compute monomial and coset-monomial forms and put into
* cache
Expand Down Expand Up @@ -65,7 +75,7 @@ std::vector<barretenberg::polynomial> compute_witness_base(const CircuitConstruc
* (1) commitments to the selector, permutation, and lagrange (first/last) polynomials,
* (2) sets the polynomial manifest using the data from proving key.
*/
std::shared_ptr<bonk::verification_key> compute_verification_key_base_common(
std::shared_ptr<bonk::verification_key> compute_verification_key_common(
std::shared_ptr<bonk::proving_key> const& proving_key, std::shared_ptr<bonk::VerifierReferenceString> const& vrs);

} // namespace bonk
Original file line number Diff line number Diff line change
Expand Up @@ -13,51 +13,6 @@

namespace bonk {

/**
* Compute proving key base.
*
* 1. Load crs.
* 2. Initialize this.circuit_proving_key.
* 3. Create constraint selector polynomials from each of this composer's `selectors` vectors and add them to the
* proving key.
*
* N.B. Need to add the fix for coefficients
*
* @param minimum_circuit_size Used as the total number of gates when larger than n + count of public inputs.
* @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<bonk::proving_key> StandardPlonkComposerHelper<CircuitConstructor>::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(
constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, plonk::ComposerType::STANDARD);
// Compute lagrange selectors
construct_lagrange_selector_forms(constructor, circuit_proving_key.get());
// Compute selectors in monomial form
compute_monomial_and_coset_selector_forms(circuit_proving_key.get(), standard_selector_properties());

return circuit_proving_key;
}

/**
* @brief Computes the verification key by computing the:
* (1) commitments to the selector, permutation, and lagrange (first/last) polynomials,
* (2) sets the polynomial manifest using the data from proving key.
*/

template <typename CircuitConstructor>
std::shared_ptr<bonk::verification_key> StandardPlonkComposerHelper<CircuitConstructor>::compute_verification_key_base(
std::shared_ptr<bonk::proving_key> const& proving_key, std::shared_ptr<bonk::VerifierReferenceString> const& vrs)
{

return compute_verification_key_base_common(proving_key, vrs);
}

/**
* Compute witness polynomials (w_1, w_2, w_3, w_4).
*
Expand Down Expand Up @@ -87,21 +42,38 @@ void StandardPlonkComposerHelper<CircuitConstructor>::compute_witness(const Circ
}

/**
* Compute proving key.
* Compute the polynomials q_l, q_r, etc. and sigma polynomial.
* Compute proving key
*
* 1. Load crs.
* 2. Initialize this.circuit_proving_key.
* 3. Create constraint selector polynomials from each of this composer's `selectors` vectors and add them to the
* proving key.
* 4. Compute sigma polynomial
*
* @return Proving key with saved computed polynomials.
* @return Pointer to the initialized proving key updated with selector polynomials.
* */

template <typename CircuitConstructor>
std::shared_ptr<bonk::proving_key> StandardPlonkComposerHelper<CircuitConstructor>::compute_proving_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_proving_key) {
return circuit_proving_key;
}
// Compute q_l, q_r, q_o, etc polynomials
StandardPlonkComposerHelper::compute_proving_key_base(circuit_constructor, plonk::ComposerType::STANDARD_HONK);
const size_t minimum_circuit_size = 0;
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_constructor,
crs_factory_.get(),
minimum_circuit_size,
num_randomized_gates,
plonk::ComposerType::STANDARD);
// Compute lagrange selectors
construct_lagrange_selector_forms(circuit_constructor, circuit_proving_key.get());
// Make all selectors nonzero
enforce_nonzero_polynomial_selectors(circuit_constructor, circuit_proving_key.get());
// Compute selectors in monomial form
compute_monomial_and_coset_selector_forms(circuit_proving_key.get(), standard_selector_properties());

// Compute sigma polynomials (we should update that late)
bonk::compute_standard_plonk_sigma_permutations<CircuitConstructor::program_width>(circuit_constructor,
Expand Down Expand Up @@ -130,8 +102,7 @@ std::shared_ptr<bonk::verification_key> StandardPlonkComposerHelper<CircuitConst
compute_proving_key(circuit_constructor);
}

circuit_verification_key = StandardPlonkComposerHelper::compute_verification_key_base(
circuit_proving_key, crs_factory_->get_verifier_crs());
circuit_verification_key = compute_verification_key_common(circuit_proving_key, crs_factory_->get_verifier_crs());
circuit_verification_key->composer_type = circuit_proving_key->composer_type;
circuit_verification_key->recursive_proof_public_input_indices =
std::vector<uint32_t>(recursive_proof_public_input_indices.begin(), recursive_proof_public_input_indices.end());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "barretenberg/honk/pcs/commitment_key.hpp"
#include "barretenberg/proof_system/verification_key/verification_key.hpp"
#include "barretenberg/plonk/proof_system/verifier/verifier.hpp"
#include "barretenberg/proof_system/composer/composer_base.hpp"
#include "composer_helper_lib.hpp"
#include "permutation_helper.hpp"

Expand Down Expand Up @@ -79,18 +78,6 @@ template <typename CircuitConstructor> class StandardPlonkComposerHelper {
plonk::Verifier create_verifier(const CircuitConstructor& circuit_constructor);
plonk::Prover 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)?
std::shared_ptr<bonk::proving_key> compute_proving_key_base(
const CircuitConstructor& circuit_constructor,
const size_t minimum_ciricut_size = 0,
const size_t num_randomized_gates = NUM_RANDOMIZED_GATES);
// This needs to be static as it may be used only to compute the selector commitments.

static std::shared_ptr<bonk::verification_key> compute_verification_key_base(
std::shared_ptr<bonk::proving_key> const& proving_key,
std::shared_ptr<bonk::VerifierReferenceString> const& vrs);

void compute_witness(const CircuitConstructor& circuit_constructor, const size_t minimum_circuit_size = 0);
/**
* Create a manifest, which specifies proof rounds, elements and who supplies them.
Expand Down
Loading

0 comments on commit 7d6b0bb

Please sign in to comment.