From e480a587917ee5688904476eb94dd30ab1618f0d Mon Sep 17 00:00:00 2001 From: codygunton Date: Tue, 10 Sep 2024 21:49:59 +0000 Subject: [PATCH] Fold alphas [no-ci] --- .../utils/batch_mul_native.hpp | 14 +++++++++++ .../protogalaxy/protogalaxy_verifier.cpp | 24 +++++++------------ .../barretenberg/ultra_honk/decider_keys.hpp | 15 ++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/commitment_schemes/utils/batch_mul_native.hpp b/barretenberg/cpp/src/barretenberg/commitment_schemes/utils/batch_mul_native.hpp index 7ae350363e4a..507e2a6d2ba8 100644 --- a/barretenberg/cpp/src/barretenberg/commitment_schemes/utils/batch_mul_native.hpp +++ b/barretenberg/cpp/src/barretenberg/commitment_schemes/utils/batch_mul_native.hpp @@ -34,4 +34,18 @@ static Commitment batch_mul_native(const std::vector& _points, const } return result; } + +/** + * @brief Utility for native batch multiplication of group elements + * @note This is used only for native verification and is not optimized for efficiency + */ +template static FF linear_combination(const std::vector& as, const std::vector& bs) +{ + FF result = as[0] * bs[0]; + for (size_t idx = 1; idx < as.size(); ++idx) { + result += as[idx] * bs[idx]; + } + return result; +} + } // namespace bb diff --git a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp index aefcd02c92d9..0b8ce58db3e8 100644 --- a/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp +++ b/barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_verifier.cpp @@ -121,28 +121,22 @@ std::shared_ptr ProtogalaxyVerifier update_gate_challenges(perturbator_challenge, accumulator->gate_challenges, deltas); // // Fold the commitments - size_t commitment_idx = 0; + idx = 0; for (auto& folded_commitment : next_accumulator->verification_key->get_all()) { - folded_commitment = - batch_mul_native(keys_to_fold.get_precomputed_commitments_at_index(commitment_idx), lagranges); - commitment_idx++; + folded_commitment = batch_mul_native(keys_to_fold.get_precomputed_commitments_at_index(idx), lagranges); + idx++; } - commitment_idx = 0; + idx = 0; for (auto& folded_commitment : next_accumulator->witness_commitments.get_all()) { - folded_commitment = batch_mul_native(keys_to_fold.get_witness_commitments_at_index(commitment_idx), lagranges); - commitment_idx++; + folded_commitment = batch_mul_native(keys_to_fold.get_witness_commitments_at_index(idx), lagranges); + idx++; } // Fold the relation parameters - size_t alpha_idx = 0; + idx = 0; for (auto& alpha : next_accumulator->alphas) { - alpha = FF(0); - size_t vk_idx = 0; - for (auto& key : keys_to_fold) { - alpha += key->alphas[alpha_idx] * lagranges[vk_idx]; - vk_idx++; - } - alpha_idx++; + alpha = linear_combination(keys_to_fold.get_alphas_at_index(idx), lagranges); + idx++; } auto& expected_parameters = next_accumulator->relation_parameters; for (size_t vk_idx = 0; vk_idx < NUM_KEYS; vk_idx++) { diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/decider_keys.hpp b/barretenberg/cpp/src/barretenberg/ultra_honk/decider_keys.hpp index 0736603b0b19..c39ebe74d37d 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/decider_keys.hpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/decider_keys.hpp @@ -86,6 +86,7 @@ template struct DeciderProvingKeys_ { template struct DeciderVerificationKeys_ { static_assert(NUM_ > 1, "Must have at least two decider verification keys."); using Flavor = Flavor_; + using FF = typename Flavor_::FF; using Commitment = typename Flavor_::Commitment; using VerificationKey = typename Flavor::VerificationKey; using DeciderVK = DeciderVerificationKey_; @@ -141,5 +142,19 @@ template struct DeciderVerificationKeys_ { return result; } + + /** + * @brief Get the witness commitments at a given index + * @details This is similar to get_precomputed_commitments_at_index, but for witness commitments. + */ + std::vector get_alphas_at_index(const size_t idx) const + { + std::vector result(NUM); + for (auto [elt, key] : zip_view(result, _data)) { + elt = key->alphas[idx]; + } + + return result; + } }; } // namespace bb