Barretenberg
The ZK-SNARK library at the core of Aztec
Loading...
Searching...
No Matches
kzg.hpp
Go to the documentation of this file.
1// === AUDIT STATUS ===
2// internal: { status: Complete, auditors: [Khashayar], commit: }
3// external_1: { status: not started, auditors: [], commit: }
4// external_2: { status: not started, auditors: [], commit: }
5// =====================
6
7#pragma once
8
16
17#include <memory>
18#include <utility>
19
20namespace bb {
21
22template <typename Curve_> class KZG {
23 public:
24 using Curve = Curve_;
27 using Fr = typename Curve::ScalarField;
29 using GroupElement = typename Curve::Element;
33
42 template <typename Transcript>
43 static void compute_opening_proof(const CK& ck,
44 const ProverOpeningClaim<Curve>& opening_claim,
45 const std::shared_ptr<Transcript>& prover_trancript)
46 {
47 BB_BENCH_NAME("KZG::compute_opening_proof");
48 Polynomial quotient = opening_claim.polynomial;
49 OpeningPair<Curve> pair = opening_claim.opening_pair;
50 Commitment quotient_commitment;
51
52 if (opening_claim.polynomial.is_empty()) {
53 // We treat the empty polynomial as the zero polynomial
54 quotient_commitment = Commitment::infinity();
55 } else {
56 quotient.at(0) = quotient[0] - pair.evaluation;
57 // Computes the coefficients for the quotient polynomial q(X) = (p(X) - v) / (X - r) through an FFT
58 quotient.factor_roots(pair.challenge);
59 quotient_commitment = ck.commit(quotient);
60 }
61
62 // TODO(#479): for now we compute the KZG commitment directly to unify the KZG and IPA interfaces but in the
63 // future we might need to adjust this to use the incoming alternative to work queue (i.e. variation of
64 // pthreads) or even the work queue itself
65 prover_trancript->send_to_verifier("KZG:W", quotient_commitment);
66 };
67
78 template <typename Transcript>
80 const std::shared_ptr<Transcript>& verifier_transcript)
81 {
82 auto quotient_commitment = verifier_transcript->template receive_from_prover<Commitment>("KZG:W");
83
84 // Note: The pairing check can be expressed naturally as
85 // e(C - v * [1]_1, [1]_2) = e([W]_1, [X - r]_2) where C =[p(X)]_1. This can be rearranged (e.g. see the plonk
86 // paper) as e(C + r*[W]_1 - v*[1]_1, [1]_2) * e(-[W]_1, [X]_2) = 1, or e(P_0, [1]_2) * e(P_1, [X]_2) = 1
87 GroupElement P_0;
88 if constexpr (Curve::is_stdlib_type) {
89 // Express operation as a batch_mul in order to use Goblinization if available
90 auto builder = quotient_commitment.get_context();
91 auto one = Fr(builder, 1);
92 std::vector<GroupElement> commitments = { claim.commitment,
93 quotient_commitment,
94 GroupElement::one(builder) };
95 std::vector<Fr> scalars = { one, claim.opening_pair.challenge, -claim.opening_pair.evaluation };
96
97 // Compute C + r*[W]_1 + (-v)*[1]_1 as a batch_mul, no need of edge case handling since we don't expect the
98 // points to be linearly dependent.
99 P_0 = GroupElement::batch_mul(commitments, scalars, /*max_num_bits=*/0, /*with_edgecases=*/false);
100
101 } else {
102 P_0 = claim.commitment;
103 P_0 += quotient_commitment * claim.opening_pair.challenge;
104 P_0 -= GroupElement::one() * claim.opening_pair.evaluation;
105 }
106
107 auto P_1 = -quotient_commitment;
108 return PairingPointsType(P_0, P_1);
109 };
110
130 template <typename Transcript>
132 const std::shared_ptr<Transcript>& transcript,
133 const size_t expected_final_msm_size = 0)
134 {
135 auto quotient_commitment = transcript->template receive_from_prover<Commitment>("KZG:W");
136
137 // OriginTag suppression: The tag system flags patterns like A*α + B where A, B are
138 // prover-supplied and α is a challenge derived without hashing them. The quotient commitment
139 // W is prover-supplied and scaled by z in C + W·z, so it triggers this pattern.
140 // This is a false positive: the pairing check e(C + W·z, [1]₂) · e(−W, [x]₂) = 1 forces W
141 // to be the honest quotient commitment, so the prover cannot tamper with it.
142 // We assign W the tag of z (evaluation_point): the challenge it is scaled by,
143 // so the tag system does not flag the multiplication.
144 if constexpr (Curve::is_stdlib_type) {
145 const auto challenge_tag = batch_opening_claim.evaluation_point.get_origin_tag();
146 quotient_commitment.set_origin_tag(challenge_tag);
147 }
148
149 // The pairing check can be expressed as
150 // e(C + [W]₁ ⋅ z, [1]₂) * e(−[W]₁, [X]₂) = 1, where C = ∑ commitmentsᵢ ⋅ scalarsᵢ.
151 GroupElement P_0;
152 // Place the commitment to W to 'commitments'
153 batch_opening_claim.commitments.emplace_back(quotient_commitment);
154 // Update the scalars by adding the Shplonk evaluation challenge z
155 batch_opening_claim.scalars.emplace_back(batch_opening_claim.evaluation_point);
156
157 // Validate the final MSM size if expected size is provided
158 if (expected_final_msm_size != 0) {
159 if (batch_opening_claim.commitments.size() != expected_final_msm_size) {
160 throw_or_abort("KZG verification: unexpected final MSM size " +
161 std::to_string(batch_opening_claim.commitments.size()) + " (expected " +
162 std::to_string(expected_final_msm_size) + ")");
163 }
164 }
165
166 // Compute C + [W]₁ ⋅ z
167 P_0 = GroupElement::batch_mul(batch_opening_claim.commitments,
168 batch_opening_claim.scalars,
169 /*max_num_bits=*/0,
170 /*with_edgecases=*/true);
171 auto P_1 = -quotient_commitment;
172
173 return PairingPointsType(P_0, P_1);
174 }
175};
176
177} // namespace bb
#define BB_BENCH_NAME(name)
Definition bb_bench.hpp:264
CommitmentKey object over a pairing group 𝔾₁.
typename Curve::AffineElement Commitment
Definition kzg.hpp:28
typename Curve::Element GroupElement
Definition kzg.hpp:29
Curve_ Curve
Definition kzg.hpp:24
static PairingPointsType reduce_verify(const OpeningClaim< Curve > &claim, const std::shared_ptr< Transcript > &verifier_transcript)
Computes the input points for the pairing check needed to verify a KZG opening claim of a single poly...
Definition kzg.hpp:79
static PairingPointsType reduce_verify_batch_opening_claim(BatchOpeningClaim< Curve > &&batch_opening_claim, const std::shared_ptr< Transcript > &transcript, const size_t expected_final_msm_size=0)
Computes the input points for the pairing check needed to verify a KZG opening claim obtained from a ...
Definition kzg.hpp:131
typename Curve::ScalarField Fr
Definition kzg.hpp:27
std::conditional_t< Curve::is_stdlib_type, stdlib::recursion::PairingPoints< Curve >, bb::PairingPoints< Curve > > PairingPointsType
Definition kzg.hpp:32
static void compute_opening_proof(const CK &ck, const ProverOpeningClaim< Curve > &opening_claim, const std::shared_ptr< Transcript > &prover_trancript)
Computes the KZG commitment to an opening proof polynomial at a single evaluation point.
Definition kzg.hpp:43
Unverified claim (C,r,v) for some witness polynomial p(X) such that.
Definition claim.hpp:55
OpeningPair< Curve > opening_pair
Definition claim.hpp:64
Commitment commitment
Definition claim.hpp:66
Opening pair (r,v) for some witness polynomial p(X) such that p(r) = v.
Definition claim.hpp:21
An object storing two EC points that represent the inputs to a pairing check.
Structured polynomial class that represents the coefficients 'a' of a_0 + a_1 x .....
bool is_empty() const
Fr & at(size_t index)
Our mutable accessor, unlike operator[]. We abuse precedent a bit to differentiate at() and operator[...
void factor_roots(const Fr &root)
Divides p(X) by (X-r) in-place. Assumes that p(rⱼ)=0 for all j.
Polynomial p and an opening pair (r,v) such that p(r) = v.
Definition claim.hpp:36
Polynomial polynomial
Definition claim.hpp:41
OpeningPair< Curve > opening_pair
Definition claim.hpp:42
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition grumpkin.hpp:63
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:67
typename Group::affine_element AffineElement
Definition grumpkin.hpp:64
AluTraceBuilder builder
Definition alu.test.cpp:124
Entry point for Barretenberg command-line interface.
Definition api.hpp:5
CommitmentKey< Curve > ck
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept
Definition tuple.hpp:13
std::string to_string(bb::avm2::ValueTag tag)
An accumulator consisting of the Shplonk evaluation challenge and vectors of commitments and scalars.
Definition claim.hpp:155
void throw_or_abort(std::string const &err)