-
Notifications
You must be signed in to change notification settings - Fork 318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: flavor refactor, reduce duplication #3407
Changes from 49 commits
040a2b2
40e2a38
aaabc58
a64c2de
220999e
e956e96
750d5a8
82e0b57
ebf7527
12ad654
c83cf01
11cfe94
dd67fed
8884e24
24f1dc3
d398e2c
907e437
7621f6f
273d17f
bb923d9
f56bec6
9bb3354
560936e
3d10141
3dd704c
b716eef
a84cd10
821dfe9
73af46a
407fdb7
490cb77
9d8be5a
b3f1d7d
da73120
7a4ced3
87714ee
4fb3e68
f4ee388
6134f35
cfd5d27
0cc0385
0d83433
0d19f90
f4dae28
e2cf3b5
b939729
5ff03e7
a4abe50
638c020
7f0e26c
ad27d83
8dbeb8c
bbfdc84
bb25da5
7f0006d
699fcc7
2e1bac2
28be820
6289427
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#pragma once | ||
#include "barretenberg/common/ref_vector.hpp" | ||
#include "barretenberg/common/zip_view.hpp" | ||
#include "barretenberg/polynomials/polynomial.hpp" | ||
namespace proof_system::honk::pcs::zeromorph { | ||
|
@@ -321,7 +322,8 @@ template <typename Curve> class ZeroMorphProver_ { | |
auto& transcript, | ||
const std::vector<std::span<FF>>& concatenated_polynomials = {}, | ||
const std::vector<FF>& concatenated_evaluations = {}, | ||
const std::vector<std::vector<std::span<FF>>>& concatenation_groups = {}) | ||
// TODO(https://github.com/AztecProtocol/barretenberg/issues/743) remove span | ||
const std::vector<RefVector<std::span<FF>>>& concatenation_groups = {}) | ||
{ | ||
// Generate batching challenge \rho and powers 1,...,\rho^{m-1} | ||
FF rho = transcript.get_challenge("rho"); | ||
|
@@ -513,14 +515,14 @@ template <typename Curve> class ZeroMorphVerifier_ { | |
* @param concatenation_groups_commitments | ||
* @return Commitment | ||
*/ | ||
static Commitment compute_C_Z_x(std::vector<Commitment> f_commitments, | ||
std::vector<Commitment> g_commitments, | ||
static Commitment compute_C_Z_x(const std::vector<Commitment>& f_commitments, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We pass vector by value unnecessarily, causing full new vector allocations There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eek, thanks for catching and fixing. |
||
const std::vector<Commitment>& g_commitments, | ||
std::vector<Commitment>& C_q_k, | ||
FF rho, | ||
FF batched_evaluation, | ||
FF x_challenge, | ||
std::vector<FF> u_challenge, | ||
const std::vector<std::vector<Commitment>>& concatenation_groups_commitments = {}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new shape here is vector<RefVector<>>. This required some adapting in the tests |
||
const std::vector<RefVector<Commitment>>& concatenation_groups_commitments = {}) | ||
{ | ||
size_t log_N = C_q_k.size(); | ||
size_t N = 1 << log_N; | ||
|
@@ -611,7 +613,7 @@ template <typename Curve> class ZeroMorphVerifier_ { | |
* @brief Utility for native batch multiplication of group elements | ||
* @note This is used only for native verification and is not optimized for efficiency | ||
*/ | ||
static Commitment batch_mul_native(std::vector<Commitment> points, std::vector<FF> scalars) | ||
static Commitment batch_mul_native(const std::vector<Commitment>& points, const std::vector<FF>& scalars) | ||
{ | ||
auto result = points[0] * scalars[0]; | ||
for (size_t idx = 1; idx < scalars.size(); ++idx) { | ||
|
@@ -637,7 +639,7 @@ template <typename Curve> class ZeroMorphVerifier_ { | |
auto&& shifted_evaluations, | ||
auto& multivariate_challenge, | ||
auto& transcript, | ||
const std::vector<std::vector<Commitment>>& concatenation_group_commitments = {}, | ||
const std::vector<RefVector<Commitment>>& concatenation_group_commitments = {}, | ||
const std::vector<FF>& concatenated_evaluations = {}) | ||
{ | ||
size_t log_N = multivariate_challenge.size(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,7 +246,7 @@ template <class Curve> class ZeroMorphWithConcatenationTest : public CommitmentT | |
prover_transcript, | ||
concatenated_polynomials_views, | ||
c_evaluations, | ||
concatenation_groups_views); | ||
to_vector_of_ref_vectors(concatenation_groups_views)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to_vector_of_ref_vectors takes vector of vector to vector of RefVector |
||
|
||
auto verifier_transcript = BaseTranscript<Fr>::verifier_init_empty(prover_transcript); | ||
|
||
|
@@ -257,7 +257,7 @@ template <class Curve> class ZeroMorphWithConcatenationTest : public CommitmentT | |
w_evaluations, // shifted | ||
u_challenge, | ||
verifier_transcript, | ||
concatenation_groups_commitments, | ||
to_vector_of_ref_vectors(concatenation_groups_commitments), | ||
c_evaluations); | ||
|
||
verified = this->vk()->pairing_check(pairing_points[0], pairing_points[1]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
#include "barretenberg/common/assert.hpp" | ||
#include <array> | ||
#include <cstddef> | ||
#include <initializer_list> | ||
#include <iterator> | ||
#include <stdexcept> | ||
|
||
// TODO(https://github.com/AztecProtocol/barretenberg/issues/794) namespace this once convenient | ||
/** | ||
* @brief A template class for a reference array. Behaves as if std::array<T&, N> was possible. | ||
* | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not currently used, but originally I was using it and maybe it will come in handy? I thought that having compile time sized arrays was not worth the type complexity vs extra std::vector allocations/dynamicness, but in tight loops it certainly could be |
||
* This class provides a fixed-size array of pointers to elements of type T, exposed as references. | ||
* It offers random access to its elements and provides an iterator class | ||
* for traversal. | ||
* | ||
* @tparam T The type of elements stored in the array. | ||
* @tparam N The size of the array. | ||
*/ | ||
template <typename T, std::size_t N> class RefArray { | ||
public: | ||
RefArray(const std::array<T*, N>& ptr_array) | ||
{ | ||
std::size_t i = 0; | ||
for (T& elem : ptr_array) { | ||
storage[i++] = &elem; | ||
} | ||
} | ||
RefArray(std::initializer_list<T&> init) | ||
{ | ||
if (init.size() != N) { | ||
throw std::invalid_argument("Initializer list size does not match RefArray size"); | ||
} | ||
std::size_t i = 0; | ||
for (auto& elem : init) { | ||
storage[i++] = &elem; | ||
} | ||
} | ||
|
||
T& operator[](std::size_t idx) const | ||
{ | ||
ASSERT(idx < N); | ||
return *storage[idx]; | ||
} | ||
|
||
/** | ||
* @brief Nested iterator class for RefArray, based on indexing into the pointer array. | ||
* Provides semantics similar to what would be expected if std::array<T&, N> was possible. | ||
*/ | ||
class iterator { | ||
public: | ||
/** | ||
* @brief Constructs an iterator for a given RefArray object. | ||
* | ||
* @param array Pointer to the RefArray object. | ||
* @param pos The starting position in the array. | ||
*/ | ||
iterator(RefArray const* array, std::size_t pos) | ||
: array(array) | ||
, pos(pos) | ||
{} | ||
|
||
T& operator*() const { return (*array)[pos]; } | ||
|
||
iterator& operator++() | ||
{ | ||
pos++; | ||
return *this; | ||
} | ||
|
||
iterator operator++(int) | ||
{ | ||
iterator temp = *this; | ||
++(*this); | ||
return temp; | ||
} | ||
|
||
bool operator==(iterator const& other) const { return pos == other.pos; } | ||
bool operator!=(iterator const& other) const { return pos != other.pos; } | ||
|
||
private: | ||
RefArray const* array; | ||
std::size_t pos; | ||
}; | ||
|
||
/** | ||
* @brief Returns an iterator to the beginning of the RefArray. | ||
* | ||
* @return An iterator to the first element. | ||
*/ | ||
iterator begin() const { return iterator(this, 0); } | ||
/** | ||
* @brief Returns an iterator to the end of the RefArray. | ||
* | ||
* @return An iterator to the element following the last element. | ||
*/ | ||
iterator end() const { return iterator(this, N); } | ||
|
||
private: | ||
// We are making a high-level array, for simplicity having a C array as backing makes sense. | ||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays) | ||
T* storage[N]; | ||
}; | ||
|
||
/** | ||
* @brief Deduction guide for the RefArray class. | ||
* Allows for RefArray {a, b, c} without explicit template params. | ||
*/ | ||
template <typename T, typename... Ts> RefArray(T&, Ts&...) -> RefArray<T, 1 + sizeof...(Ts)>; | ||
|
||
/** | ||
* @brief Concatenates multiple RefArray objects into a single RefArray. | ||
* | ||
* This function takes multiple RefArray objects as input and concatenates them into a single | ||
* RefArray. | ||
|
||
* @tparam T The type of elements in the RefArray. | ||
* @tparam Ns The sizes of the input RefArrays. | ||
* @param ref_arrays The RefArray objects to be concatenated. | ||
* @return RefArray object containing all elements from the input arrays. | ||
*/ | ||
template <typename T, std::size_t... Ns> RefArray<T, (Ns + ...)> concatenate(const RefArray<T, Ns>&... ref_arrays) | ||
{ | ||
// Fold expression to calculate the total size of the new array using fold expression | ||
constexpr std::size_t TotalSize = (Ns + ...); | ||
std::array<T*, TotalSize> concatenated; | ||
|
||
std::size_t offset = 0; | ||
// Copies elements from a given RefArray to the concatenated array | ||
auto copy_into = [&](const auto& ref_array, std::size_t& offset) { | ||
for (std::size_t i = 0; i < ref_array.size(); ++i) { | ||
concatenated[offset + i] = &ref_array[i]; | ||
} | ||
offset += ref_array.size(); | ||
}; | ||
|
||
// Fold expression to copy elements from each input RefArray to the concatenated array | ||
(..., copy_into(ref_arrays, offset)); | ||
|
||
return RefArray<T, TotalSize>{ concatenated }; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GCC rightfully caught this is no longer initialized. Plan to do a pass on this with AztecProtocol/barretenberg#793