Skip to content

Commit

Permalink
add RelationWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ledwards2225 committed Jun 12, 2023
1 parent 17ace62 commit 267f67d
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 204 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,14 @@

namespace proof_system::honk::sumcheck {

template <typename FF> class ArithmeticRelation {
template <typename FF> class ArithmeticRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 4;

static constexpr size_t LEN_1 = 4; // arithmetic sub-relation
using LENGTHS = LengthsWrapper<LEN_1>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Expression for the StandardArithmetic gate.
* @details The relation is defined as C(extended_edges(X)...) =
Expand All @@ -33,10 +27,10 @@ template <typename FF> class ArithmeticRelation {
* @param scaling_factor optional term to scale the evaluation before adding to evals.
*/
template <typename TypeMuncher>
void add_edge_contribution_impl(typename TypeMuncher::Accumulators& accumulator,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor) const
void static add_edge_contribution_impl(typename TypeMuncher::Accumulators& accumulator,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor)
{
// OPTIMIZATION?: Karatsuba in general, at least for some degrees?
// See https://hackmd.io/xGLuj6biSsCjzQnYN-pEiA?both
Expand All @@ -58,21 +52,7 @@ template <typename FF> class ArithmeticRelation {
tmp *= scaling_factor;
std::get<0>(accumulator) += tmp;
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
};

template <typename FF> using ArithmeticRelation = RelationWrapper<FF, ArithmeticRelationBase>;
} // namespace proof_system::honk::sumcheck
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace proof_system::honk::sumcheck {

template <typename FF> class AuxiliaryRelation {
template <typename FF> class AuxiliaryRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 6;
Expand All @@ -22,12 +22,6 @@ template <typename FF> class AuxiliaryRelation {
static constexpr size_t LEN_6 = 6; // RAM consistency sub-relation 3
using LENGTHS = LengthsWrapper<LEN_1, LEN_2, LEN_3, LEN_4, LEN_5, LEN_6>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Expression for the generalized permutation sort gate.
* @details The following explanation is reproduced from the Plonk analog 'plookup_auxiliary_widget':
Expand Down Expand Up @@ -298,21 +292,7 @@ template <typename FF> class AuxiliaryRelation {
auxiliary_identity *= (q_aux * scaling_factor);
std::get<0>(accumulators) += auxiliary_identity;
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
};

template <typename FF> using AuxiliaryRelation = RelationWrapper<FF, AuxiliaryRelationBase>;
} // namespace proof_system::honk::sumcheck
35 changes: 8 additions & 27 deletions cpp/src/barretenberg/honk/sumcheck/relations/elliptic_relation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace proof_system::honk::sumcheck {

template <typename FF> class EllipticRelation {
template <typename FF> class EllipticRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 6; // degree(q_elliptic * q_beta * x^3) = 5
Expand All @@ -17,12 +17,6 @@ template <typename FF> class EllipticRelation {
static constexpr size_t LEN_2 = 5; // y-coordinate sub-relation
using LENGTHS = LengthsWrapper<LEN_1, LEN_2>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Expression for the Ultra Arithmetic gate.
* @details The relation is defined as C(extended_edges(X)...) =
Expand All @@ -34,10 +28,10 @@ template <typename FF> class EllipticRelation {
* @param scaling_factor optional term to scale the evaluation before adding to evals.
*/
template <typename TypeMuncher>
void add_edge_contribution_impl(typename TypeMuncher::Accumulators& accumulators,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor) const {
static void add_edge_contribution_impl(typename TypeMuncher::Accumulators& accumulators,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor){
// OPTIMIZATION?: Karatsuba in general, at least for some degrees?
// See https://hackmd.io/xGLuj6biSsCjzQnYN-pEiA?both
// TODO(luke): Formatter doesnt properly handle explicit scoping below so turning off. Whats up?
Expand Down Expand Up @@ -102,22 +96,9 @@ template <typename FF> class EllipticRelation {
std::get<1>(accumulators) += y_identity;
}
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
};

template <typename FF>
using EllipticRelation = RelationWrapper<FF, EllipticRelationBase>;
// clang-format on
} // namespace proof_system::honk::sumcheck
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace proof_system::honk::sumcheck {

template <typename FF> class GenPermSortRelation {
template <typename FF> class GenPermSortRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 6; // degree(q_sort * D(D - 1)(D - 2)(D - 3)) = 5
Expand All @@ -19,12 +19,6 @@ template <typename FF> class GenPermSortRelation {
static constexpr size_t LEN_4 = 6; // range constrain sub-relation 4
using LENGTHS = LengthsWrapper<LEN_1, LEN_2, LEN_3, LEN_4>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Expression for the generalized permutation sort gate.
* @details The relation is defined as C(extended_edges(X)...) =
Expand All @@ -41,10 +35,10 @@ template <typename FF> class GenPermSortRelation {
* @param scaling_factor optional term to scale the evaluation before adding to evals.
*/
template <typename TypeMuncher>
void add_edge_contribution_impl(typename TypeMuncher::Accumulators& accumulators,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor) const
void static add_edge_contribution_impl(typename TypeMuncher::Accumulators& accumulators,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor)
{
// OPTIMIZATION?: Karatsuba in general, at least for some degrees?
// See https://hackmd.io/xGLuj6biSsCjzQnYN-pEiA?both
Expand Down Expand Up @@ -103,21 +97,8 @@ template <typename FF> class GenPermSortRelation {
tmp_4 *= scaling_factor;
std::get<3>(accumulators) += tmp_4;
};
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
template <typename FF> using GenPermSortRelation = RelationWrapper<FF, GenPermSortRelationBase>;

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
};
} // namespace proof_system::honk::sumcheck
25 changes: 3 additions & 22 deletions cpp/src/barretenberg/honk/sumcheck/relations/lookup_relation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace proof_system::honk::sumcheck {

template <typename FF> class LookupRelation {
template <typename FF> class LookupRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 6; // deg(z_lookup * column_selector * wire * q_lookup * table) = 5
Expand All @@ -17,12 +17,6 @@ template <typename FF> class LookupRelation {
static constexpr size_t LEN_2 = 3; // left-shiftable polynomial sub-relation
using LENGTHS = LengthsWrapper<LEN_1, LEN_2>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Compute contribution of the lookup grand prod relation for a given edge (internal function)
*
Expand Down Expand Up @@ -120,21 +114,8 @@ template <typename FF> class LookupRelation {
std::get<1>(accumulators) += (lagrange_last * z_lookup_shift) * scaling_factor;
}
};
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
template <typename FF> using LookupRelation = RelationWrapper<FF, LookupRelationBase>;

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
};
} // namespace proof_system::honk::sumcheck
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace proof_system::honk::sumcheck {

template <typename FF> class PermutationRelation {
template <typename FF> class PermutationRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 5;
Expand All @@ -15,12 +15,6 @@ template <typename FF> class PermutationRelation {
static constexpr size_t LEN_2 = 3; // left-shiftable polynomial sub-relation
using LENGTHS = LengthsWrapper<LEN_1, LEN_2>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Compute contribution of the permutation relation for a given edge (internal function)
*
Expand Down Expand Up @@ -82,35 +76,11 @@ template <typename FF> class PermutationRelation {
std::get<1>(accumulator) += (lagrange_last * z_perm_shift) * scaling_factor;
}
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}

/**
* @brief Add the result of each identity in this relation evaluated at the multivariate evaluations produced by the
* Sumcheck Prover.
*
* @param full_honk_relation_value
* @param purported_evaluations
*/
};

// TODO(luke): With Cody's Flavor work it should be easier to create a simple templated relation
// for handling arbitrary width. For now I'm duplicating the width 3 logic for width 4.
template <typename FF> class UltraPermutationRelation {
template <typename FF> class UltraPermutationRelationBase {
public:
// 1 + polynomial degree of this relation
static constexpr size_t RELATION_LENGTH = 6;
Expand All @@ -119,12 +89,6 @@ template <typename FF> class UltraPermutationRelation {
static constexpr size_t LEN_2 = 3; // left-shiftable polynomial sub-relation
using LENGTHS = LengthsWrapper<LEN_1, LEN_2>;

using UnivariateAccumTypes = UnivariateAccumulatorTypes<FF, LENGTHS>;
using ValueAccumTypes = ValueAccumulatorTypes<FF, LENGTHS>;

using RelationUnivariates = typename UnivariateAccumTypes::Accumulators;
using RelationValues = typename ValueAccumTypes::Accumulators;

/**
* @brief Compute contribution of the permutation relation for a given edge (internal function)
*
Expand Down Expand Up @@ -182,21 +146,9 @@ template <typename FF> class UltraPermutationRelation {
std::get<1>(accumulators) += (lagrange_last * z_perm_shift) * scaling_factor;
}
};
};

inline void add_edge_contribution(auto& accumulator,
const auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor) const
{
add_edge_contribution_impl<UnivariateAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
template <typename FF> using PermutationRelation = RelationWrapper<FF, PermutationRelationBase>;

void add_full_relation_value_contribution(RelationValues& accumulator,
auto& input,
const RelationParameters<FF>& relation_parameters,
const FF& scaling_factor = 1) const
{
add_edge_contribution_impl<ValueAccumTypes>(accumulator, input, relation_parameters, scaling_factor);
}
};
template <typename FF> using UltraPermutationRelation = RelationWrapper<FF, UltraPermutationRelationBase>;
} // namespace proof_system::honk::sumcheck
Loading

0 comments on commit 267f67d

Please sign in to comment.