Skip to content

Commit

Permalink
WIP on building Honk.
Browse files Browse the repository at this point in the history
  • Loading branch information
codygunton committed Apr 11, 2023
1 parent 92e7e3e commit b5456e3
Show file tree
Hide file tree
Showing 12 changed files with 477 additions and 389 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "standard_honk_composer_helper.hpp"
#include "barretenberg/plonk/proof_system/proving_key/proving_key.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"
#include "barretenberg/srs/reference_string/reference_string.hpp"

#include <cstddef>
#include <cstdint>
Expand All @@ -23,15 +25,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.
* */
std::shared_ptr<plonk::proving_key> StandardHonkComposerHelper::compute_proving_key_base(
std::shared_ptr<StandardHonkComposerHelper::ProvingKey> 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<Flavor>(
constructor, crs_factory_.get(), minimum_circuit_size, num_randomized_gates, ComposerType::STANDARD_HONK);
// Compute lagrange selectors
construct_selector_polynomials(constructor, circuit_proving_key.get());
construct_selector_polynomials<Flavor>(constructor, circuit_proving_key.get());

return circuit_proving_key;
}
Expand All @@ -42,29 +44,30 @@ std::shared_ptr<plonk::proving_key> StandardHonkComposerHelper::compute_proving_
* (2) sets the polynomial manifest using the data from proving key.
*/

template <typename CircuitConstructor>
std::shared_ptr<plonk::verification_key> StandardHonkComposerHelper<CircuitConstructor>::compute_verification_key_base(
std::shared_ptr<plonk::proving_key> const& proving_key, std::shared_ptr<VerifierReferenceString> const& vrs)
std::shared_ptr<plonk::verification_key> StandardHonkComposerHelper::compute_verification_key_base(
std::shared_ptr<StandardHonkComposerHelper::ProvingKey> const& proving_key,
std::shared_ptr<VerifierReferenceString> const& vrs)
{
auto key = std::make_shared<plonk::verification_key>(
proving_key->circuit_size, proving_key->num_public_inputs, vrs, proving_key->composer_type);
// TODO(kesha): Dirty hack for now. Need to actually make commitment-agnositc
auto commitment_key = pcs::kzg::CommitmentKey(proving_key->circuit_size, "../srs_db/ignition");

// Compute and store commitments to all precomputed polynomials
key->commitments["Q_M"] = commitment_key.commit(proving_key->polynomial_store.get("q_m_lagrange"));
key->commitments["Q_1"] = commitment_key.commit(proving_key->polynomial_store.get("q_1_lagrange"));
key->commitments["Q_2"] = commitment_key.commit(proving_key->polynomial_store.get("q_2_lagrange"));
key->commitments["Q_3"] = commitment_key.commit(proving_key->polynomial_store.get("q_3_lagrange"));
key->commitments["Q_C"] = commitment_key.commit(proving_key->polynomial_store.get("q_c_lagrange"));
key->commitments["SIGMA_1"] = commitment_key.commit(proving_key->polynomial_store.get("sigma_1_lagrange"));
key->commitments["SIGMA_2"] = commitment_key.commit(proving_key->polynomial_store.get("sigma_2_lagrange"));
key->commitments["SIGMA_3"] = commitment_key.commit(proving_key->polynomial_store.get("sigma_3_lagrange"));
key->commitments["ID_1"] = commitment_key.commit(proving_key->polynomial_store.get("id_1_lagrange"));
key->commitments["ID_2"] = commitment_key.commit(proving_key->polynomial_store.get("id_2_lagrange"));
key->commitments["ID_3"] = commitment_key.commit(proving_key->polynomial_store.get("id_3_lagrange"));
key->commitments["LAGRANGE_FIRST"] = commitment_key.commit(proving_key->polynomial_store.get("L_first_lagrange"));
key->commitments["LAGRANGE_LAST"] = commitment_key.commit(proving_key->polynomial_store.get("L_last_lagrange"));
// // Compute and store commitments to all precomputed polynomials
// key->commitments["Q_M"] = commitment_key.commit(proving_key->polynomial_store.get("q_m_lagrange"));
// key->commitments["Q_1"] = commitment_key.commit(proving_key->polynomial_store.get("q_1_lagrange"));
// key->commitments["Q_2"] = commitment_key.commit(proving_key->polynomial_store.get("q_2_lagrange"));
// key->commitments["Q_3"] = commitment_key.commit(proving_key->polynomial_store.get("q_3_lagrange"));
// key->commitments["Q_C"] = commitment_key.commit(proving_key->polynomial_store.get("q_c_lagrange"));
// key->commitments["SIGMA_1"] = commitment_key.commit(proving_key->polynomial_store.get("sigma_1_lagrange"));
// key->commitments["SIGMA_2"] = commitment_key.commit(proving_key->polynomial_store.get("sigma_2_lagrange"));
// key->commitments["SIGMA_3"] = commitment_key.commit(proving_key->polynomial_store.get("sigma_3_lagrange"));
// key->commitments["ID_1"] = commitment_key.commit(proving_key->polynomial_store.get("id_1_lagrange"));
// key->commitments["ID_2"] = commitment_key.commit(proving_key->polynomial_store.get("id_2_lagrange"));
// key->commitments["ID_3"] = commitment_key.commit(proving_key->polynomial_store.get("id_3_lagrange"));
// key->commitments["LAGRANGE_FIRST"] =
// commitment_key.commit(proving_key->polynomial_store.get("L_first_lagrange")); key->commitments["LAGRANGE_LAST"] =
// commitment_key.commit(proving_key->polynomial_store.get("L_last_lagrange"));

return key;
}
Expand All @@ -78,14 +81,14 @@ std::shared_ptr<plonk::verification_key> StandardHonkComposerHelper<CircuitConst
*
* @tparam Program settings needed to establish if w_4 is being used.
* */
template <typename CircuitConstructor>
void StandardHonkComposerHelper<CircuitConstructor>::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
void StandardHonkComposerHelper::compute_witness(const CircuitConstructor& circuit_constructor,
const size_t minimum_circuit_size)
{
if (computed_witness) {
return;
}
wire_polynomials = compute_witness_base(circuit_constructor, minimum_circuit_size, NUM_RANDOMIZED_GATES);
wire_polynomials =
construct_wire_polynomials_base<Flavor>(circuit_constructor, minimum_circuit_size, NUM_RANDOMIZED_GATES);

computed_witness = true;
}
Expand All @@ -97,20 +100,19 @@ void StandardHonkComposerHelper<CircuitConstructor>::compute_witness(const Circu
* @return Proving key with saved computed polynomials.
* */

template <typename CircuitConstructor>
std::shared_ptr<plonk::proving_key> StandardHonkComposerHelper<CircuitConstructor>::compute_proving_key(
std::shared_ptr<StandardHonkComposerHelper::ProvingKey> StandardHonkComposerHelper::compute_proving_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_proving_key) {
return circuit_proving_key;
}
// Compute q_l, q_r, q_o, etc polynomials
// TODO(Cody): meaningless constructor here
StandardHonkComposerHelper::compute_proving_key_base(circuit_constructor, ComposerType::STANDARD_HONK);

// Compute sigma polynomials (we should update that late)
compute_standard_honk_sigma_permutations<CircuitConstructor::num_wires>(circuit_constructor,
circuit_proving_key.get());
compute_standard_honk_id_polynomials<CircuitConstructor::num_wires>(circuit_proving_key.get());
compute_standard_honk_sigma_permutations<Flavor>(circuit_constructor, circuit_proving_key.get());
compute_standard_honk_id_polynomials<Flavor>(circuit_proving_key.get());

compute_first_and_last_lagrange_polynomials(circuit_proving_key.get());

Expand All @@ -122,8 +124,7 @@ std::shared_ptr<plonk::proving_key> StandardHonkComposerHelper<CircuitConstructo
*
* @return Pointer to created circuit verification key.
* */
template <typename CircuitConstructor>
std::shared_ptr<plonk::verification_key> StandardHonkComposerHelper<CircuitConstructor>::compute_verification_key(
std::shared_ptr<plonk::verification_key> StandardHonkComposerHelper::compute_verification_key(
const CircuitConstructor& circuit_constructor)
{
if (circuit_verification_key) {
Expand All @@ -140,9 +141,7 @@ std::shared_ptr<plonk::verification_key> StandardHonkComposerHelper<CircuitConst
return circuit_verification_key;
}

template <typename CircuitConstructor>
StandardVerifier StandardHonkComposerHelper<CircuitConstructor>::create_verifier(
const CircuitConstructor& circuit_constructor)
StandardVerifier StandardHonkComposerHelper::create_verifier(const CircuitConstructor& circuit_constructor)
{
compute_verification_key(circuit_constructor);
StandardVerifier output_state(circuit_verification_key);
Expand All @@ -155,22 +154,20 @@ StandardVerifier StandardHonkComposerHelper<CircuitConstructor>::create_verifier
return output_state;
}

// template <typename CircuitConstructor>
template <typename Flavor>
// TODO(Cody): this file should be generic with regard to flavor/arithmetization/whatever.
StandardProver StandardHonkComposerHelper<typename Flavor::CircuitConstructor>::create_prover(
const CircuitConstructor& circuit_constructor)
//
StandardProver StandardHonkComposerHelper::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);
StandardProver output_state(std::move(wire_polynomials), circuit_proving_key);
// size_t num_sumcheck_rounds(circuit_proving_key->log_circuit_size);
// StandardProver output_state(std::move(wire_polynomials), circuit_proving_key);
auto crs_factory = ReferenceStringFactory();
auto crs = crs_factory.get_prover_crs(999);
auto pk_ptr = std::make_shared<plonk::proving_key>(12, 2, crs, ComposerType::STANDARD);

StandardProver output_state(std::move(wire_polynomials), pk_ptr);

return output_state;
}
template class StandardHonkComposerHelper<StandardCircuitConstructor>;
template StandardProver StandardHonkComposerHelper<StandardCircuitConstructor>::create_prover<StandardHonk>(
const StandardCircuitConstructor& circuit_constructor);
} // namespace proof_system::honk
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ namespace proof_system::honk {
class StandardHonkComposerHelper {
public:
using Flavor = flavor::Standard;
using CircuitConstructor = typename Flavor::CircuitConstructor;
using CircuitConstructor = Flavor::CircuitConstructor;
using ProvingKey = Flavor::ProvingKey;

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;
std::shared_ptr<ProvingKey> circuit_proving_key;
std::vector<barretenberg::polynomial> wire_polynomials;
std::shared_ptr<plonk::verification_key> circuit_verification_key;
// TODO(#218)(kesha): we need to put this into the commitment key, so that the composer doesn't have to handle srs
Expand All @@ -42,8 +43,7 @@ class StandardHonkComposerHelper {
StandardHonkComposerHelper(std::unique_ptr<ReferenceStringFactory>&& crs_factory)
: crs_factory_(std::move(crs_factory))
{}
StandardHonkComposerHelper(std::shared_ptr<plonk::proving_key> p_key,
std::shared_ptr<plonk::verification_key> v_key)
StandardHonkComposerHelper(std::shared_ptr<ProvingKey> p_key, std::shared_ptr<plonk::verification_key> v_key)
: circuit_proving_key(std::move(p_key))
, circuit_verification_key(std::move(v_key))
{}
Expand All @@ -53,7 +53,7 @@ class StandardHonkComposerHelper {
StandardHonkComposerHelper& operator=(const StandardHonkComposerHelper& other) = delete;
~StandardHonkComposerHelper() = default;

std::shared_ptr<plonk::proving_key> compute_proving_key(const CircuitConstructor& circuit_constructor);
std::shared_ptr<ProvingKey> compute_proving_key(const CircuitConstructor& circuit_constructor);
std::shared_ptr<plonk::verification_key> compute_verification_key(const CircuitConstructor& circuit_constructor);

StandardVerifier create_verifier(const CircuitConstructor& circuit_constructor);
Expand All @@ -62,14 +62,13 @@ class StandardHonkComposerHelper {

// 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<plonk::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);
std::shared_ptr<ProvingKey> 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<plonk::verification_key> compute_verification_key_base(
std::shared_ptr<plonk::proving_key> const& proving_key, std::shared_ptr<VerifierReferenceString> const& vrs);
std::shared_ptr<ProvingKey> const& proving_key, std::shared_ptr<VerifierReferenceString> const& vrs);

void compute_witness(const CircuitConstructor& circuit_constructor, const size_t minimum_circuit_size = 0);
};
Expand Down
5 changes: 3 additions & 2 deletions cpp/src/barretenberg/honk/composer/standard_honk_composer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class StandardHonkComposer {
public:
using Flavor = flavor::Standard;
using CircuitConstructor = StandardCircuitConstructor;
using ProvingKey = typename Flavor::ProvingKey;
static constexpr ComposerType type = ComposerType::STANDARD_HONK; // TODO(Cody): Get rid of this.

static constexpr size_t UINT_LOG2_BASE = 2;
Expand Down Expand Up @@ -61,7 +62,7 @@ class StandardHonkComposer {

{}

StandardHonkComposer(std::shared_ptr<plonk::proving_key> const& p_key,
StandardHonkComposer(std::shared_ptr<ProvingKey> const& p_key,
std::shared_ptr<plonk::verification_key> const& v_key,
size_t size_hint = 0)
: circuit_constructor(size_hint)
Expand Down Expand Up @@ -165,7 +166,7 @@ class StandardHonkComposer {

/**Proof and verification-related methods*/

std::shared_ptr<plonk::proving_key> compute_proving_key()
std::shared_ptr<ProvingKey> compute_proving_key()
{
return composer_helper.compute_proving_key(circuit_constructor);
}
Expand Down
25 changes: 19 additions & 6 deletions cpp/src/barretenberg/honk/composer/standard_honk_composer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "barretenberg/honk/sumcheck/relations/grand_product_computation_relation.hpp"
#include "barretenberg/honk/sumcheck/relations/grand_product_initialization_relation.hpp"
#include "barretenberg/honk/utils/public_inputs.hpp"
#include "barretenberg/polynomials/polynomial.hpp"

#include <gtest/gtest.h>

Expand Down Expand Up @@ -89,7 +90,9 @@ TEST(StandardHonkComposer, SigmaIDCorrectness)
// Let's check that indices are the same and nothing is lost, first
for (size_t j = 0; j < composer.num_wires; ++j) {
std::string index = std::to_string(j + 1);
const auto& sigma_j = proving_key->polynomial_store.get("sigma_" + index + "_lagrange");
const auto& sigma_j = barretenberg::polynomial();
// WORKTODO
// const auto& sigma_j = proving_key->polynomial_store.get("sigma_" + index + "_lagrange");
for (size_t i = 0; i < n; ++i) {
left *= (gamma + j * n + i);
right *= (gamma + sigma_j[i]);
Expand All @@ -115,9 +118,13 @@ TEST(StandardHonkComposer, SigmaIDCorrectness)

for (size_t j = 0; j < composer.num_wires; ++j) {
std::string index = std::to_string(j + 1);
const auto& permutation_polynomial = proving_key->polynomial_store.get("sigma_" + index + "_lagrange");
const auto& permutation_polynomial = barretenberg::polynomial(10);
// WORKTODO
// const auto& permutation_polynomial = proving_key->polynomial_store.get("sigma_" + index + "_lagrange");
const auto& witness_polynomial = composer.composer_helper.wire_polynomials[j];
const auto& id_polynomial = proving_key->polynomial_store.get("id_" + index + "_lagrange");
const auto& id_polynomial = barretenberg::polynomial(10);
// WORKTODO
// const auto& id_polynomial = proving_key->polynomial_store.get("id_" + index + "_lagrange");
// left = ∏ᵢ,ⱼ(ωᵢ,ⱼ + β⋅ind(i,j) + γ)
// right = ∏ᵢ,ⱼ(ωᵢ,ⱼ + β⋅σ(i,j) + γ)
for (size_t i = 0; i < proving_key->circuit_size; ++i) {
Expand Down Expand Up @@ -203,15 +210,20 @@ TEST(StandardHonkComposer, LagrangeCorrectness)
random_polynomial[i] = barretenberg::fr::random_element();
}
// Compute inner product of random polynomial and the first lagrange polynomial
barretenberg::polynomial first_lagrange_polynomial = proving_key->polynomial_store.get("L_first_lagrange");

auto first_lagrange_polynomial = barretenberg::polynomial(10);
// WORKTODO
// barretenberg::polynomial first_lagrange_polynomial = proving_key->polynomial_store.get("L_first_lagrange");
barretenberg::fr first_product(0);
for (size_t i = 0; i < proving_key->circuit_size; i++) {
first_product += random_polynomial[i] * first_lagrange_polynomial[i];
}
EXPECT_EQ(first_product, random_polynomial[0]);

// Compute inner product of random polynomial and the last lagrange polynomial
barretenberg::polynomial last_lagrange_polynomial = proving_key->polynomial_store.get("L_last_lagrange");
auto last_lagrange_polynomial = barretenberg::polynomial(10);
// WORKTODO
// barretenberg::polynomial last_lagrange_polynomial = proving_key->polynomial_store.get("L_last_lagrange");
barretenberg::fr last_product(0);
for (size_t i = 0; i < proving_key->circuit_size; i++) {
last_product += random_polynomial[i] * last_lagrange_polynomial[i];
Expand Down Expand Up @@ -260,7 +272,8 @@ TEST(StandardHonkComposer, AssertEquals)
// Put the sigma polynomials into a vector for easy access
for (size_t i = 0; i < composer.num_wires; i++) {
std::string index = std::to_string(i + 1);
sigma_polynomials.push_back(proving_key->polynomial_store.get("sigma_" + index + "_lagrange"));
// WORKTODO
// sigma_polynomials.push_back(proving_key->polynomial_store.get("sigma_" + index + "_lagrange"));
}

// Let's compute the maximum cycle
Expand Down
Loading

0 comments on commit b5456e3

Please sign in to comment.