Skip to content

Commit

Permalink
feat(honk): Shared relation arithmetic (AztecProtocol/barretenberg#514)
Browse files Browse the repository at this point in the history
* a working version of shared relation arithmetic

* add RelationWrapper
  • Loading branch information
ledwards2225 authored Jun 12, 2023
1 parent 4141f88 commit 79606e4
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 858 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class Standard {
using RelationUnivariates = decltype(create_relation_univariates_container<FF, Relations>());
using RelationValues = decltype(create_relation_values_container<FF, Relations>());

// define utilities to extend univarates from RELATION_LENGTH to MAX_RELATION_LENGTH for each Relation
// using BarycentricUtils = decltype(create_barycentric_utils<FF, Relations, MAX_RELATION_LENGTH>());

private:
/**
* @brief A base class labelling precomputed entities and (ordered) subsets of interest.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@

#include "../polynomials/univariate.hpp"
#include "relation_parameters.hpp"
#include "relation_types.hpp"

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 NUM_CONSTRAINTS = 1;
static constexpr std::array<size_t, NUM_CONSTRAINTS> CONSTRAINT_LENGTH = { 4 };

using RelationUnivariates = std::tuple<Univariate<FF, CONSTRAINT_LENGTH[0]>>;
using RelationValues = std::array<FF, NUM_CONSTRAINTS>;
static constexpr size_t LEN_1 = 4; // arithmetic sub-relation
using LENGTHS = LengthsWrapper<LEN_1>;

/**
* @brief Expression for the StandardArithmetic gate.
Expand All @@ -28,55 +26,33 @@ template <typename FF> class ArithmeticRelation {
* @param parameters contains beta, gamma, and public_input_delta, ....
* @param scaling_factor optional term to scale the evaluation before adding to evals.
*/
void add_edge_contribution(RelationUnivariates& evals,
const auto& extended_edges,
const RelationParameters<FF>&,
const FF& scaling_factor) const
template <typename TypeMuncher>
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

auto w_l = UnivariateView<FF, RELATION_LENGTH>(extended_edges.w_l);
auto w_r = UnivariateView<FF, RELATION_LENGTH>(extended_edges.w_r);
auto w_o = UnivariateView<FF, RELATION_LENGTH>(extended_edges.w_o);
auto q_m = UnivariateView<FF, RELATION_LENGTH>(extended_edges.q_m);
auto q_l = UnivariateView<FF, RELATION_LENGTH>(extended_edges.q_l);
auto q_r = UnivariateView<FF, RELATION_LENGTH>(extended_edges.q_r);
auto q_o = UnivariateView<FF, RELATION_LENGTH>(extended_edges.q_o);
auto q_c = UnivariateView<FF, RELATION_LENGTH>(extended_edges.q_c);
using View = typename std::tuple_element<0, typename TypeMuncher::AccumulatorViews>::type;
auto w_l = View(extended_edges.w_l);
auto w_r = View(extended_edges.w_r);
auto w_o = View(extended_edges.w_o);
auto q_m = View(extended_edges.q_m);
auto q_l = View(extended_edges.q_l);
auto q_r = View(extended_edges.q_r);
auto q_o = View(extended_edges.q_o);
auto q_c = View(extended_edges.q_c);

auto tmp = w_l * (q_m * w_r + q_l);
tmp += q_r * w_r;
tmp += q_o * w_o;
tmp += q_c;
tmp *= scaling_factor;
std::get<0>(evals) += tmp;
};

/**
* @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
*/
void add_full_relation_value_contribution(RelationValues& full_honk_relation_value,
const auto& purported_evaluations,
const RelationParameters<FF>&) const
{
auto w_l = purported_evaluations.w_l;
auto w_r = purported_evaluations.w_r;
auto w_o = purported_evaluations.w_o;
auto q_m = purported_evaluations.q_m;
auto q_l = purported_evaluations.q_l;
auto q_r = purported_evaluations.q_r;
auto q_o = purported_evaluations.q_o;
auto q_c = purported_evaluations.q_c;

std::get<0>(full_honk_relation_value) += w_l * (q_m * w_r + q_l);
std::get<0>(full_honk_relation_value) += q_r * w_r;
std::get<0>(full_honk_relation_value) += q_o * w_o;
std::get<0>(full_honk_relation_value) += q_c;
std::get<0>(accumulator) += tmp;
};
};

template <typename FF> using ArithmeticRelation = RelationWrapper<FF, ArithmeticRelationBase>;
} // namespace proof_system::honk::sumcheck
Loading

0 comments on commit 79606e4

Please sign in to comment.