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
15
16#include <memory>
17#include <utility>
18
19namespace bb {
20
21template <typename Curve_> class KZG {
22 public:
23 using Curve = Curve_;
26 using Fr = typename Curve::ScalarField;
28 using GroupElement = typename Curve::Element;
32
41 template <typename Transcript>
42 static void compute_opening_proof(const CK& ck,
43 const ProverOpeningClaim<Curve>& opening_claim,
44 const std::shared_ptr<Transcript>& prover_trancript)
45 {
46 Polynomial quotient = opening_claim.polynomial;
47 OpeningPair<Curve> pair = opening_claim.opening_pair;
48 Commitment quotient_commitment;
49
50 if (opening_claim.polynomial.is_empty()) {
51 // We treat the empty polynomial as the zero polynomial
52 quotient_commitment = Commitment::infinity();
53 } else {
54 quotient.at(0) = quotient[0] - pair.evaluation;
55 // Computes the coefficients for the quotient polynomial q(X) = (p(X) - v) / (X - r) through an FFT
56 quotient.factor_roots(pair.challenge);
57 quotient_commitment = ck.commit(quotient);
58 }
59
60 // TODO(#479): for now we compute the KZG commitment directly to unify the KZG and IPA interfaces but in the
61 // future we might need to adjust this to use the incoming alternative to work queue (i.e. variation of
62 // pthreads) or even the work queue itself
63 prover_trancript->send_to_verifier("KZG:W", quotient_commitment);
64 };
65
76 template <typename Transcript>
78 const std::shared_ptr<Transcript>& verifier_transcript)
79 {
80 auto quotient_commitment = verifier_transcript->template receive_from_prover<Commitment>("KZG:W");
81
82 // Note: The pairing check can be expressed naturally as
83 // 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
84 // 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
85 GroupElement P_0;
86 if constexpr (Curve::is_stdlib_type) {
87 // Express operation as a batch_mul in order to use Goblinization if available
88 auto builder = quotient_commitment.get_context();
89 auto one = Fr(builder, 1);
90 std::vector<GroupElement> commitments = { claim.commitment,
91 quotient_commitment,
92 GroupElement::one(builder) };
93 std::vector<Fr> scalars = { one, claim.opening_pair.challenge, -claim.opening_pair.evaluation };
94 P_0 = GroupElement::batch_mul(commitments, scalars);
95
96 } else {
97 P_0 = claim.commitment;
98 P_0 += quotient_commitment * claim.opening_pair.challenge;
99 P_0 -= GroupElement::one() * claim.opening_pair.evaluation;
100 }
101
102 auto P_1 = -quotient_commitment;
103 return PairingPointsType(P_0, P_1);
104 };
105
125 template <typename Transcript>
127 const std::shared_ptr<Transcript>& transcript,
128 const size_t expected_final_msm_size = 0)
129 {
130 auto quotient_commitment = transcript->template receive_from_prover<Commitment>("KZG:W");
131
132 // OriginTag suppression: The tag system flags patterns like A*α + B where A, B are
133 // prover-supplied and α is a challenge derived without hashing them. The quotient commitment
134 // W is prover-supplied and scaled by z in C + W·z, so it triggers this pattern.
135 // This is a false positive: the pairing check e(C + W·z, [1]₂) · e(−W, [x]₂) = 1 forces W
136 // to be the honest quotient commitment, so the prover cannot tamper with it.
137 // We assign W the tag of z (evaluation_point): the challenge it is scaled by,
138 // so the tag system does not flag the multiplication.
139 if constexpr (Curve::is_stdlib_type) {
140 const auto challenge_tag = batch_opening_claim.evaluation_point.get_origin_tag();
141 quotient_commitment.set_origin_tag(challenge_tag);
142 }
143
144 // The pairing check can be expressed as
145 // e(C + [W]₁ ⋅ z, [1]₂) * e(−[W]₁, [X]₂) = 1, where C = ∑ commitmentsᵢ ⋅ scalarsᵢ.
146 GroupElement P_0;
147 // Place the commitment to W to 'commitments'
148 batch_opening_claim.commitments.emplace_back(quotient_commitment);
149 // Update the scalars by adding the Shplonk evaluation challenge z
150 batch_opening_claim.scalars.emplace_back(batch_opening_claim.evaluation_point);
151
152 // Validate the final MSM size if expected size is provided
153 if (expected_final_msm_size != 0) {
154 BB_ASSERT_EQ(batch_opening_claim.commitments.size(), expected_final_msm_size);
155 }
156
157 // Compute C + [W]₁ ⋅ z
158 P_0 = GroupElement::batch_mul(batch_opening_claim.commitments,
159 batch_opening_claim.scalars,
160 /*max_num_bits=*/0,
161 /*with_edgecases=*/true);
162 auto P_1 = -quotient_commitment;
163
164 return PairingPointsType(P_0, P_1);
165 }
166};
167
168} // namespace bb
#define BB_ASSERT_EQ(actual, expected,...)
Definition assert.hpp:83
CommitmentKey object over a pairing group 𝔾₁.
typename Curve::AffineElement Commitment
Definition kzg.hpp:27
typename Curve::Element GroupElement
Definition kzg.hpp:28
Curve_ Curve
Definition kzg.hpp:23
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:77
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:126
typename Curve::ScalarField Fr
Definition kzg.hpp:26
std::conditional_t< Curve::is_stdlib_type, stdlib::recursion::PairingPoints< Curve >, bb::PairingPoints< Curve > > PairingPointsType
Definition kzg.hpp:31
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:42
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:64
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:68
typename Group::affine_element AffineElement
Definition grumpkin.hpp:65
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
An accumulator consisting of the Shplonk evaluation challenge and vectors of commitments and scalars.
Definition claim.hpp:155