diff --git a/barretenberg/barretenberg.code-workspace b/barretenberg/barretenberg.code-workspace index 0995a1c0c9f..24c8a186612 100644 --- a/barretenberg/barretenberg.code-workspace +++ b/barretenberg/barretenberg.code-workspace @@ -83,7 +83,7 @@ // Clangd. Note that this setting may be overridden by user settings // to the default value "clangd". // - "clangd.path": "clangd-15", + "clangd.path": "clangd-16", // We should disable automatic inclusion of headers unless we decide to follow "WhyIWYU". "clangd.arguments": [ "-header-insertion=never" @@ -156,6 +156,7 @@ "-g" ], "cmake.useCMakePresets": "auto", - "editor.inlayHints.enabled": "offUnlessPressed" + "editor.inlayHints.enabled": "offUnlessPressed", + "git.detectSubmodules": false }, } \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark/relations_bench/relations.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/relations_bench/relations.bench.cpp index 9e68ad32c60..20a99af6a4a 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/relations_bench/relations.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/relations_bench/relations.bench.cpp @@ -6,6 +6,7 @@ #include "barretenberg/proof_system/relations/gen_perm_sort_relation.hpp" #include "barretenberg/proof_system/relations/lookup_relation.hpp" #include "barretenberg/proof_system/relations/permutation_relation.hpp" +#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp" #include diff --git a/barretenberg/cpp/src/barretenberg/honk/flavor/ecc_vm.hpp b/barretenberg/cpp/src/barretenberg/honk/flavor/ecc_vm.hpp index 100f5f4b983..3e966d8c833 100644 --- a/barretenberg/cpp/src/barretenberg/honk/flavor/ecc_vm.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/flavor/ecc_vm.hpp @@ -13,6 +13,7 @@ #include "barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.hpp" #include "barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.hpp" #include "barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.hpp" +#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "barretenberg/proof_system/relations/relation_types.hpp" #include #include @@ -65,19 +66,19 @@ template class ECCVMBa sumcheck::ECCVMLookupRelation>; using LookupRelation = sumcheck::ECCVMLookupRelation; - static constexpr size_t MAX_RELATION_LENGTH = get_max_relation_length(); + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); - // MAX_RANDOM_RELATION_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` random - // polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation length = 3 - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = MAX_RELATION_LENGTH + 1; + // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` + // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation + // length = 3 + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1; static constexpr size_t NUM_RELATIONS = std::tuple_size::value; // Instantiate the BarycentricData needed to extend each Relation Univariate - // static_assert(instantiate_barycentric_utils()); // define the containers for storing the contributions from each relation in Sumcheck using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates()); - using TupleOfArraysOfValues = decltype(create_sumcheck_tuple_of_arrays_of_values()); + using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values()); private: /** @@ -721,7 +722,7 @@ template class ECCVMBa /** * @brief A container for univariates produced during the hot loop in sumcheck. */ - using ExtendedEdges = ProverUnivariates; + using ExtendedEdges = ProverUnivariates; /** * @brief A container for the prover polynomials handles; only stores spans. diff --git a/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_translator.hpp b/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_translator.hpp index bd03cc7236c..f68a3ff54eb 100644 --- a/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_translator.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_translator.hpp @@ -7,11 +7,11 @@ #include "barretenberg/proof_system/arithmetization/arithmetization.hpp" #include "barretenberg/proof_system/circuit_builder/goblin_translator_circuit_builder.hpp" #include "barretenberg/proof_system/flavor/flavor.hpp" -#include "barretenberg/proof_system/relations/decomposition_relation.hpp" -#include "barretenberg/proof_system/relations/extra_relations.hpp" -#include "barretenberg/proof_system/relations/gen_perm_sort_relation.hpp" -#include "barretenberg/proof_system/relations/non_native_field_relation.hpp" -#include "barretenberg/proof_system/relations/permutation_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_decomposition_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_extra_relations.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_gen_perm_sort_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_non_native_field_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_permutation_relation.hpp" #include #include #include @@ -316,16 +316,19 @@ template class GoblinTranslator_ { GoblinTranslatorDecompositionRelation, GoblinTranslatorNonNativeFieldRelation>; - static constexpr size_t MAX_RELATION_LENGTH = get_max_relation_length(); + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); + static constexpr size_t MAX_TOTAL_RELATION_LENGTH = compute_max_total_relation_length(); - // MAX_RANDOM_RELATION_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` random - // polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation length = 3 - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = MAX_RELATION_LENGTH + 1; - static constexpr size_t NUM_RELATIONS = std::tuple_size::value; + // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` + // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation + // length = 3 + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1; + static constexpr size_t BATCHED_RELATION_TOTAL_LENGTH = MAX_TOTAL_RELATION_LENGTH + 1; + static constexpr size_t NUM_RELATIONS = std::tuple_size_v; // define the containers for storing the contributions from each relation in Sumcheck using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates()); - using TupleOfArraysOfValues = decltype(create_sumcheck_tuple_of_arrays_of_values()); + using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values()); private: template @@ -1503,7 +1506,7 @@ template class GoblinTranslator_ { /** * @brief A container for univariates produced during the hot loop in sumcheck. */ - using ExtendedEdges = ProverUnivariates; + using ExtendedEdges = ProverUnivariates; /** * @brief A container for commitment labels. diff --git a/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp b/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp index ccdf63b7915..faf55851306 100644 --- a/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra.hpp @@ -51,18 +51,21 @@ class GoblinUltra { proof_system::AuxiliaryRelation, proof_system::EccOpQueueRelation>; - static constexpr size_t MAX_RELATION_LENGTH = get_max_relation_length(); + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); + static constexpr size_t MAX_TOTAL_RELATION_LENGTH = compute_max_total_relation_length(); - // MAX_RANDOM_RELATION_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` random - // polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation length = 3 - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = MAX_RELATION_LENGTH + 1; - static constexpr size_t NUM_RELATIONS = std::tuple_size::value; + // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` + // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation + // length = 3 + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1; + static constexpr size_t BATCHED_RELATION_TOTAL_LENGTH = MAX_TOTAL_RELATION_LENGTH + 1; + static constexpr size_t NUM_RELATIONS = std::tuple_size_v; template using ProtogalaxyTupleOfTuplesOfUnivariates = decltype(create_protogalaxy_tuple_of_tuples_of_univariates()); using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates()); - using TupleOfArraysOfValues = decltype(create_sumcheck_tuple_of_arrays_of_values()); + using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values()); // Whether or not the first row of the execution trace is reserved for 0s to enable shifts static constexpr bool has_zero_row = true; @@ -330,7 +333,7 @@ class GoblinUltra { /** * @brief A container for univariates produced during the hot loop in sumcheck. */ - using ExtendedEdges = ProverUnivariates; + using ExtendedEdges = ProverUnivariates; /** * @brief A field element for each entity of the flavor. These entities represent the prover polynomials evaluated diff --git a/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra_recursive.hpp b/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra_recursive.hpp index 10f6202552f..e4affda825a 100644 --- a/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra_recursive.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/flavor/goblin_ultra_recursive.hpp @@ -77,16 +77,17 @@ template class GoblinUltraRecursive_ { proof_system::AuxiliaryRelation, proof_system::EccOpQueueRelation>; - static constexpr size_t MAX_RELATION_LENGTH = get_max_relation_length(); + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); - // MAX_RANDOM_RELATION_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` random - // polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation length = 3 - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = MAX_RELATION_LENGTH + 1; + // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` + // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation + // length = 3 + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1; static constexpr size_t NUM_RELATIONS = std::tuple_size::value; // define the container for storing the univariate contribution from each relation in Sumcheck using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates()); - using TupleOfArraysOfValues = decltype(create_sumcheck_tuple_of_arrays_of_values()); + using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values()); private: template diff --git a/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp b/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp index 3330929fa8f..13540b04056 100644 --- a/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/flavor/ultra.hpp @@ -1,12 +1,11 @@ #pragma once #include "barretenberg/ecc/curves/bn254/g1.hpp" #include "barretenberg/honk/pcs/kzg/kzg.hpp" -#include "barretenberg/polynomials/barycentric.hpp" -#include "barretenberg/polynomials/univariate.hpp" - #include "barretenberg/honk/transcript/transcript.hpp" +#include "barretenberg/polynomials/barycentric.hpp" #include "barretenberg/polynomials/evaluation_domain.hpp" #include "barretenberg/polynomials/polynomial.hpp" +#include "barretenberg/polynomials/univariate.hpp" #include "barretenberg/proof_system/circuit_builder/ultra_circuit_builder.hpp" #include "barretenberg/proof_system/flavor/flavor.hpp" #include "barretenberg/proof_system/relations/auxiliary_relation.hpp" @@ -53,18 +52,23 @@ class Ultra { proof_system::EllipticRelation, proof_system::AuxiliaryRelation>; - static constexpr size_t MAX_RELATION_LENGTH = get_max_relation_length(); + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); + static constexpr size_t MAX_TOTAL_RELATION_LENGTH = compute_max_total_relation_length(); + static_assert(MAX_PARTIAL_RELATION_LENGTH == 6); + static_assert(MAX_TOTAL_RELATION_LENGTH == 12); - // MAX_RANDOM_RELATION_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` random - // polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation length = 3 - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = MAX_RELATION_LENGTH + 1; - static constexpr size_t NUM_RELATIONS = std::tuple_size::value; + // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` + // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation + // length = 3 + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1; + static constexpr size_t BATCHED_RELATION_TOTAL_LENGTH = MAX_TOTAL_RELATION_LENGTH + 1; + static constexpr size_t NUM_RELATIONS = std::tuple_size_v; template using ProtogalaxyTupleOfTuplesOfUnivariates = decltype(create_protogalaxy_tuple_of_tuples_of_univariates()); using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates()); - using TupleOfArraysOfValues = decltype(create_sumcheck_tuple_of_arrays_of_values()); + using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values()); // Whether or not the first row of the execution trace is reserved for 0s to enable shifts static constexpr bool has_zero_row = true; @@ -327,7 +331,7 @@ class Ultra { /** * @brief A container for univariates produced during the hot loop in sumcheck. */ - using ExtendedEdges = ProverUnivariates; + using ExtendedEdges = ProverUnivariates; /** * @brief A container for commitment labels. diff --git a/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp b/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp index 1343254525d..316dff3c1ba 100644 --- a/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/flavor/ultra_recursive.hpp @@ -76,16 +76,17 @@ template class UltraRecursive_ { proof_system::EllipticRelation, proof_system::AuxiliaryRelation>; - static constexpr size_t MAX_RELATION_LENGTH = get_max_relation_length(); + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = compute_max_partial_relation_length(); - // MAX_RANDOM_RELATION_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` random - // polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation length = 3 - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = MAX_RELATION_LENGTH + 1; + // BATCHED_RELATION_PARTIAL_LENGTH = algebraic degree of sumcheck relation *after* multiplying by the `pow_zeta` + // random polynomial e.g. For \sum(x) [A(x) * B(x) + C(x)] * PowZeta(X), relation length = 2 and random relation + // length = 3 + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = MAX_PARTIAL_RELATION_LENGTH + 1; static constexpr size_t NUM_RELATIONS = std::tuple_size::value; // define the container for storing the univariate contribution from each relation in Sumcheck using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates()); - using TupleOfArraysOfValues = decltype(create_sumcheck_tuple_of_arrays_of_values()); + using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values()); private: template diff --git a/barretenberg/cpp/src/barretenberg/honk/instance/instances.hpp b/barretenberg/cpp/src/barretenberg/honk/instance/instances.hpp index 280920b4796..02219aa0e73 100644 --- a/barretenberg/cpp/src/barretenberg/honk/instance/instances.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/instance/instances.hpp @@ -4,14 +4,20 @@ namespace proof_system::honk { template struct ProverInstances_ { + public: using Flavor = Flavor_; using FF = typename Flavor::FF; + static constexpr size_t NUM = NUM_; using Instance = ProverInstance_; + using ArrayType = std::array, NUM_>; + // The extended length here is the length of a composition of polynomials. + static constexpr size_t EXTENDED_LENGTH = (Flavor::MAX_TOTAL_RELATION_LENGTH - 1) * (NUM - 1) + 1; + using RelationParameters = proof_system::RelationParameters>; - public: - static constexpr size_t NUM = NUM_; ArrayType _data; + RelationParameters relation_parameters; + std::shared_ptr const& operator[](size_t idx) const { return _data[idx]; } typename ArrayType::iterator begin() { return _data.begin(); }; typename ArrayType::iterator end() { return _data.end(); }; diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner.test.cpp b/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner.test.cpp index 328045b335f..f469ec8863f 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner.test.cpp +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner.test.cpp @@ -34,7 +34,6 @@ TEST(Protogalaxy, CombinerOn2Instances) if (is_random_input) { std::vector> instance_data(NUM_INSTANCES); std::array, NUM_INSTANCES> storage_arrays; - auto relation_parameters = RelationParameters::get_random(); ProtoGalaxyProver prover; auto pow_univariate = PowUnivariate(/*zeta_pow=*/2); auto alpha = FF(0); // focus on the arithmetic relation only @@ -51,21 +50,25 @@ TEST(Protogalaxy, CombinerOn2Instances) ProverInstances instances{ instance_data }; - auto result = prover.compute_combiner(instances, relation_parameters, pow_univariate, alpha); + auto result = prover.compute_combiner(instances, pow_univariate, alpha); auto expected_result = - barretenberg::Univariate(std::array{ 87706, - 27289140, - 229355214, - 905031784, - static_cast(2504059650), - static_cast(5627174556), - static_cast(11026107190) }); - + barretenberg::Univariate(std::array{ 87706, + 27289140, + 229355214, + 905031784, + static_cast(2504059650), + static_cast(5627174556), + static_cast(11026107190), + static_cast(19603583184), + static_cast(32413323114), + static_cast(50660042500), + static_cast(75699451806), + static_cast(109038256440), + static_cast(152334156754) }); EXPECT_EQ(result, expected_result); } else { std::vector> instance_data(NUM_INSTANCES); std::array, NUM_INSTANCES> storage_arrays; - auto relation_parameters = RelationParameters::get_random(); ProtoGalaxyProver prover; auto pow_univariate = PowUnivariate(/*zeta_pow=*/2); auto alpha = FF(0); // focus on the arithmetic relation only @@ -126,8 +129,9 @@ TEST(Protogalaxy, CombinerOn2Instances) relation value: 0 0 0 0 0 0 0 0 0 6 18 36 60 90 */ - auto result = prover.compute_combiner(instances, relation_parameters, pow_univariate, alpha); - auto expected_result = barretenberg::Univariate(std::array{ 0, 0, 36, 144, 360, 720, 1260 }); + auto result = prover.compute_combiner(instances, pow_univariate, alpha); + auto expected_result = barretenberg::Univariate( + std::array{ 0, 0, 36, 144, 360, 720, 1260, 2016, 3024, 4320, 5940, 7920, 10296 }); EXPECT_EQ(result, expected_result); } }; @@ -156,7 +160,6 @@ TEST(Protogalaxy, CombinerOn4Instances) auto run_test = [&]() { std::vector> instance_data(NUM_INSTANCES); std::array, NUM_INSTANCES> storage_arrays; - auto relation_parameters = RelationParameters::get_random(); ProtoGalaxyProver prover; auto pow_univariate = PowUnivariate(/*zeta_pow=*/2); auto alpha = FF(0); // focus on the arithmetic relation only @@ -177,10 +180,10 @@ TEST(Protogalaxy, CombinerOn4Instances) zero_all_selectors(instances[2]->prover_polynomials); zero_all_selectors(instances[3]->prover_polynomials); - auto result = prover.compute_combiner(instances, relation_parameters, pow_univariate, alpha); - std::array zeroes; + auto result = prover.compute_combiner(instances, pow_univariate, alpha); + std::array zeroes; std::fill(zeroes.begin(), zeroes.end(), 0); - auto expected_result = barretenberg::Univariate(zeroes); + auto expected_result = barretenberg::Univariate(zeroes); EXPECT_EQ(result, expected_result); }; run_test(); diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner_example_gen.py b/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner_example_gen.py index 2c37304ae97..e222d8e9033 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner_example_gen.py +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/combiner_example_gen.py @@ -1,9 +1,9 @@ -# WORKTODO: delete this +# TODO(https://github.com/AztecProtocol/barretenberg/issues/760): Delete this? import numpy as np # np.set_printoptions(formatter={'int': hex}) -EXTENDED_RELATION_LENGTH = 7 +EXTENDED_RELATION_LENGTH = 13 class Row: def __init__(self, start): @@ -110,25 +110,26 @@ def compute_first_example(): def compute_second_example(): result = 0 - w_l = np.array([ 1, 3, 5, 7, 9, 11, 13]) - w_r = np.array([ 2, 4, 6, 8, 10, 12, 14]) - w_o = np.array([ 3, 7, 11, 15, 19, 23, 27]) - q_m = np.array([ 0, 0, 0, 0, 0, 0, 0]) - q_l = np.array([ 1, 1, 1, 1, 1, 1, 1]) - q_r = np.array([ 1, 1, 1, 1, 1, 1, 1]) - q_o = np.array([-1, -1, -1, -1, -1, -1, -1]) - q_c = np.array([ 0, 0, 0, 0, 0, 0, 0]) + # 0 1 2 3 4 5 6 7 8 9 10 11 12 + w_l = np.array([ 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]) + w_r = np.array([ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26]) + w_o = np.array([ 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51]) + q_m = np.array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) + q_l = np.array([ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) + q_r = np.array([ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]) + q_o = np.array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]) + q_c = np.array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) # contribution is zero, but why not? result += rel(w_l, w_r, w_o, q_m, q_l, q_r, q_o, q_c) - w_l = np.array([ 0, 1, 2, 3, 4, 5, 6]) - w_r = np.array([ 4, 4, 4, 4, 4, 4, 4]) - w_o = np.array([ 4, 4, 4, 4, 4, 4, 4]) - q_m = np.array([ 0, 1, 2, 3, 4, 5, 6]) - q_l = np.array([ 1, 0, -1, -2, -3, -4, -5]) - q_r = np.array([ 1, 0, -1, -2, -3, -4, -5]) - q_o = np.array([-1, -1, -1, -1, -1, -1, -1]) - q_c = np.array([ 0, 0, 0, 0, 0, 0, 0]) + w_l = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) + w_r = np.array([ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]) + w_o = np.array([ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]) + q_m = np.array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) + q_l = np.array([ 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9,-10,-11]) + q_r = np.array([ 1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9,-10,-11]) + q_o = np.array([-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]) + q_c = np.array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) result += rel(w_l, w_r, w_o, q_m, q_l, q_r, q_o, q_c) result *= 2 diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy.test.cpp b/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy.test.cpp index 8c35ce4d40b..5115e0a8192 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy.test.cpp +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy.test.cpp @@ -1,7 +1,16 @@ // Note these tests are a bit hacky + +// #include "instances.hpp" +// #include "barretenberg/ecc/curves/bn254/bn254.hpp" +// #include "barretenberg/honk/proof_system/grand_product_library.hpp" +// #include "barretenberg/polynomials/polynomial.hpp" +// #include "barretenberg/srs/factories/file_crs_factory.hpp" +// #include #include "barretenberg/honk/composer/ultra_composer.hpp" #include "protogalaxy_prover.hpp" #include + +using namespace barretenberg; using namespace proof_system::honk; using Flavor = flavor::Ultra; @@ -178,4 +187,48 @@ TEST_F(ProtoGalaxyTests, PowPerturbatorPolynomial) // Ensure the constant coefficient of the perturbator is equal to the target sum as indicated by the paper EXPECT_EQ(perturbator[0], target_sum); } + +TEST_F(ProtoGalaxyTests, FoldChallenges) +{ + using Instances = ProverInstances_; + using Instance = typename Instances::Instance; + + Builder builder1; + auto instance1 = std::make_shared(builder1); + instance1->relation_parameters.eta = 1; + + Builder builder2; + builder2.add_variable(3); + auto instance2 = std::make_shared(builder2); + instance2->relation_parameters.eta = 3; + + Instances instances{ { instance1, instance2 } }; + ProtoGalaxyProver::fold_parameters(instances); + + Univariate expected_eta{ { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23 } }; + EXPECT_EQ(instances.relation_parameters.eta, expected_eta); +} + +// namespace proof_system::honk::instance_tests { + +// template class InstancesTests : public testing::Test { +// using FF = typename Flavor::FF; +// using Builder = typename Flavor::CircuitBuilder; + +// public: +// static void test_parameters_to_univariates() +// { + +// }; +// }; + +// using FlavorTypes = testing::Types; +// TYPED_TEST_SUITE(InstancesTests, FlavorTypes); + +// TYPED_TEST(InstancesTests, ParametersToUnivariates) +// { +// TestFixture::test_parameters_to_univariates(); +// } + +// } // namespace proof_system::honk::instance_tests } // namespace protogalaxy_tests \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.cpp b/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.cpp index 913b4868d73..ef64c21c924 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.cpp @@ -31,6 +31,8 @@ template void ProtoGalaxyProver_::prepa instance->compute_sorted_accumulator_polynomials(eta); instance->compute_grand_product_polynomials(beta, gamma); } + + fold_parameters(instances); } // TODO(#https://github.com/AztecProtocol/barretenberg/issues/689): finalise implementation this function diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.hpp b/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.hpp index a5ae4cd5506..c4a87be54b7 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/protogalaxy_prover.hpp @@ -22,11 +22,15 @@ template class ProtoGalaxyProver_ { using RowEvaluations = typename Flavor::AllValues; using ProverPolynomials = typename Flavor::ProverPolynomials; using Relations = typename Flavor::Relations; + using BaseUnivariate = Univariate; - using ExtendedUnivariate = Univariate; - using RandomExtendedUnivariate = - Univariate; + // The length of ExtendedUnivariate is the largest length (==degree + 1) of a univariate polynomial obtained by + // composing a relation with folded instance + challenge data. + using ExtendedUnivariate = Univariate; + using ExtendedUnivariateWithRandomization = + Univariate; using ExtendedUnivariates = typename Flavor::template ProverUnivariates; + using TupleOfTuplesOfUnivariates = typename Flavor::template ProtogalaxyTupleOfTuplesOfUnivariates; using RelationEvaluations = typename Flavor::TupleOfArraysOfValues; @@ -192,10 +196,10 @@ template class ProtoGalaxyProver_ { } } - template + template void accumulate_relation_univariates(TupleOfTuplesOfUnivariates& univariate_accumulators, const ExtendedUnivariates& extended_univariates, - const proof_system::RelationParameters& relation_parameters, + const Parameters& relation_parameters, const FF& scaling_factor) { using Relation = std::tuple_element_t; @@ -204,7 +208,7 @@ template class ProtoGalaxyProver_ { // Repeat for the next relation. if constexpr (relation_idx + 1 < Flavor::NUM_RELATIONS) { - accumulate_relation_univariates( + accumulate_relation_univariates( univariate_accumulators, extended_univariates, relation_parameters, scaling_factor); } } @@ -214,11 +218,9 @@ template class ProtoGalaxyProver_ { * * @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/754) Provide the right challenge to here */ - RandomExtendedUnivariate compute_combiner( - const ProverInstances& instances, - const proof_system::RelationParameters& relation_parameters, - const PowUnivariate& pow_univariate, - const typename Flavor::FF alpha) + ExtendedUnivariateWithRandomization compute_combiner(const ProverInstances& instances, + const PowUnivariate& pow_univariate, + const typename Flavor::FF alpha) { size_t common_circuit_size = instances[0]->prover_polynomials._data[0].size(); // Precompute the vector of required powers of zeta @@ -261,11 +263,13 @@ template class ProtoGalaxyProver_ { FF pow_challenge = pow_challenges[idx]; - // Accumulate the i-th row's univariate contribution - accumulate_relation_univariates(thread_univariate_accumulators[thread_idx], - extended_univariates[thread_idx], - relation_parameters, - pow_challenge); + // Accumulate the i-th row's univariate contribution. Note that the relation parameters passed to this + // function have already been folded + accumulate_relation_univariates( + thread_univariate_accumulators[thread_idx], + extended_univariates[thread_idx], + instances.relation_parameters, // these parameters have already been folded + pow_challenge); } }); @@ -274,9 +278,32 @@ template class ProtoGalaxyProver_ { Utils::add_nested_tuples(univariate_accumulators, accumulators); } // Batch the univariate contributions from each sub-relation to obtain the round univariate - return Utils::template batch_over_relations( + return Utils::template batch_over_relations( univariate_accumulators, alpha, pow_univariate); } + + /** + * @brief Create folded (univariate) relation parameters. + * @details For a given relation parameter type, extract that parameter from each instance, place the values in a + * univariate (i.e., sum them against an appropriate univariate Lagrange basis) and then extended as needed during + * the constuction of the combiner. + */ + static void fold_parameters(ProverInstances& instances) + { + // array of parameters to be computed + auto& folded_parameters = instances.relation_parameters.to_fold; + size_t param_idx = 0; + for (auto& folded_parameter : folded_parameters) { + Univariate tmp(0); + size_t instance_idx = 0; + for (auto& instance : instances) { + tmp.value_at(instance_idx) = instance->relation_parameters.to_fold[param_idx]; + instance_idx++; + } + folded_parameter.get() = tmp.template extend_to(); + param_idx++; + } + } }; extern template class ProtoGalaxyProver_>; diff --git a/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp b/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp index fbfc5d5fc11..224fa64ab20 100644 --- a/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp +++ b/barretenberg/cpp/src/barretenberg/honk/proof_system/ultra_prover.cpp @@ -107,7 +107,7 @@ template void UltraProver_::execute_relation_check_ auto sumcheck = Sumcheck(instance->proving_key->circuit_size, transcript); - sumcheck_output = sumcheck.prove(instance->prover_polynomials, instance->relation_parameters); + sumcheck_output = sumcheck.prove(instance); } /** diff --git a/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.hpp b/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.hpp index 81af019be73..033401f6361 100644 --- a/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck.hpp @@ -1,6 +1,7 @@ #pragma once #include "barretenberg/common/serialize.hpp" #include "barretenberg/common/throw_or_abort.hpp" +#include "barretenberg/honk/instance/prover_instance.hpp" #include "barretenberg/honk/sumcheck/sumcheck_output.hpp" #include "barretenberg/honk/transcript/transcript.hpp" #include "barretenberg/honk/utils/grand_product_delta.hpp" @@ -17,6 +18,7 @@ template class SumcheckProver { using ProverPolynomials = typename Flavor::ProverPolynomials; using PartiallyEvaluatedMultivariates = typename Flavor::PartiallyEvaluatedMultivariates; using ClaimedEvaluations = typename Flavor::AllValues; + using Instance = ProverInstance_; ProverTranscript& transcript; const size_t multivariate_n; @@ -119,6 +121,15 @@ template class SumcheckProver { return { multivariate_challenge, multivariate_evaluations }; }; + /** + * @brief Compute univariate restriction place in transcript, generate challenge, partially evaluate,... repeat + * until final round, then compute multivariate evaluations and place in transcript. + */ + SumcheckOutput prove(std::shared_ptr instance) + { + return prove(instance->prover_polynomials, instance->relation_parameters); + }; + /** * @brief Evaluate at the round challenge and prepare class for next round. * Illustration of layout in example of first round when d==3 (showing just one Honk polynomial, @@ -155,7 +166,7 @@ template class SumcheckVerifier { using FF = typename Flavor::FF; using ClaimedEvaluations = typename Flavor::AllValues; - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = Flavor::MAX_RANDOM_RELATION_LENGTH; + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = Flavor::BATCHED_RELATION_PARTIAL_LENGTH; static constexpr size_t NUM_POLYNOMIALS = Flavor::NUM_ALL_ENTITIES; const size_t multivariate_d; @@ -196,7 +207,7 @@ template class SumcheckVerifier { // Obtain the round univariate from the transcript std::string round_univariate_label = "Sumcheck:univariate_" + std::to_string(round_idx); auto round_univariate = - transcript.template receive_from_prover>( + transcript.template receive_from_prover>( round_univariate_label); bool checked = round.check_sum(round_univariate); diff --git a/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck_round.hpp b/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck_round.hpp index c03d0968129..e048c48fcbc 100644 --- a/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck_round.hpp +++ b/barretenberg/cpp/src/barretenberg/honk/sumcheck/sumcheck_round.hpp @@ -65,8 +65,8 @@ template class SumcheckProverRound { size_t round_size; // a power of 2 static constexpr size_t NUM_RELATIONS = Flavor::NUM_RELATIONS; - static constexpr size_t MAX_RELATION_LENGTH = Flavor::MAX_RELATION_LENGTH; - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = Flavor::MAX_RANDOM_RELATION_LENGTH; + static constexpr size_t MAX_PARTIAL_RELATION_LENGTH = Flavor::MAX_PARTIAL_RELATION_LENGTH; + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = Flavor::BATCHED_RELATION_PARTIAL_LENGTH; SumcheckTupleOfTuplesOfUnivariates univariate_accumulators; @@ -93,7 +93,7 @@ template class SumcheckProverRound { size_t univariate_idx = 0; // TODO(https://github.com/AztecProtocol/barretenberg/issues/391) zip for (auto& poly : multivariates) { auto edge = barretenberg::Univariate({ poly[edge_idx], poly[edge_idx + 1] }); - extended_edges[univariate_idx] = edge.template extend_to(); + extended_edges[univariate_idx] = edge.template extend_to(); ++univariate_idx; } } @@ -104,7 +104,7 @@ template class SumcheckProverRound { * univariate accumulators to be zero. */ template - barretenberg::Univariate compute_univariate( + barretenberg::Univariate compute_univariate( ProverPolynomialsOrPartiallyEvaluatedMultivariates& polynomials, const proof_system::RelationParameters& relation_parameters, const barretenberg::PowUnivariate& pow_univariate, @@ -153,10 +153,10 @@ template class SumcheckProverRound { // Compute the i-th edge's univariate contribution, // scale it by the pow polynomial's constant and zeta power "c_l ⋅ ζ_{l+1}ⁱ" // and add it to the accumulators for Sˡ(Xₗ) - accumulate_relation_univariates<>(thread_univariate_accumulators[thread_idx], - extended_edges[thread_idx], - relation_parameters, - pow_challenge); + accumulate_relation_univariates(thread_univariate_accumulators[thread_idx], + extended_edges[thread_idx], + relation_parameters, + pow_challenge); } }); @@ -165,7 +165,7 @@ template class SumcheckProverRound { Utils::add_nested_tuples(univariate_accumulators, accumulators); } // Batch the univariate contributions from each sub-relation to obtain the round univariate - return Utils::template batch_over_relations>( + return Utils::template batch_over_relations>( univariate_accumulators, alpha, pow_univariate); } @@ -215,7 +215,7 @@ template class SumcheckVerifierRound { bool round_failed = false; static constexpr size_t NUM_RELATIONS = Flavor::NUM_RELATIONS; - static constexpr size_t MAX_RANDOM_RELATION_LENGTH = Flavor::MAX_RANDOM_RELATION_LENGTH; + static constexpr size_t BATCHED_RELATION_PARTIAL_LENGTH = Flavor::BATCHED_RELATION_PARTIAL_LENGTH; FF target_total_sum = 0; @@ -224,7 +224,7 @@ template class SumcheckVerifierRound { // Verifier constructor explicit SumcheckVerifierRound() { Utils::zero_elements(relation_evaluations); }; - bool check_sum(barretenberg::Univariate& univariate) + bool check_sum(barretenberg::Univariate& univariate) { // S^{l}(0) = ( (1−0) + 0⋅ζ^{ 2^l } ) ⋅ T^{l}(0) = T^{l}(0) // S^{l}(1) = ( (1−1) + 1⋅ζ^{ 2^l } ) ⋅ T^{l}(1) = ζ^{ 2^l } ⋅ T^{l}(1) @@ -252,7 +252,7 @@ template class SumcheckVerifierRound { * @param round_challenge u_l * @return FF sigma_{l+1} = S^l(u_l) */ - FF compute_next_target_sum(barretenberg::Univariate& univariate, + FF compute_next_target_sum(barretenberg::Univariate& univariate, FF& round_challenge) { // Evaluate T^{l}(u_{l}) diff --git a/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp b/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp index 3a0271bdd8d..246e11e92e1 100644 --- a/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp +++ b/barretenberg/cpp/src/barretenberg/honk/transcript/transcript.test.cpp @@ -32,10 +32,10 @@ class UltraTranscriptTests : public ::testing::Test { auto log_n = numeric::get_msb(circuit_size); - size_t max_relation_length = Flavor::MAX_RANDOM_RELATION_LENGTH; + size_t MAX_PARTIAL_RELATION_LENGTH = Flavor::BATCHED_RELATION_PARTIAL_LENGTH; size_t size_FF = sizeof(FF); size_t size_G = 2 * size_FF; - size_t size_uni = max_relation_length * size_FF; + size_t size_uni = MAX_PARTIAL_RELATION_LENGTH * size_FF; size_t size_evals = (Flavor::NUM_ALL_ENTITIES)*size_FF; size_t size_uint32 = 4; size_t size_uint64 = 8; diff --git a/barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp b/barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp index ca3701f9b9f..6cd154ba0ed 100644 --- a/barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp +++ b/barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp @@ -78,6 +78,8 @@ template class Univariate { return output; }; + static Univariate random_element() { return get_random(); }; + // Operations between Univariate and other Univariate bool operator==(const Univariate& other) const = default; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/flavor/flavor.hpp b/barretenberg/cpp/src/barretenberg/proof_system/flavor/flavor.hpp index 9db88b2c41f..7b8bc126206 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/flavor/flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/flavor/flavor.hpp @@ -202,16 +202,35 @@ class AllEntities_ : public Entities_ { }; /** - * @brief Recursive utility function to find max RELATION_LENGTH over tuple of Relations + * @brief Recursive utility function to find max PARTIAL_RELATION_LENGTH tuples of Relations. + * @details The "partial length" of a relation is 1 + the degree of the relation, where any challenges used in the + * relation are as constants, not as variables.. * */ -template static constexpr size_t get_max_relation_length() +template static constexpr size_t compute_max_partial_relation_length() { if constexpr (Index >= std::tuple_size::value) { return 0; // Return 0 when reach end of the tuple } else { constexpr size_t current_length = std::tuple_element::type::RELATION_LENGTH; - constexpr size_t next_length = get_max_relation_length(); + constexpr size_t next_length = compute_max_partial_relation_length(); + return (current_length > next_length) ? current_length : next_length; + } +} + +/** + * @brief Recursive utility function to find max TOTAL_RELATION_LENGTH among tuples of Relations. + * @details The "total length" of a relation is 1 + the degree of the relation, where any challenges used in the + * relation are regarded as variables. + * + */ +template static constexpr size_t compute_max_total_relation_length() +{ + if constexpr (Index >= std::tuple_size::value) { + return 0; // Return 0 when reach end of the tuple + } else { + constexpr size_t current_length = std::tuple_element::type::TOTAL_RELATION_LENGTH; + constexpr size_t next_length = compute_max_total_relation_length(); return (current_length > next_length) ? current_length : next_length; } } @@ -258,13 +277,13 @@ template static constexpr auto create_su * @details Container for storing value of each identity in each relation. Each Relation contributes an array of * length num-identities. */ -template static constexpr auto create_sumcheck_tuple_of_arrays_of_values() +template static constexpr auto create_tuple_of_arrays_of_values() { if constexpr (Index >= std::tuple_size::value) { return std::tuple<>{}; // Return empty when reach end of the tuple } else { using Values = typename std::tuple_element_t::SumcheckArrayOfValuesOverSubrelations; - return std::tuple_cat(std::tuple{}, create_sumcheck_tuple_of_arrays_of_values()); + return std::tuple_cat(std::tuple{}, create_tuple_of_arrays_of_values()); } } diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/auxiliary_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/auxiliary_relation.hpp index a9b66cfa450..1974006c6fe 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/auxiliary_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/auxiliary_relation.hpp @@ -1,15 +1,46 @@ #pragma once #include "barretenberg/numeric/uint256/uint256.hpp" -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { template class AuxiliaryRelationImpl { public: using FF = FF_; + /* + * TODO(https://github.com/AztecProtocol/barretenberg/issues/757): Investigate optimizations. + * It seems that we could have: + * static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ + * 5 // auxiliary sub-relation; + * 6 // ROM consistency sub-relation 1 + * 6 // ROM consistency sub-relation 2 + * 6 // RAM consistency sub-relation 1 + * 5 // RAM consistency sub-relation 2 + * 5 // RAM consistency sub-relation 3 + * }; + * + * and + * + * static constexpr std::array TOTAL_LENGTH_ADJUSTMENTS{ + * 6, // auxiliary sub-relation + * 0, // ROM consistency sub-relation 1 + * 0, // ROM consistency sub-relation 2 + * 3, // RAM consistency sub-relation 1 + * 0, // RAM consistency sub-relation 2 + * 1 // RAM consistency sub-relation 3 + * }; + */ + + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ + 6, // auxiliary sub-relation; + 6, // ROM consistency sub-relation 1 + 6, // ROM consistency sub-relation 2 + 6, // RAM consistency sub-relation 1 + 6, // RAM consistency sub-relation 2 + 6 // RAM consistency sub-relation 3 + }; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array TOTAL_LENGTH_ADJUSTMENTS{ 6, // auxiliary sub-relation 6, // ROM consistency sub-relation 1 6, // ROM consistency sub-relation 2 @@ -48,21 +79,23 @@ template class AuxiliaryRelationImpl { * N.B.2 The q_c selector is used to store circuit-specific values in the RAM/ROM access gate * * @param evals transformed to `evals + C(in(X)...)*scaling_factor` - * @param in an std::array containing the fully extended Univariate edges. + * @param in an std::array containing the Totaly extended Univariate edges. * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template + template inline static void accumulate(ContainerOverSubrelations& accumulators, const AllEntities& in, - const RelationParameters& relation_parameters, + const Parameters& params, const FF& scaling_factor) { - const auto& eta = relation_parameters.eta; // All subrelations have the same length so we use the same length view for all calculations using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; + using ParameterView = GetParameterView; + + const auto& eta = ParameterView(params.eta); auto w_1 = View(in.w_l); auto w_2 = View(in.w_r); @@ -87,6 +120,7 @@ template class AuxiliaryRelationImpl { /** * Non native field arithmetic gate 2 + * deg 4 * * _ _ * / _ _ _ 14 \ @@ -115,6 +149,8 @@ template class AuxiliaryRelationImpl { auto non_native_field_identity = non_native_field_gate_1 + non_native_field_gate_2 + non_native_field_gate_3; non_native_field_identity *= q_2; + // ((((w2' * 2^14 + w1') * 2^14 + w3) * 2^14 + w2) * 2^14 + w1 - w4) * qm + // deg 2 auto limb_accumulator_1 = w_2_shift * SUBLIMB_SHIFT; limb_accumulator_1 += w_1_shift; limb_accumulator_1 *= SUBLIMB_SHIFT; @@ -126,6 +162,8 @@ template class AuxiliaryRelationImpl { limb_accumulator_1 -= w_4; limb_accumulator_1 *= q_4; + // ((((w3' * 2^14 + w2') * 2^14 + w1') * 2^14 + w4) * 2^14 + w3 - w4') * qm + // deg 2 auto limb_accumulator_2 = w_3_shift * SUBLIMB_SHIFT; limb_accumulator_2 += w_2_shift; limb_accumulator_2 *= SUBLIMB_SHIFT; @@ -138,7 +176,7 @@ template class AuxiliaryRelationImpl { limb_accumulator_2 *= q_m; auto limb_accumulator_identity = limb_accumulator_1 + limb_accumulator_2; - limb_accumulator_identity *= q_3; + limb_accumulator_identity *= q_3; // deg 3 /** * MEMORY @@ -171,6 +209,8 @@ template class AuxiliaryRelationImpl { /** * Memory Record Check + * Partial degree: 1 + * Total degree: 4 * * A ROM/ROM access gate can be evaluated with the identity: * @@ -184,11 +224,13 @@ template class AuxiliaryRelationImpl { memory_record_check += w_1; memory_record_check *= eta; memory_record_check += q_c; - auto partial_record_check = memory_record_check; // used in RAM consistency check + auto partial_record_check = memory_record_check; // used in RAM consistency check; deg 1 or 4 memory_record_check = memory_record_check - w_4; /** * ROM Consistency Check + * Partial degree: 1 + * Total degree: 4 * * For every ROM read, a set equivalence check is applied between the record witnesses, and a second set of * records that are sorted. @@ -203,14 +245,15 @@ template class AuxiliaryRelationImpl { auto index_delta = w_1_shift - w_1; auto record_delta = w_4_shift - w_4; - auto index_is_monotonically_increasing = index_delta * index_delta - index_delta; + auto index_is_monotonically_increasing = index_delta * index_delta - index_delta; // deg 2 - auto adjacent_values_match_if_adjacent_indices_match = (index_delta * FF(-1) + FF(1)) * record_delta; + auto adjacent_values_match_if_adjacent_indices_match = (index_delta * FF(-1) + FF(1)) * record_delta; // deg 2 std::get<1>(accumulators) += - adjacent_values_match_if_adjacent_indices_match * (q_1 * q_2) * (q_aux * scaling_factor); - std::get<2>(accumulators) += index_is_monotonically_increasing * (q_1 * q_2) * (q_aux * scaling_factor); - auto ROM_consistency_check_identity = memory_record_check * (q_1 * q_2); + adjacent_values_match_if_adjacent_indices_match * (q_1 * q_2) * (q_aux * scaling_factor); // deg 5 + std::get<2>(accumulators) += + index_is_monotonically_increasing * (q_1 * q_2) * (q_aux * scaling_factor); // deg 5 + auto ROM_consistency_check_identity = memory_record_check * (q_1 * q_2); // deg 3 or 7 /** * RAM Consistency Check @@ -230,10 +273,12 @@ template class AuxiliaryRelationImpl { * N.B. it is the responsibility of the circuit writer to ensure that every RAM cell is initialized * with a WRITE operation. */ - auto access_type = (w_4 - partial_record_check); // will be 0 or 1 for honest Prover - auto access_check = access_type * access_type - access_type; // check value is 0 or 1 + auto access_type = (w_4 - partial_record_check); // will be 0 or 1 for honest Prover; deg 1 or 4 + auto access_check = access_type * access_type - access_type; // check value is 0 or 1; deg 2 or 8 - // TODO: oof nasty compute here. If we sorted in reverse order we could re-use `partial_record_check` + // TODO(https://github.com/AztecProtocol/barretenberg/issues/757): If we sorted in + // reverse order we could re-use `partial_record_check` 1 - ((w3' * eta + w2') * eta + w1') * eta + // deg 1 or 4 auto next_gate_access_type = w_3_shift * eta; next_gate_access_type += w_2_shift; next_gate_access_type *= eta; @@ -243,22 +288,23 @@ template class AuxiliaryRelationImpl { auto value_delta = w_3_shift - w_3; auto adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation = - (index_delta * FF(-1) + FF(1)) * value_delta * (next_gate_access_type * FF(-1) + FF(1)); + (index_delta * FF(-1) + FF(1)) * value_delta * (next_gate_access_type * FF(-1) + FF(1)); // deg 3 or 6 // We can't apply the RAM consistency check identity on the final entry in the sorted list (the wires in the - // next gate would make the identity fail). - // We need to validate that its 'access type' bool is correct. Can't do - // with an arithmetic gate because of the `eta` factors. We need to check that the *next* gate's access type is - // correct, to cover this edge case + // next gate would make the identity fail). We need to validate that its 'access type' bool is correct. Can't + // do with an arithmetic gate because of the `eta` factors. We need to check that the *next* gate's access + // type is correct, to cover this edge case + // deg 2 or 4 auto next_gate_access_type_is_boolean = next_gate_access_type * next_gate_access_type - next_gate_access_type; // Putting it all together... std::get<3>(accumulators) += adjacent_values_match_if_adjacent_indices_match_and_next_access_is_a_read_operation * (q_arith) * - (q_aux * scaling_factor); - std::get<4>(accumulators) += index_is_monotonically_increasing * (q_arith) * (q_aux * scaling_factor); - std::get<5>(accumulators) += next_gate_access_type_is_boolean * (q_arith) * (q_aux * scaling_factor); - auto RAM_consistency_check_identity = access_check * (q_arith); + (q_aux * scaling_factor); // deg 5 or 8 + std::get<4>(accumulators) += index_is_monotonically_increasing * (q_arith) * (q_aux * scaling_factor); // deg 4 + std::get<5>(accumulators) += + next_gate_access_type_is_boolean * (q_arith) * (q_aux * scaling_factor); // deg 4 or 6 + auto RAM_consistency_check_identity = access_check * (q_arith); // deg 3 or 9 /** * RAM Timestamp Consistency Check @@ -272,19 +318,20 @@ template class AuxiliaryRelationImpl { * Else timestamp_check = 0 */ auto timestamp_delta = w_2_shift - w_2; - auto RAM_timestamp_check_identity = (index_delta * FF(-1) + FF(1)) * timestamp_delta - w_3; + auto RAM_timestamp_check_identity = (index_delta * FF(-1) + FF(1)) * timestamp_delta - w_3; // deg 3 /** * The complete RAM/ROM memory identity - * + * Partial degree: */ - auto memory_identity = ROM_consistency_check_identity; - memory_identity += RAM_timestamp_check_identity * (q_4 * q_1); - memory_identity += memory_record_check * (q_m * q_1); - memory_identity += RAM_consistency_check_identity; + auto memory_identity = ROM_consistency_check_identity; // deg 3 or 6 + memory_identity += RAM_timestamp_check_identity * (q_4 * q_1); // deg 4 + memory_identity += memory_record_check * (q_m * q_1); // deg 3 or 6 + memory_identity += RAM_consistency_check_identity; // deg 3 or 9 + // (deg 3 or 9) + (deg 4) + (deg 3) auto auxiliary_identity = memory_identity + non_native_field_identity + limb_accumulator_identity; - auxiliary_identity *= (q_aux * scaling_factor); + auxiliary_identity *= (q_aux * scaling_factor); // deg 4 or 10 std::get<0>(accumulators) += auxiliary_identity; }; }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_op_queue_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_op_queue_relation.hpp index 6c65b045f79..ac6af95edaf 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_op_queue_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_op_queue_relation.hpp @@ -1,6 +1,5 @@ #pragma once -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -8,7 +7,7 @@ template class EccOpQueueRelationImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 3, // wire - op-queue-wire consistency sub-relation 1 3, // wire - op-queue-wire consistency sub-relation 2 3, // wire - op-queue-wire consistency sub-relation 3 @@ -38,11 +37,11 @@ template class EccOpQueueRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters&, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters&, + const FF& scaling_factor) { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.cpp index 41103bb1b0d..d577f9f328c 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.cpp @@ -1,6 +1,5 @@ #include "barretenberg/honk/flavor/ecc_vm.hpp" #include "barretenberg/honk/sumcheck/relation_definitions_fwd.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "ecc_msm_relation.hpp" namespace proof_system::honk::sumcheck { @@ -19,10 +18,10 @@ namespace proof_system::honk::sumcheck { * @param scaling_factor optional term to scale the evaluation before adding to evals. */ template -template +template void ECCVMLookupRelationBase::accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& relation_params, + const Parameters& params, [[maybe_unused]] const FF& scaling_factor) { using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>; @@ -41,9 +40,9 @@ void ECCVMLookupRelationBase::accumulate(ContainerOverSubrelations& accumula // i.e. (1 / read_term[i]) = lookup_inverse * \prod_{j /ne i} (read_term[j]) * \prod_k (write_term[k]) // (1 / write_term[i]) = lookup_inverse * \prod_j (read_term[j]) * \prod_{k ne i} (write_term[k]) barretenberg::constexpr_for<0, READ_TERMS, 1>( - [&]() { lookup_terms[i] = compute_read_term(in, relation_params); }); + [&]() { lookup_terms[i] = compute_read_term(in, params); }); barretenberg::constexpr_for<0, WRITE_TERMS, 1>( - [&]() { lookup_terms[i + READ_TERMS] = compute_write_term(in, relation_params); }); + [&]() { lookup_terms[i + READ_TERMS] = compute_write_term(in, params); }); barretenberg::constexpr_for<0, NUM_TOTAL_TERMS, 1>( [&]() { denominator_accumulator[i] = lookup_terms[i]; }); diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.hpp index c7ae8d5ef1e..a59d82936a3 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_lookup_relation.hpp @@ -5,7 +5,6 @@ #include "barretenberg/common/constexpr_utils.hpp" #include "barretenberg/polynomials/polynomial.hpp" #include "barretenberg/polynomials/univariate.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system::honk::sumcheck { @@ -18,7 +17,7 @@ template class ECCVMLookupRelationBase { // 1 + polynomial degree of this relation static constexpr size_t LENGTH = READ_TERMS + WRITE_TERMS + 3; // 9 - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ LENGTH, // grand product construction sub-relation LENGTH // left-shiftable polynomial sub-relation }; @@ -31,46 +30,46 @@ template class ECCVMLookupRelationBase { return (row.msm_add == 1) || (row.msm_skew == 1) || (row.precompute_select == 1); } - template - static Accumulator0 compute_read_term_predicate(const AllEntities& in) + template + static Accumulator compute_read_term_predicate(const AllEntities& in) { - using View = typename Accumulator0::View; + using View = typename Accumulator::View; if constexpr (read_index == 0) { - return Accumulator0(View(in.msm_add1)); + return Accumulator(View(in.msm_add1)); } if constexpr (read_index == 1) { - return Accumulator0(View(in.msm_add2)); + return Accumulator(View(in.msm_add2)); } if constexpr (read_index == 2) { - return Accumulator0(View(in.msm_add3)); + return Accumulator(View(in.msm_add3)); } if constexpr (read_index == 3) { - return Accumulator0(View(in.msm_add4)); + return Accumulator(View(in.msm_add4)); } - return Accumulator0(1); + return Accumulator(1); } - template - static Accumulator0 compute_write_term_predicate(const AllEntities& in) + template + static Accumulator compute_write_term_predicate(const AllEntities& in) { - using View = typename Accumulator0::View; + using View = typename Accumulator::View; if constexpr (write_index == 0) { - return Accumulator0(View(in.precompute_select)); + return Accumulator(View(in.precompute_select)); } if constexpr (write_index == 1) { // TODO(https://github.com/AztecProtocol/barretenberg/issues/750) Is this a bug? - return Accumulator0(View(in.precompute_select)); + return Accumulator(View(in.precompute_select)); } - return Accumulator0(1); + return Accumulator(1); } - template - static Accumulator0 compute_write_term(const AllEntities& in, const RelationParameters& relation_params) + template + static Accumulator compute_write_term(const AllEntities& in, const Parameters& params) { - using View = typename Accumulator0::View; + using View = typename Accumulator::View; static_assert(write_index < WRITE_TERMS); @@ -95,10 +94,10 @@ template class ECCVMLookupRelationBase { const auto& tx = View(in.precompute_tx); const auto& ty = View(in.precompute_ty); const auto& precompute_round = View(in.precompute_round); - const auto& gamma = relation_params.gamma; - const auto& beta = relation_params.beta; - const auto& beta_sqr = relation_params.beta_sqr; - const auto& beta_cube = relation_params.beta_cube; + const auto& gamma = params.gamma; + const auto& beta = params.beta; + const auto& beta_sqr = params.beta_sqr; + const auto& beta_cube = params.beta_cube; // slice value : (wnaf value) : lookup term // 0 : -15 : 0 @@ -135,21 +134,21 @@ template class ECCVMLookupRelationBase { if constexpr (write_index == 1) { return negative_term; // degree 1 } - return Accumulator0(1); + return Accumulator(1); } - template - static Accumulator0 compute_read_term(const AllEntities& in, const RelationParameters& relation_params) + template + static Accumulator compute_read_term(const AllEntities& in, const Parameters& params) { - using View = typename Accumulator0::View; + using View = typename Accumulator::View; // read term: // pc, slice, x, y static_assert(read_index < READ_TERMS); - const auto& gamma = relation_params.gamma; - const auto& beta = relation_params.beta; - const auto& beta_sqr = relation_params.beta_sqr; - const auto& beta_cube = relation_params.beta_cube; + const auto& gamma = params.gamma; + const auto& beta = params.beta; + const auto& beta_sqr = params.beta_sqr; + const auto& beta_cube = params.beta_cube; const auto& msm_pc = View(in.msm_pc); const auto& msm_count = View(in.msm_count); const auto& msm_slice1 = View(in.msm_slice1); @@ -190,7 +189,7 @@ template class ECCVMLookupRelationBase { if constexpr (read_index == 3) { return read_term4; // degree 1 } - return Accumulator0(1); + return Accumulator(1); } /** @@ -206,10 +205,10 @@ template class ECCVMLookupRelationBase { * @param relation_params contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template + template static void accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& relation_params, + const Parameters& params, const FF& /*unused*/); }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.cpp index a0bead44501..23851ae5959 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.cpp @@ -1,7 +1,6 @@ #include "ecc_msm_relation.hpp" #include "barretenberg/honk/flavor/ecc_vm.hpp" #include "barretenberg/honk/sumcheck/relation_definitions_fwd.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" namespace proof_system::honk::sumcheck { @@ -37,10 +36,10 @@ namespace proof_system::honk::sumcheck { * @param scaling_factor optional term to scale the evaluation before adding to evals. */ template -template +template void ECCVMMSMRelationBase::accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /*unused*/, + const Parameters& /*unused*/, const FF& scaling_factor) { using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.hpp index cd7c43aa1db..93cd28d6de2 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_msm_relation.hpp @@ -38,13 +38,14 @@ template class ECCVMMSMRelationBase { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, - 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, + 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 }; - template + template static void accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /* unused */, + const Parameters& /* unused */, const FF& scaling_factor); }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.cpp index 0656e8c492f..fbe6a299d36 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.cpp @@ -1,7 +1,6 @@ #include "ecc_point_table_relation.hpp" #include "barretenberg/honk/flavor/ecc_vm.hpp" #include "barretenberg/honk/sumcheck/relation_definitions_fwd.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" namespace proof_system::honk::sumcheck { @@ -18,10 +17,10 @@ namespace proof_system::honk::sumcheck { * @param scaling_factor optional term to scale the evaluation before adding to evals. */ template -template +template void ECCVMPointTableRelationBase::accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /*unused*/, + const Parameters& /*unused*/, const FF& scaling_factor) { using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.hpp index 40457041709..ffef181cc71 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_point_table_relation.hpp @@ -19,12 +19,12 @@ template class ECCVMPointTableRelationBase { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ 6, 6, 6, 6, 6, 6 }; + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, 6, 6, 6, 6, 6 }; - template + template static void accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /* unused */, + const Parameters& /* unused */, const FF& scaling_factor); }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.cpp index 4e6e0680db9..3663c61087f 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.cpp @@ -1,6 +1,5 @@ #include "barretenberg/honk/flavor/ecc_vm.hpp" #include "barretenberg/honk/sumcheck/relation_definitions_fwd.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "ecc_msm_relation.hpp" namespace proof_system::honk::sumcheck { @@ -34,9 +33,8 @@ namespace proof_system::honk::sumcheck { * @return ECCVMSetRelationBase::template Accumulator */ template -template -Accumulator ECCVMSetRelationBase::compute_permutation_numerator(const AllEntities& in, - const RelationParameters& relation_params) +template +Accumulator ECCVMSetRelationBase::compute_permutation_numerator(const AllEntities& in, const Parameters& params) { using View = typename Accumulator::View; @@ -44,10 +42,10 @@ Accumulator ECCVMSetRelationBase::compute_permutation_numerator(const AllEnt const auto precompute_round2 = precompute_round + precompute_round; const auto precompute_round4 = precompute_round2 + precompute_round2; - const auto& gamma = relation_params.gamma; - const auto& beta = relation_params.beta; - const auto& beta_sqr = relation_params.beta_sqr; - const auto& beta_cube = relation_params.beta_cube; + const auto& gamma = params.gamma; + const auto& beta = params.beta; + const auto& beta_sqr = params.beta_sqr; + const auto& beta_cube = params.beta_cube; const auto& precompute_pc = View(in.precompute_pc); const auto& precompute_select = View(in.precompute_select); @@ -115,7 +113,7 @@ Accumulator ECCVMSetRelationBase::compute_permutation_numerator(const AllEnt numerator *= skew_input; // degree-5 } { - const auto& eccvm_set_permutation_delta = relation_params.eccvm_set_permutation_delta; + const auto& eccvm_set_permutation_delta = params.eccvm_set_permutation_delta; numerator *= precompute_select * (-eccvm_set_permutation_delta + 1) + eccvm_set_permutation_delta; // degree-7 } @@ -228,18 +226,17 @@ Accumulator ECCVMSetRelationBase::compute_permutation_numerator(const AllEnt } template -template -Accumulator ECCVMSetRelationBase::compute_permutation_denominator(const AllEntities& in, - const RelationParameters& relation_params) +template +Accumulator ECCVMSetRelationBase::compute_permutation_denominator(const AllEntities& in, const Parameters& params) { using View = typename Accumulator::View; // TODO(@zac-williamson). The degree of this contribution is 17! makes overall relation degree 19. // Can optimise by refining the algebra, once we have a stable base to iterate off of. - const auto& gamma = relation_params.gamma; - const auto& beta = relation_params.beta; - const auto& beta_sqr = relation_params.beta_sqr; - const auto& beta_cube = relation_params.beta_cube; + const auto& gamma = params.gamma; + const auto& beta = params.beta; + const auto& beta_sqr = params.beta_sqr; + const auto& beta_cube = params.beta_cube; const auto& msm_pc = View(in.msm_pc); const auto& msm_count = View(in.msm_count); const auto& msm_round = View(in.msm_round); @@ -366,20 +363,20 @@ Accumulator ECCVMSetRelationBase::compute_permutation_denominator(const AllE * @param scaling_factor optional term to scale the evaluation before adding to evals. */ template -template +template void ECCVMSetRelationBase::accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& relation_params, + const Parameters& params, const FF& scaling_factor) { using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; // degree-11 - Accumulator numerator_evaluation = compute_permutation_numerator(in, relation_params); + Accumulator numerator_evaluation = compute_permutation_numerator(in, params); // degree-17 - Accumulator denominator_evaluation = compute_permutation_denominator(in, relation_params); + Accumulator denominator_evaluation = compute_permutation_denominator(in, params); const auto& lagrange_first = View(in.lagrange_first); const auto& lagrange_last = View(in.lagrange_last); diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.hpp index de75aaf67b6..efa01b6e177 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_set_relation.hpp @@ -5,7 +5,6 @@ #include "barretenberg/common/constexpr_utils.hpp" #include "barretenberg/polynomials/polynomial.hpp" #include "barretenberg/polynomials/univariate.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system::honk::sumcheck { @@ -14,7 +13,7 @@ template class ECCVMSetRelationBase { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 19, // grand product construction sub-relation 19 // left-shiftable polynomial sub-relation }; @@ -32,18 +31,16 @@ template class ECCVMSetRelationBase { inline static auto& get_grand_product_polynomial(auto& input) { return input.z_perm; } inline static auto& get_shifted_grand_product_polynomial(auto& input) { return input.z_perm_shift; } - template - static Accumulator compute_permutation_numerator(const AllEntities& in, - const RelationParameters& relation_params); + template + static Accumulator compute_permutation_numerator(const AllEntities& in, const Parameters& params); - template - static Accumulator compute_permutation_denominator(const AllEntities& in, - const RelationParameters& relation_params); + template + static Accumulator compute_permutation_denominator(const AllEntities& in, const Parameters& params); - template + template static void accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& relation_params, + const Parameters& params, const FF& scaling_factor); }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.cpp index 53a5b1c22f4..028fe269f40 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.cpp @@ -4,7 +4,6 @@ #include "./ecc_transcript_relation.hpp" #include "barretenberg/honk/flavor/ecc_vm.hpp" #include "barretenberg/honk/sumcheck/relation_definitions_fwd.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" namespace proof_system::honk::sumcheck { @@ -31,10 +30,10 @@ namespace proof_system::honk::sumcheck { * @tparam PolynomialTypes */ template -template +template void ECCVMTranscriptRelationBase::accumulate(ContainerOverSubrelations& accumulator, - const PolynomialTypes& in, - const RelationParameters& /*unused*/, + const AllEntities& in, + const Parameters& /*unused*/, const FF& scaling_factor) { using Accumulator = typename std::tuple_element_t<0, ContainerOverSubrelations>; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.hpp index 58c4ca6c57a..2bee205554f 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_transcript_relation.hpp @@ -30,14 +30,14 @@ template class ECCVMTranscriptRelationBase { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, }; - template + template static void accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /* unused */, + const Parameters& /* unused */, const FF& scaling_factor); // TODO(@zac-williamson #2609 find more generic way of doing this) diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.cpp index c294bceaca8..ec8412b7111 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.cpp @@ -1,7 +1,6 @@ #include "ecc_wnaf_relation.hpp" #include "barretenberg/honk/flavor/ecc_vm.hpp" #include "barretenberg/honk/sumcheck/relation_definitions_fwd.hpp" -#include "barretenberg/proof_system/relations/relation_parameters.hpp" namespace proof_system::honk::sumcheck { @@ -36,10 +35,10 @@ namespace proof_system::honk::sumcheck { * @tparam AccumulatorTypes */ template -template +template void ECCVMWnafRelationBase::accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /*unused*/, + const Parameters& /*unused*/, const FF& scaling_factor) { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.hpp index 7567b1be70f..73b3f08e814 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ecc_vm/ecc_wnaf_relation.hpp @@ -35,14 +35,14 @@ template class ECCVMWnafRelationBase { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, }; - template + template static void accumulate(ContainerOverSubrelations& accumulator, const AllEntities& in, - const RelationParameters& /* unused */, + const Parameters& /* unused */, const FF& scaling_factor); }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/elliptic_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/elliptic_relation.hpp index 6b13f01f84e..87d84c1330c 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/elliptic_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/elliptic_relation.hpp @@ -1,9 +1,7 @@ #pragma once #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/ecc/curves/grumpkin/grumpkin.hpp" - -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -11,7 +9,7 @@ template class EllipticRelationImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, // x-coordinate sub-relation 6, // y-coordinate sub-relation }; @@ -38,11 +36,11 @@ template class EllipticRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - static void accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters&, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters&, + const FF& scaling_factor) { // TODO(@zac - williamson #2608 when Pedersen refactor is completed, // replace old addition relations with these ones and diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/gen_perm_sort_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/gen_perm_sort_relation.hpp index ac25d184d23..c61dda3e2be 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/gen_perm_sort_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/gen_perm_sort_relation.hpp @@ -1,6 +1,5 @@ #pragma once -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -8,7 +7,7 @@ template class GenPermSortRelationImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, // range constrain sub-relation 1 6, // range constrain sub-relation 2 6, // range constrain sub-relation 3 @@ -30,11 +29,11 @@ template class GenPermSortRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters&, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters&, + const FF& scaling_factor) { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; @@ -93,137 +92,6 @@ template class GenPermSortRelationImpl { }; }; -template class GoblinTranslatorGenPermSortRelationImpl { - public: - using FF = FF_; - - // 1 + polynomial degree of this relation - static constexpr size_t RELATION_LENGTH = 6; // degree((lagrange_last-1) * D(D - 1)(D - 2)(D - 3)) = 5 - - static constexpr std::array SUBRELATION_LENGTHS{ - 6, // ordered_range_constraints_0 step in {0,1,2,3} subrelation - 6, // ordered_range_constraints_1 step in {0,1,2,3} subrelation - 6, // ordered_range_constraints_2 step in {0,1,2,3} subrelation - 6, // ordered_range_constraints_3 step in {0,1,2,3} subrelation - 6, // ordered_range_constraints_4 step in {0,1,2,3} subrelation - 3, // ordered_range_constraints_0 ends with defined maximum value subrelation - 3, // ordered_range_constraints_1 ends with defined maximum value subrelation - 3, // ordered_range_constraints_2 ends with defined maximum value subrelation - 3, // ordered_range_constraints_3 ends with defined maximum value subrelation - 3 // ordered_range_constraints_4 ends with defined maximum value subrelation - - }; - - /** - * @brief Expression for the generalized permutation sort relation - * - * @details The relation enforces 2 constraints on each of the ordered_range_constraints wires: - * 1) 2 sequential values are non-descending and have a difference of at most 3, except for the value at last index - * 2) The value at last index is 2¹⁴ - 1 - * - * @param evals transformed to `evals + C(in(X)...)*scaling_factor` - * @param in an std::array containing the fully extended Univariate edges. - * @param parameters contains beta, gamma, and public_input_delta, .... - * @param scaling_factor optional term to scale the evaluation before adding to evals. - */ - template - inline static void accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters&, - const FF& scaling_factor) - { - using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; - using View = typename Accumulator::View; - auto ordered_range_constraints_0 = View(in.ordered_range_constraints_0); - auto ordered_range_constraints_1 = View(in.ordered_range_constraints_1); - auto ordered_range_constraints_2 = View(in.ordered_range_constraints_2); - auto ordered_range_constraints_3 = View(in.ordered_range_constraints_3); - auto ordered_range_constraints_4 = View(in.ordered_range_constraints_4); - auto ordered_range_constraints_0_shift = View(in.ordered_range_constraints_0_shift); - auto ordered_range_constraints_1_shift = View(in.ordered_range_constraints_1_shift); - auto ordered_range_constraints_2_shift = View(in.ordered_range_constraints_2_shift); - auto ordered_range_constraints_3_shift = View(in.ordered_range_constraints_3_shift); - auto ordered_range_constraints_4_shift = View(in.ordered_range_constraints_4_shift); - auto lagrange_last = View(in.lagrange_last); - - static const FF minus_one = FF(-1); - static const FF minus_two = FF(-2); - static const FF minus_three = FF(-3); - static const size_t micro_limb_bits = 14; - static const auto maximum_sort_value = -FF((1 << micro_limb_bits) - 1); - // Compute wire differences - auto delta_1 = ordered_range_constraints_0_shift - ordered_range_constraints_0; - auto delta_2 = ordered_range_constraints_1_shift - ordered_range_constraints_1; - auto delta_3 = ordered_range_constraints_2_shift - ordered_range_constraints_2; - auto delta_4 = ordered_range_constraints_3_shift - ordered_range_constraints_3; - auto delta_5 = ordered_range_constraints_4_shift - ordered_range_constraints_4; - - // Contribution (1) (contributions 1-5 ensure that the sequential values have a difference of {0,1,2,3}) - auto tmp_1 = delta_1; - tmp_1 *= (delta_1 + minus_one); - tmp_1 *= (delta_1 + minus_two); - tmp_1 *= (delta_1 + minus_three); - tmp_1 *= (lagrange_last + minus_one); - tmp_1 *= scaling_factor; - std::get<0>(accumulators) += tmp_1; - - // Contribution (2) - auto tmp_2 = delta_2; - tmp_2 *= (delta_2 + minus_one); - tmp_2 *= (delta_2 + minus_two); - tmp_2 *= (delta_2 + minus_three); - tmp_2 *= (lagrange_last + minus_one); - tmp_2 *= scaling_factor; - - std::get<1>(accumulators) += tmp_2; - - // Contribution (3) - auto tmp_3 = delta_3; - tmp_3 *= (delta_3 + minus_one); - tmp_3 *= (delta_3 + minus_two); - tmp_3 *= (delta_3 + minus_three); - tmp_3 *= (lagrange_last + minus_one); - tmp_3 *= scaling_factor; - std::get<2>(accumulators) += tmp_3; - - // Contribution (4) - auto tmp_4 = delta_4; - tmp_4 *= (delta_4 + minus_one); - tmp_4 *= (delta_4 + minus_two); - tmp_4 *= (delta_4 + minus_three); - tmp_4 *= (lagrange_last + minus_one); - tmp_4 *= scaling_factor; - std::get<3>(accumulators) += tmp_4; - - // Contribution (5) - auto tmp_5 = delta_5; - tmp_5 *= (delta_5 + minus_one); - tmp_5 *= (delta_5 + minus_two); - tmp_5 *= (delta_5 + minus_three); - tmp_5 *= (lagrange_last + minus_one); - tmp_5 *= scaling_factor; - std::get<4>(accumulators) += tmp_5; - - // Contribution (6) (Contributions 6-10 ensure that the last value is the designated maximum value. We don't - // need to constrain the first value to be 0, because the shift mechanic does this for us) - std::get<5>(accumulators) += - lagrange_last * (ordered_range_constraints_0 + maximum_sort_value) * scaling_factor; - // Contribution (7) - std::get<6>(accumulators) += - lagrange_last * (ordered_range_constraints_1 + maximum_sort_value) * scaling_factor; - // Contribution (8) - std::get<7>(accumulators) += - lagrange_last * (ordered_range_constraints_2 + maximum_sort_value) * scaling_factor; - // Contribution (9) - std::get<8>(accumulators) += - lagrange_last * (ordered_range_constraints_3 + maximum_sort_value) * scaling_factor; - // Contribution (10) - std::get<9>(accumulators) += - lagrange_last * (ordered_range_constraints_4 + maximum_sort_value) * scaling_factor; - }; -}; template using GenPermSortRelation = Relation>; -template -using GoblinTranslatorGenPermSortRelation = Relation>; } // namespace proof_system diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/lookup_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/lookup_relation.hpp index 493d6f3d977..bd825b75d88 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/lookup_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/lookup_relation.hpp @@ -1,6 +1,5 @@ #pragma once -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -23,11 +22,15 @@ template class LookupRelationImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, // grand product construction sub-relation 3 // left-shiftable polynomial sub-relation }; + static constexpr std::array TOTAL_LENGTH_ADJUSTMENTS{ + 6, // grand product construction sub-relation + 0 // left-shiftable polynomial sub-relation + }; /** * @brief Get the grand product polynomial object (either from the proving key or AllEntities depending on context) * @@ -55,20 +58,21 @@ template class LookupRelationImpl { * @param relation_parameters * @param index If calling this method over vector inputs, index >= 0 */ - template - inline static Accumulator compute_grand_product_numerator(const AllEntities& in, - const RelationParameters& relation_parameters) + template + inline static Accumulator compute_grand_product_numerator(const AllEntities& in, const Parameters& params) { - const auto& beta = relation_parameters.beta; - const auto& gamma = relation_parameters.gamma; - const auto& eta = relation_parameters.eta; + using View = typename Accumulator::View; + using ParameterView = GetParameterView; + + const auto& beta = ParameterView(params.beta); + const auto& gamma = ParameterView(params.gamma); + const auto& eta = ParameterView(params.eta); const auto eta_sqr = eta * eta; const auto eta_cube = eta_sqr * eta; - const auto one_plus_beta = FF(1) + beta; + const auto one_plus_beta = beta + FF(1); const auto gamma_by_one_plus_beta = gamma * one_plus_beta; - using View = typename Accumulator::View; auto w_1 = View(in.w_l); auto w_2 = View(in.w_r); auto w_3 = View(in.w_o); @@ -94,19 +98,23 @@ template class LookupRelationImpl { auto q_lookup = View(in.q_lookup); // (w_1 + q_2*w_1_shift) + η(w_2 + q_m*w_2_shift) + η²(w_3 + q_c*w_3_shift) + η³q_index. + // deg 2 or 4 auto wire_accum = (w_1 + column_1_step_size * w_1_shift) + (w_2 + column_2_step_size * w_2_shift) * eta + (w_3 + column_3_step_size * w_3_shift) * eta_sqr + table_index * eta_cube; // t_1 + ηt_2 + η²t_3 + η³t_4 + // deg 1 or 4 auto table_accum = table_1 + table_2 * eta + table_3 * eta_sqr + table_4 * eta_cube; + // t_1_shift + ηt_2_shift + η²t_3_shift + η³t_4_shift + // deg 4 auto table_accum_shift = table_1_shift + table_2_shift * eta + table_3_shift * eta_sqr + table_4_shift * eta_cube; - auto tmp = (q_lookup * wire_accum + gamma); - tmp *= (table_accum + table_accum_shift * beta + gamma_by_one_plus_beta); - tmp *= one_plus_beta; - return tmp; + auto tmp = (q_lookup * wire_accum + gamma); // deg 2 or 4 + tmp *= (table_accum + table_accum_shift * beta + gamma_by_one_plus_beta); // 1 or 5 + tmp *= one_plus_beta; // deg 1 + return tmp; // deg 4 or 10 } /** @@ -119,24 +127,24 @@ template class LookupRelationImpl { * @param relation_parameters * @param index */ - template - inline static Accumulator compute_grand_product_denominator(const AllEntities& in, - const RelationParameters& relation_parameters) + template + inline static Accumulator compute_grand_product_denominator(const AllEntities& in, const Parameters& params) { - const auto& beta = relation_parameters.beta; - const auto& gamma = relation_parameters.gamma; + using View = typename Accumulator::View; + using ParameterView = GetParameterView; - const auto one_plus_beta = FF(1) + beta; - const auto gamma_by_one_plus_beta = gamma * one_plus_beta; + const auto& beta = ParameterView(params.beta); + const auto& gamma = ParameterView(params.gamma); - using View = typename Accumulator::View; + const auto one_plus_beta = beta + FF(1); + const auto gamma_by_one_plus_beta = gamma * one_plus_beta; // deg 0 or 2 // Contribution (1) auto s_accum = View(in.sorted_accum); auto s_accum_shift = View(in.sorted_accum_shift); - auto tmp = (s_accum + s_accum_shift * beta + gamma_by_one_plus_beta); + auto tmp = (s_accum + s_accum_shift * beta + gamma_by_one_plus_beta); // 1 or 2 return tmp; } @@ -158,17 +166,19 @@ template class LookupRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters& relation_parameters, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters& params, + const FF& scaling_factor) { - const auto& grand_product_delta = relation_parameters.lookup_grand_product_delta; { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; + using ParameterView = GetParameterView; + + const auto& grand_product_delta = ParameterView(params.lookup_grand_product_delta); auto z_lookup = View(in.z_lookup); auto z_lookup_shift = View(in.z_lookup_shift); @@ -176,9 +186,10 @@ template class LookupRelationImpl { auto lagrange_first = View(in.lagrange_first); auto lagrange_last = View(in.lagrange_last); - const auto lhs = compute_grand_product_numerator(in, relation_parameters); - const auto rhs = compute_grand_product_denominator(in, relation_parameters); + const auto lhs = compute_grand_product_numerator(in, params); // deg 4 or 10 + const auto rhs = compute_grand_product_denominator(in, params); // deg 1 or 2 + // (deg 5 or 11) - (deg 3 or 5) const auto tmp = lhs * (z_lookup + lagrange_first) - rhs * (z_lookup_shift + lagrange_last * grand_product_delta); std::get<0>(accumulators) += tmp * scaling_factor; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/permutation_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/permutation_relation.hpp index c542000c127..8e4cdf1c107 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/permutation_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/permutation_relation.hpp @@ -1,6 +1,5 @@ #pragma once -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -8,19 +7,24 @@ template class UltraPermutationRelationImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, // grand product construction sub-relation 3 // left-shiftable polynomial sub-relation }; + static constexpr std::array TOTAL_LENGTH_ADJUSTMENTS{ + 5, // grand product construction sub-relation + 0 // left-shiftable polynomial sub-relation + }; + inline static auto& get_grand_product_polynomial(auto& in) { return in.z_perm; } inline static auto& get_shifted_grand_product_polynomial(auto& in) { return in.z_perm_shift; } - template - inline static Accumulator compute_grand_product_numerator(const AllEntities& in, - const RelationParameters& relation_parameters) + template + inline static Accumulator compute_grand_product_numerator(const AllEntities& in, const Parameters& params) { using View = typename Accumulator::View; + using ParameterView = GetParameterView; auto w_1 = View(in.w_l); auto w_2 = View(in.w_r); @@ -31,18 +35,19 @@ template class UltraPermutationRelationImpl { auto id_3 = View(in.id_3); auto id_4 = View(in.id_4); - const auto& beta = relation_parameters.beta; - const auto& gamma = relation_parameters.gamma; + const auto& beta = ParameterView(params.beta); + const auto& gamma = ParameterView(params.gamma); + // witness degree 4; fully degree 8 return (w_1 + id_1 * beta + gamma) * (w_2 + id_2 * beta + gamma) * (w_3 + id_3 * beta + gamma) * (w_4 + id_4 * beta + gamma); } - template - inline static Accumulator compute_grand_product_denominator(const AllEntities& in, - const RelationParameters& relation_parameters) + template + inline static Accumulator compute_grand_product_denominator(const AllEntities& in, const Parameters& params) { using View = typename Accumulator::View; + using ParameterView = GetParameterView; auto w_1 = View(in.w_l); auto w_2 = View(in.w_r); @@ -54,9 +59,10 @@ template class UltraPermutationRelationImpl { auto sigma_3 = View(in.sigma_3); auto sigma_4 = View(in.sigma_4); - const auto& beta = relation_parameters.beta; - const auto& gamma = relation_parameters.gamma; + const auto& beta = ParameterView(params.beta); + const auto& gamma = ParameterView(params.gamma); + // witness degree 4; fully degree 8 return (w_1 + sigma_1 * beta + gamma) * (w_2 + sigma_2 * beta + gamma) * (w_3 + sigma_3 * beta + gamma) * (w_4 + sigma_4 * beta + gamma); } @@ -72,152 +78,44 @@ template class UltraPermutationRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template + template inline static void accumulate(ContainerOverSubrelations& accumulators, const AllEntities& in, - const RelationParameters& relation_parameters, + const Parameters& params, const FF& scaling_factor) { - const auto& public_input_delta = relation_parameters.public_input_delta; - // Contribution (1) - { - using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; - using View = typename Accumulator::View; - auto z_perm = View(in.z_perm); - auto z_perm_shift = View(in.z_perm_shift); - auto lagrange_first = View(in.lagrange_first); - auto lagrange_last = View(in.lagrange_last); - - // Contribution (1) - std::get<0>(accumulators) += - (((z_perm + lagrange_first) * compute_grand_product_numerator(in, relation_parameters)) - - ((z_perm_shift + lagrange_last * public_input_delta) * - compute_grand_product_denominator(in, relation_parameters))) * - scaling_factor; - } - // Contribution (2) - { - using Accumulator = std::tuple_element_t<1, ContainerOverSubrelations>; - using View = typename Accumulator::View; - auto z_perm_shift = View(in.z_perm_shift); - auto lagrange_last = View(in.lagrange_last); - - std::get<1>(accumulators) += (lagrange_last * z_perm_shift) * scaling_factor; - } - }; -}; - -template class GoblinTranslatorPermutationRelationImpl { - public: - using FF = FF_; - // 1 + polynomial degree of this relation - static constexpr size_t RELATION_LENGTH = 7; - - static constexpr std::array SUBRELATION_LENGTHS{ - 7, // grand product construction sub-relation - 3 // left-shiftable polynomial sub-relation - }; - - inline static auto& get_grand_product_polynomial(auto& in) { return in.z_perm; } - inline static auto& get_shifted_grand_product_polynomial(auto& in) { return in.z_perm_shift; } - - template - inline static Accumulator compute_grand_product_numerator(const AllEntities& in, - const RelationParameters& relation_parameters) - { - using View = typename Accumulator::View; - - auto concatenated_range_constraints_0 = View(in.concatenated_range_constraints_0); - auto concatenated_range_constraints_1 = View(in.concatenated_range_constraints_1); - auto concatenated_range_constraints_2 = View(in.concatenated_range_constraints_2); - auto concatenated_range_constraints_3 = View(in.concatenated_range_constraints_3); - - auto ordered_extra_range_constraints_numerator = View(in.ordered_extra_range_constraints_numerator); - - const auto& gamma = relation_parameters.gamma; - return (concatenated_range_constraints_0 + gamma) * (concatenated_range_constraints_1 + gamma) * - (concatenated_range_constraints_2 + gamma) * (concatenated_range_constraints_3 + gamma) * - (ordered_extra_range_constraints_numerator + gamma); - } - - template - inline static Accumulator compute_grand_product_denominator(const AllEntities& in, - const RelationParameters& relation_parameters) - { - using View = typename Accumulator::View; - - auto ordered_range_constraints_0 = View(in.ordered_range_constraints_0); - auto ordered_range_constraints_1 = View(in.ordered_range_constraints_1); - auto ordered_range_constraints_2 = View(in.ordered_range_constraints_2); - auto ordered_range_constraints_3 = View(in.ordered_range_constraints_3); - auto ordered_range_constraints_4 = View(in.ordered_range_constraints_4); - - const auto& gamma = relation_parameters.gamma; - - return (ordered_range_constraints_0 + gamma) * (ordered_range_constraints_1 + gamma) * - (ordered_range_constraints_2 + gamma) * (ordered_range_constraints_3 + gamma) * - (ordered_range_constraints_4 + gamma); - } - /** - * @brief Compute contribution of the goblin translator permutation relation for a given edge (internal function) - * - * @details There are 2 relations associated with enforcing the set permutation relation - * This file handles the relation that confirms faithful calculation of the grand - * product polynomial Z_perm. - * - * C(in(X)...) = - * ( z_perm(X) + lagrange_first(X) )*P(X) - * - ( z_perm_shift(X) + lagrange_last(X))*Q(X), - * where P(X) = Prod_{i=0:4} numerator_polynomial_i(X) + γ - * Q(X) = Prod_{i=0:4} ordered_range_constraint_i(X) + γ - * the first 4 numerator polynomials are concatenated range constraint polynomials and the last one is the constant - * extra numerator - * - * @param evals transformed to `evals + C(in(X)...)*scaling_factor` - * @param in an std::array containing the fully extended Univariate edges. - * @param parameters contains beta, gamma, and public_input_delta, .... - * @param scaling_factor optional term to scale the evaluation before adding to evals. - */ - - template - inline static void accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters& relation_parameters, - const FF& scaling_factor) - { [&]() { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; - - auto z_perm = View(in.z_perm); - auto z_perm_shift = View(in.z_perm_shift); - auto lagrange_first = View(in.lagrange_first); - auto lagrange_last = View(in.lagrange_last); - - // Contribution (1) + using ParameterView = GetParameterView; + const auto public_input_delta = ParameterView(params.public_input_delta); + const auto z_perm = View(in.z_perm); + const auto z_perm_shift = View(in.z_perm_shift); + const auto lagrange_first = View(in.lagrange_first); + const auto lagrange_last = View(in.lagrange_last); + + // witness degree: deg 5 - deg 5 = deg 5 + // total degree: deg 9 - deg 10 = deg 10 std::get<0>(accumulators) += - (((z_perm + lagrange_first) * compute_grand_product_numerator(in, relation_parameters)) - - ((z_perm_shift + lagrange_last) * - compute_grand_product_denominator(in, relation_parameters))) * + (((z_perm + lagrange_first) * compute_grand_product_numerator(in, params)) - + ((z_perm_shift + lagrange_last * public_input_delta) * + compute_grand_product_denominator(in, params))) * scaling_factor; }(); + // Contribution (2) [&]() { using Accumulator = std::tuple_element_t<1, ContainerOverSubrelations>; using View = typename Accumulator::View; - auto z_perm_shift = View(in.z_perm_shift); auto lagrange_last = View(in.lagrange_last); - // Contribution (2) std::get<1>(accumulators) += (lagrange_last * z_perm_shift) * scaling_factor; }(); }; }; template using UltraPermutationRelation = Relation>; -template -using GoblinTranslatorPermutationRelation = Relation>; } // namespace proof_system diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_parameters.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_parameters.hpp index 59e76b613c4..096f5b79603 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_parameters.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_parameters.hpp @@ -1,80 +1,76 @@ #pragma once +#include "barretenberg/ecc/curves/bn254/fr.hpp" #include + namespace proof_system { /** * @brief Container for parameters used by the grand product (permutation, lookup) Honk relations * - * @tparam FF + * @tparam T, either a native field type or a Univariate. */ -template struct RelationParameters { +template struct RelationParameters { + using DataType = T; static constexpr int NUM_BINARY_LIMBS_IN_GOBLIN_TRANSLATOR = 4; static constexpr int NUM_NATIVE_LIMBS_IN_GOBLIN_TRANSLATOR = 1; static constexpr int NUM_CHALLENGE_POWERS_IN_GOBLIN_TRANSLATOR = 4; - FF eta = FF(0); // Lookup - FF beta = FF(0); // Permutation + Lookup - FF gamma = FF(0); // Permutation + Lookup - FF public_input_delta = FF(0); // Permutation - FF lookup_grand_product_delta = FF(0); // Lookup - FF beta_sqr = 0; - FF beta_cube = 0; + T eta = T(0); // Lookup + T beta = T(0); // Permutation + Lookup + T gamma = T(0); // Permutation + Lookup + T public_input_delta = T(0); // Permutation + T lookup_grand_product_delta = T(0); // Lookup + T beta_sqr = T(0); + T beta_cube = T(0); // eccvm_set_permutation_delta is used in the set membership gadget in eccvm/ecc_set_relation.hpp // We can remove this by modifying the relation, but increases complexity - FF eccvm_set_permutation_delta = 0; - std::array accumulated_result = { - FF(0), FF(0), FF(0), FF(0) + T eccvm_set_permutation_delta = T(0); + std::array accumulated_result = { + T(0), T(0), T(0), T(0) }; // Goblin Translator - std::array evaluation_input_x = { - FF(0), FF(0), FF(0), FF(0), FF(0) + std::array evaluation_input_x = { + T(0), T(0), T(0), T(0), T(0) }; // Goblin Translator - std::array, + std::array, NUM_CHALLENGE_POWERS_IN_GOBLIN_TRANSLATOR> - batching_challenge_v = { { { FF(0), FF(0), FF(0), FF(0), FF(0) }, - { FF(0), FF(0), FF(0), FF(0), FF(0) }, - { FF(0), FF(0), FF(0), FF(0), FF(0) }, - { FF(0), FF(0), FF(0), FF(0), FF(0) } } }; + batching_challenge_v = { { { T(0), T(0), T(0), T(0), T(0) }, + { T(0), T(0), T(0), T(0), T(0) }, + { T(0), T(0), T(0), T(0), T(0) }, + { T(0), T(0), T(0), T(0), T(0) } } }; + + static constexpr int NUM_TO_FOLD = 5; + std::array, NUM_TO_FOLD> to_fold = { + eta, beta, gamma, public_input_delta, lookup_grand_product_delta + }; static RelationParameters get_random() { RelationParameters result; - result.eta = FF::random_element(); - result.beta_sqr = result.beta.sqr(); + result.eta = T::random_element(); + result.beta_sqr = result.beta * result.beta; result.beta_cube = result.beta_sqr * result.beta; - result.beta = FF::random_element(); - result.gamma = FF::random_element(); - result.public_input_delta = FF::random_element(); - result.lookup_grand_product_delta = FF::random_element(); + result.beta = T::random_element(); + result.gamma = T::random_element(); + result.public_input_delta = T::random_element(); + result.lookup_grand_product_delta = T::random_element(); result.eccvm_set_permutation_delta = result.gamma * (result.gamma + result.beta_sqr) * (result.gamma + result.beta_sqr + result.beta_sqr) * (result.gamma + result.beta_sqr + result.beta_sqr + result.beta_sqr); result.accumulated_result = { - FF::random_element(), FF::random_element(), FF::random_element(), FF::random_element() + T::random_element(), T::random_element(), T::random_element(), T::random_element() }; result.evaluation_input_x = { - FF::random_element(), FF::random_element(), FF::random_element(), FF::random_element(), FF::random_element() + T::random_element(), T::random_element(), T::random_element(), T::random_element(), T::random_element() }; result.batching_challenge_v = { - std::array{ FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element() }, - { FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element() }, - { FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element() }, - { FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element(), - FF::random_element() }, + std::array{ T::random_element(), + T::random_element(), + T::random_element(), + T::random_element(), + T::random_element() }, + { T::random_element(), T::random_element(), T::random_element(), T::random_element(), T::random_element() }, + { T::random_element(), T::random_element(), T::random_element(), T::random_element(), T::random_element() }, + { T::random_element(), T::random_element(), T::random_element(), T::random_element(), T::random_element() }, }; return result; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_types.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_types.hpp index 4fc4f7673bd..b53d154cbfe 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_types.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/relation_types.hpp @@ -1,14 +1,25 @@ #pragma once +#include "barretenberg/ecc/curves/bn254/fr.hpp" #include "nested_containers.hpp" -#include "relation_parameters.hpp" #include -namespace barretenberg { -template class Polynomial; -} +template +concept IsField = std::same_as /* || std::same_as */; + namespace proof_system { -// forward-declare Polynomial so we can use in a concept +/** + * @brief A type to optionally extract a view of a relation parameter in a relation. + * + * @details In sumcheck, challenges in relations are always field elements, but in folding we need univariate + * challenges. This template inspecting the underlying type of a RelationParameters instance. When this type is a field + * type, do nothing, otherwise apply the provided view type. + * @tparam Params + * @tparam View + * @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/759): Optimize + */ +template +using GetParameterView = std::conditional_t, typename Params::DataType, View>; template concept HasSubrelationLinearlyIndependentMember = requires(T) { @@ -17,13 +28,16 @@ concept HasSubrelationLinearlyIndependentMember = requires(T) { } -> std::convertible_to; }; +template +concept HasParameterLengthAdjustmentsMember = requires { T::TOTAL_LENGTH_ADJUSTMENTS; }; + /** * @brief Check whether a given subrelation is linearly independent from the other subrelations. * - * @details More often than not, we want multiply each subrelation contribution by a power of the relation separator - * challenge. In cases where we wish to define a subrelation that merges into another, we encode this in a boolean array - * `SUBRELATION_LINEARLY_INDEPENDENT` in the relation. If no such array is defined, then the default case where all - * subrelations are independent is engaged. + * @details More often than not, we want multiply each subrelation contribution by a power of the relation + * separator challenge. In cases where we wish to define a subrelation that merges into another, we encode this + * in a boolean array `SUBRELATION_LINEARLY_INDEPENDENT` in the relation. If no such array is defined, then the + * default case where all subrelations are independent is engaged. */ template constexpr bool subrelation_is_linearly_independent() { @@ -34,49 +48,72 @@ template constexpr bool subrelatio } } +/** + * @brief Compute the total subrelation lengths, i.e., the lengths when regarding the challenges as + * variables. + */ +template +consteval std::array compute_total_subrelation_lengths() +{ + if constexpr (HasParameterLengthAdjustmentsMember) { + constexpr size_t NUM_SUBRELATIONS = RelationImpl::SUBRELATION_PARTIAL_LENGTHS.size(); + std::array result; + for (size_t idx = 0; idx < NUM_SUBRELATIONS; idx++) { + result[idx] = RelationImpl::SUBRELATION_PARTIAL_LENGTHS[idx] + RelationImpl::TOTAL_LENGTH_ADJUSTMENTS[idx]; + } + return result; + } else { + return RelationImpl::SUBRELATION_PARTIAL_LENGTHS; + } +}; + /** * @brief Get the subrelation accumulators for the Protogalaxy combiner calculation. - * @details A subrelation of degree D, when evaluated on polynomials of degree N, gives a polynomial of degree D * - * N. In the context of Protogalaxy, N = NUM_INSTANCES-1. Hence, given a subrelation of length x, its evaluation on - * such polynomials will have degree (x-1) * (NUM_INSTANCES-1), and the length of this evaluation will be one - * greater than this. + * @details A subrelation of degree D, when evaluated on polynomials of degree N, gives a polynomial of degree D + * * N. In the context of Protogalaxy, N = NUM_INSTANCES-1. Hence, given a subrelation of length x, its + * evaluation on such polynomials will have degree (x-1) * (NUM_INSTANCES-1), and the length of this evaluation + * will be one greater than this. * @tparam NUM_INSTANCES * @tparam NUM_SUBRELATIONS - * @param subrelation_lengths The array of subrelation lengths supplied by a relation. + * @param SUBRELATION_PARTIAL_LENGTHS The array of subrelation lengths supplied by a relation. * @return The transformed subrelation lenths */ template -consteval std::array get_composed_subrelation_lengths( - std::array subrelation_lengths) +consteval std::array compute_composed_subrelation_partial_lengths( + std::array SUBRELATION_PARTIAL_LENGTHS) { - std::transform(subrelation_lengths.begin(), - subrelation_lengths.end(), - subrelation_lengths.begin(), + std::transform(SUBRELATION_PARTIAL_LENGTHS.begin(), + SUBRELATION_PARTIAL_LENGTHS.end(), + SUBRELATION_PARTIAL_LENGTHS.begin(), [](const size_t x) { return (x - 1) * (NUM_INSTANCES - 1) + 1; }); - return subrelation_lengths; + return SUBRELATION_PARTIAL_LENGTHS; }; /** - * @brief The templates defined herein facilitate sharing the relation arithmetic between the prover and the verifier. + * @brief The templates defined herein facilitate sharing the relation arithmetic between the prover and the + * verifier. * - * The sumcheck prover and verifier accumulate the contributions from each relation (really, each sub-relation) into, - * respectively, Univariates and individual field elements. When performing relation arithmetic on Univariates, we - * introduce UnivariateViews to reduce full length Univariates to the minimum required length and to avoid unnecessary - * copies. + * @details The sumcheck prover and verifier accumulate the contributions from each relation (really, each sub-relation) + * into, respectively, Univariates and individual field elements. When performing relation arithmetic on + * Univariates, we introduce UnivariateViews to reduce full length Univariates to the minimum required length + * and to avoid unnecessary copies. * * To share the relation arithmetic, we introduce simple structs that specify two types: Accumulators and - * AccumulatorViews. For the prover, who accumulates Univariates, these are respectively std::tuple and - * std::tuple. For the verifier, who accumulates FFs, both types are simply aliases for std::array - * (since no "view" type is necessary). The containers std::tuple and std::array are needed to accommodate multiple - * sub-relations within each relation, where, for efficiency, each sub-relation has its own specified degree. + * AccumulatorViews. For the prover, who accumulates Univariates, these are respectively std::tuple + * and std::tuple. For the verifier, who accumulates FFs, both types are simply aliases for + * std::array (since no "view" type is necessary). The containers std::tuple and std::array are needed to + * accommodate multiple sub-relations within each relation, where, for efficiency, each sub-relation has its own + * specified degree. * - * @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/720) + * @note We use some funny terminology: we use the term "length" for 1 + the degree of a relation. When the relation is + * regarded as a polynomial in all of its arguments, we refer to this length as the "total length", and when we + * hold the relation parameters constant we refer to it as a "partial length." * */ /** - * @brief A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the contribution of - * a given relation to the corresponding accumulator. + * @brief A wrapper for Relations to expose methods used by the Sumcheck prover or verifier to add the + * contribution of a given relation to the corresponding accumulator. * * @tparam FF * @tparam RelationImpl Base class that implements the arithmetic for a given relation (or set of sub-relations) @@ -85,18 +122,25 @@ template class Relation : public RelationImpl { public: using FF = typename RelationImpl::FF; - static constexpr size_t RELATION_LENGTH = - *std::max_element(RelationImpl::SUBRELATION_LENGTHS.begin(), RelationImpl::SUBRELATION_LENGTHS.end()); + static constexpr std::array SUBRELATION_TOTAL_LENGTHS = + compute_total_subrelation_lengths(); + + static constexpr size_t RELATION_LENGTH = *std::max_element(RelationImpl::SUBRELATION_PARTIAL_LENGTHS.begin(), + RelationImpl::SUBRELATION_PARTIAL_LENGTHS.end()); + + static constexpr size_t TOTAL_RELATION_LENGTH = + *std::max_element(SUBRELATION_TOTAL_LENGTHS.begin(), SUBRELATION_TOTAL_LENGTHS.end()); template using ProtogalaxyTupleOfUnivariatesOverSubrelations = - TupleOfUnivariates(RelationImpl::SUBRELATION_LENGTHS)>; - using SumcheckTupleOfUnivariatesOverSubrelations = TupleOfUnivariates; - using SumcheckArrayOfValuesOverSubrelations = ArrayOfValues; + TupleOfUnivariates(SUBRELATION_TOTAL_LENGTHS)>; + using SumcheckTupleOfUnivariatesOverSubrelations = + TupleOfUnivariates; + using SumcheckArrayOfValuesOverSubrelations = ArrayOfValues; - // These are commonly needed, most importantly, for explicitly instantiating compute_foo_numerator/denomintor. + // These are commonly needed, most importantly, for explicitly instantiating + // compute_foo_numerator/denomintor. using UnivariateAccumulator0 = std::tuple_element_t<0, SumcheckTupleOfUnivariatesOverSubrelations>; using ValueAccumulator0 = std::tuple_element_t<0, SumcheckArrayOfValuesOverSubrelations>; }; - } // namespace proof_system diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/goblin_translator_relation_consistency.test.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/goblin_translator_relation_consistency.test.cpp similarity index 99% rename from barretenberg/cpp/src/barretenberg/proof_system/relations/goblin_translator_relation_consistency.test.cpp rename to barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/goblin_translator_relation_consistency.test.cpp index 5f4d243a9d6..147515bf028 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/goblin_translator_relation_consistency.test.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/goblin_translator_relation_consistency.test.cpp @@ -12,13 +12,12 @@ * */ #include "barretenberg/ecc/curves/bn254/fr.hpp" -#include "barretenberg/proof_system/relations/decomposition_relation.hpp" -#include "barretenberg/proof_system/relations/extra_relations.hpp" -#include "barretenberg/proof_system/relations/gen_perm_sort_relation.hpp" -#include "barretenberg/proof_system/relations/non_native_field_relation.hpp" -#include "barretenberg/proof_system/relations/permutation_relation.hpp" -#include "decomposition_relation.hpp" -#include "extra_relations.hpp" +#include "barretenberg/proof_system/relations/relation_parameters.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_decomposition_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_extra_relations.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_gen_perm_sort_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_non_native_field_relation.hpp" +#include "barretenberg/proof_system/relations/translator_vm/translator_permutation_relation.hpp" #include using namespace proof_system; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/decomposition_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_decomposition_relation.hpp similarity index 99% rename from barretenberg/cpp/src/barretenberg/proof_system/relations/decomposition_relation.hpp rename to barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_decomposition_relation.hpp index f26b3075566..c0a81d50d19 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/decomposition_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_decomposition_relation.hpp @@ -1,7 +1,6 @@ #pragma once #include "barretenberg/numeric/uint256/uint256.hpp" -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -12,7 +11,7 @@ template class GoblinTranslatorDecompositionRelationImpl { // 1 + polynomial degree of this relation static constexpr size_t RELATION_LENGTH = 3; // degree(lagrange_odd_in_minicircuit_in_minicircuit(a - a_0 - a_1*2¹⁴ ... - a_l⋅2¹⁴ˡ )) = 2 - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 3, // decomposition of P.x limb 0 into microlimbs subrelation 3, // decomposition of P.x limb 1 into microlimbs subrelation 3, // decomposition of P.x limb 2 into microlimbs subrelation @@ -81,10 +80,10 @@ template class GoblinTranslatorDecompositionRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template + template inline static void accumulate(ContainerOverSubrelations& accumulators, const AllEntities& in, - const RelationParameters&, + const Parameters&, const FF& scaling_factor) { static constexpr size_t NUM_LIMB_BITS = 68; // Number of bits in a standard limb used for bigfield operations diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/extra_relations.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_extra_relations.hpp similarity index 87% rename from barretenberg/cpp/src/barretenberg/proof_system/relations/extra_relations.hpp rename to barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_extra_relations.hpp index bb03ae5913b..00b99bdb34f 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/extra_relations.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_extra_relations.hpp @@ -1,7 +1,6 @@ #pragma once #include "barretenberg/numeric/uint256/uint256.hpp" -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -11,7 +10,7 @@ template class GoblinTranslatorOpcodeConstraintRelationImpl { // 1 + polynomial degree of this relation static constexpr size_t RELATION_LENGTH = 7; // degree(op(op - 1)(op - 2)(op - 3)(op - 4)(op - 8)) = 6 - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 7 // opcode constraint relation }; @@ -26,11 +25,11 @@ template class GoblinTranslatorOpcodeConstraintRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& accumulators, - const AllEntitites& in, - const RelationParameters&, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters&, + const FF& scaling_factor) { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; @@ -60,7 +59,7 @@ template class GoblinTranslatorAccumulatorTransferRelationImpl { // 1 + polynomial degree of this relation static constexpr size_t RELATION_LENGTH = 3; // degree((SOME_LAGRANGE)(A-B)) = 2 - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 3, // transfer accumulator limb 0 at even index subrelation 3, // transfer accumulator limb 1 at even index subrelation 3, // transfer accumulator limb 2 at even index subrelation @@ -89,11 +88,11 @@ template class GoblinTranslatorAccumulatorTransferRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& accumulators, - const AllEntities& in, - const RelationParameters& relation_parameters, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters& params, + const FF& scaling_factor) { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; using View = typename Accumulator::View; @@ -159,22 +158,22 @@ template class GoblinTranslatorAccumulatorTransferRelationImpl { // Contribution (9) (9-12 ensure the output is as stated, we basically use this to get the result out of the // proof) - auto tmp_9 = (accumulators_binary_limbs_0 - relation_parameters.accumulated_result[0]) * lagrange_second; + auto tmp_9 = (accumulators_binary_limbs_0 - params.accumulated_result[0]) * lagrange_second; tmp_9 *= scaling_factor; std::get<8>(accumulators) += tmp_9; // Contribution (10) - auto tmp_10 = (accumulators_binary_limbs_1 - relation_parameters.accumulated_result[1]) * lagrange_second; + auto tmp_10 = (accumulators_binary_limbs_1 - params.accumulated_result[1]) * lagrange_second; tmp_10 *= scaling_factor; std::get<9>(accumulators) += tmp_10; // Contribution (11) - auto tmp_11 = (accumulators_binary_limbs_2 - relation_parameters.accumulated_result[2]) * lagrange_second; + auto tmp_11 = (accumulators_binary_limbs_2 - params.accumulated_result[2]) * lagrange_second; tmp_11 *= scaling_factor; std::get<10>(accumulators) += tmp_11; // Contribution (12) - auto tmp_12 = (accumulators_binary_limbs_3 - relation_parameters.accumulated_result[3]) * lagrange_second; + auto tmp_12 = (accumulators_binary_limbs_3 - params.accumulated_result[3]) * lagrange_second; tmp_12 *= scaling_factor; std::get<11>(accumulators) += tmp_12; }; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_gen_perm_sort_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_gen_perm_sort_relation.hpp new file mode 100644 index 00000000000..365c6044c36 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_gen_perm_sort_relation.hpp @@ -0,0 +1,138 @@ +#pragma once +#include "barretenberg/proof_system/relations/relation_types.hpp" + +namespace proof_system { + +template class GoblinTranslatorGenPermSortRelationImpl { + public: + using FF = FF_; + + // 1 + polynomial degree of this relation + static constexpr size_t RELATION_LENGTH = 6; // degree((lagrange_last-1) * D(D - 1)(D - 2)(D - 3)) = 5 + + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ + 6, // ordered_range_constraints_0 step in {0,1,2,3} subrelation + 6, // ordered_range_constraints_1 step in {0,1,2,3} subrelation + 6, // ordered_range_constraints_2 step in {0,1,2,3} subrelation + 6, // ordered_range_constraints_3 step in {0,1,2,3} subrelation + 6, // ordered_range_constraints_4 step in {0,1,2,3} subrelation + 3, // ordered_range_constraints_0 ends with defined maximum value subrelation + 3, // ordered_range_constraints_1 ends with defined maximum value subrelation + 3, // ordered_range_constraints_2 ends with defined maximum value subrelation + 3, // ordered_range_constraints_3 ends with defined maximum value subrelation + 3 // ordered_range_constraints_4 ends with defined maximum value subrelation + + }; + + /** + * @brief Expression for the generalized permutation sort relation + * + * @details The relation enforces 2 constraints on each of the ordered_range_constraints wires: + * 1) 2 sequential values are non-descending and have a difference of at most 3, except for the value at last index + * 2) The value at last index is 2¹⁴ - 1 + * + * @param evals transformed to `evals + C(in(X)...)*scaling_factor` + * @param in an std::array containing the fully extended Univariate edges. + * @param parameters contains beta, gamma, and public_input_delta, .... + * @param scaling_factor optional term to scale the evaluation before adding to evals. + */ + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters&, + const FF& scaling_factor) + { + using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; + using View = typename Accumulator::View; + auto ordered_range_constraints_0 = View(in.ordered_range_constraints_0); + auto ordered_range_constraints_1 = View(in.ordered_range_constraints_1); + auto ordered_range_constraints_2 = View(in.ordered_range_constraints_2); + auto ordered_range_constraints_3 = View(in.ordered_range_constraints_3); + auto ordered_range_constraints_4 = View(in.ordered_range_constraints_4); + auto ordered_range_constraints_0_shift = View(in.ordered_range_constraints_0_shift); + auto ordered_range_constraints_1_shift = View(in.ordered_range_constraints_1_shift); + auto ordered_range_constraints_2_shift = View(in.ordered_range_constraints_2_shift); + auto ordered_range_constraints_3_shift = View(in.ordered_range_constraints_3_shift); + auto ordered_range_constraints_4_shift = View(in.ordered_range_constraints_4_shift); + auto lagrange_last = View(in.lagrange_last); + + static const FF minus_one = FF(-1); + static const FF minus_two = FF(-2); + static const FF minus_three = FF(-3); + static const size_t micro_limb_bits = 14; + static const auto maximum_sort_value = -FF((1 << micro_limb_bits) - 1); + // Compute wire differences + auto delta_1 = ordered_range_constraints_0_shift - ordered_range_constraints_0; + auto delta_2 = ordered_range_constraints_1_shift - ordered_range_constraints_1; + auto delta_3 = ordered_range_constraints_2_shift - ordered_range_constraints_2; + auto delta_4 = ordered_range_constraints_3_shift - ordered_range_constraints_3; + auto delta_5 = ordered_range_constraints_4_shift - ordered_range_constraints_4; + + // Contribution (1) (contributions 1-5 ensure that the sequential values have a difference of {0,1,2,3}) + auto tmp_1 = delta_1; + tmp_1 *= (delta_1 + minus_one); + tmp_1 *= (delta_1 + minus_two); + tmp_1 *= (delta_1 + minus_three); + tmp_1 *= (lagrange_last + minus_one); + tmp_1 *= scaling_factor; + std::get<0>(accumulators) += tmp_1; + + // Contribution (2) + auto tmp_2 = delta_2; + tmp_2 *= (delta_2 + minus_one); + tmp_2 *= (delta_2 + minus_two); + tmp_2 *= (delta_2 + minus_three); + tmp_2 *= (lagrange_last + minus_one); + tmp_2 *= scaling_factor; + + std::get<1>(accumulators) += tmp_2; + + // Contribution (3) + auto tmp_3 = delta_3; + tmp_3 *= (delta_3 + minus_one); + tmp_3 *= (delta_3 + minus_two); + tmp_3 *= (delta_3 + minus_three); + tmp_3 *= (lagrange_last + minus_one); + tmp_3 *= scaling_factor; + std::get<2>(accumulators) += tmp_3; + + // Contribution (4) + auto tmp_4 = delta_4; + tmp_4 *= (delta_4 + minus_one); + tmp_4 *= (delta_4 + minus_two); + tmp_4 *= (delta_4 + minus_three); + tmp_4 *= (lagrange_last + minus_one); + tmp_4 *= scaling_factor; + std::get<3>(accumulators) += tmp_4; + + // Contribution (5) + auto tmp_5 = delta_5; + tmp_5 *= (delta_5 + minus_one); + tmp_5 *= (delta_5 + minus_two); + tmp_5 *= (delta_5 + minus_three); + tmp_5 *= (lagrange_last + minus_one); + tmp_5 *= scaling_factor; + std::get<4>(accumulators) += tmp_5; + + // Contribution (6) (Contributions 6-10 ensure that the last value is the designated maximum value. We don't + // need to constrain the first value to be 0, because the shift mechanic does this for us) + std::get<5>(accumulators) += + lagrange_last * (ordered_range_constraints_0 + maximum_sort_value) * scaling_factor; + // Contribution (7) + std::get<6>(accumulators) += + lagrange_last * (ordered_range_constraints_1 + maximum_sort_value) * scaling_factor; + // Contribution (8) + std::get<7>(accumulators) += + lagrange_last * (ordered_range_constraints_2 + maximum_sort_value) * scaling_factor; + // Contribution (9) + std::get<8>(accumulators) += + lagrange_last * (ordered_range_constraints_3 + maximum_sort_value) * scaling_factor; + // Contribution (10) + std::get<9>(accumulators) += + lagrange_last * (ordered_range_constraints_4 + maximum_sort_value) * scaling_factor; + }; +}; +template +using GoblinTranslatorGenPermSortRelation = Relation>; + +} // namespace proof_system diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/non_native_field_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_non_native_field_relation.hpp similarity index 87% rename from barretenberg/cpp/src/barretenberg/proof_system/relations/non_native_field_relation.hpp rename to barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_non_native_field_relation.hpp index f992a12b7a7..2ba6be4eb95 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/non_native_field_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_non_native_field_relation.hpp @@ -1,8 +1,7 @@ #pragma once #include "barretenberg/ecc/curves/bn254/bn254.hpp" #include "barretenberg/numeric/uintx/uintx.hpp" -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -11,7 +10,7 @@ template class GoblinTranslatorNonNativeFieldRelationImpl { using FF = FF_; // 1 + polynomial degree of this relation - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 3, // Lower wide limb subrelation (checks result is 0 mod 2¹³⁶) 3, // Higher wide limb subrelation (checks result is 0 in higher mod 2¹³⁶), 3 // Prime subrelation (checks result in native field) @@ -79,11 +78,11 @@ template class GoblinTranslatorNonNativeFieldRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& accumulators, - const AllEntitites& in, - const RelationParameters& relation_parameters, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters& params, + const FF& scaling_factor) { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; @@ -104,33 +103,33 @@ template class GoblinTranslatorNonNativeFieldRelationImpl { -FF(curve::BN254::BaseField::modulus) }; - const auto& evaluation_input_x_0 = relation_parameters.evaluation_input_x[0]; - const auto& evaluation_input_x_1 = relation_parameters.evaluation_input_x[1]; - const auto& evaluation_input_x_2 = relation_parameters.evaluation_input_x[2]; - const auto& evaluation_input_x_3 = relation_parameters.evaluation_input_x[3]; - const auto& evaluation_input_x_4 = relation_parameters.evaluation_input_x[4]; + const auto& evaluation_input_x_0 = params.evaluation_input_x[0]; + const auto& evaluation_input_x_1 = params.evaluation_input_x[1]; + const auto& evaluation_input_x_2 = params.evaluation_input_x[2]; + const auto& evaluation_input_x_3 = params.evaluation_input_x[3]; + const auto& evaluation_input_x_4 = params.evaluation_input_x[4]; // for j < 4, v_i_j is the j-th limb of v^{1+i} // v_i_4 is v^{1+i} in the native field - const auto& v_0_0 = relation_parameters.batching_challenge_v[0][0]; - const auto& v_0_1 = relation_parameters.batching_challenge_v[0][1]; - const auto& v_0_2 = relation_parameters.batching_challenge_v[0][2]; - const auto& v_0_3 = relation_parameters.batching_challenge_v[0][3]; - const auto& v_0_4 = relation_parameters.batching_challenge_v[0][4]; - const auto& v_1_0 = relation_parameters.batching_challenge_v[1][0]; - const auto& v_1_1 = relation_parameters.batching_challenge_v[1][1]; - const auto& v_1_2 = relation_parameters.batching_challenge_v[1][2]; - const auto& v_1_3 = relation_parameters.batching_challenge_v[1][3]; - const auto& v_1_4 = relation_parameters.batching_challenge_v[1][4]; - const auto& v_2_0 = relation_parameters.batching_challenge_v[2][0]; - const auto& v_2_1 = relation_parameters.batching_challenge_v[2][1]; - const auto& v_2_2 = relation_parameters.batching_challenge_v[2][2]; - const auto& v_2_3 = relation_parameters.batching_challenge_v[2][3]; - const auto& v_2_4 = relation_parameters.batching_challenge_v[2][4]; - const auto& v_3_0 = relation_parameters.batching_challenge_v[3][0]; - const auto& v_3_1 = relation_parameters.batching_challenge_v[3][1]; - const auto& v_3_2 = relation_parameters.batching_challenge_v[3][2]; - const auto& v_3_3 = relation_parameters.batching_challenge_v[3][3]; - const auto& v_3_4 = relation_parameters.batching_challenge_v[3][4]; + const auto& v_0_0 = params.batching_challenge_v[0][0]; + const auto& v_0_1 = params.batching_challenge_v[0][1]; + const auto& v_0_2 = params.batching_challenge_v[0][2]; + const auto& v_0_3 = params.batching_challenge_v[0][3]; + const auto& v_0_4 = params.batching_challenge_v[0][4]; + const auto& v_1_0 = params.batching_challenge_v[1][0]; + const auto& v_1_1 = params.batching_challenge_v[1][1]; + const auto& v_1_2 = params.batching_challenge_v[1][2]; + const auto& v_1_3 = params.batching_challenge_v[1][3]; + const auto& v_1_4 = params.batching_challenge_v[1][4]; + const auto& v_2_0 = params.batching_challenge_v[2][0]; + const auto& v_2_1 = params.batching_challenge_v[2][1]; + const auto& v_2_2 = params.batching_challenge_v[2][2]; + const auto& v_2_3 = params.batching_challenge_v[2][3]; + const auto& v_2_4 = params.batching_challenge_v[2][4]; + const auto& v_3_0 = params.batching_challenge_v[3][0]; + const auto& v_3_1 = params.batching_challenge_v[3][1]; + const auto& v_3_2 = params.batching_challenge_v[3][2]; + const auto& v_3_3 = params.batching_challenge_v[3][3]; + const auto& v_3_4 = params.batching_challenge_v[3][4]; const auto& op = View(in.op); const auto& p_x_low_limbs = View(in.p_x_low_limbs); diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_permutation_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_permutation_relation.hpp new file mode 100644 index 00000000000..448b9cc5b33 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/translator_vm/translator_permutation_relation.hpp @@ -0,0 +1,114 @@ +#pragma once +#include "barretenberg/proof_system/relations/relation_types.hpp" + +namespace proof_system { + +template class GoblinTranslatorPermutationRelationImpl { + public: + using FF = FF_; + // 1 + polynomial degree of this relation + static constexpr size_t RELATION_LENGTH = 7; + + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ + 7, // grand product construction sub-relation + 3 // left-shiftable polynomial sub-relation + }; + + inline static auto& get_grand_product_polynomial(auto& in) { return in.z_perm; } + inline static auto& get_shifted_grand_product_polynomial(auto& in) { return in.z_perm_shift; } + + template + inline static Accumulator compute_grand_product_numerator(const AllEntities& in, const Parameters& params) + { + using View = typename Accumulator::View; + + auto concatenated_range_constraints_0 = View(in.concatenated_range_constraints_0); + auto concatenated_range_constraints_1 = View(in.concatenated_range_constraints_1); + auto concatenated_range_constraints_2 = View(in.concatenated_range_constraints_2); + auto concatenated_range_constraints_3 = View(in.concatenated_range_constraints_3); + + auto ordered_extra_range_constraints_numerator = View(in.ordered_extra_range_constraints_numerator); + + const auto& gamma = params.gamma; + return (concatenated_range_constraints_0 + gamma) * (concatenated_range_constraints_1 + gamma) * + (concatenated_range_constraints_2 + gamma) * (concatenated_range_constraints_3 + gamma) * + (ordered_extra_range_constraints_numerator + gamma); + } + + template + inline static Accumulator compute_grand_product_denominator(const AllEntities& in, const Parameters& params) + { + using View = typename Accumulator::View; + + auto ordered_range_constraints_0 = View(in.ordered_range_constraints_0); + auto ordered_range_constraints_1 = View(in.ordered_range_constraints_1); + auto ordered_range_constraints_2 = View(in.ordered_range_constraints_2); + auto ordered_range_constraints_3 = View(in.ordered_range_constraints_3); + auto ordered_range_constraints_4 = View(in.ordered_range_constraints_4); + + const auto& gamma = params.gamma; + + return (ordered_range_constraints_0 + gamma) * (ordered_range_constraints_1 + gamma) * + (ordered_range_constraints_2 + gamma) * (ordered_range_constraints_3 + gamma) * + (ordered_range_constraints_4 + gamma); + } + /** + * @brief Compute contribution of the goblin translator permutation relation for a given edge (internal function) + * + * @details There are 2 relations associated with enforcing the set permutation relation + * This file handles the relation that confirms faithful calculation of the grand + * product polynomial Z_perm. + * + * C(in(X)...) = + * ( z_perm(X) + lagrange_first(X) )*P(X) + * - ( z_perm_shift(X) + lagrange_last(X))*Q(X), + * where P(X) = Prod_{i=0:4} numerator_polynomial_i(X) + γ + * Q(X) = Prod_{i=0:4} ordered_range_constraint_i(X) + γ + * the first 4 numerator polynomials are concatenated range constraint polynomials and the last one is the constant + * extra numerator + * + * @param evals transformed to `evals + C(in(X)...)*scaling_factor` + * @param in an std::array containing the fully extended Univariate edges. + * @param parameters contains beta, gamma, and public_input_delta, .... + * @param scaling_factor optional term to scale the evaluation before adding to evals. + */ + + template + inline static void accumulate(ContainerOverSubrelations& accumulators, + const AllEntities& in, + const Parameters& params, + const FF& scaling_factor) + { + [&]() { + using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; + using View = typename Accumulator::View; + + auto z_perm = View(in.z_perm); + auto z_perm_shift = View(in.z_perm_shift); + auto lagrange_first = View(in.lagrange_first); + auto lagrange_last = View(in.lagrange_last); + + // Contribution (1) + std::get<0>(accumulators) += + (((z_perm + lagrange_first) * compute_grand_product_numerator(in, params)) - + ((z_perm_shift + lagrange_last) * compute_grand_product_denominator(in, params))) * + scaling_factor; + }(); + + [&]() { + using Accumulator = std::tuple_element_t<1, ContainerOverSubrelations>; + using View = typename Accumulator::View; + + auto z_perm_shift = View(in.z_perm_shift); + auto lagrange_last = View(in.lagrange_last); + + // Contribution (2) + std::get<1>(accumulators) += (lagrange_last * z_perm_shift) * scaling_factor; + }(); + }; +}; + +template +using GoblinTranslatorPermutationRelation = Relation>; + +} // namespace proof_system diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp index 9ac92c227f9..957b6baaa36 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp @@ -1,6 +1,5 @@ #pragma once -#include "relation_parameters.hpp" -#include "relation_types.hpp" +#include "barretenberg/proof_system/relations/relation_types.hpp" namespace proof_system { @@ -8,7 +7,7 @@ template class UltraArithmeticRelationImpl { public: using FF = FF_; - static constexpr std::array SUBRELATION_LENGTHS{ + static constexpr std::array SUBRELATION_PARTIAL_LENGTHS{ 6, // primary arithmetic sub-relation 5 // secondary arithmetic sub-relation }; @@ -64,11 +63,11 @@ template class UltraArithmeticRelationImpl { * @param parameters contains beta, gamma, and public_input_delta, .... * @param scaling_factor optional term to scale the evaluation before adding to evals. */ - template - void static accumulate(ContainerOverSubrelations& evals, - const AllEntities& in, - const RelationParameters&, - const FF& scaling_factor) + template + inline static void accumulate(ContainerOverSubrelations& evals, + const AllEntities& in, + const Parameters&, + const FF& scaling_factor) { { using Accumulator = std::tuple_element_t<0, ContainerOverSubrelations>; diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_relation_consistency.test.cpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_relation_consistency.test.cpp index acec6116c83..6cd80499c4d 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_relation_consistency.test.cpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/ultra_relation_consistency.test.cpp @@ -18,6 +18,7 @@ #include "barretenberg/proof_system/relations/gen_perm_sort_relation.hpp" #include "barretenberg/proof_system/relations/lookup_relation.hpp" #include "barretenberg/proof_system/relations/permutation_relation.hpp" +#include "barretenberg/proof_system/relations/relation_parameters.hpp" #include "barretenberg/proof_system/relations/ultra_arithmetic_relation.hpp" #include diff --git a/barretenberg/cpp/src/barretenberg/proof_system/relations/utils.hpp b/barretenberg/cpp/src/barretenberg/proof_system/relations/utils.hpp index eb4fb3a56af..d4a0a9f5e4c 100644 --- a/barretenberg/cpp/src/barretenberg/proof_system/relations/utils.hpp +++ b/barretenberg/cpp/src/barretenberg/proof_system/relations/utils.hpp @@ -121,8 +121,8 @@ template class RelationUtils { * @param tuple A tuple of tuples of Univariates * @param result A Univariate of length extended_size */ - template - static void extend_and_batch_univariates(const OfTuplesOfUnivariates& tuple, + template + static void extend_and_batch_univariates(const TupleOfTuplesOfUnivariates& tuple, const PowUnivariate& pow_univariate, ExtendedUnivariate& result) { @@ -175,12 +175,12 @@ template class RelationUtils { * together, with appropriate scaling factors, produces the expected value of the full Honk relation. This value is * checked against the final value of the target total sum (called sigma_0 in the thesis). */ - template + template // TODO(#224)(Cody): Input should be an array? - static void accumulate_relation_evaluations(PolynomialEvaluations evaluations, - RelationEvaluations& relation_evaluations, - const proof_system::RelationParameters& relation_parameters, - const FF& partial_evaluation_constant) + inline static void accumulate_relation_evaluations(PolynomialEvaluations evaluations, + RelationEvaluations& relation_evaluations, + const Parameters& relation_parameters, + const FF& partial_evaluation_constant) { using Relation = std::tuple_element_t; Relation::accumulate(std::get(relation_evaluations), @@ -190,7 +190,7 @@ template class RelationUtils { // Repeat for the next relation. if constexpr (relation_idx + 1 < NUM_RELATIONS) { - accumulate_relation_evaluations( + accumulate_relation_evaluations( evaluations, relation_evaluations, relation_parameters, partial_evaluation_constant); } }