-
Notifications
You must be signed in to change notification settings - Fork 327
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: k-shifts #11663
Merged
Merged
feat: k-shifts #11663
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
e1c2a35
right shift method plus test
ledwards2225 8d73f02
Merge branch 'master' into lde/concatenate_5
ledwards2225 8701e1e
cleanup: use poly batcher in test util where possible
ledwards2225 2584224
move claim batcher to its own file
ledwards2225 11a02d4
use claim batcher in gemini
ledwards2225 9948751
clean up mock witness gen a bit
ledwards2225 8ebc800
update naming
ledwards2225 74c0d8d
add slot ofr k shifts in claim gen
ledwards2225 592f8d1
some reorg
ledwards2225 1c9cb0a
revamp
ledwards2225 b062474
add k shifts to poly batcher; utterly untested
ledwards2225 ae0c631
test logic for gemini k shifts but gem verifier not updated
ledwards2225 df411d7
passes one of the not so meaningful shplem tests
ledwards2225 d57120b
add k shifts to shplem tests by default
ledwards2225 3cfc8eb
update rec verifier test
ledwards2225 ff70aca
allow designated initializer in wasm
ledwards2225 6518bd9
gemini will not independently support k shifts
ledwards2225 58ebe5a
cleauyp/comments
ledwards2225 1d9f88b
comment correction
ledwards2225 6ed50ea
comment update
ledwards2225 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
barretenberg/cpp/src/barretenberg/commitment_schemes/claim_batcher.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
#pragma once | ||
#include "barretenberg/common/ref_vector.hpp" | ||
#include <optional> | ||
|
||
namespace bb { | ||
|
||
/** | ||
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. This class was added in a prior PR, I'm just moving it to its own file. The only changes are those associated with the right shift by k claims. |
||
* @brief Logic to support batching opening claims for unshifted and shifted polynomials in Shplemini | ||
* @details Stores references to the commitments/evaluations of unshifted and shifted polynomials to be batched | ||
* opened via Shplemini. Aggregates the commitments and batching scalars for each batch into the corresponding | ||
* containers for Shplemini. Computes the batched evaluation. Contains logic for computing the per-batch scalars | ||
* used to batch each set of claims (see details below). | ||
* @note This class performs the actual batching of the evaluations but not of the commitments. The latter are | ||
* simply appended to a larger container, along with the scalars used to batch them. This is because Shplemini | ||
* is optimized to perform a single batch mul that includes all commitments from each stage of the PCS. See | ||
* description of ShpleminiVerifier for more details. | ||
* | ||
*/ | ||
template <typename Curve> struct ClaimBatcher_ { | ||
using Fr = typename Curve::ScalarField; | ||
using Commitment = typename Curve::AffineElement; | ||
|
||
struct Batch { | ||
RefVector<Commitment> commitments; | ||
RefVector<Fr> evaluations; | ||
// scalar used for batching the claims, excluding the power of batching challenge \rho | ||
Fr scalar = 0; | ||
}; | ||
|
||
std::optional<Batch> unshifted; // commitments and evaluations of unshifted polynomials | ||
std::optional<Batch> shifted; // commitments of to-be-shifted-by-1 polys, evals of their shifts | ||
std::optional<Batch> right_shifted_by_k; // commitments of to-be-right-shifted-by-k polys, evals of their shifts | ||
|
||
Batch get_unshifted() { return (unshifted) ? *unshifted : Batch{}; } | ||
Batch get_shifted() { return (shifted) ? *shifted : Batch{}; } | ||
Batch get_right_shifted_by_k() { return (right_shifted_by_k) ? *right_shifted_by_k : Batch{}; } | ||
|
||
size_t k_shift_magnitude = 0; // magnitude of right-shift-by-k (assumed even) | ||
|
||
Fr get_unshifted_batch_scalar() const { return unshifted ? unshifted->scalar : Fr{ 0 }; } | ||
|
||
/** | ||
* @brief Compute scalars used to batch each set of claims, excluding contribution from batching challenge \rho | ||
* @details Computes scalars s_0, s_1, s_2 given by | ||
* \f[ | ||
* - s_0 = \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right) \f], | ||
* - s_1 = \frac{1}{r} \times \left(\frac{1}{z-r} - \nu \times \frac{1}{z+r}\right) | ||
* - s_2 = r^{k} \times \left(\frac{1}{z-r} + \nu \times \frac{1}{z+r}\right) | ||
* \f] | ||
* where the scalars used to batch the claims are given by | ||
* \f[ | ||
* \left( | ||
* - s_0, | ||
* \ldots, | ||
* - \rho^{i+k-1} \times s_0, | ||
* - \rho^{i+k} \times s_1, | ||
* \ldots, | ||
* - \rho^{k+m-1} \times s_1 | ||
* \right) | ||
* \f] | ||
* | ||
* @param inverse_vanishing_eval_pos 1/(z-r) | ||
* @param inverse_vanishing_eval_neg 1/(z+r) | ||
* @param nu_challenge ν (shplonk batching challenge) | ||
* @param r_challenge r (gemini evaluation challenge) | ||
*/ | ||
void compute_scalars_for_each_batch(const Fr& inverse_vanishing_eval_pos, | ||
const Fr& inverse_vanishing_eval_neg, | ||
const Fr& nu_challenge, | ||
const Fr& r_challenge) | ||
{ | ||
if (unshifted) { | ||
// (1/(z−r) + ν/(z+r)) | ||
unshifted->scalar = inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg; | ||
} | ||
if (shifted) { | ||
// r⁻¹ ⋅ (1/(z−r) − ν/(z+r)) | ||
shifted->scalar = | ||
r_challenge.invert() * (inverse_vanishing_eval_pos - nu_challenge * inverse_vanishing_eval_neg); | ||
} | ||
if (right_shifted_by_k) { | ||
// r^k ⋅ (1/(z−r) + ν/(z+r)) | ||
right_shifted_by_k->scalar = r_challenge.pow(k_shift_magnitude) * | ||
(inverse_vanishing_eval_pos + nu_challenge * inverse_vanishing_eval_neg); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Append the commitments and scalars from each batch of claims to the Shplemini batch mul input vectors; | ||
* update the batched evaluation and the running batching challenge (power of rho) in place. | ||
* | ||
* @param commitments commitment inputs to the single Shplemini batch mul | ||
* @param scalars scalar inputs to the single Shplemini batch mul | ||
* @param batched_evaluation running batched evaluation of the committed multilinear polynomials | ||
* @param rho multivariate batching challenge \rho | ||
* @param rho_power current power of \rho used in the batching scalar | ||
*/ | ||
void update_batch_mul_inputs_and_batched_evaluation(std::vector<Commitment>& commitments, | ||
std::vector<Fr>& scalars, | ||
Fr& batched_evaluation, | ||
const Fr& rho, | ||
Fr& rho_power) | ||
{ | ||
// Append the commitments/scalars from a given batch to the corresponding containers; update the batched | ||
// evaluation and the running batching challenge in place | ||
auto aggregate_claim_data_and_update_batched_evaluation = [&](const Batch& batch, Fr& rho_power) { | ||
for (auto [commitment, evaluation] : zip_view(batch.commitments, batch.evaluations)) { | ||
commitments.emplace_back(std::move(commitment)); | ||
scalars.emplace_back(-batch.scalar * rho_power); | ||
batched_evaluation += evaluation * rho_power; | ||
rho_power *= rho; | ||
} | ||
}; | ||
|
||
// Incorporate the claim data from each batch of claims that is present | ||
if (unshifted) { | ||
aggregate_claim_data_and_update_batched_evaluation(*unshifted, rho_power); | ||
} | ||
if (shifted) { | ||
aggregate_claim_data_and_update_batched_evaluation(*shifted, rho_power); | ||
} | ||
if (right_shifted_by_k) { | ||
aggregate_claim_data_and_update_batched_evaluation(*right_shifted_by_k, rho_power); | ||
} | ||
} | ||
}; | ||
|
||
} // namespace bb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This was added after a discussion with Adam. IIUC this is part of Charlies incoming PR anyway.