Skip to content

Commit

Permalink
Different strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Rumata888 committed Apr 24, 2024
1 parent 3aca309 commit 70f78df
Show file tree
Hide file tree
Showing 10 changed files with 177 additions and 153 deletions.
6 changes: 6 additions & 0 deletions barretenberg/cpp/src/barretenberg/eccvm/eccvm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,12 @@ class ECCVMFlavor {
* @brief A container for univariates used during sumcheck.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
188 changes: 106 additions & 82 deletions barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ template <class ProverInstances_> class ProtoGalaxyProver_ {
// obtained by composing a relation with folded instance + relation parameters .
using ExtendedUnivariate = Univariate<FF, (Flavor::MAX_TOTAL_RELATION_LENGTH - 1) * (ProverInstances::NUM - 1) + 1>;
using OptimisedExtendedUnivariate =
Univariate<FF, (Flavor::MAX_TOTAL_RELATION_LENGTH - 2) * (ProverInstances::NUM - 1) + 1>;
Univariate<FF,
(Flavor::MAX_TOTAL_RELATION_LENGTH - 1) * (ProverInstances::NUM - 1) + 1,
0,
ProverInstances::NUM - 1>;
// Represents the total length of the combiner univariate, obtained by combining the already folded relations with
// the folded relation batching challenge.
using ExtendedUnivariateWithRandomization =
Univariate<FF,
(Flavor::MAX_TOTAL_RELATION_LENGTH - 1 + ProverInstances::NUM - 1) * (ProverInstances::NUM - 1) + 1>;
using ExtendedUnivariates = typename Flavor::template ProverUnivariates<ExtendedUnivariate::LENGTH>;
using OptimisedExtendedUnivariates =
typename Flavor::template ProverUnivariates<ExtendedUnivariate::LENGTH - (1 * (ProverInstances::NUM - 1))>;
typename Flavor::template OptimisedProverUnivariates<ExtendedUnivariate::LENGTH, ProverInstances::NUM - 1>;

using TupleOfTuplesOfUnivariates =
typename Flavor::template ProtogalaxyTupleOfTuplesOfUnivariates<ProverInstances::NUM>;
Expand Down Expand Up @@ -287,32 +290,14 @@ template <class ProverInstances_> class ProtoGalaxyProver_ {
* (i.e., compute additional evaluations at adjacent domain values) as needed.
* @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/751) Optimize memory
*/
void extend_univariates(ExtendedUnivariates& extended_univariates,
template <size_t skip_count = 0>
void extend_univariates(OptimisedExtendedUnivariates& extended_univariates,
const ProverInstances& instances,
const size_t row_idx)
{
auto base_univariates = instances.row_to_univariates(row_idx);
auto base_univariates = instances.template row_to_univariates<skip_count>(row_idx);
for (auto [extended_univariate, base_univariate] : zip_view(extended_univariates.get_all(), base_univariates)) {
extended_univariate = base_univariate.template extend_to<ExtendedUnivariate::LENGTH>();
}
}

/**
* @brief Prepare a univariate polynomial for relation execution in one step of the main loop in folded instance
* construction.
* @details For a fixed prover polynomial index, extract that polynomial from each instance in Instances. From each
* polynomial, extract the value at row_idx. Use these values to create a univariate polynomial, and then extend
* (i.e., compute additional evaluations at adjacent domain values) as needed.
* @todo TODO(https://github.com/AztecProtocol/barretenberg/issues/751) Optimize memory
*/
void optimised_extend_univariates(OptimisedExtendedUnivariates& extended_univariates,
const ProverInstances& instances,
const size_t row_idx)
{
auto base_univariates = instances.row_to_univariates(row_idx);
for (auto [extended_univariate, base_univariate] : zip_view(extended_univariates.get_all(), base_univariates)) {
extended_univariate =
base_univariate.template extend_to<OptimisedExtendedUnivariate::LENGTH, ProverInstances::NUM - 1>();
extended_univariate = base_univariate.template extend_to<ExtendedUnivariate::LENGTH, skip_count>();
}
}

Expand Down Expand Up @@ -371,7 +356,7 @@ template <class ProverInstances_> class ProtoGalaxyProver_ {

for (size_t idx = start; idx < end; idx++) {
// No need to initialise extended_univariates to 0, it's assigned to
optimised_extend_univariates(extended_univariates[thread_idx], instances, idx);
extend_univariates<ProverInstances::NUM - 1>(extended_univariates[thread_idx], instances, idx);

FF pow_challenge = pow_betas[idx];

Expand All @@ -391,31 +376,24 @@ template <class ProverInstances_> class ProtoGalaxyProver_ {
for (auto& accumulators : thread_univariate_accumulators) {
Utils::add_nested_tuples(optimised_univariate_accumulators, accumulators);
}
deoptimise_univariates(optimised_univariate_accumulators, univariate_accumulators);
zero_skipped_indices(optimised_univariate_accumulators);
// Batch the univariate contributions from each sub-relation to obtain the round univariate
return batch_over_relations(univariate_accumulators, instances.alphas);
}

static void deoptimise_univariates(OptimisedTupleOfTuplesOfUnivariates& optimised_univariate_accumulators,
TupleOfTuplesOfUnivariates& univariate_accumulators
static void zero_skipped_indices(OptimisedTupleOfTuplesOfUnivariates& optimised_univariate_accumulators

)
{
auto deoptimise = [&]<size_t outer_idx, size_t inner_idx>(auto& element) {
auto& optimised_element = std::get<inner_idx>(std::get<outer_idx>(optimised_univariate_accumulators));
static_assert(std::remove_reference_t<decltype(optimised_element)>::LENGTH + (ProverInstances::NUM - 1) ==
std::remove_reference_t<decltype(element)>::LENGTH);
element.evaluations[0] = optimised_element.evaluations[0];
// auto& optimised_element = std::get<inner_idx>(std::get<outer_idx>(optimised_univariate_accumulators));
for (size_t i = 1; i < ProverInstances::NUM; i++) {
element.evaluations[i] = FF(0);
}
for (size_t i = 1; i < std::remove_reference_t<decltype(optimised_element)>::LENGTH; i++) {
element.evaluations[i + ProverInstances::NUM - 1] = optimised_element.evaluations[i];
}
info("Element ", outer_idx, ".", inner_idx, "[", ":", "] = ", element);
// info("Element ", outer_idx, ".", inner_idx, "[", ":", "] = ", element);
};

Utils::template apply_to_tuple_of_tuples<0, 0>(univariate_accumulators, deoptimise);
Utils::template apply_to_tuple_of_tuples<0, 0>(optimised_univariate_accumulators, deoptimise);
}

static ExtendedUnivariateWithRandomization batch_over_relations(TupleOfTuplesOfUnivariates& univariate_accumulators,
Expand Down
24 changes: 18 additions & 6 deletions barretenberg/cpp/src/barretenberg/relations/nested_containers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,42 @@ namespace bb {
*
* @details Credit: https://stackoverflow.com/a/60440611
*/
template <template <typename, size_t, size_t> typename InnerContainer,
template <template <typename, size_t, size_t, size_t> typename InnerContainer,
typename ValueType,
auto domain_end,
size_t domain_start = 0,
size_t skip_count = 0,
typename IS = decltype(std::make_index_sequence<domain_end.size()>())>
struct TupleOfContainersOverArray;
template <template <typename, size_t, size_t> typename InnerContainer,
template <template <typename, size_t, size_t, size_t> typename InnerContainer,
typename ValueType,
auto domain_end,
size_t domain_start,
size_t skip_count,
std::size_t... I>
struct TupleOfContainersOverArray<InnerContainer, ValueType, domain_end, domain_start, std::index_sequence<I...>> {
using type = std::tuple<InnerContainer<ValueType, domain_end[I], domain_start>...>;
struct TupleOfContainersOverArray<InnerContainer,
ValueType,
domain_end,
domain_start,
skip_count,
std::index_sequence<I...>> {
using type = std::tuple<InnerContainer<ValueType, domain_end[I], domain_start, skip_count>...>;
};

// Helpers
template <typename ValueType, size_t, size_t> using ExtractValueType = ValueType;
template <typename ValueType, size_t, size_t, size_t> using ExtractValueType = ValueType;

template <typename Tuple>
using HomogeneousTupleToArray = std::array<std::tuple_element_t<0, Tuple>, std::tuple_size_v<Tuple>>;

// Types needed for sumcheck and folding.
template <typename FF, auto LENGTHS>
using TupleOfUnivariates = typename TupleOfContainersOverArray<bb::Univariate, FF, LENGTHS, 0>::type;
using TupleOfUnivariates = typename TupleOfContainersOverArray<bb::Univariate, FF, LENGTHS, 0, 0>::type;

// Types needed for sumcheck and folding.
template <typename FF, auto LENGTHS, size_t SKIP_COUNT>
using OptimisedTupleOfUnivariates =
typename TupleOfContainersOverArray<bb::Univariate, FF, LENGTHS, 0, SKIP_COUNT>::type;

template <typename FF, auto LENGTHS>
using TupleOfValues = typename TupleOfContainersOverArray<ExtractValueType, FF, LENGTHS>::type;
Expand Down
29 changes: 4 additions & 25 deletions barretenberg/cpp/src/barretenberg/relations/relation_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,6 @@ consteval std::array<size_t, NUM_SUBRELATIONS> compute_composed_subrelation_part
return 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.
* @tparam NUM_INSTANCES
* @tparam NUM_SUBRELATIONS
* @param SUBRELATION_PARTIAL_LENGTHS The array of subrelation lengths supplied by a relation.
* @return The transformed subrelation lenths
*/
template <size_t NUM_INSTANCES, size_t NUM_SUBRELATIONS>
consteval std::array<size_t, NUM_SUBRELATIONS> compute_optimised_composed_subrelation_partial_lengths(
std::array<size_t, NUM_SUBRELATIONS> SUBRELATION_PARTIAL_LENGTHS)
{
std::transform(SUBRELATION_PARTIAL_LENGTHS.begin(),
SUBRELATION_PARTIAL_LENGTHS.end(),
SUBRELATION_PARTIAL_LENGTHS.begin(),
[](const size_t x) { return (x - 2) * (NUM_INSTANCES - 1) + 1; });
return SUBRELATION_PARTIAL_LENGTHS;
};

/**
* @brief The templates defined herein facilitate sharing the relation arithmetic between the prover and the
* verifier.
Expand Down Expand Up @@ -173,9 +151,10 @@ template <typename RelationImpl> class Relation : public RelationImpl {
TupleOfUnivariates<FF, compute_composed_subrelation_partial_lengths<NUM_INSTANCES>(SUBRELATION_TOTAL_LENGTHS)>;
template <size_t NUM_INSTANCES>
using OptimisedProtogalaxyTupleOfUnivariatesOverSubrelations =
TupleOfUnivariates<FF,
compute_optimised_composed_subrelation_partial_lengths<NUM_INSTANCES>(
SUBRELATION_TOTAL_LENGTHS)>;
OptimisedTupleOfUnivariates<FF,
compute_composed_subrelation_partial_lengths<NUM_INSTANCES>(
SUBRELATION_TOTAL_LENGTHS),
NUM_INSTANCES - 1>;
using SumcheckTupleOfUnivariatesOverSubrelations =
TupleOfUnivariates<FF, RelationImpl::SUBRELATION_PARTIAL_LENGTHS>;
using SumcheckArrayOfValuesOverSubrelations = ArrayOfValues<FF, RelationImpl::SUBRELATION_PARTIAL_LENGTHS>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ class GoblinUltraFlavor {
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;

/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,12 @@ class UltraFlavor {
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ template <typename Flavor_, size_t NUM_ = 2> struct ProverInstances_ {
static constexpr size_t EXTENDED_LENGTH = (Flavor::MAX_TOTAL_RELATION_LENGTH - 1) * (NUM - 1) + 1;
static constexpr size_t BATCHED_EXTENDED_LENGTH = (Flavor::MAX_TOTAL_RELATION_LENGTH - 1 + NUM - 1) * (NUM - 1) + 1;
using RelationParameters = bb::RelationParameters<Univariate<FF, EXTENDED_LENGTH>>;
using OptimisedRelationParameters = bb::RelationParameters<Univariate<FF, EXTENDED_LENGTH - NUM + 1>>;
using OptimisedRelationParameters = bb::RelationParameters<Univariate<FF, EXTENDED_LENGTH, 0, NUM_ - 1>>;
using RelationSeparator = std::array<Univariate<FF, BATCHED_EXTENDED_LENGTH>, NUM_SUBRELATIONS - 1>;
ArrayType _data;
RelationParameters relation_parameters;
Expand Down Expand Up @@ -56,10 +56,10 @@ template <typename Flavor_, size_t NUM_ = 2> struct ProverInstances_ {
* @param row_idx A fixed row position in several execution traces
* @return The univariates whose extensions will be used to construct the combiner.
*/
auto row_to_univariates(size_t row_idx) const
template <size_t skip_count = 0> auto row_to_univariates(size_t row_idx) const
{
auto insts_prover_polynomials_views = get_polynomials_views();
std::array<Univariate<FF, NUM>, insts_prover_polynomials_views[0].size()> results;
std::array<Univariate<FF, NUM, 0, skip_count>, insts_prover_polynomials_views[0].size()> results;
// Set the size corresponding to the number of rows in the execution trace
size_t instance_idx = 0;
// Iterate over the prover polynomials' views corresponding to each instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,12 @@ class GoblinTranslatorFlavor {
* @brief A container for univariates used during sumcheck.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
6 changes: 6 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1707,6 +1707,12 @@ class AvmFlavor {
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down

0 comments on commit 70f78df

Please sign in to comment.