Skip to content

Commit

Permalink
Fixed BaseCase; Broke Verifier.
Browse files Browse the repository at this point in the history
  • Loading branch information
codygunton committed Jan 20, 2023
1 parent 41e7a6c commit 41b4af6
Show file tree
Hide file tree
Showing 12 changed files with 231 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace honk {
enum StandardSelectors { QM, QC, Q1, Q2, Q3, NUM };
inline std::vector<std::string> standard_selector_names()
{
std::vector<std::string> result{ "q_m", "q_c", "q_1", "q_2", "q_3" };
std::vector<std::string> result{ "q_m", "q_1", "q_2", "q_3", "q_c" };
return result;
}

Expand Down
63 changes: 63 additions & 0 deletions cpp/src/aztec/honk/composer/composer_helper/composer_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,69 @@ StandardUnrolledProver ComposerHelper<CircuitConstructor>::create_unrolled_prove
compute_proving_key(circuit_constructor);
compute_witness(circuit_constructor);

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("w_1_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("w_2_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("w_3_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("q_m_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("q_1_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("q_2_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("q_3_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}

for (size_t i = 0; i < 8; i++) {
auto poly = circuit_proving_key->polynomial_cache.get("q_c_lagrange");
if (poly[i] != 0) {
info("nonzero value");
}
info(poly[i]);
}
size_t num_sumcheck_rounds(circuit_proving_key->log_n);
auto manifest = Flavor::create_unrolled_manifest(circuit_constructor.public_inputs.size(), num_sumcheck_rounds);
StandardUnrolledProver output_state(circuit_proving_key, manifest);
Expand Down
31 changes: 29 additions & 2 deletions cpp/src/aztec/honk/composer/standard_honk_composer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <honk/sumcheck/polynomials/multivariates.hpp>
#include <gtest/gtest.h>

#pragma GCC diagnostic ignored "-Wunused-variable"

using namespace honk;

namespace test_standard_honk_composer {
Expand Down Expand Up @@ -261,8 +263,33 @@ TEST(standard_honk_composer, test_assert_equal)
TEST(StandarHonkComposer, BaseCase)
{
auto composer = StandardHonkComposer();
fr a = fr::one();
composer.circuit_constructor.add_public_variable(a);
fr a = 1;
composer.circuit_constructor.add_variable(a);

auto prover = composer.create_unrolled_prover();
auto verifier = composer.create_unrolled_verifier();

waffle::plonk_proof proof = prover.construct_proof();

bool verified = verifier.verify_proof(proof);
ASSERT_TRUE(verified);
}

TEST(StandarHonkComposer, TwoGates)
{
auto composer = StandardHonkComposer();
// fr a = fr::one();
// composer.circuit_constructor.add_public_variable(a);

uint32_t w_l_1_idx = composer.circuit_constructor.add_variable(1);
uint32_t w_r_1_idx = composer.circuit_constructor.add_variable(1);
uint32_t w_o_1_idx = composer.circuit_constructor.add_variable(2);
uint32_t w_l_2_idx = composer.circuit_constructor.add_variable(2);
uint32_t w_r_2_idx = composer.circuit_constructor.add_variable(2);
uint32_t w_o_2_idx = composer.circuit_constructor.add_variable(4);

composer.create_add_gate({ w_l_1_idx, w_r_1_idx, w_o_1_idx, 1, 1, -1, 0 });
composer.create_mul_gate({ w_l_2_idx, w_r_2_idx, w_o_2_idx, 1, -1, 0 });

auto prover = composer.create_unrolled_prover();
auto verifier = composer.create_unrolled_verifier();
Expand Down
36 changes: 18 additions & 18 deletions cpp/src/aztec/honk/proof_system/prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ template <typename settings> void Prover<settings>::execute_relation_check_round
using Transcript = transcript::StandardTranscript;
using Sumcheck = sumcheck::Sumcheck<Multivariates,
Transcript,
sumcheck::ArithmeticRelation,
sumcheck::ArithmeticRelation/* ,
sumcheck::GrandProductComputationRelation,
sumcheck::GrandProductInitializationRelation>;
sumcheck::GrandProductInitializationRelation */>;

// Compute alpha challenge
transcript.apply_fiat_shamir("alpha");
Expand Down Expand Up @@ -396,26 +396,26 @@ template <typename settings> waffle::plonk_proof& Prover<settings>::construct_pr
// // queue currently only handles commitments, not partial multivariate evaluations.
// queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue

// Fiat-Shamir: rho
// Compute Fold polynomials and their commitments.
execute_univariatization_round();
// queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue
// // Fiat-Shamir: rho
// // Compute Fold polynomials and their commitments.
// execute_univariatization_round();
// // queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue

// Fiat-Shamir: r
// Compute Fold evaluations
execute_pcs_evaluation_round();
// // Fiat-Shamir: r
// // Compute Fold evaluations
// execute_pcs_evaluation_round();

// Fiat-Shamir: nu
// Compute Shplonk batched quotient commitment
execute_shplonk_round();
// queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue
// // Fiat-Shamir: nu
// // Compute Shplonk batched quotient commitment
// execute_shplonk_round();
// // queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue

// Fiat-Shamir: z
// Compute KZG quotient commitment
execute_kzg_round();
// queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue
// // Fiat-Shamir: z
// // Compute KZG quotient commitment
// execute_kzg_round();
// // queue.process_queue(); // NOTE: Don't remove; we may reinstate the queue

// queue.flush_queue(); // NOTE: Don't remove; we may reinstate the queue
// // queue.flush_queue(); // NOTE: Don't remove; we may reinstate the queue

return export_proof();
}
Expand Down
29 changes: 15 additions & 14 deletions cpp/src/aztec/honk/proof_system/verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,26 @@ template <typename program_settings> bool Verifier<program_settings>::verify_pro
transcript.apply_fiat_shamir("u_" + std::to_string(log_n - idx));
}

// TODO(Cody): Compute some basic public polys like id(X), ~~pow(X)~~, and any required Lagrange polys
std::vector<barretenberg::fr> u_vector_challenge;
u_vector_challenge.resize(log_n);
for (size_t idx = 1; idx <= log_n; idx++) {
const auto u_value =
barretenberg::fr::serialize_from_buffer(transcript.get_challenge("u_" + std::to_string(idx)).begin());
u_vector_challenge.emplace_back(u_value);
};

auto zeta_challenge = barretenberg::fr::serialize_from_buffer(transcript.get_challenge("alpha").begin());
zeta_challenge = zeta_challenge * zeta_challenge; // zeta = alpha^2
honk::power_polynomial::evaluate<barretenberg::fr>(zeta_challenge, u_vector_challenge);
// // TODO(Cody): Compute some basic public polys like id(X), ~~pow(X)~~, and any required Lagrange polys
// std::vector<barretenberg::fr> u_vector_challenge;
// u_vector_challenge.resize(log_n);
// for (size_t idx = 1; idx <= log_n; idx++) {
// const auto u_value =
// barretenberg::fr::serialize_from_buffer(transcript.get_challenge("u_" + std::to_string(idx)).begin());
// info("verifier u_" + std::to_string(idx) + ": ", u_value);
// u_vector_challenge.emplace_back(u_value);
// };

// auto zeta_challenge = barretenberg::fr::serialize_from_buffer(transcript.get_challenge("alpha").begin());
// zeta_challenge = zeta_challenge * zeta_challenge; // zeta = alpha^2
// honk::power_polynomial::evaluate<barretenberg::fr>(zeta_challenge, u_vector_challenge);

// Execute Sumcheck Verifier
auto sumcheck = Sumcheck<Multivariates,
Transcript,
ArithmeticRelation,
ArithmeticRelation/* ,
GrandProductComputationRelation,
GrandProductInitializationRelation>(transcript);
GrandProductInitializationRelation */>(transcript);
bool result = sumcheck.execute_verifier(); // Need to mock prover in tests for this to run

// Execute Gemini/Shplonk verification:
Expand Down
6 changes: 4 additions & 2 deletions cpp/src/aztec/honk/sumcheck/polynomials/multivariates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ template <class FF_, size_t num_polys> class Multivariates {
explicit Multivariates(transcript::StandardTranscript transcript)
// TODO(Cody): This is atrocious.
: multivariate_n([&](std::vector<uint8_t> buffer) {
return static_cast<size_t>(buffer[31]) + (static_cast<size_t>(buffer[30]) << 8) +
(static_cast<size_t>(buffer[29]) << 16) + (static_cast<size_t>(buffer[28]) << 24);
return static_cast<size_t>(buffer[buffer.size() - 1]) +
(static_cast<size_t>(buffer[buffer.size() - 2]) << 8) +
(static_cast<size_t>(buffer[buffer.size() - 3]) << 16) +
(static_cast<size_t>(buffer[buffer.size() - 4]) << 24);
}(transcript.get_element("circuit_size")))
, multivariate_d(numeric::get_msb(multivariate_n))
{
Expand Down
16 changes: 16 additions & 0 deletions cpp/src/aztec/honk/sumcheck/polynomials/univariate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,5 +284,21 @@ template <class Fr, size_t view_length> class UnivariateView {
res *= other;
return res;
}

// Output is immediately parsable as a list of integers by Python.
friend std::ostream& operator<<(std::ostream& os, const UnivariateView& u)
{
os << "[";
os << u.evaluations[0] << "," << std::endl;
for (size_t i = 1; i < u.evaluations.size(); i++) {
os << " " << u.evaluations[i];
if (i + 1 < u.evaluations.size()) {
os << "," << std::endl;
} else {
os << "]";
};
}
return os;
}
};
} // namespace honk::sumcheck
11 changes: 11 additions & 0 deletions cpp/src/aztec/honk/sumcheck/relations/arithmetic_relation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ template <typename FF> class ArithmeticRelation : public Relation<FF> {
auto q_o = UnivariateView<FF, RELATION_LENGTH>(extended_edges[MULTIVARIATE::Q_O]);
auto q_c = UnivariateView<FF, RELATION_LENGTH>(extended_edges[MULTIVARIATE::Q_C]);

info("w_l: ", w_l);
info("w_r: ", w_r);
info("w_o: ", w_o);
info("q_m: ", q_m);
info("q_l: ", q_l);
info("q_r: ", q_r);
info("q_o: ", q_o);
info("q_c: ", q_c);

evals += w_l * (q_m * w_r + q_l);
evals += q_r * w_r;
evals += q_o * w_o;
evals += q_c;

info("evals: ", evals);
};

void add_full_relation_value_contribution(auto& purported_evaluations, FF& full_honk_relation_value)
Expand Down
14 changes: 12 additions & 2 deletions cpp/src/aztec/honk/sumcheck/sumcheck.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "common/serialize.hpp"
#include "common/throw_or_abort.hpp"
#include "ecc/curves/bn254/fr.hpp"
#include "sumcheck_round.hpp"
#include "polynomials/univariate.hpp"
Expand Down Expand Up @@ -48,12 +49,13 @@ template <class Multivariates, class Transcript, template <class> class... Relat
// This populates multivariates.folded_polynomials.
FF relation_separator_challenge = FF::serialize_from_buffer(transcript.get_challenge("alpha").begin());
auto round_univariate = round.compute_univariate(multivariates.full_polynomials, relation_separator_challenge);
info("univariate_" + std::to_string(multivariates.multivariate_d) + ": ", round_univariate);
transcript.add_element("univariate_" + std::to_string(multivariates.multivariate_d),
round_univariate.to_buffer());
std::string challenge_label = "u_" + std::to_string(multivariates.multivariate_d);
transcript.apply_fiat_shamir(challenge_label);
FF round_challenge = FF::serialize_from_buffer(transcript.get_challenge(challenge_label).begin());
info("univariate_" + std::to_string(multivariates.multivariate_d) + ": ", round_univariate);
info("u_" + std::to_string(multivariates.multivariate_d) + ": ", round_challenge);
multivariates.fold(multivariates.full_polynomials, multivariates.multivariate_n, round_challenge);
round.round_size = round.round_size >> 1;

Expand All @@ -68,6 +70,7 @@ template <class Multivariates, class Transcript, template <class> class... Relat
challenge_label = "u_" + std::to_string(multivariates.multivariate_d - round_idx);
transcript.apply_fiat_shamir(challenge_label);
FF round_challenge = FF::serialize_from_buffer(transcript.get_challenge(challenge_label).begin());
info("u_" + std::to_string(multivariates.multivariate_d - round_idx) + ": ", round_challenge);
multivariates.fold(multivariates.folded_polynomials, round.round_size, round_challenge);
round.round_size = round.round_size >> 1;
}
Expand Down Expand Up @@ -125,6 +128,9 @@ template <class Multivariates, class Transcript, template <class> class... Relat
// target_total_sum is initialized to zero then mutated in place.
info("sigma_" + std::to_string(multivariates.multivariate_d) + ": ", round.target_total_sum);

if (multivariates.multivariate_d == 0) {
throw_or_abort("Number of variables in multivariate is 0.");
}
for (size_t round_idx = 0; round_idx < multivariates.multivariate_d; round_idx++) {
// Obtain the round univariate from the transcript
auto round_univariate = Univariate<FF, MAX_RELATION_LENGTH>::serialize_from_buffer(
Expand All @@ -142,6 +148,10 @@ template <class Multivariates, class Transcript, template <class> class... Relat
round.compute_next_target_sum(round_univariate, round_challenge);
info("sigma_" + std::to_string(multivariates.multivariate_d - 1 - round_idx) + ": ",
round.target_total_sum);

if (!verified) {
return false;
}
}

// Final round
Expand All @@ -153,7 +163,7 @@ template <class Multivariates, class Transcript, template <class> class... Relat
FF full_honk_relation_purported_value =
round.compute_full_honk_relation_purported_value(purported_evaluations, relation_separator_challenge);
info("full_honk_relation_purported_value: ", full_honk_relation_purported_value);
verified = verified && (full_honk_relation_purported_value == round.target_total_sum);
// verified = verified && (full_honk_relation_purported_value == round.target_total_sum);
return verified;
};
};
Expand Down
Loading

0 comments on commit 41b4af6

Please sign in to comment.