Skip to content

Commit

Permalink
refactor based on discussion
Browse files Browse the repository at this point in the history
  • Loading branch information
maramihali committed Nov 30, 2023
1 parent d1f7246 commit 4fa6c3d
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,20 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
InnerComposer inner_composer;
auto instance = inner_composer.create_instance(inner_circuit);
auto prover = inner_composer.create_prover(instance); // A prerequisite for computing VK
const auto native_verification_key = instance->compute_verification_key();

// Instantiate the recursive verification key from the native verification key
OuterBuilder outer_circuit;
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, native_verification_key);
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, instance->verification_key);

// Spot check some values in the recursive VK to ensure it was constructed correctly
EXPECT_EQ(verification_key->circuit_size, native_verification_key->circuit_size);
EXPECT_EQ(verification_key->log_circuit_size, native_verification_key->log_circuit_size);
EXPECT_EQ(verification_key->num_public_inputs, native_verification_key->num_public_inputs);
EXPECT_EQ(verification_key->q_m.get_value(), native_verification_key->q_m);
EXPECT_EQ(verification_key->q_r.get_value(), native_verification_key->q_r);
EXPECT_EQ(verification_key->sigma_1.get_value(), native_verification_key->sigma_1);
EXPECT_EQ(verification_key->id_3.get_value(), native_verification_key->id_3);
EXPECT_EQ(verification_key->lagrange_ecc_op.get_value(), native_verification_key->lagrange_ecc_op);
EXPECT_EQ(verification_key->circuit_size, instance->verification_key->circuit_size);
EXPECT_EQ(verification_key->log_circuit_size, instance->verification_key->log_circuit_size);
EXPECT_EQ(verification_key->num_public_inputs, instance->verification_key->num_public_inputs);
EXPECT_EQ(verification_key->q_m.get_value(), instance->verification_key->q_m);
EXPECT_EQ(verification_key->q_r.get_value(), instance->verification_key->q_r);
EXPECT_EQ(verification_key->sigma_1.get_value(), instance->verification_key->sigma_1);
EXPECT_EQ(verification_key->id_3.get_value(), instance->verification_key->id_3);
EXPECT_EQ(verification_key->lagrange_ecc_op.get_value(), instance->verification_key->lagrange_ecc_op);
}

/**
Expand All @@ -166,11 +165,10 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
auto instance = inner_composer.create_instance(inner_circuit);
auto inner_prover = inner_composer.create_prover(instance);
auto inner_proof = inner_prover.construct_proof();
const auto native_verification_key = instance->compute_verification_key();

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, native_verification_key);
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, instance->verification_key);
RecursiveVerifier verifier(&outer_circuit, verification_key);
auto pairing_points = verifier.verify_proof(inner_proof);

Expand Down Expand Up @@ -213,7 +211,6 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi
auto instance = inner_composer.create_instance(inner_circuit);
auto inner_prover = inner_composer.create_prover(instance);
auto inner_proof = inner_prover.construct_proof();
const auto native_verification_key = instance->compute_verification_key();

// Arbitrarily tamper with the proof to be verified
inner_prover.transcript.deserialize_full_transcript();
Expand All @@ -223,7 +220,7 @@ template <typename BuilderType> class GoblinRecursiveVerifierTest : public testi

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, native_verification_key);
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, instance->verification_key);
RecursiveVerifier verifier(&outer_circuit, verification_key);
verifier.verify_proof(inner_proof);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,18 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
InnerComposer inner_composer;
auto instance = inner_composer.create_instance(inner_circuit);
auto prover = inner_composer.create_prover(instance); // A prerequisite for computing VK
const auto native_verification_key = instance->compute_verification_key();

// Instantiate the recursive verification key from the native verification key
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, native_verification_key);
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, instance->verification_key);

// Spot check some values in the recursive VK to ensure it was constructed correctly
EXPECT_EQ(verification_key->circuit_size, native_verification_key->circuit_size);
EXPECT_EQ(verification_key->log_circuit_size, native_verification_key->log_circuit_size);
EXPECT_EQ(verification_key->num_public_inputs, native_verification_key->num_public_inputs);
EXPECT_EQ(verification_key->q_m.get_value(), native_verification_key->q_m);
EXPECT_EQ(verification_key->q_r.get_value(), native_verification_key->q_r);
EXPECT_EQ(verification_key->sigma_1.get_value(), native_verification_key->sigma_1);
EXPECT_EQ(verification_key->id_3.get_value(), native_verification_key->id_3);
EXPECT_EQ(verification_key->circuit_size, instance->verification_key->circuit_size);
EXPECT_EQ(verification_key->log_circuit_size, instance->verification_key->log_circuit_size);
EXPECT_EQ(verification_key->num_public_inputs, instance->verification_key->num_public_inputs);
EXPECT_EQ(verification_key->q_m.get_value(), instance->verification_key->q_m);
EXPECT_EQ(verification_key->q_r.get_value(), instance->verification_key->q_r);
EXPECT_EQ(verification_key->sigma_1.get_value(), instance->verification_key->sigma_1);
EXPECT_EQ(verification_key->id_3.get_value(), instance->verification_key->id_3);
}

/**
Expand All @@ -150,11 +149,10 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
auto instance = inner_composer.create_instance(inner_circuit);
auto inner_prover = inner_composer.create_prover(instance);
auto inner_proof = inner_prover.construct_proof();
const auto native_verification_key = instance->compute_verification_key();

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, native_verification_key);
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, instance->verification_key);
RecursiveVerifier verifier(&outer_circuit, verification_key);
auto pairing_points = verifier.verify_proof(inner_proof);

Expand Down Expand Up @@ -198,7 +196,6 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te
auto instance = inner_composer.create_instance(inner_circuit);
auto inner_prover = inner_composer.create_prover(instance);
auto inner_proof = inner_prover.construct_proof();
const auto native_verification_key = instance->compute_verification_key();

// Arbitrarily tamper with the proof to be verified
inner_prover.transcript.deserialize_full_transcript();
Expand All @@ -208,7 +205,7 @@ template <typename BuilderType> class RecursiveVerifierTest : public testing::Te

// Create a recursive verification circuit for the proof of the inner circuit
OuterBuilder outer_circuit;
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, native_verification_key);
auto verification_key = std::make_shared<VerificationKey>(&outer_circuit, instance->verification_key);
RecursiveVerifier verifier(&outer_circuit, verification_key);
verifier.verify_proof(inner_proof);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,28 +353,6 @@ template <class Flavor> void ProverInstance_<Flavor>::initialize_prover_polynomi
}
}

/**
* @brief Commit to the wire polynomials (part of the witness), with the exception of the fourth wire, which is
* only commited to after adding memory records. In the Goblin Flavor, we also commit to the ECC OP wires and the
* DataBus columns
*/
template <class Flavor> void ProverInstance_<Flavor>::compute_wire_commitments()
{
witness_commitments.w_l = commitment_key->commit(proving_key->w_l);
witness_commitments.w_r = commitment_key->commit(proving_key->w_r);
witness_commitments.w_o = commitment_key->commit(proving_key->w_o);

if constexpr (IsGoblinFlavor<Flavor>) {
witness_commitments.ecc_op_wire_1 = commitment_key->commit(proving_key->ecc_op_wire_1);
witness_commitments.ecc_op_wire_2 = commitment_key->commit(proving_key->ecc_op_wire_2);
witness_commitments.ecc_op_wire_3 = commitment_key->commit(proving_key->ecc_op_wire_3);
witness_commitments.ecc_op_wire_4 = commitment_key->commit(proving_key->ecc_op_wire_4);

witness_commitments.calldata = commitment_key->commit(proving_key->calldata);
witness_commitments.calldata_read_counts = commitment_key->commit(proving_key->calldata_read_counts);
}
}

template <class Flavor> void ProverInstance_<Flavor>::compute_sorted_accumulator_polynomials(FF eta)
{
relation_parameters.eta = eta;
Expand All @@ -383,14 +361,10 @@ template <class Flavor> void ProverInstance_<Flavor>::compute_sorted_accumulator
prover_polynomials.sorted_accum = proving_key->sorted_accum;
prover_polynomials.sorted_accum_shift = proving_key->sorted_accum.shifted();

witness_commitments.sorted_accum = commitment_key->commit(prover_polynomials.sorted_accum);

// Finalize fourth wire polynomial by adding lookup memory records
add_plookup_memory_records_to_wire_4(eta);
prover_polynomials.w_4 = proving_key->w_4;
prover_polynomials.w_4_shift = proving_key->w_4.shifted();

witness_commitments.w_4 = commitment_key->commit(prover_polynomials.w_4);
}

/**
Expand Down Expand Up @@ -479,8 +453,6 @@ void ProverInstance_<Flavor>::compute_logderivative_inverse(FF beta, FF gamma)
// Compute permutation and lookup grand product polynomials
lookup_library::compute_logderivative_inverse<Flavor, typename Flavor::LogDerivLookupRelation>(
prover_polynomials, relation_parameters, proving_key->circuit_size);

witness_commitments.lookup_inverses = commitment_key->commit(prover_polynomials.lookup_inverses);
}

template <class Flavor> void ProverInstance_<Flavor>::compute_grand_product_polynomials(FF beta, FF gamma)
Expand All @@ -497,63 +469,6 @@ template <class Flavor> void ProverInstance_<Flavor>::compute_grand_product_poly

// Compute permutation and lookup grand product polynomials
grand_product_library::compute_grand_products<Flavor>(proving_key, prover_polynomials, relation_parameters);

witness_commitments.z_perm = commitment_key->commit(prover_polynomials.z_perm);
witness_commitments.z_lookup = commitment_key->commit(prover_polynomials.z_lookup);
}

/**
* Compute verification key consisting of selector precommitments.
*
* @return Pointer to the resulting verification key of the Instance.
* */
template <class Flavor>
std::shared_ptr<typename Flavor::VerificationKey> ProverInstance_<Flavor>::compute_verification_key()
{
if (verification_key) {
return verification_key;
}

verification_key =
std::make_shared<typename Flavor::VerificationKey>(proving_key->circuit_size, proving_key->num_public_inputs);

// Compute and store commitments to all precomputed polynomials
verification_key->q_m = commitment_key->commit(proving_key->q_m);
verification_key->q_l = commitment_key->commit(proving_key->q_l);
verification_key->q_r = commitment_key->commit(proving_key->q_r);
verification_key->q_o = commitment_key->commit(proving_key->q_o);
verification_key->q_c = commitment_key->commit(proving_key->q_c);
verification_key->sigma_1 = commitment_key->commit(proving_key->sigma_1);
verification_key->sigma_2 = commitment_key->commit(proving_key->sigma_2);
verification_key->sigma_3 = commitment_key->commit(proving_key->sigma_3);
verification_key->id_1 = commitment_key->commit(proving_key->id_1);
verification_key->id_2 = commitment_key->commit(proving_key->id_2);
verification_key->id_3 = commitment_key->commit(proving_key->id_3);
verification_key->lagrange_first = commitment_key->commit(proving_key->lagrange_first);
verification_key->lagrange_last = commitment_key->commit(proving_key->lagrange_last);

verification_key->q_4 = commitment_key->commit(proving_key->q_4);
verification_key->q_arith = commitment_key->commit(proving_key->q_arith);
verification_key->q_sort = commitment_key->commit(proving_key->q_sort);
verification_key->q_elliptic = commitment_key->commit(proving_key->q_elliptic);
verification_key->q_aux = commitment_key->commit(proving_key->q_aux);
verification_key->q_lookup = commitment_key->commit(proving_key->q_lookup);
verification_key->sigma_4 = commitment_key->commit(proving_key->sigma_4);
verification_key->id_4 = commitment_key->commit(proving_key->id_4);
verification_key->table_1 = commitment_key->commit(proving_key->table_1);
verification_key->table_2 = commitment_key->commit(proving_key->table_2);
verification_key->table_3 = commitment_key->commit(proving_key->table_3);
verification_key->table_4 = commitment_key->commit(proving_key->table_4);

// TODO(luke): Similar to the lagrange_first/last polynomials, we dont really need to commit to these polynomials
// due to their simple structure.
if constexpr (IsGoblinFlavor<Flavor>) {
verification_key->lagrange_ecc_op = commitment_key->commit(proving_key->lagrange_ecc_op);
verification_key->q_busread = commitment_key->commit(proving_key->q_busread);
verification_key->databus_id = commitment_key->commit(proving_key->databus_id);
}

return verification_key;
}

template class ProverInstance_<honk::flavor::Ultra>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ template <class Flavor> class ProverInstance_ {
public:
std::shared_ptr<ProvingKey> proving_key;
std::shared_ptr<VerificationKey> verification_key;
std::shared_ptr<CommitmentKey> commitment_key;

ProverPolynomials prover_polynomials;
WitnessCommitments witness_commitments;
Expand Down Expand Up @@ -68,8 +67,6 @@ template <class Flavor> class ProverInstance_ {
ProverInstance_() = default;
~ProverInstance_() = default;

std::shared_ptr<VerificationKey> compute_verification_key();

void initialize_prover_polynomials();

void compute_wire_commitments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,7 @@ template <class Flavor> class InstanceTests : public testing::Test {
// Get random challenge eta
auto eta = FF::random_element();

// Construct mock sorted list polynomials.
std::vector<Polynomial> sorted_lists;
auto sorted_list_polynomials = instance.sorted_polynomials;
for (auto& sorted_list_poly : sorted_list_polynomials) {
Polynomial random_polynomial = get_random_polynomial(instance.proving_key->circuit_size);
sorted_lists.emplace_back(random_polynomial);
populate_span(sorted_list_poly, random_polynomial);
}

// Method 1: computed sorted list accumulator polynomial using prover library method
instance.compute_sorted_list_accumulator(eta);
Expand All @@ -78,10 +71,11 @@ template <class Flavor> class InstanceTests : public testing::Test {
const FF eta_cube = eta_sqr * eta;

// Compute s = s_1 + η*s_2 + η²*s_3 + η³*s_4
Polynomial sorted_list_accumulator_expected{ sorted_lists[0] };
Polynomial sorted_list_accumulator_expected{ sorted_list_polynomials[0] };
for (size_t i = 0; i < instance.proving_key->circuit_size; ++i) {
sorted_list_accumulator_expected[i] +=
sorted_lists[1][i] * eta + sorted_lists[2][i] * eta_sqr + sorted_lists[3][i] * eta_cube;
sorted_list_accumulator_expected[i] += sorted_list_polynomials[1][i] * eta +
sorted_list_polynomials[2][i] * eta_sqr +
sorted_list_polynomials[3][i] * eta_cube;
}

EXPECT_EQ(sorted_list_accumulator, sorted_list_accumulator_expected);
Expand Down
Loading

0 comments on commit 4fa6c3d

Please sign in to comment.