From 5012bf8f46fff45529980c250f48b80e2ab9328f Mon Sep 17 00:00:00 2001 From: ledwards2225 Date: Fri, 25 Aug 2023 21:57:38 +0000 Subject: [PATCH] batch mul in kzg --- .../honk/flavor/ultra_recursive.hpp | 1 - .../cpp/src/barretenberg/honk/pcs/kzg/kzg.hpp | 34 ++++++++++++------- .../circuit_builder/ultra_circuit_builder.cpp | 2 +- .../circuit_builder/ultra_circuit_builder.hpp | 3 +- .../verifier/ultra_recursive_verifier.cpp | 5 +-- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp index 3162b8604828..1484a5b5fe1b 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp @@ -41,7 +41,6 @@ class UltraRecursive { public: using CircuitBuilder = UltraCircuitBuilder; using Curve = plonk::stdlib::bn254; - using PCS = pcs::kzg::KZG; using GroupElement = Curve::Element; using Commitment = Curve::Element; using CommitmentHandle = Curve::Element; diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.hpp index ae0ee53a517f..281aa0a67af7 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/honk/pcs/kzg/kzg.hpp @@ -11,7 +11,7 @@ namespace proof_system::honk::pcs::kzg { -template class KZG { +template class KZG { using CK = CommitmentKey; using VK = VerifierCommitmentKey; using Fr = typename Curve::ScalarField; @@ -72,31 +72,39 @@ template class KZG { * * @param claim OpeningClaim ({r, v}, C) * @return {P₀, P₁} where - * - P₀ = C − v⋅[1]₁ + r⋅[x]₁ - * - P₁ = [Q(x)]₁ + * - P₀ = C − v⋅[1]₁ + r⋅[W(x)]₁ + * - P₁ = [W(x)]₁ */ static std::array compute_pairing_points(const OpeningClaim& claim, auto& verifier_transcript) { auto quotient_commitment = verifier_transcript.template receive_from_prover("KZG:W"); - auto lhs = claim.commitment + (quotient_commitment * claim.opening_pair.challenge); - // Add the evaluation point contribution v⋅[1]₁. + GroupElement P_0; // Note: In the recursive setting, we only add the contribution if it is not the point at infinity (i.e. if the // evaluation is not equal to zero). - // TODO(luke): What is the proper way to handle this? Contraints to show scalar (evaluation) is zero? if constexpr (Curve::is_stdlib_type) { - if (!claim.opening_pair.evaluation.get_value().is_zero()) { - auto ctx = verifier_transcript.builder; - lhs -= GroupElement::one(ctx) * claim.opening_pair.evaluation; - } + auto builder = verifier_transcript.builder; + auto one = Fr::from_witness(builder, 1); + std::vector commitments = { claim.commitment, quotient_commitment }; + std::vector scalars = { one, claim.opening_pair.challenge }; + P_0 = GroupElement::template batch_mul(commitments, scalars); + // WORKTODO(luke): The evaluation is always zero due to the nature of shplonk. What is the proper way to + // handle this? Contraints to show scalar (evaluation) is zero? Or simply dont add anything and ensure this + // function is only used in the current context? + // if (!claim.opening_pair.evaluation.get_value().is_zero()) { + // auto ctx = verifier_transcript.builder; + // lhs -= GroupElement::one(ctx) * claim.opening_pair.evaluation; + // } } else { - lhs -= GroupElement::one() * claim.opening_pair.evaluation; + P_0 = claim.commitment; + P_0 += quotient_commitment * claim.opening_pair.challenge; + P_0 -= GroupElement::one() * claim.opening_pair.evaluation; } - auto rhs = -quotient_commitment; + auto P_1 = -quotient_commitment; - return { lhs, rhs }; + return { P_0, P_1 }; }; }; } // namespace proof_system::honk::pcs::kzg diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp index 7657f445fd37..3446b5cef878 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.cpp @@ -527,7 +527,7 @@ ecc_op_tuple UltraCircuitBuilder_::queue_ecc_add_accum(const barretenberg::g */ template ecc_op_tuple UltraCircuitBuilder_::queue_ecc_mul_accum(const barretenberg::g1::affine_element& point, - const barretenberg::fr& scalar) + const barretenberg::fr& scalar) { // Add raw op to op queue op_queue.mul_accumulate(point, scalar); diff --git a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp index bf90e980fc57..ab242bec44db 100644 --- a/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp +++ b/circuits/cpp/barretenberg/cpp/src/barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp @@ -705,13 +705,12 @@ template class UltraCircuitBuilder_ : public CircuitBuilderBase UltraRecursiveVerifier_; using Shplonk = ::proof_system::honk::pcs::shplonk::ShplonkVerifier_; - using PCS = typename Flavor::PCS; // note: This can only be KZG + using KZG = ::proof_system::honk::pcs::kzg::KZG; // note: This can only be KZG using VerifierCommitments = typename Flavor::VerifierCommitments; using CommitmentLabels = typename Flavor::CommitmentLabels; using RelationParams = ::proof_system::honk::sumcheck::RelationParameters; RelationParams relation_parameters; + info("Initial: num gates = ", builder->get_num_gates()); size_t prev_num_gates = builder->get_num_gates(); transcript = Transcript{ builder, proof.proof_data }; @@ -177,7 +178,7 @@ std::array UltraRecursiveVerifier_get_num_gates(); // Constuct the inputs to the final KZG pairing check - auto pairing_points = PCS::compute_pairing_points(shplonk_claim, transcript); + auto pairing_points = KZG::compute_pairing_points(shplonk_claim, transcript); info("KZG: num gates = ", builder->get_num_gates() - prev_num_gates);