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 // Masking challenge is used in the recursive setting to perform batch_mul. This is not used
66 // by the prover directly, we just need it for consistent transcript state with the verifier.
67 prover_trancript->template get_challenge<Fr>("KZG:masking_challenge");
68 };
69
80 template <typename Transcript>
82 const std::shared_ptr<Transcript>& verifier_transcript)
83 {
84 auto quotient_commitment = verifier_transcript->template receive_from_prover<Commitment>("KZG:W");
85
86 // Note: The pairing check can be expressed naturally as
87 // 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
88 // 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
89 GroupElement P_0;
90 if constexpr (Curve::is_stdlib_type) {
91 // Express operation as a batch_mul in order to use Goblinization if available
92 auto builder = quotient_commitment.get_context();
93 auto one = Fr(builder, 1);
94 std::vector<GroupElement> commitments = { claim.commitment,
95 quotient_commitment,
96 GroupElement::one(builder) };
97 std::vector<Fr> scalars = { one, claim.opening_pair.challenge, -claim.opening_pair.evaluation };
98 P_0 = GroupElement::batch_mul(commitments, scalars);
99
100 } else {
101 P_0 = claim.commitment;
102 P_0 += quotient_commitment * claim.opening_pair.challenge;
103 P_0 -= GroupElement::one() * claim.opening_pair.evaluation;
104 }
105
106 auto P_1 = -quotient_commitment;
107 return PairingPointsType(P_0, P_1);
108 };
109
129 template <typename Transcript>
131 const std::shared_ptr<Transcript>& transcript,
132 const size_t expected_final_msm_size = 0)
133 {
134 auto quotient_commitment = transcript->template receive_from_prover<Commitment>("KZG:W");
135
136 // This challenge is used to compute offset generators in the batch_mul call below
137 const Fr masking_challenge = transcript->template get_challenge<Fr>("KZG:masking_challenge");
138
139 // The pairing check can be expressed as
140 // e(C + [W]₁ ⋅ z, [1]₂) * e(−[W]₁, [X]₂) = 1, where C = ∑ commitmentsᵢ ⋅ scalarsᵢ.
141 GroupElement P_0;
142 // Place the commitment to W to 'commitments'
143 batch_opening_claim.commitments.emplace_back(quotient_commitment);
144 // Update the scalars by adding the Shplonk evaluation challenge z
145 batch_opening_claim.scalars.emplace_back(batch_opening_claim.evaluation_point);
146
147 // Validate the final MSM size if expected size is provided
148 if (expected_final_msm_size != 0) {
149 BB_ASSERT_EQ(batch_opening_claim.commitments.size(), expected_final_msm_size);
150 }
151
152 // Compute C + [W]₁ ⋅ z
153 P_0 = GroupElement::batch_mul(batch_opening_claim.commitments,
154 batch_opening_claim.scalars,
155 /*max_num_bits=*/0,
156 /*with_edgecases=*/true,
157 /*masking_scalar=*/masking_challenge);
158 auto P_1 = -quotient_commitment;
159
160 return PairingPointsType(P_0, P_1);
161 }
162};
163
164} // 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:81
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:130
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:53
OpeningPair< Curve > opening_pair
Definition claim.hpp:62
Commitment commitment
Definition claim.hpp:64
Opening pair (r,v) for some witness polynomial p(X) such that p(r) = v.
Definition claim.hpp:19
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:34
Polynomial polynomial
Definition claim.hpp:39
OpeningPair< Curve > opening_pair
Definition claim.hpp:40
Representation of the Grumpkin Verifier Commitment Key inside a bn254 circuit.
typename Group::element Element
Definition grumpkin.hpp:62
static constexpr bool is_stdlib_type
Definition grumpkin.hpp:69
typename Group::affine_element AffineElement
Definition grumpkin.hpp:63
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:151